OSDN Git Service

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