OSDN Git Service

Merge pull request #2723 from Hourier/Fix-History-Generator-Humand
[hengbandforosx/hengbandosx.git] / src / monster-floor / monster-summon.cpp
1 #include "monster-floor/monster-summon.h"
2 #include "dungeon/dungeon-flag-types.h"
3 #include "floor/geometry.h"
4 #include "floor/wild.h"
5 #include "main/sound-definitions-table.h"
6 #include "main/sound-of-music.h"
7 #include "monster-floor/monster-generator.h"
8 #include "monster-floor/place-monster-types.h"
9 #include "monster-race/monster-race-hook.h"
10 #include "monster-race/monster-race.h"
11 #include "monster-race/race-flags1.h"
12 #include "monster-race/race-flags7.h"
13 #include "monster-race/race-indice-types.h"
14 #include "monster/monster-info.h"
15 #include "monster/monster-list.h"
16 #include "monster/monster-util.h"
17 #include "mspell/summon-checker.h"
18 #include "spell/summon-types.h"
19 #include "system/dungeon-info.h"
20 #include "system/floor-type-definition.h"
21 #include "system/monster-race-definition.h"
22 #include "system/monster-type-definition.h"
23 #include "system/player-type-definition.h"
24
25 /*!
26  * @var summon_specific_who
27  * @brief 召喚を行ったプレイヤーあるいはモンスターのIDを示すグローバル変数 / Hack -- the index of the summoning monster
28  * @todo summon_specific_who グローバル変数の除去と関数引数への代替を行う
29  */
30 int summon_specific_who = -1;
31
32 /*!
33  * @var summon_unique_okay
34  * @brief 召喚対象にユニークを含めるかを示すグローバル変数 / summoning unique enable
35  * @todo summon_unique_okay グローバル変数の除去と関数引数への代替を行う
36  */
37 bool summon_unique_okay = false;
38
39 /*!
40  * @brief モンスターが召喚の基本条件に合っているかをチェックする / Hack -- help decide if a monster race is "okay" to summon
41  * @param r_idx チェックするモンスター種族ID
42  * @return 召喚対象にできるならばTRUE
43  */
44 static bool summon_specific_okay(PlayerType *player_ptr, MonsterRaceId r_idx)
45 {
46     auto *r_ptr = &monraces_info[r_idx];
47     if (!mon_hook_dungeon(player_ptr, r_idx)) {
48         return false;
49     }
50
51     if (summon_specific_who > 0) {
52         auto *m_ptr = &player_ptr->current_floor_ptr->m_list[summon_specific_who];
53         if (monster_has_hostile_align(player_ptr, m_ptr, 0, 0, r_ptr)) {
54             return false;
55         }
56     } else if (summon_specific_who < 0) {
57         if (monster_has_hostile_align(player_ptr, nullptr, 10, -10, r_ptr) && !one_in_(std::abs(player_ptr->alignment) / 2 + 1)) {
58             return false;
59         }
60     }
61
62     if (!summon_unique_okay && (r_ptr->kind_flags.has(MonsterKindType::UNIQUE) || (r_ptr->population_flags.has(MonsterPopulationType::NAZGUL)))) {
63         return false;
64     }
65
66     if (!summon_specific_type) {
67         return true;
68     }
69
70     if ((summon_specific_who < 0) && (r_ptr->kind_flags.has(MonsterKindType::UNIQUE) || (r_ptr->population_flags.has(MonsterPopulationType::NAZGUL))) && monster_has_hostile_align(player_ptr, nullptr, 10, -10, r_ptr)) {
71         return false;
72     }
73
74     if ((r_ptr->flags7 & RF7_CHAMELEON) && dungeons_info[player_ptr->dungeon_idx].flags.has(DungeonFeatureType::CHAMELEON)) {
75         return true;
76     }
77
78     if (summon_specific_who > 0) {
79         auto *m_ptr = &player_ptr->current_floor_ptr->m_list[summon_specific_who];
80         return check_summon_specific(player_ptr, m_ptr->r_idx, r_idx);
81     } else {
82         return check_summon_specific(player_ptr, MonsterRaceId::PLAYER, r_idx);
83     }
84 }
85
86 /*!
87  * @brief モンスター死亡時に召喚されうるモンスターかどうかの判定
88  * @param type モンスター種族ID
89  * @return 召喚されうるならばTRUE (あやしい影として生成されない)
90  */
91 static bool is_dead_summoning(summon_type type)
92 {
93     bool summoning = type == SUMMON_BLUE_HORROR;
94     summoning |= type == SUMMON_DAWN;
95     summoning |= type == SUMMON_TOTEM_MOAI;
96     return summoning;
97 }
98
99 /*!
100  * @brief 荒野のレベルを含めた階層レベルを返す
101  * @param player_ptr プレイヤーへの参照ポインタ
102  * @return 階層レベル
103  * @details
104  * ダンジョン及びクエストはdun_level>0となる。
105  * 荒野はdun_level==0なので、その場合荒野レベルを返す。
106  */
107 DEPTH get_dungeon_or_wilderness_level(PlayerType *player_ptr)
108 {
109     auto *floor_ptr = player_ptr->current_floor_ptr;
110     if (floor_ptr->dun_level > 0) {
111         return floor_ptr->dun_level;
112     }
113
114     return wilderness[player_ptr->wilderness_y][player_ptr->wilderness_x].level;
115 }
116
117 /*!
118  * @brief モンスターを召喚により配置する / Place a monster (of the specified "type") near the given location. Return TRUE if a monster was actually summoned.
119  * @param player_ptr プレイヤーへの参照ポインタ
120  * @param who 召喚主のモンスター情報ID
121  * @param y1 目標地点y座標
122  * @param x1 目標地点x座標
123  * @param lev 相当生成階
124  * @param type 召喚種別
125  * @param mode 生成オプション
126  * @return 召喚できたらtrueを返す
127  */
128 bool summon_specific(PlayerType *player_ptr, MONSTER_IDX who, POSITION y1, POSITION x1, DEPTH lev, summon_type type, BIT_FLAGS mode)
129 {
130     auto *floor_ptr = player_ptr->current_floor_ptr;
131     if (floor_ptr->inside_arena) {
132         return false;
133     }
134
135     POSITION x, y;
136     if (!mon_scatter(player_ptr, MonsterRace::empty_id(), &y, &x, y1, x1, 2)) {
137         return false;
138     }
139
140     summon_specific_who = who;
141     summon_specific_type = type;
142     summon_unique_okay = (mode & PM_ALLOW_UNIQUE) != 0;
143     get_mon_num_prep(player_ptr, summon_specific_okay, get_monster_hook2(player_ptr, y, x));
144
145     DEPTH dlev = get_dungeon_or_wilderness_level(player_ptr);
146     MonsterRaceId r_idx = get_mon_num(player_ptr, 0, (dlev + lev) / 2 + 5, 0);
147     if (!MonsterRace(r_idx).is_valid()) {
148         summon_specific_type = SUMMON_NONE;
149         return false;
150     }
151
152     if (is_dead_summoning(type)) {
153         mode |= PM_NO_KAGE;
154     }
155
156     if (!place_monster_aux(player_ptr, who, y, x, r_idx, mode)) {
157         summon_specific_type = SUMMON_NONE;
158         return false;
159     }
160
161     summon_specific_type = SUMMON_NONE;
162
163     bool notice = false;
164     if (who <= 0) {
165         notice = true;
166     } else {
167         auto *m_ptr = &player_ptr->current_floor_ptr->m_list[who];
168         if (m_ptr->is_pet()) {
169             notice = true;
170         } else if (is_seen(player_ptr, m_ptr)) {
171             notice = true;
172         } else if (player_can_see_bold(player_ptr, y, x)) {
173             notice = true;
174         }
175     }
176
177     if (notice) {
178         sound(SOUND_SUMMON);
179     }
180
181     return true;
182 }
183
184 /*!
185  * @brief 特定モンスター種族を召喚により生成する / A "dangerous" function, creates a pet of the specified type
186  * @param player_ptr プレイヤーへの参照ポインタ
187  * @param who 召喚主のモンスター情報ID
188  * @param oy 目標地点y座標
189  * @param ox 目標地点x座標
190  * @param r_idx 生成するモンスター種族ID
191  * @param mode 生成オプション
192  * @return 召喚できたらtrueを返す
193  */
194 bool summon_named_creature(PlayerType *player_ptr, MONSTER_IDX who, POSITION oy, POSITION ox, MonsterRaceId r_idx, BIT_FLAGS mode)
195 {
196     if (!MonsterRace(r_idx).is_valid() || (r_idx >= static_cast<MonsterRaceId>(monraces_info.size()))) {
197         return false;
198     }
199
200     POSITION x, y;
201     if (player_ptr->current_floor_ptr->inside_arena || !mon_scatter(player_ptr, r_idx, &y, &x, oy, ox, 2)) {
202         return false;
203     }
204
205     return place_monster_aux(player_ptr, who, y, x, r_idx, (mode | PM_NO_KAGE));
206 }