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