pxl8/demo3d/client/net/demo3d_protocol.h

144 lines
3.9 KiB
C
Raw Normal View History

#pragma once
#include "pxl8_types.h"
#ifdef __cplusplus
extern "C" {
#endif
#define DEMO3D_PROTOCOL_VERSION 1
#define DEMO3D_MAX_SNAPSHOT_ENTITIES 256
#define DEMO3D_MAX_SNAPSHOT_EVENTS 32
#define DEMO3D_COMMAND_PAYLOAD_SIZE 64
#define DEMO3D_EVENT_PAYLOAD_SIZE 15
typedef enum demo3d_msg_type {
DEMO3D_MSG_NONE = 0,
DEMO3D_MSG_CHUNK,
DEMO3D_MSG_CHUNK_ENTER,
DEMO3D_MSG_CHUNK_EXIT,
DEMO3D_MSG_COMMAND,
DEMO3D_MSG_CONNECT,
DEMO3D_MSG_DISCONNECT,
DEMO3D_MSG_EVENT,
DEMO3D_MSG_INPUT,
DEMO3D_MSG_SNAPSHOT
} demo3d_msg_type;
typedef struct demo3d_msg_header {
u32 sequence;
u16 size;
u8 type;
u8 version;
} demo3d_msg_header;
typedef enum demo3d_cmd_type {
DEMO3D_CMD_NONE = 0,
DEMO3D_CMD_SPAWN_ENTITY,
} demo3d_cmd_type;
typedef struct demo3d_input_msg {
u32 buttons;
f32 look_dx;
f32 look_dy;
f32 move_x;
f32 move_y;
f32 yaw;
u64 tick;
u64 timestamp;
} demo3d_input_msg;
typedef struct demo3d_command_msg {
u16 cmd_type;
u8 payload[DEMO3D_COMMAND_PAYLOAD_SIZE];
u16 payload_size;
u64 tick;
} demo3d_command_msg;
typedef struct demo3d_entity_state {
u64 entity_id;
u8 userdata[56];
} demo3d_entity_state;
typedef struct demo3d_event_msg {
u8 event_type;
u8 payload[DEMO3D_EVENT_PAYLOAD_SIZE];
} demo3d_event_msg;
typedef struct demo3d_snapshot_header {
u16 entity_count;
u16 event_count;
u64 player_id;
u64 tick;
f32 time;
} demo3d_snapshot_header;
#define DEMO3D_CHUNK_TYPE_BSP 1
#define DEMO3D_CHUNK_FLAG_FINAL 0x04
#define DEMO3D_CHUNK_MAX_PAYLOAD 1400
typedef struct demo3d_chunk_msg_header {
u8 chunk_type;
u8 flags;
u8 fragment_idx;
u8 fragment_count;
u32 id;
i32 cx, cy, cz;
u32 version;
u16 payload_size;
u16 reserved;
} demo3d_chunk_msg_header;
typedef struct demo3d_bsp_wire_header {
u32 num_vertices;
u32 num_edges;
u32 num_faces;
u32 num_planes;
u32 num_nodes;
u32 num_leafs;
u32 num_surfedges;
u32 num_marksurfaces;
u32 num_cell_portals;
u32 visdata_size;
u32 num_vertex_lights;
u32 num_heightfield;
} demo3d_bsp_wire_header;
usize demo3d_protocol_serialize_header(const demo3d_msg_header* msg, u8* buf, usize len);
usize demo3d_protocol_deserialize_header(const u8* buf, usize len, demo3d_msg_header* msg);
usize demo3d_protocol_serialize_input(const demo3d_input_msg* msg, u8* buf, usize len);
usize demo3d_protocol_deserialize_input(const u8* buf, usize len, demo3d_input_msg* msg);
usize demo3d_protocol_serialize_command(const demo3d_command_msg* msg, u8* buf, usize len);
usize demo3d_protocol_deserialize_command(const u8* buf, usize len, demo3d_command_msg* msg);
usize demo3d_protocol_serialize_entity_state(const demo3d_entity_state* state, u8* buf, usize len);
usize demo3d_protocol_deserialize_entity_state(const u8* buf, usize len, demo3d_entity_state* state);
usize demo3d_protocol_serialize_event(const demo3d_event_msg* msg, u8* buf, usize len);
usize demo3d_protocol_deserialize_event(const u8* buf, usize len, demo3d_event_msg* msg);
usize demo3d_protocol_serialize_snapshot_header(const demo3d_snapshot_header* hdr, u8* buf, usize len);
usize demo3d_protocol_deserialize_snapshot_header(const u8* buf, usize len, demo3d_snapshot_header* hdr);
usize demo3d_protocol_serialize_chunk_msg_header(const demo3d_chunk_msg_header* hdr, u8* buf, usize len);
usize demo3d_protocol_deserialize_chunk_msg_header(const u8* buf, usize len, demo3d_chunk_msg_header* hdr);
usize demo3d_protocol_deserialize_bsp_wire_header(const u8* buf, usize len, demo3d_bsp_wire_header* hdr);
typedef struct demo3d_chunk_enter_msg {
i32 cx;
i32 cz;
} demo3d_chunk_enter_msg;
usize demo3d_protocol_serialize_chunk_enter(const demo3d_chunk_enter_msg* msg, u8* buf, usize len);
usize demo3d_protocol_deserialize_chunk_enter(const u8* buf, usize len, demo3d_chunk_enter_msg* msg);
u32 demo3d_chunk_hash(i32 cx, i32 cz);
#ifdef __cplusplus
}
#endif