OSDN Git Service

[Refactor] #1489 Moved player-race.* and player-race-types.h to player-info/
[hengbandforosx/hengbandosx.git] / src / inventory / inventory-curse.cpp
1 #include "inventory/inventory-curse.h"
2 #include "artifact/fixed-art-types.h"
3 #include "core/asking-player.h"
4 #include "core/disturbance.h"
5 #include "core/player-redraw-types.h"
6 #include "core/player-update-types.h"
7 #include "flavor/flavor-describer.h"
8 #include "flavor/object-flavor-types.h"
9 #include "inventory/inventory-slot-types.h"
10 #include "io/files-util.h"
11 #include "monster-floor/monster-summon.h"
12 #include "monster-floor/place-monster-types.h"
13 #include "object-enchant/item-feeling.h"
14 #include "object-enchant/object-curse.h"
15 #include "object-enchant/special-object-flags.h"
16 #include "object-enchant/tr-types.h"
17 #include "object-enchant/trc-types.h"
18 #include "object/object-flags.h"
19 #include "perception/object-perception.h"
20 #include "player-info/race-types.h"
21 #include "player/player-damage.h"
22 #include "player/player-status-flags.h"
23 #include "player/player-status.h"
24 #include "spell-kind/spells-random.h"
25 #include "spell-kind/spells-teleport.h"
26 #include "spell/summon-types.h"
27 #include "status/bad-status-setter.h"
28 #include "status/buff-setter.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 #include "util/quarks.h"
34 #include "util/string-processor.h"
35 #include "view/display-messages.h"
36
37 namespace {
38 const EnumClassFlagGroup<TRC> TRC_P_FLAG_MASK({ TRC::TY_CURSE, TRC::DRAIN_EXP, TRC::ADD_L_CURSE, TRC::ADD_H_CURSE, TRC::CALL_ANIMAL, TRC::CALL_DEMON,
39     TRC::CALL_DRAGON, TRC::COWARDICE, TRC::TELEPORT, TRC::DRAIN_HP, TRC::DRAIN_MANA, TRC::CALL_UNDEAD, TRC::BERS_RAGE });
40 const EnumClassFlagGroup<TRCS> TRCS_P_FLAG_MASK({ TRCS::TELEPORT_SELF, TRCS::CHAINSWORD });
41 }
42
43 static bool is_specific_curse(TRC flag)
44 {
45     switch (flag) {
46     case TRC::ADD_L_CURSE:
47     case TRC::ADD_H_CURSE:
48     case TRC::DRAIN_HP:
49     case TRC::DRAIN_MANA:
50     case TRC::CALL_ANIMAL:
51     case TRC::CALL_DEMON:
52     case TRC::CALL_DRAGON:
53     case TRC::CALL_UNDEAD:
54     case TRC::COWARDICE:
55     case TRC::LOW_MELEE:
56     case TRC::LOW_AC:
57     case TRC::HARD_SPELL:
58     case TRC::FAST_DIGEST:
59     case TRC::SLOW_REGEN:
60     case TRC::BERS_RAGE:
61         return true;
62     default:
63         return false;
64     }
65 }
66
67 static void choise_cursed_item(TRC flag, object_type *o_ptr, int *choices, int *number, int item_num)
68 {
69     if (!is_specific_curse(flag))
70         return;
71
72     tr_type cf = TR_STR;
73     auto flgs = object_flags(o_ptr);
74     switch (flag) {
75     case TRC::ADD_L_CURSE:
76         cf = TR_ADD_L_CURSE;
77         break;
78     case TRC::ADD_H_CURSE:
79         cf = TR_ADD_H_CURSE;
80         break;
81     case TRC::DRAIN_HP:
82         cf = TR_DRAIN_HP;
83         break;
84     case TRC::DRAIN_MANA:
85         cf = TR_DRAIN_MANA;
86         break;
87     case TRC::CALL_ANIMAL:
88         cf = TR_CALL_ANIMAL;
89         break;
90     case TRC::CALL_DEMON:
91         cf = TR_CALL_DEMON;
92         break;
93     case TRC::CALL_DRAGON:
94         cf = TR_CALL_DRAGON;
95         break;
96     case TRC::CALL_UNDEAD:
97         cf = TR_CALL_UNDEAD;
98         break;
99     case TRC::COWARDICE:
100         cf = TR_COWARDICE;
101         break;
102     case TRC::LOW_MELEE:
103         cf = TR_LOW_MELEE;
104         break;
105     case TRC::LOW_AC:
106         cf = TR_LOW_AC;
107         break;
108     case TRC::HARD_SPELL:
109         cf = TR_HARD_SPELL;
110         break;
111     case TRC::FAST_DIGEST:
112         cf = TR_FAST_DIGEST;
113         break;
114     case TRC::SLOW_REGEN:
115         cf = TR_SLOW_REGEN;
116         break;
117     case TRC::BERS_RAGE:
118         cf = TR_BERS_RAGE;
119         break;
120     default:
121         break;
122     }
123
124     if (flgs.has_not(cf))
125         return;
126
127     choices[*number] = item_num;
128     (*number)++;
129 }
130
131 /*!
132  * @brief 現在呪いを保持している装備品を一つランダムに探し出す / Choose one of items that have cursed flag
133  * @param flag 探し出したい呪いフラグ配列
134  * @return 該当の呪いが一つでもあった場合にランダムに選ばれた装備品のオブジェクト構造体参照ポインタを返す。\n
135  * 呪いがない場合nullptrを返す。
136  */
137 object_type *choose_cursed_obj_name(player_type *creature_ptr, TRC flag)
138 {
139     int choices[INVEN_TOTAL - INVEN_MAIN_HAND];
140     int number = 0;
141     if (creature_ptr->cursed.has_not(flag))
142         return nullptr;
143
144     for (int i = INVEN_MAIN_HAND; i < INVEN_TOTAL; i++) {
145         object_type *o_ptr = &creature_ptr->inventory_list[i];
146         if (o_ptr->curse_flags.has(flag)) {
147             choices[number] = i;
148             number++;
149             continue;
150         }
151
152         choise_cursed_item(flag, o_ptr, choices, &number, i);
153     }
154
155     return &creature_ptr->inventory_list[choices[randint0(number)]];
156 }
157
158 /*!
159  * @brief 呪われている、トランプエゴ等による装備品由来のテレポートを実行する
160  * @param creature_ptr プレーヤーへの参照ポインタ
161  */
162 static void curse_teleport(player_type *creature_ptr)
163 {
164     if ((creature_ptr->cursed_special.has_not(TRCS::TELEPORT_SELF)) || !one_in_(200))
165         return;
166
167     GAME_TEXT o_name[MAX_NLEN];
168     object_type *o_ptr;
169     int i_keep = 0, count = 0;
170     for (int i = INVEN_MAIN_HAND; i < INVEN_TOTAL; i++) {
171         o_ptr = &creature_ptr->inventory_list[i];
172         if (!o_ptr->k_idx)
173             continue;
174
175         auto flgs = object_flags(o_ptr);
176
177         if (flgs.has_not(TR_TELEPORT))
178             continue;
179
180         if (o_ptr->inscription && angband_strchr(quark_str(o_ptr->inscription), '.'))
181             continue;
182
183         count++;
184         if (one_in_(count))
185             i_keep = i;
186     }
187
188     o_ptr = &creature_ptr->inventory_list[i_keep];
189     describe_flavor(creature_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
190     msg_format(_("%sがテレポートの能力を発動させようとしている。", "Your %s tries to teleport you."), o_name);
191     if (get_check_strict(creature_ptr, _("テレポートしますか?", "Teleport? "), CHECK_OKAY_CANCEL)) {
192         disturb(creature_ptr, false, true);
193         teleport_player(creature_ptr, 50, TELEPORT_SPONTANEOUS);
194     } else {
195         msg_format(_("%sに{.}(ピリオド)と銘を刻むと発動を抑制できます。", "You can inscribe {.} on your %s to disable random teleportation. "), o_name);
196         disturb(creature_ptr, true, true);
197     }
198 }
199
200 /*!
201  * @details 元々呪い効果の発揮ルーチン中にいたので、整合性保持のためここに置いておく
202  */
203 static void occur_chainsword_effect(player_type *creature_ptr)
204 {
205     if ((creature_ptr->cursed_special.has_not(TRCS::CHAINSWORD)) || !one_in_(CHAINSWORD_NOISE))
206         return;
207
208     char noise[1024];
209     if (!get_rnd_line(_("chainswd_j.txt", "chainswd.txt"), 0, noise))
210         msg_print(noise);
211     disturb(creature_ptr, false, false);
212 }
213
214 static void curse_drain_exp(player_type *creature_ptr)
215 {
216     if ((creature_ptr->prace == player_race_type::ANDROID) || (creature_ptr->cursed.has_not(TRC::DRAIN_EXP)) || !one_in_(4))
217         return;
218
219     creature_ptr->exp -= (creature_ptr->lev + 1) / 2;
220     if (creature_ptr->exp < 0)
221         creature_ptr->exp = 0;
222
223     creature_ptr->max_exp -= (creature_ptr->lev + 1) / 2;
224     if (creature_ptr->max_exp < 0)
225         creature_ptr->max_exp = 0;
226
227     check_experience(creature_ptr);
228 }
229
230 static void multiply_low_curse(player_type *creature_ptr)
231 {
232     if ((creature_ptr->cursed.has_not(TRC::ADD_L_CURSE)) || !one_in_(2000))
233         return;
234
235     object_type *o_ptr;
236     o_ptr = choose_cursed_obj_name(creature_ptr, TRC::ADD_L_CURSE);
237     auto new_curse = get_curse(0, o_ptr);
238     if (o_ptr->curse_flags.has(new_curse))
239         return;
240
241     GAME_TEXT o_name[MAX_NLEN];
242     describe_flavor(creature_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
243     o_ptr->curse_flags.set(new_curse);
244     msg_format(_("悪意に満ちた黒いオーラが%sをとりまいた...", "There is a malignant black aura surrounding your %s..."), o_name);
245     o_ptr->feeling = FEEL_NONE;
246     creature_ptr->update |= (PU_BONUS);
247 }
248
249 static void multiply_high_curse(player_type *creature_ptr)
250 {
251     if ((creature_ptr->cursed.has_not(TRC::ADD_H_CURSE)) || !one_in_(2000))
252         return;
253
254     object_type *o_ptr;
255     o_ptr = choose_cursed_obj_name(creature_ptr, TRC::ADD_H_CURSE);
256     auto new_curse = get_curse(1, o_ptr);
257     if (o_ptr->curse_flags.has(new_curse))
258         return;
259
260     GAME_TEXT o_name[MAX_NLEN];
261     describe_flavor(creature_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
262     o_ptr->curse_flags.set(new_curse);
263     msg_format(_("悪意に満ちた黒いオーラが%sをとりまいた...", "There is a malignant black aura surrounding your %s..."), o_name);
264     o_ptr->feeling = FEEL_NONE;
265     creature_ptr->update |= (PU_BONUS);
266 }
267
268 static void curse_call_monster(player_type *creature_ptr)
269 {
270     const int call_type = PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET;
271     const int obj_desc_type = OD_OMIT_PREFIX | OD_NAME_ONLY;
272     floor_type *floor_ptr = creature_ptr->current_floor_ptr;
273     if (creature_ptr->cursed.has(TRC::CALL_ANIMAL) && one_in_(2500)) {
274         if (summon_specific(creature_ptr, 0, creature_ptr->y, creature_ptr->x, floor_ptr->dun_level, SUMMON_ANIMAL, call_type)) {
275             GAME_TEXT o_name[MAX_NLEN];
276             describe_flavor(creature_ptr, o_name, choose_cursed_obj_name(creature_ptr, TRC::CALL_ANIMAL), obj_desc_type);
277             msg_format(_("%sが動物を引き寄せた!", "Your %s has attracted an animal!"), o_name);
278             disturb(creature_ptr, false, true);
279         }
280     }
281
282     if (creature_ptr->cursed.has(TRC::CALL_DEMON) && one_in_(1111)) {
283         if (summon_specific(creature_ptr, 0, creature_ptr->y, creature_ptr->x, floor_ptr->dun_level, SUMMON_DEMON, call_type)) {
284             GAME_TEXT o_name[MAX_NLEN];
285             describe_flavor(creature_ptr, o_name, choose_cursed_obj_name(creature_ptr, TRC::CALL_DEMON), obj_desc_type);
286             msg_format(_("%sが悪魔を引き寄せた!", "Your %s has attracted a demon!"), o_name);
287             disturb(creature_ptr, false, true);
288         }
289     }
290
291     if (creature_ptr->cursed.has(TRC::CALL_DRAGON) && one_in_(800)) {
292         if (summon_specific(creature_ptr, 0, creature_ptr->y, creature_ptr->x, floor_ptr->dun_level, SUMMON_DRAGON, call_type)) {
293             GAME_TEXT o_name[MAX_NLEN];
294             describe_flavor(creature_ptr, o_name, choose_cursed_obj_name(creature_ptr, TRC::CALL_DRAGON), obj_desc_type);
295             msg_format(_("%sがドラゴンを引き寄せた!", "Your %s has attracted a dragon!"), o_name);
296             disturb(creature_ptr, false, true);
297         }
298     }
299
300     if (creature_ptr->cursed.has(TRC::CALL_UNDEAD) && one_in_(1111)) {
301         if (summon_specific(creature_ptr, 0, creature_ptr->y, creature_ptr->x, floor_ptr->dun_level, SUMMON_UNDEAD, call_type)) {
302             GAME_TEXT o_name[MAX_NLEN];
303             describe_flavor(creature_ptr, o_name, choose_cursed_obj_name(creature_ptr, TRC::CALL_UNDEAD), obj_desc_type);
304             msg_format(_("%sが死霊を引き寄せた!", "Your %s has attracted an undead!"), o_name);
305             disturb(creature_ptr, false, true);
306         }
307     }
308 }
309
310 static void curse_cowardice(player_type *creature_ptr)
311 {
312     if ((creature_ptr->cursed.has_not(TRC::COWARDICE)) || !one_in_(1500))
313         return;
314
315     if (has_resist_fear(creature_ptr))
316         return;
317
318     disturb(creature_ptr, false, true);
319     msg_print(_("とても暗い... とても恐い!", "It's so dark... so scary!"));
320     set_afraid(creature_ptr, creature_ptr->afraid + 13 + randint1(26));
321 }
322
323 /*!
324  * @brief 装備による狂戦士化の発作を引き起こす
325  * @param creature_ptr プレイヤー情報への参照ポインタ
326  */
327 static void curse_berserk_rage(player_type *creature_ptr)
328 {
329     if ((creature_ptr->cursed.has_not(TRC::BERS_RAGE)) || !one_in_(1500))
330         return;
331
332     disturb(creature_ptr, false, true);
333     msg_print(_("ウガァァア!", "RAAAAGHH!"));
334     msg_print(_("激怒の発作に襲われた!", "You feel a fit of rage coming over you!"));
335     (void)set_shero(creature_ptr, 10 + randint1(creature_ptr->lev), false);
336     (void)set_afraid(creature_ptr, 0);
337 }
338
339 static void curse_drain_hp(player_type *creature_ptr)
340 {
341     if ((creature_ptr->cursed.has_not(TRC::DRAIN_HP)) || !one_in_(666))
342         return;
343
344     GAME_TEXT o_name[MAX_NLEN];
345     describe_flavor(creature_ptr, o_name, choose_cursed_obj_name(creature_ptr, TRC::DRAIN_HP), (OD_OMIT_PREFIX | OD_NAME_ONLY));
346     msg_format(_("%sはあなたの体力を吸収した!", "Your %s drains HP from you!"), o_name);
347     take_hit(creature_ptr, DAMAGE_LOSELIFE, MIN(creature_ptr->lev * 2, 100), o_name);
348 }
349
350 static void curse_drain_mp(player_type *creature_ptr)
351 {
352     if ((creature_ptr->cursed.has_not(TRC::DRAIN_MANA)) || (creature_ptr->csp == 0) || !one_in_(666))
353         return;
354
355     GAME_TEXT o_name[MAX_NLEN];
356     describe_flavor(creature_ptr, o_name, choose_cursed_obj_name(creature_ptr, TRC::DRAIN_MANA), (OD_OMIT_PREFIX | OD_NAME_ONLY));
357     msg_format(_("%sはあなたの魔力を吸収した!", "Your %s drains mana from you!"), o_name);
358     creature_ptr->csp -= MIN(creature_ptr->lev, 50);
359     if (creature_ptr->csp < 0) {
360         creature_ptr->csp = 0;
361         creature_ptr->csp_frac = 0;
362     }
363
364     creature_ptr->redraw |= PR_MANA;
365 }
366
367 static void occur_curse_effects(player_type *creature_ptr)
368 {
369     if ((creature_ptr->cursed.has_none_of(TRC_P_FLAG_MASK) && creature_ptr->cursed_special.has_none_of(TRCS_P_FLAG_MASK)) || creature_ptr->phase_out
370         || creature_ptr->wild_mode)
371         return;
372
373     curse_teleport(creature_ptr);
374     occur_chainsword_effect(creature_ptr);
375     if (creature_ptr->cursed.has(TRC::TY_CURSE) && one_in_(TY_CURSE_CHANCE)) {
376         int count = 0;
377         (void)activate_ty_curse(creature_ptr, false, &count);
378     }
379
380     curse_drain_exp(creature_ptr);
381     multiply_low_curse(creature_ptr);
382     multiply_high_curse(creature_ptr);
383     curse_call_monster(creature_ptr);
384     curse_cowardice(creature_ptr);
385     curse_berserk_rage(creature_ptr);
386     if (creature_ptr->cursed.has(TRC::TELEPORT) && one_in_(200) && !creature_ptr->anti_tele) {
387         disturb(creature_ptr, false, true);
388         teleport_player(creature_ptr, 40, TELEPORT_PASSIVE);
389     }
390
391     curse_drain_hp(creature_ptr);
392     curse_drain_mp(creature_ptr);
393 }
394
395 /*!
396  * @brief 10ゲームターンが進行するごとに装備効果の発動判定を行う処理
397  * / Handle curse effects once every 10 game turns
398  * @param creature_ptr プレーヤーへの参照ポインタ
399  */
400 void execute_cursed_items_effect(player_type *creature_ptr)
401 {
402     occur_curse_effects(creature_ptr);
403     if (!one_in_(999) || creature_ptr->anti_magic || (one_in_(2) && has_resist_curse(creature_ptr)))
404         return;
405
406     object_type *o_ptr = &creature_ptr->inventory_list[INVEN_LITE];
407     if (o_ptr->name1 != ART_JUDGE)
408         return;
409
410     if (o_ptr->is_known())
411         msg_print(_("『審判の宝石』はあなたの体力を吸収した!", "The Jewel of Judgement drains life from you!"));
412     else
413         msg_print(_("なにかがあなたの体力を吸収した!", "Something drains life from you!"));
414
415     take_hit(creature_ptr, DAMAGE_LOSELIFE, MIN(creature_ptr->lev, 50), _("審判の宝石", "the Jewel of Judgement"));
416 }