1 #include "selection.qh"
3 #include "weaponsystem.qh"
4 #include <common/t_items.qh>
5 #include <common/constants.qh>
6 #include <common/util.qh>
7 #include <common/items/item.qh>
8 #include <common/weapons/all.qh>
9 #include <common/state.qh>
10 #include <common/mutators/mutator/waypoints/waypointsprites.qh>
12 // switch between weapons
13 void Send_WeaponComplain(entity e, float wpn, float type)
16 WriteHeader(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
17 WriteByte(MSG_ONE, wpn);
18 WriteByte(MSG_ONE, type);
21 void Weapon_whereis(Weapon this, entity cl)
23 if (!autocvar_g_showweaponspawns) return;
24 for (entity it = NULL; (it = findfloat(it, weapon, this.m_id)); )
26 if (it.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
28 if (!(it.flags & FL_ITEM))
30 entity wp = WaypointSprite_Spawn(
33 NULL, it.origin + ('0 0 1' * it.maxs.z) * 1.2,
39 wp.wp_extra = this.m_id;
43 bool client_hasweapon(entity cl, Weapon wpn, float andammo, bool complain)
48 if (time < cl.hasweapon_complain_spam)
51 // ignore hook button when using other offhand equipment
52 if (cl.offhand != OFFHAND_HOOK)
53 if (wpn == WEP_HOOK && !((cl.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
57 cl.hasweapon_complain_spam = time + 0.2;
62 sprint(cl, "Invalid weapon\n");
65 if (cl.weapons & WepSet_FromWeapon(wpn))
69 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
75 WITH(entity, self, cl, f = wpn.wr_checkammo1(wpn) + wpn.wr_checkammo2(wpn));
77 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
78 if(wpn == WEP_MINE_LAYER)
79 FOREACH_ENTITY_CLASS("mine", it.owner == cl,
82 break; // no need to continue
88 if(IS_REAL_CLIENT(cl))
90 play2(cl, SND(UNAVAILABLE));
91 Send_WeaponComplain (cl, wpn.m_id, 0);
101 // Report Proper Weapon Status / Modified Weapon Ownership Message
102 if (weaponsInMap & WepSet_FromWeapon(wpn))
104 Send_WeaponComplain(cl, wpn.m_id, 1);
105 Weapon_whereis(wpn, cl);
109 Send_WeaponComplain (cl, wpn.m_id, 2);
112 play2(cl, SND(UNAVAILABLE));
117 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
119 // We cannot tokenize in this function, as GiveItems calls this
120 // function. Thus we must use car/cdr.
121 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
122 WepSet wepset = '0 0 0';
123 switchtonext = switchtolast = 0;
124 first_valid = prev_valid = 0;
128 if(skipmissing || pl.selectweapon == 0)
129 weaponcur = PS(pl).m_switchweapon.m_id;
131 weaponcur = pl.selectweapon;
138 string rest = weaponorder;
141 weaponwant = stof(car(rest)); rest = cdr(rest);
142 wep = Weapons_from(weaponwant);
143 wepset = wep.m_wepset;
145 if(wep.impulse != imp)
148 bool have_other = false;
149 FOREACH(Weapons, it != WEP_Null, {
151 if(it.impulse == imp || imp < 0)
152 if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
156 // skip weapons we don't own that aren't normal and aren't in the map
157 if(!(pl.weapons & wepset))
158 if(!(weaponsInMap & wepset))
159 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
164 if(!skipmissing || client_hasweapon(pl, wep, true, false))
169 first_valid = weaponwant;
170 if(weaponwant == weaponcur)
179 prev_valid = weaponwant;
189 // complain (but only for one weapon on the button that has been pressed)
192 self.weaponcomplainindex += 1;
193 c = (self.weaponcomplainindex % c) + 1;
197 weaponwant = stof(car(rest)); rest = cdr(rest);
198 wep = Weapons_from(weaponwant);
199 wepset = wep.m_wepset;
201 if(wep.impulse != imp)
204 bool have_other = false;
205 FOREACH(Weapons, it != WEP_Null, {
207 if(it.impulse == imp || imp < 0)
208 if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
212 // skip weapons we don't own that aren't normal and aren't in the map
213 if(!(pl.weapons & wepset))
214 if(!(weaponsInMap & wepset))
215 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
221 client_hasweapon(pl, wep, true, true);
229 void W_SwitchWeapon_Force(entity e, Weapon wep)
231 e.cnt = PS(e).m_switchweapon.m_id;
232 PS(e).m_switchweapon = wep;
233 e.selectweapon = wep.m_id;
236 // perform weapon to attack (weaponstate and attack_finished check is here)
237 void W_SwitchToOtherWeapon(entity pl)
239 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
241 WepSet set = WepSet_FromWeapon(PS(pl).m_weapon);
242 if (pl.weapons & set)
245 ww = w_getbestweapon(pl);
250 ww = w_getbestweapon(pl);
252 if (ww == WEP_Null) return;
253 W_SwitchWeapon_Force(pl, ww);
256 void W_SwitchWeapon(Weapon w)
258 if (PS(self).m_switchweapon != w)
260 if (client_hasweapon(self, w, true, true))
261 W_SwitchWeapon_Force(self, w);
263 self.selectweapon = w.m_id; // update selectweapon ANYWAY
265 else if(!forbidWeaponUse(self)) {
267 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
268 w.wr_reload(w, actor, weaponentity);
272 void W_CycleWeapon(string weaponorder, float dir)
275 w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
277 W_SwitchWeapon(Weapons_from(w));
280 void W_NextWeaponOnImpulse(float imp)
283 w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
285 W_SwitchWeapon(Weapons_from(w));
289 void W_NextWeapon(float list)
292 W_CycleWeapon(weaponorder_byid, -1);
294 W_CycleWeapon(self.weaponorder_byimpulse, -1);
296 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
300 void W_PreviousWeapon(float list)
303 W_CycleWeapon(weaponorder_byid, +1);
305 W_CycleWeapon(self.weaponorder_byimpulse, +1);
307 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
310 // previously used if exists and has ammo, (second) best otherwise
311 void W_LastWeapon(entity this)
313 Weapon wep = Weapons_from(this.cnt);
314 if (client_hasweapon(this, wep, true, false))
317 W_SwitchToOtherWeapon(this);