]> git.localhorst.tv Git - blank.git/commitdiff
save and load world seed
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 9 Aug 2015 18:16:16 +0000 (20:16 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 9 Aug 2015 18:18:40 +0000 (20:18 +0200)
first four bytes of persistence, yay ^^

Makefile
running
src/app/Runtime.hpp
src/app/runtime.cpp
src/world/Generator.cpp
src/world/Generator.hpp
src/world/WorldSave.cpp [new file with mode: 0644]
src/world/WorldSave.hpp [new file with mode: 0644]

index 5989839db7a74d8935dd755e3c0c94a4b9dc2668..9b261db863a2bb9c7b62e4d16cb171dc220c77d2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -85,6 +85,7 @@ clean:
 
 distclean: clean
        rm -f $(BIN) cachegrind.out.* callgrind.out.*
+       rm -Rf build saves
 
 .PHONY: all release debug profile tests run gdb cachegrind callgrind test clean distclean
 
diff --git a/running b/running
index d3a632e35501d20a600ba08135577a025c1813c2..26a0f9e8112809c27ccfbff423790ba7f2054203 100644 (file)
--- a/running
+++ b/running
@@ -56,8 +56,14 @@ World
 
 -s <seed>
        use <seed> (unsigned integer) as the world seed
+       only used for newly created worlds
        default is 0
 
+--world-name <name>
+       use given name for the world save
+       no checks are being done right now, so make sure it can be
+       used as a directory name
+
 
 Controls
 ========
index ac508b90677bf8fbe5d19e44c7111bc2c55456a5..81c015c3668456360b1c84dc77587ba026848b1a 100644 (file)
@@ -34,6 +34,7 @@ public:
 
                std::string asset_path;
                std::string save_path;
+               std::string world_name = "default";
 
                Interface::Config interface = Interface::Config();
                World::Config world = World::Config();
index 5dce72159a439203843581f84cb14fc86fdda08a..30b15aeb4b0d105b713255fbba892837481d45a7 100644 (file)
@@ -5,6 +5,7 @@
 #include "WorldState.hpp"
 
 #include "init.hpp"
+#include "../world/WorldSave.hpp"
 
 #include <cctype>
 #include <cstdlib>
@@ -112,6 +113,14 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                                                } else {
                                                        config.save_path = argv[i];
                                                }
+                                       } else if (strcmp(param, "world-name") == 0) {
+                                               ++i;
+                                               if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                       cerr << "missing argument to --world-name" << endl;
+                                                       error = true;
+                                               } else {
+                                                       config.world_name = argv[i];
+                                               }
                                        } else {
                                                cerr << "unknown option " << arg << endl;
                                                error = true;
@@ -148,8 +157,7 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                                                                cerr << "missing argument to -s" << endl;
                                                                error = true;
                                                        } else {
-                                                               config.world.gen.solid_seed = strtoul(argv[i], nullptr, 10);
-                                                               config.world.gen.type_seed = config.world.gen.solid_seed;
+                                                               config.world.gen.seed = strtoul(argv[i], nullptr, 10);
                                                        }
                                                        break;
                                                case 't':
@@ -224,6 +232,13 @@ int Runtime::Execute() {
        Environment env(init.window, config.asset_path);
        env.viewport.VSync(config.vsync);
 
+       WorldSave save(config.save_path + config.world_name + '/');
+       if (save.Exists()) {
+               save.Read(config.world);
+       } else {
+               save.Create(config.world);
+       }
+
        Application app(env);
 
        WorldState world_state(env, config.interface, config.world);
index c49dce17bf153d15bdc7dd12360254b2d86d137a..3962252fa71837cf5152990a820dd03732f33ef1 100644 (file)
@@ -9,8 +9,8 @@
 namespace blank {
 
 Generator::Generator(const Config &config) noexcept
-: solidNoise(config.solid_seed)
-, typeNoise(config.type_seed)
+: solidNoise(config.seed)
+, typeNoise(config.seed)
 , stretch(1.0f/config.stretch)
 , solid_threshold(config.solid_threshold)
 , space(0)
index a2ae18a3e99fa56450f57c88abd8e4deea8d2f33..b3cb61f891e5bef8f8535f5a4a52980747f9145f 100644 (file)
@@ -16,8 +16,7 @@ class Generator {
 
 public:
        struct Config {
-               unsigned int solid_seed = 0;
-               unsigned int type_seed = 0;
+               unsigned int seed = 0;
                float stretch = 64.0f;
                float solid_threshold = 0.5f;
        };
diff --git a/src/world/WorldSave.cpp b/src/world/WorldSave.cpp
new file mode 100644 (file)
index 0000000..2bfa818
--- /dev/null
@@ -0,0 +1,84 @@
+#include "WorldSave.hpp"
+
+#include "../app/io.hpp"
+
+#include <cctype>
+#include <fstream>
+#include <iostream>
+#include <stdexcept>
+
+using namespace std;
+
+
+namespace blank {
+
+WorldSave::WorldSave(const string &path)
+: root_path(path)
+, conf_path(path + "world.conf") {
+
+}
+
+
+bool WorldSave::Exists() const noexcept {
+       return is_dir(root_path) && is_file(conf_path);
+}
+
+
+void WorldSave::Create(const World::Config &conf) const {
+       cout << "creating world save" << endl;
+
+       if (!make_dirs(root_path)) {
+               throw runtime_error("failed to create world save directory");
+       }
+
+       ofstream out(conf_path);
+       out << "seed = " << conf.gen.seed << endl;
+       out.close();
+
+       if (!out) {
+               throw runtime_error("failed to write world config");
+       }
+}
+
+void WorldSave::Read(World::Config &conf) const {
+       cout << "reading world save" << endl;
+
+       ifstream in(conf_path);
+       if (!in) {
+               throw runtime_error("failed to open world config");
+       }
+
+       constexpr char spaces[] = "\n\r\t ";
+
+       string line;
+       while (getline(in, line)) {
+               if (line.empty() || line[0] == '#') continue;
+               auto equals_pos = line.find_first_of('=');
+
+               auto name_begin = line.find_first_not_of(spaces, 0, sizeof(spaces));
+               auto name_end = equals_pos - 1;
+               while (name_end > name_begin && isspace(line[name_end])) {
+                       --name_end;
+               }
+
+               auto value_begin = line.find_first_not_of(spaces, equals_pos + 1, sizeof(spaces));
+               auto value_end = line.length() - 1;
+               while (value_end > value_begin && isspace(line[value_end])) {
+                       --value_end;
+               }
+
+               string name(line, name_begin, name_end - name_begin + 1);
+               string value(line, value_begin, value_end - value_begin + 1);
+
+               if (name == "seed") {
+                       conf.gen.seed = stoul(value);
+               } else {
+                       throw runtime_error("unknown world option: " + name);
+               }
+       }
+       if (in.bad()) {
+               throw runtime_error("IO error reading world config");
+       }
+}
+
+}
diff --git a/src/world/WorldSave.hpp b/src/world/WorldSave.hpp
new file mode 100644 (file)
index 0000000..7e40fc8
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef BLANK_WORLD_WORLDSAVE_HPP_
+#define BLANK_WORLD_WORLDSAVE_HPP_
+
+#include "World.hpp"
+
+#include <string>
+
+
+namespace blank {
+
+class WorldSave {
+
+public:
+       explicit WorldSave(const std::string &path);
+
+public:
+       bool Exists() const noexcept;
+
+       void Create(const World::Config &) const;
+       void Read(World::Config &) const;
+
+private:
+       std::string root_path;
+       std::string conf_path;
+
+};
+
+}
+
+#endif