]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/ent_cs.qc
1290a9356477a0704c3e15893c94eb1b8a369518
[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         /**/
43
44 #ifdef SVQC
45
46         int ENTCS_PUBLICMASK = 0;
47         STATIC_INIT(ENTCS_PUBLICMASK)
48         {
49                 int i = 1;
50                 #define X(public, fld, sv, cl) { if (public) ENTCS_PUBLICMASK |= BIT(i); } i += 1;
51                 ENTCS_NETPROPS(X);
52         #undef X
53                 if (i >= BITS(16 - 1)) LOG_FATAL("Exceeded ENTCS_NETPROPS limit");
54         }
55
56         bool _entcs_send(entity this, entity to, int sf, int chan)
57         {
58                 entity player = this.owner;
59                 sf |= BIT(0) | BIT(1);
60                 if (IS_PLAYER(to) || to.caplayer)                                  // unless spectating,
61                 {
62                         bool same_team = (to == player) || (teamplay && player.team == to.team);
63                         if (!same_team && !radar_showennemies) sf &= ENTCS_PUBLICMASK; // no private updates
64                 }
65                 sf |= this.m_forceupdate;
66                 this.m_forceupdate = 0;
67                 bool valid =
68                         time > game_starttime
69                     && (IS_PLAYER(player)          // player must be active
70                     || player == to)               // player is self
71                 ;
72                 if (!valid) sf = 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 = 1;
80                 #define X(public, fld, sv, cl) { if (sf & BIT(i)) sv; } i += 1;
81                 ENTCS_NETPROPS(X);
82         #undef X
83                 return true;
84         }
85
86         bool entcs_send(entity this, entity to, int sf)
87         {
88                 return _entcs_send(this, to, sf, MSG_ENTITY);
89         }
90
91         void entcs_think(entity this)
92         {
93                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
94                 entity o = this.owner;
95                 int i = 1;
96                 #define X(public, fld, sv, cl) \
97                         if (o.fld != this.fld) \
98                         { \
99                                 this.fld = o.fld; \
100                                 this.SendFlags |= BIT(i); \
101                         } \
102                         i += 1;
103                 ENTCS_NETPROPS(X);
104         #undef X
105             setorigin(this, this.origin);  // relink
106         }
107
108         void entcs_attach(entity player)
109         {
110                 entity e = player.entcs = new(entcs_sender);
111                 e.owner = player;
112                 setthink(e, entcs_think);
113                 e.nextthink = time;
114                 Net_LinkEntity(e, false, 0, entcs_send);
115                 if (!IS_REAL_CLIENT(player)) return;
116                 FOREACH_CLIENT(true, {
117                         assert(it.entcs);
118                         _entcs_send(it.entcs, msg_entity = player, BITS(23), MSG_ONE);
119                 });
120         }
121
122         void entcs_detach(entity player)
123         {
124                 if (!player.entcs) return;
125                 delete(player.entcs);
126                 player.entcs = NULL;
127         }
128
129 #endif
130
131 #ifdef CSQC
132
133         void Ent_RemoveEntCS(entity this)
134         {
135                 int n = this.sv_entnum;
136                 entity e = entcs_receiver(n);
137                 entcs_receiver(n, NULL);
138                 if (e != this) delete(e);
139         }
140
141         void entcs_think(entity this)
142         {
143                 entity e = CSQCModel_server2csqc(this.sv_entnum);
144                 if (e == NULL)
145                 {
146                         this.has_origin = this.has_sv_origin;
147                         return;
148                 }
149                 this.has_origin = true;
150                 this.origin = e.origin;
151                 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
152                 if (this.model != e.model)
153                 {
154                         if (this.model) strunzone(this.model);
155                         this.model = strzone(e.model);
156                 }
157         }
158
159         bool ReadEntcs(entity this)
160         {
161                 int n = ReadByte();
162                 entity e = entcs_receiver(n);
163                 if (e == NULL)
164                 {
165                         if (this)
166                         {
167                                 e = this;
168                         }
169                         else
170                         {
171                                 e = new(entcs_receiver);
172                                 make_pure(e);
173                         }
174                         e.sv_entnum = n;
175                         setthink(e, entcs_think);
176                         entcs_receiver(n, e);
177                 }
178                 else if (this && e != this)
179                 {
180                         this.classname = "entcs_gc";
181                         this.sv_entnum = n;
182                 }
183                 this = e;
184                 InterpolateOrigin_Undo(this);
185                 this.sv_entnum = n;
186                 int sf = ReadShort();
187                 this.has_sv_origin = false;
188                 this.m_entcs_private = boolean(sf & BIT(0));
189                 int i = 1;
190                 #define X(public, fld, sv, cl) { if (sf & BIT(i)) cl; } i += 1;
191                 ENTCS_NETPROPS(X);
192         #undef X
193                 this.iflags |= IFLAG_ORIGIN;
194                 InterpolateOrigin_Note(this);
195                 getthink(this)(this);
196                 return true;
197         }
198
199         NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
200         {
201                 if (isnew)
202                 {
203                         make_pure(this);
204                         this.classname = "entcs_receiver";
205                         this.entremove = Ent_RemoveEntCS;
206                 }
207                 return ReadEntcs(this);
208         }
209
210         NET_HANDLE(CLIENT_ENTCS, bool isnew)
211         {
212                 return ReadEntcs(NULL);
213         }
214
215 #endif