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