OSDN Git Service

Merge pull request #3814 from Slimebreath6078/feature/Add_Laffey_II
[hengbandforosx/hengbandosx.git] / src / monster / monster-util.cpp
1 #include "monster/monster-util.h"
2 #include "dungeon/dungeon-flag-types.h"
3 #include "dungeon/quest.h"
4 #include "floor/wild.h"
5 #include "game-option/cheat-options.h"
6 #include "monster-race/monster-race-hook.h"
7 #include "monster-race/monster-race.h"
8 #include "monster-race/race-ability-mask.h"
9 #include "monster-race/race-flags-resistance.h"
10 #include "monster-race/race-indice-types.h"
11 #include "monster-race/race-misc-flags.h"
12 #include "spell/summon-types.h"
13 #include "system/alloc-entries.h"
14 #include "system/angband-system.h"
15 #include "system/dungeon-info.h"
16 #include "system/floor-type-definition.h"
17 #include "system/grid-type-definition.h"
18 #include "system/monster-race-info.h"
19 #include "system/player-type-definition.h"
20 #include "system/terrain-type-definition.h"
21 #include "util/bit-flags-calculator.h"
22 #include "view/display-messages.h"
23 #include <algorithm>
24 #include <iterator>
25
26 enum dungeon_mode_type {
27     DUNGEON_MODE_AND = 1,
28     DUNGEON_MODE_NAND = 2,
29     DUNGEON_MODE_OR = 3,
30     DUNGEON_MODE_NOR = 4,
31 };
32
33 MONSTER_IDX hack_m_idx = 0; /* Hack -- see "process_monsters()" */
34 MONSTER_IDX hack_m_idx_ii = 0;
35
36 /*!
37  * @var chameleon_change_m_idx
38  * @brief カメレオンの変身先モンスターIDを受け渡すためのグローバル変数
39  * @todo 変数渡しの問題などもあるができればchameleon_change_m_idxのグローバル変数を除去し、関数引き渡しに移行すること
40  */
41 int chameleon_change_m_idx = 0;
42
43 /*!
44  * @var summon_specific_type
45  * @brief 召喚条件を指定するグローバル変数 / Hack -- the "type" of the current "summon specific"
46  * @todo summon_specific_typeグローバル変数の除去と関数引数への代替を行う
47  */
48 summon_type summon_specific_type = SUMMON_NONE;
49
50 /**
51  * @brief モンスターがダンジョンに出現できる条件を満たしているかのフラグ判定関数(AND)
52  *
53  * @param r_flags モンスター側のフラグ
54  * @param d_flags ダンジョン側の判定フラグ
55  * @return 出現可能かどうか
56  */
57 template <class T>
58 static bool is_possible_monster_and(const EnumClassFlagGroup<T> &r_flags, const EnumClassFlagGroup<T> &d_flags)
59 {
60     return r_flags.has_all_of(d_flags);
61 }
62
63 /**
64  * @brief モンスターがダンジョンに出現できる条件を満たしているかのフラグ判定関数(OR)
65  *
66  * @param r_flags モンスター側のフラグ
67  * @param d_flags ダンジョン側の判定フラグ
68  * @return 出現可能かどうか
69  */
70 template <class T>
71 static bool is_possible_monster_or(const EnumClassFlagGroup<T> &r_flags, const EnumClassFlagGroup<T> &d_flags)
72 {
73     return r_flags.has_any_of(d_flags);
74 }
75
76 /*!
77  * @brief 指定されたモンスター種族がダンジョンの制限にかかるかどうかをチェックする / Some dungeon types restrict the possible monsters.
78  * @param floor_ptr フロアへの参照ポインタ
79  * @param r_idx チェックするモンスター種族ID
80  * @return 召喚条件が一致するならtrue / Return TRUE is the monster is OK and FALSE otherwise
81  */
82 static bool restrict_monster_to_dungeon(const FloorType *floor_ptr, MonsterRaceId r_idx)
83 {
84     const auto *d_ptr = &floor_ptr->get_dungeon_definition();
85     const auto *r_ptr = &monraces_info[r_idx];
86     if (d_ptr->flags.has(DungeonFeatureType::CHAMELEON)) {
87         if (chameleon_change_m_idx) {
88             return true;
89         }
90     }
91
92     if (d_ptr->flags.has(DungeonFeatureType::NO_MAGIC)) {
93         if (r_idx != MonsterRaceId::CHAMELEON && r_ptr->freq_spell && r_ptr->ability_flags.has_none_of(RF_ABILITY_NOMAGIC_MASK)) {
94             return false;
95         }
96     }
97
98     if (d_ptr->flags.has(DungeonFeatureType::NO_MELEE)) {
99         if (r_idx == MonsterRaceId::CHAMELEON) {
100             return true;
101         }
102         if (r_ptr->ability_flags.has_none_of(RF_ABILITY_BOLT_MASK | RF_ABILITY_BEAM_MASK | RF_ABILITY_BALL_MASK) && r_ptr->ability_flags.has_none_of(
103                                                                                                                         { MonsterAbilityType::CAUSE_1, MonsterAbilityType::CAUSE_2, MonsterAbilityType::CAUSE_3, MonsterAbilityType::CAUSE_4, MonsterAbilityType::MIND_BLAST, MonsterAbilityType::BRAIN_SMASH })) {
104             return false;
105         }
106     }
107
108     if (d_ptr->flags.has(DungeonFeatureType::BEGINNER)) {
109         if (r_ptr->level > floor_ptr->dun_level) {
110             return false;
111         }
112     }
113
114     if (d_ptr->special_div >= 64) {
115         return true;
116     }
117     if (summon_specific_type && d_ptr->flags.has_not(DungeonFeatureType::CHAMELEON)) {
118         return true;
119     }
120
121     switch (d_ptr->mode) {
122     case DUNGEON_MODE_AND:
123     case DUNGEON_MODE_NAND: {
124         std::vector<bool> is_possible = {
125             is_possible_monster_and(r_ptr->ability_flags, d_ptr->mon_ability_flags),
126             is_possible_monster_and(r_ptr->behavior_flags, d_ptr->mon_behavior_flags),
127             is_possible_monster_and(r_ptr->resistance_flags, d_ptr->mon_resistance_flags),
128             is_possible_monster_and(r_ptr->drop_flags, d_ptr->mon_drop_flags),
129             is_possible_monster_and(r_ptr->kind_flags, d_ptr->mon_kind_flags),
130             is_possible_monster_and(r_ptr->wilderness_flags, d_ptr->mon_wilderness_flags),
131             is_possible_monster_and(r_ptr->feature_flags, d_ptr->mon_feature_flags),
132             is_possible_monster_and(r_ptr->population_flags, d_ptr->mon_population_flags),
133             is_possible_monster_and(r_ptr->speak_flags, d_ptr->mon_speak_flags),
134             is_possible_monster_and(r_ptr->brightness_flags, d_ptr->mon_brightness_flags),
135             is_male(d_ptr->mon_sex) ? is_male(r_ptr->sex) : true,
136             is_female(d_ptr->mon_sex) ? is_female(r_ptr->sex) : true,
137         };
138
139         auto result = std::all_of(is_possible.begin(), is_possible.end(), [](const auto &v) { return v; });
140         result &= std::all_of(d_ptr->r_chars.begin(), d_ptr->r_chars.end(), [r_ptr](const auto &v) { return v == r_ptr->d_char; });
141
142         return d_ptr->mode == DUNGEON_MODE_AND ? result : !result;
143     }
144     case DUNGEON_MODE_OR:
145     case DUNGEON_MODE_NOR: {
146         std::vector<bool> is_possible = {
147             is_possible_monster_or(r_ptr->ability_flags, d_ptr->mon_ability_flags),
148             is_possible_monster_or(r_ptr->behavior_flags, d_ptr->mon_behavior_flags),
149             is_possible_monster_or(r_ptr->resistance_flags, d_ptr->mon_resistance_flags),
150             is_possible_monster_or(r_ptr->drop_flags, d_ptr->mon_drop_flags),
151             is_possible_monster_or(r_ptr->kind_flags, d_ptr->mon_kind_flags),
152             is_possible_monster_or(r_ptr->wilderness_flags, d_ptr->mon_wilderness_flags),
153             is_possible_monster_or(r_ptr->feature_flags, d_ptr->mon_feature_flags),
154             is_possible_monster_or(r_ptr->population_flags, d_ptr->mon_population_flags),
155             is_possible_monster_or(r_ptr->speak_flags, d_ptr->mon_speak_flags),
156             is_possible_monster_or(r_ptr->brightness_flags, d_ptr->mon_brightness_flags),
157             is_male(d_ptr->mon_sex) ? is_male(r_ptr->sex) : true,
158             is_female(d_ptr->mon_sex) ? is_female(r_ptr->sex) : true,
159         };
160
161         auto result = std::any_of(is_possible.begin(), is_possible.end(), [](const auto &v) { return v; });
162         result |= std::any_of(d_ptr->r_chars.begin(), d_ptr->r_chars.end(), [r_ptr](const auto &v) { return v == r_ptr->d_char; });
163
164         return d_ptr->mode == DUNGEON_MODE_OR ? result : !result;
165     }
166     }
167
168     return true;
169 }
170
171 /*!
172  * @brief プレイヤーの現在の広域マップ座標から得た地勢を元にモンスターの生成条件関数を返す
173  * @param player_ptr プレイヤーへの参照ポインタ
174  * @return 地勢にあったモンスターの生成条件関数
175  */
176 monsterrace_hook_type get_monster_hook(PlayerType *player_ptr)
177 {
178     const auto &floor = *player_ptr->current_floor_ptr;
179     if ((floor.dun_level > 0) || (floor.is_in_quest())) {
180         return (monsterrace_hook_type)mon_hook_dungeon;
181     }
182
183     switch (wilderness[player_ptr->wilderness_y][player_ptr->wilderness_x].terrain) {
184     case TERRAIN_TOWN:
185         return (monsterrace_hook_type)mon_hook_town;
186     case TERRAIN_DEEP_WATER:
187         return (monsterrace_hook_type)mon_hook_ocean;
188     case TERRAIN_SHALLOW_WATER:
189     case TERRAIN_SWAMP:
190         return (monsterrace_hook_type)mon_hook_shore;
191     case TERRAIN_DIRT:
192     case TERRAIN_DESERT:
193         return (monsterrace_hook_type)mon_hook_waste;
194     case TERRAIN_GRASS:
195         return (monsterrace_hook_type)mon_hook_grass;
196     case TERRAIN_TREES:
197         return (monsterrace_hook_type)mon_hook_wood;
198     case TERRAIN_SHALLOW_LAVA:
199     case TERRAIN_DEEP_LAVA:
200         return (monsterrace_hook_type)mon_hook_volcano;
201     case TERRAIN_MOUNTAIN:
202         return (monsterrace_hook_type)mon_hook_mountain;
203     default:
204         return (monsterrace_hook_type)mon_hook_dungeon;
205     }
206 }
207
208 /*!
209  * @brief 指定された広域マップ座標の地勢を元にモンスターの生成条件関数を返す
210  * @return 地勢にあったモンスターの生成条件関数
211  */
212 monsterrace_hook_type get_monster_hook2(PlayerType *player_ptr, POSITION y, POSITION x)
213 {
214     const Pos2D pos(y, x);
215     const auto &terrain = player_ptr->current_floor_ptr->get_grid(pos).get_terrain();
216     if (terrain.flags.has(TerrainCharacteristics::WATER)) {
217         return terrain.flags.has(TerrainCharacteristics::DEEP) ? (monsterrace_hook_type)mon_hook_deep_water : (monsterrace_hook_type)mon_hook_shallow_water;
218     }
219
220     if (terrain.flags.has(TerrainCharacteristics::LAVA)) {
221         return (monsterrace_hook_type)mon_hook_lava;
222     }
223
224     return (monsterrace_hook_type)mon_hook_floor;
225 }
226
227 /*!
228  * @brief モンスター生成テーブルの重みを指定条件に従って変更する。
229  * @param player_ptr
230  * @param hook1 生成制約関数1 (nullptr の場合、制約なし)
231  * @param hook2 生成制約関数2 (nullptr の場合、制約なし)
232  * @param restrict_to_dungeon 現在プレイヤーのいるダンジョンの制約を適用するか
233  * @return 常に 0
234  *
235  * モンスター生成テーブル alloc_race_table の各要素の基本重み prob1 を指定条件
236  * に従って変更し、結果を prob2 に書き込む。
237  */
238 static errr do_get_mon_num_prep(PlayerType *player_ptr, const monsterrace_hook_type hook1, const monsterrace_hook_type hook2, const bool restrict_to_dungeon)
239 {
240     const FloorType *const floor_ptr = player_ptr->current_floor_ptr;
241
242     // デバッグ用統計情報。
243     int mon_num = 0; // 重み(prob2)が正の要素数
244     DEPTH lev_min = MAX_DEPTH; // 重みが正の要素のうち最小階
245     DEPTH lev_max = 0; // 重みが正の要素のうち最大階
246     int prob2_total = 0; // 重みの総和
247
248     // モンスター生成テーブルの各要素について重みを修正する。
249     const auto &system = AngbandSystem::get_instance();
250     for (auto i = 0U; i < alloc_race_table.size(); i++) {
251         alloc_entry *const entry = &alloc_race_table[i];
252         const auto entry_r_idx = i2enum<MonsterRaceId>(entry->index);
253         const MonsterRaceInfo *const r_ptr = &monraces_info[entry_r_idx];
254
255         // 生成を禁止する要素は重み 0 とする。
256         entry->prob2 = 0;
257
258         // 基本重みが 0 以下なら生成禁止。
259         // テーブル内の無効エントリもこれに該当する(alloc_race_table は生成時にゼロクリアされるため)。
260         if (entry->prob1 <= 0) {
261             continue;
262         }
263
264         // いずれかの生成制約関数が偽を返したら生成禁止。
265         if ((hook1 && !hook1(player_ptr, entry_r_idx)) || (hook2 && !hook2(player_ptr, entry_r_idx))) {
266             continue;
267         }
268
269         // 原則生成禁止するものたち(フェイズアウト状態 / カメレオンの変身先 / ダンジョンの主召喚 は例外)。
270         if (!system.is_phase_out() && !chameleon_change_m_idx && summon_specific_type != SUMMON_GUARDIANS) {
271             // クエストモンスターは生成禁止。
272             if (r_ptr->misc_flags.has(MonsterMiscType::QUESTOR)) {
273                 continue;
274             }
275
276             // ダンジョンの主は生成禁止。
277             if (r_ptr->misc_flags.has(MonsterMiscType::GUARDIAN)) {
278                 continue;
279             }
280
281             // FORCE_DEPTH フラグ持ちは指定階未満では生成禁止。
282             if (r_ptr->misc_flags.has(MonsterMiscType::FORCE_DEPTH) && (r_ptr->level > floor_ptr->dun_level)) {
283                 continue;
284             }
285
286             // クエスト内でRES_ALL及び指定階未満でのDIMINISH_MAX_DAMAGEの生成を禁止する (殲滅系クエストの詰み防止)
287             if (player_ptr->current_floor_ptr->is_in_quest()) {
288                 auto is_indefeatable = r_ptr->resistance_flags.has(MonsterResistanceType::RESIST_ALL);
289                 is_indefeatable |= r_ptr->special_flags.has(MonsterSpecialType::DIMINISH_MAX_DAMAGE) && r_ptr->level > floor_ptr->dun_level;
290                 if (is_indefeatable) {
291                     continue;
292                 }
293             }
294         }
295
296         // 生成を許可するものは基本重みをそのまま引き継ぐ。
297         entry->prob2 = entry->prob1;
298
299         // 引数で指定されていればさらにダンジョンによる制約を試みる。
300         if (restrict_to_dungeon) {
301             // ダンジョンによる制約を適用する条件:
302             //
303             //   * フェイズアウト状態でない
304             //   * 1階かそれより深いところにいる
305             //   * ランダムクエスト中でない
306             const bool in_random_quest = floor_ptr->is_in_quest() && !QuestType::is_fixed(floor_ptr->quest_number);
307             const bool cond = !system.is_phase_out() && floor_ptr->dun_level > 0 && !in_random_quest;
308
309             if (cond && !restrict_monster_to_dungeon(floor_ptr, entry_r_idx)) {
310                 // ダンジョンによる制約に掛かった場合、重みを special_div/64 倍する。
311                 // 丸めは確率的に行う。
312                 const int numer = entry->prob2 * floor_ptr->get_dungeon_definition().special_div;
313                 const int q = numer / 64;
314                 const int r = numer % 64;
315                 entry->prob2 = (PROB)(randint0(64) < r ? q + 1 : q);
316             }
317         }
318
319         // 統計情報更新。
320         if (entry->prob2 > 0) {
321             mon_num++;
322             if (lev_min > entry->level) {
323                 lev_min = entry->level;
324             }
325             if (lev_max < entry->level) {
326                 lev_max = entry->level;
327             }
328             prob2_total += entry->prob2;
329         }
330     }
331
332     // チートオプションが有効なら統計情報を出力。
333     if (cheat_hear) {
334         msg_format(_("モンスター第2次候補数:%d(%d-%dF)%d ", "monster second selection:%d(%d-%dF)%d "), mon_num, lev_min, lev_max, prob2_total);
335     }
336
337     return 0;
338 }
339
340 /*!
341  * @brief モンスター生成テーブルの重み修正
342  * @param player_ptr
343  * @param hook1 生成制約関数1 (nullptr の場合、制約なし)
344  * @param hook2 生成制約関数2 (nullptr の場合、制約なし)
345  * @return 常に 0
346  *
347  * get_mon_num() を呼ぶ前に get_mon_num_prep() 系関数のいずれかを呼ぶこと。
348  */
349 errr get_mon_num_prep(PlayerType *player_ptr, const monsterrace_hook_type hook1, const monsterrace_hook_type hook2)
350 {
351     return do_get_mon_num_prep(player_ptr, hook1, hook2, true);
352 }
353
354 /*!
355  * @brief モンスター生成テーブルの重み修正(賞金首選定用)
356  * @return 常に 0
357  *
358  * get_mon_num() を呼ぶ前に get_mon_num_prep 系関数のいずれかを呼ぶこと。
359  */
360 errr get_mon_num_prep_bounty(PlayerType *player_ptr)
361 {
362     return do_get_mon_num_prep(player_ptr, nullptr, nullptr, false);
363 }
364
365 bool is_player(MONSTER_IDX m_idx)
366 {
367     return m_idx == 0;
368 }
369
370 bool is_monster(MONSTER_IDX m_idx)
371 {
372     return m_idx > 0;
373 }