OSDN Git Service

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