BIG REFACTOR

This commit is contained in:
2022-02-16 17:54:33 +01:00
parent 387cff36ad
commit bdebabb79e
13 changed files with 111 additions and 74 deletions

View File

@@ -12,23 +12,30 @@
namespace td { namespace td {
namespace game { namespace game {
typedef std::pair<std::int16_t, std::int16_t> ChunkCoord;
struct ChunkCoord {
std::int16_t x, y;
friend bool operator==(const td::game::ChunkCoord& first, const td::game::ChunkCoord& other) {
return first.x == other.x && first.y == other.y;
}
};
} }
} }
namespace std { namespace std {
template <> template <>
struct hash<td::game::ChunkCoord> { struct hash<td::game::ChunkCoord> {
std::size_t operator()(const td::game::ChunkCoord& key) const { std::size_t operator()(const td::game::ChunkCoord& key) const {
// Compute individual hash values for first, return std::hash<std::int16_t>()(key.x) ^ std::hash<std::int16_t>()(key.y);
// second and third and combine them using XOR
// and bit shifting:
return ((std::hash<std::int16_t>()(key.first) ^ (std::hash<std::int16_t>()(key.second) << 1)) >> 1);
} }
}; };
} }
namespace td { namespace td {
namespace protocol { namespace protocol {
class WorldBeginDataPacket; class WorldBeginDataPacket;
@@ -91,12 +98,13 @@ typedef std::vector<std::uint16_t> ChunkPalette;
typedef std::shared_ptr<WalkableTile> WalkableTilePtr; typedef std::shared_ptr<WalkableTile> WalkableTilePtr;
typedef std::array<std::uint16_t, 32 * 32> ChunkData;
typedef std::uint32_t TileIndex; typedef std::uint32_t TileIndex;
//32 x 32 area //32 x 32 area
struct Chunk { struct Chunk {
enum { ChunkWidth = 32, ChunkHeight = 32, ChunkSize = ChunkWidth * ChunkHeight }; enum { ChunkWidth = 32, ChunkHeight = 32, ChunkSize = ChunkWidth * ChunkHeight };
typedef std::array<std::uint16_t, ChunkSize> ChunkData;
// stores index of tile palette // stores index of tile palette
ChunkData tiles{ 0 }; ChunkData tiles{ 0 };
ChunkPalette palette; ChunkPalette palette;

View File

@@ -6,6 +6,9 @@
namespace td { namespace td {
namespace utils { namespace utils {
std::uint64_t inflate(const std::string& source, std::string& dest);
std::uint64_t deflate(const std::string& source, std::string& dest);
DataBuffer Compress(const DataBuffer& buffer); DataBuffer Compress(const DataBuffer& buffer);
DataBuffer Decompress(DataBuffer& buffer); DataBuffer Decompress(DataBuffer& buffer);
DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength); DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength);

View File

@@ -1,5 +1,4 @@
#ifndef MCLIB_COMMON_DATA_BUFFER_H_ #pragma once
#define MCLIB_COMMON_DATA_BUFFER_H_
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
@@ -13,7 +12,7 @@ class DataBuffer {
private: private:
typedef std::vector<std::uint8_t> Data; typedef std::vector<std::uint8_t> Data;
Data m_Buffer; Data m_Buffer;
std::size_t m_ReadOffset = 0; std::size_t m_ReadOffset;
public: public:
typedef Data::iterator iterator; typedef Data::iterator iterator;
@@ -23,7 +22,7 @@ public:
DataBuffer(); DataBuffer();
DataBuffer(const DataBuffer& other); DataBuffer(const DataBuffer& other);
DataBuffer(const DataBuffer& other, std::size_t offset); DataBuffer(const DataBuffer& other, Data::difference_type offset);
DataBuffer(DataBuffer&& other); DataBuffer(DataBuffer&& other);
DataBuffer(const std::string& str); DataBuffer(const std::string& str);
@@ -64,7 +63,7 @@ public:
template <typename T> template <typename T>
DataBuffer& operator>>(T& data) { DataBuffer& operator>>(T& data) {
assert(m_ReadOffset + sizeof(T) <= GetSize()); assert(m_ReadOffset + sizeof(T) <= GetSize());
data = *(T*)&m_Buffer[m_ReadOffset]; data = *(reinterpret_cast<T*>(&m_Buffer[m_ReadOffset]));
//std::reverse((std::uint8_t*)&data, (std::uint8_t*)&data + sizeof(T)); //std::reverse((std::uint8_t*)&data, (std::uint8_t*)&data + sizeof(T));
m_ReadOffset += sizeof(T); m_ReadOffset += sizeof(T);
return *this; return *this;
@@ -72,42 +71,43 @@ public:
DataBuffer& operator>>(DataBuffer& data) { DataBuffer& operator>>(DataBuffer& data) {
data.Resize(GetSize() - m_ReadOffset); data.Resize(GetSize() - m_ReadOffset);
std::copy(m_Buffer.begin() + m_ReadOffset, m_Buffer.end(), data.begin()); std::copy(m_Buffer.begin() + static_cast<Data::difference_type>(m_ReadOffset), m_Buffer.end(), data.begin());
m_ReadOffset = m_Buffer.size(); m_ReadOffset = m_Buffer.size();
return *this; return *this;
} }
DataBuffer& operator>>(std::string& str) { DataBuffer& operator>>(std::string& str) {
std::size_t stringSize = strlen((const char*)m_Buffer.data() + m_ReadOffset) + 1; // including null character std::size_t stringSize = strlen(reinterpret_cast<const char*>(m_Buffer.data()) + m_ReadOffset) + 1; // including null character
str.resize(stringSize); str.resize(stringSize);
std::copy(m_Buffer.begin() + m_ReadOffset, m_Buffer.begin() + m_ReadOffset + stringSize, str.begin()); std::copy(m_Buffer.begin() + static_cast<Data::difference_type>(m_ReadOffset),
m_Buffer.begin() + static_cast<Data::difference_type>(m_ReadOffset + stringSize), str.begin());
m_ReadOffset += stringSize; m_ReadOffset += stringSize;
return *this; return *this;
} }
void ReadSome(char* buffer, std::size_t amount) { void ReadSome(char* buffer, std::size_t amount) {
assert(m_ReadOffset + amount <= GetSize()); assert(m_ReadOffset + amount <= GetSize());
std::copy_n(m_Buffer.begin() + m_ReadOffset, amount, buffer); std::copy_n(m_Buffer.begin() + static_cast<Data::difference_type>(m_ReadOffset), amount, buffer);
m_ReadOffset += amount; m_ReadOffset += amount;
} }
void ReadSome(std::uint8_t* buffer, std::size_t amount) { void ReadSome(std::uint8_t* buffer, std::size_t amount) {
assert(m_ReadOffset + amount <= GetSize()); assert(m_ReadOffset + amount <= GetSize());
std::copy_n(m_Buffer.begin() + m_ReadOffset, amount, buffer); std::copy_n(m_Buffer.begin() + static_cast<Data::difference_type>(m_ReadOffset), amount, buffer);
m_ReadOffset += amount; m_ReadOffset += amount;
} }
void ReadSome(DataBuffer& buffer, std::size_t amount) { void ReadSome(DataBuffer& buffer, std::size_t amount) {
assert(m_ReadOffset + amount <= GetSize()); assert(m_ReadOffset + amount <= GetSize());
buffer.Resize(amount); buffer.Resize(amount);
std::copy_n(m_Buffer.begin() + m_ReadOffset, amount, buffer.begin()); std::copy_n(m_Buffer.begin() + static_cast<Data::difference_type>(m_ReadOffset), amount, buffer.begin());
m_ReadOffset += amount; m_ReadOffset += amount;
} }
void ReadSome(std::string& buffer, std::size_t amount) { void ReadSome(std::string& buffer, std::size_t amount) {
assert(m_ReadOffset + amount <= GetSize()); assert(m_ReadOffset + amount <= GetSize());
buffer.resize(amount); buffer.resize(amount);
std::copy_n(m_Buffer.begin() + m_ReadOffset, amount, buffer.begin()); std::copy_n(m_Buffer.begin() + static_cast<Data::difference_type>(m_ReadOffset), amount, buffer.begin());
m_ReadOffset += amount; m_ReadOffset += amount;
} }
@@ -163,5 +163,3 @@ public:
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer); std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
} // ns td } // ns td
#endif

View File

@@ -15,7 +15,7 @@ class TCPSocket : public Socket {
private: private:
IPAddress m_RemoteIP; IPAddress m_RemoteIP;
uint16_t m_Port; uint16_t m_Port;
sockaddr_in m_RemoteAddr; sockaddr m_RemoteAddr;
public: public:
TCPSocket(); TCPSocket();

View File

@@ -20,7 +20,9 @@ public:
bool Connect(const IPAddress& address, std::uint16_t port); bool Connect(const IPAddress& address, std::uint16_t port);
std::size_t Send(const std::uint8_t* data, std::size_t size); std::size_t Send(const std::uint8_t* data, std::size_t size);
DataBuffer Receive(std::size_t amount);
virtual DataBuffer Receive(std::size_t amount);
virtual std::size_t Receive(DataBuffer& buffer, std::size_t amount);
}; };
} // ns network } // ns network

View File

@@ -8,18 +8,17 @@
namespace td { namespace td {
namespace utils { namespace utils {
unsigned long inflate(const std::string& source, std::string& dest) { std::uint64_t inflate(const std::string& source, std::string& dest) {
unsigned long size = dest.size(); std::size_t size;
uncompress((Bytef*)&dest[0], &size, (const Bytef*)source.c_str(), source.length()); uncompress(reinterpret_cast<Bytef*>(dest.data()), &size, reinterpret_cast<const Bytef*>(source.c_str()), source.length());
return size; return size;
} }
unsigned long deflate(const std::string& source, std::string& dest) { std::uint64_t deflate(const std::string& source, std::string& dest) {
unsigned long size = source.length(); std::size_t size;
dest.resize(size); dest.resize(source.size()); // Resize for the compressed data to fit into
compress(reinterpret_cast<Bytef*>(dest.data()), &size, reinterpret_cast<const Bytef*>(source.c_str()), source.length());
compress((Bytef*)&dest[0], &size, (const Bytef*)source.c_str(), source.length()); dest.resize(size); // Resize to cut useless data
dest.resize(size);
return size; return size;
} }

View File

@@ -7,12 +7,13 @@
namespace td { namespace td {
DataBuffer::DataBuffer() { } DataBuffer::DataBuffer() : m_ReadOffset(0) { }
DataBuffer::DataBuffer(const DataBuffer& other) : m_Buffer(other.m_Buffer), m_ReadOffset(other.m_ReadOffset) { } DataBuffer::DataBuffer(const DataBuffer& other) : m_Buffer(other.m_Buffer), m_ReadOffset(other.m_ReadOffset) { }
DataBuffer::DataBuffer(DataBuffer&& other) : m_Buffer(std::move(other.m_Buffer)), m_ReadOffset(std::move(other.m_ReadOffset)) { } DataBuffer::DataBuffer(DataBuffer&& other) : m_Buffer(std::move(other.m_Buffer)), m_ReadOffset(std::move(other.m_ReadOffset)) { }
DataBuffer::DataBuffer(const std::string& str) : m_Buffer(str.begin(), str.end()) { } DataBuffer::DataBuffer(const std::string& str) : m_Buffer(str.begin(), str.end()), m_ReadOffset(0) { }
DataBuffer::DataBuffer(const DataBuffer& other, std::size_t offset) {
m_Buffer.reserve(other.GetSize() - offset); DataBuffer::DataBuffer(const DataBuffer& other, Data::difference_type offset) : m_ReadOffset(0) {
m_Buffer.reserve(other.GetSize() - static_cast<std::size_t>(offset));
std::copy(other.m_Buffer.begin() + offset, other.m_Buffer.end(), std::back_inserter(m_Buffer)); std::copy(other.m_Buffer.begin() + offset, other.m_Buffer.end(), std::back_inserter(m_Buffer));
} }
@@ -58,7 +59,7 @@ DataBuffer::const_iterator DataBuffer::end() const {
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer) { std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer) {
for (unsigned char u : buffer) for (unsigned char u : buffer)
os << std::hex << std::setfill('0') << std::setw(2) << (int)u << " "; os << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(u) << " ";
os << std::dec; os << std::dec;
return os; return os;
} }
@@ -81,7 +82,7 @@ bool DataBuffer::ReadFile(const std::string& fileName) {
bool DataBuffer::WriteFile(const std::string& fileName) { bool DataBuffer::WriteFile(const std::string& fileName) {
try { try {
std::ofstream file(fileName, std::ostream::binary); std::ofstream file(fileName, std::ostream::binary);
file.write((const char*)m_Buffer.data(), m_Buffer.size()); file.write(reinterpret_cast<const char*>(m_Buffer.data()), static_cast<std::streamsize>(m_Buffer.size()));
file.flush(); file.flush();
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Failed to write file \"" << fileName << "\" reason : " << e.what() << std::endl; std::cerr << "Failed to write file \"" << fileName << "\" reason : " << e.what() << std::endl;

View File

@@ -33,7 +33,8 @@ NetworkInitializer initializer;
IPAddresses Dns::Resolve(const std::string& host) { IPAddresses Dns::Resolve(const std::string& host) {
IPAddresses list; IPAddresses list;
addrinfo hints = { 0 }, * addresses; addrinfo hints{};
addrinfo* addresses = nullptr;
//hints.ai_family = AF_UNSPEC; //hints.ai_family = AF_UNSPEC;
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
@@ -54,7 +55,7 @@ IPAddresses Dns::Resolve(const std::string& host) {
#else #else
char straddr[512]; char straddr[512];
inet_ntop(p->ai_family, &((sockaddr_in*)p->ai_addr)->sin_addr, straddr, sizeof(straddr)); inet_ntop(p->ai_family, &(reinterpret_cast<sockaddr_in*>(p->ai_addr))->sin_addr, straddr, sizeof(straddr));
#endif #endif
list.push_back(IPAddress(straddr)); list.push_back(IPAddress(straddr));

View File

@@ -16,7 +16,7 @@ Socket::Socket(Type type)
: m_Blocking(false), : m_Blocking(false),
m_Type(type), m_Type(type),
m_Status(Disconnected), m_Status(Disconnected),
m_Handle((SocketHandle)INVALID_SOCKET) m_Handle(static_cast<SocketHandle>(INVALID_SOCKET))
{ {
} }

View File

@@ -13,10 +13,8 @@
namespace td { namespace td {
namespace network { namespace network {
TCPSocket::TCPSocket() TCPSocket::TCPSocket() : Socket(Socket::TCP), m_Port(0) {
: Socket(Socket::TCP), m_Port(0) m_Handle = static_cast<SocketHandle>(INVALID_SOCKET);
{
m_Handle = (SocketHandle)INVALID_SOCKET;
} }
bool TCPSocket::Connect(const IPAddress& address, unsigned short port) { bool TCPSocket::Connect(const IPAddress& address, unsigned short port) {
@@ -32,7 +30,7 @@ bool TCPSocket::Connect(const IPAddress& address, unsigned short port) {
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
if (::getaddrinfo(address.ToString().c_str(), std::to_string(port).c_str(), &hints, &result) != 0) { if (::getaddrinfo(address.ToString().c_str(), std::to_string(static_cast<int>(port)).c_str(), &hints, &result) != 0) {
std::cerr << "Failed to get address info !\n"; std::cerr << "Failed to get address info !\n";
return false; return false;
} }
@@ -44,8 +42,8 @@ bool TCPSocket::Connect(const IPAddress& address, unsigned short port) {
struct addrinfo* ptr = nullptr; struct addrinfo* ptr = nullptr;
for (ptr = result; ptr != nullptr; ptr = ptr->ai_next) { for (ptr = result; ptr != nullptr; ptr = ptr->ai_next) {
struct sockaddr_in* sockaddr = (struct sockaddr_in*)ptr->ai_addr; struct sockaddr* sockaddr = ptr->ai_addr;
if (::connect(m_Handle, (struct sockaddr*)sockaddr, sizeof(struct sockaddr_in)) != 0) { if (::connect(m_Handle, sockaddr, sizeof(sockaddr_in)) != 0) {
std::cerr << "Failed to connect with this address !\n"; std::cerr << "Failed to connect with this address !\n";
continue; continue;
} }
@@ -78,7 +76,7 @@ size_t TCPSocket::Send(const unsigned char* data, size_t size) {
Disconnect(); Disconnect();
return 0; return 0;
} }
sent += cur; sent += static_cast<std::size_t>(cur);
} }
return sent; return sent;
@@ -88,7 +86,7 @@ std::size_t TCPSocket::Receive(DataBuffer& buffer, std::size_t amount) {
buffer.Resize(amount); buffer.Resize(amount);
buffer.SetReadOffset(0); buffer.SetReadOffset(0);
int recvAmount = ::recv(m_Handle, (char*)&buffer[0], amount, 0); int recvAmount = ::recv(m_Handle, reinterpret_cast<char*>(buffer.data()), amount, 0);
if (recvAmount <= 0) { if (recvAmount <= 0) {
#if defined(_WIN32) || defined(WIN32) #if defined(_WIN32) || defined(WIN32)
int err = WSAGetLastError(); int err = WSAGetLastError();
@@ -104,8 +102,8 @@ std::size_t TCPSocket::Receive(DataBuffer& buffer, std::size_t amount) {
buffer.Clear(); buffer.Clear();
return 0; return 0;
} }
buffer.Resize(recvAmount); buffer.Resize(static_cast<std::size_t>(recvAmount));
return recvAmount; return static_cast<std::size_t>(recvAmount);
} }
DataBuffer TCPSocket::Receive(std::size_t amount) { DataBuffer TCPSocket::Receive(std::size_t amount) {
@@ -126,7 +124,7 @@ DataBuffer TCPSocket::Receive(std::size_t amount) {
return DataBuffer(); return DataBuffer();
} }
return DataBuffer(std::string(buf.get(), received)); return DataBuffer(std::string(buf.get(), static_cast<std::size_t>(received)));
} }
TCPSocket::TCPSocket(TCPSocket&& other) : Socket(TCP) { TCPSocket::TCPSocket(TCPSocket&& other) : Socket(TCP) {
@@ -136,12 +134,12 @@ TCPSocket::TCPSocket(TCPSocket&& other) : Socket(TCP) {
m_RemoteIP = other.m_RemoteIP; m_RemoteIP = other.m_RemoteIP;
SetStatus(other.GetStatus()); SetStatus(other.GetStatus());
SetBlocking(other.IsBlocking()); SetBlocking(other.IsBlocking());
other.m_Handle = (SocketHandle)INVALID_SOCKET; other.m_Handle = static_cast<SocketHandle>(INVALID_SOCKET);
} }
void SendPacket(const DataBuffer& data, network::TCPSocket& socket) { void SendPacket(const DataBuffer& data, network::TCPSocket& socket) {
DataBuffer compressed = utils::Compress(data); DataBuffer compressed = utils::Compress(data);
socket.Send((const std::uint8_t*)compressed.ToString().data(), compressed.GetSize()); socket.Send(reinterpret_cast<const std::uint8_t*>(compressed.ToString().data()), compressed.GetSize());
} }

View File

@@ -14,7 +14,7 @@ namespace network {
UDPSocket::UDPSocket() UDPSocket::UDPSocket()
: Socket(Socket::UDP), m_Port(0) : Socket(Socket::UDP), m_Port(0)
{ {
m_Handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); m_Handle = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
} }
bool UDPSocket::Connect(const IPAddress& address, unsigned short port) { bool UDPSocket::Connect(const IPAddress& address, unsigned short port) {
@@ -31,20 +31,20 @@ bool UDPSocket::Connect(const IPAddress& address, unsigned short port) {
return true; return true;
} }
size_t UDPSocket::Send(const unsigned char* data, size_t size) { std::size_t UDPSocket::Send(const unsigned char* data, std::size_t size) {
size_t sent = 0; std::size_t sent = 0;
if (this->GetStatus() != Connected) if (this->GetStatus() != Connected)
return 0; return 0;
while (sent < size) { while (sent < size) {
int cur = sendto(m_Handle, reinterpret_cast<const char*>(data + sent), size - sent, 0, int cur = ::sendto(m_Handle, reinterpret_cast<const char*>(data + sent), size - sent, 0,
reinterpret_cast<sockaddr*>(&m_RemoteAddr), sizeof(sockaddr_in)); reinterpret_cast<sockaddr*>(&m_RemoteAddr), sizeof(sockaddr_in));
if (cur <= 0) { if (cur <= 0) {
Disconnect(); Disconnect();
return 0; return 0;
} }
sent += cur; sent += static_cast<std::size_t>(cur);
} }
return sent; return sent;
@@ -54,7 +54,7 @@ DataBuffer UDPSocket::Receive(std::size_t amount) {
std::unique_ptr<char[]> buf(new char[amount]); std::unique_ptr<char[]> buf(new char[amount]);
socklen_t slen = sizeof(sockaddr_in); socklen_t slen = sizeof(sockaddr_in);
int received = recvfrom(m_Handle, buf.get(), amount, 0, int received = ::recvfrom(m_Handle, buf.get(), amount, 0,
reinterpret_cast<sockaddr*>(&m_RemoteAddr), &slen); reinterpret_cast<sockaddr*>(&m_RemoteAddr), &slen);
if (received <= 0) { if (received <= 0) {
@@ -64,13 +64,40 @@ DataBuffer UDPSocket::Receive(std::size_t amount) {
int err = errno; int err = errno;
#endif #endif
if (err == WOULDBLOCK) if (err == WOULDBLOCK)
return DataBuffer(std::string(buf.get(), received)); return DataBuffer(std::string(buf.get(), static_cast<std::size_t>(received)));
Disconnect(); Disconnect();
return DataBuffer(); return DataBuffer();
} }
return DataBuffer(std::string(buf.get(), received)); return DataBuffer(std::string(buf.get(), static_cast<std::size_t>(received)));
}
std::size_t UDPSocket::Receive(DataBuffer& buffer, std::size_t amount) {
buffer.Resize(amount);
buffer.SetReadOffset(0);
socklen_t slen = sizeof(sockaddr_in);
int recvAmount = ::recvfrom(m_Handle, reinterpret_cast<char*>(buffer.data()), amount, 0,
reinterpret_cast<sockaddr*>(&m_RemoteAddr), &slen);
if (recvAmount <= 0) {
#if defined(_WIN32) || defined(WIN32)
int err = WSAGetLastError();
#else
int err = errno;
#endif
if (err == WOULDBLOCK) {
buffer.Clear();
return 0;
}
Disconnect();
buffer.Clear();
return 0;
}
buffer.Resize(static_cast<std::size_t>(recvAmount));
return static_cast<std::size_t>(recvAmount);
} }
} // ns network } // ns network

View File

@@ -192,7 +192,7 @@ DataBuffer WorldDataPacket::SerializeCustom() const {
game::ChunkCoord coords = pair.first; game::ChunkCoord coords = pair.first;
game::ChunkPtr chunk = pair.second; game::ChunkPtr chunk = pair.second;
data << coords.first << coords.second << (std::uint64_t)chunk->palette.size(); data << coords.x << coords.y << (std::uint64_t)chunk->palette.size();
std::size_t bufferSize = data.GetSize(); std::size_t bufferSize = data.GetSize();
data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type)); data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type));
@@ -200,7 +200,7 @@ DataBuffer WorldDataPacket::SerializeCustom() const {
std::uint8_t bitsPerTile = countBits(chunk->palette.size()); std::uint8_t bitsPerTile = countBits(chunk->palette.size());
game::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0);
@@ -235,7 +235,7 @@ DataBuffer WorldDataPacket::Serialize() const {
game::ChunkCoord coords = pair.first; game::ChunkCoord coords = pair.first;
game::ChunkPtr chunk = pair.second; game::ChunkPtr chunk = pair.second;
data << coords.first << coords.second << (std::uint64_t)chunk->palette.size(); data << coords.x << coords.y << (std::uint64_t)chunk->palette.size();
std::size_t bufferSize = data.GetSize(); std::size_t bufferSize = data.GetSize();
data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type)); data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type));
@@ -243,7 +243,7 @@ DataBuffer WorldDataPacket::Serialize() const {
std::uint8_t bitsPerTile = countBits(chunk->palette.size()); std::uint8_t bitsPerTile = countBits(chunk->palette.size());
game::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0);
@@ -278,7 +278,7 @@ void WorldDataPacket::Deserialize(DataBuffer& data) {
for (std::uint64_t chunkNumber = 0; chunkNumber < chunkCount; chunkNumber++) { for (std::uint64_t chunkNumber = 0; chunkNumber < chunkCount; chunkNumber++) {
game::ChunkPtr chunk = std::make_shared<game::Chunk>(); game::ChunkPtr chunk = std::make_shared<game::Chunk>();
game::ChunkCoord::first_type chunkX, chunkY; decltype(game::ChunkCoord::x) chunkX, chunkY;
data >> chunkX >> chunkY; data >> chunkX >> chunkY;
std::uint64_t chunkPaletteSize; std::uint64_t chunkPaletteSize;
@@ -294,7 +294,7 @@ void WorldDataPacket::Deserialize(DataBuffer& data) {
std::uint8_t bitsPerTile = countBits(chunkPaletteSize); std::uint8_t bitsPerTile = countBits(chunkPaletteSize);
// A bitmask that contains bitsPerTile set bits // A bitmask that contains bitsPerTile set bits
game::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0);
@@ -306,7 +306,7 @@ void WorldDataPacket::Deserialize(DataBuffer& data) {
int startOffset = (tileNumber * bitsPerTile) % BITS_IN_LONG; int startOffset = (tileNumber * bitsPerTile) % BITS_IN_LONG;
int endLong = ((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG; int endLong = ((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG;
game::ChunkData::value_type value; game::Chunk::ChunkData::value_type value;
if (startLong == endLong) { if (startLong == endLong) {
value = (chunkData[startLong] >> startOffset); value = (chunkData[startLong] >> startOffset);
} else { } else {

View File

@@ -56,8 +56,8 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
const td::game::ChunkCoord& coords = chunkInfo.first; const td::game::ChunkCoord& coords = chunkInfo.first;
td::game::ChunkPtr chunk = chunkInfo.second; td::game::ChunkPtr chunk = chunkInfo.second;
std::int32_t chunkX = coords.first * td::game::Chunk::ChunkWidth; std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth;
std::int32_t chunkY = coords.second * td::game::Chunk::ChunkHeight; std::int32_t chunkY = coords.y * td::game::Chunk::ChunkHeight;
for (int tileY = 0; tileY < td::game::Chunk::ChunkHeight; tileY++) { for (int tileY = 0; tileY < td::game::Chunk::ChunkHeight; tileY++) {
for (int tileX = 0; tileX < td::game::Chunk::ChunkWidth; tileX++) { for (int tileX = 0; tileX < td::game::Chunk::ChunkWidth; tileX++) {