7 // netcode mismatch and not sure what caused it? developer_csqcentities 1
11 .bool(entity this, entity sender, bool isNew) m_read;
12 #define NET_HANDLE(id, param) bool Net_Handle_##id(entity this, entity sender, param)
14 #define NET_GUARD(id) \
15 bool Net_Handle_##id##_guard(entity this, entity sender, bool isNew) { \
17 serialize_marker(to, valid); \
18 if (!valid) LOG_FATALF("Last message not fully parsed: %s", _net_prevmsgstr); \
19 _net_prevmsgstr = #id; \
20 return Net_Handle_##id(this, sender, isNew); \
24 string _net_prevmsgstr;
25 #define REGISTER_NET_TEMP(id) \
26 NET_HANDLE(id, bool); \
28 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) { \
30 this.m_read = Net_Handle_##id##_guard; \
33 #define REGISTER_NET_TEMP(id) \
34 const bool NET_##id##_istemp = true; \
35 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
40 #define REGISTER_NET_S2C(id) REGISTER_NET_TEMP(id)
42 REGISTRY(TempEntities, BITS(8) - 80)
43 REGISTER_REGISTRY(TempEntities)
44 REGISTRY_SORT(TempEntities)
45 REGISTRY_CHECK(TempEntities)
47 REGISTRY_DEFINE_GET(TempEntities, NULL)
48 STATIC_INIT(TempEntities_renumber) { FOREACH(TempEntities, true, it.m_id = 80 + i); }
53 #define REGISTER_NET_LINKED(id) \
54 ACCUMULATE NET_HANDLE(id, bool isnew) \
57 this.sourceLoc = __FILE__":"STR(__LINE__); \
58 if (!this) isnew = true; \
61 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
64 this.m_read = Net_Handle_##id##_guard; \
67 #define REGISTER_NET_LINKED(id) \
68 const bool NET_##id##_istemp = false; \
69 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
75 REGISTRY(LinkedEntities, BITS(8) - 1)
76 REGISTER_REGISTRY(LinkedEntities)
77 REGISTRY_SORT(LinkedEntities)
78 REGISTRY_CHECK(LinkedEntities)
80 REGISTRY_DEFINE_GET(LinkedEntities, NULL)
81 STATIC_INIT(LinkedEntities_renumber) { FOREACH(LinkedEntities, true, it.m_id = 1 + i); }
86 #define REGISTER_NET_C2S(id) \
87 NET_HANDLE(id, bool); \
88 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
91 this.m_read = Net_Handle_##id; \
94 #define REGISTER_NET_C2S(id) \
95 const bool NET_##id##_istemp = true; \
96 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
102 REGISTRY(C2S_Protocol, BITS(8) - 1)
103 REGISTER_REGISTRY(C2S_Protocol)
104 REGISTRY_SORT(C2S_Protocol)
105 REGISTRY_CHECK(C2S_Protocol)
107 REGISTRY_DEFINE_GET(C2S_Protocol, NULL)
108 STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); }
111 const int MSG_ENTITY = 5;
115 IntrusiveList g_uncustomizables;
116 STATIC_INIT(g_uncustomizables) { g_uncustomizables = IL_NEW(); }
118 void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
120 if (e.classname == "") e.classname = "net_linked";
122 if (e.model == "" || e.modelindex == 0)
126 _setmodel(e, "null");
130 setSendEntity(e, sendfunc);
131 e.SendFlags = 0xFFFFFF;
133 if (!docull) e.effects |= EF_NODEPTHTEST;
137 e.nextthink = time + dt;
138 setthink(e, SUB_Remove);
142 void Net_UnlinkEntity(entity e)
144 setSendEntity(e, func_null);
147 .void(entity this) uncustomizeentityforclient;
148 .float uncustomizeentityforclient_set;
150 void SetCustomizer(entity e, bool(entity this, entity client) customizer, void(entity this) uncustomizer)
152 setcefc(e, customizer);
153 e.uncustomizeentityforclient = uncustomizer;
154 e.uncustomizeentityforclient_set = !!uncustomizer;
156 IL_PUSH(g_uncustomizables, e);
159 void UncustomizeEntitiesRun()
161 IL_EACH(g_uncustomizables, it.uncustomizeentityforclient_set, it.uncustomizeentityforclient(it));
164 STRING_ITERATOR(g_buf, string_null, 0);
168 void Net_ClientCommand(entity sender, string command)
170 // command matches `c2s "(.+)"`
171 string buf = substring(command, argv_start_index(1) + 1, -2);
172 if (buf == "") return;
173 STRING_ITERATOR_SET(g_buf, buf, 0);
174 for (int C2S; (C2S = ReadByte()) >= 0; )
176 entity reader = REGISTRY_GET(C2S_Protocol, C2S);
177 if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
178 LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d", C2S);
182 int expected = strlen(buf);
183 if (g_buf_i > expected) LOG_WARNF("Underflow: %d", g_buf_i - expected);
184 if (g_buf_i < expected) LOG_WARNF("Overrflow: %d", expected - g_buf_i);
190 const int MSG_C2S = 0;
192 #define Net_Accept(classname) \
194 if (!this) this = new(classname); \
196 #define Net_Reject() \
198 if (this) delete(this); \
205 if (g_buf == "") return;
206 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
212 #define WriteHeader(to, id) \
213 WriteByte(to, NET_##id.m_id)
215 #define WriteHeader(to, id) \
217 if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
218 WriteByte(to, NET_##id.m_id); \
219 bool _net_valid = false; serialize_marker(to, _net_valid); \
223 // serialization: new style
227 #define stream_reading(stream) false
228 #define stream_writing(stream) true
230 #define stream_reading(stream) true
231 #define stream_writing(stream) false
234 #define serialize(T, stream, ...) \
236 noref Stream _stream = stream; \
237 serialize_##T(_stream, __VA_ARGS__); \
241 #define serialize_byte(stream, this) \
243 WriteByte(stream, this); \
246 #define serialize_byte(stream, this) \
253 #define serialize_float(stream, this) \
255 WriteCoord(stream, this); \
258 #define serialize_float(stream, this) \
260 this = ReadCoord(); \
264 #define serialize_vector(stream, this) \
267 serialize_float(stream, _v.x); \
268 serialize_float(stream, _v.y); \
269 serialize_float(stream, _v.z); \
273 #define serialize_marker(stream, this) \
278 int _de = 0xDE, _ad = 0xAD, _be = 0xBE, _ef = 0xEF; \
279 serialize_byte(stream, _de); \
280 serialize_byte(stream, _ad); \
281 serialize_byte(stream, _be); \
282 serialize_byte(stream, _ef); \
283 this = (_de == 0xDE && _ad == 0xAD && _be == 0xBE && _ef == 0xEF); \
287 // serialization: old
289 #define ReadRegistered(r) REGISTRY_GET(r, Read_byte())
290 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
292 #define Read_byte() ReadByte()
293 #define Write_byte(to, f) WriteByte(to, f)
297 void WriteByte(int to, int b)
299 assert(to == MSG_C2S);
300 string s = string_null;
302 string tmp = strcat(g_buf, s);
305 void WriteShort(int to, int b)
307 WriteByte(to, (b >> 8) & 0xFF);
308 WriteByte(to, b & 0xFF);
314 ydec_single(g_buf, ret);
319 return (ReadByte() << 8) | (ReadByte());
321 void WriteByte(int to, int b);
324 #define Read_int() ReadInt24_t()
325 #define Write_int(to, f) WriteInt24_t(to, f)
327 #define Read_float() ReadFloat()
328 #define Write_float(to, f) WriteFloat(to, f)
330 #define Read_string() ReadString()
331 #define Write_string(to, f) WriteString(to, f)
334 const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
335 #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
336 #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
340 entity ReadCSQCEntity()
343 if (f == 0) return NULL;
344 return findfloat(NULL, entnum, f);
348 int v = ReadShort() << 8; // note: this is signed
349 v += ReadByte(); // note: this is unsigned
352 #define ReadInt48_t() vec2(ReadInt24_t(), ReadInt24_t())
353 #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t())
355 noref int _ReadSByte;
356 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
357 #define ReadFloat() ReadCoord()
358 #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
359 #define ReadVector2D() vec2(ReadFloat(), ReadFloat())
360 #define ReadAngleVector() vec3(ReadAngle(), ReadAngle(), ReadAngle())
361 #define ReadAngleVector2D() vec2(ReadAngle(), ReadAngle())
363 int Readbits(int num)
365 if (num > 16) return ReadInt24_t();
366 if (num > 8) return ReadShort();
370 float ReadApproxPastTime()
372 float dt = ReadByte();
374 // map from range...PPROXPASTTIME_MAX / 256
375 dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
377 return servertime - dt;
381 void WriteInt24_t(float dst, float val)
384 WriteShort(dst, (v = floor(val >> 8)));
385 WriteByte(dst, val - (v << 8)); // 0..255
387 void WriteInt48_t(float dst, vector val)
389 WriteInt24_t(dst, val.x);
390 WriteInt24_t(dst, val.y);
392 void WriteInt72_t(float dst, vector val)
394 WriteInt24_t(dst, val.x);
395 WriteInt24_t(dst, val.y);
396 WriteInt24_t(dst, val.z);
399 #define WriteFloat(to, f) WriteCoord(to, f)
400 #define WriteVector(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); MACRO_END
401 #define WriteVector2D(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); MACRO_END
402 #define WriteAngleVector(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); WriteAngle(to, v.z); MACRO_END
403 #define WriteAngleVector2D(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); MACRO_END
405 void Writebits(float dst, float val, int num)
407 if (num > 16) { WriteInt24_t(dst, val); return; }
408 if (num > 8) { WriteShort(dst, val); return; }
412 // this will use the value:
414 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
415 // accuracy at x is 1/derivative, i.e.
416 // APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
417 void WriteApproxPastTime(float dst, float t)
421 // warning: this is approximate; do not resend when you don't have to!
422 // be careful with sendflags here!
423 // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
426 dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
429 dt = rint(bound(0, dt, 255));
434 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
435 #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN \
436 entity prev = msg_entity; \
438 FOREACH_CLIENT(IS_REAL_CLIENT(it), { \
439 if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \