OSDN Git Service

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