OSDN Git Service

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