OSDN Git Service

[Fix] #41266 名前の長いモンスターを検索するとソフトウェアがクラッシュする問題を解消した / Resolved the issue that software...
[hengband/hengband.git] / src / object / warning.c
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-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/player-status-flags.h"
33 #include "player/player-status-resist.h"
34 #include "player/special-defense-types.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 (has_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 (has_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 (has_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 (has_immune_fire(target_ptr)) {
118             ignore_wraith_form = TRUE;
119         }
120         dam = dam * calc_fire_damage_rate(target_ptr) / 100;
121         break;
122
123     case GF_PSY_SPEAR:
124         ignore_wraith_form = TRUE;
125         break;
126
127     case GF_ARROW:
128         if (!target_ptr->blind && (has_invuln_arrow(target_ptr))) {
129             dam = 0;
130             ignore_wraith_form = TRUE;
131         }
132         break;
133
134     case GF_LITE:
135         dam = dam * calc_lite_damage_rate(target_ptr, CALC_MAX) / 100;
136         break;
137
138     case GF_DARK:
139         dam = dam * calc_dark_damage_rate(target_ptr, CALC_MAX) / 100;
140         if (is_specific_player_race(target_ptr, RACE_VAMPIRE) || (target_ptr->mimic_form == MIMIC_VAMPIRE) || target_ptr->wraith_form)
141             ignore_wraith_form = TRUE;
142         break;
143
144     case GF_SHARDS:
145         dam = dam * calc_shards_damage_rate(target_ptr, CALC_MAX) / 100;
146         break;
147
148     case GF_SOUND:
149         dam = dam * calc_sound_damage_rate(target_ptr, CALC_MAX) / 100;
150         break;
151
152     case GF_CONFUSION:
153         dam = dam * calc_conf_damage_rate(target_ptr, CALC_MAX) / 100;
154         break;
155
156     case GF_CHAOS:
157         dam = dam * calc_chaos_damage_rate(target_ptr, CALC_MAX) / 100;
158         break;
159
160     case GF_NETHER:
161         dam = dam * calc_nether_damage_rate(target_ptr, CALC_MAX) / 100;
162         if (is_specific_player_race(target_ptr, RACE_SPECTRE)) {
163             ignore_wraith_form = TRUE;
164             dam = 0;
165         }
166         break;
167
168     case GF_DISENCHANT:
169         dam = dam * calc_disenchant_damage_rate(target_ptr, CALC_MAX) / 100;
170         break;
171
172     case GF_NEXUS:
173         dam = dam * calc_nexus_damage_rate(target_ptr, CALC_MAX) / 100;
174         break;
175
176     case GF_TIME:
177         dam = dam * calc_time_damage_rate(target_ptr, CALC_MAX) / 100;
178         break;
179
180     case GF_GRAVITY:
181         dam = dam * calc_gravity_damage_rate(target_ptr, CALC_MAX) / 100;
182         break;
183
184     case GF_ROCKET:
185         dam = dam * calc_rocket_damage_rate(target_ptr, CALC_MAX) / 100;
186         break;
187
188     case GF_NUKE:
189         dam = dam * calc_nuke_damage_rate(target_ptr) / 100;
190         break;
191
192     case GF_DEATH_RAY:
193         dam = dam * calc_deathray_damage_rate(target_ptr, CALC_MAX) / 100;
194         if (dam == 0)
195             ignore_wraith_form = TRUE;
196         break;
197
198     case GF_HOLY_FIRE:
199         dam = dam * calc_holy_fire_damage_rate(target_ptr, CALC_MAX) / 100;
200         break;
201
202     case GF_HELL_FIRE:
203         dam = dam * calc_hell_fire_damage_rate(target_ptr, CALC_MAX) / 100;
204         break;
205
206     case GF_MIND_BLAST:
207     case GF_BRAIN_SMASH:
208         if (100 + rlev / 2 <= MAX(5, target_ptr->skill_sav)) {
209             dam = 0;
210             ignore_wraith_form = TRUE;
211         }
212
213         break;
214
215     case GF_CAUSE_1:
216     case GF_CAUSE_2:
217     case GF_CAUSE_3:
218     case GF_HAND_DOOM:
219         if (100 + rlev / 2 <= target_ptr->skill_sav) {
220             dam = 0;
221             ignore_wraith_form = TRUE;
222         }
223
224         break;
225
226     case GF_CAUSE_4:
227         if ((100 + rlev / 2 <= target_ptr->skill_sav) && (m_ptr->r_idx != MON_KENSHIROU)) {
228             dam = 0;
229             ignore_wraith_form = TRUE;
230         }
231
232         break;
233     }
234
235     if (target_ptr->wraith_form && !ignore_wraith_form) {
236         dam /= 2;
237         if (!dam)
238             dam = 1;
239     }
240
241     if (dam > *max)
242         *max = dam;
243 }
244
245 /*!
246  * @brief 警告基準を定めるために魔法の効果属性に基づいて最大魔法ダメージを計算する。 /
247  * Calculate spell damages
248  * @param spell_num RF4ならRF4_SPELL_STARTのように32区切りのベースとなる数値
249  * @param typ 効果属性のID
250  * @param m_idx 魔法を行使するモンスターのID
251  * @param max 算出した最大ダメージを返すポインタ
252  * @return なし
253  */
254 void spell_damcalc_by_spellnum(player_type *creature_ptr, monster_spell_type ms_type, EFFECT_ID typ, MONSTER_IDX m_idx, int *max)
255 {
256     monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[m_idx];
257     HIT_POINT dam = monspell_damage(creature_ptr, ms_type, m_idx, DAM_MAX);
258     spell_damcalc(creature_ptr, m_ptr, typ, dam, max);
259 }
260
261 /*!
262  * @brief 警告基準を定めるためにモンスターの打撃最大ダメージを算出する /
263  * Calculate blow damages
264  * @param m_ptr 打撃を行使するモンスターの構造体参照ポインタ
265  * @param blow_ptr モンスターの打撃能力の構造体参照ポインタ
266  * @return 算出された最大ダメージを返す。
267  */
268 static int blow_damcalc(monster_type *m_ptr, player_type *target_ptr, monster_blow *blow_ptr)
269 {
270     int dam = blow_ptr->d_dice * blow_ptr->d_side;
271     int dummy_max = 0;
272
273     if (blow_ptr->method == RBM_EXPLODE) {
274         dam = (dam + 1) / 2;
275         spell_damcalc(target_ptr, m_ptr, mbe_info[blow_ptr->effect].explode_type, dam, &dummy_max);
276         dam = dummy_max;
277         return dam;
278     }
279
280     ARMOUR_CLASS ac = target_ptr->ac + target_ptr->to_a;
281     bool check_wraith_form = TRUE;
282     switch (blow_ptr->effect) {
283     case RBE_SUPERHURT: {
284         int tmp_dam = dam - (dam * ((ac < 150) ? ac : 150) / 250);
285         dam = MAX(dam, tmp_dam * 2);
286         break;
287     }
288
289     case RBE_HURT:
290     case RBE_SHATTER:
291         dam -= (dam * ((ac < 150) ? ac : 150) / 250);
292         break;
293
294     case RBE_ACID:
295         spell_damcalc(target_ptr, m_ptr, GF_ACID, dam, &dummy_max);
296         dam = dummy_max;
297         check_wraith_form = FALSE;
298         break;
299
300     case RBE_ELEC:
301         spell_damcalc(target_ptr, m_ptr, GF_ELEC, dam, &dummy_max);
302         dam = dummy_max;
303         check_wraith_form = FALSE;
304         break;
305
306     case RBE_FIRE:
307         spell_damcalc(target_ptr, m_ptr, GF_FIRE, dam, &dummy_max);
308         dam = dummy_max;
309         check_wraith_form = FALSE;
310         break;
311
312     case RBE_COLD:
313         spell_damcalc(target_ptr, m_ptr, GF_COLD, dam, &dummy_max);
314         dam = dummy_max;
315         check_wraith_form = FALSE;
316         break;
317
318     case RBE_DR_MANA:
319         dam = 0;
320         check_wraith_form = FALSE;
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                 BIT_FLAGS f4 = r_ptr->flags4;
376                 BIT_FLAGS f5 = r_ptr->a_ability_flags1;
377                 BIT_FLAGS f6 = r_ptr->a_ability_flags2;
378
379                 if (!(d_info[creature_ptr->dungeon_idx].flags1 & DF1_NO_MAGIC)) {
380                     if (f4 & RF4_BA_CHAO)
381                         spell_damcalc_by_spellnum(creature_ptr, MS_BALL_CHAOS, GF_CHAOS, g_ptr->m_idx, &dam_max0);
382                     if (f5 & RF5_BA_MANA)
383                         spell_damcalc_by_spellnum(creature_ptr, MS_BALL_MANA, GF_MANA, g_ptr->m_idx, &dam_max0);
384                     if (f5 & RF5_BA_DARK)
385                         spell_damcalc_by_spellnum(creature_ptr, MS_BALL_DARK, GF_DARK, g_ptr->m_idx, &dam_max0);
386                     if (f5 & RF5_BA_LITE)
387                         spell_damcalc_by_spellnum(creature_ptr, MS_STARBURST, GF_LITE, g_ptr->m_idx, &dam_max0);
388                     if (f6 & RF6_HAND_DOOM)
389                         spell_damcalc_by_spellnum(creature_ptr, MS_HAND_DOOM, GF_HAND_DOOM, g_ptr->m_idx, &dam_max0);
390                     if (f6 & RF6_PSY_SPEAR)
391                         spell_damcalc_by_spellnum(creature_ptr, MS_PSY_SPEAR, GF_PSY_SPEAR, g_ptr->m_idx, &dam_max0);
392                 }
393
394                 if (f4 & RF4_ROCKET)
395                     spell_damcalc_by_spellnum(creature_ptr, MS_ROCKET, GF_ROCKET, g_ptr->m_idx, &dam_max0);
396                 if (f4 & RF4_BR_ACID)
397                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_ACID, GF_ACID, g_ptr->m_idx, &dam_max0);
398                 if (f4 & RF4_BR_ELEC)
399                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_ELEC, GF_ELEC, g_ptr->m_idx, &dam_max0);
400                 if (f4 & RF4_BR_FIRE)
401                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_FIRE, GF_FIRE, g_ptr->m_idx, &dam_max0);
402                 if (f4 & RF4_BR_COLD)
403                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_COLD, GF_COLD, g_ptr->m_idx, &dam_max0);
404                 if (f4 & RF4_BR_POIS)
405                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_POIS, GF_POIS, g_ptr->m_idx, &dam_max0);
406                 if (f4 & RF4_BR_NETH)
407                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_NETHER, GF_NETHER, g_ptr->m_idx, &dam_max0);
408                 if (f4 & RF4_BR_LITE)
409                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_LITE, GF_LITE, g_ptr->m_idx, &dam_max0);
410                 if (f4 & RF4_BR_DARK)
411                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_DARK, GF_DARK, g_ptr->m_idx, &dam_max0);
412                 if (f4 & RF4_BR_CONF)
413                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_CONF, GF_CONFUSION, g_ptr->m_idx, &dam_max0);
414                 if (f4 & RF4_BR_SOUN)
415                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_SOUND, GF_SOUND, g_ptr->m_idx, &dam_max0);
416                 if (f4 & RF4_BR_CHAO)
417                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_CHAOS, GF_CHAOS, g_ptr->m_idx, &dam_max0);
418                 if (f4 & RF4_BR_DISE)
419                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_DISEN, GF_DISENCHANT, g_ptr->m_idx, &dam_max0);
420                 if (f4 & RF4_BR_NEXU)
421                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_NEXUS, GF_NEXUS, g_ptr->m_idx, &dam_max0);
422                 if (f4 & RF4_BR_TIME)
423                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_TIME, GF_TIME, g_ptr->m_idx, &dam_max0);
424                 if (f4 & RF4_BR_INER)
425                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_INERTIA, GF_INERTIAL, g_ptr->m_idx, &dam_max0);
426                 if (f4 & RF4_BR_GRAV)
427                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_GRAVITY, GF_GRAVITY, g_ptr->m_idx, &dam_max0);
428                 if (f4 & RF4_BR_SHAR)
429                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_SHARDS, GF_SHARDS, g_ptr->m_idx, &dam_max0);
430                 if (f4 & RF4_BR_PLAS)
431                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_PLASMA, GF_PLASMA, g_ptr->m_idx, &dam_max0);
432                 if (f4 & RF4_BR_WALL)
433                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_FORCE, GF_FORCE, g_ptr->m_idx, &dam_max0);
434                 if (f4 & RF4_BR_MANA)
435                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_MANA, GF_MANA, g_ptr->m_idx, &dam_max0);
436                 if (f4 & RF4_BR_NUKE)
437                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_NUKE, GF_NUKE, g_ptr->m_idx, &dam_max0);
438                 if (f4 & RF4_BR_DISI)
439                     spell_damcalc_by_spellnum(creature_ptr, MS_BR_DISI, GF_DISINTEGRATE, g_ptr->m_idx, &dam_max0);
440             }
441
442             /* Monster melee attacks */
443             if ((r_ptr->flags1 & RF1_NEVER_BLOW) || (d_info[creature_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)) {
444                 dam_max += dam_max0;
445                 continue;
446             }
447
448             if (!(mx <= xx + 1 && mx >= xx - 1 && my <= yy + 1 && my >= yy - 1)) {
449                 dam_max += dam_max0;
450                 continue;
451             }
452
453             int dam_melee = 0;
454             for (int m = 0; m < 4; m++) {
455                 /* Skip non-attacks */
456                 if (!r_ptr->blow[m].method || (r_ptr->blow[m].method == RBM_SHOOT))
457                     continue;
458
459                 /* Extract the attack info */
460                 dam_melee += blow_damcalc(m_ptr, creature_ptr, &r_ptr->blow[m]);
461                 if (r_ptr->blow[m].method == RBM_EXPLODE)
462                     break;
463             }
464
465             if (dam_melee > dam_max0)
466                 dam_max0 = dam_melee;
467             dam_max += dam_max0;
468         }
469     }
470
471     /* Prevent excessive warning */
472     if (dam_max > old_damage) {
473         old_damage = dam_max * 3 / 2;
474
475         if (dam_max > creature_ptr->chp / 2) {
476             object_type *o_ptr = choose_warning_item(creature_ptr);
477
478             if (o_ptr)
479                 describe_flavor(creature_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
480             else
481                 strcpy(o_name, _("体", "body")); /* Warning ability without item */
482             msg_format(_("%sが鋭く震えた!", "Your %s pulsates sharply!"), o_name);
483
484             disturb(creature_ptr, FALSE, TRUE);
485             return get_check(_("本当にこのまま進むか?", "Really want to go ahead? "));
486         }
487     } else
488         old_damage = old_damage / 2;
489
490     g_ptr = &creature_ptr->current_floor_ptr->grid_array[yy][xx];
491     bool is_warning = (!easy_disarm && is_trap(creature_ptr, g_ptr->feat)) || (g_ptr->mimic && is_trap(creature_ptr, g_ptr->feat));
492     is_warning &= !one_in_(13);
493     if (!is_warning)
494         return TRUE;
495
496     object_type *o_ptr = choose_warning_item(creature_ptr);
497     if (o_ptr != NULL)
498         describe_flavor(creature_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
499     else
500         strcpy(o_name, _("体", "body")); /* Warning ability without item */
501     msg_format(_("%sが鋭く震えた!", "Your %s pulsates sharply!"), o_name);
502     disturb(creature_ptr, FALSE, TRUE);
503     return get_check(_("本当にこのまま進むか?", "Really want to go ahead? "));
504 }