]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/net.qh
Merge branch 'master' into Mario/csqc_muzzleflash
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / net.qh
1 #pragma once
2
3 #include "registry.qh"
4 #include "sort.qh"
5 #include "yenc.qh"
6
7 // netcode mismatch and not sure what caused it? developer_csqcentities 1
8
9 .string netname;
10 .int m_id;
11 .bool(entity this, entity sender, bool isNew) m_read;
12 #define NET_HANDLE(id, param) bool Net_Handle_##id(entity this, entity sender, param)
13
14 #define NET_GUARD(id) \
15     bool Net_Handle_##id##_guard(entity this, entity sender, bool isNew) { \
16         bool valid = false; \
17         serialize_marker(to, valid); \
18         if (!valid) LOG_FATALF("Last message not fully parsed: %s", _net_prevmsgstr); \
19         _net_prevmsgstr = #id; \
20         return Net_Handle_##id(this, sender, isNew); \
21     }
22
23 #ifdef CSQC
24 string _net_prevmsgstr;
25         #define REGISTER_NET_TEMP(id) \
26                 NET_HANDLE(id, bool); \
27         NET_GUARD(id); \
28                 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) { \
29                         this.netname = #id; \
30                         this.m_read = Net_Handle_##id##_guard; \
31                 }
32 #else
33         #define REGISTER_NET_TEMP(id) \
34                 const bool NET_##id##_istemp = true; \
35                 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
36                 { \
37                         this.netname = #id; \
38                 }
39 #endif
40 #define REGISTER_NET_S2C(id) REGISTER_NET_TEMP(id)
41
42 REGISTRY(TempEntities, BITS(8) - 80)
43 REGISTER_REGISTRY(TempEntities)
44 REGISTRY_SORT(TempEntities)
45 REGISTRY_CHECK(TempEntities)
46
47 REGISTRY_DEFINE_GET(TempEntities, NULL)
48 STATIC_INIT(TempEntities_renumber) { FOREACH(TempEntities, true, it.m_id = 80 + i); }
49
50
51
52 #ifdef CSQC
53         #define REGISTER_NET_LINKED(id) \
54                 ACCUMULATE NET_HANDLE(id, bool isnew) \
55                 { \
56                         this = __self; \
57                         this.sourceLoc = __FILE__":"STR(__LINE__); \
58                         if (!this) isnew = true; \
59                 } \
60                 NET_GUARD(id); \
61                 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
62                 { \
63                         this.netname = #id; \
64                         this.m_read = Net_Handle_##id##_guard; \
65                 }
66 #else
67         #define REGISTER_NET_LINKED(id) \
68                 const bool NET_##id##_istemp = false; \
69                 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
70                 { \
71                         this.netname = #id; \
72                 }
73 #endif
74
75 REGISTRY(LinkedEntities, BITS(8) - 1)
76 REGISTER_REGISTRY(LinkedEntities)
77 REGISTRY_SORT(LinkedEntities)
78 REGISTRY_CHECK(LinkedEntities)
79
80 REGISTRY_DEFINE_GET(LinkedEntities, NULL)
81 STATIC_INIT(LinkedEntities_renumber) { FOREACH(LinkedEntities, true, it.m_id = 1 + i); }
82
83
84
85 #ifdef SVQC
86         #define REGISTER_NET_C2S(id) \
87                 NET_HANDLE(id, bool); \
88                 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
89                 { \
90                         this.netname = #id; \
91                         this.m_read = Net_Handle_##id; \
92                 }
93 #else
94         #define REGISTER_NET_C2S(id) \
95                 const bool NET_##id##_istemp = true; \
96                 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
97                 { \
98                         this.netname = #id; \
99                 }
100 #endif
101
102 REGISTRY(C2S_Protocol, BITS(8) - 1)
103 REGISTER_REGISTRY(C2S_Protocol)
104 REGISTRY_SORT(C2S_Protocol)
105 REGISTRY_CHECK(C2S_Protocol)
106
107 REGISTRY_DEFINE_GET(C2S_Protocol, NULL)
108 STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); }
109
110 #ifdef SVQC
111         const int MSG_ENTITY = 5;
112
113         .int SendFlags;
114
115         IntrusiveList g_uncustomizables;
116         STATIC_INIT(g_uncustomizables) { g_uncustomizables = IL_NEW(); }
117
118         void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
119         {
120                 if (e.classname == "") e.classname = "net_linked";
121
122                 if (e.model == "" || e.modelindex == 0)
123                 {
124                         vector mi = e.mins;
125                         vector ma = e.maxs;
126                         _setmodel(e, "null");
127                         setsize(e, mi, ma);
128                 }
129
130                 setSendEntity(e, sendfunc);
131                 e.SendFlags = 0xFFFFFF;
132
133                 if (!docull) e.effects |= EF_NODEPTHTEST;
134
135                 if (dt)
136                 {
137                         e.nextthink = time + dt;
138                         setthink(e, SUB_Remove);
139                 }
140         }
141
142         void Net_UnlinkEntity(entity e)
143         {
144                 setSendEntity(e, func_null);
145         }
146
147         .void(entity this) uncustomizeentityforclient;
148         .float uncustomizeentityforclient_set;
149
150         void SetCustomizer(entity e, bool(entity this, entity client) customizer, void(entity this) uncustomizer)
151         {
152                 setcefc(e, customizer);
153                 e.uncustomizeentityforclient = uncustomizer;
154                 e.uncustomizeentityforclient_set = !!uncustomizer;
155                 if(uncustomizer)
156                         IL_PUSH(g_uncustomizables, e);
157         }
158
159         void UncustomizeEntitiesRun()
160         {
161                 IL_EACH(g_uncustomizables, it.uncustomizeentityforclient_set, it.uncustomizeentityforclient(it));
162         }
163
164         STRING_ITERATOR(g_buf, string_null, 0);
165
166         int ReadByte();
167
168         void Net_ClientCommand(entity sender, string command)
169         {
170                 // command matches `c2s "(.+)"`
171                 string buf = substring(command, argv_start_index(1) + 1, -2);
172                 if (buf == "") return;
173                 STRING_ITERATOR_SET(g_buf, buf, 0);
174                 for (int C2S; (C2S = ReadByte()) >= 0; )
175                 {
176                         entity reader = REGISTRY_GET(C2S_Protocol, C2S);
177                         if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
178                         LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d", C2S);
179                         return;
180                 }
181                 g_buf_i--;
182                 int expected = strlen(buf);
183                 if (g_buf_i > expected) LOG_WARNF("Underflow: %d", g_buf_i - expected);
184                 if (g_buf_i < expected) LOG_WARNF("Overrflow: %d", expected - g_buf_i);
185         }
186
187 #endif
188
189 #ifdef CSQC
190         const int MSG_C2S = 0;
191
192         #define Net_Accept(classname) \
193                 MACRO_BEGIN \
194                         if (!this) this = new(classname); \
195                 MACRO_END
196         #define Net_Reject() \
197                 MACRO_BEGIN \
198                         if (this) delete(this); \
199                 MACRO_END
200
201         string g_buf;
202
203         void Net_Flush()
204         {
205                 if (g_buf == "") return;
206                 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
207                 strfree(g_buf);
208         }
209 #endif
210
211 #if defined(CSQC)
212         #define WriteHeader(to, id) \
213                 WriteByte(to, NET_##id.m_id)
214 #elif defined(SVQC)
215         #define WriteHeader(to, id) \
216                 MACRO_BEGIN \
217                         if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
218                         WriteByte(to, NET_##id.m_id); \
219                         bool _net_valid = false; serialize_marker(to, _net_valid); \
220                 MACRO_END
221 #endif
222
223 // serialization: new style
224
225 USING(Stream, int);
226 #if defined(SVQC)
227         #define stream_reading(stream) false
228         #define stream_writing(stream) true
229 #elif defined(CSQC)
230         #define stream_reading(stream) true
231         #define stream_writing(stream) false
232 #endif
233
234 #define serialize(T, stream, ...) \
235 MACRO_BEGIN \
236     noref Stream _stream = stream; \
237     serialize_##T(_stream, __VA_ARGS__); \
238 MACRO_END
239
240 #if defined(SVQC)
241         #define serialize_byte(stream, this) \
242                 MACRO_BEGIN \
243                 WriteByte(stream, this); \
244                 MACRO_END
245 #elif defined(CSQC)
246         #define serialize_byte(stream, this) \
247                 MACRO_BEGIN \
248                 this = ReadByte(); \
249                 MACRO_END
250 #endif
251
252 #if defined(SVQC)
253         #define serialize_float(stream, this) \
254                 MACRO_BEGIN \
255                 WriteCoord(stream, this); \
256                 MACRO_END
257 #elif defined(CSQC)
258         #define serialize_float(stream, this) \
259                 MACRO_BEGIN \
260                 this = ReadCoord(); \
261                 MACRO_END
262 #endif
263
264 #define serialize_vector(stream, this) \
265 MACRO_BEGIN \
266     vector _v = this; \
267     serialize_float(stream, _v.x); \
268     serialize_float(stream, _v.y); \
269     serialize_float(stream, _v.z); \
270     this = _v; \
271 MACRO_END
272
273 #define serialize_marker(stream, this) \
274 MACRO_BEGIN \
275     if (NDEBUG) { \
276         this = true; \
277     } else { \
278         int _de = 0xDE, _ad = 0xAD, _be = 0xBE, _ef = 0xEF; \
279         serialize_byte(stream, _de); \
280         serialize_byte(stream, _ad); \
281         serialize_byte(stream, _be); \
282         serialize_byte(stream, _ef); \
283         this = (_de == 0xDE && _ad == 0xAD && _be == 0xBE && _ef == 0xEF); \
284     } \
285 MACRO_END
286
287 // serialization: old
288
289 #define ReadRegistered(r) REGISTRY_GET(r, Read_byte())
290 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
291
292 #define Read_byte() ReadByte()
293 #define Write_byte(to, f) WriteByte(to, f)
294
295 #if defined(CSQC)
296         int ReadByte();
297         void WriteByte(int to, int b)
298         {
299                 assert(to == MSG_C2S);
300                 string s = string_null;
301                 yenc_single(b, s);
302                 string tmp = strcat(g_buf, s);
303                 strcpy(g_buf, tmp);
304         }
305         void WriteShort(int to, int b)
306         {
307                 WriteByte(to, (b >> 8) & 0xFF);
308                 WriteByte(to, b & 0xFF);
309         }
310 #elif defined(SVQC)
311         int ReadByte()
312         {
313                 int ret = -1;
314                 ydec_single(g_buf, ret);
315                 return ret;
316         }
317         int ReadShort()
318         {
319                 return (ReadByte() << 8) | (ReadByte());
320         }
321         void WriteByte(int to, int b);
322 #endif
323
324 #define Read_int() ReadInt24_t()
325 #define Write_int(to, f) WriteInt24_t(to, f)
326
327 #define Read_float() ReadFloat()
328 #define Write_float(to, f) WriteFloat(to, f)
329
330 #define Read_string() ReadString()
331 #define Write_string(to, f) WriteString(to, f)
332
333 #ifdef GAMEQC
334         const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
335         #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
336         #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
337
338         #ifdef CSQC
339                 float servertime;
340                 entity ReadCSQCEntity()
341                 {
342                         int f = ReadShort();
343                         if (f == 0) return NULL;
344                         return findfloat(NULL, entnum, f);
345                 }
346                 int ReadInt24_t()
347                 {
348                         int v = ReadShort() << 8; // note: this is signed
349                         v += ReadByte();          // note: this is unsigned
350                         return v;
351                 }
352                 #define ReadInt48_t() vec2(ReadInt24_t(), ReadInt24_t())
353                 #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t())
354
355                 noref int _ReadSByte;
356                 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
357                 #define ReadFloat() ReadCoord()
358                 #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
359                 #define ReadVector2D() vec2(ReadFloat(), ReadFloat())
360
361                 int Readbits(int num)
362                 {
363                         if (num > 16) return ReadInt24_t();
364                         if (num > 8) return ReadShort();
365                         return ReadByte();
366                 }
367
368                 float ReadApproxPastTime()
369                 {
370                         float dt = ReadByte();
371
372                         // map from range...PPROXPASTTIME_MAX / 256
373                         dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
374
375                         return servertime - dt;
376                 }
377
378         #else
379                 void WriteInt24_t(float dst, float val)
380                 {
381                         float v;
382                         WriteShort(dst, (v = floor(val >> 8)));
383                         WriteByte(dst, val - (v << 8));  // 0..255
384                 }
385                 void WriteInt48_t(float dst, vector val)
386                 {
387                         WriteInt24_t(dst, val.x);
388                         WriteInt24_t(dst, val.y);
389                 }
390                 void WriteInt72_t(float dst, vector val)
391                 {
392                         WriteInt24_t(dst, val.x);
393                         WriteInt24_t(dst, val.y);
394                         WriteInt24_t(dst, val.z);
395                 }
396
397                 #define WriteFloat(to, f) WriteCoord(to, f)
398                 #define WriteVector(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); MACRO_END
399                 #define WriteVector2D(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); MACRO_END
400
401                 void Writebits(float dst, float val, int num)
402                 {
403                         if (num > 16) { WriteInt24_t(dst, val); return; }
404                         if (num > 8) { WriteShort(dst, val); return; }
405                         WriteByte(dst, val);
406                 }
407
408                 // this will use the value:
409                 //   128
410                 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
411                 // accuracy at x is 1/derivative, i.e.
412                 //   APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
413                 void WriteApproxPastTime(float dst, float t)
414                 {
415                         float dt = time - t;
416
417                         // warning: this is approximate; do not resend when you don't have to!
418                         // be careful with sendflags here!
419                         // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
420
421                         // map to range...
422                         dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
423
424                         // round...
425                         dt = rint(bound(0, dt, 255));
426
427                         WriteByte(dst, dt);
428                 }
429
430                 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
431                 #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN \
432                         entity prev = msg_entity; \
433                         entity dst = to; \
434                         FOREACH_CLIENT(IS_REAL_CLIENT(it), { \
435                                 if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \
436                                 { \
437                                         msg_entity = it; \
438                                         LAMBDA(statement); \
439                                 } \
440                         }); \
441                         msg_entity = prev; \
442                 MACRO_END
443         #endif
444 #endif