OSDN Git Service

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