OSDN Git Service

[Refactor] MULTIPLY を新定義に合わせた
[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/speed-table.h"
9 #include "dungeon/quest.h"
10 #include "effect/attribute-types.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-table.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-kind-mask.h"
25 #include "monster-race/monster-race.h"
26 #include "monster-race/race-brightness-mask.h"
27 #include "monster-race/race-flags1.h"
28 #include "monster-race/race-flags2.h"
29 #include "monster-race/race-flags3.h"
30 #include "monster-race/race-flags7.h"
31 #include "monster-race/race-indice-types.h"
32 #include "monster-race/race-misc-flags.h"
33 #include "monster/monster-flag-types.h"
34 #include "monster/monster-info.h"
35 #include "monster/monster-list.h"
36 #include "monster/monster-status-setter.h"
37 #include "monster/monster-status.h"
38 #include "monster/monster-update.h"
39 #include "monster/monster-util.h"
40 #include "object/warning.h"
41 #include "player/player-status.h"
42 #include "system/angband-system.h"
43 #include "system/floor-type-definition.h"
44 #include "system/grid-type-definition.h"
45 #include "system/monster-race-info.h"
46 #include "system/player-type-definition.h"
47 #include "system/redrawing-flags-updater.h"
48 #include "util/bit-flags-calculator.h"
49 #include "view/display-messages.h"
50 #include "wizard/wizard-messages.h"
51 #include "world/world.h"
52
53 static bool is_friendly_idx(PlayerType *player_ptr, MONSTER_IDX m_idx)
54 {
55     if (m_idx == 0) {
56         return false;
57     }
58
59     const auto &m_ref = player_ptr->current_floor_ptr->m_list[m_idx];
60     return m_ref.is_friendly();
61 }
62
63 /*!
64  * @brief たぬきの変身対象となるモンスターかどうか判定する / Hook for Tanuki
65  * @param r_idx モンスター種族ID
66  * @return 対象にできるならtrueを返す
67  * @todo グローバル変数対策の上 monster_hook.cへ移す。
68  */
69 static bool monster_hook_tanuki(PlayerType *player_ptr, MonsterRaceId r_idx)
70 {
71     const auto &monrace = monraces_info[r_idx];
72     bool unselectable = monrace.kind_flags.has(MonsterKindType::UNIQUE);
73     unselectable |= monrace.misc_flags.has(MonsterMiscType::MULTIPLY);
74     unselectable |= monrace.behavior_flags.has(MonsterBehaviorType::FRIENDLY);
75     unselectable |= monrace.feature_flags.has(MonsterFeatureType::AQUATIC);
76     unselectable |= monrace.misc_flags.has(MonsterMiscType::CHAMELEON);
77     unselectable |= monrace.is_explodable();
78     if (unselectable) {
79         return false;
80     }
81
82     auto hook_pf = get_monster_hook(player_ptr);
83     return (*hook_pf)(player_ptr, r_idx);
84 }
85
86 /*!
87  * @param player_ptr プレイヤーへの参照ポインタ
88  * @brief モンスターの表層IDを設定する / Set initial racial appearance of a monster
89  * @param r_idx モンスター種族ID
90  * @return モンスター種族の表層ID
91  */
92 static MonsterRaceId initial_r_appearance(PlayerType *player_ptr, MonsterRaceId r_idx, BIT_FLAGS generate_mode)
93 {
94     auto *floor_ptr = player_ptr->current_floor_ptr;
95     if (is_chargeman(player_ptr) && any_bits(generate_mode, PM_JURAL) && none_bits(generate_mode, PM_MULTIPLY | PM_KAGE)) {
96         return MonsterRaceId::ALIEN_JURAL;
97     }
98
99     if (monraces_info[r_idx].misc_flags.has_not(MonsterMiscType::TANUKI)) {
100         return r_idx;
101     }
102
103     get_mon_num_prep(player_ptr, monster_hook_tanuki, nullptr);
104     int attempts = 1000;
105     DEPTH min = std::min(floor_ptr->base_level - 5, 50);
106     while (--attempts) {
107         auto ap_r_idx = get_mon_num(player_ptr, 0, floor_ptr->base_level + 10, PM_NONE);
108         if (monraces_info[ap_r_idx].level >= min) {
109             return ap_r_idx;
110         }
111     }
112
113     return r_idx;
114 }
115
116 /*!
117  * @brief ユニークが生成可能か評価する
118  * @param player_ptr プレイヤーへの参照ポインタ
119  * @param r_idx 生成モンスター種族
120  * @return ユニークの生成が不可能な条件ならFALSE、それ以外はTRUE
121  */
122 static bool check_unique_placeable(const FloorType &floor, MonsterRaceId r_idx, BIT_FLAGS mode)
123 {
124     if (AngbandSystem::get_instance().is_phase_out()) {
125         return true;
126     }
127
128     if (any_bits(mode, PM_CLONE)) {
129         return true;
130     }
131
132     auto *r_ptr = &monraces_info[r_idx];
133     auto is_unique = r_ptr->kind_flags.has(MonsterKindType::UNIQUE) || r_ptr->population_flags.has(MonsterPopulationType::NAZGUL);
134     is_unique &= r_ptr->cur_num >= r_ptr->max_num;
135     if (is_unique) {
136         return false;
137     }
138
139     if (r_ptr->population_flags.has(MonsterPopulationType::ONLY_ONE) && (r_ptr->cur_num >= 1)) {
140         return false;
141     }
142
143     if (!MonraceList::get_instance().is_selectable(r_idx)) {
144         return false;
145     }
146
147     const auto is_deep = r_ptr->misc_flags.has(MonsterMiscType::FORCE_DEPTH) && (floor.dun_level < r_ptr->level);
148     const auto is_questor = !ironman_nightmare || any_bits(r_ptr->flags1, RF1_QUESTOR);
149     return !is_deep || !is_questor;
150 }
151
152 /*!
153  * @brief クエスト内に生成可能か評価する
154  * @param floor フロアへの参照
155  * @param r_idx 生成モンスター種族
156  * @return 生成が可能ならTRUE、不可能ならFALSE
157  */
158 static bool check_quest_placeable(const FloorType &floor, MonsterRaceId r_idx)
159 {
160     if (!inside_quest(floor.get_quest_id())) {
161         return true;
162     }
163
164     const auto &quest_list = QuestList::get_instance();
165     QuestId number = floor.get_quest_id();
166     const auto *q_ptr = &quest_list[number];
167     if ((q_ptr->type != QuestKindType::KILL_LEVEL) && (q_ptr->type != QuestKindType::RANDOM)) {
168         return true;
169     }
170     if (r_idx != q_ptr->r_idx) {
171         return true;
172     }
173     int number_mon = 0;
174     for (int i2 = 0; i2 < floor.width; ++i2) {
175         for (int j2 = 0; j2 < floor.height; j2++) {
176             auto quest_monster = (floor.grid_array[j2][i2].m_idx > 0);
177             quest_monster &= (floor.m_list[floor.grid_array[j2][i2].m_idx].r_idx == q_ptr->r_idx);
178             if (quest_monster) {
179                 number_mon++;
180             }
181         }
182     }
183
184     if (number_mon + q_ptr->cur_num >= q_ptr->max_num) {
185         return false;
186     }
187     return true;
188 }
189
190 /*!
191  * @brief 守りのルーン上にモンスターの配置を試みる
192  * @param player_ptr プレイヤーへの参照ポインタ
193  * @param r_idx 生成モンスター種族
194  * @param y 生成位置y座標
195  * @param x 生成位置x座標
196  * @return 生成が可能ならTRUE、不可能ならFALSE
197  */
198 static bool check_procection_rune(PlayerType *player_ptr, MonsterRaceId r_idx, POSITION y, POSITION x)
199 {
200     auto *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
201     if (!g_ptr->is_rune_protection()) {
202         return true;
203     }
204
205     auto *r_ptr = &monraces_info[r_idx];
206     if (randint1(BREAK_RUNE_PROTECTION) >= (r_ptr->level + 20)) {
207         return false;
208     }
209
210     if (any_bits(g_ptr->info, CAVE_MARK)) {
211         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
212     }
213
214     reset_bits(g_ptr->info, CAVE_MARK);
215     reset_bits(g_ptr->info, CAVE_OBJECT);
216     g_ptr->mimic = 0;
217     note_spot(player_ptr, y, x);
218     return true;
219 }
220
221 static void warn_unique_generation(PlayerType *player_ptr, MonsterRaceId r_idx)
222 {
223     if (!player_ptr->warning || !w_ptr->character_dungeon) {
224         return;
225     }
226
227     auto *r_ptr = &monraces_info[r_idx];
228     if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
229         return;
230     }
231
232     std::string color;
233     if (r_ptr->level > player_ptr->lev + 30) {
234         color = _("黒く", "black");
235     } else if (r_ptr->level > player_ptr->lev + 15) {
236         color = _("紫色に", "purple");
237     } else if (r_ptr->level > player_ptr->lev + 5) {
238         color = _("ルビー色に", "deep red");
239     } else if (r_ptr->level > player_ptr->lev - 5) {
240         color = _("赤く", "red");
241     } else if (r_ptr->level > player_ptr->lev - 15) {
242         color = _("ピンク色に", "pink");
243     } else {
244         color = _("白く", "white");
245     }
246
247     auto *o_ptr = choose_warning_item(player_ptr);
248     if (o_ptr != nullptr) {
249         const auto item_name = describe_flavor(player_ptr, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
250         msg_format(_("%sは%s光った。", "%s glows %s."), item_name.data(), color.data());
251     } else {
252         msg_format(_("%s光る物が頭に浮かんだ。", "A %s image forms in your mind."), color.data());
253     }
254 }
255
256 /*!
257  * @brief モンスターを一体生成する / Attempt to place a monster of the given race at the given location.
258  * @param player_ptr プレイヤーへの参照ポインタ
259  * @param who 召喚を行ったモンスターID
260  * @param y 生成位置y座標
261  * @param x 生成位置x座標
262  * @param r_idx 生成モンスター種族
263  * @param mode 生成オプション
264  * @return 成功したらtrue
265  */
266 bool place_monster_one(PlayerType *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MonsterRaceId r_idx, BIT_FLAGS mode)
267 {
268     auto &floor = *player_ptr->current_floor_ptr;
269     auto *g_ptr = &floor.grid_array[y][x];
270     auto *r_ptr = &monraces_info[r_idx];
271     concptr name = r_ptr->name.data();
272
273     if (player_ptr->wild_mode || !in_bounds(&floor, y, x) || !MonsterRace(r_idx).is_valid()) {
274         return false;
275     }
276
277     if (none_bits(mode, PM_IGNORE_TERRAIN) && (pattern_tile(&floor, y, x) || !monster_can_enter(player_ptr, y, x, r_ptr, 0))) {
278         return false;
279     }
280
281     if (!check_unique_placeable(floor, r_idx, mode) || !check_quest_placeable(floor, r_idx) || !check_procection_rune(player_ptr, r_idx, y, x)) {
282         return false;
283     }
284
285     msg_format_wizard(player_ptr, CHEAT_MONSTER, _("%s(Lv%d)を生成しました。", "%s(Lv%d) was generated."), name, r_ptr->level);
286     if (r_ptr->kind_flags.has(MonsterKindType::UNIQUE) || r_ptr->population_flags.has(MonsterPopulationType::NAZGUL) || (r_ptr->level < 10)) {
287         reset_bits(mode, PM_KAGE);
288     }
289
290     g_ptr->m_idx = m_pop(&floor);
291     hack_m_idx_ii = g_ptr->m_idx;
292     if (!g_ptr->m_idx) {
293         return false;
294     }
295
296     MonsterEntity *m_ptr;
297     m_ptr = &floor.m_list[g_ptr->m_idx];
298     m_ptr->r_idx = r_idx;
299     m_ptr->ap_r_idx = initial_r_appearance(player_ptr, r_idx, mode);
300
301     m_ptr->mflag.clear();
302     m_ptr->mflag2.clear();
303     if (any_bits(mode, PM_MULTIPLY) && (who > 0) && !floor.m_list[who].is_original_ap()) {
304         m_ptr->ap_r_idx = floor.m_list[who].ap_r_idx;
305         if (floor.m_list[who].mflag2.has(MonsterConstantFlagType::KAGE)) {
306             m_ptr->mflag2.set(MonsterConstantFlagType::KAGE);
307         }
308     }
309
310     if ((who > 0) && r_ptr->kind_flags.has_none_of(alignment_mask)) {
311         m_ptr->sub_align = floor.m_list[who].sub_align;
312     } else {
313         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
314         if (r_ptr->kind_flags.has(MonsterKindType::EVIL)) {
315             set_bits(m_ptr->sub_align, SUB_ALIGN_EVIL);
316         }
317         if (r_ptr->kind_flags.has(MonsterKindType::GOOD)) {
318             set_bits(m_ptr->sub_align, SUB_ALIGN_GOOD);
319         }
320     }
321
322     m_ptr->fy = y;
323     m_ptr->fx = x;
324     m_ptr->current_floor_ptr = &floor;
325
326     for (int cmi = 0; cmi < MAX_MTIMED; cmi++) {
327         m_ptr->mtimed[cmi] = 0;
328     }
329
330     m_ptr->cdis = 0;
331     reset_target(m_ptr);
332     m_ptr->nickname.clear();
333     m_ptr->exp = 0;
334
335     if (who > 0 && floor.m_list[who].is_pet()) {
336         set_bits(mode, PM_FORCE_PET);
337         m_ptr->parent_m_idx = who;
338     } else {
339         m_ptr->parent_m_idx = 0;
340     }
341
342     if (r_ptr->misc_flags.has(MonsterMiscType::CHAMELEON)) {
343         choose_new_monster(player_ptr, g_ptr->m_idx, true, MonsterRace::empty_id());
344         r_ptr = &m_ptr->get_monrace();
345         m_ptr->mflag2.set(MonsterConstantFlagType::CHAMELEON);
346         if (r_ptr->kind_flags.has(MonsterKindType::UNIQUE) && (who <= 0)) {
347             m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
348         }
349     } else if (any_bits(mode, PM_KAGE) && none_bits(mode, PM_FORCE_PET)) {
350         m_ptr->ap_r_idx = MonsterRaceId::KAGE;
351         m_ptr->mflag2.set(MonsterConstantFlagType::KAGE);
352     }
353
354     if (any_bits(mode, PM_CLONE)) {
355         m_ptr->mflag2.set(MonsterConstantFlagType::CLONED);
356     }
357
358     if (any_bits(mode, PM_NO_PET)) {
359         m_ptr->mflag2.set(MonsterConstantFlagType::NOPET);
360     }
361
362     m_ptr->ml = false;
363     if (any_bits(mode, PM_FORCE_PET)) {
364         set_pet(player_ptr, m_ptr);
365     } else if (((who == 0) && r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY)) || is_friendly_idx(player_ptr, who) || any_bits(mode, PM_FORCE_FRIENDLY)) {
366         if (!monster_has_hostile_align(player_ptr, nullptr, 0, -1, r_ptr) && !player_ptr->current_floor_ptr->inside_arena) {
367             set_friendly(m_ptr);
368         }
369     }
370
371     m_ptr->mtimed[MTIMED_CSLEEP] = 0;
372     if (any_bits(mode, PM_ALLOW_SLEEP) && r_ptr->sleep && !ironman_nightmare) {
373         int val = r_ptr->sleep;
374         (void)set_monster_csleep(player_ptr, g_ptr->m_idx, (val * 2) + randint1(val * 10));
375     }
376
377     if (r_ptr->misc_flags.has(MonsterMiscType::FORCE_MAXHP)) {
378         m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
379     } else {
380         m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
381     }
382
383     if (ironman_nightmare) {
384         auto hp = m_ptr->max_maxhp * 2;
385         m_ptr->max_maxhp = std::min(MONSTER_MAXHP, hp);
386     }
387
388     m_ptr->maxhp = m_ptr->max_maxhp;
389     if (r_ptr->cur_hp_per != 0) {
390         m_ptr->hp = m_ptr->maxhp * r_ptr->cur_hp_per / 100;
391     } else {
392         m_ptr->hp = m_ptr->maxhp;
393     }
394
395     m_ptr->dealt_damage = 0;
396
397     m_ptr->mspeed = get_mspeed(&floor, r_ptr);
398
399     if (any_bits(mode, PM_HASTE)) {
400         (void)set_monster_fast(player_ptr, g_ptr->m_idx, 100);
401     }
402
403     if (!ironman_nightmare) {
404         m_ptr->energy_need = ENERGY_NEED() - (int16_t)randint0(100);
405     } else {
406         m_ptr->energy_need = ENERGY_NEED() - (int16_t)randint0(100) * 2;
407     }
408
409     if (r_ptr->behavior_flags.has(MonsterBehaviorType::PREVENT_SUDDEN_MAGIC) && !ironman_nightmare) {
410         m_ptr->mflag.set(MonsterTemporaryFlagType::PREVENT_MAGIC);
411     }
412
413     if (g_ptr->m_idx < hack_m_idx) {
414         m_ptr->mflag.set(MonsterTemporaryFlagType::BORN);
415     }
416
417     auto is_awake_lightning_monster = r_ptr->brightness_flags.has_any_of(self_ld_mask);
418     is_awake_lightning_monster |= r_ptr->brightness_flags.has_any_of(has_ld_mask) && !m_ptr->is_asleep();
419     if (is_awake_lightning_monster) {
420         RedrawingFlagsUpdater::get_instance().set_flag(StatusRecalculatingFlag::MONSTER_LITE);
421     }
422
423     update_monster(player_ptr, g_ptr->m_idx, true);
424
425     m_ptr->get_real_monrace().cur_num++;
426
427     /*
428      * Memorize location of the unique monster in saved floors.
429      * A unique monster move from old saved floor.
430      */
431     if (w_ptr->character_dungeon && (r_ptr->kind_flags.has(MonsterKindType::UNIQUE) || r_ptr->population_flags.has(MonsterPopulationType::NAZGUL))) {
432         m_ptr->get_real_monrace().floor_id = player_ptr->floor_id;
433     }
434
435     if (r_ptr->misc_flags.has(MonsterMiscType::MULTIPLY)) {
436         floor.num_repro++;
437     }
438
439     warn_unique_generation(player_ptr, r_idx);
440     if (!g_ptr->is_rune_explosion()) {
441         return true;
442     }
443
444     if (randint1(BREAK_RUNE_EXPLOSION) > r_ptr->level) {
445         if (any_bits(g_ptr->info, CAVE_MARK)) {
446             msg_print(_("ルーンが爆発した!", "The rune explodes!"));
447             project(player_ptr, 0, 2, y, x, 2 * (player_ptr->lev + damroll(7, 7)), AttributeType::MANA,
448                 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI));
449         }
450     } else {
451         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
452     }
453
454     reset_bits(g_ptr->info, CAVE_MARK);
455     reset_bits(g_ptr->info, CAVE_OBJECT);
456     g_ptr->mimic = 0;
457
458     note_spot(player_ptr, y, x);
459     lite_spot(player_ptr, y, x);
460
461     return true;
462 }