8 #include "monster-floor/monster-generator.h"
9 #include "dungeon/dungeon.h"
10 #include "floor/floor.h"
11 #include "main/sound-definitions-table.h"
12 #include "monster-floor/one-monster-placer.h"
13 #include "monster-floor/place-monster-types.h"
14 #include "monster-race/monster-race-hook.h"
15 #include "monster-race/race-flags1.h"
16 #include "monster-race/race-flags7.h"
17 #include "monster-race/race-indice-types.h"
18 #include "monster/monster-flag-types.h"
19 #include "monster/monster-info.h"
20 #include "monster/monster-list.h"
21 #include "monster/monster-util.h"
22 #include "monster/smart-learn-types.h"
23 #include "mspell/summon-checker.h"
24 #include "spell/spells-summon.h"
26 #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */
29 * @var place_monster_idx
30 * @brief 護衛対象となるモンスター種族IDを渡すグローバル変数 / Hack -- help pick an escort type
31 * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_idxを除去し、関数引数化する
33 static MONSTER_IDX place_monster_idx = 0;
36 * @var place_monster_m_idx
37 * @brief 護衛対象となるモンスターIDを渡すグローバル変数 / Hack -- help pick an escort type
38 * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_m_idxを除去し、関数引数化する
40 static MONSTER_IDX place_monster_m_idx = 0;
43 * @var summon_specific_who
44 * @brief 召喚を行ったプレイヤーあるいはモンスターのIDを示すグローバル変数 / Hack -- the index of the summoning monster
45 * @todo summon_specific_who グローバル変数の除去と関数引数への代替を行う
47 int summon_specific_who = -1;
50 * @var summon_unique_okay
51 * @brief 召喚対象にユニークを含めるかを示すグローバル変数 / summoning unique enable
52 * @todo summon_unique_okay グローバル変数の除去と関数引数への代替を行う
54 bool summon_unique_okay = FALSE;
57 * @brief モンスター1体を目標地点に可能な限り近い位置に生成する / improved version of scatter() for place monster
58 * @param player_ptr プレーヤーへの参照ポインタ
59 * @param r_idx 生成モンスター種族
64 * @param max_dist 生成位置の最大半径
68 static bool mon_scatter(player_type *player_ptr, MONRACE_IDX r_idx, POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION max_dist)
70 POSITION place_x[MON_SCAT_MAXD];
71 POSITION place_y[MON_SCAT_MAXD];
72 int num[MON_SCAT_MAXD];
74 if (max_dist >= MON_SCAT_MAXD)
78 for (i = 0; i < MON_SCAT_MAXD; i++)
81 floor_type *floor_ptr = player_ptr->current_floor_ptr;
82 for (POSITION nx = x - max_dist; nx <= x + max_dist; nx++) {
83 for (POSITION ny = y - max_dist; ny <= y + max_dist; ny++) {
84 if (!in_bounds(floor_ptr, ny, nx))
86 if (!projectable(player_ptr, y, x, ny, nx))
89 monster_race *r_ptr = &r_info[r_idx];
90 if (!monster_can_enter(player_ptr, ny, nx, r_ptr, 0))
93 if (!is_cave_empty_bold2(player_ptr, ny, nx))
95 if (pattern_tile(floor_ptr, ny, nx))
99 i = distance(y, x, ny, nx);
104 if (one_in_(num[i])) {
112 while (i < MON_SCAT_MAXD && 0 == num[i])
114 if (i >= MON_SCAT_MAXD)
124 * todo ここにplayer_typeを追加すると関数ポインタ周りの収拾がつかなくなるので保留
125 * @brief モンスターが召喚の基本条件に合っているかをチェックする / Hack -- help decide if a monster race is "okay" to summon
126 * @param r_idx チェックするモンスター種族ID
127 * @return 召喚対象にできるならばTRUE
129 static bool summon_specific_okay(MONRACE_IDX r_idx)
131 monster_race *r_ptr = &r_info[r_idx];
132 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[summon_specific_who];
133 if (!mon_hook_dungeon(r_idx))
136 if (summon_specific_who > 0) {
137 if (monster_has_hostile_align(p_ptr, m_ptr, 0, 0, r_ptr))
139 } else if (summon_specific_who < 0) {
140 if (monster_has_hostile_align(p_ptr, NULL, 10, -10, r_ptr)) {
141 if (!one_in_(ABS(p_ptr->align) / 2 + 1))
146 if (!summon_unique_okay && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)))
149 if (!summon_specific_type)
152 if ((summon_specific_who < 0) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && monster_has_hostile_align(p_ptr, NULL, 10, -10, r_ptr))
155 if ((r_ptr->flags7 & RF7_CHAMELEON) && (d_info[p_ptr->dungeon_idx].flags1 & DF1_CHAMELEON))
158 return (check_summon_specific(p_ptr, m_ptr->r_idx, r_idx));
162 * @brief モンスターを召喚により配置する / Place a monster (of the specified "type") near the given location. Return TRUE if a monster was actually summoned.
163 * @param player_ptr プレーヤーへの参照ポインタ
164 * @param who 召喚主のモンスター情報ID
169 * @param mode 生成オプション
170 * @return 召喚できたらtrueを返す
172 bool summon_specific(player_type *player_ptr, MONSTER_IDX who, POSITION y1, POSITION x1, DEPTH lev, int type, BIT_FLAGS mode)
174 floor_type *floor_ptr = player_ptr->current_floor_ptr;
175 if (floor_ptr->inside_arena)
179 if (!mon_scatter(player_ptr, 0, &y, &x, y1, x1, 2))
182 summon_specific_who = who;
183 summon_specific_type = type;
184 summon_unique_okay = (mode & PM_ALLOW_UNIQUE) ? TRUE : FALSE;
185 get_mon_num_prep(player_ptr, summon_specific_okay, get_monster_hook2(player_ptr, y, x));
187 MONRACE_IDX r_idx = get_mon_num(player_ptr, (floor_ptr->dun_level + lev) / 2 + 5, 0);
189 summon_specific_type = 0;
193 if ((type == SUMMON_BLUE_HORROR) || (type == SUMMON_DAWN))
196 if (!place_monster_aux(player_ptr, who, y, x, r_idx, mode)) {
197 summon_specific_type = 0;
201 summon_specific_type = 0;
207 * @brief 特定モンスター種族を召喚により生成する / A "dangerous" function, creates a pet of the specified type
208 * @param player_ptr プレーヤーへの参照ポインタ
209 * @param who 召喚主のモンスター情報ID
212 * @param r_idx 生成するモンスター種族ID
213 * @param mode 生成オプション
214 * @return 召喚できたらtrueを返す
216 bool summon_named_creature(player_type *player_ptr, MONSTER_IDX who, POSITION oy, POSITION ox, MONRACE_IDX r_idx, BIT_FLAGS mode)
218 if (r_idx >= max_r_idx)
222 if (player_ptr->current_floor_ptr->inside_arena)
225 if (!mon_scatter(player_ptr, r_idx, &y, &x, oy, ox, 2))
228 return place_monster_aux(player_ptr, who, y, x, r_idx, (mode | PM_NO_KAGE));
232 * @brief モンスターを増殖生成する / Let the given monster attempt to reproduce.
233 * @param player_ptr プレーヤーへの参照ポインタ
234 * @param m_idx 増殖するモンスター情報ID
235 * @param clone クローン・モンスター処理ならばtrue
236 * @param mode 生成オプション
237 * @return 生成できたらtrueを返す
239 * Note that "reproduction" REQUIRES empty space.
241 bool multiply_monster(player_type *player_ptr, MONSTER_IDX m_idx, bool clone, BIT_FLAGS mode)
243 floor_type *floor_ptr = player_ptr->current_floor_ptr;
244 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
246 if (!mon_scatter(player_ptr, m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1))
249 if (m_ptr->mflag2 & MFLAG2_NOPET)
252 if (!place_monster_aux(player_ptr, m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY)))
255 if (clone || (m_ptr->smart & SM_CLONED)) {
256 floor_ptr->m_list[hack_m_idx_ii].smart |= SM_CLONED;
257 floor_ptr->m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
264 * @brief モンスターを目標地点に集団生成する / Attempt to place a "group" of monsters around the given location
265 * @param who 召喚主のモンスター情報ID
268 * @param r_idx 生成モンスター種族
269 * @param mode 生成オプション
272 static bool place_monster_group(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
274 monster_race *r_ptr = &r_info[r_idx];
275 int total = randint1(10);
277 floor_type *floor_ptr = player_ptr->current_floor_ptr;
279 if (r_ptr->level > floor_ptr->dun_level) {
280 extra = r_ptr->level - floor_ptr->dun_level;
281 extra = 0 - randint1(extra);
282 } else if (r_ptr->level < floor_ptr->dun_level) {
283 extra = floor_ptr->dun_level - r_ptr->level;
284 extra = randint1(extra);
294 if (total > GROUP_MAX)
298 POSITION hack_x[GROUP_MAX];
300 POSITION hack_y[GROUP_MAX];
303 for (int n = 0; (n < hack_n) && (hack_n < total); n++) {
304 POSITION hx = hack_x[n];
305 POSITION hy = hack_y[n];
306 for (int i = 0; (i < 8) && (hack_n < total); i++) {
308 scatter(player_ptr, &my, &mx, hy, hx, 4, 0);
309 if (!is_cave_empty_bold2(player_ptr, my, mx))
312 if (place_monster_one(player_ptr, who, my, mx, r_idx, mode)) {
324 * todo ここには本来floor_type*を追加したいが、monster.hにfloor.hの参照を追加するとコンパイルエラーが出るので保留
325 * todo ここにplayer_typeを追加すると関数ポインタ周りの収拾がつかなくなるので保留
326 * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
327 * @param r_idx チェックするモンスター種族のID
328 * @return 護衛にできるならばtrue
330 static bool place_monster_can_escort(MONRACE_IDX r_idx)
332 monster_race *r_ptr = &r_info[place_monster_idx];
333 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[place_monster_m_idx];
334 monster_race *z_ptr = &r_info[r_idx];
336 if (mon_hook_dungeon(place_monster_idx) != mon_hook_dungeon(r_idx))
338 if (z_ptr->d_char != r_ptr->d_char)
340 if (z_ptr->level > r_ptr->level)
342 if (z_ptr->flags1 & RF1_UNIQUE)
344 if (place_monster_idx == r_idx)
346 if (monster_has_hostile_align(p_ptr, m_ptr, 0, 0, z_ptr))
349 if (r_ptr->flags7 & RF7_FRIENDLY) {
350 if (monster_has_hostile_align(p_ptr, NULL, 1, -1, z_ptr))
354 if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
361 * @brief 一般的なモンスター生成処理のサブルーチン / Attempt to place a monster of the given race at the given location
362 * @param player_ptr プレーヤーへの参照ポインタ
363 * @param who 召喚主のモンスター情報ID
366 * @param r_idx 生成するモンスターの種族ID
367 * @param mode 生成オプション
368 * @return 生成に成功したらtrue
370 bool place_monster_aux(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
372 monster_race *r_ptr = &r_info[r_idx];
374 if (!(mode & PM_NO_KAGE) && one_in_(333))
377 if (!place_monster_one(player_ptr, who, y, x, r_idx, mode))
379 if (!(mode & PM_ALLOW_GROUP))
382 place_monster_m_idx = hack_m_idx_ii;
385 for (int i = 0; i < 6; i++) {
386 if (!r_ptr->reinforce_id[i])
388 int n = damroll(r_ptr->reinforce_dd[i], r_ptr->reinforce_ds[i]);
389 for (int j = 0; j < n; j++) {
390 POSITION nx, ny, d = 7;
391 scatter(player_ptr, &ny, &nx, y, x, d, 0);
392 (void)place_monster_one(player_ptr, place_monster_m_idx, ny, nx, r_ptr->reinforce_id[i], mode);
396 if (r_ptr->flags1 & (RF1_FRIENDS)) {
397 (void)place_monster_group(player_ptr, who, y, x, r_idx, mode);
400 if (!(r_ptr->flags1 & (RF1_ESCORT)))
403 place_monster_idx = r_idx;
404 for (int i = 0; i < 32; i++) {
405 POSITION nx, ny, d = 3;
407 scatter(player_ptr, &ny, &nx, y, x, d, 0);
408 if (!is_cave_empty_bold2(player_ptr, ny, nx))
411 get_mon_num_prep(player_ptr, place_monster_can_escort, get_monster_hook2(player_ptr, ny, nx));
412 z = get_mon_num(player_ptr, r_ptr->level, 0);
416 (void)place_monster_one(player_ptr, place_monster_m_idx, ny, nx, z, mode);
417 if ((r_info[z].flags1 & RF1_FRIENDS) || (r_ptr->flags1 & RF1_ESCORTS)) {
418 (void)place_monster_group(player_ptr, place_monster_m_idx, ny, nx, z, mode);
426 * @brief 一般的なモンスター生成処理のメインルーチン / Attempt to place a monster of the given race at the given location
427 * @param player_ptr プレーヤーへの参照ポインタ
430 * @param mode 生成オプション
431 * @return 生成に成功したらtrue
433 bool place_monster(player_type *player_ptr, POSITION y, POSITION x, BIT_FLAGS mode)
436 get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
437 r_idx = get_mon_num(player_ptr, player_ptr->current_floor_ptr->monster_level, 0);
441 if ((one_in_(5) || (player_ptr->current_floor_ptr->base_level == 0)) && !(r_info[r_idx].flags1 & RF1_UNIQUE)
442 && my_strchr("hkoptuyAHLOPTUVY", r_info[r_idx].d_char)) {
446 if (place_monster_aux(player_ptr, 0, y, x, r_idx, mode))
453 * @brief 指定地点に1種類のモンスター種族による群れを生成する
454 * @param player_ptr プレーヤーへの参照ポインタ
457 * @return 生成に成功したらtrue
459 bool alloc_horde(player_type *player_ptr, POSITION y, POSITION x)
461 get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
463 floor_type *floor_ptr = player_ptr->current_floor_ptr;
464 MONRACE_IDX r_idx = 0;
466 monster_race *r_ptr = NULL;
468 r_idx = get_mon_num(player_ptr, floor_ptr->monster_level, 0);
472 r_ptr = &r_info[r_idx];
473 if (r_ptr->flags1 & RF1_UNIQUE)
476 if (r_idx == MON_HAGURE)
487 if (place_monster_aux(player_ptr, 0, y, x, r_idx, 0L))
494 MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
495 if (floor_ptr->m_list[m_idx].mflag2 & MFLAG2_CHAMELEON)
496 r_ptr = &r_info[floor_ptr->m_list[m_idx].r_idx];
500 for (attempts = randint1(10) + 5; attempts; attempts--) {
501 scatter(player_ptr, &cy, &cx, y, x, 5, 0);
502 (void)summon_specific(player_ptr, m_idx, cy, cx, floor_ptr->dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
508 msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
513 * @brief ダンジョンの主生成を試みる / Put the Guardian
514 * @param player_ptr プレーヤーへの参照ポインタ
515 * @param def_val 現在の主の生成状態
516 * @return 生成に成功したらtrue
518 bool alloc_guardian(player_type *player_ptr, bool def_val)
520 MONRACE_IDX guardian = d_info[player_ptr->dungeon_idx].final_guardian;
521 floor_type *floor_ptr = player_ptr->current_floor_ptr;
522 bool is_guardian_applicable = guardian > 0;
523 is_guardian_applicable &= d_info[player_ptr->dungeon_idx].maxdepth == floor_ptr->dun_level;
524 is_guardian_applicable &= r_info[guardian].cur_num < r_info[guardian].max_num;
525 if (!is_guardian_applicable)
528 int try_count = 4000;
530 POSITION oy = randint1(floor_ptr->height - 4) + 2;
531 POSITION ox = randint1(floor_ptr->width - 4) + 2;
532 if (!is_cave_empty_bold2(player_ptr, oy, ox)) {
537 if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[oy][ox].feat, &r_info[guardian], 0)) {
542 if (place_monster_aux(player_ptr, 0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET)))
552 * @brief ダンジョンの初期配置モンスターを生成1回生成する / Attempt to allocate a random monster in the dungeon.
553 * @param dis プレイヤーから離れるべき最低距離
554 * @param mode 生成オプション
555 * @return 生成に成功したらtrue
557 * Place the monster at least "dis" distance from the player.
558 * Use "slp" to choose the initial "sleep" status
559 * Use "floor_ptr->monster_level" for the monster level
561 bool alloc_monster(player_type *player_ptr, POSITION dis, BIT_FLAGS mode)
563 if (alloc_guardian(player_ptr, FALSE))
566 floor_type *floor_ptr = player_ptr->current_floor_ptr;
567 POSITION y = 0, x = 0;
568 int attempts_left = 10000;
569 while (attempts_left--) {
570 y = randint0(floor_ptr->height);
571 x = randint0(floor_ptr->width);
573 if (floor_ptr->dun_level) {
574 if (!is_cave_empty_bold2(player_ptr, y, x))
577 if (!is_cave_empty_bold(player_ptr, y, x))
581 if (distance(y, x, player_ptr->y, player_ptr->x) > dis)
585 if (!attempts_left) {
586 if (cheat_xtra || cheat_hear) {
587 msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
593 if (randint1(5000) <= floor_ptr->dun_level) {
594 if (alloc_horde(player_ptr, y, x)) {
598 if (place_monster(player_ptr, y, x, (mode | PM_ALLOW_GROUP)))