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