Commits

Rhys ! committed 65441e3 Draft

implemented loading items from world file

  • Participants
  • Parent commits 654633f

Comments (0)

Files changed (3)

File Location.hpp

 class Location {
 private:
     std::string list_exits() const {
-        using std::begin; using std::end;
         if (this->exits.empty()) {
             return "\nThere are no exits leading from this location.";
         } else {

File MainMenuStage.hpp

     MainMenuStage(ZorkishGame& game) : ZorkishStage(game) {};
 
     virtual Status update(ActiveStageManager& smgr) {
-        switch (this->input_int()) {
+        switch (ZorkishStage::input_int()) {
         case 1:
             smgr.set_next_stage(this->game.select_adventure().get());
             break;

File SelectAdventureStage.hpp

 #ifndef SELECTADVENTURESTAGE_HPP_
 #define SELECTADVENTURESTAGE_HPP_
 
+#include <stdexcept>
 #include <fstream>
 #include "AdventureStage.hpp"
 
 private:
     std::string message;
 
-    std::unique_ptr<World> test_world() {
+    static std::unique_ptr<World> test_world() {
         auto w = make_unique<World>();
-        auto* sloc = new Location("Void World", "This world is simple and pointless. Used it to test Zorkish phase 1 spec.");
+        auto* sloc = new Location("Void World", "This world is simple and "
+                "pointless. Used it to test Zorkish phase 1 spec.");
         sloc->inv.add(make_unique<Item>("delicious cake", Item::Ids {"cake"}));
         sloc->inv.add(make_unique<Item>("a rock", Item::Ids {"rock"}));
         w->add_location(0, sloc);
         return w;
     }
 
-    std::unique_ptr<World> load() {
+    static Location* load_location(std::string& name, std::ifstream& f) {
+        std::string desc;
+        getline(f, desc, '|');
+        auto* loc = new Location(name, desc);
+        while (f.peek() != '\n' && !f.eof()) { // peek sets eof
+            std::string link;
+            getline(f, link, ';');
+
+            auto seploc = link.find('=');
+            auto to = boost::lexical_cast<int>(
+                    link.substr(seploc + 1, link.length()));
+            loc->exits[link.substr(0, seploc)] = to;
+        }
+        return loc;
+    }
+
+    static void load_item(std::string& name, int location_id, std::ifstream& f,
+            const std::unique_ptr<World>& w) {
+        Item::Ids ids;
+        while (f.peek() != '\n' && !f.eof()) {
+            std::string identifier;
+            getline(f, identifier, ';');
+            ids.push_back(identifier);
+        }
+        // Obviously the following will crash and burn if the location hasn't
+        // been defined yet.
+        auto* location = w->get_location(location_id);
+        location->inv.add(make_unique<Item>(name, ids));
+    }
+
+    static std::unique_ptr<World> load() {
         std::ifstream f;
         auto w = make_unique<World>();
         f.open("world.txt");
         while (!f.eof()) {
+            char type;
             int id;
+            std::string name;
+
+            f >> type;
+            f.ignore(1);
             f >> id;
-            f.ignore(1, '|');
-            std::string name;
+            f.ignore(1);
             getline(f, name, '|');
-            std::string desc;
-            getline(f, desc, '|');
-            auto* loc = new Location(name, desc);
-            while (f.peek() != '\n' && !f.eof()) { // peek sets eof
-                std::string link;
-                getline(f, link, ';');
 
-                auto seploc = link.find('=');
-                auto to = boost::lexical_cast<int>(link.substr(seploc + 1, link.length()));
-                loc->exits[link.substr(0, seploc)] = to;
+            switch (type) {
+            case 'L':
+                w->add_location(id,
+                        SelectAdventureStage::load_location(name, f));
+                break;
+            case 'I':
+                SelectAdventureStage::load_item(name, id, f, w);
+                break;
+            default:
+                throw std::runtime_error("Unknown entity type specified");
             }
-            w->add_location(id, loc);
         }
         return w;
     }
     virtual Status update(ActiveStageManager& smgr) {
         switch (this->input_int()) {
         case 1:
-            return this->setup_adventure(smgr, this->test_world());
+            return this->setup_adventure(smgr,
+                    SelectAdventureStage::test_world());
         case 2:
-            return this->setup_adventure(smgr, this->load());
+            return this->setup_adventure(smgr, SelectAdventureStage::load());
         default:
             this->message = "Please select a number listed above.";
             break;