]> git.localhorst.tv Git - blank.git/blobdiff - src/server/net.cpp
rearrange init of standalone state a little
[blank.git] / src / server / net.cpp
index 8b4448e46d6b363051d755f8fe6f05c2d2c871ca..1e60ff6ead06d3dfd7905946866d4d4920ead0fd 100644 (file)
@@ -3,6 +3,7 @@
 #include "Server.hpp"
 
 #include "../app/init.hpp"
+#include "../io/WorldSave.hpp"
 #include "../model/CompositeModel.hpp"
 #include "../world/ChunkIndex.hpp"
 #include "../world/Entity.hpp"
@@ -405,6 +406,7 @@ void ClientConnection::AttachPlayer(Player &player) {
                        }
                }
        }
+       // TODO: should the server do this?
        if (HasPlayerModel()) {
                GetPlayerModel().Instantiate(PlayerEntity().GetModel());
        }
@@ -415,6 +417,7 @@ void ClientConnection::AttachPlayer(Player &player) {
 void ClientConnection::DetachPlayer() {
        if (!HasPlayer()) return;
        cout << "player \"" << input->GetPlayer().Name() << "\" left" << endl;
+       server.GetWorldSave().Write(input->GetPlayer());
        PlayerEntity().Kill();
        PlayerEntity().UnRef();
        input.reset();
@@ -482,7 +485,7 @@ void ClientConnection::On(const Packet::Login &pack) {
        string name;
        pack.ReadPlayerName(name);
 
-       Player *new_player = server.GetWorld().AddPlayer(name);
+       Player *new_player = server.JoinPlayer(name);
 
        if (new_player) {
                // success!
@@ -560,11 +563,17 @@ bool ClientConnection::ChunkInRange(const glm::ivec3 &pos) const noexcept {
 }
 
 
-Server::Server(const Config::Network &conf, World &world)
+Server::Server(
+       const Config::Network &conf,
+       World &world,
+       const World::Config &wc,
+       const WorldSave &save)
 : serv_sock(nullptr)
 , serv_pack{ -1, nullptr, 0 }
 , clients()
 , world(world)
+, spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
+, save(save)
 , player_model(nullptr) {
        serv_sock = SDLNet_UDP_Open(conf.port);
        if (!serv_sock) {
@@ -576,6 +585,7 @@ Server::Server(const Config::Network &conf, World &world)
 }
 
 Server::~Server() {
+       world.Chunks().UnregisterIndex(spawn_index);
        delete[] serv_pack.data;
        SDLNet_UDP_Close(serv_sock);
 }
@@ -647,6 +657,22 @@ const CompositeModel &Server::GetPlayerModel() const noexcept {
        return *player_model;
 }
 
+Player *Server::JoinPlayer(const string &name) {
+       if (spawn_index.MissingChunks() > 0) {
+               return nullptr;
+       }
+       Player *player = world.AddPlayer(name);
+       if (!player) {
+               return nullptr;
+       }
+       if (save.Exists(*player)) {
+               save.Read(*player);
+       } else {
+               // TODO: spawn
+       }
+       return player;
+}
+
 void Server::SetBlock(Chunk &chunk, int index, const Block &block) {
        chunk.SetBlock(index, block);
        // TODO: batch chunk changes