]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/ent_cs.qc
entcs: use SAME_TEAM
[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                 do {
66                         if (radar_showennemies) break;
67                         if (SAME_TEAM(to, player)) break;
68                         if (!(IS_PLAYER(to) || to.caplayer) && time > game_starttime) break;
69                         sf &= ENTCS_PUBLICMASK; // no private updates
70                 } while (0);
71                 sf |= this.m_forceupdate;
72                 this.m_forceupdate = 0;
73                 if (chan == MSG_ENTITY)
74                         WriteHeader(chan, ENT_CLIENT_ENTCS);
75                 else
76                         WriteHeader(chan, CLIENT_ENTCS);
77                 WriteByte(chan, etof(player) - 1);
78                 WriteShort(chan, sf);
79                 int i = 0;
80                 #define X(public, fld, sv, cl) { \
81                         if (sf & BIT(i)) { \
82                                 sv; \
83                         } \
84                         i += 1; \
85                 }
86                 ENTCS_NETPROPS(X);
87         #undef X
88                 return true;
89         }
90
91         bool entcs_send(entity this, entity to, int sf)
92         {
93                 return _entcs_send(this, to, sf, MSG_ENTITY);
94         }
95
96         void entcs_think(entity this)
97         {
98                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
99                 entity o = this.owner;
100                 int i = 0;
101                 #define X(public, fld, sv, cl) { \
102                         if (o.fld != this.fld) { \
103                                 this.fld = o.fld; \
104                                 this.SendFlags |= BIT(i); \
105                         } \
106                         i += 1; \
107                 }
108                 ENTCS_NETPROPS(X);
109         #undef X
110             setorigin(this, this.origin);  // relink
111         }
112
113         void entcs_attach(entity player)
114         {
115                 entity e = player.entcs = new(entcs_sender);
116                 e.owner = player;
117                 setthink(e, entcs_think);
118                 e.nextthink = time;
119                 Net_LinkEntity(e, false, 0, entcs_send);
120                 if (!IS_REAL_CLIENT(player)) return;
121                 FOREACH_CLIENT(true, {
122                         assert(it.entcs);
123                         _entcs_send(it.entcs, msg_entity = player, BITS(23), MSG_ONE);
124                 });
125         }
126
127         void entcs_detach(entity player)
128         {
129                 if (!player.entcs) return;
130                 delete(player.entcs);
131                 player.entcs = NULL;
132         }
133
134 #endif
135
136 #ifdef CSQC
137
138         void Ent_RemoveEntCS(entity this)
139         {
140                 int n = this.sv_entnum;
141                 entity e = entcs_receiver(n);
142                 entcs_receiver(n, NULL);
143                 if (e != this) delete(e);
144         }
145
146         void entcs_think(entity this)
147         {
148                 entity e = CSQCModel_server2csqc(this.sv_entnum);
149                 if (e == NULL)
150                 {
151                         this.has_origin = this.has_sv_origin;
152                         return;
153                 }
154                 this.has_origin = true;
155                 this.origin = e.origin;
156                 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
157                 if (this.model != e.model)
158                 {
159                         if (this.model) strunzone(this.model);
160                         this.model = strzone(e.model);
161                 }
162         }
163
164         bool ReadEntcs(entity this)
165         {
166                 int n = ReadByte();
167                 entity e = entcs_receiver(n);
168                 #define X(e) { \
169                         setthink(e, entcs_think); \
170                         entcs_receiver(n, e); \
171                 }
172                 if (e == NULL)
173                 {
174                         if (!this)
175                         {
176                                 // initial = temp
177                                 e = new_pure(entcs_receiver);
178                                 X(e);
179                         }
180                         else
181                         {
182                                 // initial = linked
183                                 e = this;
184                                 X(e);
185                         }
186                 }
187                 else if (e != this && this)
188                 {
189                         // upgrade to linked
190                         delete(e);
191                         e = this;
192                         X(e);
193                 }
194                 #undef X
195                 InterpolateOrigin_Undo(e);
196                 e.sv_entnum = n;
197                 int sf = ReadShort();
198                 e.has_sv_origin = false;
199                 e.m_entcs_private = boolean(sf & BIT(0));
200                 int i = 0;
201                 #define X(public, fld, sv, cl) { \
202                         if (sf & BIT(i)) { \
203                                 cl; \
204                         } \
205                         i += 1; \
206                 }
207                 ENTCS_NETPROPS(X);
208         #undef X
209                 e.iflags |= IFLAG_ORIGIN;
210                 InterpolateOrigin_Note(e);
211                 getthink(e)(e);
212                 return true;
213         }
214
215         NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
216         {
217                 if (isnew)
218                 {
219                         make_pure(this);
220                         this.classname = "entcs_receiver";
221                         this.entremove = Ent_RemoveEntCS;
222                 }
223                 return ReadEntcs(this);
224         }
225
226         NET_HANDLE(CLIENT_ENTCS, bool isnew)
227         {
228                 return ReadEntcs(NULL);
229         }
230
231 #endif