2 #include <common/gamemodes/_mod.qh>
3 #include <common/resources.qh>
5 #include <server/resources.qh>
8 REGISTRY(EntCSProps, BITS(16) - 1)
9 #define EntCSProps_from(i) _EntCSProps_from(i, NULL)
10 REGISTER_REGISTRY(EntCSProps)
11 REGISTRY_SORT(EntCSProps)
12 REGISTRY_CHECK(EntCSProps)
13 STATIC_INIT(EntCSProps_renumber) { FOREACH(EntCSProps, true, it.m_id = i); }
15 // these entcs_props ids need to be referenced directly
16 int ENTCS_PROP_ENTNUM_id = 0;
17 int ENTCS_PROP_ORIGIN_id = 0;
18 STATIC_INIT(EntCSProps_setglobalids)
20 FOREACH(EntCSProps, true, {
21 if (it.registered_id == "ENTCS_PROP_ENTNUM")
22 ENTCS_PROP_ENTNUM_id = it.m_id;
23 if (it.registered_id == "ENTCS_PROP_ORIGIN")
24 ENTCS_PROP_ORIGIN_id = it.m_id;
29 // Force an origin update, for player sounds
30 void entcs_force_origin(entity player)
32 CS(player).entcs.m_forceupdate = BIT(ENTCS_PROP_ORIGIN_id);
37 .bool(entity ent, entity player) m_check;
38 .void(entity ent, entity player) m_set;
39 .void(int chan, entity ent) m_send;
40 .void(entity ent) m_receive;
43 #define _ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive) \
44 void id##_set(entity ent, entity player) { setprop(ent.(checkprop), player.(checkprop)); } \
45 void id##_send(int chan, entity ent) { LAMBDA(svsend); } \
46 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
47 this.m_public = ispublic; \
48 this.m_check = id##_check; \
49 this.m_set = id##_set; \
50 this.m_send = id##_send; \
53 #define ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive) \
54 bool id##_check(entity ent, entity player) { return (ent.(checkprop) != player.(checkprop)); } \
55 _ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive)
57 #define ENTCS_PROP_CODED(id, ispublic, checkprop, setprop, decfactor, svsend, clreceive) \
58 bool id##_check(entity ent, entity player) { \
59 return (floor(ent.(checkprop)) / decfactor != floor(player.(checkprop)) / decfactor); \
61 _ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive)
64 #define ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive) \
65 void id##_receive(entity ent) { LAMBDA(clreceive); } \
66 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
67 this.m_public = ispublic; \
68 this.m_receive = id##_receive; \
71 #define ENTCS_PROP_CODED(id, ispublic, checkprop, setprop, decfactor, svsend, clreceive) \
72 ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive)
76 #define ENTCS_PROP_RESOURCE(id, ispublic, checkprop, setprop, decfactor, svsend, clreceive) \
77 bool id##_check(entity ent, entity player) { \
78 return (floor(GetResource(ent, checkprop) / decfactor) != floor(GetResource(player, checkprop) / decfactor)); \
80 void id##_set(entity ent, entity player) { SetResourceExplicit(ent, checkprop, GetResource(player, checkprop)); } \
81 void id##_send(int chan, entity ent) { LAMBDA(svsend); } \
82 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
83 this.m_public = ispublic; \
84 this.m_check = id##_check; \
85 this.m_set = id##_set; \
86 this.m_send = id##_send; \
89 #define ENTCS_PROP_RESOURCE(id, ispublic, checkprop, setprop, decfactor, svsend, clreceive) \
90 void id##_receive(entity ent) { LAMBDA(clreceive); } \
91 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
92 this.m_public = ispublic; \
93 this.m_receive = id##_receive; \
97 #define ENTCS_SET_NORMAL(var, x) MACRO_BEGIN \
101 /** the engine player name strings are mutable! */
102 #define ENTCS_SET_MUTABLE_STRING(var, x) MACRO_BEGIN \
106 ENTCS_PROP(ENTNUM, false, sv_entnum, ENTCS_SET_NORMAL, {}, {}) /* sentinel */
108 ENTCS_PROP(ORIGIN, false, origin, ENTCS_SET_NORMAL,
109 { WriteVector(chan, ent.origin); },
110 { ent.has_sv_origin = true; vector v = ReadVector(); setorigin(ent, v); })
112 #define DEC_FACTOR (360 / 32)
113 ENTCS_PROP_CODED(ANGLES, false, angles_y, ENTCS_SET_NORMAL, DEC_FACTOR,
114 { WriteByte(chan, ent.angles.y / DEC_FACTOR); },
115 { vector v = '0 0 0'; v.y = ReadByte() * DEC_FACTOR; ent.angles = v; })
118 // FIXME: use a better scale?
119 #define DEC_FACTOR 10
120 ENTCS_PROP_RESOURCE(HEALTH, false, RES_HEALTH, ENTCS_SET_NORMAL, DEC_FACTOR,
121 { WriteByte(chan, bound(0, GetResource(ent, RES_HEALTH) / DEC_FACTOR, 255)); },
122 { ent.healthvalue = ReadByte() * DEC_FACTOR; })
124 ENTCS_PROP_RESOURCE(ARMOR, false, RES_ARMOR, ENTCS_SET_NORMAL, DEC_FACTOR,
125 { WriteByte(chan, bound(0, GetResource(ent, RES_ARMOR) / DEC_FACTOR, 255)); },
126 { SetResourceExplicit(ent, RES_ARMOR, ReadByte() * DEC_FACTOR); })
129 ENTCS_PROP(NAME, true, netname, ENTCS_SET_MUTABLE_STRING,
130 { WriteString(chan, ent.netname); },
131 { strcpy(ent.netname, ReadString()); })
133 ENTCS_PROP(MODEL, true, model, ENTCS_SET_NORMAL,
134 { WriteString(chan, ent.model); },
135 { strcpy(ent.model, ReadString()); })
137 ENTCS_PROP(SKIN, true, skin, ENTCS_SET_NORMAL,
138 { WriteByte(chan, ent.skin); },
139 { ent.skin = ReadByte(); })
141 ENTCS_PROP(CLIENTCOLORS, true, clientcolors, ENTCS_SET_NORMAL,
142 { WriteByte(chan, ent.clientcolors); },
143 { ent.colormap = ReadByte(); })
145 ENTCS_PROP(FRAGS, true, frags, ENTCS_SET_NORMAL,
146 { WriteShort(chan, ent.frags); },
147 { ent.frags = ReadShort(); })
151 int ENTCS_PUBLICMASK = 0;
152 STATIC_INIT(ENTCS_PUBLICMASK)
154 FOREACH(EntCSProps, it.m_public,
156 ENTCS_PUBLICMASK |= BIT(it.m_id);
160 bool _entcs_send(entity this, entity to, int sf, int chan)
162 entity player = this.owner;
163 sf |= BIT(ENTCS_PROP_ENTNUM_id); // assume private
165 if (IS_PLAYER(player))
167 if (radar_showennemies) break;
168 if (SAME_TEAM(to, player)) break;
169 if (!(IS_PLAYER(to) || to.caplayer) && time > game_starttime) break;
171 sf &= ENTCS_PUBLICMASK; // no private updates
174 sf |= this.m_forceupdate;
175 this.m_forceupdate = 0;
176 if (chan == MSG_ENTITY)
177 WriteHeader(chan, ENT_CLIENT_ENTCS);
179 WriteHeader(chan, CLIENT_ENTCS);
180 WriteByte(chan, etof(player) - 1);
181 WriteShort(chan, sf);
182 FOREACH(EntCSProps, sf & BIT(it.m_id),
184 it.m_set(this, player);
185 it.m_send(chan, this);
190 bool entcs_send(entity this, entity to, int sf)
192 return _entcs_send(this, to, sf, MSG_ENTITY);
195 void entcs_think(entity this)
197 this.nextthink = time + 0.033333333333; // TODO: increase this to like 0.15 once the client can do smoothing
198 entity o = this.owner;
199 FOREACH(EntCSProps, it.m_check(this, o),
201 this.SendFlags |= BIT(it.m_id);
203 setorigin(this, this.origin); // relink
206 void entcs_attach(entity player)
208 entity e = CS(player).entcs = new(entcs_sender);
210 setthink(e, entcs_think);
212 Net_LinkEntity(e, false, 0, entcs_send);
215 void entcs_detach(entity player)
217 if (!CS(player).entcs) return;
218 delete(CS(player).entcs);
219 CS(player).entcs = NULL;
226 void Ent_RemoveEntCS(entity this)
228 int n = this.sv_entnum;
229 entity e = entcs_receiver(n);
230 entcs_receiver(n, NULL);
233 if (e != this) delete(e);
236 void entcs_think(entity this)
238 entity e = CSQCModel_server2csqc(this.sv_entnum);
241 this.has_origin = this.has_sv_origin;
244 this.has_origin = true;
245 this.origin = e.origin;
246 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
247 if (this.model != e.model)
249 strcpy(this.model, e.model);
253 bool ReadEntcs(entity this)
256 entity e = entcs_receiver(n);
261 e = new_pure(entcs_receiver);
265 setthink(e, entcs_think);
266 entcs_receiver(n, e);
268 else if (e != this && this)
273 setthink(e, entcs_think);
274 entcs_receiver(n, e);
277 InterpolateOrigin_Undo(e);
279 int sf = ReadShort();
280 e.has_sv_origin = false;
281 e.m_entcs_private = boolean(sf & BIT(ENTCS_PROP_ENTNUM_id));
282 FOREACH(EntCSProps, sf & BIT(it.m_id),
286 e.iflags |= IFLAG_ORIGIN;
287 InterpolateOrigin_Note(e);
292 NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
297 this.classname = "entcs_receiver";
298 this.entremove = Ent_RemoveEntCS;
300 return ReadEntcs(this);
303 NET_HANDLE(CLIENT_ENTCS, bool isnew)
305 return ReadEntcs(NULL);