10 .bool(entity this, entity sender, bool isNew) m_read;
11 #define NET_HANDLE(id, param) bool Net_Handle_##id(entity this, entity sender, param)
15 #define REGISTER_NET_TEMP(id) \
16 NET_HANDLE(id, bool); \
17 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
20 this.m_read = Net_Handle_##id; \
23 #define REGISTER_NET_TEMP(id) \
24 const bool NET_##id##_istemp = true; \
25 REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
30 #define REGISTER_NET_S2C(id) REGISTER_NET_TEMP(id)
32 REGISTRY(TempEntities, BITS(8) - 80)
33 #define TempEntities_from(i) _TempEntities_from(i, NULL)
34 REGISTER_REGISTRY(TempEntities)
35 REGISTRY_SORT(TempEntities)
36 REGISTRY_CHECK(TempEntities)
37 STATIC_INIT(RegisterTempEntities_renumber) { FOREACH(TempEntities, true, it.m_id = 80 + i); }
42 #define REGISTER_NET_LINKED(id) \
43 [[accumulate]] NET_HANDLE(id, bool isnew) \
46 this.sourceLocFile = __FILE__; \
47 this.sourceLocLine = __LINE__; \
48 if (!this) isnew = true; \
50 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
53 this.m_read = Net_Handle_##id; \
56 #define REGISTER_NET_LINKED(id) \
57 const bool NET_##id##_istemp = false; \
58 REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
64 REGISTRY(LinkedEntities, BITS(8) - 1)
65 #define LinkedEntities_from(i) _LinkedEntities_from(i, NULL)
66 REGISTER_REGISTRY(LinkedEntities)
67 REGISTRY_SORT(LinkedEntities)
68 REGISTRY_CHECK(LinkedEntities)
69 STATIC_INIT(RegisterLinkedEntities_renumber) { FOREACH(LinkedEntities, true, it.m_id = 1 + i); }
74 #define REGISTER_NET_C2S(id) \
75 NET_HANDLE(id, bool); \
76 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
79 this.m_read = Net_Handle_##id; \
82 #define REGISTER_NET_C2S(id) \
83 const bool NET_##id##_istemp = true; \
84 REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
90 REGISTRY(C2S_Protocol, BITS(8) - 1)
91 #define C2S_Protocol_from(i) _C2S_Protocol_from(i, NULL)
92 REGISTER_REGISTRY(C2S_Protocol)
93 REGISTRY_SORT(C2S_Protocol)
94 REGISTRY_CHECK(C2S_Protocol)
95 STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); }
98 const int MSG_ENTITY = 5;
100 .int Version; // deprecated, use SendFlags
102 .bool(entity to, int sendflags) SendEntity;
103 /** return false to remove from the client */
104 .bool(entity this, entity to, int sendflags) SendEntity3;
106 bool SendEntity_self(entity to, int sendflags) { return self.SendEntity3(self, to, sendflags); }
108 void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
110 if (e.classname == "") e.classname = "net_linked";
112 if (e.model == "" || self.modelindex == 0)
116 _setmodel(e, "null");
120 e.SendEntity = SendEntity_self;
121 e.SendEntity3 = sendfunc;
122 e.SendFlags = 0xFFFFFF;
124 if (!docull) e.effects |= EF_NODEPTHTEST;
128 e.nextthink = time + dt;
129 e.think = SUB_Remove_self;
133 void Net_UnlinkEntity(entity e)
135 e.SendEntity = func_null;
138 .void() uncustomizeentityforclient;
139 .float uncustomizeentityforclient_set;
141 void SetCustomizer(entity e, float() customizer, void() uncustomizer)
143 e.customizeentityforclient = customizer;
144 e.uncustomizeentityforclient = uncustomizer;
145 e.uncustomizeentityforclient_set = !!uncustomizer;
148 void UncustomizeEntitiesRun()
150 FOREACH_ENTITY_FLOAT(uncustomizeentityforclient_set, true, LAMBDA(
151 WITH(entity, self, it, it.uncustomizeentityforclient());
155 STRING_ITERATOR(g_buf, string_null, 0);
159 void Net_ClientCommand(entity sender, string command)
161 // command matches `c2s "(.+)"`
162 string buf = substring(command, argv_start_index(1) + 1, -2);
163 if (buf == "") return;
164 STRING_ITERATOR_SET(g_buf, buf, 0);
165 for (int C2S; (C2S = ReadByte()) >= 0; )
167 entity reader = C2S_Protocol_from(C2S);
168 if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
169 LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d\n", C2S);
173 int expected = strlen(buf);
174 if (g_buf_i > expected) LOG_WARNINGF("Underflow: %d", g_buf_i - expected);
175 if (g_buf_i < expected) LOG_WARNINGF("Overrflow: %d", expected - g_buf_i);
181 const int MSG_C2S = 0;
183 #define Net_Accept(classname) \
185 if (!this) this = new(classname); \
187 #define Net_Reject() \
189 if (this) remove(this); \
196 if (g_buf == "") return;
197 localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
204 #define WriteHeader(to, id) \
206 WriteByte(to, NET_##id.m_id); \
209 #define WriteHeader(to, id) \
211 if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
212 WriteByte(to, NET_##id.m_id); \
216 #define ReadRegistered(r) r##_from(Read_byte())
217 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
219 #define Read_byte() ReadByte()
220 #define Write_byte(to, f) WriteByte(to, f)
224 void WriteByte(int to, int b)
226 assert(to == MSG_C2S);
227 string s = string_null;
229 string tmp = strcat(g_buf, s);
230 if (g_buf) strunzone(g_buf);
231 g_buf = strzone(tmp);
237 ydec_single(g_buf, ret);
240 void WriteByte(int to, int b);
243 #define Read_int() ReadInt24_t()
244 #define Write_int(to, f) WriteInt24_t(to, f)
246 #define Read_float() ReadFloat()
247 #define Write_float(to, f) WriteFloat(to, f)
249 #define Read_string() ReadString()
250 #define Write_string(to, f) WriteString(to, f)
253 const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
254 #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
255 #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
258 entity ReadCSQCEntity()
261 if (f == 0) return NULL;
262 return findfloat(NULL, entnum, f);
266 int v = ReadShort() << 8; // note: this is signed
267 v += ReadByte(); // note: this is unsigned
288 #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
289 #define ReadFloat() ReadCoord()
290 vector ReadVector() { vector v; v.x = ReadFloat(); v.y = ReadFloat(); v.z = ReadFloat(); return v; }
291 vector ReadVector2D() { vector v; v.x = ReadFloat(); v.y = ReadFloat(); v.z = 0; return v; }
293 float ReadApproxPastTime()
295 float dt = ReadByte();
297 // map from range...PPROXPASTTIME_MAX / 256
298 dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
300 return servertime - dt;
304 void WriteInt24_t(float dst, float val)
307 WriteShort(dst, (v = floor(val >> 8)));
308 WriteByte(dst, val - (v << 8)); // 0..255
310 void WriteInt48_t(float dst, vector val)
312 WriteInt24_t(dst, val.x);
313 WriteInt24_t(dst, val.y);
315 void WriteInt72_t(float dst, vector val)
317 WriteInt24_t(dst, val.x);
318 WriteInt24_t(dst, val.y);
319 WriteInt24_t(dst, val.z);
322 #define WriteFloat(to, f) WriteCoord(to, f)
323 #define WriteVector(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); } MACRO_END
324 #define WriteVector2D(to, v) MACRO_BEGIN { WriteFloat(to, v.x); WriteFloat(to, v.y); } MACRO_END
326 // this will use the value:
328 // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
329 // accuracy at x is 1/derivative, i.e.
330 // APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
331 void WriteApproxPastTime(float dst, float t)
335 // warning: this is approximate; do not resend when you don't have to!
336 // be careful with sendflags here!
337 // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
340 dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
343 dt = rint(bound(0, dt, 255));
348 // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
349 #define WRITESPECTATABLE_MSG_ONE_VARNAME(varname, statement) \
350 entity varname; varname = msg_entity; \
351 FOR_EACH_REALCLIENT(msg_entity) \
352 if (msg_entity == varname || (msg_entity.classname == STR_SPECTATOR && msg_entity.enemy == varname)) \
353 statement msg_entity = varname
354 #define WRITESPECTATABLE_MSG_ONE(statement) \
357 WRITESPECTATABLE_MSG_ONE_VARNAME(oldmsg_entity, statement); \
359 #define WRITESPECTATABLE(msg, statement) \
360 if (msg == MSG_ONE) WRITESPECTATABLE_MSG_ONE(statement); \
362 statement float WRITESPECTATABLE_workaround = 0