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