]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/ent_cs.qc
entcs: update connecting clients
[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, this.health / 10);  /* FIXME: use a better scale? */ }, \
20         { this.healthvalue = ReadByte() * 10; }) \
21     \
22         PROP(false, armorvalue, \
23         { WriteByte(chan, this.armorvalue / 10);  /* 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_init;
53         bool entcs_send(entity this, entity to, int sf)
54         {
55                 int chan = entcs_send_init ? MSG_INIT : MSG_ENTITY;
56                 entcs_send_init = false;
57                 entity player = this.owner;
58                 sf |= BIT(0) | BIT(1);
59                 if (IS_PLAYER(to) || to.caplayer)                                  // unless spectating,
60                 {
61                         bool same_team = (to == player) || (teamplay && player.team == to.team);
62                         if (!same_team && !radar_showennemies) sf &= ENTCS_PUBLICMASK; // no private updates
63                 }
64                 sf |= this.m_forceupdate;
65                 this.m_forceupdate = 0;
66                 bool valid =
67                     IS_PLAYER(player)             // player must be active
68                     || player == to               // player is self
69                 ;
70                 if (!valid) sf = 0;
71                 WriteHeader(chan, ENT_CLIENT_ENTCS);
72                 WriteShort(chan, sf);
73                 int i = 1;
74                 #define X(public, fld, sv, cl) { if (sf & BIT(i)) sv; } i += 1;
75                 ENTCS_NETPROPS(X);
76         #undef X
77                 return true;
78         }
79
80         void entcs_think()
81         {
82                 SELFPARAM();
83                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
84                 entity o = this.owner;
85                 int i = 1;
86                 #define X(public, fld, sv, cl) \
87                         if (o.fld != this.fld) \
88                         { \
89                                 this.fld = o.fld; \
90                                 this.SendFlags |= BIT(i); \
91                         } \
92                         i += 1;
93                 ENTCS_NETPROPS(X);
94         #undef X
95         }
96
97         void entcs_attach(entity player)
98         {
99                 entity e = player.entcs = new(entcs_sender);
100                 make_pure(e);
101                 e.owner = player;
102                 e.think = entcs_think;
103                 e.nextthink = time;
104                 Net_LinkEntity(e, false, 0, entcs_send);
105                 if (!IS_REAL_CLIENT(player)) return;
106                 FOR_EACH_CLIENT(e)
107                 {
108                         assert(e.entcs);
109                         entcs_send_init = true;
110                         entcs_send(e.entcs, player, BITS(23));
111                 }
112         }
113
114         void entcs_detach(entity player)
115         {
116                 if (!player.entcs) return;
117                 remove(player.entcs);
118                 player.entcs = NULL;
119         }
120
121 #endif
122
123 #ifdef CSQC
124
125         void Ent_RemoveEntCS()
126         {
127                 SELFPARAM();
128                 entcs_receiver(this.sv_entnum, NULL);
129         }
130
131         void entcs_think()
132         {
133                 SELFPARAM();
134                 entity e = CSQCModel_server2csqc(this.sv_entnum);
135                 if (e == NULL)
136                 {
137                         this.has_origin = this.has_sv_origin;
138                         return;
139                 }
140                 this.has_origin = true;
141                 this.origin = e.origin;
142                 // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
143                 if (this.model != e.model)
144                 {
145                         if (this.model) strunzone(this.model);
146                         this.model = strzone(e.model);
147                 }
148         }
149
150         NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
151         {
152                 if (isnew)
153                 {
154                         make_pure(this);
155                         this.classname = "entcs_receiver";
156                         this.entremove = Ent_RemoveEntCS;
157                         this.think = entcs_think;
158                 }
159                 InterpolateOrigin_Undo(this);
160                 int sf = ReadShort();
161                 this.has_sv_origin = false;
162                 this.m_entcs_private = boolean(sf & BIT(0));
163                 int i = 1;
164                 #define X(public, fld, sv, cl) { if (sf & BIT(i)) cl; } i += 1;
165                 ENTCS_NETPROPS(X);
166         #undef X
167                 entcs_receiver(this.sv_entnum, this);
168                 this.iflags |= IFLAG_ORIGIN;
169                 InterpolateOrigin_Note(this);
170                 this.think();
171                 return true;
172         }
173
174 #endif