diff options
Diffstat (limited to 'game_data.cpp')
-rw-r--r-- | game_data.cpp | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/game_data.cpp b/game_data.cpp new file mode 100644 index 0000000..db574d3 --- /dev/null +++ b/game_data.cpp | |||
@@ -0,0 +1,255 @@ | |||
1 | #include "game_data.h" | ||
2 | |||
3 | #include <hkutil/string.h> | ||
4 | #include <iostream> | ||
5 | #include <yaml-cpp/yaml.h> | ||
6 | |||
7 | LingoColor GetColorForString(const std::string &str) { | ||
8 | if (str == "black") { | ||
9 | return LingoColor::kBlack; | ||
10 | } else if (str == "red") { | ||
11 | return LingoColor::kRed; | ||
12 | } else if (str == "blue") { | ||
13 | return LingoColor::kBlue; | ||
14 | } else if (str == "yellow") { | ||
15 | return LingoColor::kYellow; | ||
16 | } else if (str == "orange") { | ||
17 | return LingoColor::kOrange; | ||
18 | } else if (str == "green") { | ||
19 | return LingoColor::kGreen; | ||
20 | } else if (str == "gray") { | ||
21 | return LingoColor::kGray; | ||
22 | } else if (str == "brown") { | ||
23 | return LingoColor::kBrown; | ||
24 | } else if (str == "purple") { | ||
25 | return LingoColor::kPurple; | ||
26 | } else { | ||
27 | std::cout << "Invalid color: " << str << std::endl; | ||
28 | return LingoColor::kNone; | ||
29 | } | ||
30 | } | ||
31 | |||
32 | GameData::GameData() { | ||
33 | YAML::Node lingo_config = YAML::LoadFile("assets/LL1.yaml"); | ||
34 | YAML::Node areas_config = YAML::LoadFile("assets/areas.yaml"); | ||
35 | |||
36 | rooms_.reserve(lingo_config.size() + 1); // The +1 is for Menu | ||
37 | |||
38 | for (const auto &room_it : lingo_config) { | ||
39 | int room_id = AddOrGetRoom(room_it.first.as<std::string>()); | ||
40 | Room &room_obj = rooms_[room_id]; | ||
41 | |||
42 | for (const auto &entrance_it : room_it.second["entrances"]) { | ||
43 | int from_room_id = AddOrGetRoom(entrance_it.first.as<std::string>()); | ||
44 | Room &from_room_obj = rooms_[from_room_id]; | ||
45 | |||
46 | switch (entrance_it.second.Type()) { | ||
47 | case YAML::NodeType::Scalar: { | ||
48 | // This is just "true". | ||
49 | from_room_obj.exits.push_back({.destination_room = room_id}); | ||
50 | break; | ||
51 | } | ||
52 | case YAML::NodeType::Map: { | ||
53 | Exit exit_obj; | ||
54 | exit_obj.destination_room = room_id; | ||
55 | |||
56 | if (entrance_it.second["door"]) { | ||
57 | std::string door_room = room_obj.name; | ||
58 | if (entrance_it.second["room"]) { | ||
59 | door_room = entrance_it.second["room"].as<std::string>(); | ||
60 | } | ||
61 | exit_obj.door = AddOrGetDoor( | ||
62 | door_room, entrance_it.second["door"].as<std::string>()); | ||
63 | } | ||
64 | |||
65 | from_room_obj.exits.push_back(exit_obj); | ||
66 | break; | ||
67 | } | ||
68 | case YAML::NodeType::Sequence: { | ||
69 | for (const auto &option : entrance_it.second) { | ||
70 | Exit exit_obj; | ||
71 | exit_obj.destination_room = room_id; | ||
72 | |||
73 | std::string door_room = room_obj.name; | ||
74 | if (option["room"]) { | ||
75 | door_room = option["room"].as<std::string>(); | ||
76 | } | ||
77 | exit_obj.door = | ||
78 | AddOrGetDoor(door_room, option["door"].as<std::string>()); | ||
79 | |||
80 | from_room_obj.exits.push_back(exit_obj); | ||
81 | } | ||
82 | |||
83 | break; | ||
84 | } | ||
85 | default: { | ||
86 | // This shouldn't happen. | ||
87 | std::cout << "Error reading game data: " << entrance_it << std::endl; | ||
88 | break; | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | |||
93 | if (room_it.second["panels"]) { | ||
94 | for (const auto &panel_it : room_it.second["panels"]) { | ||
95 | int panel_id = | ||
96 | AddOrGetPanel(room_obj.name, panel_it.first.as<std::string>()); | ||
97 | Panel &panel_obj = panels_[panel_id]; | ||
98 | |||
99 | if (panel_it.second["colors"]) { | ||
100 | if (panel_it.second["colors"].IsScalar()) { | ||
101 | panel_obj.colors.push_back( | ||
102 | GetColorForString(panel_it.second["colors"].as<std::string>())); | ||
103 | } else { | ||
104 | for (const auto &color_node : panel_it.second["colors"]) { | ||
105 | panel_obj.colors.push_back( | ||
106 | GetColorForString(color_node.as<std::string>())); | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | |||
111 | if (panel_it.second["required_room"]) { | ||
112 | if (panel_it.second["required_room"].IsScalar()) { | ||
113 | panel_obj.required_rooms.push_back(AddOrGetRoom( | ||
114 | panel_it.second["required_room"].as<std::string>())); | ||
115 | } else { | ||
116 | for (const auto &rr_node : panel_it.second["required_room"]) { | ||
117 | panel_obj.required_rooms.push_back( | ||
118 | AddOrGetRoom(rr_node.as<std::string>())); | ||
119 | } | ||
120 | } | ||
121 | } | ||
122 | |||
123 | if (panel_it.second["required_door"]) { | ||
124 | if (panel_it.second["required_door"].IsMap()) { | ||
125 | std::string rd_room = room_obj.name; | ||
126 | if (panel_it.second["required_door"]["room"]) { | ||
127 | rd_room = | ||
128 | panel_it.second["required_door"]["room"].as<std::string>(); | ||
129 | } | ||
130 | |||
131 | panel_obj.required_doors.push_back(AddOrGetDoor( | ||
132 | rd_room, | ||
133 | panel_it.second["required_door"]["door"].as<std::string>())); | ||
134 | } else { | ||
135 | for (const auto &rr_node : panel_it.second["required_door"]) { | ||
136 | std::string rd_room = room_obj.name; | ||
137 | if (rr_node["room"]) { | ||
138 | rd_room = rr_node["room"].as<std::string>(); | ||
139 | } | ||
140 | |||
141 | panel_obj.required_doors.push_back( | ||
142 | AddOrGetDoor(rd_room, rr_node["door"].as<std::string>())); | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | |||
147 | if (panel_it.second["check"]) { | ||
148 | panel_obj.check = panel_it.second["check"].as<bool>(); | ||
149 | } | ||
150 | |||
151 | if (panel_it.second["exclude_reduce"]) { | ||
152 | panel_obj.exclude_reduce = | ||
153 | panel_it.second["exclude_reduce"].as<bool>(); | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | |||
158 | if (room_it.second["doors"]) { | ||
159 | for (const auto &door_it : room_it.second["doors"]) { | ||
160 | int door_id = | ||
161 | AddOrGetDoor(room_obj.name, door_it.first.as<std::string>()); | ||
162 | Door &door_obj = doors_[door_id]; | ||
163 | |||
164 | bool has_external_panels = false; | ||
165 | std::vector<std::string> panel_names; | ||
166 | |||
167 | for (const auto &panel_node : door_it.second["panels"]) { | ||
168 | if (panel_node.IsScalar()) { | ||
169 | panel_names.push_back(panel_node.as<std::string>()); | ||
170 | door_obj.panels.push_back( | ||
171 | AddOrGetPanel(room_obj.name, panel_node.as<std::string>())); | ||
172 | } else { | ||
173 | has_external_panels = true; | ||
174 | panel_names.push_back(panel_node["panel"].as<std::string>()); | ||
175 | door_obj.panels.push_back( | ||
176 | AddOrGetPanel(panel_node["room"].as<std::string>(), | ||
177 | panel_node["panel"].as<std::string>())); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | if (door_it.second["skip_location"]) { | ||
182 | door_obj.skip_location = door_it.second["skip_location"].as<bool>(); | ||
183 | } | ||
184 | |||
185 | if (door_it.second["item_name"]) { | ||
186 | door_obj.item_name = door_it.second["item_name"].as<std::string>(); | ||
187 | } else if (!door_it.second["skip_item"] && !door_it.second["event"]) { | ||
188 | door_obj.item_name = room_obj.name + " - " + door_obj.name; | ||
189 | } | ||
190 | |||
191 | if (door_it.second["group"]) { | ||
192 | door_obj.group_name = door_it.second["group"].as<std::string>(); | ||
193 | } | ||
194 | |||
195 | if (door_it.second["location_name"]) { | ||
196 | door_obj.location_name = | ||
197 | door_it.second["location_name"].as<std::string>(); | ||
198 | } else if (!door_it.second["skip_location"] && | ||
199 | !door_it.second["event"]) { | ||
200 | if (has_external_panels) { | ||
201 | std::cout | ||
202 | << room_obj.name << " - " << door_obj.name | ||
203 | << " has panels from other rooms but does not have an explicit " | ||
204 | "location name and is not marked skip_location or event" | ||
205 | << std::endl; | ||
206 | } | ||
207 | |||
208 | door_obj.location_name = | ||
209 | room_obj.name + " - " + hatkirby::implode(panel_names, ", "); | ||
210 | } | ||
211 | |||
212 | if (door_it.second["include_reduce"]) { | ||
213 | door_obj.exclude_reduce = | ||
214 | !door_it.second["include_reduce"].as<bool>(); | ||
215 | } | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | } | ||
220 | |||
221 | int GameData::AddOrGetRoom(std::string room) { | ||
222 | if (!room_by_id_.count(room)) { | ||
223 | room_by_id_[room] = rooms_.size(); | ||
224 | rooms_.push_back({.name = room}); | ||
225 | } | ||
226 | |||
227 | return room_by_id_[room]; | ||
228 | } | ||
229 | |||
230 | int GameData::AddOrGetDoor(std::string room, std::string door) { | ||
231 | std::string full_name = room + " - " + door; | ||
232 | |||
233 | if (!door_by_id_.count(full_name)) { | ||
234 | door_by_id_[full_name] = doors_.size(); | ||
235 | doors_.push_back({.name = door, .room = AddOrGetRoom(room)}); | ||
236 | } | ||
237 | |||
238 | return door_by_id_[full_name]; | ||
239 | } | ||
240 | |||
241 | int GameData::AddOrGetPanel(std::string room, std::string panel) { | ||
242 | std::string full_name = room + " - " + panel; | ||
243 | |||
244 | if (!panel_by_id_.count(full_name)) { | ||
245 | panel_by_id_[full_name] = panels_.size(); | ||
246 | panels_.push_back({.name = panel, .room = AddOrGetRoom(room)}); | ||
247 | } | ||
248 | |||
249 | return panel_by_id_[full_name]; | ||
250 | } | ||
251 | |||
252 | const GameData &GetGameData() { | ||
253 | static GameData *instance = new GameData(); | ||
254 | return *instance; | ||
255 | } | ||