]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/ent_cs.qc
entcs: save some bandwidth by scaling angles.y even more
[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 / 32)
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_set(this, player);
164                         it.m_send(chan, this);
165                 });
166                 return true;
167         }
168
169         bool entcs_send(entity this, entity to, int sf)
170         {
171                 return _entcs_send(this, to, sf, MSG_ENTITY);
172         }
173
174         void entcs_think(entity this)
175         {
176                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
177                 entity o = this.owner;
178                 FOREACH(EntCSProps, it.m_check(this, o),
179                 {
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         }
193
194         void entcs_detach(entity player)
195         {
196                 if (!CS(player).entcs) return;
197                 delete(CS(player).entcs);
198                 CS(player).entcs = NULL;
199         }
200
201 #endif
202
203 #ifdef CSQC
204
205         void Ent_RemoveEntCS(entity this)
206         {
207                 int n = this.sv_entnum;
208                 entity e = entcs_receiver(n);
209                 entcs_receiver(n, NULL);
210                 strfree(e.netname);
211                 strfree(e.model);
212                 if (e != this) delete(e);
213         }
214
215         void entcs_think(entity this)
216         {
217                 entity e = CSQCModel_server2csqc(this.sv_entnum);
218                 if (e == NULL)
219                 {
220                         this.has_origin = this.has_sv_origin;
221                         return;
222                 }
223                 this.has_origin = true;
224                 this.origin = e.origin;
225                 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
226                 if (this.model != e.model)
227                 {
228                         strcpy(this.model, e.model);
229                 }
230         }
231
232         bool ReadEntcs(entity this)
233         {
234                 int n = ReadByte();
235                 entity e = entcs_receiver(n);
236                 if (e == NULL)
237                 {
238                         if (!this)
239                                 // initial = temp
240                                 e = new_pure(entcs_receiver);
241                         else
242                                 // initial = linked
243                                 e = this;
244                         setthink(e, entcs_think);
245                         entcs_receiver(n, e);
246                 }
247                 else if (e != this && this)
248                 {
249                         // upgrade to linked
250                         delete(e);
251                         e = this;
252                         setthink(e, entcs_think);
253                         entcs_receiver(n, e);
254                 }
255
256                 InterpolateOrigin_Undo(e);
257                 e.sv_entnum = n;
258                 int sf = ReadShort();
259                 e.has_sv_origin = false;
260                 e.m_entcs_private = boolean(sf & BIT(0));
261                 FOREACH(EntCSProps, sf & BIT(it.m_id),
262                 {
263                         it.m_receive(e);
264                 });
265                 e.iflags |= IFLAG_ORIGIN;
266                 InterpolateOrigin_Note(e);
267                 getthink(e)(e);
268                 return true;
269         }
270
271         NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
272         {
273                 if (isnew)
274                 {
275                         make_pure(this);
276                         this.classname = "entcs_receiver";
277                         this.entremove = Ent_RemoveEntCS;
278                 }
279                 return ReadEntcs(this);
280         }
281
282         NET_HANDLE(CLIENT_ENTCS, bool isnew)
283         {
284                 return ReadEntcs(NULL);
285         }
286
287 #endif