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 #define TempEntities_from(i) _TempEntities_from(i, NULL)
44 REGISTER_REGISTRY(TempEntities)
45 REGISTRY_SORT(TempEntities)
46 REGISTRY_CHECK(TempEntities)
47 STATIC_INIT(RegisterTempEntities_renumber) { FOREACH(TempEntities, true, it.m_id = 80 + i); }
52 #define REGISTER_NET_LINKED(id) \
53 ACCUMULATE NET_HANDLE(id, bool isnew) \
56 this.sourceLoc = __FILE__ ":" STR(__LINE__); \
57 if (!this) isnew = true; \
60 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
63 this.m_read = Net_Handle_##id##_guard; \
66 #define REGISTER_NET_LINKED(id) \
67 const bool NET_##id##_istemp = false; \
68 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
74 REGISTRY(LinkedEntities, BITS(8) - 1)
75 #define LinkedEntities_from(i) _LinkedEntities_from(i, NULL)
76 REGISTER_REGISTRY(LinkedEntities)
77 REGISTRY_SORT(LinkedEntities)
78 REGISTRY_CHECK(LinkedEntities)
79 STATIC_INIT(RegisterLinkedEntities_renumber) { FOREACH(LinkedEntities, true, it.m_id = 1 + i); }
84 #define REGISTER_NET_C2S(id) \
85 NET_HANDLE(id, bool); \
86 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
89 this.m_read = Net_Handle_##id; \
92 #define REGISTER_NET_C2S(id) \
93 const bool NET_##id##_istemp = true; \
94 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
100 REGISTRY(C2S_Protocol, BITS(8) - 1)
101 #define C2S_Protocol_from(i) _C2S_Protocol_from(i, NULL)
102 REGISTER_REGISTRY(C2S_Protocol)
103 REGISTRY_SORT(C2S_Protocol)
104 REGISTRY_CHECK(C2S_Protocol)
105 STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); }
108 const int MSG_ENTITY = 5;
112 IntrusiveList g_uncustomizables;
113 STATIC_INIT(g_uncustomizables) { g_uncustomizables = IL_NEW(); }
115 void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
117 if (e.classname == "") e.classname = "net_linked";
119 if (e.model == "" || e.modelindex == 0)
123 _setmodel(e, "null");
127 setSendEntity(e, sendfunc);
128 e.SendFlags = 0xFFFFFF;
130 if (!docull) e.effects |= EF_NODEPTHTEST;
134 e.nextthink = time + dt;
135 setthink(e, SUB_Remove);
139 void Net_UnlinkEntity(entity e)
141 setSendEntity(e, func_null);
144 .void(entity this) uncustomizeentityforclient;
145 .float uncustomizeentityforclient_set;
147 void SetCustomizer(entity e, bool(entity this, entity client) customizer, void(entity this) uncustomizer)
149 setcefc(e, customizer);
150 e.uncustomizeentityforclient = uncustomizer;
151 e.uncustomizeentityforclient_set = !!uncustomizer;
153 IL_PUSH(g_uncustomizables, e);
156 void UncustomizeEntitiesRun()
158 IL_EACH(g_uncustomizables, it.uncustomizeentityforclient_set, it.uncustomizeentityforclient(it));
161 STRING_ITERATOR(g_buf, string_null, 0);
165 void Net_ClientCommand(entity sender, string command)
167 // command matches `c2s "(.+)"`
168 string buf = substring(command, argv_start_index(1) + 1, -2);
169 if (buf == "") return;
170 STRING_ITERATOR_SET(g_buf, buf, 0);
171 for (int C2S; (C2S = ReadByte()) >= 0; )
173 entity reader = C2S_Protocol_from(C2S);
174 if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
175 LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d", C2S);
179 int expected = strlen(buf);
180 if (g_buf_i > expected) LOG_WARNF("Underflow: %d", g_buf_i - expected);
181 if (g_buf_i < expected) LOG_WARNF("Overrflow: %d", expected - g_buf_i);
187 const int MSG_C2S = 0;
189 #define Net_Accept(classname) \
191 if (!this) this = new(classname); \
193 #define Net_Reject() \
195 if (this) delete(this); \
202 if (g_buf == "") return;
203 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
209 #define WriteHeader(to, id) \
211 WriteByte(to, NET_##id.m_id); \
214 #define WriteHeader(to, id) \
216 if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
217 WriteByte(to, NET_##id.m_id); \
218 bool _net_valid = false; serialize_marker(to, _net_valid); \
222 // serialization: new style
226 #define stream_reading(stream) false
227 #define stream_writing(stream) true
229 #define stream_reading(stream) true
230 #define stream_writing(stream) false
233 #define serialize(T, stream, ...) \
235 noref Stream _stream = stream; \
236 serialize_##T(_stream, __VA_ARGS__); \
240 #define serialize_byte(stream, this) \
242 WriteByte(stream, this); \
245 #define serialize_byte(stream, this) \
252 #define serialize_float(stream, this) \
254 WriteCoord(stream, this); \
257 #define serialize_float(stream, this) \
259 this = ReadCoord(); \
263 #define serialize_vector(stream, this) \
266 serialize_float(stream, _v.x); \
267 serialize_float(stream, _v.y); \
268 serialize_float(stream, _v.z); \
272 #define serialize_marker(stream, this) \
277 int _de = 0xDE, _ad = 0xAD, _be = 0xBE, _ef = 0xEF; \
278 serialize_byte(stream, _de); \
279 serialize_byte(stream, _ad); \
280 serialize_byte(stream, _be); \
281 serialize_byte(stream, _ef); \
282 this = (_de == 0xDE && _ad == 0xAD && _be == 0xBE && _ef == 0xEF); \
286 // serialization: old
288 #define ReadRegistered(r) r##_from(Read_byte())
289 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
291 #define Read_byte() ReadByte()
292 #define Write_byte(to, f) WriteByte(to, f)
296 void WriteByte(int to, int b)
298 assert(to == MSG_C2S);
299 string s = string_null;
301 string tmp = strcat(g_buf, s);
304 void WriteShort(int to, int b)
306 WriteByte(to, (b >> 8) & 0xFF);
307 WriteByte(to, b & 0xFF);
313 ydec_single(g_buf, ret);
318 return (ReadByte() << 8) | (ReadByte());
320 void WriteByte(int to, int b);
323 #define Read_int() ReadInt24_t()
324 #define Write_int(to, f) WriteInt24_t(to, f)
326 #define Read_float() ReadFloat()
327 #define Write_float(to, f) WriteFloat(to, f)
329 #define Read_string() ReadString()
330 #define Write_string(to, f) WriteString(to, f)
333 const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
334 #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
335 #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
339 entity ReadCSQCEntity()
342 if (f == 0) return NULL;
343 return findfloat(NULL, entnum, f);
347 int v = ReadShort() << 8; // note: this is signed
348 v += ReadByte(); // note: this is unsigned
351 #define ReadInt48_t() vec2(ReadInt24_t(), ReadInt24_t())
352 #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t())
354 noref int _ReadSByte;
355 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
356 #define ReadFloat() ReadCoord()
357 #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
358 #define ReadVector2D() vec2(ReadFloat(), ReadFloat())
360 float ReadApproxPastTime()
362 float dt = ReadByte();
364 // map from range...PPROXPASTTIME_MAX / 256
365 dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
367 return servertime - dt;
371 void WriteInt24_t(float dst, float val)
374 WriteShort(dst, (v = floor(val >> 8)));
375 WriteByte(dst, val - (v << 8)); // 0..255
377 void WriteInt48_t(float dst, vector val)
379 WriteInt24_t(dst, val.x);
380 WriteInt24_t(dst, val.y);
382 void WriteInt72_t(float dst, vector val)
384 WriteInt24_t(dst, val.x);
385 WriteInt24_t(dst, val.y);
386 WriteInt24_t(dst, val.z);
389 #define WriteFloat(to, f) WriteCoord(to, f)
390 #define WriteVector(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); } MACRO_END
391 #define WriteVector2D(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); } MACRO_END
393 // this will use the value:
395 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
396 // accuracy at x is 1/derivative, i.e.
397 // APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
398 void WriteApproxPastTime(float dst, float t)
402 // warning: this is approximate; do not resend when you don't have to!
403 // be careful with sendflags here!
404 // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
407 dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
410 dt = rint(bound(0, dt, 255));
415 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
416 #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN { \
417 entity prev = msg_entity; \
419 FOREACH_CLIENT(IS_REAL_CLIENT(it), { \
420 if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \