OSDN Git Service

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