X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnet%2Fnet.cpp;h=f59b80962530e5a4f4ca8b883b454247eb466a86;hb=ae5a7e7d8517fac406a88e9bf98fd3d5bb1728b9;hp=d56e4093545526e980bde0ee95bf6e916025effc;hpb=51a0b19601cb4b044c7eee1782aa85fc3a399d33;p=blank.git diff --git a/src/net/net.cpp b/src/net/net.cpp index d56e409..f59b809 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -7,6 +7,10 @@ #include "Server.hpp" #include "../app/init.hpp" +#include "../model/CompositeModel.hpp" +#include "../world/ChunkIndex.hpp" +#include "../world/Entity.hpp" +#include "../world/EntityState.hpp" #include "../world/World.hpp" #include @@ -26,6 +30,9 @@ constexpr size_t Packet::PlayerUpdate::MAX_LEN; constexpr size_t Packet::SpawnEntity::MAX_LEN; constexpr size_t Packet::DespawnEntity::MAX_LEN; constexpr size_t Packet::EntityUpdate::MAX_LEN; +constexpr size_t Packet::PlayerCorrection::MAX_LEN; +constexpr size_t Packet::ChunkBegin::MAX_LEN; +constexpr size_t Packet::ChunkData::MAX_LEN; namespace { @@ -121,9 +128,15 @@ uint16_t Client::SendPart() { ClientConnection::ClientConnection(Server &server, const IPaddress &addr) : server(server) , conn(addr) -, player(nullptr) +, player(nullptr, nullptr) , spawns() -, confirm_wait(0) { +, confirm_wait(0) +, player_update_state() +, player_update_pack(0) +, player_update_timer(1500) +, transmitter(*this) +, chunk_queue() +, old_base() { conn.SetHandler(this); } @@ -182,6 +195,9 @@ void ClientConnection::Update(int dt) { SendDespawn(*local_iter); ++local_iter; } + + CheckPlayerFix(); + CheckChunkQueue(); } if (conn.ShouldPing()) { conn.SendPing(server.GetPacket(), server.GetSocket()); @@ -201,24 +217,33 @@ ClientConnection::SpawnStatus::~SpawnStatus() { bool ClientConnection::CanSpawn(const Entity &e) const noexcept { return - &e != player && + &e != player.entity && !e.Dead() && - manhattan_radius(e.ChunkCoords() - Player().ChunkCoords()) < 7; + manhattan_radius(e.ChunkCoords() - PlayerEntity().ChunkCoords()) < 7; } bool ClientConnection::CanDespawn(const Entity &e) const noexcept { return e.Dead() || - manhattan_radius(e.ChunkCoords() - Player().ChunkCoords()) > 7; + manhattan_radius(e.ChunkCoords() - PlayerEntity().ChunkCoords()) > 7; +} + +uint16_t ClientConnection::Send() { + return conn.Send(server.GetPacket(), server.GetSocket()); +} + +uint16_t ClientConnection::Send(size_t len) { + server.GetPacket().len = len; + return Send(); } void ClientConnection::SendSpawn(SpawnStatus &status) { // don't double spawn if (status.spawn_pack != -1) return; - auto pack = Packet::Make(server.GetPacket()); + auto pack = Prepare(); pack.WriteEntity(*status.entity); - status.spawn_pack = conn.Send(server.GetPacket(), server.GetSocket()); + status.spawn_pack = Send(); ++confirm_wait; } @@ -226,9 +251,9 @@ void ClientConnection::SendDespawn(SpawnStatus &status) { // don't double despawn if (status.despawn_pack != -1) return; - auto pack = Packet::Make(server.GetPacket()); + auto pack = Prepare(); pack.WriteEntityID(status.entity->ID()); - status.despawn_pack = conn.Send(server.GetPacket(), server.GetSocket()); + status.despawn_pack = Send(); ++confirm_wait; } @@ -237,29 +262,102 @@ void ClientConnection::SendUpdate(SpawnStatus &status) { if (status.spawn_pack != -1 || status.despawn_pack != -1) return; // TODO: pack entity updates - auto pack = Packet::Make(server.GetPacket()); + auto pack = Prepare(); pack.WriteEntityCount(1); pack.WriteEntity(*status.entity, 0); - server.GetPacket().len = Packet::EntityUpdate::GetSize(1); - conn.Send(server.GetPacket(), server.GetSocket()); + Send(Packet::EntityUpdate::GetSize(1)); +} + +void ClientConnection::CheckPlayerFix() { + // player_update_state's position holds the client's most recent prediction + glm::vec3 diff = player_update_state.Diff(PlayerEntity().GetState()); + float dist_squared = dot(diff, diff); + + // if client's prediction is off by more than 1cm, send + // our (authoritative) state back so it can fix it + constexpr float fix_thresh = 0.0001f; + + if (dist_squared > fix_thresh) { + auto pack = Prepare(); + pack.WritePacketSeq(player_update_pack); + pack.WritePlayer(PlayerEntity()); + Send(); + } +} + +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) { + 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()) { + chunk_queue.push_back(pos); + } + } + } + } + old_base = PlayerChunks().Base(); + } + if (transmitter.Transmitting()) { + transmitter.Transmit(); + return; + } + if (transmitter.Idle()) { + int count = 0; + constexpr int max = 64; + while (count < max && !chunk_queue.empty()) { + Chunk::Pos pos = chunk_queue.front(); + chunk_queue.pop_front(); + if (PlayerChunks().InRange(pos)) { + Chunk *chunk = PlayerChunks().Get(pos); + if (chunk) { + transmitter.Send(*chunk); + return; + } else { + chunk_queue.push_back(pos); + } + ++count; + } + } + } } -void ClientConnection::AttachPlayer(Entity &new_player) { +void ClientConnection::AttachPlayer(const Player &new_player) { DetachPlayer(); - player = &new_player; - player->Ref(); - cout << "player \"" << player->Name() << "\" joined" << endl; + player = new_player; + player.entity->Ref(); + + old_base = player.chunks->Base(); + Chunk::Pos begin = player.chunks->CoordsBegin(); + Chunk::Pos end = player.chunks->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) { + chunk_queue.push_back(pos); + } + } + } + + cout << "player \"" << player.entity->Name() << "\" joined" << endl; } void ClientConnection::DetachPlayer() { - if (!player) return; - player->Kill(); - player->UnRef(); - cout << "player \"" << player->Name() << "\" left" << endl; - player = nullptr; + if (!HasPlayer()) return; + cout << "player \"" << player.entity->Name() << "\" left" << endl; + player.entity->Kill(); + player.entity->UnRef(); + player.entity = nullptr; + player.chunks = nullptr; + transmitter.Abort(); + chunk_queue.clear(); } void ClientConnection::OnPacketReceived(uint16_t seq) { + if (transmitter.Waiting()) { + transmitter.Ack(seq); + } if (!confirm_wait) return; for (auto iter = spawns.begin(), end = spawns.end(); iter != end; ++iter) { if (seq == iter->spawn_pack) { @@ -276,6 +374,9 @@ void ClientConnection::OnPacketReceived(uint16_t seq) { } void ClientConnection::OnPacketLost(uint16_t seq) { + if (transmitter.Waiting()) { + transmitter.Nack(seq); + } if (!confirm_wait) return; for (SpawnStatus &status : spawns) { if (seq == status.spawn_pack) { @@ -297,21 +398,26 @@ void ClientConnection::On(const Packet::Login &pack) { string name; pack.ReadPlayerName(name); - Entity *new_player = server.GetWorld().AddPlayer(name); + Player new_player = server.GetWorld().AddPlayer(name); - if (new_player) { + if (new_player.entity) { // success! - AttachPlayer(*new_player); + AttachPlayer(new_player); cout << "accepted login from player \"" << name << '"' << endl; - auto response = Packet::Make(server.GetPacket()); - response.WritePlayer(*new_player); + auto response = Prepare(); + response.WritePlayer(*new_player.entity); response.WriteWorldName(server.GetWorld().Name()); - conn.Send(server.GetPacket(), server.GetSocket()); + Send(); + // set up update tracking + player_update_state = new_player.entity->GetState(); + player_update_pack = pack.Seq(); + player_update_timer.Reset(); + player_update_timer.Start(); } else { // aw no :( cout << "rejected login from player \"" << name << '"' << endl; - Packet::Make(server.GetPacket()); - conn.Send(server.GetPacket(), server.GetSocket()); + Prepare(); + Send(); conn.Close(); } } @@ -322,7 +428,16 @@ void ClientConnection::On(const Packet::Part &) { void ClientConnection::On(const Packet::PlayerUpdate &pack) { if (!HasPlayer()) return; - pack.ReadPlayer(Player()); + 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); + } } @@ -422,7 +537,7 @@ void Connection::Received(const UDPpacket &udp_pack) { } } // check for newly ack'd packets - for (uint16_t s = ctrl_new.AckBegin(); s != ctrl_new.AckEnd(); ++s) { + for (uint16_t s = ctrl_new.AckBegin(); s != ctrl_new.AckEnd(); --s) { if (ctrl_new.Acks(s) && !ctrl_in.Acks(s)) { Handler().OnPacketReceived(s); } @@ -475,6 +590,12 @@ const char *Packet::Type2String(uint8_t t) noexcept { return "DespawnEntity"; case EntityUpdate::TYPE: return "EntityUpdate"; + case PlayerCorrection::TYPE: + return "PlayerCorrection"; + case ChunkBegin::TYPE: + return "ChunkBegin"; + case ChunkData::TYPE: + return "ChunkData"; default: return "Unknown"; } @@ -529,34 +650,15 @@ void Packet::Login::ReadPlayerName(string &name) const noexcept { void Packet::Join::WritePlayer(const Entity &player) noexcept { Write(player.ID(), 0); - Write(player.ChunkCoords(), 4); - Write(player.Position(), 16); - Write(player.Velocity(), 28); - Write(player.Orientation(), 40); - Write(player.AngularVelocity(), 56); + Write(player.GetState(), 4); } void Packet::Join::ReadPlayerID(uint32_t &id) const noexcept { Read(id, 0); } -void Packet::Join::ReadPlayer(Entity &player) const noexcept { - glm::ivec3 chunk_coords(0); - glm::vec3 pos; - glm::vec3 vel; - glm::quat rot; - glm::vec3 ang; - - Read(chunk_coords, 4); - Read(pos, 16); - Read(vel, 28); - Read(rot, 40); - Read(ang, 56); - - player.Position(chunk_coords, pos); - player.Velocity(vel); - player.Orientation(rot); - player.AngularVelocity(ang); +void Packet::Join::ReadPlayerState(EntityState &state) const noexcept { + Read(state, 4); } void Packet::Join::WriteWorldName(const string &name) noexcept { @@ -568,75 +670,50 @@ void Packet::Join::ReadWorldName(string &name) const noexcept { } void Packet::PlayerUpdate::WritePlayer(const Entity &player) noexcept { - Write(player.ChunkCoords(), 0); - Write(player.Position(), 12); - Write(player.Velocity(), 24); - Write(player.Orientation(), 36); - Write(player.AngularVelocity(), 52); + Write(player.GetState(), 0); } -void Packet::PlayerUpdate::ReadPlayer(Entity &player) const noexcept { - glm::ivec3 chunk_coords(0); - glm::vec3 pos; - glm::vec3 vel; - glm::quat rot; - glm::vec3 ang; - - Read(chunk_coords, 0); - Read(pos, 12); - Read(vel, 24); - Read(rot, 36); - Read(ang, 52); - - player.Position(chunk_coords, pos); - player.Velocity(vel); - player.Orientation(rot); - player.AngularVelocity(ang); +void Packet::PlayerUpdate::ReadPlayerState(EntityState &state) const noexcept { + Read(state, 0); } void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept { Write(e.ID(), 0); - Write(e.ChunkCoords(), 4); - Write(e.Position(), 16); - Write(e.Velocity(), 28); - Write(e.Orientation(), 40); - Write(e.AngularVelocity(), 56); - Write(e.Bounds(), 68); + if (e.GetModel()) { + Write(e.GetModel().GetModel().ID(), 4); + } else { + Write(uint32_t(0), 4); + } + Write(e.GetState(), 8); + Write(e.Bounds(), 72); uint32_t flags = 0; if (e.WorldCollidable()) { flags |= 1; } - Write(flags, 92); - WriteString(e.Name(), 96, 32); + Write(flags, 96); + WriteString(e.Name(), 100, 32); } void Packet::SpawnEntity::ReadEntityID(uint32_t &id) const noexcept { Read(id, 0); } +void Packet::SpawnEntity::ReadSkeletonID(uint32_t &id) const noexcept { + Read(id, 4); +} + void Packet::SpawnEntity::ReadEntity(Entity &e) const noexcept { - glm::ivec3 chunk_coords(0); - glm::vec3 pos; - glm::vec3 vel; - glm::quat rot; - glm::vec3 ang; + EntityState state; AABB bounds; uint32_t flags = 0; string name; - Read(chunk_coords, 4); - Read(pos, 16); - Read(vel, 28); - Read(rot, 40); - Read(ang, 56); - Read(bounds, 68); - Read(flags, 92); - ReadString(name, 96, 32); - - e.Position(chunk_coords, pos); - e.Velocity(vel); - e.Orientation(rot); - e.AngularVelocity(ang); + Read(state, 8); + Read(bounds, 72); + Read(flags, 96); + ReadString(name, 100, 32); + + e.SetState(state); e.Bounds(bounds); e.WorldCollidable(flags & 1); e.Name(name); @@ -662,36 +739,98 @@ void Packet::EntityUpdate::WriteEntity(const Entity &entity, uint32_t num) noexc uint32_t off = 4 + (num * 64); Write(entity.ID(), off); - Write(entity.ChunkCoords(), off + 4); - Write(entity.Position(), off + 16); - Write(entity.Velocity(), off + 28); - Write(entity.Orientation(), off + 40); - Write(entity.AngularVelocity(), off + 56); + Write(entity.GetState(), off + 4); } void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexcept { Read(id, 4 + (num * 64)); } -void Packet::EntityUpdate::ReadEntity(Entity &entity, uint32_t num) const noexcept { +void Packet::EntityUpdate::ReadEntityState(EntityState &state, uint32_t num) const noexcept { uint32_t off = 4 + (num * 64); + Read(state, off + 4); +} + +void Packet::PlayerCorrection::WritePacketSeq(std::uint16_t s) noexcept { + Write(s, 0); +} + +void Packet::PlayerCorrection::ReadPacketSeq(std::uint16_t &s) const noexcept { + Read(s, 0); +} + +void Packet::PlayerCorrection::WritePlayer(const Entity &player) noexcept { + Write(player.GetState(), 2); +} + +void Packet::PlayerCorrection::ReadPlayerState(EntityState &state) const noexcept { + Read(state, 2); +} + +void Packet::ChunkBegin::WriteTransmissionId(uint32_t id) noexcept { + Write(id, 0); +} + +void Packet::ChunkBegin::ReadTransmissionId(uint32_t &id) const noexcept { + Read(id, 0); +} + +void Packet::ChunkBegin::WriteFlags(uint32_t f) noexcept { + Write(f, 4); +} + +void Packet::ChunkBegin::ReadFlags(uint32_t &f) const noexcept { + Read(f, 4); +} + +void Packet::ChunkBegin::WriteChunkCoords(const glm::ivec3 &pos) noexcept { + Write(pos, 8); +} + +void Packet::ChunkBegin::ReadChunkCoords(glm::ivec3 &pos) const noexcept { + Read(pos, 8); +} - glm::ivec3 chunk_coords(0); - glm::vec3 pos; - glm::vec3 vel; - glm::quat rot; - glm::vec3 ang; +void Packet::ChunkBegin::WriteDataSize(uint32_t s) noexcept { + Write(s, 20); +} + +void Packet::ChunkBegin::ReadDataSize(uint32_t &s) const noexcept { + Read(s, 20); +} - Read(chunk_coords, off + 4); - Read(pos, off + 16); - Read(vel, off + 28); - Read(rot, off + 40); - Read(ang, off + 56); +void Packet::ChunkData::WriteTransmissionId(uint32_t id) noexcept { + Write(id, 0); +} - entity.Position(chunk_coords, pos); - entity.Velocity(vel); - entity.Orientation(rot); - entity.AngularVelocity(ang); +void Packet::ChunkData::ReadTransmissionId(uint32_t &id) const noexcept { + Read(id, 0); +} + +void Packet::ChunkData::WriteDataOffset(uint32_t o) noexcept { + Write(o, 4); +} + +void Packet::ChunkData::ReadDataOffset(uint32_t &o) const noexcept { + Read(o, 4); +} + +void Packet::ChunkData::WriteDataSize(uint32_t s) noexcept { + Write(s, 8); +} + +void Packet::ChunkData::ReadDataSize(uint32_t &s) const noexcept { + Read(s, 8); +} + +void Packet::ChunkData::WriteData(const uint8_t *d, size_t l) noexcept { + size_t len = min(length - 12, l); + memcpy(&data[12], d, len); +} + +void Packet::ChunkData::ReadData(uint8_t *d, size_t l) const noexcept { + size_t len = min(length - 12, l); + memcpy(d, &data[12], len); } @@ -722,6 +861,15 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) { case Packet::EntityUpdate::TYPE: On(Packet::As(udp_pack)); break; + case Packet::PlayerCorrection::TYPE: + On(Packet::As(udp_pack)); + break; + case Packet::ChunkBegin::TYPE: + On(Packet::As(udp_pack)); + break; + case Packet::ChunkData::TYPE: + On(Packet::As(udp_pack)); + break; default: // drop unknown or unhandled packets break;