OSDN Git Service

[Refactor] #2205 Separated enchant_chest() from OtherItemsEnchanter::apply_magic()
[hengbandforosx/hengbandosx.git] / src / object-enchant / others / apply-magic-others.cpp
1 /*!
2  * @brief 防具系のアイテムを強化して(恐らく床に)生成する処理
3  * @date 2020/06/02
4  * @author Hourier
5  * @todo ちょっと長い。要分割
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-flags9.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/object-kind.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/floor-type-definition.h"
26 #include "system/monster-race-definition.h"
27 #include "system/object-type-definition.h"
28 #include "system/player-type-definition.h"
29 #include "util/bit-flags-calculator.h"
30 #include "view/display-messages.h"
31
32 OtherItemsEnchanter::OtherItemsEnchanter(PlayerType *player_ptr, ObjectType *o_ptr)
33     : player_ptr(player_ptr)
34     , o_ptr(o_ptr)
35 {
36 }
37
38 /*!
39  * @brief その他雑多のオブジェクトに生成ランクごとの強化を与えるサブルーチン
40  * Apply magic to an item known to be "boring"
41  * @param player_ptr プレイヤーへの参照ポインタ
42  * @param o_ptr 強化を与えたいオブジェクトの構造体参照ポインタ
43  * @param power 生成ランク
44  * @details power > 2はデバッグ専用.
45  */
46 void OtherItemsEnchanter::apply_magic()
47 {
48     switch (this->o_ptr->tval) {
49     case ItemKindType::FLASK:
50         this->o_ptr->fuel = this->o_ptr->pval;
51         this->o_ptr->pval = 0;
52         break;
53     case ItemKindType::WAND:
54     case ItemKindType::STAFF:
55         this->enchant_wand_staff();
56         break;
57     case ItemKindType::ROD:
58         this->o_ptr->pval = k_info[this->o_ptr->k_idx].pval;
59         break;
60     case ItemKindType::CAPTURE:
61         this->o_ptr->pval = 0;
62         object_aware(this->player_ptr, this->o_ptr);
63         object_known(this->o_ptr);
64         break;
65     case ItemKindType::FIGURINE:
66         this->enchant_figurine();
67         break;
68     case ItemKindType::CORPSE:
69         this->enchant_corpse();
70         break;
71     case ItemKindType::STATUE:
72         this->enchant_statue();
73         break;
74     case ItemKindType::CHEST:
75         this->enchant_chest();
76         break;
77     default:
78         break;
79     }
80 }
81
82 /*
83  * @brief 杖を強化する
84  * The wand or staff gets a number of initial charges equal
85  * to between 1/2 (+1) and the full object kind's pval.
86  */
87 void OtherItemsEnchanter::enchant_wand_staff()
88 {
89     auto *k_ptr = &k_info[this->o_ptr->k_idx];
90     this->o_ptr->pval = k_ptr->pval / 2 + randint1((k_ptr->pval + 1) / 2);
91 }
92
93 void OtherItemsEnchanter::enchant_figurine()
94 {
95     auto *floor_ptr = this->player_ptr->current_floor_ptr;
96     short r_idx;
97     while (true)
98     {
99         r_idx = randint1(r_info.size() - 1);
100         if (!item_monster_okay(this->player_ptr, r_idx) || (r_idx == MON_TSUCHINOKO)) {
101             continue;
102         }
103
104         auto *r_ptr = &r_info[r_idx];
105         auto check = (floor_ptr->dun_level < r_ptr->level) ? (r_ptr->level - floor_ptr->dun_level) : 0;
106         if ((r_ptr->rarity == 0) || (r_ptr->rarity > 100) || (randint0(check) > 0)) {
107             continue;
108         }
109
110         break;
111     }
112
113     this->o_ptr->pval = r_idx;
114     if (one_in_(6)) {
115         this->o_ptr->curse_flags.set(CurseTraitType::CURSED);
116     }
117 }
118
119 void OtherItemsEnchanter::enchant_corpse()
120 {
121     uint32_t match = 0;
122     if (this->o_ptr->sval == SV_SKELETON) {
123         match = RF9_DROP_SKELETON;
124     } else if (this->o_ptr->sval == SV_CORPSE) {
125         match = RF9_DROP_CORPSE;
126     }
127
128     get_mon_num_prep(this->player_ptr, item_monster_okay, nullptr);
129     auto *floor_ptr = this->player_ptr->current_floor_ptr;
130     short r_idx;
131     while (true) {
132         r_idx = get_mon_num(this->player_ptr, 0, floor_ptr->dun_level, 0);
133         auto *r_ptr = &r_info[r_idx];
134         auto check = (floor_ptr->dun_level < r_ptr->level) ? (r_ptr->level - floor_ptr->dun_level) : 0;
135         if ((r_ptr->rarity == 0) || none_bits(r_ptr->flags9, match) || (randint0(check) > 0)) {
136             continue;
137         }
138
139         break;
140     }
141
142     this->o_ptr->pval = r_idx;
143     object_aware(this->player_ptr, this->o_ptr);
144     object_known(this->o_ptr);
145 }
146
147 void OtherItemsEnchanter::enchant_statue()
148 {
149     short r_idx;
150     auto &r_ref = r_info[0];
151     while (true) {
152         r_idx = randint1(r_info.size() - 1);
153         r_ref = r_info[r_idx];
154         if (r_ref.rarity == 0) {
155             continue;
156         }
157
158         break;
159     }
160
161     this->o_ptr->pval = r_idx;
162     if (cheat_peek) {
163         msg_format(_("%sの像", "Statue of %s"), r_ref.name.c_str());
164     }
165
166     object_aware(this->player_ptr, this->o_ptr);
167     object_known(this->o_ptr);
168 }
169
170 void OtherItemsEnchanter::enchant_chest()
171 {
172     auto obj_level = k_info[this->o_ptr->k_idx].level;
173     if (obj_level <= 0) {
174         return;
175     }
176
177     this->o_ptr->pval = randint1(obj_level);
178     if (this->o_ptr->sval == SV_CHEST_KANDUME) {
179         this->o_ptr->pval = 6;
180     }
181
182     this->o_ptr->chest_level = this->player_ptr->current_floor_ptr->dun_level + 5;
183     if (this->o_ptr->pval > 55) {
184         this->o_ptr->pval = 55 + randint0(5);
185     }
186 }