]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/ent_cs.qc
Add strfree to reduce explicit use of strunzone/string_null
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / ent_cs.qc
1 #include "ent_cs.qh"
2
3 REGISTRY(EntCSProps, BITS(16) - 1)
4 #define EntCSProps_from(i) _EntCSProps_from(i, NULL)
5 REGISTER_REGISTRY(EntCSProps)
6 REGISTRY_SORT(EntCSProps)
7 REGISTRY_CHECK(EntCSProps)
8 STATIC_INIT(RegisterEntCSProps_renumber) { FOREACH(EntCSProps, true, it.m_id = i); }
9
10 .bool m_public;
11 .bool(entity ent, entity player) m_check;
12 .void(entity ent, entity player) m_set;
13 .void(int chan, entity ent) m_send;
14 .void(entity ent) m_receive;
15
16 #ifdef SVQC
17 #define ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive) \
18         bool id##_check(entity ent, entity player) { return (ent.(checkprop) != player.(checkprop)); } \
19         void id##_set(entity ent, entity player) { setprop(ent.(checkprop), player.(checkprop)); } \
20         void id##_send(int chan, entity ent) { LAMBDA(svsend); } \
21         REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
22                 this.m_public = ispublic; \
23                 this.m_check = id##_check; \
24                 this.m_set = id##_set; \
25                 this.m_send = id##_send; \
26         }
27 #elif defined(CSQC)
28 #define ENTCS_PROP(id, ispublic, checkprop, setprop, svsend, clreceive) \
29         void id##_receive(entity ent) { LAMBDA(clreceive); } \
30         REGISTER(EntCSProps, ENTCS_PROP, id, m_id, new_pure(entcs_prop)) { \
31                 this.m_public = ispublic; \
32                 this.m_receive = id##_receive; \
33         }
34 #endif
35
36 #define ENTCS_SET_NORMAL(var, x) MACRO_BEGIN \
37         var = x; \
38 MACRO_END
39
40 /** the engine player name strings are mutable! */
41 #define ENTCS_SET_MUTABLE_STRING(var, x) MACRO_BEGIN \
42         strcpy(var, x); \
43 MACRO_END
44
45 ENTCS_PROP(ENTNUM, false, sv_entnum, ENTCS_SET_NORMAL, {}, {}) /* sentinel */
46
47 ENTCS_PROP(ORIGIN, false, origin, ENTCS_SET_NORMAL,
48         { WriteVector(chan, ent.origin); },
49         { ent.has_sv_origin = true; vector v = ReadVector(); setorigin(ent, v); })
50
51 ENTCS_PROP(ANGLES, false, angles_y, ENTCS_SET_NORMAL,
52         { WriteByte(chan, ent.angles.y / 360 * 256); },
53         { vector v = '0 0 0'; v.y = ReadByte() / 256 * 360; ent.angles = v; })
54
55 ENTCS_PROP(HEALTH, false, health, ENTCS_SET_NORMAL,
56         { WriteByte(chan, bound(0, ent.health / 10, 255));  /* FIXME: use a better scale? */ },
57         { ent.healthvalue = ReadByte() * 10; })
58
59 ENTCS_PROP(ARMOR, false, armorvalue, ENTCS_SET_NORMAL,
60         { WriteByte(chan, bound(0, ent.armorvalue / 10, 255));  /* FIXME: use a better scale? */ },
61         { ent.armorvalue = ReadByte() * 10; })
62
63 ENTCS_PROP(NAME, true, netname, ENTCS_SET_MUTABLE_STRING,
64         { WriteString(chan, ent.netname); },
65         { strcpy(ent.netname, ReadString()); })
66
67 ENTCS_PROP(MODEL, true, model, ENTCS_SET_NORMAL,
68         { WriteString(chan, ent.model); },
69         { strcpy(ent.model, ReadString()); })
70
71 ENTCS_PROP(SKIN, true, skin, ENTCS_SET_NORMAL,
72         { WriteByte(chan, ent.skin); },
73         { ent.skin = ReadByte(); })
74
75 ENTCS_PROP(CLIENTCOLORS, true, clientcolors, ENTCS_SET_NORMAL,
76         { WriteByte(chan, ent.clientcolors); },
77         { ent.colormap = ReadByte(); })
78
79 ENTCS_PROP(FRAGS, true, frags, ENTCS_SET_NORMAL,
80         { WriteShort(chan, ent.frags); },
81         { ent.frags = ReadShort(); })
82
83 #ifdef SVQC
84
85         int ENTCS_PUBLICMASK = 0;
86         STATIC_INIT(ENTCS_PUBLICMASK)
87         {
88                 FOREACH(EntCSProps, it.m_public,
89                 {
90                         ENTCS_PUBLICMASK |= BIT(it.m_id);
91                 });
92         }
93
94         bool _entcs_send(entity this, entity to, int sf, int chan)
95         {
96                 entity player = this.owner;
97                 sf |= BIT(0); // assume private
98                 do {
99                         if (IS_PLAYER(player))
100                         {
101                                 if (radar_showennemies) break;
102                                 if (SAME_TEAM(to, player)) break;
103                                 if (!(IS_PLAYER(to) || to.caplayer) && time > game_starttime) break;
104                         }
105                         sf &= ENTCS_PUBLICMASK; // no private updates
106                 } while (0);
107
108                 sf |= this.m_forceupdate;
109                 this.m_forceupdate = 0;
110                 if (chan == MSG_ENTITY)
111                         WriteHeader(chan, ENT_CLIENT_ENTCS);
112                 else
113                         WriteHeader(chan, CLIENT_ENTCS);
114                 WriteByte(chan, etof(player) - 1);
115                 WriteShort(chan, sf);
116                 FOREACH(EntCSProps, sf & BIT(it.m_id),
117                 {
118                         it.m_send(chan, this);
119                 });
120                 return true;
121         }
122
123         bool entcs_send(entity this, entity to, int sf)
124         {
125                 return _entcs_send(this, to, sf, MSG_ENTITY);
126         }
127
128         void entcs_think(entity this)
129         {
130                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
131                 entity o = this.owner;
132                 FOREACH(EntCSProps, it.m_check(this, o),
133                 {
134                         it.m_set(this, o);
135                         this.SendFlags |= BIT(it.m_id);
136                 });
137             setorigin(this, this.origin);  // relink
138         }
139
140         void entcs_attach(entity player)
141         {
142                 entity e = player.entcs = new(entcs_sender);
143                 e.owner = player;
144                 setthink(e, entcs_think);
145                 e.nextthink = time;
146                 Net_LinkEntity(e, false, 0, entcs_send);
147                 if (!IS_REAL_CLIENT(player)) return;
148                 FOREACH_CLIENT(true, {
149                         assert(it.entcs);
150                         _entcs_send(it.entcs, msg_entity = player, BITS(23), MSG_ONE);
151                 });
152         }
153
154         void entcs_detach(entity player)
155         {
156                 if (!player.entcs) return;
157                 delete(player.entcs);
158                 player.entcs = NULL;
159         }
160
161 #endif
162
163 #ifdef CSQC
164
165         void Ent_RemoveEntCS(entity this)
166         {
167                 int n = this.sv_entnum;
168                 entity e = entcs_receiver(n);
169                 entcs_receiver(n, NULL);
170                 strfree(e.netname);
171                 strfree(e.model);
172                 if (e != this) delete(e);
173         }
174
175         void entcs_think(entity this)
176         {
177                 entity e = CSQCModel_server2csqc(this.sv_entnum);
178                 if (e == NULL)
179                 {
180                         this.has_origin = this.has_sv_origin;
181                         return;
182                 }
183                 this.has_origin = true;
184                 this.origin = e.origin;
185                 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
186                 if (this.model != e.model)
187                 {
188                         strcpy(this.model, e.model);
189                 }
190         }
191
192         bool ReadEntcs(entity this)
193         {
194                 int n = ReadByte();
195                 entity e = entcs_receiver(n);
196                 if (e == NULL)
197                 {
198                         if (!this)
199                                 // initial = temp
200                                 e = new_pure(entcs_receiver);
201                         else
202                                 // initial = linked
203                                 e = this;
204                         setthink(e, entcs_think);
205                         entcs_receiver(n, e);
206                 }
207                 else if (e != this && this)
208                 {
209                         // upgrade to linked
210                         delete(e);
211                         e = this;
212                         setthink(e, entcs_think);
213                         entcs_receiver(n, e);
214                 }
215
216                 InterpolateOrigin_Undo(e);
217                 e.sv_entnum = n;
218                 int sf = ReadShort();
219                 e.has_sv_origin = false;
220                 e.m_entcs_private = boolean(sf & BIT(0));
221                 FOREACH(EntCSProps, sf & BIT(it.m_id),
222                 {
223                         it.m_receive(e);
224                 });
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