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