OSDN Git Service

[fix] #41503 超能力者でゲームを開始しようとするとクラッシュ
[hengband/hengband.git] / src / monster / monster-util.c
1 #include "monster/monster-util.h"
2 #include "dungeon/dungeon-flag-types.h"
3 #include "dungeon/dungeon.h"
4 #include "dungeon/quest.h"
5 #include "floor/wild.h"
6 #include "game-option/cheat-options.h"
7 #include "grid/grid.h"
8 #include "monster-race/monster-race-hook.h"
9 #include "monster-race/monster-race.h"
10 #include "monster-race/race-flags-ability1.h"
11 #include "monster-race/race-flags-ability2.h"
12 #include "monster-race/race-flags1.h"
13 #include "monster-race/race-flags4.h"
14 #include "monster-race/race-flags7.h"
15 #include "monster-race/race-indice-types.h"
16 #include "mspell/mspell-mask-definitions.h"
17 #include "spell/summon-types.h"
18 #include "system/alloc-entries.h"
19 #include "system/floor-type-definition.h"
20 #include "util/bit-flags-calculator.h"
21 #include "view/display-messages.h"
22
23 typedef enum dungeon_mode_type {
24     DUNGEON_MODE_AND = 1,
25     DUNGEON_MODE_NAND = 2,
26     DUNGEON_MODE_OR = 3,
27     DUNGEON_MODE_NOR = 4,
28 } dungeon_mode_type;
29
30 MONSTER_IDX hack_m_idx = 0; /* Hack -- see "process_monsters()" */
31 MONSTER_IDX hack_m_idx_ii = 0;
32
33 /*!
34  * @var chameleon_change_m_idx
35  * @brief カメレオンの変身先モンスターIDを受け渡すためのグローバル変数
36  * @todo 変数渡しの問題などもあるができればchameleon_change_m_idxのグローバル変数を除去し、関数引き渡しに移行すること
37  */
38 int chameleon_change_m_idx = 0;
39
40 /*!
41  * @var summon_specific_type
42  * @brief 召喚条件を指定するグローバル変数 / Hack -- the "type" of the current "summon specific"
43  * @todo summon_specific_typeグローバル変数の除去と関数引数への代替を行う
44  */
45 summon_type summon_specific_type = SUMMON_NONE;
46
47 /*!
48  * @brief 指定されたモンスター種族がダンジョンの制限にかかるかどうかをチェックする / Some dungeon types restrict the possible monsters.
49  * @param player_ptr プレーヤーへの参照ポインタ
50  * @param r_idx チェックするモンスター種族ID
51  * @return 召喚条件が一致するならtrue / Return TRUE is the monster is OK and FALSE otherwise
52  */
53 static bool restrict_monster_to_dungeon(player_type *player_ptr, MONRACE_IDX r_idx)
54 {
55     DUNGEON_IDX d_idx = player_ptr->dungeon_idx;
56     dungeon_type *d_ptr = &d_info[d_idx];
57     monster_race *r_ptr = &r_info[r_idx];
58
59     if (d_ptr->flags1 & DF1_CHAMELEON) {
60         if (chameleon_change_m_idx)
61             return TRUE;
62     }
63
64     if (d_ptr->flags1 & DF1_NO_MAGIC) {
65         if (r_idx != MON_CHAMELEON && r_ptr->freq_spell && !(r_ptr->flags4 & RF4_NOMAGIC_MASK) && !(r_ptr->a_ability_flags1 & RF5_NOMAGIC_MASK)
66             && !(r_ptr->a_ability_flags2 & RF6_NOMAGIC_MASK))
67             return FALSE;
68     }
69
70     if (d_ptr->flags1 & DF1_NO_MELEE) {
71         if (r_idx == MON_CHAMELEON)
72             return TRUE;
73         if (!(r_ptr->flags4 & (RF4_BOLT_MASK | RF4_BEAM_MASK | RF4_BALL_MASK))
74             && !(r_ptr->a_ability_flags1
75                 & (RF5_BOLT_MASK | RF5_BEAM_MASK | RF5_BALL_MASK | RF5_CAUSE_1 | RF5_CAUSE_2 | RF5_CAUSE_3 | RF5_CAUSE_4 | RF5_MIND_BLAST | RF5_BRAIN_SMASH))
76             && !(r_ptr->a_ability_flags2 & (RF6_BOLT_MASK | RF6_BEAM_MASK | RF6_BALL_MASK)))
77             return FALSE;
78     }
79
80     floor_type *floor_ptr = player_ptr->current_floor_ptr;
81     if (d_ptr->flags1 & DF1_BEGINNER) {
82         if (r_ptr->level > floor_ptr->dun_level)
83             return FALSE;
84     }
85
86     if (d_ptr->special_div >= 64)
87         return TRUE;
88     if (summon_specific_type && !(d_ptr->flags1 & DF1_CHAMELEON))
89         return TRUE;
90
91     byte a;
92     switch (d_ptr->mode) {
93     case DUNGEON_MODE_AND: {
94         if (d_ptr->mflags1) {
95             if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
96                 return FALSE;
97         }
98
99         if (d_ptr->mflags2) {
100             if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
101                 return FALSE;
102         }
103
104         if (d_ptr->mflags3) {
105             if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
106                 return FALSE;
107         }
108
109         if (d_ptr->mflags4) {
110             if ((d_ptr->mflags4 & r_ptr->flags4) != d_ptr->mflags4)
111                 return FALSE;
112         }
113
114         if (d_ptr->m_a_ability_flags1) {
115             if ((d_ptr->m_a_ability_flags1 & r_ptr->a_ability_flags1) != d_ptr->m_a_ability_flags1)
116                 return FALSE;
117         }
118
119         if (d_ptr->m_a_ability_flags2) {
120             if ((d_ptr->m_a_ability_flags2 & r_ptr->a_ability_flags2) != d_ptr->m_a_ability_flags2)
121                 return FALSE;
122         }
123
124         if (d_ptr->mflags7) {
125             if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
126                 return FALSE;
127         }
128
129         if (d_ptr->mflags8) {
130             if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
131                 return FALSE;
132         }
133
134         if (d_ptr->mflags9) {
135             if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
136                 return FALSE;
137         }
138
139         if (d_ptr->mflagsr) {
140             if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
141                 return FALSE;
142         }
143
144         for (a = 0; a < 5; a++)
145             if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char))
146                 return FALSE;
147
148         return TRUE;
149     }
150     case DUNGEON_MODE_NAND: {
151         if (d_ptr->mflags1) {
152             if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
153                 return TRUE;
154         }
155
156         if (d_ptr->mflags2) {
157             if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
158                 return TRUE;
159         }
160
161         if (d_ptr->mflags3) {
162             if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
163                 return TRUE;
164         }
165
166         if (d_ptr->mflags4) {
167             if ((d_ptr->mflags4 & r_ptr->flags4) != d_ptr->mflags4)
168                 return TRUE;
169         }
170
171         if (d_ptr->m_a_ability_flags1) {
172             if ((d_ptr->m_a_ability_flags1 & r_ptr->a_ability_flags1) != d_ptr->m_a_ability_flags1)
173                 return TRUE;
174         }
175
176         if (d_ptr->m_a_ability_flags2) {
177             if ((d_ptr->m_a_ability_flags2 & r_ptr->a_ability_flags2) != d_ptr->m_a_ability_flags2)
178                 return TRUE;
179         }
180
181         if (d_ptr->mflags7) {
182             if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
183                 return TRUE;
184         }
185
186         if (d_ptr->mflags8) {
187             if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
188                 return TRUE;
189         }
190
191         if (d_ptr->mflags9) {
192             if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
193                 return TRUE;
194         }
195
196         if (d_ptr->mflagsr) {
197             if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
198                 return TRUE;
199         }
200
201         for (a = 0; a < 5; a++)
202             if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char))
203                 return TRUE;
204
205         return FALSE;
206     }
207     case DUNGEON_MODE_OR: {
208         if (r_ptr->flags1 & d_ptr->mflags1)
209             return TRUE;
210         if (r_ptr->flags2 & d_ptr->mflags2)
211             return TRUE;
212         if (r_ptr->flags3 & d_ptr->mflags3)
213             return TRUE;
214         if (r_ptr->flags4 & d_ptr->mflags4)
215             return TRUE;
216         if (r_ptr->a_ability_flags1 & d_ptr->m_a_ability_flags1)
217             return TRUE;
218         if (r_ptr->a_ability_flags2 & d_ptr->m_a_ability_flags2)
219             return TRUE;
220         if (r_ptr->flags7 & d_ptr->mflags7)
221             return TRUE;
222         if (r_ptr->flags8 & d_ptr->mflags8)
223             return TRUE;
224         if (r_ptr->flags9 & d_ptr->mflags9)
225             return TRUE;
226         if (r_ptr->flagsr & d_ptr->mflagsr)
227             return TRUE;
228         for (a = 0; a < 5; a++)
229             if (d_ptr->r_char[a] == r_ptr->d_char)
230                 return TRUE;
231
232         return FALSE;
233     }
234     case DUNGEON_MODE_NOR: {
235         if (r_ptr->flags1 & d_ptr->mflags1)
236             return FALSE;
237         if (r_ptr->flags2 & d_ptr->mflags2)
238             return FALSE;
239         if (r_ptr->flags3 & d_ptr->mflags3)
240             return FALSE;
241         if (r_ptr->flags4 & d_ptr->mflags4)
242             return FALSE;
243         if (r_ptr->a_ability_flags1 & d_ptr->m_a_ability_flags1)
244             return FALSE;
245         if (r_ptr->a_ability_flags2 & d_ptr->m_a_ability_flags2)
246             return FALSE;
247         if (r_ptr->flags7 & d_ptr->mflags7)
248             return FALSE;
249         if (r_ptr->flags8 & d_ptr->mflags8)
250             return FALSE;
251         if (r_ptr->flags9 & d_ptr->mflags9)
252             return FALSE;
253         if (r_ptr->flagsr & d_ptr->mflagsr)
254             return FALSE;
255         for (a = 0; a < 5; a++)
256             if (d_ptr->r_char[a] == r_ptr->d_char)
257                 return FALSE;
258
259         return TRUE;
260     }
261     }
262
263     return TRUE;
264 }
265
266 /*!
267  * @brief プレイヤーの現在の広域マップ座標から得た地勢を元にモンスターの生成条件関数を返す
268  * @param player_ptr プレーヤーへの参照ポインタ
269  * @return 地勢にあったモンスターの生成条件関数
270  */
271 monsterrace_hook_type get_monster_hook(player_type *player_ptr)
272 {
273     if ((player_ptr->current_floor_ptr->dun_level > 0) || (player_ptr->current_floor_ptr->inside_quest > 0))
274         return (monsterrace_hook_type)mon_hook_dungeon;
275
276     switch (wilderness[player_ptr->wilderness_y][player_ptr->wilderness_x].terrain) {
277     case TERRAIN_TOWN:
278         return (monsterrace_hook_type)mon_hook_town;
279     case TERRAIN_DEEP_WATER:
280         return (monsterrace_hook_type)mon_hook_ocean;
281     case TERRAIN_SHALLOW_WATER:
282     case TERRAIN_SWAMP:
283         return (monsterrace_hook_type)mon_hook_shore;
284     case TERRAIN_DIRT:
285     case TERRAIN_DESERT:
286         return (monsterrace_hook_type)mon_hook_waste;
287     case TERRAIN_GRASS:
288         return (monsterrace_hook_type)mon_hook_grass;
289     case TERRAIN_TREES:
290         return (monsterrace_hook_type)mon_hook_wood;
291     case TERRAIN_SHALLOW_LAVA:
292     case TERRAIN_DEEP_LAVA:
293         return (monsterrace_hook_type)mon_hook_volcano;
294     case TERRAIN_MOUNTAIN:
295         return (monsterrace_hook_type)mon_hook_mountain;
296     default:
297         return (monsterrace_hook_type)mon_hook_dungeon;
298     }
299 }
300
301 /*!
302  * @brief 指定された広域マップ座標の地勢を元にモンスターの生成条件関数を返す
303  * @return 地勢にあったモンスターの生成条件関数
304  */
305 monsterrace_hook_type get_monster_hook2(player_type *player_ptr, POSITION y, POSITION x)
306 {
307     feature_type *f_ptr = &f_info[player_ptr->current_floor_ptr->grid_array[y][x].feat];
308     if (has_flag(f_ptr->flags, FF_WATER))
309         return has_flag(f_ptr->flags, FF_DEEP) ? (monsterrace_hook_type)mon_hook_deep_water : (monsterrace_hook_type)mon_hook_shallow_water;
310
311     if (has_flag(f_ptr->flags, FF_LAVA))
312         return (monsterrace_hook_type)mon_hook_lava;
313
314     return (monsterrace_hook_type)mon_hook_floor;
315 }
316
317 /*!
318  * @brief モンスター生成制限関数最大2つから / Apply a "monster restriction function" to the "monster allocation table"
319  * @param player_ptr プレーヤーへの参照ポインタ
320  * @param monster_hook 制限関数1
321  * @param monster_hook2 制限関数2
322  * @return エラーコード
323  */
324 errr get_mon_num_prep(player_type *player_ptr, monsterrace_hook_type monster_hook, monsterrace_hook_type monster_hook2)
325 {
326     int mon_num = 0;
327     DEPTH lev_min = 127;
328     DEPTH lev_max = 0;
329     int total = 0;
330     floor_type *floor_ptr = player_ptr->current_floor_ptr;
331     for (int i = 0; i < alloc_race_size; i++) {
332         alloc_entry *entry = &alloc_race_table[i];
333         entry->prob2 = 0;
334         monster_race *r_ptr = &r_info[entry->index];
335         if (((monster_hook != NULL) && !((*monster_hook)(player_ptr, entry->index)))
336             || ((monster_hook2 != NULL) && !((*monster_hook2)(player_ptr, entry->index))))
337             continue;
338
339         if (!player_ptr->phase_out && !chameleon_change_m_idx && summon_specific_type != SUMMON_GUARDIANS) {
340             if (r_ptr->flags1 & RF1_QUESTOR)
341                 continue;
342
343             if (r_ptr->flags7 & RF7_GUARDIAN)
344                 continue;
345
346             if (((r_ptr->flags1 & RF1_FORCE_DEPTH) != 0) && (r_ptr->level > floor_ptr->dun_level))
347                 continue;
348         }
349
350         if (entry->prob1 <= 0)
351             continue;
352
353         mon_num++;
354         if (lev_min > entry->level)
355             lev_min = entry->level;
356         if (lev_max < entry->level)
357             lev_max = entry->level;
358
359         entry->prob2 = entry->prob1;
360         if (floor_ptr->dun_level && (!floor_ptr->inside_quest || is_fixed_quest_idx(floor_ptr->inside_quest))
361             && !restrict_monster_to_dungeon(player_ptr, entry->index) && !player_ptr->phase_out) {
362             int hoge = entry->prob2 * d_info[player_ptr->dungeon_idx].special_div;
363             entry->prob2 = hoge / 64;
364             if (randint0(64) < (hoge & 0x3f))
365                 entry->prob2++;
366             if (entry->prob2 <= 0)
367                 entry->prob2 = 1;
368         }
369
370         total += entry->prob2;
371     }
372     if (cheat_hear) {
373         msg_format(_("モンスター第2次候補数:%d(%d-%dF)%d ", "monster second selection:%d(%d-%dF)&d "), mon_num, lev_min, lev_max, total);
374     }
375     return 0;
376 }