]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/ent_cs.qc
Merge branch 'master' into Mario/race_target_waypoint
[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                                 if (radar_showennemies) break;
79                                 if (SAME_TEAM(to, player)) break;
80                                 if (!(IS_PLAYER(to) || to.caplayer) && time > game_starttime) break;
81                         }
82                         sf &= ENTCS_PUBLICMASK; // no private updates
83                 } while (0);
84
85                 sf |= this.m_forceupdate;
86                 this.m_forceupdate = 0;
87                 if (chan == MSG_ENTITY)
88                         WriteHeader(chan, ENT_CLIENT_ENTCS);
89                 else
90                         WriteHeader(chan, CLIENT_ENTCS);
91                 WriteByte(chan, etof(player) - 1);
92                 WriteShort(chan, sf);
93                 int i = 0;
94                 #define X(public, fld, set, sv, cl) { \
95                         if (sf & BIT(i)) { \
96                                 sv; \
97                         } \
98                         i += 1; \
99                 }
100                 ENTCS_NETPROPS(this, X);
101                 #undef X
102                 return true;
103         }
104
105         bool entcs_send(entity this, entity to, int sf)
106         {
107                 return _entcs_send(this, to, sf, MSG_ENTITY);
108         }
109
110         void entcs_think(entity this)
111         {
112                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
113                 entity o = this.owner;
114                 int i = 0;
115                 #define X(public, fld, set, sv, cl) { \
116                         if (o.fld != this.fld) { \
117                                 set(this.fld, o.fld); \
118                                 this.SendFlags |= BIT(i); \
119                         } \
120                         i += 1; \
121                 }
122                 ENTCS_NETPROPS(this, X);
123                 #undef X
124             setorigin(this, this.origin);  // relink
125         }
126
127         void entcs_attach(entity player)
128         {
129                 entity e = player.entcs = new(entcs_sender);
130                 e.owner = player;
131                 setthink(e, entcs_think);
132                 e.nextthink = time;
133                 Net_LinkEntity(e, false, 0, entcs_send);
134                 if (!IS_REAL_CLIENT(player)) return;
135                 FOREACH_CLIENT(true, {
136                         assert(it.entcs);
137                         _entcs_send(it.entcs, msg_entity = player, BITS(23), MSG_ONE);
138                 });
139         }
140
141         void entcs_detach(entity player)
142         {
143                 if (!player.entcs) return;
144                 delete(player.entcs);
145                 player.entcs = NULL;
146         }
147
148 #endif
149
150 #ifdef CSQC
151
152         void Ent_RemoveEntCS(entity this)
153         {
154                 int n = this.sv_entnum;
155                 entity e = entcs_receiver(n);
156                 entcs_receiver(n, NULL);
157                 if (e.netname) strunzone(e.netname);
158                 e.netname = string_null;
159                 if (e.model) strunzone(e.model);
160                 e.model = string_null;
161                 if (e != this) delete(e);
162         }
163
164         void entcs_think(entity this)
165         {
166                 entity e = CSQCModel_server2csqc(this.sv_entnum);
167                 if (e == NULL)
168                 {
169                         this.has_origin = this.has_sv_origin;
170                         return;
171                 }
172                 this.has_origin = true;
173                 this.origin = e.origin;
174                 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
175                 if (this.model != e.model)
176                 {
177                         if (this.model) strunzone(this.model);
178                         this.model = strzone(e.model);
179                 }
180         }
181
182         bool ReadEntcs(entity this)
183         {
184                 int n = ReadByte();
185                 entity e = entcs_receiver(n);
186                 if (e == NULL)
187                 {
188                         if (!this)
189                                 // initial = temp
190                                 e = new_pure(entcs_receiver);
191                         else
192                                 // initial = linked
193                                 e = this;
194                         setthink(e, entcs_think);
195                         entcs_receiver(n, e);
196                 }
197                 else if (e != this && this)
198                 {
199                         // upgrade to linked
200                         delete(e);
201                         e = this;
202                         setthink(e, entcs_think);
203                         entcs_receiver(n, e);
204                 }
205
206                 InterpolateOrigin_Undo(e);
207                 e.sv_entnum = n;
208                 int sf = ReadShort();
209                 e.has_sv_origin = false;
210                 e.m_entcs_private = boolean(sf & BIT(0));
211                 int i = 0;
212                 #define X(public, fld, set, sv, cl) { \
213                         if (sf & BIT(i)) { \
214                                 cl; \
215                         } \
216                         i += 1; \
217                 }
218                 ENTCS_NETPROPS(e, X);
219                 #undef X
220                 e.iflags |= IFLAG_ORIGIN;
221                 InterpolateOrigin_Note(e);
222                 getthink(e)(e);
223                 return true;
224         }
225
226         NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
227         {
228                 if (isnew)
229                 {
230                         make_pure(this);
231                         this.classname = "entcs_receiver";
232                         this.entremove = Ent_RemoveEntCS;
233                 }
234                 return ReadEntcs(this);
235         }
236
237         NET_HANDLE(CLIENT_ENTCS, bool isnew)
238         {
239                 return ReadEntcs(NULL);
240         }
241
242 #endif