OSDN Git Service

56221944a93a91d07e86db4ff69c57feeb22ef61
[hengbandforosx/hengbandosx.git] / src / object-enchant / others / apply-magic-others.cpp
1 /*!
2  * @brief 武器でも防具でもアクセサリでもない、その他のアイテム群を生成・強化する処理
3  * @date 2022/02/23
4  * @author Hourier
5  * @details 他との兼ね合いでEnchanterとなっているが、油つぼ・人形・死体・像は生成のみで強化はしない
6  */
7
8 #include "object-enchant/others/apply-magic-others.h"
9 #include "artifact/random-art-generator.h"
10 #include "game-option/cheat-options.h"
11 #include "inventory/inventory-slot-types.h"
12 #include "monster-floor/place-monster-types.h"
13 #include "monster-race/monster-race-hook.h"
14 #include "monster-race/monster-race.h"
15 #include "monster-race/race-indice-types.h"
16 #include "monster/monster-list.h"
17 #include "monster/monster-util.h"
18 #include "object-enchant/object-ego.h"
19 #include "object-enchant/tr-types.h"
20 #include "object-enchant/trc-types.h"
21 #include "object/tval-types.h"
22 #include "perception/object-perception.h"
23 #include "sv-definition/sv-lite-types.h"
24 #include "sv-definition/sv-other-types.h"
25 #include "system/baseitem-info.h"
26 #include "system/floor-type-definition.h"
27 #include "system/item-entity.h"
28 #include "system/monster-race-info.h"
29 #include "system/player-type-definition.h"
30 #include "util/bit-flags-calculator.h"
31 #include "view/display-messages.h"
32 #include <unordered_map>
33
34 /*!
35  * @brief コンストラクタ
36  * @param player_ptr プレイヤーへの参照ポインタ
37  * @param o_ptr 強化を与えたい/生成したいオブジェクトの構造体参照ポインタ
38  * @param power 生成ランク
39  * @details power > 2はデバッグ専用.
40  */
41 OtherItemsEnchanter::OtherItemsEnchanter(PlayerType *player_ptr, ItemEntity *o_ptr)
42     : player_ptr(player_ptr)
43     , o_ptr(o_ptr)
44 {
45 }
46
47 /*!
48  * @brief その他雑多のオブジェクトに生成ランクごとの強化を与える
49  * @details power > 2はデバッグ専用.
50  */
51 void OtherItemsEnchanter::apply_magic()
52 {
53     const auto tval = this->o_ptr->bi_key.tval();
54     switch (tval) {
55     case ItemKindType::FLASK:
56         this->o_ptr->fuel = this->o_ptr->pval;
57         this->o_ptr->pval = 0;
58         break;
59     case ItemKindType::WAND:
60     case ItemKindType::STAFF:
61         this->enchant_wand_staff();
62         break;
63     case ItemKindType::ROD:
64         this->o_ptr->pval = this->o_ptr->get_baseitem().pval;
65         break;
66     case ItemKindType::CAPTURE:
67         this->o_ptr->pval = 0;
68         object_aware(this->player_ptr, this->o_ptr);
69         this->o_ptr->mark_as_known();
70         break;
71     case ItemKindType::FIGURINE:
72         this->generate_figurine();
73         break;
74     case ItemKindType::CORPSE:
75         this->generate_corpse();
76         break;
77     case ItemKindType::STATUE:
78         this->generate_statue();
79         break;
80     case ItemKindType::CHEST:
81         this->generate_chest();
82         break;
83     default:
84         break;
85     }
86 }
87
88 /*
89  * @brief 杖を強化する
90  * The wand or staff gets a number of initial charges equal
91  * to between 1/2 (+1) and the full object kind's pval.
92  */
93 void OtherItemsEnchanter::enchant_wand_staff()
94 {
95     const auto &baseitem = this->o_ptr->get_baseitem();
96     this->o_ptr->pval = baseitem.pval / 2 + randint1((baseitem.pval + 1) / 2);
97 }
98
99 /*
100  * @brief ランダムに選択したモンスター種族IDからその人形を作る
101  * @details
102  * ツチノコの人形は作らない
103  * レアリティが1~100のものだけ生成対象になる
104  * レベルの高い人形ほど生成されにくい
105  * たまに呪われる
106  */
107 void OtherItemsEnchanter::generate_figurine()
108 {
109     auto *floor_ptr = this->player_ptr->current_floor_ptr;
110     MonsterRaceId r_idx;
111     while (true) {
112         r_idx = MonsterRace::pick_one_at_random();
113         if (!item_monster_okay(this->player_ptr, r_idx) || (r_idx == MonsterRaceId::TSUCHINOKO)) {
114             continue;
115         }
116
117         auto *r_ptr = &monraces_info[r_idx];
118         auto check = (floor_ptr->dun_level < r_ptr->level) ? (r_ptr->level - floor_ptr->dun_level) : 0;
119         if ((r_ptr->rarity == 0) || (r_ptr->rarity > 100) || (randint0(check) > 0)) {
120             continue;
121         }
122
123         break;
124     }
125
126     this->o_ptr->pval = enum2i(r_idx);
127     if (one_in_(6)) {
128         this->o_ptr->curse_flags.set(CurseTraitType::CURSED);
129     }
130 }
131
132 /*
133  * @brief ランダムに選択したモンスター種族IDからその死体/骨を作る
134  * @details
135  * そもそも死体も骨も落とさないモンスターは対象外
136  * ユニークやあやしい影等、そこらに落ちている死体としてふさわしくないものは弾く
137  * レアリティが1~100のものだけ生成対象になる (はず)
138  * レベルの高い死体/骨ほど生成されにくい
139  */
140 void OtherItemsEnchanter::generate_corpse()
141 {
142     const std::unordered_map<int, MonsterDropType> match = {
143         { SV_SKELETON, MonsterDropType::DROP_SKELETON },
144         { SV_CORPSE, MonsterDropType::DROP_CORPSE },
145     };
146
147     get_mon_num_prep(this->player_ptr, item_monster_okay, nullptr);
148     auto *floor_ptr = this->player_ptr->current_floor_ptr;
149     MonsterRaceId r_idx;
150     while (true) {
151         r_idx = get_mon_num(this->player_ptr, 0, floor_ptr->dun_level, PM_NONE);
152         auto &r_ref = monraces_info[r_idx];
153         auto check = (floor_ptr->dun_level < r_ref.level) ? (r_ref.level - floor_ptr->dun_level) : 0;
154         const auto sval = this->o_ptr->bi_key.sval();
155         if (!sval.has_value()) {
156             continue;
157         }
158
159         if ((r_ref.rarity == 0) || (match.find(sval.value()) != match.end() && r_ref.drop_flags.has_not(match.at(sval.value()))) || (randint0(check) > 0)) {
160             continue;
161         }
162
163         break;
164     }
165
166     this->o_ptr->pval = enum2i(r_idx);
167     object_aware(this->player_ptr, this->o_ptr);
168     this->o_ptr->mark_as_known();
169 }
170
171 /*
172  * @brief ランダムに選択したモンスター種族IDからその像を作る
173  * @details レアリティが1以上のものだけ生成対象になる
174  */
175 void OtherItemsEnchanter::generate_statue()
176 {
177     auto pick_r_idx_for_statue = [] {
178         while (true) {
179             auto r_idx = MonsterRace::pick_one_at_random();
180             if (monraces_info[r_idx].rarity > 0) {
181                 return r_idx;
182             }
183         }
184     };
185     auto r_idx = pick_r_idx_for_statue();
186     auto *r_ptr = &monraces_info[r_idx];
187
188     this->o_ptr->pval = enum2i(r_idx);
189     if (cheat_peek) {
190         msg_format(_("%sの像", "Statue of %s"), r_ptr->name.data());
191     }
192
193     object_aware(this->player_ptr, this->o_ptr);
194     this->o_ptr->mark_as_known();
195 }
196
197 /*
198  * @brief 箱を生成する
199  * @details 箱にはレベルがあり、箱の召喚トラップが発動すると箱レベルと同等のモンスターが召喚される
200  */
201 void OtherItemsEnchanter::generate_chest()
202 {
203     auto obj_level = this->o_ptr->get_baseitem().level;
204     if (obj_level <= 0) {
205         return;
206     }
207
208     this->o_ptr->pval = randint1(obj_level);
209     if (this->o_ptr->bi_key.sval() == SV_CHEST_KANDUME) {
210         this->o_ptr->pval = 6;
211     }
212
213     this->o_ptr->chest_level = this->player_ptr->current_floor_ptr->dun_level + 5;
214     if (this->o_ptr->pval > 55) {
215         this->o_ptr->pval = 55 + randint0(5);
216     }
217 }