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