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