OSDN Git Service

[Refactor] #2124 Changed struct object_type to class ObjectType
[hengbandforosx/hengbandosx.git] / src / artifact / fixed-art-generator.cpp
1 /*!
2  * @file fixed-art-generator.cpp
3  * @brief 固定アーティファクトの生成 / Artifact code
4  * @date 2020/07/14
5  * @author
6  * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
7  * This software may be copied and distributed for educational, research, and
8  * not for profit purposes provided that this copyright and statement are
9  * included in all such copies.
10  * 2013 Deskull rearranged comment for Doxygen.
11  * 2020 Hourier rearranged
12  */
13
14 #include "artifact/fixed-art-generator.h"
15 #include "artifact/fixed-art-types.h"
16 #include "floor/floor-object.h"
17 #include "object-enchant/object-boost.h"
18 #include "object-enchant/object-curse.h"
19 #include "object-enchant/special-object-flags.h"
20 #include "object-enchant/tr-types.h"
21 #include "object-enchant/trc-types.h"
22 #include "object-enchant/trg-types.h"
23 #include "object/object-kind-hook.h"
24 #include "object/object-kind.h"
25 #include "player-base/player-class.h"
26 #include "player/player-sex.h"
27 #include "specific-object/bloody-moon.h"
28 #include "system/artifact-type-definition.h"
29 #include "system/floor-type-definition.h"
30 #include "system/object-type-definition.h"
31 #include "system/player-type-definition.h"
32 #include "util/bit-flags-calculator.h"
33
34 /*!
35  * @brief 恐怖の仮面への特殊処理
36  * @param player_ptr プレイヤーへの参照ポインタ
37  * @param o_ptr 対象のオブジェクト構造体への参照ポインタ
38  * @return 追加能力/耐性がもらえるならtrue、もらえないならfalse
39  * @details
40  * 純戦士系職業は追加能力/耐性がもらえる。
41  * それ以外では、反感、太古の怨念、呪いが付き追加能力/耐性はもらえない。
42  */
43 static bool invest_terror_mask(PlayerType *player_ptr, ObjectType *o_ptr)
44 {
45     if (o_ptr->name1 != ART_TERROR)
46         return false;
47
48     switch (player_ptr->pclass) {
49     case PlayerClassType::WARRIOR:
50     case PlayerClassType::ARCHER:
51     case PlayerClassType::CAVALRY:
52     case PlayerClassType::BERSERKER:
53         return true;
54     default:
55         o_ptr->art_flags.set(TR_AGGRAVATE);
56         o_ptr->art_flags.set(TR_TY_CURSE);
57         o_ptr->curse_flags.set({ CurseTraitType::CURSED, CurseTraitType::HEAVY_CURSE });
58         o_ptr->curse_flags.set(get_curse(2, o_ptr));
59         return false;
60     }
61 }
62
63 /*!
64  * @brief 戦乙女ミリムの危ない水着への特殊処理 (セクシーギャルのみpval追加)
65  * @param player_ptr プレイヤーへの参照ポインタ
66  * @param o_ptr 対象のオブジェクト構造体への参照ポインタ
67  */
68 static void milim_swimsuit(PlayerType *player_ptr, ObjectType *o_ptr)
69 {
70     if ((o_ptr->name1 != ART_MILIM) || (player_ptr->ppersonality != PERSONALITY_SEXY))
71         return;
72
73     o_ptr->pval = 3;
74     o_ptr->art_flags.set(TR_STR);
75     o_ptr->art_flags.set(TR_INT);
76     o_ptr->art_flags.set(TR_WIS);
77     o_ptr->art_flags.set(TR_DEX);
78     o_ptr->art_flags.set(TR_CON);
79     o_ptr->art_flags.set(TR_CHR);
80 }
81
82 /*!
83  * @brief 特定の固定アーティファクトの条件付き追加能力/耐性を付加する
84  * @attention プレイヤーの各種ステータスに依存した処理がある。
85  * @todo 折を見て関数名を変更すること。
86  * @param player_ptr プレイヤーへの参照ポインタ
87  * @param o_ptr 対象のオブジェクト構造体ポインタ
88  * @param a_ptr 生成する固定アーティファクト構造体ポインタ
89  * @details
90  * 対象は村正、ロビントンのハープ、龍争虎鬪、ブラッディムーン、羽衣、天女の羽衣、ミリム
91  */
92 static void invest_special_artifact_abilities(PlayerType *player_ptr, ObjectType *o_ptr)
93 {
94     const auto pc = PlayerClass(player_ptr);
95     switch (o_ptr->name1) {
96     case ART_MURAMASA:
97         if (!pc.equals(PlayerClassType::SAMURAI)) {
98             o_ptr->art_flags.set(TR_NO_MAGIC);
99             o_ptr->curse_flags.set(CurseTraitType::HEAVY_CURSE);
100         }
101         return;
102     case ART_ROBINTON:
103         if (pc.equals(PlayerClassType::BARD))
104             o_ptr->art_flags.set(TR_DEC_MANA);
105         return;
106     case ART_XIAOLONG:
107         if (pc.equals(PlayerClassType::MONK))
108             o_ptr->art_flags.set(TR_BLOWS);
109         return;
110     case ART_BLOOD:
111         get_bloody_moon_flags(o_ptr);
112         return;
113     case ART_HEAVENLY_MAIDEN:
114         if (player_ptr->psex != SEX_FEMALE)
115             o_ptr->art_flags.set(TR_AGGRAVATE);
116         return;
117     case ART_MILIM:
118         milim_swimsuit(player_ptr, o_ptr);
119         return;
120     default:
121         break;
122     }
123 }
124
125 /*!
126  * @brief 固定アーティファクトオブジェクトに追加能力/耐性を付加する
127  * @param player_ptr プレイヤー情報への参照ポインタ
128  * @param a_ptr 固定アーティファクト情報への参照ポインタ
129  * @param q_ptr オブジェクト情報への参照ポインタ
130  */
131 static void fixed_artifact_random_abilities(PlayerType *player_ptr, artifact_type *a_ptr, ObjectType *o_ptr)
132 {
133     auto give_power = false;
134     auto give_resistance = false;
135
136     if (invest_terror_mask(player_ptr, o_ptr)) {
137         give_power = true;
138         give_resistance = true;
139     }
140
141     invest_special_artifact_abilities(player_ptr, o_ptr);
142
143     if (a_ptr->gen_flags.has(ItemGenerationTraitType::XTRA_POWER))
144         give_power = true;
145
146     if (a_ptr->gen_flags.has(ItemGenerationTraitType::XTRA_H_RES))
147         give_resistance = true;
148
149     if (a_ptr->gen_flags.has(ItemGenerationTraitType::XTRA_RES_OR_POWER)) {
150         if (one_in_(2))
151             give_resistance = true;
152         else
153             give_power = true;
154     }
155
156     if (give_power)
157         one_ability(o_ptr);
158
159     if (give_resistance)
160         one_high_resistance(o_ptr);
161
162     if (a_ptr->gen_flags.has(ItemGenerationTraitType::XTRA_DICE)) {
163         do {
164             o_ptr->dd++;
165         } while (one_in_(o_ptr->dd));
166
167         if (o_ptr->dd > 9)
168             o_ptr->dd = 9;
169     }
170 }
171
172 /*!
173  * @brief 固定アーティファクトオブジェクトに呪いフラグを付加する
174  * @param player_ptr プレイヤー情報への参照ポインタ
175  * @param a_ptr 固定アーティファクト情報への参照ポインタ
176  * @param q_ptr オブジェクト情報への参照ポインタ
177  */
178 static void invest_curse_to_fixed_artifact(artifact_type *a_ptr, ObjectType *o_ptr)
179 {
180     if (!a_ptr->cost)
181         set_bits(o_ptr->ident, IDENT_BROKEN);
182
183     if (a_ptr->gen_flags.has(ItemGenerationTraitType::CURSED))
184         o_ptr->curse_flags.set(CurseTraitType::CURSED);
185
186     if (a_ptr->gen_flags.has(ItemGenerationTraitType::HEAVY_CURSE))
187         o_ptr->curse_flags.set(CurseTraitType::HEAVY_CURSE);
188
189     if (a_ptr->gen_flags.has(ItemGenerationTraitType::PERMA_CURSE))
190         o_ptr->curse_flags.set(CurseTraitType::PERMA_CURSE);
191
192     if (a_ptr->gen_flags.has(ItemGenerationTraitType::RANDOM_CURSE0))
193         o_ptr->curse_flags.set(get_curse(0, o_ptr));
194
195     if (a_ptr->gen_flags.has(ItemGenerationTraitType::RANDOM_CURSE1))
196         o_ptr->curse_flags.set(get_curse(1, o_ptr));
197
198     if (a_ptr->gen_flags.has(ItemGenerationTraitType::RANDOM_CURSE2))
199         o_ptr->curse_flags.set(get_curse(2, o_ptr));
200 }
201
202 /*!
203  * @brief オブジェクトに指定した固定アーティファクトをオブジェクトに割り当てる。
204  * @param player_ptr プレイヤーへの参照ポインタ
205  * @param o_ptr 生成に割り当てたいオブジェクトの構造体参照ポインタ
206  * @return 適用したアーティファクト情報への参照ポインタ
207  */
208 artifact_type *apply_artifact(PlayerType *player_ptr, ObjectType *o_ptr)
209 {
210     auto a_ptr = &a_info[o_ptr->name1];
211     o_ptr->pval = a_ptr->pval;
212     o_ptr->ac = a_ptr->ac;
213     o_ptr->dd = a_ptr->dd;
214     o_ptr->ds = a_ptr->ds;
215     o_ptr->to_a = a_ptr->to_a;
216     o_ptr->to_h = a_ptr->to_h;
217     o_ptr->to_d = a_ptr->to_d;
218     o_ptr->weight = a_ptr->weight;
219     o_ptr->activation_id = a_ptr->act_idx;
220
221     invest_curse_to_fixed_artifact(a_ptr, o_ptr);
222     fixed_artifact_random_abilities(player_ptr, a_ptr, o_ptr);
223
224     return a_ptr;
225 }
226
227 /*!
228  * @brief フロアの指定された位置に固定アーティファクトを生成する。 / Create the artifact of the specified number
229  * @details 固定アーティファクト構造体から基本ステータスをコピーした後、所定の座標でdrop_item()で落とす。
230  * @param player_ptr プレイヤーへの参照ポインタ
231  * @param a_idx 生成する固定アーティファクト構造体のID
232  * @param y アイテムを落とす地点のy座標
233  * @param x アイテムを落とす地点のx座標
234  * @return 生成が成功したか否か、失敗はIDの不全、ベースアイテムの不全、drop_item()の失敗時に起こる。
235  * @attention この処理はdrop_near()内で普通の固定アーティファクトが重ならない性質に依存する.
236  * 仮に2個以上存在可能かつ装備品以外の固定アーティファクトが作成されれば
237  * drop_near()関数の返り値は信用できなくなる.
238  */
239 bool create_named_art(PlayerType *player_ptr, ARTIFACT_IDX a_idx, POSITION y, POSITION x)
240 {
241     auto a_ptr = &a_info[a_idx];
242     if (a_ptr->name.empty())
243         return false;
244
245     auto i = lookup_kind(a_ptr->tval, a_ptr->sval);
246     if (i == 0)
247         return true;
248
249     ObjectType forge;
250     auto q_ptr = &forge;
251     q_ptr->prep(i);
252     q_ptr->name1 = a_idx;
253
254     (void)apply_artifact(player_ptr, q_ptr);
255
256     return drop_near(player_ptr, q_ptr, -1, y, x) > 0;
257 }
258
259 /*!
260  * @brief 非INSTA_ART型の固定アーティファクトの生成を確率に応じて試行する。
261  * Mega-Hack -- Attempt to create one of the "Special Objects"
262  * @param player_ptr プレイヤーへの参照ポインタ
263  * @param o_ptr 生成に割り当てたいオブジェクトの構造体参照ポインタ
264  * @return 生成に成功したらTRUEを返す。
265  * @details
266  * Attempt to change an object into an artifact\n
267  * This routine should only be called by "apply_magic()"\n
268  * Note -- see "make_artifact_special()" and "apply_magic()"\n
269  */
270 bool make_artifact(PlayerType *player_ptr, ObjectType *o_ptr)
271 {
272     auto floor_ptr = player_ptr->current_floor_ptr;
273     if (floor_ptr->dun_level == 0)
274         return false;
275
276     if (o_ptr->number != 1)
277         return false;
278
279     for (const auto &a_ref : a_info) {
280         if (a_ref.name.empty())
281             continue;
282
283         if (a_ref.cur_num)
284             continue;
285
286         if (a_ref.gen_flags.has(ItemGenerationTraitType::QUESTITEM))
287             continue;
288
289         if (a_ref.gen_flags.has(ItemGenerationTraitType::INSTA_ART))
290             continue;
291
292         if (a_ref.tval != o_ptr->tval)
293             continue;
294
295         if (a_ref.sval != o_ptr->sval)
296             continue;
297
298         if (a_ref.level > floor_ptr->dun_level) {
299             int d = (a_ref.level - floor_ptr->dun_level) * 2;
300             if (!one_in_(d))
301                 continue;
302         }
303
304         if (!one_in_(a_ref.rarity))
305             continue;
306
307         o_ptr->name1 = a_ref.idx;
308         return true;
309     }
310
311     return false;
312 }
313
314 /*!
315  * @brief INSTA_ART型の固定アーティファクトの生成を確率に応じて試行する。
316  * Mega-Hack -- Attempt to create one of the "Special Objects"
317  * @param player_ptr プレイヤーへの参照ポインタ
318  * @param o_ptr 生成に割り当てたいオブジェクトの構造体参照ポインタ
319  * @return 生成に成功したらTRUEを返す。
320  * @details
321  * We are only called from "make_object()", and we assume that\n
322  * "apply_magic()" is called immediately after we return.\n
323  *\n
324  * Note -- see "make_artifact()" and "apply_magic()"\n
325  */
326 bool make_artifact_special(PlayerType *player_ptr, ObjectType *o_ptr)
327 {
328     KIND_OBJECT_IDX k_idx = 0;
329
330     /*! @note 地上ではキャンセルする / No artifacts in the town */
331     auto floor_ptr = player_ptr->current_floor_ptr;
332     if (floor_ptr->dun_level == 0)
333         return false;
334
335     /*! @note get_obj_num_hookによる指定がある場合は生成をキャンセルする / Themed object */
336     if (get_obj_num_hook)
337         return false;
338
339     /*! @note 全固定アーティファクト中からIDの若い順に生成対象とその確率を走査する / Check the artifact list (just the "specials") */
340     for (const auto &a_ref : a_info) {
341         /*! @note アーティファクト名が空の不正なデータは除外する / Skip "empty" artifacts */
342         if (a_ref.name.empty())
343             continue;
344
345         /*! @note 既に生成回数がカウントされたアーティファクト、QUESTITEMと非INSTA_ARTは除外 / Cannot make an artifact twice */
346         if (a_ref.cur_num)
347             continue;
348         if (a_ref.gen_flags.has(ItemGenerationTraitType::QUESTITEM))
349             continue;
350         if (!(a_ref.gen_flags.has(ItemGenerationTraitType::INSTA_ART)))
351             continue;
352
353         /*! @note アーティファクト生成階が現在に対して足りない場合は高確率で1/(不足階層*2)を満たさないと生成リストに加えられない /
354          *  XXX XXX Enforce minimum "depth" (loosely) */
355         if (a_ref.level > floor_ptr->object_level) {
356             /* @note  / Acquire the "out-of-depth factor". Roll for out-of-depth creation. */
357             int d = (a_ref.level - floor_ptr->object_level) * 2;
358             if (!one_in_(d))
359                 continue;
360         }
361
362         /*! @note 1/(レア度)の確率を満たさないと除外される / Artifact "rarity roll" */
363         if (!one_in_(a_ref.rarity))
364             continue;
365
366         /*! @note INSTA_ART型固定アーティファクトのベースアイテムもチェック対象とする。ベースアイテムの生成階層が足りない場合1/(不足階層*5)
367          * を満たさないと除外される。 / Find the base object. XXX XXX Enforce minimum "object" level (loosely). Acquire the "out-of-depth factor". Roll for
368          * out-of-depth creation. */
369         k_idx = lookup_kind(a_ref.tval, a_ref.sval);
370         if (k_info[k_idx].level > floor_ptr->object_level) {
371             int d = (k_info[k_idx].level - floor_ptr->object_level) * 5;
372             if (!one_in_(d))
373                 continue;
374         }
375
376         /*! @note 前述の条件を満たしたら、後のIDのアーティファクトはチェックせずすぐ確定し生成処理に移す /
377          * Assign the template. Mega-Hack -- mark the item as an artifact. Hack: Some artifacts get random extra powers. Success. */
378         o_ptr->prep(k_idx);
379
380         o_ptr->name1 = a_ref.idx;
381         return true;
382     }
383
384     /*! @note 全INSTA_ART固定アーティファクトを試行しても決まらなかった場合 FALSEを返す / Failure */
385     return false;
386 }