diff options
Diffstat (limited to 'src/game_data.cpp')
| -rw-r--r-- | src/game_data.cpp | 406 |
1 files changed, 406 insertions, 0 deletions
| diff --git a/src/game_data.cpp b/src/game_data.cpp new file mode 100644 index 0000000..e15847e --- /dev/null +++ b/src/game_data.cpp | |||
| @@ -0,0 +1,406 @@ | |||
| 1 | #include "game_data.h" | ||
| 2 | |||
| 3 | #include <hkutil/string.h> | ||
| 4 | #include <yaml-cpp/yaml.h> | ||
| 5 | |||
| 6 | #include <iostream> | ||
| 7 | |||
| 8 | LingoColor GetColorForString(const std::string &str) { | ||
| 9 | if (str == "black") { | ||
| 10 | return LingoColor::kBlack; | ||
| 11 | } else if (str == "red") { | ||
| 12 | return LingoColor::kRed; | ||
| 13 | } else if (str == "blue") { | ||
| 14 | return LingoColor::kBlue; | ||
| 15 | } else if (str == "yellow") { | ||
| 16 | return LingoColor::kYellow; | ||
| 17 | } else if (str == "orange") { | ||
| 18 | return LingoColor::kOrange; | ||
| 19 | } else if (str == "green") { | ||
| 20 | return LingoColor::kGreen; | ||
| 21 | } else if (str == "gray") { | ||
| 22 | return LingoColor::kGray; | ||
| 23 | } else if (str == "brown") { | ||
| 24 | return LingoColor::kBrown; | ||
| 25 | } else if (str == "purple") { | ||
| 26 | return LingoColor::kPurple; | ||
| 27 | } else { | ||
| 28 | std::cout << "Invalid color: " << str << std::endl; | ||
| 29 | return LingoColor::kNone; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | GameData::GameData() { | ||
| 34 | YAML::Node lingo_config = YAML::LoadFile("assets/LL1.yaml"); | ||
| 35 | YAML::Node areas_config = YAML::LoadFile("assets/areas.yaml"); | ||
| 36 | |||
| 37 | rooms_.reserve(lingo_config.size() * 2); | ||
| 38 | |||
| 39 | for (const auto &room_it : lingo_config) { | ||
| 40 | int room_id = AddOrGetRoom(room_it.first.as<std::string>()); | ||
| 41 | Room &room_obj = rooms_[room_id]; | ||
| 42 | |||
| 43 | for (const auto &entrance_it : room_it.second["entrances"]) { | ||
| 44 | int from_room_id = AddOrGetRoom(entrance_it.first.as<std::string>()); | ||
| 45 | Room &from_room_obj = rooms_[from_room_id]; | ||
| 46 | |||
| 47 | switch (entrance_it.second.Type()) { | ||
| 48 | case YAML::NodeType::Scalar: { | ||
| 49 | // This is just "true". | ||
| 50 | from_room_obj.exits.push_back({.destination_room = room_id}); | ||
| 51 | break; | ||
| 52 | } | ||
| 53 | case YAML::NodeType::Map: { | ||
| 54 | Exit exit_obj; | ||
| 55 | exit_obj.destination_room = room_id; | ||
| 56 | |||
| 57 | if (entrance_it.second["door"]) { | ||
| 58 | std::string door_room = room_obj.name; | ||
| 59 | if (entrance_it.second["room"]) { | ||
| 60 | door_room = entrance_it.second["room"].as<std::string>(); | ||
| 61 | } | ||
| 62 | exit_obj.door = AddOrGetDoor( | ||
| 63 | door_room, entrance_it.second["door"].as<std::string>()); | ||
| 64 | } | ||
| 65 | |||
| 66 | if (entrance_it.second["painting"]) { | ||
| 67 | exit_obj.painting = entrance_it.second["painting"].as<bool>(); | ||
| 68 | } | ||
| 69 | |||
| 70 | from_room_obj.exits.push_back(exit_obj); | ||
| 71 | break; | ||
| 72 | } | ||
| 73 | case YAML::NodeType::Sequence: { | ||
| 74 | for (const auto &option : entrance_it.second) { | ||
| 75 | Exit exit_obj; | ||
| 76 | exit_obj.destination_room = room_id; | ||
| 77 | |||
| 78 | std::string door_room = room_obj.name; | ||
| 79 | if (option["room"]) { | ||
| 80 | door_room = option["room"].as<std::string>(); | ||
| 81 | } | ||
| 82 | exit_obj.door = | ||
| 83 | AddOrGetDoor(door_room, option["door"].as<std::string>()); | ||
| 84 | |||
| 85 | if (option["painting"]) { | ||
| 86 | exit_obj.painting = option["painting"].as<bool>(); | ||
| 87 | } | ||
| 88 | |||
| 89 | from_room_obj.exits.push_back(exit_obj); | ||
| 90 | } | ||
| 91 | |||
| 92 | break; | ||
| 93 | } | ||
| 94 | default: { | ||
| 95 | // This shouldn't happen. | ||
| 96 | std::cout << "Error reading game data: " << entrance_it << std::endl; | ||
| 97 | break; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | if (room_it.second["panels"]) { | ||
| 103 | for (const auto &panel_it : room_it.second["panels"]) { | ||
| 104 | int panel_id = | ||
| 105 | AddOrGetPanel(room_obj.name, panel_it.first.as<std::string>()); | ||
| 106 | Panel &panel_obj = panels_[panel_id]; | ||
| 107 | |||
| 108 | if (panel_it.second["colors"]) { | ||
| 109 | if (panel_it.second["colors"].IsScalar()) { | ||
| 110 | panel_obj.colors.push_back( | ||
| 111 | GetColorForString(panel_it.second["colors"].as<std::string>())); | ||
| 112 | } else { | ||
| 113 | for (const auto &color_node : panel_it.second["colors"]) { | ||
| 114 | panel_obj.colors.push_back( | ||
| 115 | GetColorForString(color_node.as<std::string>())); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | if (panel_it.second["required_room"]) { | ||
| 121 | if (panel_it.second["required_room"].IsScalar()) { | ||
| 122 | panel_obj.required_rooms.push_back(AddOrGetRoom( | ||
| 123 | panel_it.second["required_room"].as<std::string>())); | ||
| 124 | } else { | ||
| 125 | for (const auto &rr_node : panel_it.second["required_room"]) { | ||
| 126 | panel_obj.required_rooms.push_back( | ||
| 127 | AddOrGetRoom(rr_node.as<std::string>())); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | if (panel_it.second["required_door"]) { | ||
| 133 | if (panel_it.second["required_door"].IsMap()) { | ||
| 134 | std::string rd_room = room_obj.name; | ||
| 135 | if (panel_it.second["required_door"]["room"]) { | ||
| 136 | rd_room = | ||
| 137 | panel_it.second["required_door"]["room"].as<std::string>(); | ||
| 138 | } | ||
| 139 | |||
| 140 | panel_obj.required_doors.push_back(AddOrGetDoor( | ||
| 141 | rd_room, | ||
| 142 | panel_it.second["required_door"]["door"].as<std::string>())); | ||
| 143 | } else { | ||
| 144 | for (const auto &rr_node : panel_it.second["required_door"]) { | ||
| 145 | std::string rd_room = room_obj.name; | ||
| 146 | if (rr_node["room"]) { | ||
| 147 | rd_room = rr_node["room"].as<std::string>(); | ||
| 148 | } | ||
| 149 | |||
| 150 | panel_obj.required_doors.push_back( | ||
| 151 | AddOrGetDoor(rd_room, rr_node["door"].as<std::string>())); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | if (panel_it.second["check"]) { | ||
| 157 | panel_obj.check = panel_it.second["check"].as<bool>(); | ||
| 158 | } | ||
| 159 | |||
| 160 | if (panel_it.second["achievement"]) { | ||
| 161 | panel_obj.achievement = panel_it.second["achievement"].as<bool>(); | ||
| 162 | |||
| 163 | if (panel_obj.achievement) { | ||
| 164 | achievement_panels_.push_back(panel_id); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | if (panel_it.second["exclude_reduce"]) { | ||
| 169 | panel_obj.exclude_reduce = | ||
| 170 | panel_it.second["exclude_reduce"].as<bool>(); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | if (room_it.second["doors"]) { | ||
| 176 | for (const auto &door_it : room_it.second["doors"]) { | ||
| 177 | int door_id = | ||
| 178 | AddOrGetDoor(room_obj.name, door_it.first.as<std::string>()); | ||
| 179 | Door &door_obj = doors_[door_id]; | ||
| 180 | |||
| 181 | bool has_external_panels = false; | ||
| 182 | std::vector<std::string> panel_names; | ||
| 183 | |||
| 184 | for (const auto &panel_node : door_it.second["panels"]) { | ||
| 185 | if (panel_node.IsScalar()) { | ||
| 186 | panel_names.push_back(panel_node.as<std::string>()); | ||
| 187 | door_obj.panels.push_back( | ||
| 188 | AddOrGetPanel(room_obj.name, panel_node.as<std::string>())); | ||
| 189 | } else { | ||
| 190 | has_external_panels = true; | ||
| 191 | panel_names.push_back(panel_node["panel"].as<std::string>()); | ||
| 192 | door_obj.panels.push_back( | ||
| 193 | AddOrGetPanel(panel_node["room"].as<std::string>(), | ||
| 194 | panel_node["panel"].as<std::string>())); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | if (door_it.second["skip_location"]) { | ||
| 199 | door_obj.skip_location = door_it.second["skip_location"].as<bool>(); | ||
| 200 | } | ||
| 201 | |||
| 202 | if (door_it.second["skip_item"]) { | ||
| 203 | door_obj.skip_item = door_it.second["skip_item"].as<bool>(); | ||
| 204 | } | ||
| 205 | |||
| 206 | if (door_it.second["event"]) { | ||
| 207 | door_obj.skip_location = door_it.second["event"].as<bool>(); | ||
| 208 | door_obj.skip_item = door_it.second["event"].as<bool>(); | ||
| 209 | } | ||
| 210 | |||
| 211 | if (door_it.second["item_name"]) { | ||
| 212 | door_obj.item_name = door_it.second["item_name"].as<std::string>(); | ||
| 213 | } else if (!door_it.second["skip_item"] && !door_it.second["event"]) { | ||
| 214 | door_obj.item_name = room_obj.name + " - " + door_obj.name; | ||
| 215 | } | ||
| 216 | |||
| 217 | if (door_it.second["group"]) { | ||
| 218 | door_obj.group_name = door_it.second["group"].as<std::string>(); | ||
| 219 | } | ||
| 220 | |||
| 221 | if (door_it.second["location_name"]) { | ||
| 222 | door_obj.location_name = | ||
| 223 | door_it.second["location_name"].as<std::string>(); | ||
| 224 | } else if (!door_it.second["skip_location"] && | ||
| 225 | !door_it.second["event"]) { | ||
| 226 | if (has_external_panels) { | ||
| 227 | std::cout | ||
| 228 | << room_obj.name << " - " << door_obj.name | ||
| 229 | << " has panels from other rooms but does not have an explicit " | ||
| 230 | "location name and is not marked skip_location or event" | ||
| 231 | << std::endl; | ||
| 232 | } | ||
| 233 | |||
| 234 | door_obj.location_name = | ||
| 235 | room_obj.name + " - " + hatkirby::implode(panel_names, ", "); | ||
| 236 | } | ||
| 237 | |||
| 238 | if (door_it.second["include_reduce"]) { | ||
| 239 | door_obj.exclude_reduce = | ||
| 240 | !door_it.second["include_reduce"].as<bool>(); | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | if (room_it.second["paintings"]) { | ||
| 246 | for (const auto &painting : room_it.second["paintings"]) { | ||
| 247 | std::string painting_id = painting["id"].as<std::string>(); | ||
| 248 | room_by_painting_[painting_id] = room_id; | ||
| 249 | |||
| 250 | if (!painting["exit_only"] || !painting["exit_only"].as<bool>()) { | ||
| 251 | PaintingExit painting_exit; | ||
| 252 | painting_exit.id = painting_id; | ||
| 253 | |||
| 254 | if (painting["required_door"]) { | ||
| 255 | std::string rd_room = room_obj.name; | ||
| 256 | if (painting["required_door"]["room"]) { | ||
| 257 | rd_room = painting["required_door"]["room"].as<std::string>(); | ||
| 258 | } | ||
| 259 | |||
| 260 | painting_exit.door = AddOrGetDoor( | ||
| 261 | rd_room, painting["required_door"]["door"].as<std::string>()); | ||
| 262 | } | ||
| 263 | |||
| 264 | room_obj.paintings.push_back(painting_exit); | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | if (room_it.second["progression"]) { | ||
| 270 | for (const auto &progression_it : room_it.second["progression"]) { | ||
| 271 | std::string progressive_item_name = | ||
| 272 | progression_it.first.as<std::string>(); | ||
| 273 | |||
| 274 | int index = 1; | ||
| 275 | for (const auto &stage : progression_it.second) { | ||
| 276 | int door_id = -1; | ||
| 277 | |||
| 278 | if (stage.IsScalar()) { | ||
| 279 | door_id = AddOrGetDoor(room_obj.name, stage.as<std::string>()); | ||
| 280 | } else { | ||
| 281 | door_id = AddOrGetDoor(stage["room"].as<std::string>(), | ||
| 282 | stage["door"].as<std::string>()); | ||
| 283 | } | ||
| 284 | |||
| 285 | doors_[door_id].progressives.push_back( | ||
| 286 | {.item_name = progressive_item_name, .quantity = index}); | ||
| 287 | index++; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | map_areas_.reserve(areas_config.size()); | ||
| 294 | |||
| 295 | std::map<std::string, int> fold_areas; | ||
| 296 | for (const auto &area_it : areas_config) { | ||
| 297 | if (area_it.second["map"]) { | ||
| 298 | int area_id = AddOrGetArea(area_it.first.as<std::string>()); | ||
| 299 | MapArea &area_obj = map_areas_[area_id]; | ||
| 300 | area_obj.map_x = area_it.second["map"][0].as<int>(); | ||
| 301 | area_obj.map_y = area_it.second["map"][1].as<int>(); | ||
| 302 | } else if (area_it.second["fold_into"]) { | ||
| 303 | fold_areas[area_it.first.as<std::string>()] = | ||
| 304 | AddOrGetArea(area_it.second["fold_into"].as<std::string>()); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | for (const Panel &panel : panels_) { | ||
| 309 | if (panel.check) { | ||
| 310 | int room_id = panel.room; | ||
| 311 | std::string room_name = rooms_[room_id].name; | ||
| 312 | |||
| 313 | std::string area_name = room_name; | ||
| 314 | if (fold_areas.count(room_name)) { | ||
| 315 | int fold_area_id = fold_areas[room_name]; | ||
| 316 | area_name = map_areas_[fold_area_id].name; | ||
| 317 | } | ||
| 318 | |||
| 319 | int area_id = AddOrGetArea(area_name); | ||
| 320 | MapArea &map_area = map_areas_[area_id]; | ||
| 321 | // room field should be the original room ID | ||
| 322 | map_area.locations.push_back( | ||
| 323 | {.name = panel.name, | ||
| 324 | .ap_location_name = room_name + " - " + panel.name, | ||
| 325 | .room = panel.room, | ||
| 326 | .panels = {panel.id}}); | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | for (const Door &door : doors_) { | ||
| 331 | if (!door.skip_location) { | ||
| 332 | int room_id = door.room; | ||
| 333 | std::string area_name = rooms_[room_id].name; | ||
| 334 | std::string section_name; | ||
| 335 | |||
| 336 | size_t divider_pos = door.location_name.find(" - "); | ||
| 337 | if (divider_pos == std::string::npos) { | ||
| 338 | section_name = door.location_name; | ||
| 339 | } else { | ||
| 340 | area_name = door.location_name.substr(0, divider_pos); | ||
| 341 | section_name = door.location_name.substr(divider_pos + 3); | ||
| 342 | } | ||
| 343 | |||
| 344 | if (fold_areas.count(area_name)) { | ||
| 345 | int fold_area_id = fold_areas[area_name]; | ||
| 346 | area_name = map_areas_[fold_area_id].name; | ||
| 347 | } | ||
| 348 | |||
| 349 | int area_id = AddOrGetArea(area_name); | ||
| 350 | MapArea &map_area = map_areas_[area_id]; | ||
| 351 | // room field should be the original room ID | ||
| 352 | map_area.locations.push_back({.name = section_name, | ||
| 353 | .ap_location_name = door.location_name, | ||
| 354 | .room = door.room, | ||
| 355 | .panels = door.panels}); | ||
| 356 | } | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | int GameData::AddOrGetRoom(std::string room) { | ||
| 361 | if (!room_by_id_.count(room)) { | ||
| 362 | room_by_id_[room] = rooms_.size(); | ||
| 363 | rooms_.push_back({.name = room}); | ||
| 364 | } | ||
| 365 | |||
| 366 | return room_by_id_[room]; | ||
| 367 | } | ||
| 368 | |||
| 369 | int GameData::AddOrGetDoor(std::string room, std::string door) { | ||
| 370 | std::string full_name = room + " - " + door; | ||
| 371 | |||
| 372 | if (!door_by_id_.count(full_name)) { | ||
| 373 | door_by_id_[full_name] = doors_.size(); | ||
| 374 | doors_.push_back({.room = AddOrGetRoom(room), .name = door}); | ||
| 375 | } | ||
| 376 | |||
| 377 | return door_by_id_[full_name]; | ||
| 378 | } | ||
| 379 | |||
| 380 | int GameData::AddOrGetPanel(std::string room, std::string panel) { | ||
| 381 | std::string full_name = room + " - " + panel; | ||
| 382 | |||
| 383 | if (!panel_by_id_.count(full_name)) { | ||
| 384 | int panel_id = panels_.size(); | ||
| 385 | panel_by_id_[full_name] = panel_id; | ||
| 386 | panels_.push_back( | ||
| 387 | {.id = panel_id, .room = AddOrGetRoom(room), .name = panel}); | ||
| 388 | } | ||
| 389 | |||
| 390 | return panel_by_id_[full_name]; | ||
| 391 | } | ||
| 392 | |||
| 393 | int GameData::AddOrGetArea(std::string area) { | ||
| 394 | if (!area_by_id_.count(area)) { | ||
| 395 | int area_id = map_areas_.size(); | ||
| 396 | area_by_id_[area] = area_id; | ||
| 397 | map_areas_.push_back({.id = area_id, .name = area}); | ||
| 398 | } | ||
| 399 | |||
| 400 | return area_by_id_[area]; | ||
| 401 | } | ||
| 402 | |||
| 403 | const GameData &GetGameData() { | ||
| 404 | static GameData *instance = new GameData(); | ||
| 405 | return *instance; | ||
| 406 | } | ||
