We need to send location as coords, can't 'short'cut this (big maps)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / ent_cs.qc
1 #include "ent_cs.qh"
2
3 #define ENTCS_SET_NORMAL(var, x) MACRO_BEGIN \
4         var = x; \
5 MACRO_END
6
7 /** the engine player name strings are mutable! */
8 #define ENTCS_SET_MUTABLE_STRING(var, x) MACRO_BEGIN \
9         if (var) strunzone(var); \
10         var = strzone(x); \
11 MACRO_END
12
13 // #define PROP(public, fld, set, sv, cl)
14 #define ENTCS_NETPROPS(ent, PROP) PROP(false, sv_entnum, ENTCS_SET_NORMAL, {}, {}) /* sentinel */ \
15         PROP(false, origin, ENTCS_SET_NORMAL, \
16         { WriteCoord(chan, ent.origin.x);  WriteCoord(chan, ent.origin.y); \
17           WriteCoord(chan, ent.origin.z); }, \
18         { ent.has_sv_origin = true; vector v; v.x = ReadCoord(); v.y = ReadCoord(); v.z = ReadCoord(); setorigin(ent, v); }) \
19     \
20         PROP(false, angles_y, ENTCS_SET_NORMAL, \
21         { WriteByte(chan, ent.angles.y / 360 * 256); }, \
22         { vector v = '0 0 0'; v.y = ReadByte() / 256 * 360; ent.angles = v; }) \
23     \
24         PROP(false, health, ENTCS_SET_NORMAL, \
25         { WriteByte(chan, bound(0, ent.health / 10, 255));  /* FIXME: use a better scale? */ }, \
26         { ent.healthvalue = ReadByte() * 10; }) \
27     \
28         PROP(false, armorvalue, ENTCS_SET_NORMAL, \
29         { WriteByte(chan, bound(0, ent.armorvalue / 10, 255));  /* FIXME: use a better scale? */ }, \
30         { ent.armorvalue = ReadByte() * 10; }) \
31     \
32         PROP(true, netname, ENTCS_SET_MUTABLE_STRING, \
33         { WriteString(chan, ent.netname); }, \
34         { if (ent.netname) strunzone(ent.netname); ent.netname = strzone(ReadString()); }) \
35     \
36         PROP(true, model, ENTCS_SET_NORMAL, \
37         { WriteString(chan, ent.model); }, \
38         { if (ent.model) strunzone(ent.model); ent.model = strzone(ReadString()); }) \
39     \
40         PROP(true, skin, ENTCS_SET_NORMAL, \
41         { WriteByte(chan, ent.skin); }, \
42         { ent.skin = ReadByte(); }) \
43     \
44     PROP(true, clientcolors, ENTCS_SET_NORMAL, \
45         { WriteByte(chan, ent.clientcolors); }, \
46         { ent.colormap = ReadByte(); }) \
47     \
48     PROP(true, frags, ENTCS_SET_NORMAL, \
49         { WriteShort(chan, ent.frags); }, \
50         { ent.frags = ReadShort(); }) \
51     \
52         /**/
53
54 #ifdef SVQC
55
56         int ENTCS_PUBLICMASK = 0;
57         STATIC_INIT(ENTCS_PUBLICMASK)
58         {
59                 int i = 0;
60                 #define X(public, fld, set, sv, cl) { \
61                         if (public) { \
62                                 ENTCS_PUBLICMASK |= BIT(i); \
63                         } \
64                         i += 1; \
65                 }
66                 ENTCS_NETPROPS(this, X);
67         #undef X
68                 if (i >= BITS(16 - 1)) LOG_FATAL("Exceeded ENTCS_NETPROPS limit");
69         }
70
71         bool _entcs_send(entity this, entity to, int sf, int chan)
72         {
73                 entity player = this.owner;
74                 sf |= BIT(0); // assume private
75                 do {
76                         if (!(IS_PLAYER(player)))
77                         {
78                                 sf &= ENTCS_PUBLICMASK; // no private updates
79                                 break;
80                         }
81                         if (radar_showennemies) break;
82                         if (SAME_TEAM(to, player)) break;
83                         if (!(IS_PLAYER(to) || to.caplayer) && time > game_starttime) break;
84                         sf &= ENTCS_PUBLICMASK; // no private updates
85                 } while (0);
86                 sf |= this.m_forceupdate;
87                 this.m_forceupdate = 0;
88                 if (chan == MSG_ENTITY)
89                         WriteHeader(chan, ENT_CLIENT_ENTCS);
90                 else
91                         WriteHeader(chan, CLIENT_ENTCS);
92                 WriteByte(chan, etof(player) - 1);
93                 WriteShort(chan, sf);
94                 int i = 0;
95                 #define X(public, fld, set, sv, cl) { \
96                         if (sf & BIT(i)) { \
97                                 sv; \
98                         } \
99                         i += 1; \
100                 }
101                 ENTCS_NETPROPS(this, X);
102         #undef X
103                 return true;
104         }
105
106         bool entcs_send(entity this, entity to, int sf)
107         {
108                 return _entcs_send(this, to, sf, MSG_ENTITY);
109         }
110
111         void entcs_think(entity this)
112         {
113                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
114                 entity o = this.owner;
115                 int i = 0;
116                 #define X(public, fld, set, sv, cl) { \
117                         if (o.fld != this.fld) { \
118                                 set(this.fld, o.fld); \
119                                 this.SendFlags |= BIT(i); \
120                         } \
121                         i += 1; \
122                 }
123                 ENTCS_NETPROPS(this, X);
124         #undef X
125             setorigin(this, this.origin);  // relink
126         }
127
128         void entcs_attach(entity player)
129         {
130                 entity e = player.entcs = new(entcs_sender);
131                 e.owner = player;
132                 setthink(e, entcs_think);
133                 e.nextthink = time;
134                 Net_LinkEntity(e, false, 0, entcs_send);
135                 if (!IS_REAL_CLIENT(player)) return;
136                 FOREACH_CLIENT(true, {
137                         assert(it.entcs);
138                         _entcs_send(it.entcs, msg_entity = player, BITS(23), MSG_ONE);
139                 });
140         }
141
142         void entcs_detach(entity player)
143         {
144                 if (!player.entcs) return;
145                 delete(player.entcs);
146                 player.entcs = NULL;
147         }
148
149 #endif
150
151 #ifdef CSQC
152
153         void Ent_RemoveEntCS(entity this)
154         {
155                 int n = this.sv_entnum;
156                 entity e = entcs_receiver(n);
157                 entcs_receiver(n, NULL);
158                 if (e != this) delete(e);
159         }
160
161         void entcs_think(entity this)
162         {
163                 entity e = CSQCModel_server2csqc(this.sv_entnum);
164                 if (e == NULL)
165                 {
166                         this.has_origin = this.has_sv_origin;
167                         return;
168                 }
169                 this.has_origin = true;
170                 this.origin = e.origin;
171                 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
172                 if (this.model != e.model)
173                 {
174                         if (this.model) strunzone(this.model);
175                         this.model = strzone(e.model);
176                 }
177         }
178
179         bool ReadEntcs(entity this)
180         {
181                 int n = ReadByte();
182                 entity e = entcs_receiver(n);
183                 if (e == NULL)
184                 {
185                         if (!this)
186                                 // initial = temp
187                                 e = new_pure(entcs_receiver);
188                         else
189                                 // initial = linked
190                                 e = this;
191                         setthink(e, entcs_think);
192                         entcs_receiver(n, e);
193                 }
194                 else if (e != this && this)
195                 {
196                         // upgrade to linked
197                         delete(e);
198                         e = this;
199                         setthink(e, entcs_think);
200                         entcs_receiver(n, e);
201                 }
202
203                 InterpolateOrigin_Undo(e);
204                 e.sv_entnum = n;
205                 int sf = ReadShort();
206                 e.has_sv_origin = false;
207                 e.m_entcs_private = boolean(sf & BIT(0));
208                 int i = 0;
209                 #define X(public, fld, set, sv, cl) { \
210                         if (sf & BIT(i)) { \
211                                 cl; \
212                         } \
213                         i += 1; \
214                 }
215                 ENTCS_NETPROPS(e, X);
216         #undef X
217                 e.iflags |= IFLAG_ORIGIN;
218                 InterpolateOrigin_Note(e);
219                 getthink(e)(e);
220                 return true;
221         }
222
223         NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
224         {
225                 if (isnew)
226                 {
227                         make_pure(this);
228                         this.classname = "entcs_receiver";
229                         this.entremove = Ent_RemoveEntCS;
230                 }
231                 return ReadEntcs(this);
232         }
233
234         NET_HANDLE(CLIENT_ENTCS, bool isnew)
235         {
236                 return ReadEntcs(NULL);
237         }
238
239 #endif