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 == "")
122 LOG_WARN("Net_LinkEntity called on an entity without a classname, assigning default");
123 e.classname = "net_linked";
126 if (e.model == "" || e.modelindex == 0)
130 _setmodel(e, "null");
134 setSendEntity(e, sendfunc);
135 e.SendFlags = 0xFFFFFF;
137 if (!docull) e.effects |= EF_NODEPTHTEST;
141 e.nextthink = time + dt;
142 setthink(e, SUB_Remove);
146 void Net_UnlinkEntity(entity e)
148 setSendEntity(e, func_null);
151 .void(entity this) uncustomizeentityforclient;
152 .float uncustomizeentityforclient_set;
154 void SetCustomizer(entity e, bool(entity this, entity client) customizer, void(entity this) uncustomizer)
156 setcefc(e, customizer);
157 e.uncustomizeentityforclient = uncustomizer;
158 e.uncustomizeentityforclient_set = !!uncustomizer;
160 IL_PUSH(g_uncustomizables, e);
163 void UncustomizeEntitiesRun()
165 IL_EACH(g_uncustomizables, it.uncustomizeentityforclient_set, it.uncustomizeentityforclient(it));
168 STRING_ITERATOR(g_buf, string_null, 0);
172 void Net_ClientCommand(entity sender, string command)
174 // command matches `c2s "(.+)"`
175 string buf = substring(command, argv_start_index(1) + 1, -2);
176 if (buf == "") return;
177 STRING_ITERATOR_SET(g_buf, buf, 0);
178 for (int C2S; (C2S = ReadByte()) >= 0; )
180 entity reader = REGISTRY_GET(C2S_Protocol, C2S);
181 if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
182 LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d", C2S);
186 int expected = strlen(buf);
187 if (g_buf_i > expected) LOG_WARNF("Underflow: %d", g_buf_i - expected);
188 if (g_buf_i < expected) LOG_WARNF("Overrflow: %d", expected - g_buf_i);
194 const int MSG_C2S = 0;
196 #define Net_Accept(classname) \
198 if (!this) this = new(classname); \
200 #define Net_Reject() \
202 if (this) delete(this); \
209 if (g_buf == "") return;
210 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
216 #define WriteHeader(to, id) \
217 WriteByte(to, NET_##id.m_id)
219 #define WriteHeader(to, id) \
221 if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
222 WriteByte(to, NET_##id.m_id); \
223 bool _net_valid = false; serialize_marker(to, _net_valid); \
227 // serialization: new style
231 #define stream_reading(stream) false
232 #define stream_writing(stream) true
234 #define stream_reading(stream) true
235 #define stream_writing(stream) false
238 #define serialize(T, stream, ...) \
240 noref Stream _stream = stream; \
241 serialize_##T(_stream, __VA_ARGS__); \
245 #define serialize_byte(stream, this) \
247 WriteByte(stream, this); \
250 #define serialize_byte(stream, this) \
257 #define serialize_float(stream, this) \
259 WriteCoord(stream, this); \
262 #define serialize_float(stream, this) \
264 this = ReadCoord(); \
268 #define serialize_vector(stream, this) \
271 serialize_float(stream, _v.x); \
272 serialize_float(stream, _v.y); \
273 serialize_float(stream, _v.z); \
277 #define serialize_marker(stream, this) \
282 int _de = 0xDE, _ad = 0xAD, _be = 0xBE, _ef = 0xEF; \
283 serialize_byte(stream, _de); \
284 serialize_byte(stream, _ad); \
285 serialize_byte(stream, _be); \
286 serialize_byte(stream, _ef); \
287 this = (_de == 0xDE && _ad == 0xAD && _be == 0xBE && _ef == 0xEF); \
291 // serialization: old
293 #define ReadRegistered(r) REGISTRY_GET(r, Read_byte())
294 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
296 #define Read_byte() ReadByte()
297 #define Write_byte(to, f) WriteByte(to, f)
301 void WriteByte(int to, int b)
303 assert(to == MSG_C2S);
304 string s = string_null;
306 string tmp = strcat(g_buf, s);
309 void WriteShort(int to, int b)
311 WriteByte(to, (b >> 8) & 0xFF);
312 WriteByte(to, b & 0xFF);
318 ydec_single(g_buf, ret);
323 return (ReadByte() << 8) | (ReadByte());
325 void WriteByte(int to, int b);
328 #define Read_int() ReadInt24_t()
329 #define Write_int(to, f) WriteInt24_t(to, f)
331 #define Read_float() ReadFloat()
332 #define Write_float(to, f) WriteFloat(to, f)
334 #define Read_string() ReadString()
335 #define Write_string(to, f) WriteString(to, f)
338 const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
339 #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
340 #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
344 entity ReadCSQCEntity()
347 if (f == 0) return NULL;
348 return findfloat(NULL, entnum, f);
352 int v = ReadShort() << 8; // note: this is signed
353 v += ReadByte(); // note: this is unsigned
356 #define ReadInt48_t() vec2(ReadInt24_t(), ReadInt24_t())
357 #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t())
359 noref int _ReadSByte;
360 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
361 #define ReadFloat() ReadCoord()
362 #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
363 #define ReadVector2D() vec2(ReadFloat(), ReadFloat())
364 #define ReadAngleVector() vec3(ReadAngle(), ReadAngle(), ReadAngle())
365 #define ReadAngleVector2D() vec2(ReadAngle(), ReadAngle())
367 int Readbits(int num)
369 if (num > 16) return ReadInt24_t();
370 if (num > 8) return ReadShort();
374 float ReadApproxPastTime()
376 float dt = ReadByte();
378 // map from range...PPROXPASTTIME_MAX / 256
379 dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
381 return servertime - dt;
385 void WriteInt24_t(float dst, float val)
388 WriteShort(dst, (v = floor(val >> 8)));
389 WriteByte(dst, val - (v << 8)); // 0..255
391 void WriteInt48_t(float dst, vector val)
393 WriteInt24_t(dst, val.x);
394 WriteInt24_t(dst, val.y);
396 void WriteInt72_t(float dst, vector val)
398 WriteInt24_t(dst, val.x);
399 WriteInt24_t(dst, val.y);
400 WriteInt24_t(dst, val.z);
403 #define WriteFloat(to, f) WriteCoord(to, f)
404 #define WriteVector(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); MACRO_END
405 #define WriteVector2D(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); MACRO_END
406 #define WriteAngleVector(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); WriteAngle(to, v.z); MACRO_END
407 #define WriteAngleVector2D(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); MACRO_END
409 void Writebits(float dst, float val, int num)
411 if (num > 16) { WriteInt24_t(dst, val); return; }
412 if (num > 8) { WriteShort(dst, val); return; }
416 // this will use the value:
418 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
419 // accuracy at x is 1/derivative, i.e.
420 // APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
421 void WriteApproxPastTime(float dst, float t)
425 // warning: this is approximate; do not resend when you don't have to!
426 // be careful with sendflags here!
427 // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
430 dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
433 dt = rint(bound(0, dt, 255));
438 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
439 #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN \
440 entity prev = msg_entity; \
442 FOREACH_CLIENT(IS_REAL_CLIENT(it), { \
443 if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \