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 PROTOCOL_PORT 29386
9 #define PROTOCOL_VERSION 3
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 SV_SBUL = 131,
25 } MessageType;
27 typedef union DoesBits {
28 uint8_t bits;
29 struct PACKED {
30 uint8_t up : 1;
31 uint8_t down : 1;
32 uint8_t left : 1;
33 uint8_t right : 1;
34 uint8_t fire : 1;
35 };
36 } DoesBits;
38 typedef struct PACKED ClInfo {
39 uint8_t type;
40 uint8_t version;
41 uint8_t name[32];
42 } ClInfo;
44 typedef struct PACKED ClKill {
45 uint8_t type;
46 } ClKill;
48 typedef struct PACKED ClDoes {
49 uint8_t type;
50 DoesBits code;
51 } ClDoes;
53 typedef struct PACKED SvInfo {
54 uint8_t type;
55 uint8_t version;
56 uint8_t clientid;
57 uint8_t maxclients;
58 uint8_t name[32];
59 } SvInfo;
61 typedef struct PACKED SvKill {
62 uint8_t type;
63 uint8_t message[256];
64 } SvKill;
66 typedef struct PACKED SvSplr {
67 uint8_t type;
68 uint8_t clid;
69 uint8_t live;
70 uint8_t x, y, r;
71 uint8_t vx, vy, vr;
72 uint8_t shoot;
73 } SvSplr;
75 typedef struct PACKED SvSbul {
76 uint8_t type;
77 uint8_t id;
78 uint8_t owner;
79 uint8_t live;
80 uint8_t x, y;
81 uint8_t vx, vy;
82 uint8_t tick;
83 } SvSbul;
85 typedef union ClMessage {
86 uint8_t type;
87 ClInfo info;
88 ClKill kill;
89 ClDoes does;
90 } ClMessage;
92 typedef union SvMessage {
93 uint8_t type;
94 SvInfo info;
95 SvKill kill;
96 SvSplr splr;
97 SvSbul sbul;
98 } SvMessage;
100 typedef union ProtocolMessage {
101 uint8_t type;
102 ClMessage cl;
103 SvMessage sv;
104 } ProtocolMessage;
106 static inline int8_t f2b(float x) { return x * PROTOCOL_F8FRAC; }
108 static inline float b2f(int8_t x) { return (float) x / PROTOCOL_F8FRAC; }
110 static inline int MessageTypeSize(MessageType type) {
111 switch(type) {
112 case CL_INFO: return sizeof(ClInfo);
113 case CL_KILL: return sizeof(ClKill);
114 case CL_DOES: return sizeof(ClDoes);
115 case SV_INFO: return sizeof(SvInfo);
116 case SV_KILL: return sizeof(SvKill);
117 case SV_SPLR: return sizeof(SvSplr);
118 case SV_SBUL: return sizeof(SvSbul);
119 default: return sizeof(ProtocolMessage);
123 static inline int MessageSize(ProtocolMessage m) {
124 return MessageTypeSize(m.type);
127 static inline ProtocolMessage cl_info(const char * name) {
128 ClInfo m = {
129 .type = CL_INFO,
130 .version = PROTOCOL_VERSION,
131 };
132 strncpy((char*)m.name, name, sizeof(m.name));
133 return (ProtocolMessage) (ClMessage) m;
136 static inline ProtocolMessage cl_kill() {
137 return (ProtocolMessage) (ClMessage) (ClKill) {
138 .type = CL_KILL,
139 };
142 static inline ProtocolMessage cl_does(DoesBits code) {
143 return (ProtocolMessage) (ClMessage) (ClDoes) {
144 .type = CL_DOES,
145 .code = code,
146 };
149 static inline ProtocolMessage sv_info(const char * name, int clientid, int maxclients) {
150 SvInfo m = {
151 .type = SV_INFO,
152 .version = PROTOCOL_VERSION,
153 .clientid = clientid,
154 .maxclients = maxclients,
155 };
156 strncpy((char*) m.name, name, sizeof(m.name));
157 return (ProtocolMessage) (SvMessage) m;
160 static inline ProtocolMessage sv_kill(const char * msg) {
161 SvKill m = {
162 .type = SV_KILL,
163 };
164 strncpy((char*) m.message, msg, sizeof(m.message));
165 return (ProtocolMessage) (SvMessage) m;
168 static inline ProtocolMessage sv_splr(int clid, bool live, float x, float y, float r, float vx, float vy, float vr, int shoot) {
169 return (ProtocolMessage) (SvMessage) (SvSplr) {
170 .type = SV_SPLR,
171 .clid = clid,
172 .live = live,
173 .x = f2b(x),
174 .y = f2b(y),
175 .r = f2b(r),
176 .vx = f2b(vx),
177 .vy = f2b(vy),
178 .vr = f2b(vr),
179 .shoot = shoot,
180 };
183 static inline ProtocolMessage sv_sbul(int id, int owner, bool live, float x, float y, float vx, float vy, int tick) {
184 return (ProtocolMessage) (SvMessage) (SvSbul) {
185 .type = SV_SBUL,
186 .id = id,
187 .owner = owner,
188 .live = live,
189 .x = f2b(x),
190 .y = f2b(y),
191 .vx = f2b(vx),
192 .vy = f2b(vy),
193 .tick = tick,
194 };
197 #undef PACKED
199 #endif /* PROTOCOL_H_INCLUDED */