DEADSOFTWARE

Первая версия протестированная на сервере
[netwar.git] / protocol.h
1 #ifndef PROTOCOL_H_INCLUDED
2 #define PROTOCOL_H_INCLUDED
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <string.h>
8 #define DEFAULT_PORT 29386
9 #define PROTOCOL_VERSION 0
10 #define PROTOCOL_F8FRAC (1 << 7)
12 #define PACKED __attribute__((__packed__))
14 typedef enum {
15 INVALID = 0,
17 CL_INFO = 1,
18 CL_KILL = 2,
19 CL_DOES = 3,
21 SV_INFO = 128,
22 SV_KILL = 139,
23 SV_SPLR = 130,
24 } MessageType;
26 typedef enum {
27 DOES_UP = 0,
28 DOES_DOWN = 1,
29 DOES_LEFT = 2,
30 DOES_RIGHT = 3,
31 DOES_FIRE = 4,
32 } DoesCode;
34 typedef struct PACKED ClInfo {
35 uint8_t type;
36 uint8_t version;
37 uint8_t name[32];
38 } ClInfo;
40 typedef struct PACKED ClKill {
41 uint8_t type;
42 } ClKill;
44 typedef struct PACKED ClDoes {
45 uint8_t type;
46 uint8_t code;
47 } ClDoes;
49 typedef struct PACKED SvInfo {
50 uint8_t type;
51 uint8_t version;
52 uint8_t clientid;
53 uint8_t maxclients;
54 uint8_t name[32];
55 } SvInfo;
57 typedef struct PACKED SvKill {
58 uint8_t type;
59 uint8_t message[256];
60 } SvKill;
62 typedef struct PACKED SvSplr {
63 uint8_t type;
64 uint8_t clid;
65 uint8_t live;
66 uint8_t x, y, r;
67 uint8_t vx, vy, vr;
68 } SvSplr;
70 typedef union ClMessage {
71 uint8_t type;
72 ClInfo info;
73 ClKill kill;
74 ClDoes does;
75 } ClMessage;
77 typedef union SvMessage {
78 uint8_t type;
79 SvInfo info;
80 SvKill kill;
81 SvSplr splr;
82 } SvMessage;
84 typedef union ProtocolMessage {
85 uint8_t type;
86 ClMessage cl;
87 SvMessage sv;
88 } ProtocolMessage;
90 static inline int8_t f2b(float x) { return x * PROTOCOL_F8FRAC; }
92 static inline float b2f(int8_t x) { return (float) x / PROTOCOL_F8FRAC; }
94 static inline int MessageTypeSize(MessageType type) {
95 switch(type) {
96 case CL_INFO: return sizeof(ClInfo);
97 case CL_KILL: return sizeof(ClKill);
98 case CL_DOES: return sizeof(ClDoes);
99 case SV_INFO: return sizeof(SvInfo);
100 case SV_KILL: return sizeof(SvKill);
101 case SV_SPLR: return sizeof(SvSplr);
102 default: return sizeof(ProtocolMessage);
106 static inline int MessageSize(ProtocolMessage m) {
107 return MessageTypeSize(m.type);
110 static inline ProtocolMessage cl_info(const char * name) {
111 ClInfo m = {
112 .type = CL_INFO,
113 .version = PROTOCOL_VERSION,
114 };
115 strncpy((char*)m.name, name, sizeof(m.name));
116 return (ProtocolMessage) (ClMessage) m;
119 static inline ProtocolMessage cl_kill() {
120 return (ProtocolMessage) (ClMessage) (ClKill) {
121 .type = CL_KILL,
122 };
125 static inline ProtocolMessage cl_does(DoesCode code) {
126 return (ProtocolMessage) (ClMessage) (ClDoes) {
127 .type = CL_DOES,
128 .code = code,
129 };
132 static inline ProtocolMessage sv_info(const char * name, int clientid, int maxclients) {
133 SvInfo m = {
134 .type = SV_INFO,
135 .version = PROTOCOL_VERSION,
136 .clientid = clientid,
137 .maxclients = maxclients,
138 };
139 strncpy((char*) m.name, name, sizeof(m.name));
140 return (ProtocolMessage) (SvMessage) m;
143 static inline ProtocolMessage sv_kill(const char * msg) {
144 SvKill m = {
145 .type = SV_KILL,
146 };
147 strncpy((char*) m.message, msg, sizeof(m.message));
148 return (ProtocolMessage) (SvMessage) m;
151 static inline ProtocolMessage sv_splr(int clid, bool live, float x, float y, float r, float vx, float vy, float vr) {
152 return (ProtocolMessage) (SvMessage) (SvSplr) {
153 .type = SV_SPLR,
154 .clid = clid,
155 .live = live,
156 .x = f2b(x),
157 .y = f2b(y),
158 .r = f2b(r),
159 .vx = f2b(vx),
160 .vy = f2b(vy),
161 .vr = f2b(vr),
162 };
165 #undef PACKED
167 #endif /* PROTOCOL_H_INCLUDED */