9 .bool(entity this, entity sender, bool isNew) m_read;
10 #define NET_HANDLE(id, param) bool Net_Handle_##id(entity this, entity sender, param)
14 #define REGISTER_NET_TEMP(id) \
15 NET_HANDLE(id, bool); \
16 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
19 this.m_read = Net_Handle_##id; \
22 #define REGISTER_NET_TEMP(id) \
23 const bool NET_##id##_istemp = true; \
24 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
29 #define REGISTER_NET_S2C(id) REGISTER_NET_TEMP(id)
31 REGISTRY(TempEntities, BITS(8) - 80)
32 #define TempEntities_from(i) _TempEntities_from(i, NULL)
33 REGISTER_REGISTRY(TempEntities)
34 REGISTRY_SORT(TempEntities)
35 REGISTRY_CHECK(TempEntities)
36 STATIC_INIT(RegisterTempEntities_renumber) { FOREACH(TempEntities, true, it.m_id = 80 + i); }
41 #define REGISTER_NET_LINKED(id) \
42 [[accumulate]] NET_HANDLE(id, bool isnew) \
45 this.sourceLoc = __FILE__ ":" STR(__LINE__); \
46 if (!this) isnew = true; \
48 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
51 this.m_read = Net_Handle_##id; \
54 #define REGISTER_NET_LINKED(id) \
55 const bool NET_##id##_istemp = false; \
56 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
62 REGISTRY(LinkedEntities, BITS(8) - 1)
63 #define LinkedEntities_from(i) _LinkedEntities_from(i, NULL)
64 REGISTER_REGISTRY(LinkedEntities)
65 REGISTRY_SORT(LinkedEntities)
66 REGISTRY_CHECK(LinkedEntities)
67 STATIC_INIT(RegisterLinkedEntities_renumber) { FOREACH(LinkedEntities, true, it.m_id = 1 + i); }
72 #define REGISTER_NET_C2S(id) \
73 NET_HANDLE(id, bool); \
74 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
77 this.m_read = Net_Handle_##id; \
80 #define REGISTER_NET_C2S(id) \
81 const bool NET_##id##_istemp = true; \
82 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
88 REGISTRY(C2S_Protocol, BITS(8) - 1)
89 #define C2S_Protocol_from(i) _C2S_Protocol_from(i, NULL)
90 REGISTER_REGISTRY(C2S_Protocol)
91 REGISTRY_SORT(C2S_Protocol)
92 REGISTRY_CHECK(C2S_Protocol)
93 STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); }
96 const int MSG_ENTITY = 5;
98 .int Version; // deprecated, use SendFlags
101 void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
103 if (e.classname == "") e.classname = "net_linked";
105 if (e.model == "" || e.modelindex == 0)
109 _setmodel(e, "null");
113 setSendEntity(e, sendfunc);
114 e.SendFlags = 0xFFFFFF;
116 if (!docull) e.effects |= EF_NODEPTHTEST;
120 e.nextthink = time + dt;
121 setthink(e, SUB_Remove);
125 void Net_UnlinkEntity(entity e)
127 setSendEntity(e, func_null);
130 .void(entity this) uncustomizeentityforclient;
131 .float uncustomizeentityforclient_set;
133 void SetCustomizer(entity e, bool(entity this, entity client) customizer, void(entity this) uncustomizer)
135 setcefc(e, customizer);
136 e.uncustomizeentityforclient = uncustomizer;
137 e.uncustomizeentityforclient_set = !!uncustomizer;
140 void UncustomizeEntitiesRun()
142 FOREACH_ENTITY_FLOAT(uncustomizeentityforclient_set, true, it.uncustomizeentityforclient(it));
145 STRING_ITERATOR(g_buf, string_null, 0);
149 void Net_ClientCommand(entity sender, string command)
151 // command matches `c2s "(.+)"`
152 string buf = substring(command, argv_start_index(1) + 1, -2);
153 if (buf == "") return;
154 STRING_ITERATOR_SET(g_buf, buf, 0);
155 for (int C2S; (C2S = ReadByte()) >= 0; )
157 entity reader = C2S_Protocol_from(C2S);
158 if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
159 LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d", C2S);
163 int expected = strlen(buf);
164 if (g_buf_i > expected) LOG_WARNF("Underflow: %d", g_buf_i - expected);
165 if (g_buf_i < expected) LOG_WARNF("Overrflow: %d", expected - g_buf_i);
171 const int MSG_C2S = 0;
173 #define Net_Accept(classname) \
175 if (!this) this = new(classname); \
177 #define Net_Reject() \
179 if (this) delete(this); \
186 if (g_buf == "") return;
187 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
194 #define WriteHeader(to, id) \
196 WriteByte(to, NET_##id.m_id); \
199 #define WriteHeader(to, id) \
201 if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
202 WriteByte(to, NET_##id.m_id); \
206 // serialization: new style
210 #define stream_reading(stream) false
211 #define stream_writing(stream) true
213 #define stream_reading(stream) true
214 #define stream_writing(stream) false
217 #define serialize(T, stream, ...) serialize_##T(stream, __VA_ARGS__)
220 #define serialize_byte(stream, this) \
222 WriteByte(stream, this); \
225 #define serialize_byte(stream, this) \
232 #define serialize_float(stream, this) \
234 WriteCoord(stream, this); \
237 #define serialize_float(stream, this) \
239 this = ReadCoord(); \
243 #define serialize_vector(stream, this) \
246 serialize_float(stream, _v.x); \
247 serialize_float(stream, _v.y); \
248 serialize_float(stream, _v.z); \
252 // serialization: old
254 #define ReadRegistered(r) r##_from(Read_byte())
255 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
257 #define Read_byte() ReadByte()
258 #define Write_byte(to, f) WriteByte(to, f)
262 void WriteByte(int to, int b)
264 assert(to == MSG_C2S);
265 string s = string_null;
267 string tmp = strcat(g_buf, s);
268 if (g_buf) strunzone(g_buf);
269 g_buf = strzone(tmp);
275 ydec_single(g_buf, ret);
278 void WriteByte(int to, int b);
281 #define Read_int() ReadInt24_t()
282 #define Write_int(to, f) WriteInt24_t(to, f)
284 #define Read_float() ReadFloat()
285 #define Write_float(to, f) WriteFloat(to, f)
287 #define Read_string() ReadString()
288 #define Write_string(to, f) WriteString(to, f)
291 const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
292 #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
293 #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
296 entity ReadCSQCEntity()
299 if (f == 0) return NULL;
300 return findfloat(NULL, entnum, f);
304 int v = ReadShort() << 8; // note: this is signed
305 v += ReadByte(); // note: this is unsigned
308 #define ReadInt48_t() vec3(ReadInt24_t(), ReadInt24_t(), 0)
309 #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t())
312 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
313 #define ReadFloat() ReadCoord()
314 #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
315 #define ReadVector2D() vec3(ReadFloat(), ReadFloat(), 0)
317 float ReadApproxPastTime()
319 float dt = ReadByte();
321 // map from range...PPROXPASTTIME_MAX / 256
322 dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
324 return servertime - dt;
328 void WriteInt24_t(float dst, float val)
331 WriteShort(dst, (v = floor(val >> 8)));
332 WriteByte(dst, val - (v << 8)); // 0..255
334 void WriteInt48_t(float dst, vector val)
336 WriteInt24_t(dst, val.x);
337 WriteInt24_t(dst, val.y);
339 void WriteInt72_t(float dst, vector val)
341 WriteInt24_t(dst, val.x);
342 WriteInt24_t(dst, val.y);
343 WriteInt24_t(dst, val.z);
346 #define WriteFloat(to, f) WriteCoord(to, f)
347 #define WriteVector(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); } MACRO_END
348 #define WriteVector2D(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); } MACRO_END
350 // this will use the value:
352 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
353 // accuracy at x is 1/derivative, i.e.
354 // APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
355 void WriteApproxPastTime(float dst, float t)
359 // warning: this is approximate; do not resend when you don't have to!
360 // be careful with sendflags here!
361 // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
364 dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
367 dt = rint(bound(0, dt, 255));
372 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
373 #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN { \
374 entity prev = msg_entity; \
376 FOREACH_CLIENT(IS_REAL_CLIENT(it), { \
377 if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \