X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fserver%2Fnet.cpp;h=27643ee8fc34abb4d9b18c0b2b47f0837df7f491;hb=d5ef462f3bd70a9717c1309466ce8d90b038c5cd;hp=8b4448e46d6b363051d755f8fe6f05c2d2c871ca;hpb=933ca0fe6c660e482edd45742d981f2de59a32df;p=blank.git diff --git a/src/server/net.cpp b/src/server/net.cpp index 8b4448e..27643ee 100644 --- a/src/server/net.cpp +++ b/src/server/net.cpp @@ -3,11 +3,13 @@ #include "Server.hpp" #include "../app/init.hpp" +#include "../io/WorldSave.hpp" #include "../model/CompositeModel.hpp" #include "../world/ChunkIndex.hpp" #include "../world/Entity.hpp" #include "../world/World.hpp" +#include #include #include #include @@ -351,6 +353,23 @@ void ClientConnection::CheckPlayerFix() { } } +namespace { + +struct QueueCompare { + explicit QueueCompare(const glm::ivec3 &base) + : base(base) { } + bool operator ()(const glm::ivec3 &left, const glm::ivec3 &right) const noexcept { + const glm::ivec3 ld(left - base); + const glm::ivec3 rd(right - base); + return + ld.x * ld.x + ld.y * ld.y + ld.z * ld.z < + rd.x * rd.x + rd.y * rd.y + rd.z * rd.z; + } + const glm::ivec3 &base; +}; + +} + void ClientConnection::CheckChunkQueue() { if (PlayerChunks().Base() != old_base) { Chunk::Pos begin = PlayerChunks().CoordsBegin(); @@ -365,6 +384,7 @@ void ClientConnection::CheckChunkQueue() { } } old_base = PlayerChunks().Base(); + sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base)); } if (transmitter.Transmitting()) { transmitter.Transmit(); @@ -405,6 +425,8 @@ void ClientConnection::AttachPlayer(Player &player) { } } } + sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base)); + // TODO: should the server do this? if (HasPlayerModel()) { GetPlayerModel().Instantiate(PlayerEntity().GetModel()); } @@ -415,6 +437,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 +505,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 +583,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 +605,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 +677,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