OSDN Git Service

[feature] *_info の std::vector 化
[hengbandforosx/hengbandosx.git] / src / object / warning.cpp
1 #include "object/warning.h"
2 #include "artifact/fixed-art-types.h"
3 #include "core/asking-player.h"
4 #include "core/disturbance.h"
5 #include "dungeon/dungeon-flag-types.h"
6 #include "dungeon/dungeon.h"
7 #include "flavor/flavor-describer.h"
8 #include "flavor/object-flavor-types.h"
9 #include "floor/cave.h"
10 #include "game-option/input-options.h"
11 #include "grid/feature.h"
12 #include "grid/grid.h"
13 #include "inventory/inventory-slot-types.h"
14 #include "monster-attack/monster-attack-effect.h"
15 #include "monster-attack/monster-attack-types.h"
16 #include "monster-race/monster-race.h"
17 #include "monster-race/race-flags1.h"
18 #include "monster-race/race-indice-types.h"
19 #include "monster/monster-info.h"
20 #include "monster/monster-status.h"
21 #include "mspell/mspell-damage-calculator.h"
22 #include "mspell/mspell-type.h"
23 #include "mutation/mutation-flag-types.h"
24 #include "object-enchant/tr-types.h"
25 #include "object/object-flags.h"
26 #include "player/mimic-info-table.h"
27 #include "player/player-class.h"
28 #include "player/player-race-types.h"
29 #include "player/player-status-flags.h"
30 #include "player/player-status-resist.h"
31 #include "player/special-defense-types.h"
32 #include "spell/spell-types.h"
33 #include "status/element-resistance.h"
34 #include "system/floor-type-definition.h"
35 #include "target/projection-path-calculator.h"
36 #include "util/bit-flags-calculator.h"
37 #include "view/display-messages.h"
38
39 /*!
40  * @brief 警告を放つアイテムを選択する /
41  * Choose one of items that have warning flag
42  * Calculate spell damages
43  * @return 警告を行う
44  */
45 object_type *choose_warning_item(player_type *creature_ptr)
46 {
47     int choices[INVEN_TOTAL - INVEN_MAIN_HAND];
48
49     /* Paranoia -- Player has no warning ability */
50     if (!creature_ptr->warning)
51         return NULL;
52
53     /* Search Inventory */
54     int number = 0;
55     for (int i = INVEN_MAIN_HAND; i < INVEN_TOTAL; i++) {
56         BIT_FLAGS flgs[TR_FLAG_SIZE];
57         object_type *o_ptr = &creature_ptr->inventory_list[i];
58
59         object_flags(creature_ptr, o_ptr, flgs);
60         if (has_flag(flgs, TR_WARNING)) {
61             choices[number] = i;
62             number++;
63         }
64     }
65
66     /* Choice one of them */
67     return number ? &creature_ptr->inventory_list[choices[randint0(number)]] : NULL;
68 }
69
70 /*!
71  * @brief 警告基準を定めるために魔法の効果属性に基づいて最大魔法ダメージを計算する /
72  * Calculate spell damages
73  * @param m_ptr 魔法を行使するモンスターの構造体参照ポインタ
74  * @param typ 効果属性のID
75  * @param dam 基本ダメージ
76  * @param max 算出した最大ダメージを返すポインタ
77  * @return なし
78  */
79 static void spell_damcalc(player_type *target_ptr, monster_type *m_ptr, EFFECT_ID typ, HIT_POINT dam, int *max)
80 {
81     monster_race *r_ptr = &r_info[m_ptr->r_idx];
82     int rlev = r_ptr->level;
83     bool ignore_wraith_form = FALSE;
84
85     /* Vulnerability, resistance and immunity */
86     switch (typ) {
87     case GF_ELEC:
88         if (has_immune_elec(target_ptr)) {
89             ignore_wraith_form = TRUE;
90         }
91         dam = dam * calc_elec_damage_rate(target_ptr) / 100;
92         break;
93
94     case GF_POIS:
95         dam = dam * calc_pois_damage_rate(target_ptr) / 100;
96         break;
97
98     case GF_ACID:
99         if (has_immune_acid(target_ptr)) {
100             ignore_wraith_form = TRUE;
101         }
102         dam = dam * calc_acid_damage_rate(target_ptr) / 100;
103         break;
104
105     case GF_COLD:
106     case GF_ICE:
107         if (has_immune_cold(target_ptr)) {
108             ignore_wraith_form = TRUE;
109         }
110         dam = dam * calc_cold_damage_rate(target_ptr) / 100;
111         break;
112
113     case GF_FIRE:
114         if (has_immune_fire(target_ptr)) {
115             ignore_wraith_form = TRUE;
116         }
117         dam = dam * calc_fire_damage_rate(target_ptr) / 100;
118         break;
119
120     case GF_PSY_SPEAR:
121         ignore_wraith_form = TRUE;
122         break;
123
124     case GF_ARROW:
125         if (!target_ptr->blind && (has_invuln_arrow(target_ptr))) {
126             dam = 0;
127             ignore_wraith_form = TRUE;
128         }
129         break;
130
131     case GF_LITE:
132         dam = dam * calc_lite_damage_rate(target_ptr, CALC_MAX) / 100;
133         break;
134
135     case GF_DARK:
136         dam = dam * calc_dark_damage_rate(target_ptr, CALC_MAX) / 100;
137         if (is_specific_player_race(target_ptr, RACE_VAMPIRE) || (target_ptr->mimic_form == MIMIC_VAMPIRE) || target_ptr->wraith_form)
138             ignore_wraith_form = TRUE;
139         break;
140
141     case GF_SHARDS:
142         dam = dam * calc_shards_damage_rate(target_ptr, CALC_MAX) / 100;
143         break;
144
145     case GF_SOUND:
146         dam = dam * calc_sound_damage_rate(target_ptr, CALC_MAX) / 100;
147         break;
148
149     case GF_CONFUSION:
150         dam = dam * calc_conf_damage_rate(target_ptr, CALC_MAX) / 100;
151         break;
152
153     case GF_CHAOS:
154         dam = dam * calc_chaos_damage_rate(target_ptr, CALC_MAX) / 100;
155         break;
156
157     case GF_NETHER:
158         dam = dam * calc_nether_damage_rate(target_ptr, CALC_MAX) / 100;
159         if (is_specific_player_race(target_ptr, RACE_SPECTRE)) {
160             ignore_wraith_form = TRUE;
161             dam = 0;
162         }
163         break;
164
165     case GF_DISENCHANT:
166         dam = dam * calc_disenchant_damage_rate(target_ptr, CALC_MAX) / 100;
167         break;
168
169     case GF_NEXUS:
170         dam = dam * calc_nexus_damage_rate(target_ptr, CALC_MAX) / 100;
171         break;
172
173     case GF_TIME:
174         dam = dam * calc_time_damage_rate(target_ptr, CALC_MAX) / 100;
175         break;
176
177     case GF_GRAVITY:
178         dam = dam * calc_gravity_damage_rate(target_ptr, CALC_MAX) / 100;
179         break;
180
181     case GF_ROCKET:
182         dam = dam * calc_rocket_damage_rate(target_ptr, CALC_MAX) / 100;
183         break;
184
185     case GF_NUKE:
186         dam = dam * calc_nuke_damage_rate(target_ptr) / 100;
187         break;
188
189     case GF_DEATH_RAY:
190         dam = dam * calc_deathray_damage_rate(target_ptr, CALC_MAX) / 100;
191         if (dam == 0)
192             ignore_wraith_form = TRUE;
193         break;
194
195     case GF_HOLY_FIRE:
196         dam = dam * calc_holy_fire_damage_rate(target_ptr, CALC_MAX) / 100;
197         break;
198
199     case GF_HELL_FIRE:
200         dam = dam * calc_hell_fire_damage_rate(target_ptr, CALC_MAX) / 100;
201         break;
202
203     case GF_MIND_BLAST:
204     case GF_BRAIN_SMASH:
205         if (100 + rlev / 2 <= MAX(5, target_ptr->skill_sav)) {
206             dam = 0;
207             ignore_wraith_form = TRUE;
208         }
209
210         break;
211
212     case GF_CAUSE_1:
213     case GF_CAUSE_2:
214     case GF_CAUSE_3:
215     case GF_HAND_DOOM:
216         if (100 + rlev / 2 <= target_ptr->skill_sav) {
217             dam = 0;
218             ignore_wraith_form = TRUE;
219         }
220
221         break;
222
223     case GF_CAUSE_4:
224         if ((100 + rlev / 2 <= target_ptr->skill_sav) && (m_ptr->r_idx != MON_KENSHIROU)) {
225             dam = 0;
226             ignore_wraith_form = TRUE;
227         }
228
229         break;
230     }
231
232     if (target_ptr->wraith_form && !ignore_wraith_form) {
233         dam /= 2;
234         if (!dam)
235             dam = 1;
236     }
237
238     if (dam > *max)
239         *max = dam;
240 }
241
242 /*!
243  * @brief 警告基準を定めるために魔法の効果属性に基づいて最大魔法ダメージを計算する。 /
244  * Calculate spell damages
245  * @param spell_num RF4ならRF_ABILITY::SPELL_STARTのように32区切りのベースとなる数値
246  * @param typ 効果属性のID
247  * @param m_idx 魔法を行使するモンスターのID
248  * @param max 算出した最大ダメージを返すポインタ
249  * @return なし
250  */
251 void spell_damcalc_by_spellnum(player_type *creature_ptr, monster_spell_type ms_type, EFFECT_ID typ, MONSTER_IDX m_idx, int *max)
252 {
253     monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[m_idx];
254     HIT_POINT dam = monspell_damage(creature_ptr, ms_type, m_idx, DAM_MAX);
255     spell_damcalc(creature_ptr, m_ptr, typ, dam, max);
256 }
257
258 /*!
259  * @brief 警告基準を定めるためにモンスターの打撃最大ダメージを算出する /
260  * Calculate blow damages
261  * @param m_ptr 打撃を行使するモンスターの構造体参照ポインタ
262  * @param blow_ptr モンスターの打撃能力の構造体参照ポインタ
263  * @return 算出された最大ダメージを返す。
264  */
265 static int blow_damcalc(monster_type *m_ptr, player_type *target_ptr, monster_blow *blow_ptr)
266 {
267     int dam = blow_ptr->d_dice * blow_ptr->d_side;
268     int dummy_max = 0;
269
270     if (blow_ptr->method == RBM_EXPLODE) {
271         dam = (dam + 1) / 2;
272         spell_damcalc(target_ptr, m_ptr, mbe_info[blow_ptr->effect].explode_type, dam, &dummy_max);
273         dam = dummy_max;
274         return dam;
275     }
276
277     ARMOUR_CLASS ac = target_ptr->ac + target_ptr->to_a;
278     bool check_wraith_form = TRUE;
279     switch (blow_ptr->effect) {
280     case RBE_SUPERHURT: {
281         int tmp_dam = dam - (dam * ((ac < 150) ? ac : 150) / 250);
282         dam = MAX(dam, tmp_dam * 2);
283         break;
284     }
285
286     case RBE_HURT:
287     case RBE_SHATTER:
288         dam -= (dam * ((ac < 150) ? ac : 150) / 250);
289         break;
290
291     case RBE_ACID:
292         spell_damcalc(target_ptr, m_ptr, GF_ACID, dam, &dummy_max);
293         dam = dummy_max;
294         check_wraith_form = FALSE;
295         break;
296
297     case RBE_ELEC:
298         spell_damcalc(target_ptr, m_ptr, GF_ELEC, dam, &dummy_max);
299         dam = dummy_max;
300         check_wraith_form = FALSE;
301         break;
302
303     case RBE_FIRE:
304         spell_damcalc(target_ptr, m_ptr, GF_FIRE, dam, &dummy_max);
305         dam = dummy_max;
306         check_wraith_form = FALSE;
307         break;
308
309     case RBE_COLD:
310         spell_damcalc(target_ptr, m_ptr, GF_COLD, dam, &dummy_max);
311         dam = dummy_max;
312         check_wraith_form = FALSE;
313         break;
314
315     case RBE_DR_MANA:
316         dam = 0;
317         check_wraith_form = FALSE;
318         break;
319
320     default:
321         break;
322     }
323
324     if (check_wraith_form && target_ptr->wraith_form) {
325         dam /= 2;
326         if (!dam)
327             dam = 1;
328     }
329
330     return dam;
331 }
332
333 /*!
334  * @brief プレイヤーが特定地点へ移動した場合に警告を発する処理 /
335  * Examine the grid (xx,yy) and warn the player if there are any danger
336  * @param xx 危険性を調査するマスのX座標
337  * @param yy 危険性を調査するマスのY座標
338  * @return 警告を無視して進むことを選択するかか問題が無ければTRUE、警告に従ったならFALSEを返す。
339  */
340 bool process_warning(player_type *creature_ptr, POSITION xx, POSITION yy)
341 {
342     POSITION mx, my;
343     grid_type *g_ptr;
344     GAME_TEXT o_name[MAX_NLEN];
345
346 #define WARNING_AWARE_RANGE 12
347     int dam_max = 0;
348     static int old_damage = 0;
349
350     for (mx = xx - WARNING_AWARE_RANGE; mx < xx + WARNING_AWARE_RANGE + 1; mx++) {
351         for (my = yy - WARNING_AWARE_RANGE; my < yy + WARNING_AWARE_RANGE + 1; my++) {
352             int dam_max0 = 0;
353             monster_type *m_ptr;
354             monster_race *r_ptr;
355
356             if (!in_bounds(creature_ptr->current_floor_ptr, my, mx) || (distance(my, mx, yy, xx) > WARNING_AWARE_RANGE))
357                 continue;
358
359             g_ptr = &creature_ptr->current_floor_ptr->grid_array[my][mx];
360
361             if (!g_ptr->m_idx)
362                 continue;
363
364             m_ptr = &creature_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
365
366             if (monster_csleep_remaining(m_ptr))
367                 continue;
368             if (!is_hostile(m_ptr))
369                 continue;
370
371             r_ptr = &r_info[m_ptr->r_idx];
372
373             /* Monster spells (only powerful ones)*/
374             if (projectable(creature_ptr, my, mx, yy, xx)) {
375                 const auto flags = r_ptr->ability_flags;
376
377                 if (!(d_info[creature_ptr->dungeon_idx].flags1 & DF1_NO_MAGIC)) {
378                     if (flags.has(RF_ABILITY::BA_CHAO))
379                         spell_damcalc_by_spellnum(creature_ptr, MS_BALL_CHAOS, GF_CHAOS, g_ptr->m_idx, &dam_max0);
380                     if (flags.has(RF_ABILITY::BA_MANA))
381                         spell_damcalc_by_spellnum(creature_ptr, MS_BALL_MANA, GF_MANA, g_ptr->m_idx, &dam_max0);
382                     if (flags.has(RF_ABILITY::BA_DARK))
383                         spell_damcalc_by_spellnum(creature_ptr, MS_BALL_DARK, GF_DARK, g_ptr->m_idx, &dam_max0);
384                     if (flags.has(RF_ABILITY::BA_LITE))
385                         spell_damcalc_by_spellnum(creature_ptr, MS_STARBURST, GF_LITE, g_ptr->m_idx, &dam_max0);
386                     if (flags.has(RF_ABILITY::HAND_DOOM))
387                         spell_damcalc_by_spellnum(creature_ptr, MS_HAND_DOOM, GF_HAND_DOOM, g_ptr->m_idx, &dam_max0);
388                     if (flags.has(RF_ABILITY::PSY_SPEAR))
389                         spell_damcalc_by_spellnum(creature_ptr, MS_PSY_SPEAR, GF_PSY_SPEAR, g_ptr->m_idx, &dam_max0);
390                 }
391
392                 if (flags.has(RF_ABILITY::ROCKET))
393                     spell_damcalc_by_spellnum(creature_ptr, MS_ROCKET, GF_ROCKET, g_ptr->m_idx, &dam_max0);
394                 if (flags.has(RF_ABILITY::BR_ACID))
395                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_ACID, GF_ACID, g_ptr->m_idx, &dam_max0);
396                 if (flags.has(RF_ABILITY::BR_ELEC))
397                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_ELEC, GF_ELEC, g_ptr->m_idx, &dam_max0);
398                 if (flags.has(RF_ABILITY::BR_FIRE))
399                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_FIRE, GF_FIRE, g_ptr->m_idx, &dam_max0);
400                 if (flags.has(RF_ABILITY::BR_COLD))
401                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_COLD, GF_COLD, g_ptr->m_idx, &dam_max0);
402                 if (flags.has(RF_ABILITY::BR_POIS))
403                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_POIS, GF_POIS, g_ptr->m_idx, &dam_max0);
404                 if (flags.has(RF_ABILITY::BR_NETH))
405                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_NETHER, GF_NETHER, g_ptr->m_idx, &dam_max0);
406                 if (flags.has(RF_ABILITY::BR_LITE))
407                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_LITE, GF_LITE, g_ptr->m_idx, &dam_max0);
408                 if (flags.has(RF_ABILITY::BR_DARK))
409                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_DARK, GF_DARK, g_ptr->m_idx, &dam_max0);
410                 if (flags.has(RF_ABILITY::BR_CONF))
411                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_CONF, GF_CONFUSION, g_ptr->m_idx, &dam_max0);
412                 if (flags.has(RF_ABILITY::BR_SOUN))
413                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_SOUND, GF_SOUND, g_ptr->m_idx, &dam_max0);
414                 if (flags.has(RF_ABILITY::BR_CHAO))
415                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_CHAOS, GF_CHAOS, g_ptr->m_idx, &dam_max0);
416                 if (flags.has(RF_ABILITY::BR_DISE))
417                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_DISEN, GF_DISENCHANT, g_ptr->m_idx, &dam_max0);
418                 if (flags.has(RF_ABILITY::BR_NEXU))
419                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_NEXUS, GF_NEXUS, g_ptr->m_idx, &dam_max0);
420                 if (flags.has(RF_ABILITY::BR_TIME))
421                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_TIME, GF_TIME, g_ptr->m_idx, &dam_max0);
422                 if (flags.has(RF_ABILITY::BR_INER))
423                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_INERTIA, GF_INERTIAL, g_ptr->m_idx, &dam_max0);
424                 if (flags.has(RF_ABILITY::BR_GRAV))
425                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_GRAVITY, GF_GRAVITY, g_ptr->m_idx, &dam_max0);
426                 if (flags.has(RF_ABILITY::BR_SHAR))
427                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_SHARDS, GF_SHARDS, g_ptr->m_idx, &dam_max0);
428                 if (flags.has(RF_ABILITY::BR_PLAS))
429                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_PLASMA, GF_PLASMA, g_ptr->m_idx, &dam_max0);
430                 if (flags.has(RF_ABILITY::BR_WALL))
431                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_FORCE, GF_FORCE, g_ptr->m_idx, &dam_max0);
432                 if (flags.has(RF_ABILITY::BR_MANA))
433                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_MANA, GF_MANA, g_ptr->m_idx, &dam_max0);
434                 if (flags.has(RF_ABILITY::BR_NUKE))
435                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_NUKE, GF_NUKE, g_ptr->m_idx, &dam_max0);
436                 if (flags.has(RF_ABILITY::BR_DISI))
437                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_DISI, GF_DISINTEGRATE, g_ptr->m_idx, &dam_max0);
438             }
439
440             /* Monster melee attacks */
441             if ((r_ptr->flags1 & RF1_NEVER_BLOW) || (d_info[creature_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)) {
442                 dam_max += dam_max0;
443                 continue;
444             }
445
446             if (!(mx <= xx + 1 && mx >= xx - 1 && my <= yy + 1 && my >= yy - 1)) {
447                 dam_max += dam_max0;
448                 continue;
449             }
450
451             int dam_melee = 0;
452             for (int m = 0; m < 4; m++) {
453                 /* Skip non-attacks */
454                 if (!r_ptr->blow[m].method || (r_ptr->blow[m].method == RBM_SHOOT))
455                     continue;
456
457                 /* Extract the attack info */
458                 dam_melee += blow_damcalc(m_ptr, creature_ptr, &r_ptr->blow[m]);
459                 if (r_ptr->blow[m].method == RBM_EXPLODE)
460                     break;
461             }
462
463             if (dam_melee > dam_max0)
464                 dam_max0 = dam_melee;
465             dam_max += dam_max0;
466         }
467     }
468
469     /* Prevent excessive warning */
470     if (dam_max > old_damage) {
471         old_damage = dam_max * 3 / 2;
472
473         if (dam_max > creature_ptr->chp / 2) {
474             object_type *o_ptr = choose_warning_item(creature_ptr);
475
476             if (o_ptr)
477                 describe_flavor(creature_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
478             else
479                 strcpy(o_name, _("体", "body")); /* Warning ability without item */
480             msg_format(_("%sが鋭く震えた!", "Your %s pulsates sharply!"), o_name);
481
482             disturb(creature_ptr, FALSE, TRUE);
483             return get_check(_("本当にこのまま進むか?", "Really want to go ahead? "));
484         }
485     } else
486         old_damage = old_damage / 2;
487
488     g_ptr = &creature_ptr->current_floor_ptr->grid_array[yy][xx];
489     bool is_warning = (!easy_disarm && is_trap(creature_ptr, g_ptr->feat)) || (g_ptr->mimic && is_trap(creature_ptr, g_ptr->feat));
490     is_warning &= !one_in_(13);
491     if (!is_warning)
492         return TRUE;
493
494     object_type *o_ptr = choose_warning_item(creature_ptr);
495     if (o_ptr != NULL)
496         describe_flavor(creature_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
497     else
498         strcpy(o_name, _("体", "body")); /* Warning ability without item */
499     msg_format(_("%sが鋭く震えた!", "Your %s pulsates sharply!"), o_name);
500     disturb(creature_ptr, FALSE, TRUE);
501     return get_check(_("本当にこのまま進むか?", "Really want to go ahead? "));
502 }