OSDN Git Service

cdb047f6865271e510a9292459400fdb101412bd
[hengbandforosx/hengbandosx.git] / src / monster-floor / one-monster-placer.c
1 /*!
2  * @brief モンスターをフロアに1体配置する処理
3  * @date 2020/06/13
4  * @author Hourier
5  */
6
7 #include "monster-floor/one-monster-placer.h"
8 #include "core/player-update-types.h"
9 #include "core/speed-table.h"
10 #include "dungeon/quest.h"
11 #include "effect/effect-characteristics.h"
12 #include "effect/effect-processor.h"
13 #include "flavor/flavor-describer.h"
14 #include "flavor/object-flavor-types.h"
15 #include "floor/cave.h"
16 #include "floor/floor-save-util.h"
17 #include "game-option/birth-options.h"
18 #include "game-option/cheat-types.h"
19 #include "grid/grid.h"
20 #include "monster-attack/monster-attack-types.h"
21 #include "monster-floor/monster-move.h"
22 #include "monster-floor/monster-summon.h"
23 #include "monster-floor/place-monster-types.h"
24 #include "monster-race/monster-race.h"
25 #include "monster-race/race-flags1.h"
26 #include "monster-race/race-flags2.h"
27 #include "monster-race/race-flags3.h"
28 #include "monster-race/race-flags7.h"
29 #include "monster-race/race-indice-types.h"
30 #include "monster/monster-flag-types.h"
31 #include "monster/monster-info.h"
32 #include "monster/monster-list.h"
33 #include "monster/monster-status-setter.h"
34 #include "monster/monster-status.h"
35 #include "monster/monster-update.h"
36 #include "monster/monster-util.h"
37 #include "object/warning.h"
38 #include "spell/spell-types.h"
39 #include "system/floor-type-definition.h"
40 #include "view/display-messages.h"
41 #include "wizard/wizard-messages.h"
42 #include "world/world.h"
43
44 static bool is_friendly_idx(player_type *player_ptr, MONSTER_IDX m_idx) { return m_idx > 0 && is_friendly(&player_ptr->current_floor_ptr->m_list[(m_idx)]); }
45
46 /*!
47  * @brief たぬきの変身対象となるモンスターかどうか判定する / Hook for Tanuki
48  * @param r_idx モンスター種族ID
49  * @return 対象にできるならtrueを返す
50  * @todo グローバル変数対策の上 monster_hook.cへ移す。
51  */
52 static bool monster_hook_tanuki(player_type *player_ptr, MONRACE_IDX r_idx)
53 {
54     monster_race *r_ptr = &r_info[r_idx];
55     bool unselectable = (r_ptr->flags1 & RF1_UNIQUE) != 0;
56     unselectable |= (r_ptr->flags2 & RF2_MULTIPLY) != 0;
57     unselectable |= (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) != 0;
58     unselectable |= (r_ptr->flags7 & RF7_AQUATIC) != 0;
59     if (unselectable)
60         return FALSE;
61
62     for (int i = 0; i < 4; i++)
63         if (r_ptr->blow[i].method == RBM_EXPLODE)
64             return FALSE;
65
66     return (*(get_monster_hook(player_ptr)))(player_ptr, r_idx);
67 }
68
69 /*!
70  * @param player_ptr プレーヤーへの参照ポインタ
71  * @brief モンスターの表層IDを設定する / Set initial racial appearance of a monster
72  * @param r_idx モンスター種族ID
73  * @return モンスター種族の表層ID
74  */
75 static MONRACE_IDX initial_r_appearance(player_type *player_ptr, MONRACE_IDX r_idx, BIT_FLAGS generate_mode)
76 {
77     floor_type *floor_ptr = player_ptr->current_floor_ptr;
78     if (player_ptr->pseikaku == PERSONALITY_CHARGEMAN && (generate_mode & PM_JURAL) && !(generate_mode & (PM_MULTIPLY | PM_KAGE)))
79         return MON_ALIEN_JURAL;
80
81     if (!(r_info[r_idx].flags7 & RF7_TANUKI))
82         return r_idx;
83
84     get_mon_num_prep(player_ptr, monster_hook_tanuki, NULL);
85     int attempts = 1000;
86     DEPTH min = MIN(floor_ptr->base_level - 5, 50);
87     while (--attempts) {
88         MONRACE_IDX ap_r_idx = get_mon_num(player_ptr, 0, floor_ptr->base_level + 10, 0);
89         if (r_info[ap_r_idx].level >= min)
90             return ap_r_idx;
91     }
92
93     return r_idx;
94 }
95
96 /*!
97  * @brief ユニークが生成可能か評価する
98  * @param player_ptr プレーヤーへの参照ポインタ
99  * @param r_idx 生成モンスター種族
100  * @return ユニークの生成が不可能な条件ならFALSE、それ以外はTRUE
101  */
102 static bool check_unique_placeable(player_type *player_ptr, MONRACE_IDX r_idx)
103 {
104     if (player_ptr->phase_out)
105         return TRUE;
106
107     monster_race *r_ptr = &r_info[r_idx];
108     if (((r_ptr->flags1 & (RF1_UNIQUE)) || (r_ptr->flags7 & (RF7_NAZGUL))) && (r_ptr->cur_num >= r_ptr->max_num)) {
109         return FALSE;
110     }
111
112     if ((r_ptr->flags7 & (RF7_UNIQUE2)) && (r_ptr->cur_num >= 1)) {
113         return FALSE;
114     }
115
116     if (r_idx == MON_BANORLUPART) {
117         if (r_info[MON_BANOR].cur_num > 0)
118             return FALSE;
119         if (r_info[MON_LUPART].cur_num > 0)
120             return FALSE;
121     }
122
123     if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) && (player_ptr->current_floor_ptr->dun_level < r_ptr->level)
124         && (!ironman_nightmare || (r_ptr->flags1 & (RF1_QUESTOR)))) {
125         return FALSE;
126     }
127
128     return TRUE;
129 }
130
131 /*!
132  * @brief クエスト内に生成可能か評価する
133  * @param player_ptr プレーヤーへの参照ポインタ
134  * @param r_idx 生成モンスター種族
135  * @return 生成が可能ならTRUE、不可能ならFALSE
136  */
137 static bool check_quest_placeable(player_type *player_ptr, MONRACE_IDX r_idx)
138 {
139     floor_type *floor_ptr = player_ptr->current_floor_ptr;
140     if (quest_number(player_ptr, floor_ptr->dun_level) == 0)
141         return TRUE;
142
143     int hoge = quest_number(player_ptr, floor_ptr->dun_level);
144     if ((quest[hoge].type != QUEST_TYPE_KILL_LEVEL) && (quest[hoge].type != QUEST_TYPE_RANDOM))
145         return TRUE;
146
147     if (r_idx != quest[hoge].r_idx)
148         return TRUE;
149
150     int number_mon = 0;
151     for (int i2 = 0; i2 < floor_ptr->width; ++i2)
152         for (int j2 = 0; j2 < floor_ptr->height; j2++)
153             if ((floor_ptr->grid_array[j2][i2].m_idx > 0) && (floor_ptr->m_list[floor_ptr->grid_array[j2][i2].m_idx].r_idx == quest[hoge].r_idx))
154                 number_mon++;
155
156     if (number_mon + quest[hoge].cur_num >= quest[hoge].max_num)
157         return FALSE;
158
159     return TRUE;
160 }
161
162 /*!
163  * @brief 守りのルーン上にモンスターの配置を試みる
164  * @param player_ptr プレーヤーへの参照ポインタ
165  * @param r_idx 生成モンスター種族
166  * @param y 生成位置y座標
167  * @param x 生成位置x座標
168  * @return 生成が可能ならTRUE、不可能ならFALSE
169  */
170 static bool check_procection_rune(player_type *player_ptr, MONRACE_IDX r_idx, POSITION y, POSITION x)
171 {
172     grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
173     if (!is_rune_protection_grid(g_ptr))
174         return TRUE;
175
176     monster_race *r_ptr = &r_info[r_idx];
177     if (randint1(BREAK_RUNE_PROTECTION) >= (r_ptr->level + 20))
178         return FALSE;
179
180     if (g_ptr->info & CAVE_MARK)
181         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
182
183     g_ptr->info &= ~(CAVE_MARK);
184     g_ptr->info &= ~(CAVE_OBJECT);
185     g_ptr->mimic = 0;
186     note_spot(player_ptr, y, x);
187     return TRUE;
188 }
189
190 static void warn_unique_generation(player_type *player_ptr, MONRACE_IDX r_idx)
191 {
192     if (!player_ptr->warning || !current_world_ptr->character_dungeon)
193         return;
194
195     monster_race *r_ptr = &r_info[r_idx];
196     if ((r_ptr->flags1 & RF1_UNIQUE) == 0)
197         return;
198
199     concptr color;
200     object_type *o_ptr;
201     GAME_TEXT o_name[MAX_NLEN];
202     if (r_ptr->level > player_ptr->lev + 30)
203         color = _("黒く", "black");
204     else if (r_ptr->level > player_ptr->lev + 15)
205         color = _("紫色に", "purple");
206     else if (r_ptr->level > player_ptr->lev + 5)
207         color = _("ルビー色に", "deep red");
208     else if (r_ptr->level > player_ptr->lev - 5)
209         color = _("赤く", "red");
210     else if (r_ptr->level > player_ptr->lev - 15)
211         color = _("ピンク色に", "pink");
212     else
213         color = _("白く", "white");
214
215     o_ptr = choose_warning_item(player_ptr);
216     if (o_ptr != NULL) {
217         describe_flavor(player_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
218         msg_format(_("%sは%s光った。", "%s glows %s."), o_name, color);
219     } else {
220         msg_format(_("%s光る物が頭に浮かんだ。", "A %s image forms in your mind."), color);
221     }
222 }
223
224 /*!
225  * @brief モンスターを一体生成する / Attempt to place a monster of the given race at the given location.
226  * @param player_ptr プレーヤーへの参照ポインタ
227  * @param who 召喚を行ったモンスターID
228  * @param y 生成位置y座標
229  * @param x 生成位置x座標
230  * @param r_idx 生成モンスター種族
231  * @param mode 生成オプション
232  * @return 成功したらtrue
233  */
234 bool place_monster_one(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
235 {
236     floor_type *floor_ptr = player_ptr->current_floor_ptr;
237     grid_type *g_ptr = &floor_ptr->grid_array[y][x];
238     monster_race *r_ptr = &r_info[r_idx];
239     concptr name = (r_name + r_ptr->name);
240
241     if (player_ptr->wild_mode || !in_bounds(floor_ptr, y, x) || (r_idx == 0) || (r_ptr->name == 0))
242         return FALSE;
243
244     if (((mode & PM_IGNORE_TERRAIN) == 0) && (pattern_tile(floor_ptr, y, x) || !monster_can_enter(player_ptr, y, x, r_ptr, 0)))
245         return FALSE;
246
247     if (!check_unique_placeable(player_ptr, r_idx) || !check_quest_placeable(player_ptr, r_idx) || !check_procection_rune(player_ptr, r_idx, y, x))
248         return FALSE;
249
250     msg_format_wizard(player_ptr, CHEAT_MONSTER, _("%s(Lv%d)を生成しました。", "%s(Lv%d) was generated."), name, r_ptr->level);
251     if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL) || (r_ptr->level < 10))
252         mode &= ~PM_KAGE;
253
254     g_ptr->m_idx = m_pop(floor_ptr);
255     hack_m_idx_ii = g_ptr->m_idx;
256     if (!g_ptr->m_idx)
257         return FALSE;
258
259     monster_type *m_ptr;
260     m_ptr = &floor_ptr->m_list[g_ptr->m_idx];
261     m_ptr->r_idx = r_idx;
262     m_ptr->ap_r_idx = initial_r_appearance(player_ptr, r_idx, mode);
263
264     m_ptr->mflag = 0;
265     m_ptr->mflag2 = 0;
266     if ((mode & PM_MULTIPLY) && (who > 0) && !is_original_ap(&floor_ptr->m_list[who])) {
267         m_ptr->ap_r_idx = floor_ptr->m_list[who].ap_r_idx;
268         if (floor_ptr->m_list[who].mflag2 & MFLAG2_KAGE)
269             m_ptr->mflag2 |= MFLAG2_KAGE;
270     }
271
272     if ((who > 0) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
273         m_ptr->sub_align = floor_ptr->m_list[who].sub_align;
274     else {
275         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
276         if (r_ptr->flags3 & RF3_EVIL)
277             m_ptr->sub_align |= SUB_ALIGN_EVIL;
278         if (r_ptr->flags3 & RF3_GOOD)
279             m_ptr->sub_align |= SUB_ALIGN_GOOD;
280     }
281
282     m_ptr->fy = y;
283     m_ptr->fx = x;
284     m_ptr->current_floor_ptr = floor_ptr;
285
286     for (int cmi = 0; cmi < MAX_MTIMED; cmi++)
287         m_ptr->mtimed[cmi] = 0;
288
289     m_ptr->cdis = 0;
290     reset_target(m_ptr);
291     m_ptr->nickname = 0;
292     m_ptr->exp = 0;
293
294     if (who > 0 && is_pet(&floor_ptr->m_list[who])) {
295         mode |= PM_FORCE_PET;
296         m_ptr->parent_m_idx = who;
297     } else {
298         m_ptr->parent_m_idx = 0;
299     }
300
301     if (r_ptr->flags7 & RF7_CHAMELEON) {
302         choose_new_monster(player_ptr, g_ptr->m_idx, TRUE, 0);
303         r_ptr = &r_info[m_ptr->r_idx];
304         m_ptr->mflag2 |= MFLAG2_CHAMELEON;
305         if ((r_ptr->flags1 & RF1_UNIQUE) && (who <= 0))
306             m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
307     } else if ((mode & PM_KAGE) && !(mode & PM_FORCE_PET)) {
308         m_ptr->ap_r_idx = MON_KAGE;
309         m_ptr->mflag2 |= MFLAG2_KAGE;
310     }
311
312     if (mode & PM_NO_PET)
313         m_ptr->mflag2 |= MFLAG2_NOPET;
314
315     m_ptr->ml = FALSE;
316     if (mode & PM_FORCE_PET) {
317         set_pet(player_ptr, m_ptr);
318     } else if ((r_ptr->flags7 & RF7_FRIENDLY) || (mode & PM_FORCE_FRIENDLY) || is_friendly_idx(player_ptr, who)) {
319         if (!monster_has_hostile_align(player_ptr, NULL, 0, -1, r_ptr) && !player_ptr->current_floor_ptr->inside_arena)
320             set_friendly(m_ptr);
321     }
322
323     m_ptr->mtimed[MTIMED_CSLEEP] = 0;
324     if ((mode & PM_ALLOW_SLEEP) && r_ptr->sleep && !ironman_nightmare) {
325         int val = r_ptr->sleep;
326         (void)set_monster_csleep(player_ptr, g_ptr->m_idx, (val * 2) + randint1(val * 10));
327     }
328
329     if (r_ptr->flags1 & RF1_FORCE_MAXHP) {
330         m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
331     } else {
332         m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
333     }
334
335     if (ironman_nightmare) {
336         u32b hp = m_ptr->max_maxhp * 2L;
337
338         m_ptr->max_maxhp = (HIT_POINT)MIN(30000, hp);
339     }
340
341     m_ptr->maxhp = m_ptr->max_maxhp;
342     if (m_ptr->r_idx == MON_WOUNDED_BEAR)
343         m_ptr->hp = m_ptr->maxhp / 2;
344     else
345         m_ptr->hp = m_ptr->maxhp;
346
347     m_ptr->dealt_damage = 0;
348
349     m_ptr->mspeed = get_mspeed(floor_ptr, r_ptr);
350
351     if (mode & PM_HASTE)
352         (void)set_monster_fast(player_ptr, g_ptr->m_idx, 100);
353
354     if (!ironman_nightmare) {
355         m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100);
356     } else {
357         m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100) * 2;
358     }
359
360     if ((r_ptr->flags1 & RF1_PREVENT_SUDDEN_MAGIC) && !ironman_nightmare) {
361         m_ptr->mflag |= (MFLAG_PREVENT_MAGIC);
362         repair_monsters = TRUE;
363     }
364
365     if (g_ptr->m_idx < hack_m_idx) {
366         m_ptr->mflag |= (MFLAG_BORN);
367     }
368
369     if (r_ptr->flags7 & RF7_SELF_LD_MASK)
370         player_ptr->update |= (PU_MON_LITE);
371     else if ((r_ptr->flags7 & RF7_HAS_LD_MASK) && !monster_csleep_remaining(m_ptr))
372         player_ptr->update |= (PU_MON_LITE);
373     update_monster(player_ptr, g_ptr->m_idx, TRUE);
374
375     real_r_ptr(m_ptr)->cur_num++;
376
377     /*
378      * Memorize location of the unique monster in saved floors.
379      * A unique monster move from old saved floor.
380      */
381     if (current_world_ptr->character_dungeon && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)))
382         real_r_ptr(m_ptr)->floor_id = player_ptr->floor_id;
383
384     if (r_ptr->flags2 & RF2_MULTIPLY)
385         floor_ptr->num_repro++;
386
387     warn_unique_generation(player_ptr, r_idx);
388     if (!is_rune_explosion_grid(g_ptr))
389         return TRUE;
390
391     if (randint1(BREAK_RUNE_EXPLOSION) > r_ptr->level) {
392         if (g_ptr->info & CAVE_MARK) {
393             msg_print(_("ルーンが爆発した!", "The rune explodes!"));
394             project(player_ptr, 0, 2, y, x, 2 * (player_ptr->lev + damroll(7, 7)), GF_MANA,
395                 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
396         }
397     } else {
398         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
399     }
400
401     g_ptr->info &= ~(CAVE_MARK);
402     g_ptr->info &= ~(CAVE_OBJECT);
403     g_ptr->mimic = 0;
404
405     note_spot(player_ptr, y, x);
406     lite_spot(player_ptr, y, x);
407
408     return TRUE;
409 }