]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/ent_cs.qc
Avoid changing solidity state of entcs_sender entities too; it fixes #2225
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / ent_cs.qc
1 #include "ent_cs.qh"
2 #include <common/gamemodes/_mod.qh>
3 #include <common/resources.qh>
4 #ifdef SVQC
5 #include <server/resources.qh>
6 #endif
7
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); }
14
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)
19 {
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;
25         });
26 }
27
28 #ifdef SVQC
29 // Force an origin update, for player sounds
30 void entcs_force_origin(entity player)
31 {
32         CS(player).entcs.m_forceupdate = BIT(ENTCS_PROP_ORIGIN_id);
33 }
34 #endif
35
36 .bool m_public;
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;
41
42 #ifdef SVQC
43 #define _ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive) \
44         void id##_set(entity ent, entity player) { setprop(ent.(checkprop), player.(checkprop_pl)); } \
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; \
51         }
52
53 #define ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive) \
54         bool id##_check(entity ent, entity player) { return (ent.(checkprop) != player.(checkprop_pl)); } \
55         _ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive)
56
57 #define ENTCS_PROP_CODED(id, ispublic, checkprop, checkprop_pl, setprop, decfactor, svsend, clreceive) \
58         bool id##_check(entity ent, entity player) { \
59                 return (floor(ent.(checkprop)) / decfactor != floor(player.(checkprop_pl)) / decfactor); \
60         } \
61         _ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive)
62
63 #elif defined(CSQC)
64 #define ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, 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; \
69         }
70
71 #define ENTCS_PROP_CODED(id, ispublic, checkprop, checkprop_pl, setprop, decfactor, svsend, clreceive) \
72         ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive)
73 #endif
74
75 #ifdef SVQC
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)); \
79         } \
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; \
87         }
88 #elif defined(CSQC)
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; \
94         }
95 #endif
96
97 #define ENTCS_SET_NORMAL(var, x) MACRO_BEGIN \
98         var = x; \
99 MACRO_END
100
101 /** the engine player name strings are mutable! */
102 #define ENTCS_SET_MUTABLE_STRING(var, x) MACRO_BEGIN \
103         strcpy(var, x); \
104 MACRO_END
105
106 ENTCS_PROP(ENTNUM, false, sv_entnum, sv_entnum, ENTCS_SET_NORMAL, {}, {}) /* sentinel */
107
108 ENTCS_PROP(ORIGIN, false, origin, origin, ENTCS_SET_NORMAL,
109         { WriteVector(chan, ent.origin); },
110         { ent.has_sv_origin = true; vector v = ReadVector(); setorigin(ent, v); })
111
112 #define DEC_FACTOR (360 / 32)
113 ENTCS_PROP_CODED(ANGLES, false, angles_y, 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; })
116 #undef DEC_FACTOR
117
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; })
123
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); })
127 #undef DEC_FACTOR
128
129 ENTCS_PROP(NAME, true, netname, netname, ENTCS_SET_MUTABLE_STRING,
130         { WriteString(chan, ent.netname); },
131         { strcpy(ent.netname, ReadString()); })
132
133 ENTCS_PROP(MODEL, true, model, model, ENTCS_SET_NORMAL,
134         { WriteString(chan, ent.model); },
135         { strcpy(ent.model, ReadString()); })
136
137 ENTCS_PROP(SKIN, true, skin, skin, ENTCS_SET_NORMAL,
138         { WriteByte(chan, ent.skin); },
139         { ent.skin = ReadByte(); })
140
141 ENTCS_PROP(CLIENTCOLORS, true, clientcolors, clientcolors, ENTCS_SET_NORMAL,
142         { WriteByte(chan, ent.clientcolors); },
143         { ent.colormap = ReadByte(); })
144
145 ENTCS_PROP(FRAGS, true, frags, frags, ENTCS_SET_NORMAL,
146         { WriteShort(chan, ent.frags); },
147         { ent.frags = ReadShort(); })
148
149 // use sv_solid to avoid changing solidity state of entcs entities
150 ENTCS_PROP(SOLID, true, sv_solid, solid, ENTCS_SET_NORMAL,
151         { WriteByte(chan, ent.sv_solid); },
152         { ent.sv_solid = ReadByte(); })
153
154 #ifdef SVQC
155
156         int ENTCS_PUBLICMASK = 0;
157         STATIC_INIT(ENTCS_PUBLICMASK)
158         {
159                 FOREACH(EntCSProps, it.m_public,
160                 {
161                         ENTCS_PUBLICMASK |= BIT(it.m_id);
162                 });
163         }
164
165         bool _entcs_send(entity this, entity to, int sf, int chan)
166         {
167                 entity player = this.owner;
168                 sf |= BIT(ENTCS_PROP_ENTNUM_id); // assume private
169                 do {
170                         if (IS_PLAYER(player))
171                         {
172                                 if (radar_showennemies) break;
173                                 if (SAME_TEAM(to, player)) break;
174                                 if (!(IS_PLAYER(to) || to.caplayer) && time > game_starttime) break;
175                         }
176                         sf &= ENTCS_PUBLICMASK; // no private updates
177                 } while (0);
178
179                 sf |= this.m_forceupdate;
180                 this.m_forceupdate = 0;
181                 if (chan == MSG_ENTITY)
182                         WriteHeader(chan, ENT_CLIENT_ENTCS);
183                 else
184                         WriteHeader(chan, CLIENT_ENTCS);
185                 WriteByte(chan, etof(player) - 1);
186                 WriteShort(chan, sf);
187                 FOREACH(EntCSProps, sf & BIT(it.m_id),
188                 {
189                         it.m_send(chan, this);
190                 });
191                 return true;
192         }
193
194         bool entcs_send(entity this, entity to, int sf)
195         {
196                 return _entcs_send(this, to, sf, MSG_ENTITY);
197         }
198
199         void entcs_think(entity this)
200         {
201                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
202                 entity player = this.owner;
203                 FOREACH(EntCSProps, it.m_check(this, player),
204                 {
205                         it.m_set(this, player);
206                         this.SendFlags |= BIT(it.m_id);
207                 });
208                 setorigin(this, this.origin); // relink
209         }
210
211         void entcs_attach(entity player)
212         {
213                 entity e = CS(player).entcs = new(entcs_sender);
214                 e.owner = player;
215                 setthink(e, entcs_think);
216                 e.nextthink = time;
217                 Net_LinkEntity(e, false, 0, entcs_send);
218                 if (!IS_REAL_CLIENT(player)) return;
219                 FOREACH_CLIENT(true, {
220                         assert(CS(it).entcs);
221                         _entcs_send(CS(it).entcs, msg_entity = player, BITS(23), MSG_ONE);
222                 });
223         }
224
225         void entcs_detach(entity player)
226         {
227                 if (!CS(player).entcs) return;
228                 delete(CS(player).entcs);
229                 CS(player).entcs = NULL;
230         }
231
232 #endif
233
234 #ifdef CSQC
235
236         void Ent_RemoveEntCS(entity this)
237         {
238                 int n = this.sv_entnum;
239                 entity e = entcs_receiver(n);
240                 entcs_receiver(n, NULL);
241                 strfree(e.netname);
242                 strfree(e.model);
243                 if (e != this) delete(e);
244         }
245
246         void entcs_think(entity this)
247         {
248                 entity e = CSQCModel_server2csqc(this.sv_entnum);
249                 if (e == NULL)
250                 {
251                         this.has_origin = this.has_sv_origin;
252                         return;
253                 }
254                 this.has_origin = true;
255                 this.origin = e.origin;
256                 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
257                 if (this.model != e.model)
258                 {
259                         strcpy(this.model, e.model);
260                 }
261         }
262
263         bool ReadEntcs(entity this)
264         {
265                 int n = ReadByte();
266                 entity e = entcs_receiver(n);
267                 if (e == NULL)
268                 {
269                         if (!this)
270                                 // initial = temp
271                                 e = new_pure(entcs_receiver);
272                         else
273                                 // initial = linked
274                                 e = this;
275                         setthink(e, entcs_think);
276                         entcs_receiver(n, e);
277                 }
278                 else if (e != this && this)
279                 {
280                         // upgrade to linked
281                         delete(e);
282                         e = this;
283                         setthink(e, entcs_think);
284                         entcs_receiver(n, e);
285                 }
286
287                 InterpolateOrigin_Undo(e);
288                 e.sv_entnum = n;
289                 int sf = ReadShort();
290                 e.has_sv_origin = false;
291                 e.m_entcs_private = boolean(sf & BIT(ENTCS_PROP_ENTNUM_id));
292                 FOREACH(EntCSProps, sf & BIT(it.m_id),
293                 {
294                         it.m_receive(e);
295                 });
296                 e.iflags |= IFLAG_ORIGIN;
297                 InterpolateOrigin_Note(e);
298                 getthink(e)(e);
299                 return true;
300         }
301
302         NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
303         {
304                 if (isnew)
305                 {
306                         make_pure(this);
307                         this.classname = "entcs_receiver";
308                         this.entremove = Ent_RemoveEntCS;
309                 }
310                 return ReadEntcs(this);
311         }
312
313         NET_HANDLE(CLIENT_ENTCS, bool isnew)
314         {
315                 return ReadEntcs(NULL);
316         }
317
318 #endif