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 1
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 enum {
28 DOES_UP = 0,
29 DOES_DOWN = 1,
30 DOES_LEFT = 2,
31 DOES_RIGHT = 3,
32 DOES_FIRE = 4,
33 } DoesCode;
35 typedef struct PACKED ClInfo {
36 uint8_t type;
37 uint8_t version;
38 uint8_t name[32];
39 } ClInfo;
41 typedef struct PACKED ClKill {
42 uint8_t type;
43 } ClKill;
45 typedef struct PACKED ClDoes {
46 uint8_t type;
47 uint8_t code;
48 } ClDoes;
50 typedef struct PACKED SvInfo {
51 uint8_t type;
52 uint8_t version;
53 uint8_t clientid;
54 uint8_t maxclients;
55 uint8_t name[32];
56 } SvInfo;
58 typedef struct PACKED SvKill {
59 uint8_t type;
60 uint8_t message[256];
61 } SvKill;
63 typedef struct PACKED SvSplr {
64 uint8_t type;
65 uint8_t clid;
66 uint8_t live;
67 uint8_t x, y, r;
68 uint8_t vx, vy, vr;
69 } SvSplr;
71 typedef struct PACKED SvSbul {
72 uint8_t type;
73 uint8_t id;
74 uint8_t owner;
75 uint8_t live;
76 uint8_t x, y;
77 uint8_t vx, vy;
78 uint8_t tick;
79 } SvSbul;
81 typedef union ClMessage {
82 uint8_t type;
83 ClInfo info;
84 ClKill kill;
85 ClDoes does;
86 } ClMessage;
88 typedef union SvMessage {
89 uint8_t type;
90 SvInfo info;
91 SvKill kill;
92 SvSplr splr;
93 SvSbul sbul;
94 } SvMessage;
96 typedef union ProtocolMessage {
97 uint8_t type;
98 ClMessage cl;
99 SvMessage sv;
100 } 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) {
107 switch(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) {
124 ClInfo m = {
125 .type = CL_INFO,
126 .version = PROTOCOL_VERSION,
127 };
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) {
134 .type = CL_KILL,
135 };
138 static inline ProtocolMessage cl_does(DoesCode code) {
139 return (ProtocolMessage) (ClMessage) (ClDoes) {
140 .type = CL_DOES,
141 .code = code,
142 };
145 static inline ProtocolMessage sv_info(const char * name, int clientid, int maxclients) {
146 SvInfo m = {
147 .type = SV_INFO,
148 .version = PROTOCOL_VERSION,
149 .clientid = clientid,
150 .maxclients = maxclients,
151 };
152 strncpy((char*) m.name, name, sizeof(m.name));
153 return (ProtocolMessage) (SvMessage) m;
156 static inline ProtocolMessage sv_kill(const char * msg) {
157 SvKill m = {
158 .type = SV_KILL,
159 };
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) {
166 .type = SV_SPLR,
167 .clid = clid,
168 .live = live,
169 .x = f2b(x),
170 .y = f2b(y),
171 .r = f2b(r),
172 .vx = f2b(vx),
173 .vy = f2b(vy),
174 .vr = f2b(vr),
175 };
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) {
180 .type = SV_SBUL,
181 .id = id,
182 .owner = owner,
183 .live = live,
184 .x = f2b(x),
185 .y = f2b(y),
186 .vx = f2b(vx),
187 .vy = f2b(vy),
188 .tick = tick,
189 };
192 #undef PACKED
194 #endif /* PROTOCOL_H_INCLUDED */