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