OSDN Git Service

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