OSDN Git Service

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