X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fclient%2Fnet.cpp;h=8a4930410605b7a8eacf0ae0dd8886ef2630ad51;hb=8639a90bcbcd045c57cd489f02a25e0df4236deb;hp=31f13d947631a2e2c615bc3a5a891f96bc145238;hpb=a1a52a8dcfe961c39b39b3abe01e49fd1430102d;p=blank.git diff --git a/src/client/net.cpp b/src/client/net.cpp index 31f13d9..8a49304 100644 --- a/src/client/net.cpp +++ b/src/client/net.cpp @@ -1,11 +1,15 @@ #include "ChunkReceiver.hpp" #include "ChunkTransmission.hpp" #include "Client.hpp" +#include "NetworkedInput.hpp" #include "../app/init.hpp" +#include "../io/WorldSave.hpp" #include "../net/Packet.hpp" #include "../world/Chunk.hpp" #include "../world/ChunkStore.hpp" +#include "../world/Player.hpp" +#include "../world/World.hpp" #include #include @@ -18,8 +22,9 @@ namespace blank { namespace client { -ChunkReceiver::ChunkReceiver(ChunkStore &store) +ChunkReceiver::ChunkReceiver(ChunkStore &store, const WorldSave &save) : store(store) +, save(save) , transmissions() , timer(5000) { timer.Start(); @@ -45,8 +50,50 @@ void ChunkReceiver::Update(int dt) { } } } + LoadN(10); + StoreN(10); } +int ChunkReceiver::ToLoad() const noexcept { + return store.EstimateMissing(); +} + +void ChunkReceiver::LoadOne() { + if (!store.HasMissing()) return; + + Chunk::Pos pos = store.NextMissing(); + Chunk *chunk = store.Allocate(pos); + if (!chunk) { + // chunk store corrupted? + return; + } + + if (save.Exists(pos)) { + save.Read(*chunk); + } +} + +void ChunkReceiver::LoadN(size_t n) { + size_t end = min(n, size_t(ToLoad())); + for (size_t i = 0; i < end && store.HasMissing(); ++i) { + LoadOne(); + } +} + +void ChunkReceiver::StoreN(size_t n) { + size_t saved = 0; + for (Chunk &chunk : store) { + if (chunk.ShouldUpdateSave()) { + save.Write(chunk); + ++saved; + if (saved >= n) { + break; + } + } + } +} + + void ChunkReceiver::Handle(const Packet::ChunkBegin &pack) { uint32_t id; pack.ReadTransmissionId(id); @@ -177,7 +224,7 @@ IPaddress client_resolve(const char *host, Uint16 port) { } -Client::Client(const Config &conf) +Client::Client(const Config::Network &conf) : conn(client_resolve(conf.host.c_str(), conf.port)) , client_sock(client_bind(0)) , client_pack{ -1, nullptr, 0 } { @@ -236,9 +283,21 @@ uint16_t Client::SendLogin(const string &name) { return conn.Send(client_pack, client_sock); } -uint16_t Client::SendPlayerUpdate(const Entity &player) { +uint16_t Client::SendPlayerUpdate( + const EntityState &prediction, + const glm::vec3 &movement, + float pitch, + float yaw, + uint8_t actions, + uint8_t slot +) { auto pack = Packet::Make(client_pack); - pack.WritePlayer(player); + pack.WritePredictedState(prediction); + pack.WriteMovement(movement); + pack.WritePitch(pitch); + pack.WriteYaw(yaw); + pack.WriteActions(actions); + pack.WriteSlot(slot); return conn.Send(client_pack, client_sock); } @@ -247,5 +306,133 @@ uint16_t Client::SendPart() { return conn.Send(client_pack, client_sock); } + +NetworkedInput::NetworkedInput(World &world, Player &player, Client &client) +: PlayerController(world, player) +, client(client) +, player_hist() +, actions(0) { + +} + +void NetworkedInput::Update(int dt) { + Invalidate(); + UpdatePlayer(); +} + +void NetworkedInput::PushPlayerUpdate(int dt) { + const EntityState &state = GetPlayer().GetEntity().GetState(); + + uint16_t packet = client.SendPlayerUpdate( + state, + GetMovement(), + GetPitch(), + GetYaw(), + actions, + InventorySlot() + ); + if (player_hist.size() < 16) { + player_hist.emplace_back(state, GetPlayer().GetEntity().TargetVelocity(), dt, packet); + } else { + auto entry = player_hist.begin(); + entry->state = state; + entry->tgt_vel = GetPlayer().GetEntity().TargetVelocity(); + entry->delta_t = dt; + entry->packet = packet; + player_hist.splice(player_hist.end(), player_hist, entry); + } +} + +void NetworkedInput::MergePlayerCorrection(uint16_t seq, const EntityState &corrected_state) { + if (player_hist.empty()) return; + + auto entry = player_hist.begin(); + auto end = player_hist.end(); + + // we may have received an older packet + int pack_diff = int16_t(seq) - int16_t(entry->packet); + if (pack_diff < 0) { + // indeed we have, just ignore it + return; + } + + // drop anything older than the fix + while (entry != end) { + pack_diff = int16_t(seq) - int16_t(entry->packet); + if (pack_diff > 0) { + entry = player_hist.erase(entry); + } else { + break; + } + } + + EntityState &player_state = GetPlayer().GetEntity().GetState(); + Entity replay(GetPlayer().GetEntity()); + replay.SetState(corrected_state); + + if (entry != end) { + entry->state.chunk_pos = replay.GetState().chunk_pos; + entry->state.block_pos = replay.GetState().block_pos; + ++entry; + } + + vector col; + while (entry != end) { + replay.Velocity(entry->state.velocity); + replay.TargetVelocity(entry->tgt_vel); + GetWorld().Update(replay, entry->delta_t); + entry->state.chunk_pos = replay.GetState().chunk_pos; + entry->state.block_pos = replay.GetState().block_pos; + ++entry; + } + + glm::vec3 displacement(replay.GetState().Diff(player_state)); + const float disp_squared = dot(displacement, displacement); + + if (disp_squared < 16.0f * numeric_limits::epsilon()) { + return; + } + + // if offset > 10cm, warp the player + // otherwise, move at most 1cm per frame towards + // the fixed position (160ms, so shouldn't be too noticeable) + constexpr float warp_thresh = 0.01f; // (1/10)^2 + constexpr float max_disp = 0.0001f; // (1/100)^2 + + if (disp_squared > warp_thresh) { + player_state.chunk_pos = replay.GetState().chunk_pos; + player_state.block_pos = replay.GetState().block_pos; + } else if (disp_squared < max_disp) { + player_state.block_pos += displacement; + } else { + displacement *= 0.01f / sqrt(disp_squared); + player_state.block_pos += displacement; + } +} + +void NetworkedInput::StartPrimaryAction() { + actions |= 0x01; +} + +void NetworkedInput::StopPrimaryAction() { + actions &= ~0x01; +} + +void NetworkedInput::StartSecondaryAction() { + actions |= 0x02; +} + +void NetworkedInput::StopSecondaryAction() { + actions &= ~0x02; +} + +void NetworkedInput::StartTertiaryAction() { + actions |= 0x04; +} + +void NetworkedInput::StopTertiaryAction() { + actions &= ~0x04; +} + } }