From: Daniel Karbach Date: Wed, 7 Oct 2015 15:38:13 +0000 (+0200) Subject: move serverside joining from state to server X-Git-Url: http://git.localhorst.tv/?a=commitdiff_plain;h=d91257ba2a51416683be3f54fe16cb2e96ae29f5;hp=c3397eb26a844ded6ddbf92c95cbb87cd3baf65d;p=blank.git move serverside joining from state to server --- diff --git a/src/server/Server.hpp b/src/server/Server.hpp index 372a947..c12b86a 100644 --- a/src/server/Server.hpp +++ b/src/server/Server.hpp @@ -2,6 +2,7 @@ #define BLANK_SERVER_SERVER_HPP #include "../app/Config.hpp" +#include "../world/World.hpp" #include "../world/WorldManipulator.hpp" #include @@ -10,8 +11,9 @@ namespace blank { +class ChunkIndex; class CompositeModel; -class World; +class Player; class WorldSave; namespace server { @@ -22,7 +24,7 @@ class Server : public WorldManipulator { public: - Server(const Config::Network &, World &, const WorldSave &); + Server(const Config::Network &, World &, const World::Config &, const WorldSave &); ~Server(); void Handle(); @@ -39,6 +41,8 @@ public: bool HasPlayerModel() const noexcept; const CompositeModel &GetPlayerModel() const noexcept; + Player *JoinPlayer(const std::string &name); + void SetBlock(Chunk &, int, const Block &) override; private: @@ -52,6 +56,7 @@ private: std::list clients; World &world; + ChunkIndex &spawn_index; const WorldSave &save; const CompositeModel *player_model; diff --git a/src/server/ServerState.cpp b/src/server/ServerState.cpp index e16872f..fc0da82 100644 --- a/src/server/ServerState.cpp +++ b/src/server/ServerState.cpp @@ -2,6 +2,7 @@ #include "../app/Environment.hpp" #include "../app/TextureIndex.hpp" +#include "../io/WorldSave.hpp" #include "../net/io.hpp" #include @@ -20,12 +21,11 @@ ServerState::ServerState( : env(env) , block_types() , world(block_types, wc) -, spawn_index(world.Chunks().MakeIndex(wc.spawn, 3)) , generator(gc) , chunk_loader(world.Chunks(), generator, ws) , skeletons() , spawner(world, skeletons, env.rng) -, server(config.net, world, ws) +, server(config.net, world, wc, ws) , loop_timer(16) { TextureIndex tex_index; env.loader.LoadBlockTypes("default", block_types, tex_index); @@ -40,12 +40,18 @@ ServerState::ServerState( } ServerState::~ServerState() { - world.Chunks().UnregisterIndex(spawn_index); + } void ServerState::Handle(const SDL_Event &event) { if (event.type == SDL_QUIT) { + std::cout << "saving remaining chunks" << std::endl; + for (Chunk &chunk : world.Chunks()) { + if (chunk.ShouldUpdateSave()) { + chunk_loader.SaveFile().Write(chunk); + } + } env.state.PopAll(); } } diff --git a/src/server/ServerState.hpp b/src/server/ServerState.hpp index e22f9b1..3a48514 100644 --- a/src/server/ServerState.hpp +++ b/src/server/ServerState.hpp @@ -7,7 +7,6 @@ #include "../app/State.hpp" #include "../model/Skeletons.hpp" #include "../world/BlockTypeRegistry.hpp" -#include "../world/ChunkIndex.hpp" #include "../world/ChunkLoader.hpp" #include "../world/Generator.hpp" #include "../world/World.hpp" @@ -42,7 +41,6 @@ private: HeadlessEnvironment &env; BlockTypeRegistry block_types; World world; - ChunkIndex &spawn_index; Generator generator; ChunkLoader chunk_loader; Skeletons skeletons; diff --git a/src/server/net.cpp b/src/server/net.cpp index 16aa1cb..1e60ff6 100644 --- a/src/server/net.cpp +++ b/src/server/net.cpp @@ -395,11 +395,6 @@ void ClientConnection::AttachPlayer(Player &player) { DetachPlayer(); input.reset(new DirectInput(server.GetWorld(), player, server)); PlayerEntity().Ref(); - if (server.GetWorldSave().Exists(player)) { - server.GetWorldSave().Read(player); - } else { - // TODO: spawn - } old_base = PlayerChunks().Base(); Chunk::Pos begin = PlayerChunks().CoordsBegin(); @@ -411,6 +406,7 @@ void ClientConnection::AttachPlayer(Player &player) { } } } + // TODO: should the server do this? if (HasPlayerModel()) { GetPlayerModel().Instantiate(PlayerEntity().GetModel()); } @@ -489,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! @@ -567,11 +563,16 @@ bool ClientConnection::ChunkInRange(const glm::ivec3 &pos) const noexcept { } -Server::Server(const Config::Network &conf, World &world, const WorldSave &save) +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); @@ -584,6 +585,7 @@ Server::Server(const Config::Network &conf, World &world, const WorldSave &save) } Server::~Server() { + world.Chunks().UnregisterIndex(spawn_index); delete[] serv_pack.data; SDLNet_UDP_Close(serv_sock); } @@ -655,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