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