OSDN Git Service

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