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>
13 // switch between weapons
14 void Send_WeaponComplain(entity e, float wpn, float type)
17 WriteHeader(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
18 WriteByte(MSG_ONE, wpn);
19 WriteByte(MSG_ONE, type);
22 void Weapon_whereis(Weapon this, entity cl)
24 if (!autocvar_g_showweaponspawns) return;
25 IL_EACH(g_items, it.weapon == this.m_id && (!it.team || (it.ItemStatus & ITS_AVAILABLE)),
27 if (it.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
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 this, Weapon wpn, float andammo, bool complain)
46 if (time < this.hasweapon_complain_spam)
49 // ignore hook button when using other offhand equipment
50 if (this.offhand != OFFHAND_HOOK)
51 if (wpn == WEP_HOOK && !((this.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
55 this.hasweapon_complain_spam = time + 0.2;
60 sprint(this, "Invalid weapon\n");
63 if (this.weapons & WepSet_FromWeapon(wpn))
67 if(this.items & IT_UNLIMITED_WEAPON_AMMO)
73 f = wpn.wr_checkammo1(wpn, this) + wpn.wr_checkammo2(wpn, this);
75 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
76 if(wpn == WEP_MINE_LAYER)
77 IL_EACH(g_mines, it.owner == this,
80 break; // no need to continue
86 if(IS_REAL_CLIENT(this))
88 play2(this, SND(UNAVAILABLE));
89 Send_WeaponComplain (this, wpn.m_id, 0);
99 // Report Proper Weapon Status / Modified Weapon Ownership Message
100 if (weaponsInMap & WepSet_FromWeapon(wpn))
102 Send_WeaponComplain(this, wpn.m_id, 1);
103 if(autocvar_g_showweaponspawns < 3)
104 Weapon_whereis(wpn, this);
107 FOREACH(Weapons, it.impulse == wpn.impulse,
109 Weapon_whereis(it, this);
115 Send_WeaponComplain (this, wpn.m_id, 2);
118 play2(this, SND(UNAVAILABLE));
123 float W_GetCycleWeapon(entity this, string weaponorder, float dir, float imp, float complain, float skipmissing)
125 // We cannot tokenize in this function, as GiveItems calls this
126 // function. Thus we must use car/cdr.
127 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
128 WepSet wepset = '0 0 0';
129 switchtonext = switchtolast = 0;
130 first_valid = prev_valid = 0;
134 if(skipmissing || this.selectweapon == 0)
135 weaponcur = PS(this).m_switchweapon.m_id;
137 weaponcur = this.selectweapon;
144 string rest = weaponorder;
147 weaponwant = stof(car(rest)); rest = cdr(rest);
148 wep = Weapons_from(weaponwant);
149 wepset = wep.m_wepset;
151 if(wep.impulse != imp)
154 bool have_other = false;
155 FOREACH(Weapons, it != WEP_Null, {
157 if(it.impulse == imp || imp < 0)
158 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
162 // skip weapons we don't own that aren't normal and aren't in the map
163 if(!(this.weapons & wepset))
164 if(!(weaponsInMap & wepset))
165 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
170 if(!skipmissing || client_hasweapon(this, wep, true, false))
175 first_valid = weaponwant;
176 if(weaponwant == weaponcur)
185 prev_valid = weaponwant;
195 // complain (but only for one weapon on the button that has been pressed)
198 this.weaponcomplainindex += 1;
199 c = (this.weaponcomplainindex % c) + 1;
203 weaponwant = stof(car(rest)); rest = cdr(rest);
204 wep = Weapons_from(weaponwant);
205 wepset = wep.m_wepset;
207 if(wep.impulse != imp)
210 bool have_other = false;
211 FOREACH(Weapons, it != WEP_Null, {
213 if(it.impulse == imp || imp < 0)
214 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
218 // skip weapons we don't own that aren't normal and aren't in the map
219 if(!(this.weapons & wepset))
220 if(!(weaponsInMap & wepset))
221 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
227 client_hasweapon(this, wep, true, true);
235 void W_SwitchWeapon_Force(Player this, Weapon wep)
237 TC(Player, this); TC(Weapon, wep);
238 this.cnt = PS(this).m_switchweapon.m_id;
239 PS(this).m_switchweapon = wep;
240 this.selectweapon = wep.m_id;
243 // perform weapon to attack (weaponstate and attack_finished check is here)
244 void W_SwitchToOtherWeapon(entity this)
246 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
248 WepSet set = WepSet_FromWeapon(PS(this).m_weapon);
249 if (this.weapons & set)
251 this.weapons &= ~set;
252 ww = w_getbestweapon(this);
257 ww = w_getbestweapon(this);
259 if (ww == WEP_Null) return;
260 W_SwitchWeapon_Force(this, ww);
263 void W_SwitchWeapon(entity this, Weapon w)
265 if (PS(this).m_switchweapon != w)
267 if (client_hasweapon(this, w, true, true))
268 W_SwitchWeapon_Force(this, w);
270 this.selectweapon = w.m_id; // update selectweapon ANYWAY
272 else if(!forbidWeaponUse(this)) {
274 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
276 .entity weaponentity = weaponentities[slot];
277 w.wr_reload(w, actor, weaponentity);
282 void W_CycleWeapon(entity this, string weaponorder, float dir)
285 w = W_GetCycleWeapon(this, weaponorder, dir, -1, 1, true);
287 W_SwitchWeapon(this, Weapons_from(w));
290 void W_NextWeaponOnImpulse(entity this, float imp)
293 w = W_GetCycleWeapon(this, this.cvar_cl_weaponpriority, +1, imp, 1, (this.cvar_cl_weaponimpulsemode == 0));
295 W_SwitchWeapon(this, Weapons_from(w));
299 void W_NextWeapon(entity this, int list)
302 W_CycleWeapon(this, weaponorder_byid, -1);
304 W_CycleWeapon(this, this.weaponorder_byimpulse, -1);
306 W_CycleWeapon(this, this.cvar_cl_weaponpriority, -1);
310 void W_PreviousWeapon(entity this, float list)
313 W_CycleWeapon(this, weaponorder_byid, +1);
315 W_CycleWeapon(this, this.weaponorder_byimpulse, +1);
317 W_CycleWeapon(this, this.cvar_cl_weaponpriority, +1);
320 // previously used if exists and has ammo, (second) best otherwise
321 void W_LastWeapon(entity this)
323 Weapon wep = Weapons_from(this.cnt);
324 if (client_hasweapon(this, wep, true, false))
325 W_SwitchWeapon(this, wep);
327 W_SwitchToOtherWeapon(this);