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)
85 for(e = world; (e = findfloat(e, weapon, wpn.m_id)); )
87 if(e.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
89 if(!(e.flags & FL_ITEM))
91 entity wp = WaypointSprite_Spawn(
94 world, e.origin + ('0 0 1' * e.maxs.z) * 1.2,
100 wp.wp_extra = wpn.m_id;
106 Send_WeaponComplain (cl, wpn.m_id, 2);
109 play2(cl, SND(UNAVAILABLE));
114 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
116 // We cannot tokenize in this function, as GiveItems calls this
117 // function. Thus we must use car/cdr.
118 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
119 WepSet wepset = '0 0 0';
120 switchtonext = switchtolast = 0;
121 first_valid = prev_valid = 0;
125 if(skipmissing || pl.selectweapon == 0)
126 weaponcur = PS(pl).m_switchweapon.m_id;
128 weaponcur = pl.selectweapon;
135 string rest = weaponorder;
138 weaponwant = stof(car(rest)); rest = cdr(rest);
139 wep = Weapons_from(weaponwant);
140 wepset = wep.m_wepset;
142 if(wep.impulse != imp)
145 bool have_other = false;
146 FOREACH(Weapons, it != WEP_Null, {
148 if(it.impulse == imp || imp < 0)
149 if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
153 // skip weapons we don't own that aren't normal and aren't in the map
154 if(!(pl.weapons & wepset))
155 if(!(weaponsInMap & wepset))
156 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
161 if(!skipmissing || client_hasweapon(pl, wep, true, false))
166 first_valid = weaponwant;
167 if(weaponwant == weaponcur)
176 prev_valid = weaponwant;
186 // complain (but only for one weapon on the button that has been pressed)
189 self.weaponcomplainindex += 1;
190 c = (self.weaponcomplainindex % c) + 1;
194 weaponwant = stof(car(rest)); rest = cdr(rest);
195 wep = Weapons_from(weaponwant);
196 wepset = wep.m_wepset;
198 if(wep.impulse != imp)
201 bool have_other = false;
202 FOREACH(Weapons, it != WEP_Null, {
204 if(it.impulse == imp || imp < 0)
205 if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
209 // skip weapons we don't own that aren't normal and aren't in the map
210 if(!(pl.weapons & wepset))
211 if(!(weaponsInMap & wepset))
212 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
218 client_hasweapon(pl, wep, true, true);
226 void W_SwitchWeapon_Force(entity e, Weapon wep)
228 e.cnt = PS(e).m_switchweapon.m_id;
229 PS(e).m_switchweapon = wep;
230 e.selectweapon = wep.m_id;
233 // perform weapon to attack (weaponstate and attack_finished check is here)
234 void W_SwitchToOtherWeapon(entity pl)
236 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
238 WepSet set = WepSet_FromWeapon(Weapons_from(pl.weapon));
242 ww = w_getbestweapon(pl);
246 ww = w_getbestweapon(pl);
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
306 Weapon wep = Weapons_from(self.cnt);
307 if(client_hasweapon(self, wep, true, false))
310 W_SwitchToOtherWeapon(self);