OSDN Git Service

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