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