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/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
76 if(wpn == WEP_MINE_LAYER)
77 FOREACH_ENTITY_CLASS("mine", it.owner == cl,
80 break; // no need to continue
86 if(IS_REAL_CLIENT(cl))
88 play2(cl, SND(UNAVAILABLE));
89 Send_WeaponComplain (cl, wpn.m_id, 0);
99 // Report Proper Weapon Status / Modified Weapon Ownership Message
100 if (weaponsInMap & WepSet_FromWeapon(wpn))
102 Send_WeaponComplain(cl, wpn.m_id, 1);
103 Weapon_whereis(wpn, cl);
107 Send_WeaponComplain (cl, wpn.m_id, 2);
110 play2(cl, SND(UNAVAILABLE));
115 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
117 // We cannot tokenize in this function, as GiveItems calls this
118 // function. Thus we must use car/cdr.
119 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
120 WepSet wepset = '0 0 0';
121 switchtonext = switchtolast = 0;
122 first_valid = prev_valid = 0;
126 if(skipmissing || pl.selectweapon == 0)
127 weaponcur = PS(pl).m_switchweapon.m_id;
129 weaponcur = pl.selectweapon;
136 string rest = weaponorder;
139 weaponwant = stof(car(rest)); rest = cdr(rest);
140 wep = Weapons_from(weaponwant);
141 wepset = wep.m_wepset;
143 if(wep.impulse != imp)
146 bool have_other = false;
147 FOREACH(Weapons, it != WEP_Null, {
149 if(it.impulse == imp || imp < 0)
150 if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
154 // skip weapons we don't own that aren't normal and aren't in the map
155 if(!(pl.weapons & wepset))
156 if(!(weaponsInMap & wepset))
157 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
162 if(!skipmissing || client_hasweapon(pl, wep, true, false))
167 first_valid = weaponwant;
168 if(weaponwant == weaponcur)
177 prev_valid = weaponwant;
187 // complain (but only for one weapon on the button that has been pressed)
190 self.weaponcomplainindex += 1;
191 c = (self.weaponcomplainindex % c) + 1;
195 weaponwant = stof(car(rest)); rest = cdr(rest);
196 wep = Weapons_from(weaponwant);
197 wepset = wep.m_wepset;
199 if(wep.impulse != imp)
202 bool have_other = false;
203 FOREACH(Weapons, it != WEP_Null, {
205 if(it.impulse == imp || imp < 0)
206 if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
210 // skip weapons we don't own that aren't normal and aren't in the map
211 if(!(pl.weapons & wepset))
212 if(!(weaponsInMap & wepset))
213 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
219 client_hasweapon(pl, wep, true, true);
227 void W_SwitchWeapon_Force(entity e, Weapon wep)
229 e.cnt = PS(e).m_switchweapon.m_id;
230 PS(e).m_switchweapon = wep;
231 e.selectweapon = wep.m_id;
234 // perform weapon to attack (weaponstate and attack_finished check is here)
235 void W_SwitchToOtherWeapon(entity pl)
237 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
239 WepSet set = WepSet_FromWeapon(PS(pl).m_weapon);
240 if (pl.weapons & set)
243 ww = w_getbestweapon(pl);
248 ww = w_getbestweapon(pl);
250 if (ww == WEP_Null) return;
251 W_SwitchWeapon_Force(pl, ww);
254 void W_SwitchWeapon(Weapon w)
256 if (PS(self).m_switchweapon != w)
258 if (client_hasweapon(self, w, true, true))
259 W_SwitchWeapon_Force(self, w);
261 self.selectweapon = w.m_id; // update selectweapon ANYWAY
263 else if(!forbidWeaponUse(self)) {
265 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
266 w.wr_reload(w, actor, weaponentity);
270 void W_CycleWeapon(string weaponorder, float dir)
273 w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
275 W_SwitchWeapon(Weapons_from(w));
278 void W_NextWeaponOnImpulse(float imp)
281 w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
283 W_SwitchWeapon(Weapons_from(w));
287 void W_NextWeapon(float list)
290 W_CycleWeapon(weaponorder_byid, -1);
292 W_CycleWeapon(self.weaponorder_byimpulse, -1);
294 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
298 void W_PreviousWeapon(float list)
301 W_CycleWeapon(weaponorder_byid, +1);
303 W_CycleWeapon(self.weaponorder_byimpulse, +1);
305 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
308 // previously used if exists and has ammo, (second) best otherwise
309 void W_LastWeapon(entity this)
311 Weapon wep = Weapons_from(this.cnt);
312 if (client_hasweapon(this, wep, true, false))
315 W_SwitchToOtherWeapon(this);