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) { return (GetResourceAmount(ent, checkprop) != GetResourceAmount(player, checkprop)); } \
44 void id##_set(entity ent, entity player) { SetResourceAmountExplicit(ent, checkprop, GetResourceAmount(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_RESOURCE(id, ispublic, checkprop, setprop, svsend, clreceive) \
54 void id##_receive(entity ent) { LAMBDA(clreceive); } \
55 REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
56 this.m_public = ispublic; \
57 this.m_receive = id##_receive; \
61 #define ENTCS_SET_NORMAL(var, x) MACRO_BEGIN \
65 /** the engine player name strings are mutable! */
66 #define ENTCS_SET_MUTABLE_STRING(var, x) MACRO_BEGIN \
70 ENTCS_PROP(ENTNUM, false, sv_entnum, ENTCS_SET_NORMAL, {}, {}) /* sentinel */
72 ENTCS_PROP(ORIGIN, false, origin, ENTCS_SET_NORMAL,
73 { WriteVector(chan, ent.origin); },
74 { ent.has_sv_origin = true; vector v = ReadVector(); setorigin(ent, v); })
76 ENTCS_PROP(ANGLES, false, angles_y, ENTCS_SET_NORMAL,
77 { WriteByte(chan, ent.angles.y / 360 * 256); },
78 { vector v = '0 0 0'; v.y = ReadByte() / 256 * 360; ent.angles = v; })
80 ENTCS_PROP_RESOURCE(HEALTH, false, RESOURCE_HEALTH, ENTCS_SET_NORMAL,
81 { WriteByte(chan, bound(0, GetResourceAmount(ent, RESOURCE_HEALTH) / 10, 255)); /* FIXME: use a better scale? */ },
82 { ent.healthvalue = ReadByte() * 10; })
84 ENTCS_PROP_RESOURCE(ARMOR, false, RESOURCE_ARMOR, ENTCS_SET_NORMAL,
85 { WriteByte(chan, bound(0, GetResourceAmount(ent, RESOURCE_ARMOR) / 10, 255)); /* FIXME: use a better scale? */ },
86 { SetResourceAmountExplicit(ent, RESOURCE_ARMOR, ReadByte() * 10); })
88 ENTCS_PROP(NAME, true, netname, ENTCS_SET_MUTABLE_STRING,
89 { WriteString(chan, ent.netname); },
90 { strcpy(ent.netname, ReadString()); })
92 ENTCS_PROP(MODEL, true, model, ENTCS_SET_NORMAL,
93 { WriteString(chan, ent.model); },
94 { strcpy(ent.model, ReadString()); })
96 ENTCS_PROP(SKIN, true, skin, ENTCS_SET_NORMAL,
97 { WriteByte(chan, ent.skin); },
98 { ent.skin = ReadByte(); })
100 ENTCS_PROP(CLIENTCOLORS, true, clientcolors, ENTCS_SET_NORMAL,
101 { WriteByte(chan, ent.clientcolors); },
102 { ent.colormap = ReadByte(); })
104 ENTCS_PROP(FRAGS, true, frags, ENTCS_SET_NORMAL,
105 { WriteShort(chan, ent.frags); },
106 { ent.frags = ReadShort(); })
110 int ENTCS_PUBLICMASK = 0;
111 STATIC_INIT(ENTCS_PUBLICMASK)
113 FOREACH(EntCSProps, it.m_public,
115 ENTCS_PUBLICMASK |= BIT(it.m_id);
119 bool _entcs_send(entity this, entity to, int sf, int chan)
121 entity player = this.owner;
122 sf |= BIT(0); // assume private
124 if (IS_PLAYER(player))
126 if (radar_showennemies) break;
127 if (SAME_TEAM(to, player)) break;
128 if (!(IS_PLAYER(to) || to.caplayer) && time > game_starttime) break;
130 sf &= ENTCS_PUBLICMASK; // no private updates
133 sf |= this.m_forceupdate;
134 this.m_forceupdate = 0;
135 if (chan == MSG_ENTITY)
136 WriteHeader(chan, ENT_CLIENT_ENTCS);
138 WriteHeader(chan, CLIENT_ENTCS);
139 WriteByte(chan, etof(player) - 1);
140 WriteShort(chan, sf);
141 FOREACH(EntCSProps, sf & BIT(it.m_id),
143 it.m_send(chan, this);
148 bool entcs_send(entity this, entity to, int sf)
150 return _entcs_send(this, to, sf, MSG_ENTITY);
153 void entcs_think(entity this)
155 this.nextthink = time + 0.033333333333; // TODO: increase this to like 0.15 once the client can do smoothing
156 entity o = this.owner;
157 FOREACH(EntCSProps, it.m_check(this, o),
160 this.SendFlags |= BIT(it.m_id);
162 setorigin(this, this.origin); // relink
165 void entcs_attach(entity player)
167 entity e = CS(player).entcs = new(entcs_sender);
169 setthink(e, entcs_think);
171 Net_LinkEntity(e, false, 0, entcs_send);
172 if (!IS_REAL_CLIENT(player)) return;
173 FOREACH_CLIENT(true, {
174 assert(CS(it).entcs);
175 _entcs_send(CS(it).entcs, msg_entity = player, BITS(23), MSG_ONE);
179 void entcs_detach(entity player)
181 if (!CS(player).entcs) return;
182 delete(CS(player).entcs);
183 CS(player).entcs = NULL;
190 void Ent_RemoveEntCS(entity this)
192 int n = this.sv_entnum;
193 entity e = entcs_receiver(n);
194 entcs_receiver(n, NULL);
197 if (e != this) delete(e);
200 void entcs_think(entity this)
202 entity e = CSQCModel_server2csqc(this.sv_entnum);
205 this.has_origin = this.has_sv_origin;
208 this.has_origin = true;
209 this.origin = e.origin;
210 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
211 if (this.model != e.model)
213 strcpy(this.model, e.model);
217 bool ReadEntcs(entity this)
220 entity e = entcs_receiver(n);
225 e = new_pure(entcs_receiver);
229 setthink(e, entcs_think);
230 entcs_receiver(n, e);
232 else if (e != this && this)
237 setthink(e, entcs_think);
238 entcs_receiver(n, e);
241 InterpolateOrigin_Undo(e);
243 int sf = ReadShort();
244 e.has_sv_origin = false;
245 e.m_entcs_private = boolean(sf & BIT(0));
246 FOREACH(EntCSProps, sf & BIT(it.m_id),
250 e.iflags |= IFLAG_ORIGIN;
251 InterpolateOrigin_Note(e);
256 NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
261 this.classname = "entcs_receiver";
262 this.entremove = Ent_RemoveEntCS;
264 return ReadEntcs(this);
267 NET_HANDLE(CLIENT_ENTCS, bool isnew)
269 return ReadEntcs(NULL);