1 #include "selection.qh"
3 #include "weaponsystem.qh"
4 #include <common/t_items.qh>
5 #include <server/items.qh>
6 #include <common/constants.qh>
7 #include <common/net_linked.qh>
8 #include <common/util.qh>
9 #include <common/items/item.qh>
10 #include <common/weapons/_all.qh>
11 #include <common/state.qh>
12 #include <common/mutators/mutator/waypoints/waypointsprites.qh>
13 #include <common/wepent.qh>
15 // switch between weapons
16 void Send_WeaponComplain(entity e, float wpn, float type)
19 WriteHeader(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
20 WriteByte(MSG_ONE, wpn);
21 WriteByte(MSG_ONE, type);
24 void Weapon_whereis(Weapon this, entity cl)
26 if (!autocvar_g_showweaponspawns) return;
27 IL_EACH(g_items, it.weapon == this.m_id && (!it.team || (it.ItemStatus & ITS_AVAILABLE)),
29 if (Item_IsLoot(it) && (autocvar_g_showweaponspawns < 2))
33 entity wp = WaypointSprite_Spawn(
36 NULL, it.origin + ('0 0 1' * it.maxs.z) * 1.2,
42 wp.wp_extra = this.m_id;
46 bool client_hasweapon(entity this, Weapon wpn, .entity weaponentity, float andammo, bool complain)
50 if (time < CS(this).hasweapon_complain_spam)
53 // ignore hook button when using other offhand equipment
54 if (this.offhand != OFFHAND_HOOK)
55 if (wpn == WEP_HOOK && !((this.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
59 CS(this).hasweapon_complain_spam = time + 0.2;
64 sprint(this, "Invalid weapon\n");
67 if (autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0 && !(wpn.spawnflags & WEP_FLAG_DUALWIELD) && !(PS(this).dual_weapons & wpn.m_wepset))
68 return false; // no complaints needed
69 if (this.weapons & WepSet_FromWeapon(wpn))
73 if(this.items & IT_UNLIMITED_WEAPON_AMMO)
79 f = wpn.wr_checkammo1(wpn, this, weaponentity) + wpn.wr_checkammo2(wpn, this, weaponentity);
81 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
82 if(wpn == WEP_MINE_LAYER)
83 IL_EACH(g_mines, it.owner == this && it.weaponentity_fld == weaponentity,
86 break; // no need to continue
92 if(IS_REAL_CLIENT(this))
94 play2(this, SND(UNAVAILABLE));
95 Send_WeaponComplain (this, wpn.m_id, 0);
105 // Report Proper Weapon Status / Modified Weapon Ownership Message
106 if (weaponsInMap & WepSet_FromWeapon(wpn))
108 Send_WeaponComplain(this, wpn.m_id, 1);
109 if(autocvar_g_showweaponspawns < 3)
110 Weapon_whereis(wpn, this);
113 FOREACH(Weapons, it.impulse == wpn.impulse,
115 Weapon_whereis(it, this);
121 Send_WeaponComplain (this, wpn.m_id, 2);
124 play2(this, SND(UNAVAILABLE));
129 float W_GetCycleWeapon(entity this, string weaponorder, float dir, float imp, float complain, float skipmissing, .entity weaponentity)
131 // We cannot tokenize in this function, as GiveItems calls this
132 // function. Thus we must use car/cdr.
133 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
134 WepSet wepset = '0 0 0';
135 switchtonext = switchtolast = 0;
136 first_valid = prev_valid = 0;
140 if(skipmissing || this.(weaponentity).selectweapon == 0)
141 weaponcur = this.(weaponentity).m_switchweapon.m_id;
143 weaponcur = this.(weaponentity).selectweapon;
150 string rest = weaponorder;
153 weaponwant = stof(car(rest)); rest = cdr(rest);
154 wep = Weapons_from(weaponwant);
155 wepset = wep.m_wepset;
157 if(wep.impulse != imp)
160 bool have_other = false;
161 FOREACH(Weapons, it != WEP_Null, {
163 if(it.impulse == imp || imp < 0)
164 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
168 // skip weapons we don't own that aren't normal and aren't in the map
169 if(!(this.weapons & wepset))
170 if(!(weaponsInMap & wepset))
171 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
176 if(!skipmissing || client_hasweapon(this, wep, weaponentity, true, false))
181 first_valid = weaponwant;
182 if(weaponwant == weaponcur)
191 prev_valid = weaponwant;
201 // complain (but only for one weapon on the button that has been pressed)
204 this.weaponcomplainindex += 1;
205 c = (this.weaponcomplainindex % c) + 1;
209 weaponwant = stof(car(rest)); rest = cdr(rest);
210 wep = Weapons_from(weaponwant);
211 wepset = wep.m_wepset;
213 if(wep.impulse != imp)
216 bool have_other = false;
217 FOREACH(Weapons, it != WEP_Null, {
219 if(it.impulse == imp || imp < 0)
220 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
224 // skip weapons we don't own that aren't normal and aren't in the map
225 if(!(this.weapons & wepset))
226 if(!(weaponsInMap & wepset))
227 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
233 client_hasweapon(this, wep, weaponentity, true, true);
241 void W_SwitchWeapon_Force(Player this, Weapon wep, .entity weaponentity)
244 this.(weaponentity).cnt = this.(weaponentity).m_switchweapon.m_id;
245 this.(weaponentity).m_switchweapon = wep;
246 this.(weaponentity).selectweapon = wep.m_id;
249 // perform weapon to attack (weaponstate and attack_finished check is here)
250 void W_SwitchToOtherWeapon(entity this, .entity weaponentity)
252 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
254 WepSet set = WepSet_FromWeapon(this.(weaponentity).m_weapon);
255 if (this.weapons & set)
257 this.weapons &= ~set;
258 ww = w_getbestweapon(this, weaponentity);
263 ww = w_getbestweapon(this, weaponentity);
265 if (ww == WEP_Null) return;
266 W_SwitchWeapon_Force(this, ww, weaponentity);
269 void W_SwitchWeapon(entity this, Weapon w, .entity weaponentity)
271 if(this.(weaponentity).m_switchweapon != w)
273 if(client_hasweapon(this, w, weaponentity, true, true))
274 W_SwitchWeapon_Force(this, w, weaponentity);
276 this.(weaponentity).selectweapon = w.m_id; // update selectweapon anyway
278 else if(!forbidWeaponUse(this))
281 w.wr_reload(w, actor, weaponentity);
285 void W_CycleWeapon(entity this, string weaponorder, float dir, .entity weaponentity)
288 w = W_GetCycleWeapon(this, weaponorder, dir, -1, 1, true, weaponentity);
290 W_SwitchWeapon(this, Weapons_from(w), weaponentity);
293 void W_NextWeaponOnImpulse(entity this, float imp, .entity weaponentity)
296 w = W_GetCycleWeapon(this, CS(this).cvar_cl_weaponpriority, +1, imp, 1, (CS(this).cvar_cl_weaponimpulsemode == 0), weaponentity);
298 W_SwitchWeapon(this, Weapons_from(w), weaponentity);
302 void W_NextWeapon(entity this, int list, .entity weaponentity)
305 W_CycleWeapon(this, weaponorder_byid, -1, weaponentity);
307 W_CycleWeapon(this, this.weaponorder_byimpulse, -1, weaponentity);
309 W_CycleWeapon(this, CS(this).cvar_cl_weaponpriority, -1, weaponentity);
313 void W_PreviousWeapon(entity this, float list, .entity weaponentity)
316 W_CycleWeapon(this, weaponorder_byid, +1, weaponentity);
318 W_CycleWeapon(this, this.weaponorder_byimpulse, +1, weaponentity);
320 W_CycleWeapon(this, CS(this).cvar_cl_weaponpriority, +1, weaponentity);
323 // previously used if exists and has ammo, (second) best otherwise
324 void W_LastWeapon(entity this, .entity weaponentity)
326 Weapon wep = Weapons_from(this.(weaponentity).cnt);
327 if (client_hasweapon(this, wep, weaponentity, true, false))
328 W_SwitchWeapon(this, wep, weaponentity);
330 W_SwitchToOtherWeapon(this, weaponentity);