diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-29 20:01:27 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-29 20:01:27 +0100 |
commit | 9283e1eb66d6ff96b02f317e28cb6ff060953cdf (patch) | |
tree | c03d853ef620216f1c2299936004f56c6c3cee04 /frontend/load_dungeon.cpp | |
parent | 7285f9f2c2622acff734e31314f92df9b25cae16 (diff) |
WIP load XML
Diffstat (limited to 'frontend/load_dungeon.cpp')
-rw-r--r-- | frontend/load_dungeon.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/frontend/load_dungeon.cpp b/frontend/load_dungeon.cpp new file mode 100644 index 0000000..ef8cb54 --- /dev/null +++ b/frontend/load_dungeon.cpp @@ -0,0 +1,78 @@ +#include <memory> +#include <filesystem> +#include <pugixml.hpp> +#include <map> + +#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<Dungeon> load_dungeon(const string & filename) { + unique_ptr<Dungeon> dungeon = make_unique<Dungeon>(); + + 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 <locaties> tag"); + + LocationFactory factory; + struct TempData { + Location * location; + unsigned edges[4]; + }; + map<unsigned, TempData> temp_map; + for (xml_node & tag : locations) { + const char * name = tag.text().as_string(); + const char * description = tag.child("beschrijving").text().as_string(); + + // vector<string> objects_hidden = split_string(tag.attribute("objectenverborgen").as_string(), ";"); + // vector<string> objects_visible = split_string(tag.attribute("objectenzichtbaar").as_string(), ";"); + // vector<string> 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 : { NORTH, EAST, SOUTH, WEST }) { + 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; +} + |