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,
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 < 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 this.hasweapon_complain_spam = time + 0.2;
61 sprint(this, "Invalid weapon\n");
64 if (this.weapons & WepSet_FromWeapon(wpn))
68 if(this.items & IT_UNLIMITED_WEAPON_AMMO)
74 f = wpn.wr_checkammo1(wpn, this, weaponentity) + wpn.wr_checkammo2(wpn, this, weaponentity);
76 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
77 if(wpn == WEP_MINE_LAYER)
78 IL_EACH(g_mines, it.owner == this && it.weaponentity_fld == weaponentity,
81 break; // no need to continue
87 if(IS_REAL_CLIENT(this))
89 play2(this, SND(UNAVAILABLE));
90 Send_WeaponComplain (this, wpn.m_id, 0);
100 // Report Proper Weapon Status / Modified Weapon Ownership Message
101 if (weaponsInMap & WepSet_FromWeapon(wpn))
103 Send_WeaponComplain(this, wpn.m_id, 1);
104 if(autocvar_g_showweaponspawns < 3)
105 Weapon_whereis(wpn, this);
108 FOREACH(Weapons, it.impulse == wpn.impulse,
110 Weapon_whereis(it, this);
116 Send_WeaponComplain (this, wpn.m_id, 2);
119 play2(this, SND(UNAVAILABLE));
124 float W_GetCycleWeapon(entity this, string weaponorder, float dir, float imp, float complain, float skipmissing, .entity weaponentity)
126 // We cannot tokenize in this function, as GiveItems calls this
127 // function. Thus we must use car/cdr.
128 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
129 WepSet wepset = '0 0 0';
130 switchtonext = switchtolast = 0;
131 first_valid = prev_valid = 0;
135 if(skipmissing || this.(weaponentity).selectweapon == 0)
136 weaponcur = this.(weaponentity).m_switchweapon.m_id;
138 weaponcur = this.(weaponentity).selectweapon;
145 string rest = weaponorder;
148 weaponwant = stof(car(rest)); rest = cdr(rest);
149 wep = Weapons_from(weaponwant);
150 wepset = wep.m_wepset;
152 if(wep.impulse != imp)
155 bool have_other = false;
156 FOREACH(Weapons, it != WEP_Null, {
158 if(it.impulse == imp || imp < 0)
159 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
163 // skip weapons we don't own that aren't normal and aren't in the map
164 if(!(this.weapons & wepset))
165 if(!(weaponsInMap & wepset))
166 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
171 if(!skipmissing || client_hasweapon(this, wep, weaponentity, true, false))
176 first_valid = weaponwant;
177 if(weaponwant == weaponcur)
186 prev_valid = weaponwant;
196 // complain (but only for one weapon on the button that has been pressed)
199 this.weaponcomplainindex += 1;
200 c = (this.weaponcomplainindex % c) + 1;
204 weaponwant = stof(car(rest)); rest = cdr(rest);
205 wep = Weapons_from(weaponwant);
206 wepset = wep.m_wepset;
208 if(wep.impulse != imp)
211 bool have_other = false;
212 FOREACH(Weapons, it != WEP_Null, {
214 if(it.impulse == imp || imp < 0)
215 if((this.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
219 // skip weapons we don't own that aren't normal and aren't in the map
220 if(!(this.weapons & wepset))
221 if(!(weaponsInMap & wepset))
222 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
228 client_hasweapon(this, wep, weaponentity, true, true);
236 void W_SwitchWeapon_Force(Player this, Weapon wep, .entity weaponentity)
239 this.(weaponentity).cnt = this.(weaponentity).m_switchweapon.m_id;
240 this.(weaponentity).m_switchweapon = wep;
241 this.(weaponentity).selectweapon = wep.m_id;
244 // perform weapon to attack (weaponstate and attack_finished check is here)
245 void W_SwitchToOtherWeapon(entity this, .entity weaponentity)
247 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
249 WepSet set = WepSet_FromWeapon(this.(weaponentity).m_weapon);
250 if (this.weapons & set)
252 this.weapons &= ~set;
253 ww = w_getbestweapon(this, weaponentity);
258 ww = w_getbestweapon(this, weaponentity);
260 if (ww == WEP_Null) return;
261 W_SwitchWeapon_Force(this, ww, weaponentity);
264 void W_SwitchWeapon(entity this, Weapon w, .entity weaponentity)
266 if(this.(weaponentity).m_switchweapon != w)
268 if(client_hasweapon(this, w, weaponentity, true, true))
269 W_SwitchWeapon_Force(this, w, weaponentity);
271 this.(weaponentity).selectweapon = w.m_id; // update selectweapon anyway
273 else if(!forbidWeaponUse(this))
276 w.wr_reload(w, actor, weaponentity);
280 void W_CycleWeapon(entity this, string weaponorder, float dir, .entity weaponentity)
283 w = W_GetCycleWeapon(this, weaponorder, dir, -1, 1, true, weaponentity);
285 W_SwitchWeapon(this, Weapons_from(w), weaponentity);
288 void W_NextWeaponOnImpulse(entity this, float imp, .entity weaponentity)
291 w = W_GetCycleWeapon(this, this.cvar_cl_weaponpriority, +1, imp, 1, (this.cvar_cl_weaponimpulsemode == 0), weaponentity);
293 W_SwitchWeapon(this, Weapons_from(w), weaponentity);
297 void W_NextWeapon(entity this, int list, .entity weaponentity)
300 W_CycleWeapon(this, weaponorder_byid, -1, weaponentity);
302 W_CycleWeapon(this, this.weaponorder_byimpulse, -1, weaponentity);
304 W_CycleWeapon(this, this.cvar_cl_weaponpriority, -1, weaponentity);
308 void W_PreviousWeapon(entity this, float list, .entity weaponentity)
311 W_CycleWeapon(this, weaponorder_byid, +1, weaponentity);
313 W_CycleWeapon(this, this.weaponorder_byimpulse, +1, weaponentity);
315 W_CycleWeapon(this, this.cvar_cl_weaponpriority, +1, weaponentity);
318 // previously used if exists and has ammo, (second) best otherwise
319 void W_LastWeapon(entity this, .entity weaponentity)
321 Weapon wep = Weapons_from(this.(weaponentity).cnt);
322 if (client_hasweapon(this, wep, weaponentity, true, false))
323 W_SwitchWeapon(this, wep, weaponentity);
325 W_SwitchToOtherWeapon(this, weaponentity);