#include "Server.hpp"
#include "../app/init.hpp"
+#include "../model/CompositeModel.hpp"
#include "../world/ChunkIndex.hpp"
#include "../world/Entity.hpp"
#include "../world/World.hpp"
ClientConnection::ClientConnection(Server &server, const IPaddress &addr)
: server(server)
, conn(addr)
-, player(nullptr, nullptr)
+, player(nullptr)
+, player_model(nullptr)
, spawns()
, confirm_wait(0)
+, entity_updates()
, player_update_state()
, player_update_pack(0)
, player_update_timer(1500)
SendDespawn(*local_iter);
} else {
// update
- SendUpdate(*local_iter);
+ QueueUpdate(*local_iter);
}
++global_iter;
++local_iter;
SendDespawn(*local_iter);
++local_iter;
}
+ SendUpdates();
CheckPlayerFix();
CheckChunkQueue();
bool ClientConnection::CanSpawn(const Entity &e) const noexcept {
return
- &e != player.entity &&
+ &e != &PlayerEntity() &&
!e.Dead() &&
manhattan_radius(e.ChunkCoords() - PlayerEntity().ChunkCoords()) < 7;
}
++confirm_wait;
}
-void ClientConnection::SendUpdate(SpawnStatus &status) {
+void ClientConnection::QueueUpdate(SpawnStatus &status) {
// don't send updates while spawn not ack'd or despawn sent
- if (status.spawn_pack != -1 || status.despawn_pack != -1) return;
+ if (status.spawn_pack == -1 && status.despawn_pack == -1) {
+ entity_updates.push_back(&status);
+ }
+}
- // TODO: pack entity updates
+void ClientConnection::SendUpdates() {
auto pack = Prepare<Packet::EntityUpdate>();
- pack.WriteEntityCount(1);
- pack.WriteEntity(*status.entity, 0);
- Send(Packet::EntityUpdate::GetSize(1));
+ int entity_pos = 0;
+ for (SpawnStatus *status : entity_updates) {
+ pack.WriteEntity(*status->entity, entity_pos);
+ ++entity_pos;
+ if (entity_pos == Packet::EntityUpdate::MAX_ENTITIES) {
+ pack.WriteEntityCount(entity_pos);
+ Send(Packet::EntityUpdate::GetSize(entity_pos));
+ pack = Prepare<Packet::EntityUpdate>();
+ entity_pos = 0;
+ }
+ }
+ if (entity_pos > 0) {
+ pack.WriteEntityCount(entity_pos);
+ Send(Packet::EntityUpdate::GetSize(entity_pos));
+ }
+ entity_updates.clear();
}
void ClientConnection::CheckPlayerFix() {
}
}
-void ClientConnection::AttachPlayer(const Player &new_player) {
+void ClientConnection::AttachPlayer(Player &new_player) {
DetachPlayer();
- player = new_player;
- player.entity->Ref();
+ player = &new_player;
+ PlayerEntity().Ref();
- old_base = player.chunks->Base();
- Chunk::Pos begin = player.chunks->CoordsBegin();
- Chunk::Pos end = player.chunks->CoordsEnd();
+ old_base = PlayerChunks().Base();
+ Chunk::Pos begin = PlayerChunks().CoordsBegin();
+ Chunk::Pos end = PlayerChunks().CoordsEnd();
for (Chunk::Pos pos = begin; pos.z < end.z; ++pos.z) {
for (pos.y = begin.y; pos.y < end.y; ++pos.y) {
for (pos.x = begin.x; pos.x < end.x; ++pos.x) {
}
}
}
+ if (HasPlayerModel()) {
+ GetPlayerModel().Instantiate(PlayerEntity().GetModel());
+ }
- cout << "player \"" << player.entity->Name() << "\" joined" << endl;
+ cout << "player \"" << player->Name() << "\" joined" << endl;
}
void ClientConnection::DetachPlayer() {
if (!HasPlayer()) return;
- cout << "player \"" << player.entity->Name() << "\" left" << endl;
- player.entity->Kill();
- player.entity->UnRef();
- player.entity = nullptr;
- player.chunks = nullptr;
+ cout << "player \"" << player->Name() << "\" left" << endl;
+ player->GetEntity().Kill();
+ player->GetEntity().UnRef();
+ player = nullptr;
transmitter.Abort();
chunk_queue.clear();
}
+void ClientConnection::SetPlayerModel(const CompositeModel &m) noexcept {
+ player_model = &m;
+ if (HasPlayer()) {
+ m.Instantiate(PlayerEntity().GetModel());
+ }
+}
+
+bool ClientConnection::HasPlayerModel() const noexcept {
+ return player_model;
+}
+
+const CompositeModel &ClientConnection::GetPlayerModel() const noexcept {
+ return *player_model;
+}
+
void ClientConnection::OnPacketReceived(uint16_t seq) {
if (transmitter.Waiting()) {
transmitter.Ack(seq);
string name;
pack.ReadPlayerName(name);
- Player new_player = server.GetWorld().AddPlayer(name);
+ Player *new_player = server.GetWorld().AddPlayer(name);
- if (new_player.entity) {
+ if (new_player) {
// success!
- AttachPlayer(new_player);
+ AttachPlayer(*new_player);
cout << "accepted login from player \"" << name << '"' << endl;
auto response = Prepare<Packet::Join>();
- response.WritePlayer(*new_player.entity);
+ response.WritePlayer(new_player->GetEntity());
response.WriteWorldName(server.GetWorld().Name());
Send();
// set up update tracking
- player_update_state = new_player.entity->GetState();
+ player_update_state = new_player->GetEntity().GetState();
player_update_pack = pack.Seq();
player_update_timer.Reset();
player_update_timer.Start();
}
-Server::Server(const Config &conf, World &world)
+Server::Server(const Config::Network &conf, World &world)
: serv_sock(nullptr)
, serv_pack{ -1, nullptr, 0 }
, clients()
-, world(world) {
+, world(world)
+, player_model(nullptr) {
serv_sock = SDLNet_UDP_Open(conf.port);
if (!serv_sock) {
throw NetError("SDLNet_UDP_Open");
}
}
clients.emplace_back(*this, addr);
+ if (HasPlayerModel()) {
+ clients.back().SetPlayerModel(GetPlayerModel());
+ }
return clients.back();
}
}
}
+void Server::SetPlayerModel(const CompositeModel &m) noexcept {
+ player_model = &m;
+ for (ClientConnection &client : clients) {
+ client.SetPlayerModel(m);
+ }
+}
+
+bool Server::HasPlayerModel() const noexcept {
+ return player_model;
+}
+
+const CompositeModel &Server::GetPlayerModel() const noexcept {
+ return *player_model;
+}
+
}
}