OSDN Git Service

Merge pull request #36 from taotao54321/fix-wrong-evasion-message
[hengbandforosx/hengbandosx.git] / src / mspell / mspell-attack.c
1 #include "mspell/mspell-attack.h"
2 #include "blue-magic/blue-magic-checker.h"
3 #include "core/disturbance.h"
4 #include "core/player-redraw-types.h"
5 #include "dungeon/dungeon-flag-types.h"
6 #include "dungeon/dungeon.h"
7 #include "dungeon/quest.h"
8 #include "floor/cave.h"
9 #include "monster-floor/monster-move.h"
10 #include "monster-race/monster-race.h"
11 #include "monster-race/race-flags-ability1.h"
12 #include "monster-race/race-flags-ability2.h"
13 #include "monster-race/race-flags2.h"
14 #include "monster-race/race-flags4.h"
15 #include "monster-race/race-indice-types.h"
16 #include "monster/monster-describer.h"
17 #include "monster/monster-flag-types.h"
18 #include "monster/monster-info.h"
19 #include "monster/monster-status.h"
20 #include "mspell/assign-monster-spell.h"
21 #include "mspell/improper-mspell-remover.h"
22 #include "mspell/mspell-attack-util.h"
23 #include "mspell/mspell-checker.h"
24 #include "mspell/mspell-lite.h"
25 #include "mspell/mspell-mask-definitions.h"
26 #include "mspell/mspell-selector.h"
27 #include "mspell/mspell-util.h"
28 #include "player/attack-defense-types.h"
29 #include "spell-kind/spells-world.h"
30 #include "spell-realm/spells-hex.h"
31 #include "system/floor-type-definition.h"
32 #include "target/projection-path-calculator.h"
33 #include "view/display-messages.h"
34 #include "world/world.h"
35 #ifdef JP
36 #else
37 #include "monster/monster-description-types.h"
38 #endif
39
40 static void set_no_magic_mask(msa_type *msa_ptr)
41 {
42     if (!msa_ptr->no_inate)
43         return;
44
45     msa_ptr->f4 &= ~(RF4_NOMAGIC_MASK);
46     msa_ptr->f5 &= ~(RF5_NOMAGIC_MASK);
47     msa_ptr->f6 &= ~(RF6_NOMAGIC_MASK);
48 }
49
50 static void check_mspell_stupid(player_type *target_ptr, msa_type *msa_ptr)
51 {
52     floor_type *floor_ptr = target_ptr->current_floor_ptr;
53     msa_ptr->in_no_magic_dungeon = (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MAGIC) && floor_ptr->dun_level
54         && (!floor_ptr->inside_quest || is_fixed_quest_idx(floor_ptr->inside_quest));
55     if (!msa_ptr->in_no_magic_dungeon || ((msa_ptr->r_ptr->flags2 & RF2_STUPID) != 0))
56         return;
57
58     msa_ptr->f4 &= RF4_NOMAGIC_MASK;
59     msa_ptr->f5 &= RF5_NOMAGIC_MASK;
60     msa_ptr->f6 &= RF6_NOMAGIC_MASK;
61 }
62
63 static void check_mspell_smart(player_type *target_ptr, msa_type *msa_ptr)
64 {
65     if ((msa_ptr->r_ptr->flags2 & RF2_SMART) == 0)
66         return;
67
68     if ((msa_ptr->m_ptr->hp < msa_ptr->m_ptr->maxhp / 10) && (randint0(100) < 50)) {
69         msa_ptr->f4 &= (RF4_INT_MASK);
70         msa_ptr->f5 &= (RF5_INT_MASK);
71         msa_ptr->f6 &= (RF6_INT_MASK);
72     }
73
74     if ((msa_ptr->f6 & RF6_TELE_LEVEL) && is_teleport_level_ineffective(target_ptr, 0)) {
75         msa_ptr->f6 &= ~(RF6_TELE_LEVEL);
76     }
77 }
78
79 static void check_mspell_arena(player_type *target_ptr, msa_type *msa_ptr)
80 {
81     if (!target_ptr->current_floor_ptr->inside_arena && !target_ptr->phase_out)
82         return;
83
84     msa_ptr->f4 &= ~(RF4_SUMMON_MASK);
85     msa_ptr->f5 &= ~(RF5_SUMMON_MASK);
86     msa_ptr->f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
87
88     if (msa_ptr->m_ptr->r_idx == MON_ROLENTO)
89         msa_ptr->f6 &= ~(RF6_SPECIAL);
90 }
91
92 static bool check_mspell_non_stupid(player_type *target_ptr, msa_type *msa_ptr)
93 {
94     if ((msa_ptr->r_ptr->flags2 & RF2_STUPID) != 0)
95         return TRUE;
96
97     if (!target_ptr->csp)
98         msa_ptr->f5 &= ~(RF5_DRAIN_MANA);
99
100     if (((msa_ptr->f4 & RF4_BOLT_MASK) || (msa_ptr->f5 & RF5_BOLT_MASK) || (msa_ptr->f6 & RF6_BOLT_MASK))
101         && !clean_shot(target_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, target_ptr->y, target_ptr->x, FALSE)) {
102         msa_ptr->f4 &= ~(RF4_BOLT_MASK);
103         msa_ptr->f5 &= ~(RF5_BOLT_MASK);
104         msa_ptr->f6 &= ~(RF6_BOLT_MASK);
105     }
106
107     if (((msa_ptr->f4 & RF4_SUMMON_MASK) || (msa_ptr->f5 & RF5_SUMMON_MASK) || (msa_ptr->f6 & RF6_SUMMON_MASK))
108         && !(summon_possible(target_ptr, msa_ptr->y, msa_ptr->x))) {
109         msa_ptr->f4 &= ~(RF4_SUMMON_MASK);
110         msa_ptr->f5 &= ~(RF5_SUMMON_MASK);
111         msa_ptr->f6 &= ~(RF6_SUMMON_MASK);
112     }
113
114     if ((msa_ptr->f6 & RF6_RAISE_DEAD) && !raise_possible(target_ptr, msa_ptr->m_ptr))
115         msa_ptr->f6 &= ~(RF6_RAISE_DEAD);
116
117     if (((msa_ptr->f6 & RF6_SPECIAL) != 0) && (msa_ptr->m_ptr->r_idx == MON_ROLENTO) && !summon_possible(target_ptr, msa_ptr->y, msa_ptr->x))
118         msa_ptr->f6 &= ~(RF6_SPECIAL);
119
120     return (msa_ptr->f4 != 0) || (msa_ptr->f5 != 0) || (msa_ptr->f6 != 0);
121 }
122
123 static void set_mspell_list(msa_type *msa_ptr)
124 {
125     for (int k = 0; k < 32; k++)
126         if (msa_ptr->f4 & (1L << k))
127             msa_ptr->mspells[msa_ptr->num++] = k + RF4_SPELL_START;
128
129     for (int k = 0; k < 32; k++)
130         if (msa_ptr->f5 & (1L << k))
131             msa_ptr->mspells[msa_ptr->num++] = k + RF5_SPELL_START;
132
133     for (int k = 0; k < 32; k++)
134         if (msa_ptr->f6 & (1L << k))
135             msa_ptr->mspells[msa_ptr->num++] = k + RF6_SPELL_START;
136 }
137
138 static void describe_mspell_monster(player_type *target_ptr, msa_type *msa_ptr)
139 {
140     monster_desc(target_ptr, msa_ptr->m_name, msa_ptr->m_ptr, 0x00);
141
142 #ifdef JP
143 #else
144     /* Get the monster possessive ("his"/"her"/"its") */
145     char m_poss[80];
146     monster_desc(target_ptr, m_poss, msa_ptr->m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
147 #endif
148 }
149
150 static bool switch_do_spell(player_type *target_ptr, msa_type *msa_ptr)
151 {
152     switch (msa_ptr->do_spell) {
153     case DO_SPELL_NONE: {
154         int attempt = 10;
155         while (attempt--) {
156             msa_ptr->thrown_spell = choose_attack_spell(target_ptr, msa_ptr);
157             if (msa_ptr->thrown_spell)
158                 break;
159         }
160
161         return TRUE;
162     }
163     case DO_SPELL_BR_LITE:
164         msa_ptr->thrown_spell = 96 + 14; /* RF4_BR_LITE */
165         return TRUE;
166     case DO_SPELL_BR_DISI:
167         msa_ptr->thrown_spell = 96 + 31; /* RF4_BR_DISI */
168         return TRUE;
169     case DO_SPELL_BA_LITE:
170         msa_ptr->thrown_spell = 128 + 20; /* RF5_BA_LITE */
171         return TRUE;
172     default:
173         return FALSE;
174     }
175 }
176
177 static bool check_mspell_continuation(player_type *target_ptr, msa_type *msa_ptr)
178 {
179     if ((msa_ptr->f4 == 0) && (msa_ptr->f5 == 0) && (msa_ptr->f6 == 0))
180         return FALSE;
181
182     remove_bad_spells(msa_ptr->m_idx, target_ptr, &msa_ptr->f4, &msa_ptr->f5, &msa_ptr->f6);
183     check_mspell_arena(target_ptr, msa_ptr);
184     if (((msa_ptr->f4 == 0) && (msa_ptr->f5 == 0) && (msa_ptr->f6 == 0)) || !check_mspell_non_stupid(target_ptr, msa_ptr))
185         return FALSE;
186
187     set_mspell_list(msa_ptr);
188     if ((msa_ptr->num == 0) || !target_ptr->playing || target_ptr->is_dead || target_ptr->leaving)
189         return FALSE;
190
191     describe_mspell_monster(target_ptr, msa_ptr);
192     if (!switch_do_spell(target_ptr, msa_ptr) || (msa_ptr->thrown_spell == 0))
193         return FALSE;
194
195     return TRUE;
196 }
197
198 static bool check_mspell_unexploded(player_type *target_ptr, msa_type *msa_ptr)
199 {
200     PERCENTAGE fail_rate = 25 - (msa_ptr->rlev + 3) / 4;
201     if (msa_ptr->r_ptr->flags2 & RF2_STUPID)
202         fail_rate = 0;
203
204     if (!spell_is_inate(msa_ptr->thrown_spell)
205         && (msa_ptr->in_no_magic_dungeon || (monster_stunned_remaining(msa_ptr->m_ptr) && one_in_(2)) || (randint0(100) < fail_rate))) {
206         disturb(target_ptr, TRUE, TRUE);
207         msg_format(_("%^sは呪文を唱えようとしたが失敗した。", "%^s tries to cast a spell, but fails."), msa_ptr->m_name);
208         return TRUE;
209     }
210
211     if (!spell_is_inate(msa_ptr->thrown_spell) && magic_barrier(target_ptr, msa_ptr->m_idx)) {
212         msg_format(_("反魔法バリアが%^sの呪文をかき消した。", "Anti magic barrier cancels the spell which %^s casts."), msa_ptr->m_name);
213         return TRUE;
214     }
215
216     return FALSE;
217 }
218
219 static bool check_thrown_mspell(player_type *target_ptr, msa_type *msa_ptr)
220 {
221     bool direct = player_bold(target_ptr, msa_ptr->y, msa_ptr->x);
222     msa_ptr->can_remember = is_original_ap_and_seen(target_ptr, msa_ptr->m_ptr);
223     if (direct)
224         return TRUE;
225
226     switch (msa_ptr->thrown_spell) {
227     case 96 + 2: /* RF4_DISPEL */
228     case 96 + 4: /* RF4_SHOOT */
229     case 128 + 9: /* RF5_DRAIN_MANA */
230     case 128 + 10: /* RF5_MIND_BLAST */
231     case 128 + 11: /* RF5_BRAIN_SMASH */
232     case 128 + 12: /* RF5_CAUSE_1 */
233     case 128 + 13: /* RF5_CAUSE_2 */
234     case 128 + 14: /* RF5_CAUSE_3 */
235     case 128 + 15: /* RF5_CAUSE_4 */
236     case 128 + 16: /* RF5_BO_ACID */
237     case 128 + 17: /* RF5_BO_ELEC */
238     case 128 + 18: /* RF5_BO_FIRE */
239     case 128 + 19: /* RF5_BO_COLD */
240     case 128 + 21: /* RF5_BO_NETH */
241     case 128 + 22: /* RF5_BO_WATE */
242     case 128 + 23: /* RF5_BO_MANA */
243     case 128 + 24: /* RF5_BO_PLAS */
244     case 128 + 25: /* RF5_BO_ICEE */
245     case 128 + 26: /* RF5_MISSILE */
246     case 128 + 27: /* RF5_SCARE */
247     case 128 + 28: /* RF5_BLIND */
248     case 128 + 29: /* RF5_CONF */
249     case 128 + 30: /* RF5_SLOW */
250     case 128 + 31: /* RF5_HOLD */
251     case 160 + 1: /* RF6_HAND_DOOM */
252     case 160 + 8: /* RF6_TELE_TO */
253     case 160 + 9: /* RF6_TELE_AWAY */
254     case 160 + 10: /* RF6_TELE_LEVEL */
255     case 160 + 11: /* RF6_PSY_SPEAR */
256     case 160 + 12: /* RF6_DARKNESS */
257     case 160 + 14: /* RF6_FORGET */
258         return FALSE;
259     default:
260         return TRUE;
261     }
262 }
263
264 static void check_mspell_imitation(player_type *target_ptr, msa_type *msa_ptr)
265 {
266     bool seen = (!target_ptr->blind && msa_ptr->m_ptr->ml);
267     bool can_imitate = player_has_los_bold(target_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx);
268     if (!seen || !can_imitate || (current_world_ptr->timewalk_m_idx != 0) || (target_ptr->pclass != CLASS_IMITATOR))
269         return;
270
271     /* Not RF6_SPECIAL */
272     if (msa_ptr->thrown_spell == 167)
273         return;
274
275     if (target_ptr->mane_num == MAX_MANE) {
276         target_ptr->mane_num--;
277         for (int i = 0; i < target_ptr->mane_num; i++) {
278             target_ptr->mane_spell[i] = target_ptr->mane_spell[i + 1];
279             target_ptr->mane_dam[i] = target_ptr->mane_dam[i + 1];
280         }
281     }
282
283     target_ptr->mane_spell[target_ptr->mane_num] = msa_ptr->thrown_spell - 96;
284     target_ptr->mane_dam[target_ptr->mane_num] = msa_ptr->dam;
285     target_ptr->mane_num++;
286     target_ptr->new_mane = TRUE;
287     target_ptr->redraw |= PR_IMITATION;
288 }
289
290 static void remember_mspell(msa_type *msa_ptr)
291 {
292     if (!msa_ptr->can_remember)
293         return;
294
295     if (msa_ptr->thrown_spell < 32 * 4) {
296         msa_ptr->r_ptr->r_flags4 |= (1L << (msa_ptr->thrown_spell - 32 * 3));
297         if (msa_ptr->r_ptr->r_cast_spell < MAX_UCHAR)
298             msa_ptr->r_ptr->r_cast_spell++;
299
300         return;
301     }
302     
303     if (msa_ptr->thrown_spell < 32 * 5) {
304         msa_ptr->r_ptr->r_flags5 |= (1L << (msa_ptr->thrown_spell - 32 * 4));
305         if (msa_ptr->r_ptr->r_cast_spell < MAX_UCHAR)
306             msa_ptr->r_ptr->r_cast_spell++;
307
308         return;
309     }
310     
311     if (msa_ptr->thrown_spell < 32 * 6) {
312         msa_ptr->r_ptr->r_flags6 |= (1L << (msa_ptr->thrown_spell - 32 * 5));
313         if (msa_ptr->r_ptr->r_cast_spell < MAX_UCHAR)
314             msa_ptr->r_ptr->r_cast_spell++;
315     }
316 }
317
318 /*!
319  * @brief モンスターの特殊技能メインルーチン /
320  * Creatures can cast spells, shoot missiles, and breathe.
321  * @param target_ptr プレーヤーへの参照ポインタ
322  * @param m_idx モンスター構造体配列のID
323  * @return 実際に特殊技能を利用したらTRUEを返す
324  */
325 bool make_attack_spell(player_type *target_ptr, MONSTER_IDX m_idx)
326 {
327     msa_type tmp_msa;
328     msa_type *msa_ptr = initialize_msa_type(target_ptr, &tmp_msa, m_idx);
329     if (monster_confused_remaining(msa_ptr->m_ptr)) {
330         reset_target(msa_ptr->m_ptr);
331         return FALSE;
332     }
333
334     if (((msa_ptr->m_ptr->mflag & MFLAG_PREVENT_MAGIC) != 0) || !is_hostile(msa_ptr->m_ptr)
335         || ((msa_ptr->m_ptr->cdis > get_max_range(target_ptr)) && !msa_ptr->m_ptr->target_y))
336         return FALSE;
337
338     decide_lite_range(target_ptr, msa_ptr);
339     if (!decide_lite_projection(target_ptr, msa_ptr))
340         return FALSE;
341
342     reset_target(msa_ptr->m_ptr);
343     msa_ptr->rlev = ((msa_ptr->r_ptr->level >= 1) ? msa_ptr->r_ptr->level : 1);
344     set_no_magic_mask(msa_ptr);
345     decide_lite_area(target_ptr, msa_ptr);
346     check_mspell_stupid(target_ptr, msa_ptr);
347     check_mspell_smart(target_ptr, msa_ptr);
348     if (!check_mspell_continuation(target_ptr, msa_ptr))
349         return FALSE;
350
351     if (check_mspell_unexploded(target_ptr, msa_ptr))
352         return TRUE;
353
354     if (!check_thrown_mspell(target_ptr, msa_ptr))
355         return FALSE;
356
357     msa_ptr->dam = monspell_to_player(target_ptr, msa_ptr->thrown_spell, msa_ptr->y, msa_ptr->x, m_idx);
358     if (msa_ptr->dam < 0)
359         return FALSE;
360
361     if ((target_ptr->action == ACTION_LEARN) && msa_ptr->thrown_spell > 175)
362         learn_spell(target_ptr, msa_ptr->thrown_spell - 96);
363
364     check_mspell_imitation(target_ptr, msa_ptr);
365     remember_mspell(msa_ptr);
366     if (target_ptr->is_dead && (msa_ptr->r_ptr->r_deaths < MAX_SHORT) && !target_ptr->current_floor_ptr->inside_arena)
367         msa_ptr->r_ptr->r_deaths++;
368
369     return TRUE;
370 }