]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/net.qh
C2S protocol
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / net.qh
1 #ifndef NET_H
2 #define NET_H
3
4 #include "registry.qh"
5 #include "sort.qh"
6 #include "yenc.qh"
7
8 .string netname;
9 .int m_id;
10 .bool(entity this, bool isNew) m_read;
11 #define NET_HANDLE(id, param) bool Net_Handle_##id(entity this, param)
12
13
14 #ifdef CSQC
15         #define REGISTER_NET_TEMP(id) \
16                 NET_HANDLE(id, bool); \
17                 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
18                 { \
19                         this.netname = #id; \
20                         this.m_read = Net_Handle_##id; \
21                 }
22 #else
23         #define REGISTER_NET_TEMP(id) \
24                 const bool NET_##id##_istemp = true; \
25                 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
26                 { \
27                         this.netname = #id; \
28                 }
29 #endif
30 #define REGISTER_NET_S2C(id) REGISTER_NET_TEMP(id)
31
32 REGISTRY(TempEntities, BITS(8) - 80)
33 #define TempEntities_from(i) _TempEntities_from(i, NULL)
34 REGISTER_REGISTRY(TempEntities)
35 REGISTRY_SORT(TempEntities)
36 REGISTRY_CHECK(TempEntities)
37 STATIC_INIT(RegisterTempEntities_renumber) { FOREACH(TempEntities, true, it.m_id = 80 + i); }
38
39
40
41 #ifdef CSQC
42         #define REGISTER_NET_LINKED(id) \
43                 [[accumulate]] NET_HANDLE(id, bool isnew) \
44                 { \
45                         this = self; \
46                         this.sourceLocFile = __FILE__; \
47                         this.sourceLocLine = __LINE__; \
48                         if (!this) isnew = true; \
49                 } \
50                 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
51                 { \
52                         this.netname = #id; \
53                         this.m_read = Net_Handle_##id; \
54                 }
55 #else
56         #define REGISTER_NET_LINKED(id) \
57                 const bool NET_##id##_istemp = false; \
58                 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
59                 { \
60                         this.netname = #id; \
61                 }
62 #endif
63
64 REGISTRY(LinkedEntities, BITS(8) - 1)
65 #define LinkedEntities_from(i) _LinkedEntities_from(i, NULL)
66 REGISTER_REGISTRY(LinkedEntities)
67 REGISTRY_SORT(LinkedEntities)
68 REGISTRY_CHECK(LinkedEntities)
69 STATIC_INIT(RegisterLinkedEntities_renumber) { FOREACH(LinkedEntities, true, it.m_id = 1 + i); }
70
71
72
73 #ifdef SVQC
74         #define REGISTER_NET_C2S(id) \
75                 NET_HANDLE(id, bool); \
76                 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
77                 { \
78                         this.netname = #id; \
79                         this.m_read = Net_Handle_##id; \
80                 }
81 #else
82         #define REGISTER_NET_C2S(id) \
83                 const bool NET_##id##_istemp = true; \
84                 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
85                 { \
86                         this.netname = #id; \
87                 }
88 #endif
89
90 REGISTRY(C2S_Protocol, BITS(8) - 1)
91 #define C2S_Protocol_from(i) _C2S_Protocol_from(i, NULL)
92 REGISTER_REGISTRY(C2S_Protocol)
93 REGISTRY_SORT(C2S_Protocol)
94 REGISTRY_CHECK(C2S_Protocol)
95 STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); }
96
97 #ifdef SVQC
98         const int MSG_ENTITY = 5;
99
100         .int Version;  // deprecated, use SendFlags
101         .int SendFlags;
102         .bool(entity to, int sendflags) SendEntity;
103         /** return false to remove from the client */
104         .bool(entity this, entity to, int sendflags) SendEntity3;
105
106         bool SendEntity_self(entity to, int sendflags) { return self.SendEntity3(self, to, sendflags); }
107
108         void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
109         {
110                 if (e.classname == "") e.classname = "net_linked";
111
112                 if (e.model == "" || self.modelindex == 0)
113                 {
114                         vector mi = e.mins;
115                         vector ma = e.maxs;
116                         _setmodel(e, "null");
117                         setsize(e, mi, ma);
118                 }
119
120                 e.SendEntity = SendEntity_self;
121                 e.SendEntity3 = sendfunc;
122                 e.SendFlags = 0xFFFFFF;
123
124                 if (!docull) e.effects |= EF_NODEPTHTEST;
125
126                 if (dt)
127                 {
128                         e.nextthink = time + dt;
129                         e.think = SUB_Remove_self;
130                 }
131         }
132
133         void Net_UnlinkEntity(entity e)
134         {
135                 e.SendEntity = func_null;
136         }
137
138         .void() uncustomizeentityforclient;
139         .float uncustomizeentityforclient_set;
140
141         void SetCustomizer(entity e, float() customizer, void() uncustomizer)
142         {
143                 e.customizeentityforclient = customizer;
144                 e.uncustomizeentityforclient = uncustomizer;
145                 e.uncustomizeentityforclient_set = !!uncustomizer;
146         }
147
148         void UncustomizeEntitiesRun()
149         {
150                 for (entity e = NULL; (e = findfloat(e, uncustomizeentityforclient_set, 1)); )
151                         WITH(entity, self, e, e.uncustomizeentityforclient());
152         }
153
154         STRING_ITERATOR(g_buf, string_null, 0);
155
156         int ReadByte();
157
158         void Net_ClientCommand(string command)
159         {
160                 // command matches `c2s "(.+)"`
161                 string buf = substring(command, argv_start_index(1) + 1, -2);
162                 if (buf == "") return;
163                 STRING_ITERATOR_SET(g_buf, buf, 0);
164                 for (int C2S; (C2S = ReadByte()) >= 0; )
165                 {
166                         entity reader = C2S_Protocol_from(C2S);
167                         if (reader && reader.m_read && reader.m_read(NULL, true)) continue;
168                         LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d\n", C2S);
169                         return;
170                 }
171                 g_buf_i--;
172                 int expected = strlen(buf);
173                 if (g_buf_i > expected) LOG_WARNINGF("Underflow: %d", g_buf_i - expected);
174                 if (g_buf_i < expected) LOG_WARNINGF("Overrflow: %d", expected - g_buf_i);
175         }
176
177 #endif
178
179 #ifdef CSQC
180         const int MSG_C2S = 0;
181
182         #define Net_Accept(classname) \
183                 MACRO_BEGIN { \
184                         if (!this)    this = new(classname); \
185                 } MACRO_END
186         #define Net_Reject() \
187                 MACRO_BEGIN { \
188                         if (this)     remove(this); \
189                 } MACRO_END
190
191         string g_buf;
192
193         void Net_Flush()
194         {
195                 if (g_buf == "") return;
196                 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
197                 strunzone(g_buf);
198                 g_buf = string_null;
199         }
200 #endif
201
202 #if defined(CSQC)
203         #define WriteHeader(to, id) \
204                 MACRO_BEGIN { \
205                         WriteByte(to, NET_##id.m_id); \
206                 } MACRO_END
207 #elif defined(SVQC)
208         #define WriteHeader(to, id) \
209                 MACRO_BEGIN { \
210                         if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
211                         WriteByte(to, NET_##id.m_id); \
212                 } MACRO_END
213 #endif
214
215 #define ReadRegistry(r) r##_from(Read_byte())
216 #define WriteRegistry(r, to, it) Write_byte(to, it.m_id)
217
218 #define Read_byte() ReadByte()
219 #define Write_byte(to, f) WriteByte(to, f)
220
221 #if defined(CSQC)
222         int ReadByte();
223         void WriteByte(int to, int b)
224         {
225                 assert(to == MSG_C2S);
226                 string s;
227                 yenc_single(b, s);
228                 string tmp = strcat(g_buf, s);
229                 if (g_buf) strunzone(g_buf);
230                 g_buf = strzone(tmp);
231         }
232 #elif defined(SVQC)
233         int ReadByte()
234         {
235                 ydec_single(g_buf, return);
236         }
237         void WriteByte(int to, int b);
238 #endif
239
240 #define Read_int() ReadInt24_t()
241 #define Write_int(to, f) WriteInt24_t(to, f)
242
243 #define Read_float() ReadFloat()
244 #define Write_float(to, f) WriteFloat(to, f)
245
246 #define Read_string() ReadString()
247 #define Write_string(to, f) WriteString(to, f)
248
249 #ifndef MENUQC
250         const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
251         #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
252         #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
253
254         #ifdef CSQC
255                 entity ReadCSQCEntity()
256                 {
257                         int f = ReadShort();
258                         if (f == 0) return NULL;
259                         return findfloat(NULL, entnum, f);
260                 }
261                 int ReadInt24_t()
262                 {
263                         int v = ReadShort() << 8; // note: this is signed
264                         v += ReadByte();          // note: this is unsigned
265                         return v;
266                 }
267                 vector ReadInt48_t()
268                 {
269                         vector v;
270                         v.x = ReadInt24_t();
271                         v.y = ReadInt24_t();
272                         v.z = 0;
273                         return v;
274                 }
275                 vector ReadInt72_t()
276                 {
277                         vector v;
278                         v.x = ReadInt24_t();
279                         v.y = ReadInt24_t();
280                         v.z = ReadInt24_t();
281                         return v;
282                 }
283
284                 int _ReadSByte;
285                 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
286                 #define ReadFloat() ReadCoord()
287         vector ReadVector() { vector v; v.x = ReadFloat(); v.y = ReadFloat(); v.z = ReadFloat(); return v; }
288                 vector ReadVector2D() { vector v; v.x = ReadFloat(); v.y = ReadFloat(); v.z = 0; return v; }
289
290                 float ReadApproxPastTime()
291                 {
292                         float dt = ReadByte();
293
294                         // map from range...PPROXPASTTIME_MAX / 256
295                         dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
296
297                         return servertime - dt;
298                 }
299
300         #else
301                 void WriteInt24_t(float dst, float val)
302                 {
303                         float v;
304                         WriteShort(dst, (v = floor(val >> 8)));
305                         WriteByte(dst, val - (v << 8));  // 0..255
306                 }
307                 void WriteInt48_t(float dst, vector val)
308                 {
309                         WriteInt24_t(dst, val.x);
310                         WriteInt24_t(dst, val.y);
311                 }
312                 void WriteInt72_t(float dst, vector val)
313                 {
314                         WriteInt24_t(dst, val.x);
315                         WriteInt24_t(dst, val.y);
316                         WriteInt24_t(dst, val.z);
317                 }
318
319         #define WriteFloat(to, f) WriteCoord(to, f)
320                 #define WriteVector(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); } MACRO_END
321         #define WriteVector2D(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); } MACRO_END
322
323                 // this will use the value:
324                 //   128
325                 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
326                 // accuracy at x is 1/derivative, i.e.
327                 //   APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
328                 void WriteApproxPastTime(float dst, float t)
329                 {
330                         float dt = time - t;
331
332                         // warning: this is approximate; do not resend when you don't have to!
333                         // be careful with sendflags here!
334                         // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
335
336                         // map to range...
337                         dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
338
339                         // round...
340                         dt = rint(bound(0, dt, 255));
341
342                         WriteByte(dst, dt);
343                 }
344
345                 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
346                 #define WRITESPECTATABLE_MSG_ONE_VARNAME(varname, statement) \
347                         entity varname; varname = msg_entity; \
348                         FOR_EACH_REALCLIENT(msg_entity) \
349                         if (msg_entity == varname || (msg_entity.classname == STR_SPECTATOR && msg_entity.enemy == varname)) \
350                                 statement msg_entity = varname
351                 #define WRITESPECTATABLE_MSG_ONE(statement) \
352                         MACRO_BEGIN \
353                         { \
354                                 WRITESPECTATABLE_MSG_ONE_VARNAME(oldmsg_entity, statement); \
355                         } MACRO_END
356                 #define WRITESPECTATABLE(msg, statement) \
357                         if (msg == MSG_ONE) WRITESPECTATABLE_MSG_ONE(statement); \
358                         else \
359                                 statement float WRITESPECTATABLE_workaround = 0
360         #endif
361 #endif
362
363 #endif