1 #include "selection.qh"
3 #include "weaponsystem.qh"
4 #include "../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/mutators/mutator/waypoints/waypointsprites.qh"
11 // switch between weapons
12 void Send_WeaponComplain(entity e, float wpn, float type)
15 WriteHeader(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
16 WriteByte(MSG_ONE, wpn);
17 WriteByte(MSG_ONE, type);
20 bool client_hasweapon(entity cl, Weapon wpn, float andammo, bool complain)
24 if (time < cl.hasweapon_complain_spam)
27 // ignore hook button when using other offhand equipment
28 if (cl.offhand != OFFHAND_HOOK)
29 if (wpn == WEP_HOOK && !((cl.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
33 cl.hasweapon_complain_spam = time + 0.2;
38 sprint(cl, "Invalid weapon\n");
41 if (cl.weapons & WepSet_FromWeapon(wpn))
45 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
51 WITH(entity, self, cl, f = wpn.wr_checkammo1(wpn) + wpn.wr_checkammo2(wpn));
53 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
55 if(wpn == WEP_MINE_LAYER)
56 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == cl)
63 if(IS_REAL_CLIENT(cl))
65 play2(cl, SND(UNAVAILABLE));
66 Send_WeaponComplain (cl, wpn.m_id, 0);
76 // Report Proper Weapon Status / Modified Weapon Ownership Message
77 if (weaponsInMap & WepSet_FromWeapon(wpn))
79 Send_WeaponComplain(cl, wpn.m_id, 1);
81 if(autocvar_g_showweaponspawns)
83 for(entity e = world; (e = findfloat(e, weapon, wpn.m_id)); )
85 if(e.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
87 if(!(e.flags & FL_ITEM))
89 entity wp = WaypointSprite_Spawn(
92 world, e.origin + ('0 0 1' * e.maxs.z) * 1.2,
98 wp.wp_extra = wpn.m_id;
104 Send_WeaponComplain (cl, wpn.m_id, 2);
107 play2(cl, SND(UNAVAILABLE));
112 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
114 // We cannot tokenize in this function, as GiveItems calls this
115 // function. Thus we must use car/cdr.
116 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
117 WepSet wepset = '0 0 0';
118 switchtonext = switchtolast = 0;
119 first_valid = prev_valid = 0;
123 if(skipmissing || pl.selectweapon == 0)
124 weaponcur = PS(pl).m_switchweapon.m_id;
126 weaponcur = pl.selectweapon;
133 string rest = weaponorder;
136 weaponwant = stof(car(rest)); rest = cdr(rest);
137 wep = Weapons_from(weaponwant);
138 wepset = wep.m_wepset;
140 if(wep.impulse != imp)
143 bool have_other = false;
144 FOREACH(Weapons, it != WEP_Null, {
146 if(it.impulse == imp || imp < 0)
147 if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
151 // skip weapons we don't own that aren't normal and aren't in the map
152 if(!(pl.weapons & wepset))
153 if(!(weaponsInMap & wepset))
154 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
159 if(!skipmissing || client_hasweapon(pl, wep, true, false))
164 first_valid = weaponwant;
165 if(weaponwant == weaponcur)
174 prev_valid = weaponwant;
184 // complain (but only for one weapon on the button that has been pressed)
187 self.weaponcomplainindex += 1;
188 c = (self.weaponcomplainindex % c) + 1;
192 weaponwant = stof(car(rest)); rest = cdr(rest);
193 wep = Weapons_from(weaponwant);
194 wepset = wep.m_wepset;
196 if(wep.impulse != imp)
199 bool have_other = false;
200 FOREACH(Weapons, it != WEP_Null, {
202 if(it.impulse == imp || imp < 0)
203 if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
207 // skip weapons we don't own that aren't normal and aren't in the map
208 if(!(pl.weapons & wepset))
209 if(!(weaponsInMap & wepset))
210 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
216 client_hasweapon(pl, wep, true, true);
224 void W_SwitchWeapon_Force(entity e, Weapon wep)
226 e.cnt = PS(e).m_switchweapon.m_id;
227 PS(e).m_switchweapon = wep;
228 e.selectweapon = wep.m_id;
231 // perform weapon to attack (weaponstate and attack_finished check is here)
232 void W_SwitchToOtherWeapon(entity pl)
234 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
236 WepSet set = WepSet_FromWeapon(PS(pl).m_weapon);
237 if (pl.weapons & set)
240 ww = w_getbestweapon(pl);
245 ww = w_getbestweapon(pl);
247 if (ww == WEP_Null) return;
248 W_SwitchWeapon_Force(pl, ww);
251 void W_SwitchWeapon(Weapon w)
253 if (PS(self).m_switchweapon != w)
255 if (client_hasweapon(self, w, true, true))
256 W_SwitchWeapon_Force(self, w);
258 self.selectweapon = w.m_id; // update selectweapon ANYWAY
260 else if(!forbidWeaponUse(self)) {
265 void W_CycleWeapon(string weaponorder, float dir)
268 w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
270 W_SwitchWeapon(Weapons_from(w));
273 void W_NextWeaponOnImpulse(float imp)
276 w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
278 W_SwitchWeapon(Weapons_from(w));
282 void W_NextWeapon(float list)
285 W_CycleWeapon(weaponorder_byid, -1);
287 W_CycleWeapon(self.weaponorder_byimpulse, -1);
289 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
293 void W_PreviousWeapon(float list)
296 W_CycleWeapon(weaponorder_byid, +1);
298 W_CycleWeapon(self.weaponorder_byimpulse, +1);
300 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
303 // previously used if exists and has ammo, (second) best otherwise
304 void W_LastWeapon(entity this)
306 Weapon wep = Weapons_from(this.cnt);
307 if (client_hasweapon(this, wep, true, false))
310 W_SwitchToOtherWeapon(this);