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