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