OSDN Git Service

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