4 #include <common/gamemodes/_mod.qh>
5 #include <common/resources.qh>
8 #include <common/gamemodes/_mod.qh>
9 #include <common/resources.qh>
10 #include <server/resources.qh>
13 REGISTRY(EntCSProps, BITS(16) - 1)
14 REGISTER_REGISTRY(EntCSProps)
15 REGISTRY_SORT(EntCSProps)
16 REGISTRY_CHECK(EntCSProps)
18 REGISTRY_DEFINE_GET(EntCSProps, NULL)
19 STATIC_INIT(EntCSProps_renumber) { FOREACH(EntCSProps, true, it.m_id = i); }
21 // these entcs_props ids need to be referenced directly
22 int ENTCS_PROP_ENTNUM_id = 0;
23 int ENTCS_PROP_ORIGIN_id = 0;
24 int ENTCS_PROP_HEALTH_id = 0;
25 STATIC_INIT(EntCSProps_setglobalids)
27 FOREACH(EntCSProps, true, {
28 if (it.registered_id == "ENTCS_PROP_ENTNUM") ENTCS_PROP_ENTNUM_id = it.m_id;
29 if (it.registered_id == "ENTCS_PROP_ORIGIN") ENTCS_PROP_ORIGIN_id = it.m_id;
30 if (it.registered_id == "ENTCS_PROP_HEALTH") ENTCS_PROP_HEALTH_id = it.m_id;
35 // Force an origin update, for player sounds
36 void entcs_force_origin(entity player)
38 CS(player).entcs.m_forceupdate = BIT(ENTCS_PROP_ORIGIN_id);
43 .bool(entity ent, entity player) m_check;
44 .void(entity ent, entity player) m_set;
45 .void(int chan, entity ent) m_send;
46 .void(entity ent) m_receive;
49 #define _ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive) \
50 void id##_set(entity ent, entity player) { setprop(ent.(checkprop), player.(checkprop_pl)); } \
51 void id##_send(int chan, entity ent) { LAMBDA(svsend); } \
52 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
53 this.m_public = ispublic; \
54 this.m_check = id##_check; \
55 this.m_set = id##_set; \
56 this.m_send = id##_send; \
59 #define ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive) \
60 bool id##_check(entity ent, entity player) { return (ent.(checkprop) != player.(checkprop_pl)); } \
61 _ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive)
63 #define ENTCS_PROP_CODED(id, ispublic, checkprop, checkprop_pl, setprop, decfactor, svsend, clreceive) \
64 bool id##_check(entity ent, entity player) { \
65 return (floor(ent.(checkprop)) / decfactor != floor(player.(checkprop_pl)) / decfactor); \
67 _ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive)
70 #define ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive) \
71 void id##_receive(entity ent) { LAMBDA(clreceive); } \
72 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
73 this.m_public = ispublic; \
74 this.m_receive = id##_receive; \
77 #define ENTCS_PROP_CODED(id, ispublic, checkprop, checkprop_pl, setprop, decfactor, svsend, clreceive) \
78 ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive)
82 #define ENTCS_PROP_RESOURCE(id, ispublic, checkprop, setprop, decfactor, svsend, clreceive) \
83 bool id##_check(entity ent, entity player) { \
84 return (floor(GetResource(ent, checkprop) / decfactor) != floor(GetResource(player, checkprop) / decfactor)); \
86 void id##_set(entity ent, entity player) { SetResourceExplicit(ent, checkprop, GetResource(player, checkprop)); } \
87 void id##_send(int chan, entity ent) { LAMBDA(svsend); } \
88 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
89 this.m_public = ispublic; \
90 this.m_check = id##_check; \
91 this.m_set = id##_set; \
92 this.m_send = id##_send; \
95 #define ENTCS_PROP_RESOURCE(id, ispublic, checkprop, setprop, decfactor, svsend, clreceive) \
96 void id##_receive(entity ent) { LAMBDA(clreceive); } \
97 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
98 this.m_public = ispublic; \
99 this.m_receive = id##_receive; \
103 #define ENTCS_SET_NORMAL(var, x) MACRO_BEGIN \
107 /** the engine player name strings are mutable! */
108 #define ENTCS_SET_MUTABLE_STRING(var, x) MACRO_BEGIN \
112 ENTCS_PROP(ENTNUM, false, sv_entnum, sv_entnum, ENTCS_SET_NORMAL, {}, {}) /* sentinel */
114 ENTCS_PROP(ORIGIN, false, origin, origin, ENTCS_SET_NORMAL,
115 { WriteVector(chan, ent.origin); },
116 { ent.has_sv_origin = true; vector v = ReadVector(); setorigin(ent, v); })
118 #define DEC_FACTOR (360 / 32)
119 ENTCS_PROP_CODED(ANGLES, false, angles_y, angles_y, ENTCS_SET_NORMAL, DEC_FACTOR,
120 { WriteByte(chan, ent.angles.y / DEC_FACTOR); },
121 { vector v = '0 0 0'; v.y = ReadByte() * DEC_FACTOR; ent.angles = v; })
124 // FIXME: use a better scale?
125 #define DEC_FACTOR 10
126 ENTCS_PROP_RESOURCE(HEALTH, false, RES_HEALTH, ENTCS_SET_NORMAL, DEC_FACTOR,
127 { WriteByte(chan, bound(0, GetResource(ent, RES_HEALTH) / DEC_FACTOR, 255)); },
128 { ent.healthvalue = ReadByte() * DEC_FACTOR; })
130 ENTCS_PROP_RESOURCE(ARMOR, false, RES_ARMOR, ENTCS_SET_NORMAL, DEC_FACTOR,
131 { WriteByte(chan, bound(0, GetResource(ent, RES_ARMOR) / DEC_FACTOR, 255)); },
132 { SetResourceExplicit(ent, RES_ARMOR, ReadByte() * DEC_FACTOR); })
135 ENTCS_PROP(NAME, true, netname, netname, ENTCS_SET_MUTABLE_STRING,
136 { WriteString(chan, ent.netname); },
137 { strcpy(ent.netname, ReadString()); })
139 ENTCS_PROP(MODEL, true, model, model, ENTCS_SET_NORMAL,
140 { WriteString(chan, ent.model); },
141 { strcpy(ent.model, ReadString()); })
143 ENTCS_PROP(SKIN, true, skin, skin, ENTCS_SET_NORMAL,
144 { WriteByte(chan, ent.skin); },
145 { ent.skin = ReadByte(); })
147 ENTCS_PROP(CLIENTCOLORS, true, clientcolors, clientcolors, ENTCS_SET_NORMAL,
148 { WriteByte(chan, ent.clientcolors); },
149 { ent.colormap = ReadByte(); })
151 ENTCS_PROP(FRAGS, true, frags, frags, ENTCS_SET_NORMAL,
152 { WriteShort(chan, ent.frags); },
153 { ent.frags = ReadShort(); })
155 // use sv_solid to avoid changing solidity state of entcs entities
156 ENTCS_PROP(SOLID, true, sv_solid, solid, ENTCS_SET_NORMAL,
157 { WriteByte(chan, ent.sv_solid); },
158 { ent.sv_solid = ReadByte(); })
162 int ENTCS_PUBLICMASK = 0, ENTCS_PRIVATEMASK = 0;
163 STATIC_INIT(ENTCS_PUBLICMASK)
165 FOREACH(EntCSProps, true,
168 ENTCS_PUBLICMASK |= BIT(it.m_id);
170 ENTCS_PRIVATEMASK |= BIT(it.m_id);
174 void entcs_update_players(entity player)
176 FOREACH_CLIENT(it != player && IS_PLAYER(it),
178 CS(it).entcs.SendFlags |= ENTCS_PRIVATEMASK;
182 bool _entcs_send(entity this, entity to, int sf, int chan)
184 entity player = this.owner;
185 sf |= BIT(ENTCS_PROP_ENTNUM_id); // assume private
187 if (IS_PLAYER(player))
189 if (radar_showenemies) break;
190 if (SAME_TEAM(to, player)) break;
191 if (!(IS_PLAYER(to) || to.caplayer)) break;
193 sf &= ENTCS_PUBLICMASK; // no private updates
196 sf |= this.m_forceupdate;
197 this.m_forceupdate = 0;
198 if (chan == MSG_ENTITY)
199 WriteHeader(chan, ENT_CLIENT_ENTCS);
201 WriteHeader(chan, CLIENT_ENTCS);
202 WriteByte(chan, etof(player) - 1);
203 WriteShort(chan, sf);
204 FOREACH(EntCSProps, sf & BIT(it.m_id),
206 it.m_send(chan, this);
211 bool entcs_send(entity this, entity to, int sf)
213 return _entcs_send(this, to, sf, MSG_ENTITY);
216 void entcs_think(entity this)
218 this.nextthink = time + 0.033333333333; // TODO: increase this to like 0.15 once the client can do smoothing
219 entity player = this.owner;
220 FOREACH(EntCSProps, it.m_check(this, player),
222 it.m_set(this, player);
223 this.SendFlags |= BIT(it.m_id);
226 if (intermission_running)
228 // health is set to special values after the game ends, ignore any change
229 this.SendFlags &= ~BIT(ENTCS_PROP_HEALTH_id);
232 // always send origin of players even if they stand still otherwise
233 // if a teammate isn't in my pvs and his health (or view angle or name
234 // etc...) changes then his tag disappears
235 if (IS_PLAYER(this.owner))
236 this.SendFlags |= BIT(ENTCS_PROP_ORIGIN_id);
238 setorigin(this, this.origin); // relink
241 void entcs_attach(entity player)
243 entity e = CS(player).entcs = new_pure(entcs_sender);
245 setthink(e, entcs_think);
247 Net_LinkEntity(e, false, 0, entcs_send);
248 // NOTE: the following code block has been disabled as a workaround for https://gitlab.com/xonotic/xonotic-data.pk3dir/-/issues/1824
250 if (!IS_REAL_CLIENT(player)) return;
251 FOREACH_CLIENT(true, {
252 assert(CS(it).entcs);
253 _entcs_send(CS(it).entcs, msg_entity = player, BITS(23), MSG_ONE);
258 void entcs_detach(entity player)
260 if (!CS(player).entcs) return;
261 delete(CS(player).entcs);
262 CS(player).entcs = NULL;
269 void Ent_RemoveEntCS(entity this)
271 int n = this.sv_entnum;
272 entity e = entcs_receiver(n);
273 entcs_receiver(n, NULL);
276 if (e != this) delete(e);
279 void entcs_think(entity this)
281 entity e = CSQCModel_server2csqc(this.sv_entnum);
284 this.has_origin = this.has_sv_origin;
287 this.has_origin = true;
288 this.origin = e.origin;
289 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
290 if (this.model != e.model)
292 strcpy(this.model, e.model);
296 bool ReadEntcs(entity this)
299 entity e = entcs_receiver(n);
304 e = new_pure(ENT_CLIENT_ENTCS);
308 setthink(e, entcs_think);
309 entcs_receiver(n, e);
311 else if (e != this && this)
316 setthink(e, entcs_think);
317 entcs_receiver(n, e);
320 InterpolateOrigin_Undo(e);
322 int sf = ReadShort();
323 e.has_sv_origin = false;
324 e.m_entcs_private = boolean(sf & BIT(ENTCS_PROP_ENTNUM_id));
325 FOREACH(EntCSProps, sf & BIT(it.m_id),
329 e.iflags |= IFLAG_ORIGIN;
330 InterpolateOrigin_Note(e);
335 NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
340 this.entremove = Ent_RemoveEntCS;
342 return ReadEntcs(this);
345 NET_HANDLE(CLIENT_ENTCS, bool isnew)
347 return ReadEntcs(NULL);