#include #include #include #include #include "backend/Location.h" #include "backend/LocationFactory.h" #include "backend/Dungeon.h" #include "load_dungeon.h" #include "Exception.h" #include "frontend/strings.h" using namespace std; using namespace pugi; unique_ptr load_dungeon(const string & filename) { unique_ptr dungeon = make_unique(); xml_document doc; string canonical = filename; if (canonical.starts_with("~/")) canonical = getenv("HOME") + canonical.substr(1); try { canonical = filesystem::canonical(canonical); } catch (...) { throw Exception("Kon bestand niet vinden"); } xml_parse_result result = doc.load_file(canonical.c_str()); if (!result) throw Exception("Kon XML-bestand niet lezen"); xml_node locations = doc.child("locaties"); if (!locations) throw Exception("XML-bestand mist een tag"); LocationFactory factory; struct TempData { Location * location; unsigned edges[4]; }; map temp_map; for (xml_node & tag : locations) { const char * name = tag.text().as_string(); const char * description = tag.child("beschrijving").text().as_string(); // vector objects_hidden = split_string(tag.attribute("objectenverborgen").as_string(), ";"); // vector objects_visible = split_string(tag.attribute("objectenzichtbaar").as_string(), ";"); // vector enemies = split_string(tag.attribute("vijand").as_string(), ";"); Location * location = factory.create_location(name, description); temp_map[tag.attribute("id").as_uint()] = { .location = location, .edges = { tag.attribute("noord").as_uint(0), tag.attribute("oost").as_uint(0), tag.attribute("zuid").as_uint(0), tag.attribute("west").as_uint(0), }, }; dungeon->add_location(location); } // connect edges after creating all locations for (auto & [here, temp] : temp_map) { for (Direction direction : DIRECTIONS) { unsigned there = temp.edges[direction]; if (temp.edges[direction] == 0) continue; if (!temp_map.contains(there)) continue; temp.location->set_exit(direction, temp_map[there].location); } } return dungeon; }