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/state.qh>
10 #include <common/mutators/mutator/waypoints/waypointsprites.qh>
12 // switch between weapons
13 void Send_WeaponComplain(entity e, float wpn, float type)
16 WriteHeader(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
17 WriteByte(MSG_ONE, wpn);
18 WriteByte(MSG_ONE, type);
21 void Weapon_whereis(Weapon this, entity cl)
23 if (!autocvar_g_showweaponspawns) return;
24 IL_EACH(g_items, it.weapon == this.m_id,
26 if (it.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
28 entity wp = WaypointSprite_Spawn(
31 NULL, it.origin + ('0 0 1' * it.maxs.z) * 1.2,
37 wp.wp_extra = this.m_id;
41 bool client_hasweapon(entity this, Weapon wpn, float andammo, bool complain)
45 if (time < this.hasweapon_complain_spam)
48 // ignore hook button when using other offhand equipment
49 if (this.offhand != OFFHAND_HOOK)
50 if (wpn == WEP_HOOK && !((this.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
54 this.hasweapon_complain_spam = time + 0.2;
59 sprint(this, "Invalid weapon\n");
62 if (this.weapons & WepSet_FromWeapon(wpn))
66 if(this.items & IT_UNLIMITED_WEAPON_AMMO)
72 f = wpn.wr_checkammo1(wpn, this) + wpn.wr_checkammo2(wpn, this);
74 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
75 if(wpn == WEP_MINE_LAYER)
76 IL_EACH(g_mines, it.owner == this,
79 break; // no need to continue
85 if(IS_REAL_CLIENT(this))
87 play2(this, SND(UNAVAILABLE));
88 Send_WeaponComplain (this, wpn.m_id, 0);
98 // Report Proper Weapon Status / Modified Weapon Ownership Message
99 if (weaponsInMap & WepSet_FromWeapon(wpn))
101 Send_WeaponComplain(this, wpn.m_id, 1);
102 if(autocvar_g_showweaponspawns < 3)
103 Weapon_whereis(wpn, this);
106 FOREACH(Weapons, it.impulse == wpn.impulse,
108 Weapon_whereis(it, this);
114 Send_WeaponComplain (this, wpn.m_id, 2);
117 play2(this, SND(UNAVAILABLE));
122 float W_GetCycleWeapon(entity this, string weaponorder, float dir, float imp, float complain, float skipmissing)
124 // We cannot tokenize in this function, as GiveItems calls this
125 // function. Thus we must use car/cdr.
126 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
127 WepSet wepset = '0 0 0';
128 switchtonext = switchtolast = 0;
129 first_valid = prev_valid = 0;
133 if(skipmissing || this.selectweapon == 0)
134 weaponcur = PS(this).m_switchweapon.m_id;
136 weaponcur = this.selectweapon;
143 string rest = weaponorder;
146 weaponwant = stof(car(rest)); rest = cdr(rest);
147 wep = Weapons_from(weaponwant);
148 wepset = wep.m_wepset;
150 if(wep.impulse != imp)
153 bool have_other = false;
154 FOREACH(Weapons, it != WEP_Null, {
156 if(it.impulse == imp || imp < 0)
157 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
161 // skip weapons we don't own that aren't normal and aren't in the map
162 if(!(this.weapons & wepset))
163 if(!(weaponsInMap & wepset))
164 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
169 if(!skipmissing || client_hasweapon(this, wep, true, false))
174 first_valid = weaponwant;
175 if(weaponwant == weaponcur)
184 prev_valid = weaponwant;
194 // complain (but only for one weapon on the button that has been pressed)
197 this.weaponcomplainindex += 1;
198 c = (this.weaponcomplainindex % c) + 1;
202 weaponwant = stof(car(rest)); rest = cdr(rest);
203 wep = Weapons_from(weaponwant);
204 wepset = wep.m_wepset;
206 if(wep.impulse != imp)
209 bool have_other = false;
210 FOREACH(Weapons, it != WEP_Null, {
212 if(it.impulse == imp || imp < 0)
213 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
217 // skip weapons we don't own that aren't normal and aren't in the map
218 if(!(this.weapons & wepset))
219 if(!(weaponsInMap & wepset))
220 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
226 client_hasweapon(this, wep, true, true);
234 void W_SwitchWeapon_Force(Player this, Weapon wep)
236 TC(Player, this); TC(Weapon, wep);
237 this.cnt = PS(this).m_switchweapon.m_id;
238 PS(this).m_switchweapon = wep;
239 this.selectweapon = wep.m_id;
242 // perform weapon to attack (weaponstate and attack_finished check is here)
243 void W_SwitchToOtherWeapon(entity this)
245 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
247 WepSet set = WepSet_FromWeapon(PS(this).m_weapon);
248 if (this.weapons & set)
250 this.weapons &= ~set;
251 ww = w_getbestweapon(this);
256 ww = w_getbestweapon(this);
258 if (ww == WEP_Null) return;
259 W_SwitchWeapon_Force(this, ww);
262 void W_SwitchWeapon(entity this, Weapon w)
264 if (PS(this).m_switchweapon != w)
266 if (client_hasweapon(this, w, true, true))
267 W_SwitchWeapon_Force(this, w);
269 this.selectweapon = w.m_id; // update selectweapon ANYWAY
271 else if(!forbidWeaponUse(this)) {
273 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
275 .entity weaponentity = weaponentities[slot];
276 w.wr_reload(w, actor, weaponentity);
281 void W_CycleWeapon(entity this, string weaponorder, float dir)
284 w = W_GetCycleWeapon(this, weaponorder, dir, -1, 1, true);
286 W_SwitchWeapon(this, Weapons_from(w));
289 void W_NextWeaponOnImpulse(entity this, float imp)
292 w = W_GetCycleWeapon(this, this.cvar_cl_weaponpriority, +1, imp, 1, (this.cvar_cl_weaponimpulsemode == 0));
294 W_SwitchWeapon(this, Weapons_from(w));
298 void W_NextWeapon(entity this, int list)
301 W_CycleWeapon(this, weaponorder_byid, -1);
303 W_CycleWeapon(this, this.weaponorder_byimpulse, -1);
305 W_CycleWeapon(this, this.cvar_cl_weaponpriority, -1);
309 void W_PreviousWeapon(entity this, float list)
312 W_CycleWeapon(this, weaponorder_byid, +1);
314 W_CycleWeapon(this, this.weaponorder_byimpulse, +1);
316 W_CycleWeapon(this, this.cvar_cl_weaponpriority, +1);
319 // previously used if exists and has ammo, (second) best otherwise
320 void W_LastWeapon(entity this)
322 Weapon wep = Weapons_from(this.cnt);
323 if (client_hasweapon(this, wep, true, false))
324 W_SwitchWeapon(this, wep);
326 W_SwitchToOtherWeapon(this);