Merge branch 'master' into Lyberta/StandaloneOverkillWeapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / wepent.qc
1 #include "wepent.qh"
2
3 #define WEPENT_SET_NORMAL(var, x) MACRO_BEGIN \
4         var = x; \
5 MACRO_END
6
7 // #define PROP(public, fld, set, sv, cl)
8 #define WEPENT_NETPROPS(PROP) PROP(false, sv_entnum, WEPENT_SET_NORMAL, {}, {}) /* sentinel */ \
9         PROP(false, m_switchweapon, WEPENT_SET_NORMAL, \
10         { WriteByte(chan, this.m_switchweapon.m_id); }, \
11         { (viewmodels[this.m_wepent_slot]).switchweapon = Weapons_from(ReadByte()); }) \
12     \
13     PROP(false, m_switchingweapon, WEPENT_SET_NORMAL, \
14         { WriteByte(chan, this.m_switchingweapon.m_id); }, \
15         { (viewmodels[this.m_wepent_slot]).switchingweapon = Weapons_from(ReadByte()); }) \
16     \
17     PROP(false, m_weapon, WEPENT_SET_NORMAL, \
18         { WriteByte(chan, this.m_weapon.m_id); }, \
19         { (viewmodels[this.m_wepent_slot]).activeweapon = Weapons_from(ReadByte()); }) \
20     \
21     PROP(false, m_alpha, WEPENT_SET_NORMAL, \
22         { WriteByte(chan, rint(bound(-1, 254 * this.m_alpha, 254) - -1)); }, \
23         { (viewmodels[this.m_wepent_slot]).alpha = (ReadByte() + -1) / 254; }) \
24     \
25     PROP(false, vortex_charge, WEPENT_SET_NORMAL, \
26         { WriteByte(chan, this.vortex_charge * 255); }, \
27         { (viewmodels[this.m_wepent_slot]).vortex_charge = ReadByte() / 255; }) \
28     \
29     PROP(false, oknex_charge, WEPENT_SET_NORMAL, \
30         { WriteByte(chan, this.oknex_charge * 16); }, \
31         { (viewmodels[this.m_wepent_slot]).oknex_charge = ReadByte() / 16; }) \
32     \
33     PROP(false, m_gunalign, WEPENT_SET_NORMAL, \
34         { WriteByte(chan, this.m_gunalign); }, \
35         { (viewmodels[this.m_wepent_slot]).m_gunalign = ReadByte(); }) \
36     \
37     PROP(false, porto_v_angle_held, WEPENT_SET_NORMAL, \
38         { WriteByte(chan, this.porto_v_angle_held); if(this.porto_v_angle_held) { \
39                  WriteAngle(chan, this.porto_v_angle.x); WriteAngle(chan, this.porto_v_angle.y); \
40                 } }, \
41         { (viewmodels[this.m_wepent_slot]).angles_held_status = ReadByte(); if((viewmodels[this.m_wepent_slot]).angles_held_status) { \
42                 (viewmodels[this.m_wepent_slot]).angles_held_x = ReadAngle(); (viewmodels[this.m_wepent_slot]).angles_held_y = ReadAngle(); (viewmodels[this.m_wepent_slot]).angles_held_z = 0; } \
43                 else { (viewmodels[this.m_wepent_slot]).angles_held = '0 0 0'; } }) \
44     \
45     PROP(false, tuba_instrument, WEPENT_SET_NORMAL, \
46         { WriteByte(chan, this.tuba_instrument); }, \
47         { (viewmodels[this.m_wepent_slot]).tuba_instrument = ReadByte(); }) \
48     \
49     PROP(false, hagar_load, WEPENT_SET_NORMAL, \
50         { WriteByte(chan, this.hagar_load); }, \
51         { (viewmodels[this.m_wepent_slot]).hagar_load = ReadByte(); }) \
52     \
53     PROP(false, minelayer_mines, WEPENT_SET_NORMAL, \
54         { WriteByte(chan, this.minelayer_mines); }, \
55         { (viewmodels[this.m_wepent_slot]).minelayer_mines = ReadByte(); }) \
56     \
57     PROP(false, arc_heat_percent, WEPENT_SET_NORMAL, \
58         { WriteByte(chan, this.arc_heat_percent * 16); }, \
59         { (viewmodels[this.m_wepent_slot]).arc_heat_percent = ReadByte() / 16; }) \
60     \
61     PROP(false, vortex_chargepool_ammo, WEPENT_SET_NORMAL, \
62         { WriteByte(chan, this.vortex_chargepool_ammo * 16); }, \
63         { (viewmodels[this.m_wepent_slot]).vortex_chargepool_ammo = ReadByte() / 16; }) \
64     \
65         PROP(false, oknex_chargepool_ammo, WEPENT_SET_NORMAL, \
66         { WriteByte(chan, this.oknex_chargepool_ammo * 16); }, \
67         { (viewmodels[this.m_wepent_slot]).oknex_chargepool_ammo = ReadByte() / 16; }) \
68     \
69         PROP(false, clip_load, WEPENT_SET_NORMAL, \
70         { WriteShort(chan, this.clip_load); }, \
71         { (viewmodels[this.m_wepent_slot]).clip_load = ReadShort(); }) \
72     \
73     PROP(false, clip_size, WEPENT_SET_NORMAL, \
74         { WriteShort(chan, this.clip_size); }, \
75         { (viewmodels[this.m_wepent_slot]).clip_size = ReadShort(); }) \
76     \
77         /**/
78
79 #ifdef SVQC
80
81         int WEPENT_PUBLICMASK = 0;
82         STATIC_INIT(WEPENT_PUBLICMASK)
83         {
84                 int i = 0;
85                 #define X(public, fld, set, sv, cl) { \
86                         if (public) { \
87                                 WEPENT_PUBLICMASK |= BIT(i); \
88                         } \
89                         i += 1; \
90                 }
91                 WEPENT_NETPROPS(X);
92         #undef X
93                 if (i >= BITS(24 - 1)) LOG_FATAL("Exceeded WEPENT_NETPROPS limit");
94         }
95
96         bool _wepent_send(entity this, entity to, int sf, int chan)
97         {
98                 sf |= this.m_forceupdate;
99                 this.m_forceupdate = 0;
100                 if (chan == MSG_ENTITY)
101                         WriteHeader(chan, ENT_CLIENT_WEPENT);
102                 else
103                         WriteHeader(chan, CLIENT_WEPENT);
104                 .entity weaponentity = this.owner.weaponentity_fld;
105                 WriteByte(chan, weaponslot(weaponentity));
106                 WriteInt24_t(chan, sf);
107                 int i = 0;
108                 #define X(public, fld, set, sv, cl) { \
109                         if (sf & BIT(i)) { \
110                                 sv; \
111                         } \
112                         i += 1; \
113                 }
114                 WEPENT_NETPROPS(X);
115         #undef X
116                 return true;
117         }
118
119         bool wepent_send(entity this, entity to, int sf)
120         {
121                 return _wepent_send(this, to, sf, MSG_ENTITY);
122         }
123
124         void wepent_think(entity this)
125         {
126                 if(wasfreed(this.owner) || !this.owner)
127                 {
128                         delete(this);
129                         return;
130                 }
131
132                 this.nextthink = time;
133
134                 entity o = this.owner;
135
136                 int i = 0;
137                 #define X(public, fld, set, sv, cl) { \
138                         if (this.fld != o.fld) { \
139                                 set(this.fld, o.fld); \
140                                 this.SendFlags |= BIT(i); \
141                         } \
142                         i += 1; \
143                 }
144                 WEPENT_NETPROPS(X);
145         #undef X
146         }
147
148         bool wepent_customize(entity this, entity client)
149         {
150                 entity e = WaypointSprite_getviewentity(client);
151                 .entity weaponentity = this.owner.weaponentity_fld;
152                 return e.(weaponentity) == this.owner;
153         }
154
155         void wepent_link(entity wep)
156         {
157                 entity e = new(wepent_sender);
158                 e.owner = wep;
159                 setthink(e, wepent_think);
160                 e.nextthink = time;
161                 //e.drawonlytoclient = wep.owner;
162                 setcefc(e, wepent_customize);
163                 Net_LinkEntity(e, false, 0, wepent_send);
164         }
165
166 #endif
167
168 #ifdef CSQC
169
170         bool ReadWepent(entity this)
171         {
172                 int slot = ReadByte();
173                 this.m_wepent_slot = slot;
174                 viewmodels[slot].m_wepent_slot = slot;
175                 int sf = ReadInt24_t();
176                 int i = 0;
177                 #define X(public, fld, set, sv, cl) { \
178                         if (sf & BIT(i)) { \
179                                 cl; \
180                         } \
181                         i += 1; \
182                 }
183                 WEPENT_NETPROPS(X);
184         #undef X
185                 return true;
186         }
187
188         NET_HANDLE(ENT_CLIENT_WEPENT, bool isnew)
189         {
190                 if (isnew)
191                         this.classname = "wepent_receiver";
192                 return ReadWepent(this);
193         }
194
195         NET_HANDLE(CLIENT_WEPENT, bool isnew)
196         {
197                 return ReadWepent(NULL);
198         }
199
200 #endif