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 void Weapon_whereis(Weapon this, entity cl)
22 if (!autocvar_g_showweaponspawns) return;
23 for (entity it = NULL; (it = findfloat(it, weapon, this.m_id)); )
25 if (it.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
27 if (!(it.flags & FL_ITEM))
29 entity wp = WaypointSprite_Spawn(
32 NULL, it.origin + ('0 0 1' * it.maxs.z) * 1.2,
38 wp.wp_extra = this.m_id;
42 bool client_hasweapon(entity cl, Weapon wpn, float andammo, bool complain)
46 if (time < cl.hasweapon_complain_spam)
49 // ignore hook button when using other offhand equipment
50 if (cl.offhand != OFFHAND_HOOK)
51 if (wpn == WEP_HOOK && !((cl.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
55 cl.hasweapon_complain_spam = time + 0.2;
60 sprint(cl, "Invalid weapon\n");
63 if (cl.weapons & WepSet_FromWeapon(wpn))
67 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
73 WITH(entity, self, cl, f = wpn.wr_checkammo1(wpn) + wpn.wr_checkammo2(wpn));
75 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
77 if(wpn == WEP_MINE_LAYER)
78 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == cl)
85 if(IS_REAL_CLIENT(cl))
87 play2(cl, SND(UNAVAILABLE));
88 Send_WeaponComplain (cl, wpn.m_id, 0);
98 // Report Proper Weapon Status / Modified Weapon Ownership Message
99 if (weaponsInMap & WepSet_FromWeapon(wpn))
101 Send_WeaponComplain(cl, wpn.m_id, 1);
102 Weapon_whereis(wpn, cl);
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(PS(pl).m_weapon);
239 if (pl.weapons & set)
242 ww = w_getbestweapon(pl);
247 ww = w_getbestweapon(pl);
249 if (ww == WEP_Null) return;
250 W_SwitchWeapon_Force(pl, ww);
253 void W_SwitchWeapon(Weapon w)
255 if (PS(self).m_switchweapon != w)
257 if (client_hasweapon(self, w, true, true))
258 W_SwitchWeapon_Force(self, w);
260 self.selectweapon = w.m_id; // update selectweapon ANYWAY
262 else if(!forbidWeaponUse(self)) {
267 void W_CycleWeapon(string weaponorder, float dir)
270 w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
272 W_SwitchWeapon(Weapons_from(w));
275 void W_NextWeaponOnImpulse(float imp)
278 w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
280 W_SwitchWeapon(Weapons_from(w));
284 void W_NextWeapon(float list)
287 W_CycleWeapon(weaponorder_byid, -1);
289 W_CycleWeapon(self.weaponorder_byimpulse, -1);
291 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
295 void W_PreviousWeapon(float list)
298 W_CycleWeapon(weaponorder_byid, +1);
300 W_CycleWeapon(self.weaponorder_byimpulse, +1);
302 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
305 // previously used if exists and has ammo, (second) best otherwise
306 void W_LastWeapon(entity this)
308 Weapon wep = Weapons_from(this.cnt);
309 if (client_hasweapon(this, wep, true, false))
312 W_SwitchToOtherWeapon(this);