OSDN Git Service

[Refactor] 無駄な空白、改行の削除、includeのソート
[hengband/hengband.git] / src / monster-floor / monster-generator.c
1 /*!
2  * todo 後で再分割する
3  * @brief モンスター生成処理
4  * @date 2020/06/10
5  * @author Hourier
6  */
7
8 #include "monster-floor/monster-generator.h"
9 #include "dungeon/dungeon.h"
10 #include "effect/effect-characteristics.h"
11 #include "floor/cave.h"
12 #include "floor/floor-util.h"
13 #include "game-option/cheat-options.h"
14 #include "grid/grid.h"
15 #include "monster-floor/one-monster-placer.h"
16 #include "monster-floor/place-monster-types.h"
17 #include "monster-race/monster-race-hook.h"
18 #include "monster-race/monster-race.h"
19 #include "monster-race/race-flags1.h"
20 #include "monster-race/race-flags7.h"
21 #include "monster-race/race-indice-types.h"
22 #include "monster/monster-flag-types.h"
23 #include "monster/monster-info.h"
24 #include "monster/monster-list.h"
25 #include "monster/monster-util.h"
26 #include "monster/smart-learn-types.h"
27 #include "mspell/summon-checker.h"
28 #include "spell/summon-types.h"
29 #include "system/floor-type-definition.h"
30 #include "target/projection-path-calculator.h"
31 #include "util/string-processor.h"
32 #include "view/display-messages.h"
33
34 #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */
35
36 /*!
37  * @var place_monster_idx
38  * @brief 護衛対象となるモンスター種族IDを渡すグローバル変数 / Hack -- help pick an escort type
39  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_idxを除去し、関数引数化する
40  */
41 static MONSTER_IDX place_monster_idx = 0;
42
43 /*!
44  * @var place_monster_m_idx
45  * @brief 護衛対象となるモンスターIDを渡すグローバル変数 / Hack -- help pick an escort type
46  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_m_idxを除去し、関数引数化する
47  */
48 static MONSTER_IDX place_monster_m_idx = 0;
49
50 /*!
51  * @brief モンスター1体を目標地点に可能な限り近い位置に生成する / improved version of scatter() for place monster
52  * @param player_ptr プレーヤーへの参照ポインタ
53  * @param r_idx 生成モンスター種族
54  * @param yp 結果生成位置y座標
55  * @param xp 結果生成位置x座標
56  * @param y 中心生成位置y座標
57  * @param x 中心生成位置x座標
58  * @param max_dist 生成位置の最大半径
59  * @return 成功したらtrue
60  *
61  */
62 bool mon_scatter(player_type *player_ptr, MONRACE_IDX r_idx, POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION max_dist)
63 {
64     POSITION place_x[MON_SCAT_MAXD];
65     POSITION place_y[MON_SCAT_MAXD];
66     int num[MON_SCAT_MAXD];
67
68     if (max_dist >= MON_SCAT_MAXD)
69         return FALSE;
70
71     int i;
72     for (i = 0; i < MON_SCAT_MAXD; i++)
73         num[i] = 0;
74
75     floor_type *floor_ptr = player_ptr->current_floor_ptr;
76     for (POSITION nx = x - max_dist; nx <= x + max_dist; nx++) {
77         for (POSITION ny = y - max_dist; ny <= y + max_dist; ny++) {
78             if (!in_bounds(floor_ptr, ny, nx))
79                 continue;
80             if (!projectable(player_ptr, y, x, ny, nx))
81                 continue;
82             if (r_idx > 0) {
83                 monster_race *r_ptr = &r_info[r_idx];
84                 if (!monster_can_enter(player_ptr, ny, nx, r_ptr, 0))
85                     continue;
86             } else {
87                 if (!is_cave_empty_bold2(player_ptr, ny, nx))
88                     continue;
89                 if (pattern_tile(floor_ptr, ny, nx))
90                     continue;
91             }
92
93             i = distance(y, x, ny, nx);
94             if (i > max_dist)
95                 continue;
96
97             num[i]++;
98             if (one_in_(num[i])) {
99                 place_x[i] = nx;
100                 place_y[i] = ny;
101             }
102         }
103     }
104
105     i = 0;
106     while (i < MON_SCAT_MAXD && 0 == num[i])
107         i++;
108     if (i >= MON_SCAT_MAXD)
109         return FALSE;
110
111     *xp = place_x[i];
112     *yp = place_y[i];
113
114     return TRUE;
115 }
116
117 /*!
118  * @brief モンスターを増殖生成する / Let the given monster attempt to reproduce.
119  * @param player_ptr プレーヤーへの参照ポインタ
120  * @param m_idx 増殖するモンスター情報ID
121  * @param clone クローン・モンスター処理ならばtrue
122  * @param mode 生成オプション
123  * @return 生成できたらtrueを返す
124  * @details
125  * Note that "reproduction" REQUIRES empty space.
126  */
127 bool multiply_monster(player_type *player_ptr, MONSTER_IDX m_idx, bool clone, BIT_FLAGS mode)
128 {
129     floor_type *floor_ptr = player_ptr->current_floor_ptr;
130     monster_type *m_ptr = &floor_ptr->m_list[m_idx];
131     POSITION y, x;
132     if (!mon_scatter(player_ptr, m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1))
133         return FALSE;
134
135     if (m_ptr->mflag2 & MFLAG2_NOPET)
136         mode |= PM_NO_PET;
137
138     if (!place_monster_aux(player_ptr, m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY)))
139         return FALSE;
140
141     if (clone || (m_ptr->smart & SM_CLONED)) {
142         floor_ptr->m_list[hack_m_idx_ii].smart |= SM_CLONED;
143         floor_ptr->m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
144     }
145
146     return TRUE;
147 }
148
149 /*!
150  * @brief モンスターを目標地点に集団生成する / Attempt to place a "group" of monsters around the given location
151  * @param who 召喚主のモンスター情報ID
152  * @param y 中心生成位置y座標
153  * @param x 中心生成位置x座標
154  * @param r_idx 生成モンスター種族
155  * @param mode 生成オプション
156  * @return 成功したらtrue
157  */
158 static bool place_monster_group(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
159 {
160     monster_race *r_ptr = &r_info[r_idx];
161     int total = randint1(10);
162
163     floor_type *floor_ptr = player_ptr->current_floor_ptr;
164     int extra = 0;
165     if (r_ptr->level > floor_ptr->dun_level) {
166         extra = r_ptr->level - floor_ptr->dun_level;
167         extra = 0 - randint1(extra);
168     } else if (r_ptr->level < floor_ptr->dun_level) {
169         extra = floor_ptr->dun_level - r_ptr->level;
170         extra = randint1(extra);
171     }
172
173     if (extra > 9)
174         extra = 9;
175
176     total += extra;
177
178     if (total < 1)
179         total = 1;
180     if (total > GROUP_MAX)
181         total = GROUP_MAX;
182
183     int hack_n = 1;
184     POSITION hack_x[GROUP_MAX];
185     hack_x[0] = x;
186     POSITION hack_y[GROUP_MAX];
187     hack_y[0] = y;
188
189     for (int n = 0; (n < hack_n) && (hack_n < total); n++) {
190         POSITION hx = hack_x[n];
191         POSITION hy = hack_y[n];
192         for (int i = 0; (i < 8) && (hack_n < total); i++) {
193             POSITION mx, my;
194             scatter(player_ptr, &my, &mx, hy, hx, 4, PROJECT_NONE);
195             if (!is_cave_empty_bold2(player_ptr, my, mx))
196                 continue;
197
198             if (place_monster_one(player_ptr, who, my, mx, r_idx, mode)) {
199                 hack_y[hack_n] = my;
200                 hack_x[hack_n] = mx;
201                 hack_n++;
202             }
203         }
204     }
205
206     return TRUE;
207 }
208
209 /*!
210  * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
211  * @param r_idx チェックするモンスター種族のID
212  * @return 護衛にできるならばtrue
213  */
214 static bool place_monster_can_escort(player_type *player_ptr, MONRACE_IDX r_idx)
215 {
216     monster_race *r_ptr = &r_info[place_monster_idx];
217     monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[place_monster_m_idx];
218     monster_race *z_ptr = &r_info[r_idx];
219
220     if (mon_hook_dungeon(player_ptr, place_monster_idx) != mon_hook_dungeon(player_ptr, r_idx))
221         return FALSE;
222
223     if (z_ptr->d_char != r_ptr->d_char)
224         return FALSE;
225
226     if (z_ptr->level > r_ptr->level)
227         return FALSE;
228
229     if (z_ptr->flags1 & RF1_UNIQUE)
230         return FALSE;
231
232     if (place_monster_idx == r_idx)
233         return FALSE;
234
235     if (monster_has_hostile_align(player_ptr, m_ptr, 0, 0, z_ptr))
236         return FALSE;
237
238     if (r_ptr->flags7 & RF7_FRIENDLY) {
239         if (monster_has_hostile_align(player_ptr, NULL, 1, -1, z_ptr))
240             return FALSE;
241     }
242
243     if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
244         return FALSE;
245
246     return TRUE;
247 }
248
249 /*!
250  * @brief 一般的なモンスター生成処理のサブルーチン / Attempt to place a monster of the given race at the given location
251  * @param player_ptr プレーヤーへの参照ポインタ
252  * @param who 召喚主のモンスター情報ID
253  * @param y 生成地点y座標
254  * @param x 生成地点x座標
255  * @param r_idx 生成するモンスターの種族ID
256  * @param mode 生成オプション
257  * @return 生成に成功したらtrue
258  */
259 bool place_monster_aux(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
260 {
261     monster_race *r_ptr = &r_info[r_idx];
262
263     if (!(mode & PM_NO_KAGE) && one_in_(333))
264         mode |= PM_KAGE;
265
266     if (!place_monster_one(player_ptr, who, y, x, r_idx, mode))
267         return FALSE;
268     if (!(mode & PM_ALLOW_GROUP))
269         return TRUE;
270
271     place_monster_m_idx = hack_m_idx_ii;
272
273     /* Reinforcement */
274     for (int i = 0; i < 6; i++) {
275         if (!r_ptr->reinforce_id[i])
276             break;
277         int n = damroll(r_ptr->reinforce_dd[i], r_ptr->reinforce_ds[i]);
278         for (int j = 0; j < n; j++) {
279             POSITION nx, ny, d = 7;
280             scatter(player_ptr, &ny, &nx, y, x, d, PROJECT_NONE);
281             (void)place_monster_one(player_ptr, place_monster_m_idx, ny, nx, r_ptr->reinforce_id[i], mode);
282         }
283     }
284
285     if (r_ptr->flags1 & (RF1_FRIENDS)) {
286         (void)place_monster_group(player_ptr, who, y, x, r_idx, mode);
287     }
288
289     if (!(r_ptr->flags1 & (RF1_ESCORT)))
290         return TRUE;
291
292     place_monster_idx = r_idx;
293     for (int i = 0; i < 32; i++) {
294         POSITION nx, ny, d = 3;
295         MONRACE_IDX z;
296         scatter(player_ptr, &ny, &nx, y, x, d, PROJECT_NONE);
297         if (!is_cave_empty_bold2(player_ptr, ny, nx))
298             continue;
299
300         get_mon_num_prep(player_ptr, place_monster_can_escort, get_monster_hook2(player_ptr, ny, nx));
301         z = get_mon_num(player_ptr, 0, r_ptr->level, 0);
302         if (!z)
303             break;
304
305         (void)place_monster_one(player_ptr, place_monster_m_idx, ny, nx, z, mode);
306         if ((r_info[z].flags1 & RF1_FRIENDS) || (r_ptr->flags1 & RF1_ESCORTS)) {
307             (void)place_monster_group(player_ptr, place_monster_m_idx, ny, nx, z, mode);
308         }
309     }
310
311     return TRUE;
312 }
313
314 /*!
315  * @brief 一般的なモンスター生成処理のメインルーチン / Attempt to place a monster of the given race at the given location
316  * @param player_ptr プレーヤーへの参照ポインタ
317  * @param y 生成地点y座標
318  * @param x 生成地点x座標
319  * @param mode 生成オプション
320  * @return 生成に成功したらtrue
321  */
322 bool place_monster(player_type *player_ptr, POSITION y, POSITION x, BIT_FLAGS mode)
323 {
324     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
325     MONRACE_IDX r_idx = get_mon_num(player_ptr, 0, player_ptr->current_floor_ptr->monster_level, 0);
326     if (r_idx == 0)
327         return FALSE;
328
329     if ((one_in_(5) || (player_ptr->current_floor_ptr->base_level == 0)) && !(r_info[r_idx].flags1 & RF1_UNIQUE)
330         && angband_strchr("hkoptuyAHLOPTUVY", r_info[r_idx].d_char)) {
331         mode |= PM_JURAL;
332     }
333
334     return place_monster_aux(player_ptr, 0, y, x, r_idx, mode);
335 }
336
337 /*!
338  * @brief 指定地点に1種類のモンスター種族による群れを生成する
339  * @param player_ptr プレーヤーへの参照ポインタ
340  * @param y 生成地点y座標
341  * @param x 生成地点x座標
342  * @return 生成に成功したらtrue
343  */
344 bool alloc_horde(player_type *player_ptr, POSITION y, POSITION x, summon_specific_pf summon_specific)
345 {
346     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
347
348     floor_type *floor_ptr = player_ptr->current_floor_ptr;
349     MONRACE_IDX r_idx = 0;
350     int attempts = 1000;
351     monster_race *r_ptr = NULL;
352     while (--attempts) {
353         r_idx = get_mon_num(player_ptr, 0, floor_ptr->monster_level, 0);
354         if (!r_idx)
355             return FALSE;
356
357         r_ptr = &r_info[r_idx];
358         if (r_ptr->flags1 & RF1_UNIQUE)
359             continue;
360
361         if (r_idx == MON_HAGURE)
362             continue;
363         break;
364     }
365
366     if (attempts < 1)
367         return FALSE;
368
369     attempts = 1000;
370
371     while (--attempts) {
372         if (place_monster_aux(player_ptr, 0, y, x, r_idx, 0L))
373             break;
374     }
375
376     if (attempts < 1)
377         return FALSE;
378
379     MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
380     if (floor_ptr->m_list[m_idx].mflag2 & MFLAG2_CHAMELEON)
381         r_ptr = &r_info[floor_ptr->m_list[m_idx].r_idx];
382
383     POSITION cy = y;
384     POSITION cx = x;
385     for (attempts = randint1(10) + 5; attempts; attempts--) {
386         scatter(player_ptr, &cy, &cx, y, x, 5, PROJECT_NONE);
387         (void)(*summon_specific)(player_ptr, m_idx, cy, cx, floor_ptr->dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
388         y = cy;
389         x = cx;
390     }
391
392     if (cheat_hear)
393         msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
394     return TRUE;
395 }
396
397 /*!
398  * @brief ダンジョンの主生成を試みる / Put the Guardian
399  * @param player_ptr プレーヤーへの参照ポインタ
400  * @param def_val 現在の主の生成状態
401  * @return 生成に成功したらtrue
402  */
403 bool alloc_guardian(player_type *player_ptr, bool def_val)
404 {
405     MONRACE_IDX guardian = d_info[player_ptr->dungeon_idx].final_guardian;
406     floor_type *floor_ptr = player_ptr->current_floor_ptr;
407     bool is_guardian_applicable = guardian > 0;
408     is_guardian_applicable &= d_info[player_ptr->dungeon_idx].maxdepth == floor_ptr->dun_level;
409     is_guardian_applicable &= r_info[guardian].cur_num < r_info[guardian].max_num;
410     if (!is_guardian_applicable)
411         return def_val;
412
413     int try_count = 4000;
414     while (try_count) {
415         POSITION oy = randint1(floor_ptr->height - 4) + 2;
416         POSITION ox = randint1(floor_ptr->width - 4) + 2;
417         if (!is_cave_empty_bold2(player_ptr, oy, ox)) {
418             try_count++;
419             continue;
420         }
421
422         if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[oy][ox].feat, &r_info[guardian], 0)) {
423             try_count++;
424             continue;
425         }
426
427         if (place_monster_aux(player_ptr, 0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET)))
428             return TRUE;
429
430         try_count--;
431     }
432
433     return FALSE;
434 }
435
436 /*!
437  * @brief ダンジョンの初期配置モンスターを生成1回生成する / Attempt to allocate a random monster in the dungeon.
438  * @param dis プレイヤーから離れるべき最低距離
439  * @param mode 生成オプション
440  * @return 生成に成功したらtrue
441  * @details
442  * Place the monster at least "dis" distance from the player.
443  * Use "slp" to choose the initial "sleep" status
444  * Use "floor_ptr->monster_level" for the monster level
445  */
446 bool alloc_monster(player_type *player_ptr, POSITION dis, BIT_FLAGS mode, summon_specific_pf summon_specific)
447 {
448     if (alloc_guardian(player_ptr, FALSE))
449         return TRUE;
450
451     floor_type *floor_ptr = player_ptr->current_floor_ptr;
452     POSITION y = 0, x = 0;
453     int attempts_left = 10000;
454     while (attempts_left--) {
455         y = randint0(floor_ptr->height);
456         x = randint0(floor_ptr->width);
457
458         if (floor_ptr->dun_level) {
459             if (!is_cave_empty_bold2(player_ptr, y, x))
460                 continue;
461         } else {
462             if (!is_cave_empty_bold(player_ptr, y, x))
463                 continue;
464         }
465
466         if (distance(y, x, player_ptr->y, player_ptr->x) > dis)
467             break;
468     }
469
470     if (!attempts_left) {
471         if (cheat_xtra || cheat_hear) {
472             msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
473         }
474
475         return FALSE;
476     }
477
478     if (randint1(5000) <= floor_ptr->dun_level) {
479         if (alloc_horde(player_ptr, y, x, summon_specific)) {
480             return TRUE;
481         }
482     } else {
483         if (place_monster(player_ptr, y, x, (mode | PM_ALLOW_GROUP)))
484             return TRUE;
485     }
486
487     return FALSE;
488 }