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