]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/net.qh
Step 6: complete
[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         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                 e.SendEntity = func_null;
131         }
132
133         .void(entity this) uncustomizeentityforclient;
134         .float uncustomizeentityforclient_set;
135
136         void SetCustomizer(entity e, bool(entity this) customizer, void(entity this) uncustomizer)
137         {
138                 setcefc(e, customizer);
139                 e.uncustomizeentityforclient = uncustomizer;
140                 e.uncustomizeentityforclient_set = !!uncustomizer;
141         }
142
143         void UncustomizeEntitiesRun()
144         {
145                 FOREACH_ENTITY_FLOAT(uncustomizeentityforclient_set, true, it.uncustomizeentityforclient(it));
146         }
147
148         STRING_ITERATOR(g_buf, string_null, 0);
149
150         int ReadByte();
151
152         void Net_ClientCommand(entity sender, string command)
153         {
154                 // command matches `c2s "(.+)"`
155                 string buf = substring(command, argv_start_index(1) + 1, -2);
156                 if (buf == "") return;
157                 STRING_ITERATOR_SET(g_buf, buf, 0);
158                 for (int C2S; (C2S = ReadByte()) >= 0; )
159                 {
160                         entity reader = C2S_Protocol_from(C2S);
161                         if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
162                         LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d\n", C2S);
163                         return;
164                 }
165                 g_buf_i--;
166                 int expected = strlen(buf);
167                 if (g_buf_i > expected) LOG_WARNINGF("Underflow: %d", g_buf_i - expected);
168                 if (g_buf_i < expected) LOG_WARNINGF("Overrflow: %d", expected - g_buf_i);
169         }
170
171 #endif
172
173 #ifdef CSQC
174         const int MSG_C2S = 0;
175
176         #define Net_Accept(classname) \
177                 MACRO_BEGIN { \
178                         if (!this)    this = new(classname); \
179                 } MACRO_END
180         #define Net_Reject() \
181                 MACRO_BEGIN { \
182                         if (this)     remove(this); \
183                 } MACRO_END
184
185         string g_buf;
186
187         void Net_Flush()
188         {
189                 if (g_buf == "") return;
190                 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
191                 strunzone(g_buf);
192                 g_buf = string_null;
193         }
194 #endif
195
196 #if defined(CSQC)
197         #define WriteHeader(to, id) \
198                 MACRO_BEGIN { \
199                         WriteByte(to, NET_##id.m_id); \
200                 } MACRO_END
201 #elif defined(SVQC)
202         #define WriteHeader(to, id) \
203                 MACRO_BEGIN { \
204                         if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
205                         WriteByte(to, NET_##id.m_id); \
206                 } MACRO_END
207 #endif
208
209 #define ReadRegistered(r) r##_from(Read_byte())
210 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
211
212 #define Read_byte() ReadByte()
213 #define Write_byte(to, f) WriteByte(to, f)
214
215 #if defined(CSQC)
216         int ReadByte();
217         void WriteByte(int to, int b)
218         {
219                 assert(to == MSG_C2S);
220                 string s = string_null;
221                 yenc_single(b, s);
222                 string tmp = strcat(g_buf, s);
223                 if (g_buf) strunzone(g_buf);
224                 g_buf = strzone(tmp);
225         }
226 #elif defined(SVQC)
227         int ReadByte()
228         {
229                 int ret = -1;
230                 ydec_single(g_buf, ret);
231                 return ret;
232         }
233         void WriteByte(int to, int b);
234 #endif
235
236 #define Read_int() ReadInt24_t()
237 #define Write_int(to, f) WriteInt24_t(to, f)
238
239 #define Read_float() ReadFloat()
240 #define Write_float(to, f) WriteFloat(to, f)
241
242 #define Read_string() ReadString()
243 #define Write_string(to, f) WriteString(to, f)
244
245 #ifndef MENUQC
246         const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
247         #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
248         #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
249
250         #ifdef CSQC
251                 entity ReadCSQCEntity()
252                 {
253                         int f = ReadShort();
254                         if (f == 0) return NULL;
255                         return findfloat(NULL, entnum, f);
256                 }
257                 int ReadInt24_t()
258                 {
259                         int v = ReadShort() << 8; // note: this is signed
260                         v += ReadByte();          // note: this is unsigned
261                         return v;
262                 }
263                 #define ReadInt48_t() vec3(ReadInt24_t(), ReadInt24_t(), 0)
264                 #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t())
265
266                 int _ReadSByte;
267                 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
268                 #define ReadFloat() ReadCoord()
269                 #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
270                 #define ReadVector2D() vec3(ReadFloat(), ReadFloat(), 0)
271
272                 float ReadApproxPastTime()
273                 {
274                         float dt = ReadByte();
275
276                         // map from range...PPROXPASTTIME_MAX / 256
277                         dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
278
279                         return servertime - dt;
280                 }
281
282         #else
283                 void WriteInt24_t(float dst, float val)
284                 {
285                         float v;
286                         WriteShort(dst, (v = floor(val >> 8)));
287                         WriteByte(dst, val - (v << 8));  // 0..255
288                 }
289                 void WriteInt48_t(float dst, vector val)
290                 {
291                         WriteInt24_t(dst, val.x);
292                         WriteInt24_t(dst, val.y);
293                 }
294                 void WriteInt72_t(float dst, vector val)
295                 {
296                         WriteInt24_t(dst, val.x);
297                         WriteInt24_t(dst, val.y);
298                         WriteInt24_t(dst, val.z);
299                 }
300
301         #define WriteFloat(to, f) WriteCoord(to, f)
302                 #define WriteVector(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); } MACRO_END
303         #define WriteVector2D(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); } MACRO_END
304
305                 // this will use the value:
306                 //   128
307                 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
308                 // accuracy at x is 1/derivative, i.e.
309                 //   APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
310                 void WriteApproxPastTime(float dst, float t)
311                 {
312                         float dt = time - t;
313
314                         // warning: this is approximate; do not resend when you don't have to!
315                         // be careful with sendflags here!
316                         // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
317
318                         // map to range...
319                         dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
320
321                         // round...
322                         dt = rint(bound(0, dt, 255));
323
324                         WriteByte(dst, dt);
325                 }
326
327                 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
328                 #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN { \
329                         entity prev = msg_entity; \
330                         entity dst = to; \
331                         FOREACH_CLIENT(IS_REAL_CLIENT(it), { \
332                                 if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \
333                                 { \
334                                         msg_entity = it; \
335                                         LAMBDA(statement); \
336                                 } \
337                         }); \
338                         msg_entity = prev; \
339                 } MACRO_END
340         #endif
341 #endif