1 #include "monster/monster-util.h"
2 #include "dungeon/dungeon-flag-types.h"
3 #include "dungeon/dungeon.h"
4 #include "dungeon/quest.h"
5 #include "floor/wild.h"
6 #include "game-option/cheat-options.h"
7 #include "grid/feature.h"
8 #include "monster-race/monster-race-hook.h"
9 #include "monster-race/monster-race.h"
10 #include "monster-race/race-ability-mask.h"
11 #include "monster-race/race-flags-resistance.h"
12 #include "monster-race/race-flags1.h"
13 #include "monster-race/race-flags7.h"
14 #include "monster-race/race-indice-types.h"
15 #include "spell/summon-types.h"
16 #include "system/alloc-entries.h"
17 #include "system/floor-type-definition.h"
18 #include "system/grid-type-definition.h"
19 #include "system/monster-race-definition.h"
20 #include "system/player-type-definition.h"
21 #include "util/bit-flags-calculator.h"
22 #include "view/display-messages.h"
24 enum dungeon_mode_type {
26 DUNGEON_MODE_NAND = 2,
31 MONSTER_IDX hack_m_idx = 0; /* Hack -- see "process_monsters()" */
32 MONSTER_IDX hack_m_idx_ii = 0;
35 * @var chameleon_change_m_idx
36 * @brief カメレオンの変身先モンスターIDを受け渡すためのグローバル変数
37 * @todo 変数渡しの問題などもあるができればchameleon_change_m_idxのグローバル変数を除去し、関数引き渡しに移行すること
39 int chameleon_change_m_idx = 0;
42 * @var summon_specific_type
43 * @brief 召喚条件を指定するグローバル変数 / Hack -- the "type" of the current "summon specific"
44 * @todo summon_specific_typeグローバル変数の除去と関数引数への代替を行う
46 summon_type summon_specific_type = SUMMON_NONE;
49 * @brief 指定されたモンスター種族がダンジョンの制限にかかるかどうかをチェックする / Some dungeon types restrict the possible monsters.
50 * @param player_ptr プレイヤーへの参照ポインタ
51 * @param r_idx チェックするモンスター種族ID
52 * @return 召喚条件が一致するならtrue / Return TRUE is the monster is OK and FALSE otherwise
54 static bool restrict_monster_to_dungeon(player_type *player_ptr, MONRACE_IDX r_idx)
56 DUNGEON_IDX d_idx = player_ptr->dungeon_idx;
57 dungeon_type *d_ptr = &d_info[d_idx];
58 monster_race *r_ptr = &r_info[r_idx];
60 if (d_ptr->flags.has(DungeonFeatureType::CHAMELEON)) {
61 if (chameleon_change_m_idx)
65 if (d_ptr->flags.has(DungeonFeatureType::NO_MAGIC)) {
66 if (r_idx != MON_CHAMELEON && r_ptr->freq_spell && r_ptr->ability_flags.has_none_of(RF_ABILITY_NOMAGIC_MASK))
70 if (d_ptr->flags.has(DungeonFeatureType::NO_MELEE)) {
71 if (r_idx == MON_CHAMELEON)
73 if (r_ptr->ability_flags.has_none_of(RF_ABILITY_BOLT_MASK | RF_ABILITY_BEAM_MASK | RF_ABILITY_BALL_MASK)
74 && r_ptr->ability_flags.has_none_of(
75 { MonsterAbilityType::CAUSE_1, MonsterAbilityType::CAUSE_2, MonsterAbilityType::CAUSE_3, MonsterAbilityType::CAUSE_4, MonsterAbilityType::MIND_BLAST, MonsterAbilityType::BRAIN_SMASH }))
79 floor_type *floor_ptr = player_ptr->current_floor_ptr;
80 if (d_ptr->flags.has(DungeonFeatureType::BEGINNER)) {
81 if (r_ptr->level > floor_ptr->dun_level)
85 if (d_ptr->special_div >= 64)
87 if (summon_specific_type && d_ptr->flags.has_not(DungeonFeatureType::CHAMELEON))
91 switch (d_ptr->mode) {
92 case DUNGEON_MODE_AND: {
94 if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
99 if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
103 if (d_ptr->mflags3) {
104 if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
108 if (d_ptr->m_ability_flags.any()) {
109 if (!r_ptr->ability_flags.has_all_of(d_ptr->m_ability_flags))
113 if (d_ptr->mflags7) {
114 if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
118 if (d_ptr->mflags8) {
119 if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
123 if (d_ptr->mflags9) {
124 if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
128 if (d_ptr->mflagsr) {
129 if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
133 for (a = 0; a < 5; a++)
134 if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char))
139 case DUNGEON_MODE_NAND: {
140 if (d_ptr->mflags1) {
141 if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
145 if (d_ptr->mflags2) {
146 if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
150 if (d_ptr->mflags3) {
151 if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
155 if (d_ptr->m_ability_flags.any()) {
156 if (!r_ptr->ability_flags.has_all_of(d_ptr->m_ability_flags))
160 if (d_ptr->mflags7) {
161 if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
165 if (d_ptr->mflags8) {
166 if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
170 if (d_ptr->mflags9) {
171 if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
175 if (d_ptr->mflagsr) {
176 if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
180 for (a = 0; a < 5; a++)
181 if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char))
186 case DUNGEON_MODE_OR: {
187 if (r_ptr->flags1 & d_ptr->mflags1)
189 if (r_ptr->flags2 & d_ptr->mflags2)
191 if (r_ptr->flags3 & d_ptr->mflags3)
193 if (r_ptr->ability_flags.has_any_of(d_ptr->m_ability_flags))
195 if (r_ptr->flags7 & d_ptr->mflags7)
197 if (r_ptr->flags8 & d_ptr->mflags8)
199 if (r_ptr->flags9 & d_ptr->mflags9)
201 if (r_ptr->flagsr & d_ptr->mflagsr)
203 for (a = 0; a < 5; a++)
204 if (d_ptr->r_char[a] == r_ptr->d_char)
209 case DUNGEON_MODE_NOR: {
210 if (r_ptr->flags1 & d_ptr->mflags1)
212 if (r_ptr->flags2 & d_ptr->mflags2)
214 if (r_ptr->flags3 & d_ptr->mflags3)
216 if (r_ptr->ability_flags.has_any_of(d_ptr->m_ability_flags))
218 if (r_ptr->flags7 & d_ptr->mflags7)
220 if (r_ptr->flags8 & d_ptr->mflags8)
222 if (r_ptr->flags9 & d_ptr->mflags9)
224 if (r_ptr->flagsr & d_ptr->mflagsr)
226 for (a = 0; a < 5; a++)
227 if (d_ptr->r_char[a] == r_ptr->d_char)
238 * @brief プレイヤーの現在の広域マップ座標から得た地勢を元にモンスターの生成条件関数を返す
239 * @param player_ptr プレイヤーへの参照ポインタ
240 * @return 地勢にあったモンスターの生成条件関数
242 monsterrace_hook_type get_monster_hook(player_type *player_ptr)
244 if ((player_ptr->current_floor_ptr->dun_level > 0) || (player_ptr->current_floor_ptr->inside_quest > 0))
245 return (monsterrace_hook_type)mon_hook_dungeon;
247 switch (wilderness[player_ptr->wilderness_y][player_ptr->wilderness_x].terrain) {
249 return (monsterrace_hook_type)mon_hook_town;
250 case TERRAIN_DEEP_WATER:
251 return (monsterrace_hook_type)mon_hook_ocean;
252 case TERRAIN_SHALLOW_WATER:
254 return (monsterrace_hook_type)mon_hook_shore;
257 return (monsterrace_hook_type)mon_hook_waste;
259 return (monsterrace_hook_type)mon_hook_grass;
261 return (monsterrace_hook_type)mon_hook_wood;
262 case TERRAIN_SHALLOW_LAVA:
263 case TERRAIN_DEEP_LAVA:
264 return (monsterrace_hook_type)mon_hook_volcano;
265 case TERRAIN_MOUNTAIN:
266 return (monsterrace_hook_type)mon_hook_mountain;
268 return (monsterrace_hook_type)mon_hook_dungeon;
273 * @brief 指定された広域マップ座標の地勢を元にモンスターの生成条件関数を返す
274 * @return 地勢にあったモンスターの生成条件関数
276 monsterrace_hook_type get_monster_hook2(player_type *player_ptr, POSITION y, POSITION x)
278 feature_type *f_ptr = &f_info[player_ptr->current_floor_ptr->grid_array[y][x].feat];
279 if (f_ptr->flags.has(FF::WATER))
280 return f_ptr->flags.has(FF::DEEP) ? (monsterrace_hook_type)mon_hook_deep_water : (monsterrace_hook_type)mon_hook_shallow_water;
282 if (f_ptr->flags.has(FF::LAVA))
283 return (monsterrace_hook_type)mon_hook_lava;
285 return (monsterrace_hook_type)mon_hook_floor;
289 * @brief モンスター生成テーブルの重みを指定条件に従って変更する。
291 * @param hook1 生成制約関数1 (nullptr の場合、制約なし)
292 * @param hook2 生成制約関数2 (nullptr の場合、制約なし)
293 * @param restrict_to_dungeon 現在プレイヤーのいるダンジョンの制約を適用するか
296 * モンスター生成テーブル alloc_race_table の各要素の基本重み prob1 を指定条件
297 * に従って変更し、結果を prob2 に書き込む。
299 static errr do_get_mon_num_prep(player_type *player_ptr, const monsterrace_hook_type hook1, const monsterrace_hook_type hook2, const bool restrict_to_dungeon)
301 const floor_type *const floor_ptr = player_ptr->current_floor_ptr;
304 int mon_num = 0; // 重み(prob2)が正の要素数
305 DEPTH lev_min = MAX_DEPTH; // 重みが正の要素のうち最小階
306 DEPTH lev_max = 0; // 重みが正の要素のうち最大階
307 int prob2_total = 0; // 重みの総和
309 // モンスター生成テーブルの各要素について重みを修正する。
310 for (auto i = 0U; i < alloc_race_table.size(); i++) {
311 alloc_entry *const entry = &alloc_race_table[i];
312 const monster_race *const r_ptr = &r_info[entry->index];
314 // 生成を禁止する要素は重み 0 とする。
318 // テーブル内の無効エントリもこれに該当する(alloc_race_table は生成時にゼロクリアされるため)。
319 if (entry->prob1 <= 0)
322 // いずれかの生成制約関数が偽を返したら生成禁止。
323 if ((hook1 && !hook1(player_ptr, entry->index)) || (hook2 && !hook2(player_ptr, entry->index)))
326 // 原則生成禁止するものたち(フェイズアウト状態 / カメレオンの変身先 / ダンジョンの主召喚 は例外)。
327 if (!player_ptr->phase_out && !chameleon_change_m_idx && summon_specific_type != SUMMON_GUARDIANS) {
329 if (r_ptr->flags1 & RF1_QUESTOR)
333 if (r_ptr->flags7 & RF7_GUARDIAN)
336 // RF1_FORCE_DEPTH フラグ持ちは指定階未満では生成禁止。
337 if ((r_ptr->flags1 & RF1_FORCE_DEPTH) && (r_ptr->level > floor_ptr->dun_level))
340 // クエスト内でRES_ALLの生成を禁止する (殲滅系クエストの詰み防止)
341 if (player_ptr->current_floor_ptr->inside_quest && any_bits(r_ptr->flagsr, RFR_RES_ALL))
345 // 生成を許可するものは基本重みをそのまま引き継ぐ。
346 entry->prob2 = entry->prob1;
348 // 引数で指定されていればさらにダンジョンによる制約を試みる。
349 if (restrict_to_dungeon) {
350 // ダンジョンによる制約を適用する条件:
355 const bool in_random_quest = floor_ptr->inside_quest && !quest_type::is_fixed(floor_ptr->inside_quest);
356 const bool cond = !player_ptr->phase_out && floor_ptr->dun_level > 0 && !in_random_quest;
358 if (cond && !restrict_monster_to_dungeon(player_ptr, entry->index)) {
359 // ダンジョンによる制約に掛かった場合、重みを special_div/64 倍する。
361 const int numer = entry->prob2 * d_info[player_ptr->dungeon_idx].special_div;
362 const int q = numer / 64;
363 const int r = numer % 64;
364 entry->prob2 = (PROB)(randint0(64) < r ? q + 1 : q);
369 if (entry->prob2 > 0) {
371 if (lev_min > entry->level)
372 lev_min = entry->level;
373 if (lev_max < entry->level)
374 lev_max = entry->level;
375 prob2_total += entry->prob2;
379 // チートオプションが有効なら統計情報を出力。
381 msg_format(_("モンスター第2次候補数:%d(%d-%dF)%d ", "monster second selection:%d(%d-%dF)%d "), mon_num, lev_min, lev_max, prob2_total);
387 * @brief モンスター生成テーブルの重み修正
389 * @param hook1 生成制約関数1 (nullptr の場合、制約なし)
390 * @param hook2 生成制約関数2 (nullptr の場合、制約なし)
393 * get_mon_num() を呼ぶ前に get_mon_num_prep() 系関数のいずれかを呼ぶこと。
395 errr get_mon_num_prep(player_type *player_ptr, const monsterrace_hook_type hook1, const monsterrace_hook_type hook2)
397 return do_get_mon_num_prep(player_ptr, hook1, hook2, true);
401 * @brief モンスター生成テーブルの重み修正(賞金首選定用)
404 * get_mon_num() を呼ぶ前に get_mon_num_prep 系関数のいずれかを呼ぶこと。
406 errr get_mon_num_prep_bounty(player_type *player_ptr)
408 return do_get_mon_num_prep(player_ptr, nullptr, nullptr, false);