X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=src%2Fserver%2Fnet.cpp;h=9eac33ece573ed35edb24b78893b855f75c0feb3;hb=4727825186798902f68df5b99a6a32f0ef618454;hp=6ceb186644d3a9f7d5d24526d3cfb2376492db8c;hpb=dbd214ac278019c20c883cbb76456b0b0fddc063;p=blank.git diff --git a/src/server/net.cpp b/src/server/net.cpp index 6ceb186..9eac33e 100644 --- a/src/server/net.cpp +++ b/src/server/net.cpp @@ -3,6 +3,7 @@ #include "Server.hpp" #include "../app/init.hpp" +#include "../geometry/distance.hpp" #include "../io/WorldSave.hpp" #include "../model/Model.hpp" #include "../world/ChunkIndex.hpp" @@ -180,6 +181,7 @@ ClientConnection::ClientConnection(Server &server, const IPaddress &addr) , spawns() , confirm_wait(0) , entity_updates() +, entity_updates_skipped(0) , player_update_state() , player_update_pack(0) , player_update_timer(1500) @@ -211,7 +213,7 @@ void ClientConnection::Update(int dt) { // they're the same if (CanDespawn(*global_iter)) { SendDespawn(*local_iter); - } else { + } else if (SendingUpdates()) { // update QueueUpdate(*local_iter); } @@ -247,7 +249,6 @@ void ClientConnection::Update(int dt) { } SendUpdates(); - input->Update(dt); CheckPlayerFix(); CheckChunkQueue(); } @@ -309,6 +310,10 @@ void ClientConnection::SendDespawn(SpawnStatus &status) { ++confirm_wait; } +bool ClientConnection::SendingUpdates() const noexcept { + return entity_updates_skipped >= NetStat().SuggestedPacketSkip(); +} + 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) { @@ -317,10 +322,17 @@ void ClientConnection::QueueUpdate(SpawnStatus &status) { } void ClientConnection::SendUpdates() { + if (!SendingUpdates()) { + entity_updates.clear(); + ++entity_updates_skipped; + return; + } + auto base = PlayerChunks().Base(); auto pack = Prepare(); + pack.WriteChunkBase(base); int entity_pos = 0; for (SpawnStatus *status : entity_updates) { - pack.WriteEntity(*status->entity, entity_pos); + pack.WriteEntity(*status->entity, base, entity_pos); ++entity_pos; if (entity_pos == Packet::EntityUpdate::MAX_ENTITIES) { pack.WriteEntityCount(entity_pos); @@ -334,6 +346,7 @@ void ClientConnection::SendUpdates() { Send(Packet::EntityUpdate::GetSize(entity_pos)); } entity_updates.clear(); + entity_updates_skipped = 0; } void ClientConnection::CheckPlayerFix() { @@ -372,9 +385,9 @@ struct QueueCompare { void ClientConnection::CheckChunkQueue() { if (PlayerChunks().Base() != old_base) { - Chunk::Pos begin = PlayerChunks().CoordsBegin(); - Chunk::Pos end = PlayerChunks().CoordsEnd(); - for (Chunk::Pos pos = begin; pos.z < end.z; ++pos.z) { + ExactLocation::Coarse begin = PlayerChunks().CoordsBegin(); + ExactLocation::Coarse end = PlayerChunks().CoordsEnd(); + for (ExactLocation::Coarse 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 (manhattan_radius(pos - old_base) > PlayerChunks().Extent()) { @@ -386,6 +399,11 @@ void ClientConnection::CheckChunkQueue() { old_base = PlayerChunks().Base(); sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base)); } + // if we have packet skip enabled and just pushed an entity + // update, don't also send chunk data + if (NetStat().SuggestedPacketSkip() > 0 && entity_updates_skipped == 0) { + return; + } if (transmitter.Transmitting()) { transmitter.Transmit(); return; @@ -394,7 +412,7 @@ void ClientConnection::CheckChunkQueue() { int count = 0; constexpr int max = 64; while (count < max && !chunk_queue.empty()) { - Chunk::Pos pos = chunk_queue.front(); + ExactLocation::Coarse pos = chunk_queue.front(); chunk_queue.pop_front(); if (PlayerChunks().InRange(pos)) { Chunk *chunk = PlayerChunks().Get(pos); @@ -416,9 +434,9 @@ void ClientConnection::AttachPlayer(Player &player) { PlayerEntity().Ref(); 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) { + ExactLocation::Coarse begin = PlayerChunks().CoordsBegin(); + ExactLocation::Coarse end = PlayerChunks().CoordsEnd(); + for (ExactLocation::Coarse 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) { chunk_queue.push_back(pos); @@ -431,12 +449,17 @@ void ClientConnection::AttachPlayer(Player &player) { GetPlayerModel().Instantiate(PlayerEntity().GetModel()); } - cout << "player \"" << player.Name() << "\" joined" << endl; + string msg = "player \"" + player.Name() + "\" joined"; + cout << msg << endl; + server.DistributeMessage(0, 0, msg); } void ClientConnection::DetachPlayer() { if (!HasPlayer()) return; - cout << "player \"" << input->GetPlayer().Name() << "\" left" << endl; + string msg = "player \"" + input->GetPlayer().Name() + "\" left"; + cout << msg << endl; + server.DistributeMessage(0, 0, msg); + server.GetWorldSave().Write(input->GetPlayer()); PlayerEntity().Kill(); PlayerEntity().UnRef(); @@ -543,21 +566,17 @@ void ClientConnection::On(const Packet::PlayerUpdate &pack) { 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->TurnHead(player_update_state.pitch - input->GetPitch(), player_update_state.yaw - input->GetYaw()); input->SelectInventory(slot); if ((new_actions & 0x01) && !(old_actions & 0x01)) { @@ -591,7 +610,7 @@ void ClientConnection::On(const Packet::Message &pack) { pack.ReadMessage(msg); if (type == 1 && HasPlayer()) { - server.DistributeMessage(1, PlayerEntity().ID(), msg); + server.DispatchMessage(input->GetPlayer(), msg); } } @@ -607,7 +626,8 @@ Server::Server( , world(world) , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3)) , save(save) -, player_model(nullptr) { +, player_model(nullptr) +, cli(world) { serv_sock = SDLNet_UDP_Open(conf.port); if (!serv_sock) { throw NetError("SDLNet_UDP_Open"); @@ -722,6 +742,17 @@ void Server::SetBlock(Chunk &chunk, int index, const Block &block) { } } +void Server::DispatchMessage(Player &player, const string &msg) { + if (msg.empty()) { + return; + } + if (msg[0] == '/' && msg.size() > 1 && msg[1] != '/') { + cli.Execute(player, msg.substr(1)); + } else { + DistributeMessage(1, player.GetEntity().ID(), msg); + } +} + void Server::DistributeMessage(uint8_t type, uint32_t ref, const string &msg) { auto pack = Packet::Make(serv_pack); pack.WriteType(type);