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(RegisterEntCSProps_renumber) { FOREACH(EntCSProps, true, it.m_id = i); }
16 .bool(entity ent, entity player) m_check;
17 .void(entity ent, entity player) m_set;
18 .void(int chan, entity ent) m_send;
19 .void(entity ent) m_receive;
22 #define ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive) \
23 bool id##_check(entity ent, entity player) { return (ent.(checkprop) != player.(checkprop)); } \
24 void id##_set(entity ent, entity player) { setprop(ent.(checkprop), player.(checkprop)); } \
25 void id##_send(int chan, entity ent) { LAMBDA(svsend); } \
26 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
27 this.m_public = ispublic; \
28 this.m_check = id##_check; \
29 this.m_set = id##_set; \
30 this.m_send = id##_send; \
33 #define ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive) \
34 void id##_receive(entity ent) { LAMBDA(clreceive); } \
35 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
36 this.m_public = ispublic; \
37 this.m_receive = id##_receive; \
42 #define ENTCS_PROP_RESOURCE(id, ispublic, checkprop, setprop, svsend, clreceive) \
43 bool id##_check(entity ent, entity player) { \
44 return (floor(GetResourceAmount(ent, checkprop) / 10) != floor(GetResourceAmount(player, checkprop) / 10)); \
46 void id##_set(entity ent, entity player) { SetResourceAmountExplicit(ent, checkprop, GetResourceAmount(player, checkprop)); } \
47 void id##_send(int chan, entity ent) { LAMBDA(svsend); } \
48 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
49 this.m_public = ispublic; \
50 this.m_check = id##_check; \
51 this.m_set = id##_set; \
52 this.m_send = id##_send; \
55 #define ENTCS_PROP_RESOURCE(id, ispublic, checkprop, setprop, svsend, clreceive) \
56 void id##_receive(entity ent) { LAMBDA(clreceive); } \
57 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
58 this.m_public = ispublic; \
59 this.m_receive = id##_receive; \
63 #define ENTCS_SET_NORMAL(var, x) MACRO_BEGIN \
67 /** the engine player name strings are mutable! */
68 #define ENTCS_SET_MUTABLE_STRING(var, x) MACRO_BEGIN \
72 ENTCS_PROP(ENTNUM, false, sv_entnum, ENTCS_SET_NORMAL, {}, {}) /* sentinel */
74 ENTCS_PROP(ORIGIN, false, origin, ENTCS_SET_NORMAL,
75 { WriteVector(chan, ent.origin); },
76 { ent.has_sv_origin = true; vector v = ReadVector(); setorigin(ent, v); })
78 ENTCS_PROP(ANGLES, false, angles_y, ENTCS_SET_NORMAL,
79 { WriteByte(chan, ent.angles.y / 360 * 256); },
80 { vector v = '0 0 0'; v.y = ReadByte() / 256 * 360; ent.angles = v; })
82 ENTCS_PROP_RESOURCE(HEALTH, false, RESOURCE_HEALTH, ENTCS_SET_NORMAL,
83 { WriteByte(chan, bound(0, GetResourceAmount(ent, RESOURCE_HEALTH) / 10, 255)); /* FIXME: use a better scale? */ },
84 { ent.healthvalue = ReadByte() * 10; })
86 ENTCS_PROP_RESOURCE(ARMOR, false, RESOURCE_ARMOR, ENTCS_SET_NORMAL,
87 { WriteByte(chan, bound(0, GetResourceAmount(ent, RESOURCE_ARMOR) / 10, 255)); /* FIXME: use a better scale? */ },
88 { SetResourceAmountExplicit(ent, RESOURCE_ARMOR, ReadByte() * 10); })
90 ENTCS_PROP(NAME, true, netname, ENTCS_SET_MUTABLE_STRING,
91 { WriteString(chan, ent.netname); },
92 { strcpy(ent.netname, ReadString()); })
94 ENTCS_PROP(MODEL, true, model, ENTCS_SET_NORMAL,
95 { WriteString(chan, ent.model); },
96 { strcpy(ent.model, ReadString()); })
98 ENTCS_PROP(SKIN, true, skin, ENTCS_SET_NORMAL,
99 { WriteByte(chan, ent.skin); },
100 { ent.skin = ReadByte(); })
102 ENTCS_PROP(CLIENTCOLORS, true, clientcolors, ENTCS_SET_NORMAL,
103 { WriteByte(chan, ent.clientcolors); },
104 { ent.colormap = ReadByte(); })
106 ENTCS_PROP(FRAGS, true, frags, ENTCS_SET_NORMAL,
107 { WriteShort(chan, ent.frags); },
108 { ent.frags = ReadShort(); })
112 int ENTCS_PUBLICMASK = 0;
113 STATIC_INIT(ENTCS_PUBLICMASK)
115 FOREACH(EntCSProps, it.m_public,
117 ENTCS_PUBLICMASK |= BIT(it.m_id);
121 bool _entcs_send(entity this, entity to, int sf, int chan)
123 entity player = this.owner;
124 sf |= BIT(0); // assume private
126 if (IS_PLAYER(player))
128 if (radar_showennemies) break;
129 if (SAME_TEAM(to, player)) break;
130 if (!(IS_PLAYER(to) || to.caplayer) && time > game_starttime) break;
132 sf &= ENTCS_PUBLICMASK; // no private updates
135 sf |= this.m_forceupdate;
136 this.m_forceupdate = 0;
137 if (chan == MSG_ENTITY)
138 WriteHeader(chan, ENT_CLIENT_ENTCS);
140 WriteHeader(chan, CLIENT_ENTCS);
141 WriteByte(chan, etof(player) - 1);
142 WriteShort(chan, sf);
143 FOREACH(EntCSProps, sf & BIT(it.m_id),
145 it.m_send(chan, this);
150 bool entcs_send(entity this, entity to, int sf)
152 return _entcs_send(this, to, sf, MSG_ENTITY);
155 void entcs_think(entity this)
157 this.nextthink = time + 0.033333333333; // TODO: increase this to like 0.15 once the client can do smoothing
158 entity o = this.owner;
159 FOREACH(EntCSProps, it.m_check(this, o),
162 this.SendFlags |= BIT(it.m_id);
164 setorigin(this, this.origin); // relink
167 void entcs_attach(entity player)
169 entity e = CS(player).entcs = new(entcs_sender);
171 setthink(e, entcs_think);
173 Net_LinkEntity(e, false, 0, entcs_send);
174 if (!IS_REAL_CLIENT(player)) return;
175 FOREACH_CLIENT(true, {
176 assert(CS(it).entcs);
177 _entcs_send(CS(it).entcs, msg_entity = player, BITS(23), MSG_ONE);
181 void entcs_detach(entity player)
183 if (!CS(player).entcs) return;
184 delete(CS(player).entcs);
185 CS(player).entcs = NULL;
192 void Ent_RemoveEntCS(entity this)
194 int n = this.sv_entnum;
195 entity e = entcs_receiver(n);
196 entcs_receiver(n, NULL);
199 if (e != this) delete(e);
202 void entcs_think(entity this)
204 entity e = CSQCModel_server2csqc(this.sv_entnum);
207 this.has_origin = this.has_sv_origin;
210 this.has_origin = true;
211 this.origin = e.origin;
212 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
213 if (this.model != e.model)
215 strcpy(this.model, e.model);
219 bool ReadEntcs(entity this)
222 entity e = entcs_receiver(n);
227 e = new_pure(entcs_receiver);
231 setthink(e, entcs_think);
232 entcs_receiver(n, e);
234 else if (e != this && this)
239 setthink(e, entcs_think);
240 entcs_receiver(n, e);
243 InterpolateOrigin_Undo(e);
245 int sf = ReadShort();
246 e.has_sv_origin = false;
247 e.m_entcs_private = boolean(sf & BIT(0));
248 FOREACH(EntCSProps, sf & BIT(it.m_id),
252 e.iflags |= IFLAG_ORIGIN;
253 InterpolateOrigin_Note(e);
258 NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
263 this.classname = "entcs_receiver";
264 this.entremove = Ent_RemoveEntCS;
266 return ReadEntcs(this);
269 NET_HANDLE(CLIENT_ENTCS, bool isnew)
271 return ReadEntcs(NULL);