OSDN Git Service

Merge pull request #3374 from Hourier/Constexpr-RedrawingFlags
[hengbandforosx/hengbandosx.git] / src / monster / monster-list.cpp
1 /*!
2  * @brief モンスター処理 / misc code for monsters
3  * @date 2014/07/08
4  * @author
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  * 2014 Deskull rearranged comment for Doxygen.
10  */
11
12 #include "monster/monster-list.h"
13 #include "core/speed-table.h"
14 #include "dungeon/dungeon-flag-types.h"
15 #include "floor/cave.h"
16 #include "floor/floor-object.h"
17 #include "floor/geometry.h"
18 #include "floor/wild.h"
19 #include "game-option/birth-options.h"
20 #include "game-option/cheat-options.h"
21 #include "grid/grid.h"
22 #include "monster-floor/monster-summon.h"
23 #include "monster-race/monster-kind-mask.h"
24 #include "monster-race/monster-race.h"
25 #include "monster-race/race-brightness-mask.h"
26 #include "monster-race/race-flags1.h"
27 #include "monster-race/race-flags2.h"
28 #include "monster-race/race-flags3.h"
29 #include "monster-race/race-flags7.h"
30 #include "monster-race/race-indice-types.h"
31 #include "monster/monster-describer.h"
32 #include "monster/monster-info.h"
33 #include "monster/monster-update.h"
34 #include "monster/monster-util.h"
35 #include "pet/pet-fall-off.h"
36 #include "player/player-status.h"
37 #include "system/alloc-entries.h"
38 #include "system/dungeon-info.h"
39 #include "system/floor-type-definition.h"
40 #include "system/grid-type-definition.h"
41 #include "system/monster-entity.h"
42 #include "system/monster-race-info.h"
43 #include "system/player-type-definition.h"
44 #include "system/redrawing-flags-updater.h"
45 #include "util/probability-table.h"
46 #include "view/display-messages.h"
47 #include "world/world.h"
48 #include <cmath>
49 #include <iterator>
50
51 #define HORDE_NOGOOD 0x01 /*!< (未実装フラグ)HORDE生成でGOODなモンスターの生成を禁止する? */
52 #define HORDE_NOEVIL 0x02 /*!< (未実装フラグ)HORDE生成でEVILなモンスターの生成を禁止する? */
53
54 /*!
55  * @brief モンスター配列の空きを探す / Acquires and returns the index of a "free" monster.
56  * @return 利用可能なモンスター配列の添字
57  * @details
58  * This routine should almost never fail, but it *can* happen.
59  */
60 MONSTER_IDX m_pop(FloorType *floor_ptr)
61 {
62     /* Normal allocation */
63     if (floor_ptr->m_max < w_ptr->max_m_idx) {
64         MONSTER_IDX i = floor_ptr->m_max;
65         floor_ptr->m_max++;
66         floor_ptr->m_cnt++;
67         return i;
68     }
69
70     /* Recycle dead monsters */
71     for (MONSTER_IDX i = 1; i < floor_ptr->m_max; i++) {
72         MonsterEntity *m_ptr;
73         m_ptr = &floor_ptr->m_list[i];
74         if (MonsterRace(m_ptr->r_idx).is_valid()) {
75             continue;
76         }
77         floor_ptr->m_cnt++;
78         return i;
79     }
80
81     if (w_ptr->character_dungeon) {
82         msg_print(_("モンスターが多すぎる!", "Too many monsters!"));
83     }
84     return 0;
85 }
86
87 /*!
88  * @brief 生成モンスター種族を1種生成テーブルから選択する
89  * @param player_ptr プレイヤーへの参照ポインタ
90  * @param min_level 最小生成階
91  * @param max_level 最大生成階
92  * @return 選択されたモンスター生成種族
93  * @details nasty生成 (ゲーム内経過日数に応じて、現在フロアより深いフロアのモンスターを出現させる仕様)は
94  */
95 MonsterRaceId get_mon_num(PlayerType *player_ptr, DEPTH min_level, DEPTH max_level, BIT_FLAGS option)
96 {
97     /* town max_level : same delay as 10F, no nasty mons till day18 */
98     auto delay = static_cast<int>(std::sqrt(max_level * 10000)) + (max_level * 5);
99     if (!max_level) {
100         delay = 360;
101     }
102
103     if (max_level > MAX_DEPTH - 1) {
104         max_level = MAX_DEPTH - 1;
105     }
106
107     /* +1 per day after the base date */
108     /* base dates : day5(1F), day18(10F,0F), day34(30F), day53(60F), day69(90F) */
109     const auto over_days = std::max<int>(0, w_ptr->dungeon_turn / (TURNS_PER_TICK * 10000L) - delay / 20);
110
111     /* Probability starts from 1/25, reaches 1/3 after 44days from a max_level dependent base date */
112     /* Boost level starts from 0, reaches +25lv after 75days from a max_level dependent base date */
113     constexpr auto chance_nasty_monster = 25;
114     constexpr auto max_num_nasty_monsters = 3;
115     constexpr auto max_depth_nasty_monster = 25;
116     auto chance_nasty = std::max(max_num_nasty_monsters, chance_nasty_monster - over_days / 2);
117     auto nasty_level = std::min(max_depth_nasty_monster, over_days / 3);
118     const auto &floor = *player_ptr->current_floor_ptr;
119     const auto &dungeon = floor.get_dungeon_definition();
120     if (dungeon.flags.has(DungeonFeatureType::MAZE)) {
121         chance_nasty = std::min(chance_nasty / 2, chance_nasty - 10);
122         if (chance_nasty < 2) {
123             chance_nasty = 2;
124         }
125
126         nasty_level += 2;
127         max_level += 3;
128     }
129
130     /* Boost the max_level */
131     if ((option & GMN_ARENA) || dungeon.flags.has_not(DungeonFeatureType::BEGINNER)) {
132         /* Nightmare mode allows more out-of depth monsters */
133         if (ironman_nightmare && !randint0(chance_nasty)) {
134             /* What a bizarre calculation */
135             max_level = 1 + (max_level * MAX_DEPTH / randint1(MAX_DEPTH));
136         } else {
137             /* Occasional "nasty" monster */
138             if (!randint0(chance_nasty)) {
139                 /* Pick a max_level bonus */
140                 max_level += nasty_level;
141             }
142         }
143     }
144
145     ProbabilityTable<int> prob_table;
146
147     /* Process probabilities */
148     for (auto i = 0U; i < alloc_race_table.size(); i++) {
149         const auto &entry = alloc_race_table[i];
150         if (entry.level < min_level) {
151             continue;
152         }
153         if (max_level < entry.level) {
154             break;
155         } // sorted by depth array,
156         auto r_idx = i2enum<MonsterRaceId>(entry.index);
157         auto r_ptr = &monraces_info[r_idx];
158         if (!(option & GMN_ARENA) && !chameleon_change_m_idx) {
159             if ((r_ptr->kind_flags.has(MonsterKindType::UNIQUE) || r_ptr->population_flags.has(MonsterPopulationType::NAZGUL)) && (r_ptr->cur_num >= r_ptr->max_num)) {
160                 continue;
161             }
162
163             if ((r_ptr->flags7 & (RF7_UNIQUE2)) && (r_ptr->cur_num >= 1)) {
164                 continue;
165             }
166
167             if (r_idx == MonsterRaceId::BANORLUPART) {
168                 if (monraces_info[MonsterRaceId::BANOR].cur_num > 0) {
169                     continue;
170                 }
171                 if (monraces_info[MonsterRaceId::LUPART].cur_num > 0) {
172                     continue;
173                 }
174             }
175         }
176
177         prob_table.entry_item(i, entry.prob2);
178     }
179
180     if (cheat_hear) {
181         msg_format(_("モンスター第3次候補数:%lu(%d-%dF)%d ", "monster third selection:%lu(%d-%dF)%d "), prob_table.item_count(), min_level, max_level,
182             prob_table.total_prob());
183     }
184
185     if (prob_table.empty()) {
186         return MonsterRace::empty_id();
187     }
188
189     // 40%で1回、50%で2回、10%で3回抽選し、その中で一番レベルが高いモンスターを選択する
190     int n = 1;
191
192     const int p = randint0(100);
193     if (p < 60) {
194         n++;
195     }
196     if (p < 10) {
197         n++;
198     }
199
200     std::vector<int> result;
201     ProbabilityTable<int>::lottery(std::back_inserter(result), prob_table, n);
202
203     auto it = std::max_element(result.begin(), result.end(), [](int a, int b) { return alloc_race_table[a].level < alloc_race_table[b].level; });
204
205     return i2enum<MonsterRaceId>(alloc_race_table[*it].index);
206 }
207
208 /*!
209  * @param player_ptr プレイヤーへの参照ポインタ
210  * @brief カメレオンの王の変身対象となるモンスターかどうか判定する / Hack -- the index of the summoning monster
211  * @param r_idx モンスター種族ID
212  * @return 対象にできるならtrueを返す
213  */
214 static bool monster_hook_chameleon_lord(PlayerType *player_ptr, MonsterRaceId r_idx)
215 {
216     auto *floor_ptr = player_ptr->current_floor_ptr;
217     auto *r_ptr = &monraces_info[r_idx];
218     auto *m_ptr = &floor_ptr->m_list[chameleon_change_m_idx];
219     MonsterRaceInfo *old_r_ptr = &monraces_info[m_ptr->r_idx];
220
221     if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
222         return false;
223     }
224     if (r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY) || (r_ptr->flags7 & RF7_CHAMELEON)) {
225         return false;
226     }
227
228     if (std::abs(r_ptr->level - monraces_info[MonsterRaceId::CHAMELEON_K].level) > 5) {
229         return false;
230     }
231
232     if (m_ptr->is_explodable()) {
233         return false;
234     }
235
236     if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) {
237         return false;
238     }
239
240     if (!(old_r_ptr->flags7 & RF7_CHAMELEON)) {
241         if (monster_has_hostile_align(player_ptr, m_ptr, 0, 0, r_ptr)) {
242             return false;
243         }
244     } else if (summon_specific_who > 0) {
245         if (monster_has_hostile_align(player_ptr, &floor_ptr->m_list[summon_specific_who], 0, 0, r_ptr)) {
246             return false;
247         }
248     }
249
250     return true;
251 }
252
253 /*!
254  * @brief カメレオンの変身対象となるモンスターかどうか判定する / Hack -- the index of the summoning monster
255  * @param r_idx モンスター種族ID
256  * @return 対象にできるならtrueを返す
257  * @todo グローバル変数対策の上 monster_hook.cへ移す。
258  */
259 static bool monster_hook_chameleon(PlayerType *player_ptr, MonsterRaceId r_idx)
260 {
261     auto *floor_ptr = player_ptr->current_floor_ptr;
262     auto *r_ptr = &monraces_info[r_idx];
263     auto *m_ptr = &floor_ptr->m_list[chameleon_change_m_idx];
264     MonsterRaceInfo *old_r_ptr = &monraces_info[m_ptr->r_idx];
265
266     if (r_ptr->kind_flags.has(MonsterKindType::UNIQUE)) {
267         return false;
268     }
269     if (r_ptr->flags2 & RF2_MULTIPLY) {
270         return false;
271     }
272     if (r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY) || (r_ptr->flags7 & RF7_CHAMELEON)) {
273         return false;
274     }
275
276     if (m_ptr->is_explodable()) {
277         return false;
278     }
279
280     if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) {
281         return false;
282     }
283
284     if (!(old_r_ptr->flags7 & RF7_CHAMELEON)) {
285         if (old_r_ptr->kind_flags.has(MonsterKindType::GOOD) && r_ptr->kind_flags.has_not(MonsterKindType::GOOD)) {
286             return false;
287         }
288         if (old_r_ptr->kind_flags.has(MonsterKindType::EVIL) && r_ptr->kind_flags.has_not(MonsterKindType::EVIL)) {
289             return false;
290         }
291         if (old_r_ptr->kind_flags.has_none_of(alignment_mask)) {
292             return false;
293         }
294     } else if (summon_specific_who > 0) {
295         if (monster_has_hostile_align(player_ptr, &floor_ptr->m_list[summon_specific_who], 0, 0, r_ptr)) {
296             return false;
297         }
298     }
299
300     auto hook_pf = get_monster_hook(player_ptr);
301     return (*hook_pf)(player_ptr, r_idx);
302 }
303
304 /*!
305  * @brief モンスターの変身処理
306  * @param player_ptr プレイヤーへの参照ポインタ
307  * @param m_idx 変身処理を受けるモンスター情報のID
308  * @param born 生成時の初変身先指定ならばtrue
309  * @param r_idx 旧モンスター種族のID
310  */
311 void choose_new_monster(PlayerType *player_ptr, MONSTER_IDX m_idx, bool born, MonsterRaceId r_idx)
312 {
313     auto *floor_ptr = player_ptr->current_floor_ptr;
314     auto *m_ptr = &floor_ptr->m_list[m_idx];
315     MonsterRaceInfo *r_ptr;
316
317     bool old_unique = false;
318     if (monraces_info[m_ptr->r_idx].kind_flags.has(MonsterKindType::UNIQUE)) {
319         old_unique = true;
320     }
321     if (old_unique && (r_idx == MonsterRaceId::CHAMELEON)) {
322         r_idx = MonsterRaceId::CHAMELEON_K;
323     }
324     r_ptr = &monraces_info[r_idx];
325
326     const auto old_m_name = monster_desc(player_ptr, m_ptr, 0);
327
328     if (!MonsterRace(r_idx).is_valid()) {
329         DEPTH level;
330
331         chameleon_change_m_idx = m_idx;
332         if (old_unique) {
333             get_mon_num_prep(player_ptr, monster_hook_chameleon_lord, nullptr);
334         } else {
335             get_mon_num_prep(player_ptr, monster_hook_chameleon, nullptr);
336         }
337
338         if (old_unique) {
339             level = monraces_info[MonsterRaceId::CHAMELEON_K].level;
340         } else if (!floor_ptr->dun_level) {
341             level = wilderness[player_ptr->wilderness_y][player_ptr->wilderness_x].level;
342         } else {
343             level = floor_ptr->dun_level;
344         }
345
346         if (floor_ptr->get_dungeon_definition().flags.has(DungeonFeatureType::CHAMELEON)) {
347             level += 2 + randint1(3);
348         }
349
350         r_idx = get_mon_num(player_ptr, 0, level, 0);
351         r_ptr = &monraces_info[r_idx];
352
353         chameleon_change_m_idx = 0;
354         if (!MonsterRace(r_idx).is_valid()) {
355             return;
356         }
357     }
358
359     m_ptr->r_idx = r_idx;
360     m_ptr->ap_r_idx = r_idx;
361     update_monster(player_ptr, m_idx, false);
362     lite_spot(player_ptr, m_ptr->fy, m_ptr->fx);
363
364     auto old_r_idx = m_ptr->r_idx;
365     if (monraces_info[old_r_idx].brightness_flags.has_any_of(ld_mask) || r_ptr->brightness_flags.has_any_of(ld_mask)) {
366         RedrawingFlagsUpdater::get_instance().set_flag(StatusRecalculatingFlag::MONSTER_LITE);
367     }
368
369     if (born) {
370         if (r_ptr->kind_flags.has_any_of(alignment_mask)) {
371             m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
372             if (r_ptr->kind_flags.has(MonsterKindType::EVIL)) {
373                 m_ptr->sub_align |= SUB_ALIGN_EVIL;
374             }
375             if (r_ptr->kind_flags.has(MonsterKindType::GOOD)) {
376                 m_ptr->sub_align |= SUB_ALIGN_GOOD;
377             }
378         }
379
380         return;
381     }
382
383     if (m_idx == player_ptr->riding) {
384         msg_format(_("突然%sが変身した。", "Suddenly, %s transforms!"), old_m_name.data());
385         if (!(r_ptr->flags7 & RF7_RIDING)) {
386             if (process_fall_off_horse(player_ptr, 0, true)) {
387                 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
388                 msg_print(_("地面に落とされた。", format("You have fallen from %s.", m_name.data())));
389             }
390         }
391     }
392
393     m_ptr->mspeed = get_mspeed(floor_ptr, r_ptr);
394
395     int oldmaxhp = m_ptr->max_maxhp;
396     if (r_ptr->flags1 & RF1_FORCE_MAXHP) {
397         m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
398     } else {
399         m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
400     }
401
402     if (ironman_nightmare) {
403         auto hp = m_ptr->max_maxhp * 2;
404         m_ptr->max_maxhp = std::min(MONSTER_MAXHP, hp);
405     }
406
407     m_ptr->maxhp = (long)(m_ptr->maxhp * m_ptr->max_maxhp) / oldmaxhp;
408     if (m_ptr->maxhp < 1) {
409         m_ptr->maxhp = 1;
410     }
411     m_ptr->hp = (long)(m_ptr->hp * m_ptr->max_maxhp) / oldmaxhp;
412     m_ptr->dealt_damage = 0;
413 }
414
415 /*!
416  * @brief モンスターの個体加速を設定する / Get initial monster speed
417  * @param r_ptr モンスター種族の参照ポインタ
418  * @return 加速値
419  */
420 byte get_mspeed(FloorType *floor_ptr, MonsterRaceInfo *r_ptr)
421 {
422     auto mspeed = r_ptr->speed;
423     if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE) && !floor_ptr->inside_arena) {
424         /* Allow some small variation per monster */
425         int i = speed_to_energy(r_ptr->speed) / (one_in_(4) ? 3 : 10);
426         if (i) {
427             mspeed += rand_spread(0, i);
428         }
429     }
430
431     if (mspeed > 199) {
432         mspeed = 199;
433     }
434
435     return mspeed;
436 }
437
438 /*!
439  * @brief 指定したモンスターに隣接しているモンスターの数を返す。
440  * / Count number of adjacent monsters
441  * @param player_ptr プレイヤーへの参照ポインタ
442  * @param m_idx 隣接数を調べたいモンスターのID
443  * @return 隣接しているモンスターの数
444  */
445 int get_monster_crowd_number(FloorType *floor_ptr, MONSTER_IDX m_idx)
446 {
447     auto *m_ptr = &floor_ptr->m_list[m_idx];
448     POSITION my = m_ptr->fy;
449     POSITION mx = m_ptr->fx;
450     int count = 0;
451     for (int i = 0; i < 7; i++) {
452         int ay = my + ddy_ddd[i];
453         int ax = mx + ddx_ddd[i];
454
455         if (!in_bounds(floor_ptr, ay, ax)) {
456             continue;
457         }
458         if (floor_ptr->grid_array[ay][ax].m_idx > 0) {
459             count++;
460         }
461     }
462
463     return count;
464 }