X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnet%2Fnet.cpp;h=e267855c301ff24bdf9409640724939e635b22af;hb=ca90ec459ca0bd48c1483a45f30496aed61e9c21;hp=fa9c381f15d4961dbf9ae6578c101e408fe99dfd;hpb=d635b2f08a2c3fe483d095dae04ad7ed5bd968d8;p=blank.git diff --git a/src/net/net.cpp b/src/net/net.cpp index fa9c381..e267855 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -4,7 +4,7 @@ #include "Packet.hpp" #include "../app/init.hpp" -#include "../model/CompositeModel.hpp" +#include "../model/Model.hpp" #include "../world/Entity.hpp" #include "../world/EntityState.hpp" @@ -26,6 +26,9 @@ 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; +constexpr size_t Packet::BlockUpdate::MAX_LEN; +constexpr size_t Packet::Message::MAX_LEN; +constexpr size_t Packet::Message::MAX_MESSAGE_LEN; Connection::Connection(const IPaddress &addr) : handler(nullptr) @@ -211,6 +214,10 @@ const char *Packet::Type2String(uint8_t t) noexcept { return "ChunkBegin"; case ChunkData::TYPE: return "ChunkData"; + case BlockUpdate::TYPE: + return "BlockUpdate"; + case Message::TYPE: + return "Message"; default: return "Unknown"; } @@ -284,14 +291,67 @@ void Packet::Join::ReadWorldName(string &name) const noexcept { ReadString(name, 68, 32); } -void Packet::PlayerUpdate::WritePlayer(const Entity &player) noexcept { - Write(player.GetState(), 0); +void Packet::PlayerUpdate::WritePredictedState(const EntityState &state) noexcept { + Write(state, 0); } -void Packet::PlayerUpdate::ReadPlayerState(EntityState &state) const noexcept { +void Packet::PlayerUpdate::ReadPredictedState(EntityState &state) const noexcept { Read(state, 0); } +void Packet::PlayerUpdate::WriteMovement(const glm::vec3 &mov) noexcept { + glm::ivec3 conv = clamp(glm::ivec3(mov * 32767.0f), -32767, 32767); + Write(int16_t(conv.x), 64); + Write(int16_t(conv.y), 66); + Write(int16_t(conv.z), 68); +} + +void Packet::PlayerUpdate::ReadMovement(glm::vec3 &mov) const noexcept { + int16_t x, y, z; + Read(x, 64); + Read(y, 66); + Read(z, 68); + mov = glm::vec3(x, y, z) * .00003051850947599719f; +} + +void Packet::PlayerUpdate::WritePitch(float pitch) noexcept { + int16_t conv = pitch * 20860.12008116853786870640f; + Write(conv, 70); +} + +void Packet::PlayerUpdate::ReadPitch(float &pitch) const noexcept { + int16_t conv = 0; + Read(conv, 70); + pitch = conv * .00004793836258415163f; +} + +void Packet::PlayerUpdate::WriteYaw(float yaw) noexcept { + int16_t conv = yaw * 10430.06004058426893435320f; + Write(conv, 72); +} + +void Packet::PlayerUpdate::ReadYaw(float &yaw) const noexcept { + int16_t conv = 0; + Read(conv, 72); + yaw = conv * .00009587672516830326f; +} + +void Packet::PlayerUpdate::WriteActions(uint8_t actions) noexcept { + Write(actions, 74); +} + +void Packet::PlayerUpdate::ReadActions(uint8_t &actions) const noexcept { + Read(actions, 74); +} + +void Packet::PlayerUpdate::WriteSlot(uint8_t slot) noexcept { + Write(slot, 75); +} + +void Packet::PlayerUpdate::ReadSlot(uint8_t &slot) const noexcept { + Read(slot, 75); +} + void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept { Write(e.ID(), 0); if (e.GetModel()) { @@ -351,19 +411,19 @@ void Packet::EntityUpdate::ReadEntityCount(uint32_t &count) const noexcept { } void Packet::EntityUpdate::WriteEntity(const Entity &entity, uint32_t num) noexcept { - uint32_t off = GetSize(num);; + uint32_t off = GetSize(num); Write(entity.ID(), off); Write(entity.GetState(), off + 4); } void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexcept { - uint32_t off = GetSize(num);; + uint32_t off = GetSize(num); Read(id, off); } void Packet::EntityUpdate::ReadEntityState(EntityState &state, uint32_t num) const noexcept { - uint32_t off = GetSize(num);; + uint32_t off = GetSize(num); Read(state, off + 4); } @@ -449,6 +509,66 @@ void Packet::ChunkData::ReadData(uint8_t *d, size_t l) const noexcept { memcpy(d, &data[12], len); } +void Packet::BlockUpdate::WriteChunkCoords(const glm::ivec3 &coords) noexcept { + Write(coords, 0); +} + +void Packet::BlockUpdate::ReadChunkCoords(glm::ivec3 &coords) const noexcept { + Read(coords, 0); +} + +void Packet::BlockUpdate::WriteBlockCount(uint32_t count) noexcept { + Write(count, 12); +} + +void Packet::BlockUpdate::ReadBlockCount(uint32_t &count) const noexcept { + Read(count, 12); +} + +void Packet::BlockUpdate::WriteIndex(uint16_t index, uint32_t num) noexcept { + uint32_t off = GetSize(num); + Write(index, off); +} + +void Packet::BlockUpdate::ReadIndex(uint16_t &index, uint32_t num) const noexcept { + uint32_t off = GetSize(num); + Read(index, off); +} + +void Packet::BlockUpdate::WriteBlock(const Block &block, uint32_t num) noexcept { + uint32_t off = GetSize(num) + 2; + Write(block, off); +} + +void Packet::BlockUpdate::ReadBlock(Block &block, uint32_t num) const noexcept { + uint32_t off = GetSize(num) + 2; + Read(block, off); +} + +void Packet::Message::WriteType(uint8_t type) noexcept { + Write(type, 0); +} + +void Packet::Message::ReadType(uint8_t &type) const noexcept { + Read(type, 0); +} + +void Packet::Message::WriteReferral(uint32_t ref) noexcept { + Write(ref, 1); +} + +void Packet::Message::ReadReferral(uint32_t &ref) const noexcept { + Read(ref, 1); +} + +void Packet::Message::WriteMessage(const string &msg) noexcept { + WriteString(msg, 5, MAX_MESSAGE_LEN); +} + +void Packet::Message::ReadMessage(string &msg) const noexcept { + ReadString(msg, 5, MAX_MESSAGE_LEN); +} + void ConnectionHandler::Handle(const UDPpacket &udp_pack) { const Packet &pack = *reinterpret_cast(udp_pack.data); @@ -486,6 +606,12 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) { case Packet::ChunkData::TYPE: On(Packet::As(udp_pack)); break; + case Packet::BlockUpdate::TYPE: + On(Packet::As(udp_pack)); + break; + case Packet::Message::TYPE: + On(Packet::As(udp_pack)); + break; default: // drop unknown or unhandled packets break;