]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/ent_cs.qc
General cleanup/optimize
[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(MSG_ENTITY, etof(player) - 1); }, \
7         { this.sv_entnum = ReadByte(); }) \
8     \
9         PROP(false, origin, \
10         { WriteShort(MSG_ENTITY, this.origin.x);  WriteShort(MSG_ENTITY, this.origin.y); \
11           WriteShort(MSG_ENTITY, 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(MSG_ENTITY, 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(MSG_ENTITY, this.health / 10);  /* FIXME: use a better scale? */ }, \
20         { this.healthvalue = ReadByte() * 10; }) \
21     \
22         PROP(false, armorvalue, \
23         { WriteByte(MSG_ENTITY, this.armorvalue / 10);  /* FIXME: use a better scale? */ }, \
24         { this.armorvalue = ReadByte() * 10; }) \
25     \
26         PROP(true, netname, \
27         { WriteString(MSG_ENTITY, this.netname); }, \
28         { if (this.netname) strunzone(this.netname); this.netname = strzone(ReadString()); }) \
29     \
30         PROP(true, model, \
31         { WriteString(MSG_ENTITY, this.model); }, \
32         { if (this.model) strunzone(this.model); this.model = strzone(ReadString()); }) \
33     \
34         PROP(true, skin, \
35         { WriteByte(MSG_ENTITY, 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)
53         {
54                 entity player = this.owner;
55                 sf |= 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.deadflag == DEAD_NO // player must be alive
66                     || player == to               // player is self
67                 ;
68                 if (!valid) sf = 0;
69                 WriteHeader(MSG_ENTITY, ENT_CLIENT_ENTCS);
70                 WriteShort(MSG_ENTITY, sf);
71                 int i = 1;
72                 #define X(public, fld, sv, cl) { if (sf & BIT(i)) sv; } i += 1;
73                 ENTCS_NETPROPS(X);
74         #undef X
75                 return true;
76         }
77
78         void entcs_think()
79         {
80                 SELFPARAM();
81                 this.nextthink = time + 0.033333333333;  // TODO: increase this to like 0.15 once the client can do smoothing
82                 entity o = this.owner;
83                 int i = 1;
84                 #define X(public, fld, sv, cl) \
85                         if (o.fld != this.fld) \
86                         { \
87                                 this.fld = o.fld; \
88                                 this.SendFlags |= BIT(i); \
89                         } \
90                         i += 1;
91                 ENTCS_NETPROPS(X);
92         #undef X
93         }
94
95         void entcs_attach(entity player)
96         {
97                 entity e = player.entcs = new(entcs_sender);
98                 make_pure(e);
99                 e.owner = player;
100                 e.think = entcs_think;
101                 e.nextthink = time;
102                 Net_LinkEntity(e, false, 0, entcs_send);
103         }
104
105         void entcs_detach(entity player)
106         {
107                 if (!player.entcs) return;
108                 remove(player.entcs);
109                 player.entcs = NULL;
110         }
111
112 #endif
113
114 #ifdef CSQC
115
116         void Ent_RemoveEntCS()
117         {
118                 SELFPARAM();
119                 entcs_receiver[this.sv_entnum] = NULL;
120         }
121
122         void entcs_think()
123         {
124                 SELFPARAM();
125                 this.nextthink = time;
126                 entity e = CSQCModel_server2csqc(this.sv_entnum + 1);
127                 bool exists = e != NULL;
128                 if (exists)
129                 {
130                         this.has_origin = true;
131                         this.origin = e.origin;
132                         // `cl_forceplayermodels 1` sounds will be wrong until the player has been in the PVS, but so be it
133                         if (this.model != e.model)
134                         {
135                                 if (this.model) strunzone(this.model);
136                                 this.model = strzone(e.model);
137                         }
138                 }
139                 else
140                 {
141                         this.has_origin = this.has_sv_origin;
142                 }
143         }
144
145         NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
146         {
147                 if (isnew)
148                 {
149                         make_pure(this);
150                         this.classname = "entcs_receiver";
151                         this.entremove = Ent_RemoveEntCS;
152                         this.think = entcs_think;
153                         this.nextthink = time;
154                 }
155                 InterpolateOrigin_Undo(this);
156                 int sf = ReadShort();
157                 this.has_sv_origin = false;
158                 this.m_entcs_private = boolean(sf & 1);
159                 int i = 1;
160                 #define X(public, fld, sv, cl) { if (sf & BIT(i)) cl; } i += 1;
161                 ENTCS_NETPROPS(X);
162         #undef X
163                 entcs_receiver[this.sv_entnum] = this;
164                 this.iflags |= IFLAG_ORIGIN;
165                 InterpolateOrigin_Note(this);
166                 return true;
167         }
168
169 #endif