OSDN Git Service

a7a3378825d7cdac1dc174ab40f31f8be6dc944f
[hengbandforosx/hengbandosx.git] / src / dungeon / quest-monster-placer.cpp
1 #include "dungeon/quest-monster-placer.h"
2 #include "dungeon/quest.h"
3 #include "floor/floor-generator-util.h"
4 #include "floor/geometry.h"
5 #include "monster-floor/monster-generator.h"
6 #include "monster-floor/place-monster-types.h"
7 #include "monster-race/monster-race.h"
8 #include "monster-race/race-flags1.h"
9 #include "monster/monster-info.h"
10 #include "system/floor-type-definition.h"
11 #include "system/grid-type-definition.h"
12 #include "system/monster-race-info.h"
13 #include "system/player-type-definition.h"
14 #include "system/terrain-type-definition.h"
15 #include "util/bit-flags-calculator.h"
16
17 /*!
18  * @brief クエストに関わるモンスターの配置を行う / Place quest monsters
19  * @param player_ptr プレイヤーへの参照ポインタ
20  * @return 成功したならばTRUEを返す
21  */
22 bool place_quest_monsters(PlayerType *player_ptr)
23 {
24     auto *floor_ptr = player_ptr->current_floor_ptr;
25     const auto &quest_list = QuestList::get_instance();
26     for (const auto &[q_idx, quest] : quest_list) {
27         MonsterRaceInfo *r_ptr;
28         BIT_FLAGS mode;
29
30         auto no_quest_monsters = quest.status != QuestStatusType::TAKEN;
31         no_quest_monsters |= (quest.type != QuestKindType::KILL_LEVEL && quest.type != QuestKindType::RANDOM);
32         no_quest_monsters |= quest.level != floor_ptr->dun_level;
33         no_quest_monsters |= floor_ptr->dungeon_idx != quest.dungeon;
34         no_quest_monsters |= any_bits(quest.flags, QUEST_FLAG_PRESET);
35
36         if (no_quest_monsters) {
37             continue;
38         }
39
40         r_ptr = &monraces_info[quest.r_idx];
41         if (r_ptr->kind_flags.has(MonsterKindType::UNIQUE) && (r_ptr->cur_num >= r_ptr->max_num)) {
42             continue;
43         }
44
45         mode = PM_NO_KAGE | PM_NO_PET;
46         if (!(r_ptr->flags1 & RF1_FRIENDS)) {
47             mode |= PM_ALLOW_GROUP;
48         }
49
50         for (int j = 0; j < (quest.max_num - quest.cur_num); j++) {
51             int k;
52             for (k = 0; k < SAFE_MAX_ATTEMPTS; k++) {
53                 POSITION x = 0;
54                 POSITION y = 0;
55                 int l;
56                 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--) {
57                     y = randint0(floor_ptr->height);
58                     x = randint0(floor_ptr->width);
59                     const auto &grid = floor_ptr->get_grid({ y, x });
60                     const auto &terrain = grid.get_terrain();
61                     if (terrain.flags.has_none_of({ TerrainCharacteristics::MOVE, TerrainCharacteristics::CAN_FLY })) {
62                         continue;
63                     }
64
65                     if (!monster_can_enter(player_ptr, y, x, r_ptr, 0)) {
66                         continue;
67                     }
68
69                     if (distance(y, x, player_ptr->y, player_ptr->x) < 10) {
70                         continue;
71                     }
72
73                     if (grid.is_icky()) {
74                         continue;
75                     } else {
76                         break;
77                     }
78                 }
79
80                 if (l == 0) {
81                     return false;
82                 }
83
84                 if (place_specific_monster(player_ptr, 0, y, x, quest.r_idx, mode)) {
85                     break;
86                 } else {
87                     continue;
88                 }
89             }
90
91             if (k == SAFE_MAX_ATTEMPTS) {
92                 return false;
93             }
94         }
95     }
96
97     return true;
98 }