]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/net.qh
Merge branch 'master' into Mario/hagar_notfixed
[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         .bool(entity to, int sendflags) SendEntity;
101         /** return false to remove from the client */
102         .bool(entity this, entity to, int sendflags) SendEntity3;
103
104         bool SendEntity_self(entity to, int sendflags) { SELFPARAM(); return this.SendEntity3(this, to, sendflags); }
105
106         void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
107         {
108                 if (e.classname == "") e.classname = "net_linked";
109
110                 if (e.model == "" || e.modelindex == 0)
111                 {
112                         vector mi = e.mins;
113                         vector ma = e.maxs;
114                         _setmodel(e, "null");
115                         setsize(e, mi, ma);
116                 }
117
118                 e.SendEntity = SendEntity_self;
119                 e.SendEntity3 = sendfunc;
120                 e.SendFlags = 0xFFFFFF;
121
122                 if (!docull) e.effects |= EF_NODEPTHTEST;
123
124                 if (dt)
125                 {
126                         e.nextthink = time + dt;
127                         e.think = SUB_Remove_self;
128                 }
129         }
130
131         void Net_UnlinkEntity(entity e)
132         {
133                 e.SendEntity = func_null;
134         }
135
136         .void() uncustomizeentityforclient;
137         .float uncustomizeentityforclient_set;
138
139         void SetCustomizer(entity e, float() customizer, void() uncustomizer)
140         {
141                 e.customizeentityforclient = customizer;
142                 e.uncustomizeentityforclient = uncustomizer;
143                 e.uncustomizeentityforclient_set = !!uncustomizer;
144         }
145
146         void UncustomizeEntitiesRun()
147         {
148             SELFPARAM();
149                 FOREACH_ENTITY_FLOAT(uncustomizeentityforclient_set, true, WITH(entity, self, it, it.uncustomizeentityforclient()));
150         }
151
152         STRING_ITERATOR(g_buf, string_null, 0);
153
154         int ReadByte();
155
156         void Net_ClientCommand(entity sender, string command)
157         {
158                 // command matches `c2s "(.+)"`
159                 string buf = substring(command, argv_start_index(1) + 1, -2);
160                 if (buf == "") return;
161                 STRING_ITERATOR_SET(g_buf, buf, 0);
162                 for (int C2S; (C2S = ReadByte()) >= 0; )
163                 {
164                         entity reader = C2S_Protocol_from(C2S);
165                         if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
166                         LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d\n", C2S);
167                         return;
168                 }
169                 g_buf_i--;
170                 int expected = strlen(buf);
171                 if (g_buf_i > expected) LOG_WARNINGF("Underflow: %d", g_buf_i - expected);
172                 if (g_buf_i < expected) LOG_WARNINGF("Overrflow: %d", expected - g_buf_i);
173         }
174
175 #endif
176
177 #ifdef CSQC
178         const int MSG_C2S = 0;
179
180         #define Net_Accept(classname) \
181                 MACRO_BEGIN { \
182                         if (!this)    this = new(classname); \
183                 } MACRO_END
184         #define Net_Reject() \
185                 MACRO_BEGIN { \
186                         if (this)     remove(this); \
187                 } MACRO_END
188
189         string g_buf;
190
191         void Net_Flush()
192         {
193                 if (g_buf == "") return;
194                 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
195                 strunzone(g_buf);
196                 g_buf = string_null;
197         }
198 #endif
199
200 #if defined(CSQC)
201         #define WriteHeader(to, id) \
202                 MACRO_BEGIN { \
203                         WriteByte(to, NET_##id.m_id); \
204                 } MACRO_END
205 #elif defined(SVQC)
206         #define WriteHeader(to, id) \
207                 MACRO_BEGIN { \
208                         if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
209                         WriteByte(to, NET_##id.m_id); \
210                 } MACRO_END
211 #endif
212
213 #define ReadRegistered(r) r##_from(Read_byte())
214 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
215
216 #define Read_byte() ReadByte()
217 #define Write_byte(to, f) WriteByte(to, f)
218
219 #if defined(CSQC)
220         int ReadByte();
221         void WriteByte(int to, int b)
222         {
223                 assert(to == MSG_C2S);
224                 string s = string_null;
225                 yenc_single(b, s);
226                 string tmp = strcat(g_buf, s);
227                 if (g_buf) strunzone(g_buf);
228                 g_buf = strzone(tmp);
229         }
230 #elif defined(SVQC)
231         int ReadByte()
232         {
233                 int ret = -1;
234                 ydec_single(g_buf, ret);
235                 return ret;
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                 #define ReadInt48_t() vec3(ReadInt24_t(), ReadInt24_t(), 0)
268                 #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t())
269
270                 int _ReadSByte;
271                 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
272                 #define ReadFloat() ReadCoord()
273                 #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
274                 #define ReadVector2D() vec3(ReadFloat(), ReadFloat(), 0)
275
276                 float ReadApproxPastTime()
277                 {
278                         float dt = ReadByte();
279
280                         // map from range...PPROXPASTTIME_MAX / 256
281                         dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
282
283                         return servertime - dt;
284                 }
285
286         #else
287                 void WriteInt24_t(float dst, float val)
288                 {
289                         float v;
290                         WriteShort(dst, (v = floor(val >> 8)));
291                         WriteByte(dst, val - (v << 8));  // 0..255
292                 }
293                 void WriteInt48_t(float dst, vector val)
294                 {
295                         WriteInt24_t(dst, val.x);
296                         WriteInt24_t(dst, val.y);
297                 }
298                 void WriteInt72_t(float dst, vector val)
299                 {
300                         WriteInt24_t(dst, val.x);
301                         WriteInt24_t(dst, val.y);
302                         WriteInt24_t(dst, val.z);
303                 }
304
305         #define WriteFloat(to, f) WriteCoord(to, f)
306                 #define WriteVector(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); } MACRO_END
307         #define WriteVector2D(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); } MACRO_END
308
309                 // this will use the value:
310                 //   128
311                 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
312                 // accuracy at x is 1/derivative, i.e.
313                 //   APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
314                 void WriteApproxPastTime(float dst, float t)
315                 {
316                         float dt = time - t;
317
318                         // warning: this is approximate; do not resend when you don't have to!
319                         // be careful with sendflags here!
320                         // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
321
322                         // map to range...
323                         dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
324
325                         // round...
326                         dt = rint(bound(0, dt, 255));
327
328                         WriteByte(dst, dt);
329                 }
330
331                 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
332                 #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN { \
333                         entity prev = msg_entity; \
334                         entity dst = to; \
335                         FOREACH_CLIENT(IS_REAL_CLIENT(it), { \
336                                 if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \
337                                 { \
338                                         msg_entity = it; \
339                                         LAMBDA(statement); \
340                                 } \
341                         }); \
342                         msg_entity = prev; \
343                 } MACRO_END
344         #endif
345 #endif