]> git.localhorst.tv Git - blank.git/blobdiff - src/app/runtime.cpp
special treatment for players
[blank.git] / src / app / runtime.cpp
index 30b15aeb4b0d105b713255fbba892837481d45a7..f979c653bcf8c496302d366d13a18d15c64e33c5 100644 (file)
@@ -1,14 +1,17 @@
 #include "Application.hpp"
+#include "ClientState.hpp"
 #include "Environment.hpp"
-#include "PreloadState.hpp"
 #include "Runtime.hpp"
+#include "ServerState.hpp"
 #include "WorldState.hpp"
 
 #include "init.hpp"
-#include "../world/WorldSave.hpp"
+#include "../io/filesystem.hpp"
+#include "../io/WorldSave.hpp"
 
 #include <cctype>
 #include <cstdlib>
+#include <fstream>
 #include <iostream>
 #include <SDL.h>
 
@@ -44,19 +47,30 @@ string default_save_path() {
 
 namespace blank {
 
+HeadlessEnvironment::HeadlessEnvironment(const string &asset_path)
+: loader(asset_path)
+, counter()
+, state() {
+
+}
+
 Environment::Environment(Window &win, const string &asset_path)
-: audio()
+: HeadlessEnvironment(asset_path)
+, assets(loader)
+, audio()
 , viewport()
 , window(win)
-, assets(asset_path)
-, counter() {
-
+, keymap() {
+       viewport.Clear();
+       window.Flip();
+       keymap.LoadDefault();
 }
 
 
 Runtime::Runtime() noexcept
 : name("blank")
 , mode(NORMAL)
+, target(STANDALONE)
 , n(0)
 , t(0)
 , config() {
@@ -97,6 +111,12 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                                                config.interface.visual_disabled = true;
                                        } else if (strcmp(param, "no-audio") == 0) {
                                                config.interface.audio_disabled = true;
+                                       } else if (strcmp(param, "standalone") == 0) {
+                                               target = STANDALONE;
+                                       } else if (strcmp(param, "server") == 0) {
+                                               target = SERVER;
+                                       } else if (strcmp(param, "client") == 0) {
+                                               target = CLIENT;
                                        } else if (strcmp(param, "asset-path") == 0) {
                                                ++i;
                                                if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
@@ -105,6 +125,31 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                                                } else {
                                                        config.asset_path = argv[i];
                                                }
+                                       } else if (strcmp(param, "host") == 0) {
+                                               ++i;
+                                               if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                       cerr << "missing argument to --host" << endl;
+                                                       error = true;
+                                               } else {
+                                                       config.client.host = argv[i];
+                                               }
+                                       } else if (strcmp(param, "port") == 0) {
+                                               ++i;
+                                               if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                       cerr << "missing argument to --port" << endl;
+                                                       error = true;
+                                               } else {
+                                                       config.server.port = strtoul(argv[i], nullptr, 10);
+                                                       config.client.port = config.server.port;
+                                               }
+                                       } else if (strcmp(param, "player-name") == 0) {
+                                               ++i;
+                                               if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                       cerr << "missing argument to --player-name" << endl;
+                                                       error = true;
+                                               } else {
+                                                       config.interface.player_name = argv[i];
+                                               }
                                        } else if (strcmp(param, "save-path") == 0) {
                                                ++i;
                                                if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
@@ -227,6 +272,25 @@ int Runtime::Execute() {
                return 1;
        }
 
+       InitHeadless init_headless;
+
+       switch (target) {
+               default:
+               case STANDALONE:
+                       RunStandalone();
+                       break;
+               case SERVER:
+                       RunServer();
+                       break;
+               case CLIENT:
+                       RunClient();
+                       break;
+       }
+
+       return 0;
+}
+
+void Runtime::RunStandalone() {
        Init init(config.doublebuf, config.multisampling);
 
        Environment env(init.window, config.asset_path);
@@ -236,17 +300,69 @@ int Runtime::Execute() {
        if (save.Exists()) {
                save.Read(config.world);
        } else {
-               save.Create(config.world);
+               save.Write(config.world);
        }
 
-       Application app(env);
+       std::string keys_path = config.save_path + "keys.conf";
+       if (!is_file(keys_path)) {
+               std::ofstream file(keys_path);
+               env.keymap.Save(file);
+       } else {
+               std::ifstream file(keys_path);
+               env.keymap.Load(file);
+       }
 
-       WorldState world_state(env, config.interface, config.world);
+       Application app(env);
+       WorldState world_state(env, config.interface, config.world, save);
        app.PushState(&world_state);
+       Run(app);
+}
+
+void Runtime::RunServer() {
+       HeadlessEnvironment env(config.asset_path);
+
+       WorldSave save(config.save_path + config.world_name + '/');
+       if (save.Exists()) {
+               save.Read(config.world);
+       } else {
+               save.Write(config.world);
+       }
+
+       HeadlessApplication app(env);
+       ServerState server_state(env, config.world, save, config.server);
+       app.PushState(&server_state);
+       Run(app);
+}
+
+void Runtime::RunClient() {
+       Init init(config.doublebuf, config.multisampling);
+
+       Environment env(init.window, config.asset_path);
+       env.viewport.VSync(config.vsync);
 
-       PreloadState preloader(env, world_state.GetWorld().Loader());
-       app.PushState(&preloader);
+       WorldSave save(config.save_path + config.world_name + '/');
+       if (save.Exists()) {
+               save.Read(config.world);
+       } else {
+               save.Write(config.world);
+       }
 
+       std::string keys_path = config.save_path + "keys.conf";
+       if (!is_file(keys_path)) {
+               std::ofstream file(keys_path);
+               env.keymap.Save(file);
+       } else {
+               std::ifstream file(keys_path);
+               env.keymap.Load(file);
+       }
+
+       Application app(env);
+       ClientState client_state(env, config.world, save, config.client);
+       app.PushState(&client_state);
+       Run(app);
+}
+
+void Runtime::Run(HeadlessApplication &app) {
        switch (mode) {
                default:
                case NORMAL:
@@ -262,8 +378,6 @@ int Runtime::Execute() {
                        app.RunS(n, t);
                        break;
        }
-
-       return 0;
 }
 
 }