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