OSDN Git Service

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