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(); }) \
46 int ENTCS_PUBLICMASK = 0;
47 STATIC_INIT(ENTCS_PUBLICMASK)
50 #define X(public, fld, sv, cl) { if (public) ENTCS_PUBLICMASK |= BIT(i); } i += 1;
53 if (i >= BITS(16 - 1)) LOG_FATAL("Exceeded ENTCS_NETPROPS limit");
56 bool _entcs_send(entity this, entity to, int sf, int chan)
58 entity player = this.owner;
59 sf |= BIT(0) | BIT(1);
60 if (IS_PLAYER(to) || to.caplayer) // unless spectating,
62 bool same_team = (to == player) || (teamplay && player.team == to.team);
63 if (!same_team && !radar_showennemies) sf &= ENTCS_PUBLICMASK; // no private updates
65 sf |= this.m_forceupdate;
66 this.m_forceupdate = 0;
69 && (IS_PLAYER(player) // player must be active
70 || player == to) // player is self
73 if (chan == MSG_ENTITY)
74 WriteHeader(chan, ENT_CLIENT_ENTCS);
76 WriteHeader(chan, CLIENT_ENTCS);
77 WriteByte(chan, etof(player) - 1);
80 #define X(public, fld, sv, cl) { if (sf & BIT(i)) sv; } i += 1;
86 bool entcs_send(entity this, entity to, int sf)
88 return _entcs_send(this, to, sf, MSG_ENTITY);
91 void entcs_think(entity this)
93 this.nextthink = time + 0.033333333333; // TODO: increase this to like 0.15 once the client can do smoothing
94 entity o = this.owner;
96 #define X(public, fld, sv, cl) \
97 if (o.fld != this.fld) \
100 this.SendFlags |= BIT(i); \
105 setorigin(this, this.origin); // relink
108 void entcs_attach(entity player)
110 entity e = player.entcs = new(entcs_sender);
112 setthink(e, entcs_think);
114 Net_LinkEntity(e, false, 0, entcs_send);
115 if (!IS_REAL_CLIENT(player)) return;
116 FOREACH_CLIENT(true, {
118 _entcs_send(it.entcs, msg_entity = player, BITS(23), MSG_ONE);
122 void entcs_detach(entity player)
124 if (!player.entcs) return;
125 delete(player.entcs);
133 void Ent_RemoveEntCS(entity this)
135 int n = this.sv_entnum;
136 entity e = entcs_receiver(n);
137 entcs_receiver(n, NULL);
138 if (e != this) delete(e);
141 void entcs_think(entity this)
143 entity e = CSQCModel_server2csqc(this.sv_entnum);
146 this.has_origin = this.has_sv_origin;
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)
154 if (this.model) strunzone(this.model);
155 this.model = strzone(e.model);
159 bool ReadEntcs(entity this)
162 entity e = entcs_receiver(n);
171 e = new(entcs_receiver);
175 setthink(e, entcs_think);
176 entcs_receiver(n, e);
178 else if (this && e != this)
180 this.classname = "entcs_gc";
184 InterpolateOrigin_Undo(this);
186 int sf = ReadShort();
187 this.has_sv_origin = false;
188 this.m_entcs_private = boolean(sf & BIT(0));
190 #define X(public, fld, sv, cl) { if (sf & BIT(i)) cl; } i += 1;
193 this.iflags |= IFLAG_ORIGIN;
194 InterpolateOrigin_Note(this);
195 getthink(this)(this);
199 NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
204 this.classname = "entcs_receiver";
205 this.entremove = Ent_RemoveEntCS;
207 return ReadEntcs(this);
210 NET_HANDLE(CLIENT_ENTCS, bool isnew)
212 return ReadEntcs(NULL);