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