Wrath of Zeus
Made by Torchlight Games for CSE 125 SP24
Loading...
Searching...
No Matches
mazegenerator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <unordered_map>
5#include <unordered_set>
6#include <string>
7#include <vector>
8#include <queue>
9#include <memory>
10#include <optional>
11#include <boost/filesystem.hpp>
12#include "server/game/grid.hpp"
14#define GLM_ENABLE_EXPERIMENTAL
15#include "glm/gtx/hash.hpp"
16
17#define REQUIRED_NUM_ROOMS 100
18
19enum class RoomType {
20 EMPTY, // testing
21 SPAWN,
22 EASY,
23 MEDIUM,
24 HARD,
25 LOOT,
26 EXIT,
27 ORB,
28 CUSTOM // fully custom maze
29};
30
31#define ALL_TYPES { \
32 RoomType::EMPTY, \
33 RoomType::SPAWN, \
34 RoomType::EASY, \
35 RoomType::MEDIUM, \
36 RoomType::HARD, \
37 RoomType::LOOT, \
38 RoomType::EXIT, \
39 RoomType::ORB, \
40 RoomType::CUSTOM \
41}
42
44 T = 0b0001,
45 B = 0b0010,
46 L = 0b0100,
47 R = 0b1000
48};
49
50enum class RoomSize {
51 _10x10,
52 _20x20,
53 _40x40,
55};
56
57static std::unordered_map<RoomSize, std::vector<glm::ivec2>> TOP_ENTRY_COORDS = {
58 { RoomSize::_10x10, {{4, 0}, {5, 0}} },
59 { RoomSize::_20x20, {{4, 0}, {5, 0}, {14, 0}, {15, 0}} },
60 { RoomSize::_40x40, {{4, 0}, {5, 0}, {14, 0}, {15, 0}, {24, 0}, {25, 0}, {34, 0}, {35, 0}} },
61};
62static std::unordered_map<RoomSize, std::vector<glm::ivec2>> LEFT_ENTRY_COORDS = {
63 { RoomSize::_10x10, {{0, 4}, {0, 5}} },
64 { RoomSize::_20x20, {{0, 4}, {0, 5}, {0, 14}, {0, 15}} },
65 { RoomSize::_40x40, {{0, 4}, {0, 5}, {0, 14}, {0, 15}, {0, 24}, {0, 25}, {0, 34}, {0, 35}} },
66};
67static std::unordered_map<RoomSize, std::vector<glm::ivec2>> BOTTOM_ENTRY_COORDS = {
68 { RoomSize::_10x10, {{4, 9}, {5, 9}} },
69 { RoomSize::_20x20, {{4, 19}, {5, 19}, {14, 19}, {15, 19}} },
70 { RoomSize::_40x40, {{4, 39}, {5, 39}, {14, 39}, {15, 39}, {24, 39}, {25, 39}, {34, 39}, {35, 39}} },
71};
72static std::unordered_map<RoomSize, std::vector<glm::ivec2>> RIGHT_ENTRY_COORDS = {
73 { RoomSize::_10x10, {{9, 4}, {9, 5}} },
74 { RoomSize::_20x20, {{19, 4}, {19, 5}, {19, 14}, {19, 15}} },
75 { RoomSize::_40x40, {{39, 4}, {39, 5}, {39, 14}, {39, 15}, {39, 24}, {39, 25}, {39, 34}, {39, 35}} },
76};
77
78struct RoomClass {
81 uint8_t entries;
82
83 bool operator==(const RoomClass& other) const {
84 return this->type == other.type && this->size == other.size && this->entries == other.entries;
85 }
86};
87
89 size_t operator()(const RoomClass& p) const {
90 size_t seed = 0;
91 boost::hash_combine(seed, p.type);
92 boost::hash_combine(seed, p.size);
93 boost::hash_combine(seed, p.entries);
94 return seed;
95 }
96};
97
98struct Room {
99 Room(Grid&& grid, const RoomClass& rclass, int id):
100 grid(grid), rclass(rclass), id(id) {}
101
104 int id;
105};
106
108 bool operator()(const glm::ivec2& lhs, const glm::ivec2& rhs) const {
109 if (lhs.x < rhs.x) {
110 return true;
111 }
112 if (lhs.x > rhs.x) {
113 return false;
114 }
115 return (lhs.y < rhs.y);
116 }
117};
118
119#define GRID_CELLS_PER_ROOM 10 // each room is 10x10 (or some multiple, but the unit level is 10)
120
122public:
123 explicit MazeGenerator(GameConfig config);
124
125 std::optional<Grid> generate();
126
127private:
128 RoomType _getRoomType(boost::filesystem::path path);
129 RoomSize _parseRoomSize(int rows, int columns);
130 uint8_t _identifyEntryways(Grid& grid);
131 void _validateRoom(Grid& grid, const RoomClass& rclass);
132
133 std::vector<glm::ivec2> _getRoomCoordsTakenBy(RoomSize size, glm::ivec2 top_left);
134
135 std::shared_ptr<Room> _pullRoomByPolicy();
136 std::shared_ptr<Room> _pullRoomByType(RoomType type);
137
142 std::vector<std::pair<glm::ivec2, RoomEntry>> _getAdjRoomCoords(std::shared_ptr<Room> room, glm::ivec2 origin_coord);
143
144 // bool _hasOpenConnection(std::shared_ptr<Room> room, glm::ivec2 origin_coord);
145
146 // frontier -> (coord, U) => add a room at coord, and make sure it has an UP entrance
147 // (connecting from the prior room's bottom)
148 std::queue<std::pair<glm::ivec2, RoomEntry>> frontier;
149 void _placeRoom(std::shared_ptr<Room> room, glm::ivec2 origin_coord);
150
151 bool _isOpenWorldCoord(glm::ivec2 coord);
152
153 std::vector<glm::ivec2> _getPossibleOriginCoords(std::shared_ptr<Room> room, RoomEntry required_entry, glm::ivec2 connect_coord);
154
155 std::map<glm::ivec2, int, ivec2_comparator> maze;
156
157 void _loadRoom(boost::filesystem::path path, bool procedural);
158 std::unordered_map<RoomClass, std::shared_ptr<Room>, RoomClassHash> rooms_by_class;
159 std::unordered_map<RoomType, std::vector<std::shared_ptr<Room>>> rooms_by_type;
160 std::unordered_map<int , std::shared_ptr<Room>> rooms_by_id;
161
162 int _next_room_id;
163
164 void _generatePolicy();
165
166 int _num_rooms_placed;
167 std::deque<RoomType> _policy;
168
169 // ids that should not be reused
170 // currently, only ids of 20x20 and 40x40 rooms will be placed in here to prevent them from being placed
171 // twice in the same maze
172 std::unordered_set<int> used_room_ids;
173};
Definition: grid.hpp:7
Definition: mazegenerator.hpp:121
std::optional< Grid > generate()
Definition: mazegenerator.cpp:57
GLuint id
Definition: glad.h:1776
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glad.h:1531
GLsizeiptr size
Definition: glad.h:1803
RoomType
Definition: mazegenerator.hpp:19
RoomSize
Definition: mazegenerator.hpp:50
RoomEntry
Definition: mazegenerator.hpp:43
@ R
Definition: mazegenerator.hpp:47
@ T
Definition: mazegenerator.hpp:44
@ B
Definition: mazegenerator.hpp:45
@ L
Definition: mazegenerator.hpp:46
Definition: config.hpp:12
Definition: mazegenerator.hpp:88
size_t operator()(const RoomClass &p) const
Definition: mazegenerator.hpp:89
Definition: mazegenerator.hpp:78
RoomSize size
Definition: mazegenerator.hpp:80
bool operator==(const RoomClass &other) const
Definition: mazegenerator.hpp:83
uint8_t entries
Definition: mazegenerator.hpp:81
RoomType type
Definition: mazegenerator.hpp:79
Definition: mazegenerator.hpp:98
Grid grid
Definition: mazegenerator.hpp:102
RoomClass rclass
Definition: mazegenerator.hpp:103
Room(Grid &&grid, const RoomClass &rclass, int id)
Definition: mazegenerator.hpp:99
int id
Definition: mazegenerator.hpp:104
Definition: mazegenerator.hpp:107
bool operator()(const glm::ivec2 &lhs, const glm::ivec2 &rhs) const
Definition: mazegenerator.hpp:108