1 #include "selection.qh"
3 #include "weaponsystem.qh"
4 #include <common/t_items.qh>
5 #include <common/constants.qh>
6 #include <common/net_linked.qh>
7 #include <common/util.qh>
8 #include <common/items/item.qh>
9 #include <common/weapons/_all.qh>
10 #include <common/state.qh>
11 #include <common/mutators/mutator/waypoints/waypointsprites.qh>
12 #include <common/wepent.qh>
14 // switch between weapons
15 void Send_WeaponComplain(entity e, float wpn, float type)
18 WriteHeader(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
19 WriteByte(MSG_ONE, wpn);
20 WriteByte(MSG_ONE, type);
23 void Weapon_whereis(Weapon this, entity cl)
25 if (!autocvar_g_showweaponspawns) return;
26 IL_EACH(g_items, it.weapon == this.m_id && (!it.team || (it.ItemStatus & ITS_AVAILABLE)),
28 if (it.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
30 entity wp = WaypointSprite_Spawn(
33 NULL, it.origin + ('0 0 1' * it.maxs.z) * 1.2,
39 wp.wp_extra = this.m_id;
43 bool client_hasweapon(entity this, Weapon wpn, .entity weaponentity, float andammo, bool complain)
47 if (time < CS(this).hasweapon_complain_spam)
50 // ignore hook button when using other offhand equipment
51 if (this.offhand != OFFHAND_HOOK)
52 if (wpn == WEP_HOOK && !((this.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
56 CS(this).hasweapon_complain_spam = time + 0.2;
61 sprint(this, "Invalid weapon\n");
64 if (autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0 && !(wpn.spawnflags & WEP_FLAG_DUALWIELD) && !(PS(this).dual_weapons & wpn.m_wepset))
65 return false; // no complaints needed
66 if (this.weapons & WepSet_FromWeapon(wpn))
70 if(this.items & IT_UNLIMITED_WEAPON_AMMO)
76 f = wpn.wr_checkammo1(wpn, this, weaponentity) + wpn.wr_checkammo2(wpn, this, weaponentity);
78 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
79 if(wpn == WEP_MINE_LAYER)
80 IL_EACH(g_mines, it.owner == this && it.weaponentity_fld == weaponentity,
83 break; // no need to continue
89 if(IS_REAL_CLIENT(this))
91 play2(this, SND(UNAVAILABLE));
92 Send_WeaponComplain (this, wpn.m_id, 0);
102 // Report Proper Weapon Status / Modified Weapon Ownership Message
103 if (weaponsInMap & WepSet_FromWeapon(wpn))
105 Send_WeaponComplain(this, wpn.m_id, 1);
106 if(autocvar_g_showweaponspawns < 3)
107 Weapon_whereis(wpn, this);
110 FOREACH(Weapons, it.impulse == wpn.impulse,
112 Weapon_whereis(it, this);
118 Send_WeaponComplain (this, wpn.m_id, 2);
121 play2(this, SND(UNAVAILABLE));
126 float W_GetCycleWeapon(entity this, string weaponorder, float dir, float imp, float complain, float skipmissing, .entity weaponentity)
128 // We cannot tokenize in this function, as GiveItems calls this
129 // function. Thus we must use car/cdr.
130 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
131 WepSet wepset = '0 0 0';
132 switchtonext = switchtolast = 0;
133 first_valid = prev_valid = 0;
137 if(skipmissing || this.(weaponentity).selectweapon == 0)
138 weaponcur = this.(weaponentity).m_switchweapon.m_id;
140 weaponcur = this.(weaponentity).selectweapon;
147 string rest = weaponorder;
150 weaponwant = stof(car(rest)); rest = cdr(rest);
151 wep = Weapons_from(weaponwant);
152 wepset = wep.m_wepset;
154 if(wep.impulse != imp)
157 bool have_other = false;
158 FOREACH(Weapons, it != WEP_Null, {
160 if(it.impulse == imp || imp < 0)
161 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
165 // skip weapons we don't own that aren't normal and aren't in the map
166 if(!(this.weapons & wepset))
167 if(!(weaponsInMap & wepset))
168 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
173 if(!skipmissing || client_hasweapon(this, wep, weaponentity, true, false))
178 first_valid = weaponwant;
179 if(weaponwant == weaponcur)
188 prev_valid = weaponwant;
198 // complain (but only for one weapon on the button that has been pressed)
201 this.weaponcomplainindex += 1;
202 c = (this.weaponcomplainindex % c) + 1;
206 weaponwant = stof(car(rest)); rest = cdr(rest);
207 wep = Weapons_from(weaponwant);
208 wepset = wep.m_wepset;
210 if(wep.impulse != imp)
213 bool have_other = false;
214 FOREACH(Weapons, it != WEP_Null, {
216 if(it.impulse == imp || imp < 0)
217 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
221 // skip weapons we don't own that aren't normal and aren't in the map
222 if(!(this.weapons & wepset))
223 if(!(weaponsInMap & wepset))
224 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
230 client_hasweapon(this, wep, weaponentity, true, true);
238 void W_SwitchWeapon_Force(Player this, Weapon wep, .entity weaponentity)
241 this.(weaponentity).cnt = this.(weaponentity).m_switchweapon.m_id;
242 this.(weaponentity).m_switchweapon = wep;
243 this.(weaponentity).selectweapon = wep.m_id;
246 // perform weapon to attack (weaponstate and attack_finished check is here)
247 void W_SwitchToOtherWeapon(entity this, .entity weaponentity)
249 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
251 WepSet set = WepSet_FromWeapon(this.(weaponentity).m_weapon);
252 if (this.weapons & set)
254 this.weapons &= ~set;
255 ww = w_getbestweapon(this, weaponentity);
260 ww = w_getbestweapon(this, weaponentity);
262 if (ww == WEP_Null) return;
263 W_SwitchWeapon_Force(this, ww, weaponentity);
266 void W_SwitchWeapon(entity this, Weapon w, .entity weaponentity)
268 if(this.(weaponentity).m_switchweapon != w)
270 if(client_hasweapon(this, w, weaponentity, true, true))
271 W_SwitchWeapon_Force(this, w, weaponentity);
273 this.(weaponentity).selectweapon = w.m_id; // update selectweapon anyway
275 else if(!forbidWeaponUse(this))
278 w.wr_reload(w, actor, weaponentity);
282 void W_CycleWeapon(entity this, string weaponorder, float dir, .entity weaponentity)
285 w = W_GetCycleWeapon(this, weaponorder, dir, -1, 1, true, weaponentity);
287 W_SwitchWeapon(this, Weapons_from(w), weaponentity);
290 void W_NextWeaponOnImpulse(entity this, float imp, .entity weaponentity)
293 w = W_GetCycleWeapon(this, this.cvar_cl_weaponpriority, +1, imp, 1, (this.cvar_cl_weaponimpulsemode == 0), weaponentity);
295 W_SwitchWeapon(this, Weapons_from(w), weaponentity);
299 void W_NextWeapon(entity this, int list, .entity weaponentity)
302 W_CycleWeapon(this, weaponorder_byid, -1, weaponentity);
304 W_CycleWeapon(this, this.weaponorder_byimpulse, -1, weaponentity);
306 W_CycleWeapon(this, this.cvar_cl_weaponpriority, -1, weaponentity);
310 void W_PreviousWeapon(entity this, float list, .entity weaponentity)
313 W_CycleWeapon(this, weaponorder_byid, +1, weaponentity);
315 W_CycleWeapon(this, this.weaponorder_byimpulse, +1, weaponentity);
317 W_CycleWeapon(this, this.cvar_cl_weaponpriority, +1, weaponentity);
320 // previously used if exists and has ammo, (second) best otherwise
321 void W_LastWeapon(entity this, .entity weaponentity)
323 Weapon wep = Weapons_from(this.(weaponentity).cnt);
324 if (client_hasweapon(this, wep, weaponentity, true, false))
325 W_SwitchWeapon(this, wep, weaponentity);
327 W_SwitchToOtherWeapon(this, weaponentity);