7 // netcode mismatch and not sure what caused it? developer_csqcentities 1
14 .bool(entity this, entity sender, bool isNew) m_read;
15 #define NET_HANDLE(id, param) bool Net_Handle_##id(entity this, entity sender, param)
17 #define NET_GUARD(id) \
18 bool Net_Handle_##id##_guard(entity this, entity sender, bool isNew) { \
20 serialize_marker(to, valid); \
21 if (!valid) LOG_FATALF("Last message not fully parsed: %s", _net_prevmsgstr); \
22 _net_prevmsgstr = #id; \
23 return Net_Handle_##id(this, sender, isNew); \
27 string _net_prevmsgstr;
28 #define REGISTER_NET_TEMP(id) \
29 NET_HANDLE(id, bool); \
31 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) { \
33 this.m_read = Net_Handle_##id##_guard; \
36 #define REGISTER_NET_TEMP(id) \
37 const bool NET_##id##_istemp = true; \
38 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
44 REGISTRY(TempEntities, BITS(8) - 80)
45 REGISTER_REGISTRY(TempEntities)
46 REGISTRY_SORT(TempEntities)
47 REGISTRY_CHECK(TempEntities)
49 REGISTRY_DEFINE_GET(TempEntities, NULL)
50 STATIC_INIT(TempEntities_renumber) { FOREACH(TempEntities, true, it.m_id = 80 + i); }
55 #define REGISTER_NET_LINKED(id) \
56 ACCUMULATE NET_HANDLE(id, bool isnew) \
59 this.sourceLoc = __FILE__":"STR(__LINE__); \
60 if (!this) isnew = true; \
63 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
66 this.m_read = Net_Handle_##id##_guard; \
69 #define REGISTER_NET_LINKED(id) \
70 const bool NET_##id##_istemp = false; \
71 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
77 REGISTRY(LinkedEntities, BITS(8) - 1)
78 REGISTER_REGISTRY(LinkedEntities)
79 REGISTRY_SORT(LinkedEntities)
80 REGISTRY_CHECK(LinkedEntities)
82 REGISTRY_DEFINE_GET(LinkedEntities, NULL)
83 STATIC_INIT(LinkedEntities_renumber) { FOREACH(LinkedEntities, true, it.m_id = 1 + i); }
88 #define REGISTER_NET_C2S(id) \
89 NET_HANDLE(id, bool); \
90 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
93 this.m_read = Net_Handle_##id; \
96 #define REGISTER_NET_C2S(id) \
97 const bool NET_##id##_istemp = true; \
98 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
100 this.netname = #id; \
104 REGISTRY(C2S_Protocol, BITS(8) - 1)
105 REGISTER_REGISTRY(C2S_Protocol)
106 REGISTRY_SORT(C2S_Protocol)
107 REGISTRY_CHECK(C2S_Protocol)
109 REGISTRY_DEFINE_GET(C2S_Protocol, NULL)
110 STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); }
115 const int MSG_ENTITY = 5;
119 IntrusiveList g_uncustomizables;
120 STATIC_INIT(g_uncustomizables) { g_uncustomizables = IL_NEW(); }
122 void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
124 if (e.classname == "")
126 LOG_WARN("Net_LinkEntity called on an entity without a classname, assigning default");
127 e.classname = "net_linked";
130 if (e.model == "" || e.modelindex == 0)
134 _setmodel(e, "null");
138 setSendEntity(e, sendfunc);
139 e.SendFlags = 0xFFFFFF;
141 if (!docull) e.effects |= EF_NODEPTHTEST;
145 e.nextthink = time + dt;
146 setthink(e, SUB_Remove);
150 void Net_UnlinkEntity(entity e)
152 setSendEntity(e, func_null);
155 .void(entity this) uncustomizeentityforclient;
156 .float uncustomizeentityforclient_set;
158 void SetCustomizer(entity e, bool(entity this, entity client) customizer, void(entity this) uncustomizer)
160 setcefc(e, customizer);
161 e.uncustomizeentityforclient = uncustomizer;
162 e.uncustomizeentityforclient_set = !!uncustomizer;
164 IL_PUSH(g_uncustomizables, e);
167 void UncustomizeEntitiesRun()
169 IL_EACH(g_uncustomizables, it.uncustomizeentityforclient_set, it.uncustomizeentityforclient(it));
172 STRING_ITERATOR(g_buf, string_null, 0);
176 void Net_ClientCommand(entity sender, string command)
178 // command matches `c2s "(.+)"`
179 string buf = substring(command, argv_start_index(1) + 1, -2);
180 if (buf == "") return;
181 STRING_ITERATOR_SET(g_buf, buf, 0);
182 for (int C2S; (C2S = ReadByte()) >= 0; )
184 entity reader = REGISTRY_GET(C2S_Protocol, C2S);
185 if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
186 LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d", C2S);
190 int expected = strlen(buf);
191 if (g_buf_i > expected) LOG_WARNF("Underflow: %d", g_buf_i - expected);
192 if (g_buf_i < expected) LOG_WARNF("Overrflow: %d", expected - g_buf_i);
198 const int MSG_C2S = 0;
200 #define Net_Accept(classname) \
202 if (!this) this = new(classname); \
204 #define Net_Reject() \
206 if (this) delete(this); \
213 if (g_buf == "") return;
214 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
220 #define WriteHeader(to, id) \
221 WriteByte(to, NET_##id.m_id)
223 #define WriteHeader(to, id) \
225 if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
226 WriteByte(to, NET_##id.m_id); \
227 bool _net_valid = false; serialize_marker(to, _net_valid); \
231 // serialization: new style
235 #define stream_reading(stream) false
236 #define stream_writing(stream) true
238 #define stream_reading(stream) true
239 #define stream_writing(stream) false
242 #define serialize(T, stream, ...) \
244 noref Stream _stream = stream; \
245 serialize_##T(_stream, __VA_ARGS__); \
249 #define serialize_byte(stream, this) \
251 WriteByte(stream, this); \
254 #define serialize_byte(stream, this) \
261 #define serialize_float(stream, this) \
263 WriteCoord(stream, this); \
266 #define serialize_float(stream, this) \
268 this = ReadCoord(); \
272 #define serialize_vector(stream, this) \
275 serialize_float(stream, _v.x); \
276 serialize_float(stream, _v.y); \
277 serialize_float(stream, _v.z); \
281 #define serialize_marker(stream, this) \
286 int _de = 0xDE, _ad = 0xAD, _be = 0xBE, _ef = 0xEF; \
287 serialize_byte(stream, _de); \
288 serialize_byte(stream, _ad); \
289 serialize_byte(stream, _be); \
290 serialize_byte(stream, _ef); \
291 this = (_de == 0xDE && _ad == 0xAD && _be == 0xBE && _ef == 0xEF); \
295 // serialization: old
297 #define ReadRegistered(r) REGISTRY_GET(r, Read_byte())
298 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
300 #define Read_byte() ReadByte()
301 #define Write_byte(to, f) WriteByte(to, f)
305 void WriteByte(int to, int b)
307 assert(to == MSG_C2S);
308 string s = string_null;
310 string tmp = strcat(g_buf, s);
313 void WriteShort(int to, int b)
315 WriteByte(to, (b >> 8) & 0xFF);
316 WriteByte(to, b & 0xFF);
322 ydec_single(g_buf, ret);
327 return (ReadByte() << 8) | (ReadByte());
329 void WriteByte(int to, int b);
332 #define Read_int() ReadInt24_t()
333 #define Write_int(to, f) WriteInt24_t(to, f)
335 #define Read_float() ReadFloat()
336 #define Write_float(to, f) WriteFloat(to, f)
338 #define Read_string() ReadString()
339 #define Write_string(to, f) WriteString(to, f)
342 const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
343 #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
344 #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
348 entity ReadCSQCEntity()
351 if (f == 0) return NULL;
352 return findfloat(NULL, entnum, f);
356 int v = ReadShort() << 8; // note: this is signed
357 v += ReadByte(); // note: this is unsigned
360 #define ReadInt48_t() vec2(ReadInt24_t(), ReadInt24_t())
361 #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t())
363 noref int _ReadSByte;
364 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
365 #define ReadFloat() ReadCoord()
366 #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
367 #define ReadVector2D() vec2(ReadFloat(), ReadFloat())
368 #define ReadAngleVector() vec3(ReadAngle(), ReadAngle(), ReadAngle())
369 #define ReadAngleVector2D() vec2(ReadAngle(), ReadAngle())
371 int Readbits(int num)
373 if (num > 16) return ReadInt24_t();
374 if (num > 8) return ReadShort();
378 float ReadApproxPastTime()
380 float dt = ReadByte();
382 // map from range...PPROXPASTTIME_MAX / 256
383 dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
385 return servertime - dt;
389 void WriteInt24_t(float dst, float val)
392 WriteShort(dst, (v = floor(val >> 8)));
393 WriteByte(dst, val - (v << 8)); // 0..255
395 void WriteInt48_t(float dst, vector val)
397 WriteInt24_t(dst, val.x);
398 WriteInt24_t(dst, val.y);
400 void WriteInt72_t(float dst, vector val)
402 WriteInt24_t(dst, val.x);
403 WriteInt24_t(dst, val.y);
404 WriteInt24_t(dst, val.z);
407 #define WriteFloat(to, f) WriteCoord(to, f)
408 #define WriteVector(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); MACRO_END
409 #define WriteVector2D(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); MACRO_END
410 #define WriteAngleVector(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); WriteAngle(to, v.z); MACRO_END
411 #define WriteAngleVector2D(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); MACRO_END
413 void Writebits(float dst, float val, int num)
415 if (num > 16) { WriteInt24_t(dst, val); return; }
416 if (num > 8) { WriteShort(dst, val); return; }
420 // this will use the value:
422 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
423 // accuracy at x is 1/derivative, i.e.
424 // APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
425 void WriteApproxPastTime(float dst, float t)
429 // warning: this is approximate; do not resend when you don't have to!
430 // be careful with sendflags here!
431 // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
434 dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
437 dt = rint(bound(0, dt, 255));
442 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
443 #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN \
444 entity prev = msg_entity; \
446 FOREACH_CLIENT(IS_REAL_CLIENT(it), { \
447 if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \