#include "PacketTest.hpp"
-#include "model/CompositeModel.hpp"
+#include "model/Model.hpp"
#include "world/Entity.hpp"
-#include <limits>
-
CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::PacketTest);
using namespace std;
}
+void PacketTest::testSizes() {
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "unexpected size of vec3",
+ size_t(12), sizeof(glm::vec3)
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "unexpected size of vec3i",
+ size_t(12), sizeof(glm::ivec3)
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "unexpected size of quat",
+ size_t(16), sizeof(glm::quat)
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "unexpected size of entity state",
+ size_t(64), sizeof(EntityState)
+ );
+}
+
void PacketTest::testControl() {
- Packet::TControl ctrl;
- ctrl.ack = 10;
+ Packet::TControl ctrl{ 0, 10, 0 };
CPPUNIT_ASSERT_MESSAGE(
"TControl should ack the packet in the ack field",
void PacketTest::testPlayerUpdate() {
auto pack = Packet::Make<Packet::PlayerUpdate>(udp_pack);
- AssertPacket("PlayerUpdate", 4, 64, pack);
+ AssertPacket("PlayerUpdate", 4, 76, pack);
+
+ EntityState write_state;
+ write_state.chunk_pos = { 7, 2, -3 };
+ write_state.block_pos = { 1.5f, 0.9f, 12.0f };
+ write_state.velocity = { 0.025f, 0.001f, 0.0f };
+ write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
+ write_state.ang_vel = { 0.01f, 0.00302f, 0.0985f };
+ glm::vec3 write_movement(0.5f, -1.0f, 1.0f);
+ float write_pitch = 1.25f;
+ float write_yaw = -2.5f;
+ uint8_t write_actions = 0x05;
+ uint8_t write_slot = 3;
+ pack.WritePredictedState(write_state);
+ pack.WriteMovement(write_movement);
+ pack.WritePitch(write_pitch);
+ pack.WriteYaw(write_yaw);
+ pack.WriteActions(write_actions);
+ pack.WriteSlot(write_slot);
- Entity write_entity;
- write_entity.ID(534574);
- write_entity.GetState().chunk_pos = { 7, 2, -3 };
- write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
- write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
- write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
- write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
EntityState read_state;
- pack.WritePlayer(write_entity);
-
- pack.ReadPlayerState(read_state);
+ glm::vec3 read_movement;
+ float read_pitch;
+ float read_yaw;
+ uint8_t read_actions;
+ uint8_t read_slot;
+ pack.ReadPredictedState(read_state);
+ pack.ReadMovement(read_movement);
+ pack.ReadPitch(read_pitch);
+ pack.ReadYaw(read_yaw);
+ pack.ReadActions(read_actions);
+ pack.ReadSlot(read_slot);
AssertEqual(
- "player entity state not correctly transported in PlayerUpdate packet",
- write_entity.GetState(), read_state
+ "player predicted entity state not correctly transported in PlayerUpdate packet",
+ write_state, read_state
+ );
+ AssertEqual(
+ "player movement input not correctly transported in PlayerUpdate packet",
+ write_movement, read_movement, 0.0001f
+ );
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+ "player pitch input not correctly transported in PlayerUpdate packet",
+ write_pitch, read_pitch, 0.0001f
+ );
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+ "player yaw input not correctly transported in PlayerUpdate packet",
+ write_yaw, read_yaw, 0.0001f
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "player actions not correctly transported in PlayerUpdate packet",
+ int(write_actions), int(read_actions)
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "player inventory slot not correctly transported in PlayerUpdate packet",
+ int(write_slot), int(read_slot)
);
}
Entity write_entity;
write_entity.ID(534574);
- CompositeModel model;
+ Model model;
model.ID(23);
model.Instantiate(write_entity.GetModel());
write_entity.GetState().chunk_pos = { 7, 2, -3 };
void PacketTest::testEntityUpdate() {
auto pack = Packet::Make<Packet::EntityUpdate>(udp_pack);
- AssertPacket("EntityUpdate", 7, 4, 452, pack);
+ AssertPacket("EntityUpdate", 7, 4, 480, pack);
pack.length = Packet::EntityUpdate::GetSize(3);
CPPUNIT_ASSERT_EQUAL_MESSAGE(
- "length not correctly set in DespawnEntity packet",
+ "length not correctly set in EntityUpdate packet",
size_t(4 + 3 * 68), pack.length
);
);
}
+void PacketTest::testPlayerCorrection() {
+ auto pack = Packet::Make<Packet::PlayerCorrection>(udp_pack);
+ AssertPacket("PlayerCorrection", 8, 66, pack);
+
+ uint16_t write_seq = 50050;
+ uint16_t read_seq;
+ pack.WritePacketSeq(write_seq);
+ pack.ReadPacketSeq(read_seq);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "packet sequence not correctly transported in PlayerCorrection packet",
+ write_seq, read_seq
+ );
+
+ Entity write_entity;
+ write_entity.GetState().chunk_pos = { 7, 2, -3 };
+ write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
+ write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
+ write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
+ write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
+ pack.WritePlayer(write_entity);
+
+ EntityState read_state;
+ pack.ReadPlayerState(read_state);
+ AssertEqual(
+ "entity state not correctly transported in PlayerCorrection packet",
+ write_entity.GetState(), read_state
+ );
+}
+
+void PacketTest::testChunkBegin() {
+ auto pack = Packet::Make<Packet::ChunkBegin>(udp_pack);
+ AssertPacket("ChunkBegin", 9, 24, pack);
+
+ uint32_t write_id = 532;
+ uint32_t write_flags = 9864328;
+ glm::ivec3 write_pos = { -6, 15, 38 };
+ uint32_t write_size = 4097;
+
+ pack.WriteTransmissionId(write_id);
+ pack.WriteFlags(write_flags);
+ pack.WriteChunkCoords(write_pos);
+ pack.WriteDataSize(write_size);
+
+ uint32_t read_id;
+ uint32_t read_flags;
+ glm::ivec3 read_pos;
+ uint32_t read_size;
+
+ pack.ReadTransmissionId(read_id);
+ pack.ReadFlags(read_flags);
+ pack.ReadChunkCoords(read_pos);
+ pack.ReadDataSize(read_size);
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "transmission ID not correctly transported in ChunkBegin packet",
+ write_id, read_id
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "flags not correctly transported in ChunkBegin packet",
+ write_flags, read_flags
+ );
+ AssertEqual(
+ "chunk coordinates not correctly transported in ChunkBegin packet",
+ write_pos, read_pos
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "data size not correctly transported in ChunkBegin packet",
+ write_size, read_size
+ );
+}
+
+void PacketTest::testChunkData() {
+ auto pack = Packet::Make<Packet::ChunkData>(udp_pack);
+ AssertPacket("ChunkData", 10, 12, 484, pack);
+
+ constexpr size_t block_size = 97;
+
+ uint32_t write_id = 6743124;
+ uint32_t write_offset = 8583;
+ uint32_t write_size = block_size;
+ uint8_t write_data[block_size];
+ memset(write_data, 'X', block_size);
+
+ pack.WriteTransmissionId(write_id);
+ pack.WriteDataOffset(write_offset);
+ pack.WriteDataSize(write_size);
+ pack.WriteData(write_data, write_size);
+
+ uint32_t read_id;
+ uint32_t read_offset;
+ uint32_t read_size;
+ uint8_t read_data[block_size];
+
+ pack.ReadTransmissionId(read_id);
+ pack.ReadDataOffset(read_offset);
+ pack.ReadDataSize(read_size);
+ pack.ReadData(read_data, read_size);
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "transmission ID not correctly transported in ChunkData packet",
+ write_id, read_id
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "data offset not correctly transported in ChunkData packet",
+ write_offset, read_offset
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "data size not correctly transported in ChunkData packet",
+ write_size, read_size
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "raw data not correctly transported in ChunkData packet",
+ string(write_data, write_data + write_size), string(read_data, read_data + read_size)
+ );
+}
+
+void PacketTest::testBlockUpdate() {
+ auto pack = Packet::Make<Packet::BlockUpdate>(udp_pack);
+ AssertPacket("BlockUpdate", 11, 16, 484, pack);
+
+ pack.length = Packet::BlockUpdate::GetSize(3);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "length not correctly set in BlockUpdate packet",
+ size_t(16 + 3 * 6), pack.length
+ );
+
+ glm::ivec3 write_coords(432, -325, 99998);
+ uint32_t write_count = 3;
+ uint16_t write_index = 432;
+ Block write_block(324, Block::FACE_DOWN, Block::TURN_AROUND);
+
+ pack.WriteChunkCoords(write_coords);
+ pack.WriteBlockCount(write_count);
+ pack.WriteIndex(write_index, 1);
+ pack.WriteBlock(write_block, 1);
+ pack.WriteIndex(write_index, 0);
+ pack.WriteBlock(write_block, 0);
+ pack.WriteIndex(write_index, 2);
+ pack.WriteBlock(write_block, 2);
+
+ glm::ivec3 read_coords;
+ uint32_t read_count;
+ uint16_t read_index;
+ Block read_block;
+
+ pack.ReadChunkCoords(read_coords);
+ pack.ReadBlockCount(read_count);
+ pack.ReadIndex(read_index, 1);
+ pack.ReadBlock(read_block, 1);
+
+ AssertEqual(
+ "chunk coordinates not correctly transported in BlockUpdate packet",
+ write_coords, read_coords
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "block count not correctly transported in BlockUpdate packet",
+ write_count, read_count
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "block index not correctly transported in BlockUpdate packet",
+ write_index, read_index
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "block type not correctly transported in BlockUpdate packet",
+ write_block.type, read_block.type
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "block face not correctly transported in BlockUpdate packet",
+ write_block.GetFace(), read_block.GetFace()
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "block turn not correctly transported in BlockUpdate packet",
+ write_block.GetTurn(), read_block.GetTurn()
+ );
+}
+
void PacketTest::AssertPacket(
const string &name,
void PacketTest::AssertEqual(
const string &message,
const glm::vec3 &expected,
- const glm::vec3 &actual
+ const glm::vec3 &actual,
+ float epsilon
) {
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
message + " (X component)",
- expected.x, actual.x, numeric_limits<float>::epsilon()
+ expected.x, actual.x, epsilon
);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
message + " (Y component)",
- expected.y, actual.y, numeric_limits<float>::epsilon()
+ expected.y, actual.y, epsilon
);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
message + " (Z component)",
- expected.z, actual.z, numeric_limits<float>::epsilon()
+ expected.z, actual.z, epsilon
);
}