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(); }) \
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); }) \
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; }) \
19 { WriteByte(chan, bound(0, this.health / 10, 255)); /* FIXME: use a better scale? */ }, \
20 { this.healthvalue = ReadByte() * 10; }) \
22 PROP(false, armorvalue, \
23 { WriteByte(chan, bound(0, this.armorvalue / 10, 255)); /* FIXME: use a better scale? */ }, \
24 { this.armorvalue = ReadByte() * 10; }) \
27 { WriteString(chan, this.netname); }, \
28 { if (this.netname) strunzone(this.netname); this.netname = strzone(ReadString()); }) \
31 { WriteString(chan, this.model); }, \
32 { if (this.model) strunzone(this.model); this.model = strzone(ReadString()); }) \
35 { WriteByte(chan, this.skin); }, \
36 { this.skin = ReadByte(); }) \
38 PROP(true, clientcolors, \
39 { WriteByte(chan, this.clientcolors); }, \
40 { this.colormap = ReadByte(); }) \
43 { WriteShort(chan, this.frags); }, \
44 { this.frags = ReadShort(); }) \
50 int ENTCS_PUBLICMASK = 0;
51 STATIC_INIT(ENTCS_PUBLICMASK)
54 #define X(public, fld, sv, cl) { if (public) ENTCS_PUBLICMASK |= BIT(i); } i += 1;
57 if (i >= BITS(16 - 1)) LOG_FATAL("Exceeded ENTCS_NETPROPS limit");
60 bool _entcs_send(entity this, entity to, int sf, int chan)
62 entity player = this.owner;
63 sf |= BIT(0) | BIT(1);
64 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
69 sf |= this.m_forceupdate;
70 this.m_forceupdate = 0;
73 && (IS_PLAYER(player) // player must be active
74 || player == to) // player is self
77 if (chan == MSG_ENTITY)
78 WriteHeader(chan, ENT_CLIENT_ENTCS);
80 WriteHeader(chan, CLIENT_ENTCS);
81 WriteByte(chan, etof(player) - 1);
84 #define X(public, fld, sv, cl) { if (sf & BIT(i)) sv; } i += 1;
90 bool entcs_send(entity this, entity to, int sf)
92 return _entcs_send(this, to, sf, MSG_ENTITY);
95 void entcs_think(entity this)
97 this.nextthink = time + 0.033333333333; // TODO: increase this to like 0.15 once the client can do smoothing
98 entity o = this.owner;
100 #define X(public, fld, sv, cl) \
101 if (o.fld != this.fld) \
104 this.SendFlags |= BIT(i); \
109 setorigin(this, this.origin); // relink
112 void entcs_attach(entity player)
114 entity e = player.entcs = new(entcs_sender);
116 setthink(e, entcs_think);
118 Net_LinkEntity(e, false, 0, entcs_send);
119 if (!IS_REAL_CLIENT(player)) return;
120 FOREACH_CLIENT(true, {
122 _entcs_send(it.entcs, msg_entity = player, BITS(23), MSG_ONE);
126 void entcs_detach(entity player)
128 if (!player.entcs) return;
129 delete(player.entcs);
137 void Ent_RemoveEntCS(entity this)
139 int n = this.sv_entnum;
140 entity e = entcs_receiver(n);
141 entcs_receiver(n, NULL);
142 if (e != this) delete(e);
145 void entcs_think(entity this)
147 entity e = CSQCModel_server2csqc(this.sv_entnum);
150 this.has_origin = this.has_sv_origin;
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)
158 if (this.model) strunzone(this.model);
159 this.model = strzone(e.model);
163 bool ReadEntcs(entity this)
166 entity e = entcs_receiver(n);
175 e = new(entcs_receiver);
179 setthink(e, entcs_think);
180 entcs_receiver(n, e);
182 else if (this && e != this)
184 this.classname = "entcs_gc";
188 InterpolateOrigin_Undo(this);
190 int sf = ReadShort();
191 this.has_sv_origin = false;
192 this.m_entcs_private = boolean(sf & BIT(0));
194 #define X(public, fld, sv, cl) { if (sf & BIT(i)) cl; } i += 1;
197 this.iflags |= IFLAG_ORIGIN;
198 InterpolateOrigin_Note(this);
199 getthink(this)(this);
203 NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
208 this.classname = "entcs_receiver";
209 this.entremove = Ent_RemoveEntCS;
211 return ReadEntcs(this);
214 NET_HANDLE(CLIENT_ENTCS, bool isnew)
216 return ReadEntcs(NULL);