OSDN Git Service

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