]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/ent_cs.qc
Merge remote-tracking branch 'origin/master' into pending-release
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / ent_cs.qc
1 #include "ent_cs.qh"
2
3 #if defined(CSQC)
4         #include <common/gamemodes/_mod.qh>
5         #include <common/resources/resources.qh>
6 #elif defined(MENUQC)
7 #elif defined(SVQC)
8         #include <common/gamemodes/_mod.qh>
9         #include <common/resources/resources.qh>
10         #include <common/resources/sv_resources.qh>
11 #endif
12
13 REGISTRY(EntCSProps, BITS(16) - 1)
14 REGISTER_REGISTRY(EntCSProps)
15 REGISTRY_SORT(EntCSProps)
16 REGISTRY_CHECK(EntCSProps)
17
18 REGISTRY_DEFINE_GET(EntCSProps, NULL)
19 STATIC_INIT(EntCSProps_renumber) { FOREACH(EntCSProps, true, it.m_id = i); }
20
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)
26 {
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;
31         });
32 }
33
34 #ifdef SVQC
35 // Force an origin update, for player sounds
36 void entcs_force_origin(entity player)
37 {
38         CS(player).entcs.m_forceupdate = BIT(ENTCS_PROP_ORIGIN_id);
39 }
40 #endif
41
42 .bool m_public;
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;
47
48 #ifdef SVQC
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; \
57         }
58
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)
62
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); \
66         } \
67         _ENTCS_PROP(id, ispublic, checkprop, checkprop_pl, setprop, svsend, clreceive)
68
69 #elif defined(CSQC)
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; \
75         }
76
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)
79 #endif
80
81 #ifdef SVQC
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)); \
85         } \
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; \
93         }
94 #elif defined(CSQC)
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; \
100         }
101 #endif
102
103 #define ENTCS_SET_NORMAL(var, x) MACRO_BEGIN \
104         var = x; \
105 MACRO_END
106
107 /** the engine player name strings are mutable! */
108 #define ENTCS_SET_MUTABLE_STRING(var, x) MACRO_BEGIN \
109         strcpy(var, x); \
110 MACRO_END
111
112 ENTCS_PROP(ENTNUM, false, sv_entnum, sv_entnum, ENTCS_SET_NORMAL, {}, {}) /* sentinel */
113
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); })
117
118 #define DEC_FACTOR (360 / 64)
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; })
122 #undef DEC_FACTOR
123
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; })
129
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); })
133 #undef DEC_FACTOR
134
135 ENTCS_PROP(NAME, true, netname, netname, ENTCS_SET_MUTABLE_STRING,
136         { WriteString(chan, ent.netname); },
137         { strcpy(ent.netname, ReadString()); })
138
139 ENTCS_PROP(MODEL, true, model, model, ENTCS_SET_NORMAL,
140         { WriteString(chan, ent.model); },
141         { strcpy(ent.model, ReadString()); })
142
143 ENTCS_PROP(SKIN, true, skin, skin, ENTCS_SET_NORMAL,
144         { WriteByte(chan, ent.skin); },
145         { ent.skin = ReadByte(); })
146
147 ENTCS_PROP(CLIENTCOLORS, true, clientcolors, clientcolors, ENTCS_SET_NORMAL,
148         { WriteByte(chan, ent.clientcolors); },
149         { ent.colormap = ReadByte(); })
150
151 ENTCS_PROP(FRAGS, true, frags, frags, ENTCS_SET_NORMAL,
152         { WriteShort(chan, ent.frags); },
153         { ent.frags = ReadShort(); })
154
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(); })
159
160 // gamemode specific player survival status (independent of score and frags)
161 ENTCS_PROP(SURVIVAL_STATUS, true, survival_status, survival_status, ENTCS_SET_NORMAL,
162         { WriteShort(chan, ent.survival_status); },
163         { ent.survival_status = ReadShort(); })
164
165 #ifdef SVQC
166
167         int ENTCS_PUBLICMASK = 0, ENTCS_PRIVATEMASK = 0;
168         STATIC_INIT(ENTCS_PUBLICMASK)
169         {
170                 FOREACH(EntCSProps, true,
171                 {
172                         if (it.m_public)
173                                 ENTCS_PUBLICMASK |= BIT(it.m_id);
174                         else
175                                 ENTCS_PRIVATEMASK |= BIT(it.m_id);
176                 });
177         }
178
179         void entcs_update_players(entity player)
180         {
181                 FOREACH_CLIENT(it != player && IS_PLAYER(it),
182                 {
183                         CS(it).entcs.SendFlags |= ENTCS_PRIVATEMASK;
184                 });
185         }
186
187         bool _entcs_send(entity this, entity to, int sf, int chan)
188         {
189                 entity player = this.owner;
190                 sf |= BIT(ENTCS_PROP_ENTNUM_id); // assume private
191                 do {
192                         if (IS_PLAYER(player))
193                         {
194                                 if (radar_showenemies) break;
195                                 if (SAME_TEAM(to, player)) break;
196                                 if (!(IS_PLAYER(to) || INGAME(to))) break;
197                         }
198                         sf &= ENTCS_PUBLICMASK; // no private updates
199                 } while (0);
200
201                 sf |= this.m_forceupdate;
202                 this.m_forceupdate = 0;
203                 if (chan == MSG_ENTITY)
204                         WriteHeader(chan, ENT_CLIENT_ENTCS);
205                 else
206                         WriteHeader(chan, CLIENT_ENTCS);
207                 WriteByte(chan, etof(player) - 1);
208                 WriteShort(chan, sf);
209                 FOREACH(EntCSProps, sf & BIT(it.m_id),
210                 {
211                         it.m_send(chan, this);
212                 });
213                 return true;
214         }
215
216         bool entcs_send(entity this, entity to, int sf)
217         {
218                 return _entcs_send(this, to, sf, MSG_ENTITY);
219         }
220
221         void entcs_think(entity this)
222         {
223                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
224                 entity player = this.owner;
225                 FOREACH(EntCSProps, it.m_check(this, player),
226                 {
227                         it.m_set(this, player);
228                         this.SendFlags |= BIT(it.m_id);
229                 });
230
231                 if (intermission_running)
232                 {
233                         // health is set to special values after the game ends, ignore any change
234                         this.SendFlags &= ~BIT(ENTCS_PROP_HEALTH_id);
235                 }
236
237                 // always send origin of players even if they stand still otherwise
238                 // if a teammate isn't in my pvs and their health (or view angle or name
239                 // etc...) changes then their tag disappears
240                 if (IS_PLAYER(this.owner))
241                         this.SendFlags |= BIT(ENTCS_PROP_ORIGIN_id);
242
243                 // not needed, origin is just data to be sent
244                 //setorigin(this, this.origin); // relink
245         }
246
247         void entcs_attach(entity player)
248         {
249                 entity e = CS(player).entcs = new_pure(entcs_sender);
250                 e.owner = player;
251                 setthink(e, entcs_think);
252                 e.nextthink = time;
253                 Net_LinkEntity(e, false, 0, entcs_send);
254                 // NOTE: the following code block has been disabled as a workaround for https://gitlab.com/xonotic/xonotic-data.pk3dir/-/issues/1824
255 #if 0
256                 if (!IS_REAL_CLIENT(player)) return;
257                 FOREACH_CLIENT(true, {
258                         assert(CS(it).entcs);
259                         _entcs_send(CS(it).entcs, msg_entity = player, BITS(23), MSG_ONE);
260                 });
261 #endif
262         }
263
264         void entcs_detach(entity player)
265         {
266                 if (!CS(player).entcs) return;
267                 delete(CS(player).entcs);
268                 CS(player).entcs = NULL;
269         }
270
271 #endif
272
273 #ifdef CSQC
274
275         void Ent_RemoveEntCS(entity this)
276         {
277                 int n = this.sv_entnum;
278                 entity e = entcs_receiver(n);
279                 entcs_receiver(n, NULL);
280                 strfree(e.netname);
281                 strfree(e.model);
282                 if (e != this) delete(e);
283         }
284
285         void entcs_think(entity this)
286         {
287                 entity e = CSQCModel_server2csqc(this.sv_entnum);
288                 if (e == NULL)
289                 {
290                         // player model is NOT in client's PVS
291                         InterpolateOrigin_Do(this);
292                         this.has_origin = this.has_sv_origin;
293                         return;
294                 }
295                 this.has_origin = true;
296                 // when a player model is in client's PVS we use its origin directly
297                 // (entcs networked origin is overriden)
298                 this.origin = e.origin;
299                 InterpolateOrigin_Reset(this);
300                 setorigin(this, this.origin);
301                 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
302                 if (this.model != e.model)
303                 {
304                         strcpy(this.model, e.model);
305                 }
306         }
307
308         bool ReadEntcs(entity this)
309         {
310                 int n = ReadByte();
311                 entity e = entcs_receiver(n);
312                 if (e == NULL)
313                 {
314                         if (!this)
315                                 // initial = temp
316                                 e = new_pure(ENT_CLIENT_ENTCS);
317                         else
318                                 // initial = linked
319                                 e = this;
320                         setthink(e, entcs_think);
321                         entcs_receiver(n, e);
322                 }
323                 else if (e != this && this)
324                 {
325                         // upgrade to linked
326                         delete(e);
327                         e = this;
328                         setthink(e, entcs_think);
329                         entcs_receiver(n, e);
330                 }
331
332                 InterpolateOrigin_Undo(e);
333                 e.sv_entnum = n;
334                 int sf = ReadShort();
335                 e.has_sv_origin = false;
336                 e.m_entcs_private = boolean(sf & BIT(ENTCS_PROP_ENTNUM_id));
337                 FOREACH(EntCSProps, sf & BIT(it.m_id),
338                 {
339                         it.m_receive(e);
340                 });
341                 e.iflags |= IFLAG_ORIGIN;
342                 InterpolateOrigin_Note(e);
343                 getthink(e)(e);
344                 return true;
345         }
346
347         NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
348         {
349                 if (isnew)
350                 {
351                         make_pure(this);
352                         this.entremove = Ent_RemoveEntCS;
353                 }
354                 return ReadEntcs(this);
355         }
356
357         NET_HANDLE(CLIENT_ENTCS, bool isnew)
358         {
359                 return ReadEntcs(NULL);
360         }
361
362 #endif