1 #ifndef PROTOCOL_H_INCLUDED
2 #define PROTOCOL_H_INCLUDED
8 #define DEFAULT_PORT 29386
9 #define PROTOCOL_VERSION 1
10 #define PROTOCOL_F8FRAC (1 << 7)
12 #define PACKED __attribute__((__packed__))
35 typedef struct PACKED ClInfo
{
41 typedef struct PACKED ClKill
{
45 typedef struct PACKED ClDoes
{
50 typedef struct PACKED SvInfo
{
58 typedef struct PACKED SvKill
{
63 typedef struct PACKED SvSplr
{
71 typedef struct PACKED SvSbul
{
81 typedef union ClMessage
{
88 typedef union SvMessage
{
96 typedef union ProtocolMessage
{
102 static inline int8_t f2b(float x
) { return x
* PROTOCOL_F8FRAC
; }
104 static inline float b2f(int8_t x
) { return (float) x
/ PROTOCOL_F8FRAC
; }
106 static inline int MessageTypeSize(MessageType type
) {
108 case CL_INFO
: return sizeof(ClInfo
);
109 case CL_KILL
: return sizeof(ClKill
);
110 case CL_DOES
: return sizeof(ClDoes
);
111 case SV_INFO
: return sizeof(SvInfo
);
112 case SV_KILL
: return sizeof(SvKill
);
113 case SV_SPLR
: return sizeof(SvSplr
);
114 case SV_SBUL
: return sizeof(SvSbul
);
115 default: return sizeof(ProtocolMessage
);
119 static inline int MessageSize(ProtocolMessage m
) {
120 return MessageTypeSize(m
.type
);
123 static inline ProtocolMessage
cl_info(const char * name
) {
126 .version
= PROTOCOL_VERSION
,
128 strncpy((char*)m
.name
, name
, sizeof(m
.name
));
129 return (ProtocolMessage
) (ClMessage
) m
;
132 static inline ProtocolMessage
cl_kill() {
133 return (ProtocolMessage
) (ClMessage
) (ClKill
) {
138 static inline ProtocolMessage
cl_does(DoesCode code
) {
139 return (ProtocolMessage
) (ClMessage
) (ClDoes
) {
145 static inline ProtocolMessage
sv_info(const char * name
, int clientid
, int maxclients
) {
148 .version
= PROTOCOL_VERSION
,
149 .clientid
= clientid
,
150 .maxclients
= maxclients
,
152 strncpy((char*) m
.name
, name
, sizeof(m
.name
));
153 return (ProtocolMessage
) (SvMessage
) m
;
156 static inline ProtocolMessage
sv_kill(const char * msg
) {
160 strncpy((char*) m
.message
, msg
, sizeof(m
.message
));
161 return (ProtocolMessage
) (SvMessage
) m
;
164 static inline ProtocolMessage
sv_splr(int clid
, bool live
, float x
, float y
, float r
, float vx
, float vy
, float vr
) {
165 return (ProtocolMessage
) (SvMessage
) (SvSplr
) {
178 static inline ProtocolMessage
sv_sbul(int id
, int owner
, bool live
, float x
, float y
, float vx
, float vy
, int tick
) {
179 return (ProtocolMessage
) (SvMessage
) (SvSbul
) {
194 #endif /* PROTOCOL_H_INCLUDED */