#include "Server.hpp"
#include "../app/init.hpp"
-#include "../model/CompositeModel.hpp"
+#include "../io/WorldSave.hpp"
+#include "../model/Model.hpp"
#include "../world/ChunkIndex.hpp"
#include "../world/Entity.hpp"
#include "../world/World.hpp"
+#include <algorithm>
#include <iostream>
#include <zlib.h>
#include <glm/gtx/io.hpp>
}
}
+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();
}
}
old_base = PlayerChunks().Base();
+ sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base));
}
if (transmitter.Transmitting()) {
transmitter.Transmit();
}
}
}
+ sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base));
+ // TODO: should the server do this?
if (HasPlayerModel()) {
GetPlayerModel().Instantiate(PlayerEntity().GetModel());
}
void ClientConnection::DetachPlayer() {
if (!HasPlayer()) return;
cout << "player \"" << input->GetPlayer().Name() << "\" left" << endl;
+ server.GetWorldSave().Write(input->GetPlayer());
PlayerEntity().Kill();
PlayerEntity().UnRef();
input.reset();
old_actions = 0;
}
-void ClientConnection::SetPlayerModel(const CompositeModel &m) noexcept {
+void ClientConnection::SetPlayerModel(const Model &m) noexcept {
player_model = &m;
if (HasPlayer()) {
m.Instantiate(PlayerEntity().GetModel());
return player_model;
}
-const CompositeModel &ClientConnection::GetPlayerModel() const noexcept {
+const Model &ClientConnection::GetPlayerModel() const noexcept {
return *player_model;
}
string name;
pack.ReadPlayerName(name);
- Player *new_player = server.GetWorld().AddPlayer(name);
+ Player *new_player = server.JoinPlayer(name);
if (new_player) {
// success!
}
-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) {
}
Server::~Server() {
+ world.Chunks().UnregisterIndex(spawn_index);
delete[] serv_pack.data;
SDLNet_UDP_Close(serv_sock);
}
}
}
-void Server::SetPlayerModel(const CompositeModel &m) noexcept {
+void Server::SetPlayerModel(const Model &m) noexcept {
player_model = &m;
for (ClientConnection &client : clients) {
client.SetPlayerModel(m);
return player_model;
}
-const CompositeModel &Server::GetPlayerModel() const noexcept {
+const Model &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