X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fserver%2Fnet.cpp;h=7cb07cdf02b9ae1e6f026493f20e0dd5aed65707;hb=a50aa0f2a2fea14f5f8c56209e2ecde3088ef913;hp=0cf0cdd60cc12ad6b9ab36d1a50745cf85c0999f;hpb=8ae45b6555d55f301f83daf8c1337d332d8305ab;p=blank.git diff --git a/src/server/net.cpp b/src/server/net.cpp index 0cf0cdd..7cb07cd 100644 --- a/src/server/net.cpp +++ b/src/server/net.cpp @@ -3,12 +3,15 @@ #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 using namespace std; @@ -171,12 +174,15 @@ void ChunkTransmitter::Release() { ClientConnection::ClientConnection(Server &server, const IPaddress &addr) : server(server) , conn(addr) -, player(nullptr, nullptr) +, input() +, player_model(nullptr) , spawns() , confirm_wait(0) +, entity_updates() , player_update_state() , player_update_pack(0) , player_update_timer(1500) +, old_actions(0) , transmitter(*this) , chunk_queue() , old_base() { @@ -206,7 +212,7 @@ void ClientConnection::Update(int dt) { SendDespawn(*local_iter); } else { // update - SendUpdate(*local_iter); + QueueUpdate(*local_iter); } ++global_iter; ++local_iter; @@ -238,7 +244,9 @@ void ClientConnection::Update(int dt) { SendDespawn(*local_iter); ++local_iter; } + SendUpdates(); + input->Update(dt); CheckPlayerFix(); CheckChunkQueue(); } @@ -260,7 +268,7 @@ ClientConnection::SpawnStatus::~SpawnStatus() { bool ClientConnection::CanSpawn(const Entity &e) const noexcept { return - &e != player.entity && + &e != &PlayerEntity() && !e.Dead() && manhattan_radius(e.ChunkCoords() - PlayerEntity().ChunkCoords()) < 7; } @@ -276,7 +284,7 @@ uint16_t ClientConnection::Send() { } uint16_t ClientConnection::Send(size_t len) { - server.GetPacket().len = len; + server.GetPacket().len = sizeof(Packet::Header) + len; return Send(); } @@ -300,15 +308,31 @@ void ClientConnection::SendDespawn(SpawnStatus &status) { ++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(); - 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(); + entity_pos = 0; + } + } + if (entity_pos > 0) { + pack.WriteEntityCount(entity_pos); + Send(Packet::EntityUpdate::GetSize(entity_pos)); + } + entity_updates.clear(); } void ClientConnection::CheckPlayerFix() { @@ -367,14 +391,17 @@ void ClientConnection::CheckChunkQueue() { } } -void ClientConnection::AttachPlayer(const Player &new_player) { +void ClientConnection::AttachPlayer(Player &player) { DetachPlayer(); - player = new_player; - player.entity->Ref(); + input.reset(new DirectInput(server.GetWorld(), player, server)); + PlayerEntity().Ref(); + if (server.GetWorldSave().Exists(player)) { + server.GetWorldSave().Read(player); + } - 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) { @@ -382,19 +409,38 @@ void ClientConnection::AttachPlayer(const Player &new_player) { } } } + 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 \"" << input->GetPlayer().Name() << "\" left" << endl; + server.GetWorldSave().Write(input->GetPlayer()); + PlayerEntity().Kill(); + PlayerEntity().UnRef(); + input.reset(); transmitter.Abort(); chunk_queue.clear(); + old_actions = 0; +} + +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) { @@ -441,18 +487,18 @@ void ClientConnection::On(const Packet::Login &pack) { 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(); - 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(); @@ -474,21 +520,58 @@ void ClientConnection::On(const Packet::PlayerUpdate &pack) { int pack_diff = int16_t(pack.Seq()) - int16_t(player_update_pack); bool overdue = player_update_timer.HitOnce(); player_update_timer.Reset(); - if (pack_diff > 0 || overdue) { - player_update_pack = pack.Seq(); - pack.ReadPlayerState(player_update_state); - // accept velocity and orientation as "user input" - PlayerEntity().Velocity(player_update_state.velocity); - PlayerEntity().Orientation(player_update_state.orient); + if (pack_diff <= 0 && !overdue) { + // drop old packets if we have a fairly recent state + return; } + glm::vec3 movement(0.0f); + float pitch = 0.0f; + float yaw = 0.0f; + uint8_t new_actions; + uint8_t slot; + + player_update_pack = pack.Seq(); + pack.ReadPredictedState(player_update_state); + pack.ReadMovement(movement); + pack.ReadPitch(pitch); + pack.ReadYaw(yaw); + pack.ReadActions(new_actions); + pack.ReadSlot(slot); + + input->SetMovement(movement); + input->TurnHead(pitch - input->GetPitch(), yaw - input->GetYaw()); + input->SelectInventory(slot); + + if ((new_actions & 0x01) && !(old_actions & 0x01)) { + input->StartPrimaryAction(); + } else if (!(new_actions & 0x01) && (old_actions & 0x01)) { + input->StopPrimaryAction(); + } + if ((new_actions & 0x02) && !(old_actions & 0x02)) { + input->StartSecondaryAction(); + } else if (!(new_actions & 0x02) && (old_actions & 0x02)) { + input->StopSecondaryAction(); + } + if ((new_actions & 0x04) && !(old_actions & 0x04)) { + input->StartTertiaryAction(); + } else if (!(new_actions & 0x04) && (old_actions & 0x04)) { + input->StopTertiaryAction(); + } + old_actions = new_actions; +} + +bool ClientConnection::ChunkInRange(const glm::ivec3 &pos) const noexcept { + return HasPlayer() && PlayerChunks().InRange(pos); } -Server::Server(const Config &conf, World &world) +Server::Server(const Config::Network &conf, World &world, const WorldSave &save) : serv_sock(nullptr) , serv_pack{ -1, nullptr, 0 } , clients() -, world(world) { +, world(world) +, save(save) +, player_model(nullptr) { serv_sock = SDLNet_UDP_Open(conf.port); if (!serv_sock) { throw NetError("SDLNet_UDP_Open"); @@ -538,6 +621,9 @@ ClientConnection &Server::GetClient(const IPaddress &addr) { } } clients.emplace_back(*this, addr); + if (HasPlayerModel()) { + clients.back().SetPlayerModel(GetPlayerModel()); + } return clients.back(); } @@ -552,5 +638,36 @@ void Server::Update(int dt) { } } +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; +} + +void Server::SetBlock(Chunk &chunk, int index, const Block &block) { + chunk.SetBlock(index, block); + // TODO: batch chunk changes + auto pack = Packet::Make(GetPacket()); + pack.WriteChunkCoords(chunk.Position()); + pack.WriteBlockCount(uint32_t(1)); + pack.WriteIndex(index, 0); + pack.WriteBlock(chunk.BlockAt(index), 0); + GetPacket().len = sizeof(Packet::Header) + Packet::BlockUpdate::GetSize(1); + for (ClientConnection &client : clients) { + if (client.ChunkInRange(chunk.Position())) { + client.Send(); + } + } +} + } }