1 #include "selection.qh"
3 #include <common/constants.qh>
4 #include <common/items/item.qh>
5 #include <common/mapobjects/triggers.qh>
6 #include <common/mutators/mutator/waypoints/waypointsprites.qh>
7 #include <common/net_linked.qh>
8 #include <common/replicate.qh>
9 #include <common/state.qh>
10 #include <common/util.qh>
11 #include <common/weapons/_all.qh>
12 #include <common/wepent.qh>
13 #include <server/items/items.qh>
14 #include <server/items/spawning.qh>
15 #include <server/weapons/weaponsystem.qh>
17 // switch between weapons
18 void Send_WeaponComplain(entity e, float wpn, float type)
21 WriteHeader(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
22 WriteByte(MSG_ONE, wpn);
23 WriteByte(MSG_ONE, type);
26 void Weapon_whereis(Weapon this, entity cl)
28 if (!autocvar_g_showweaponspawns) return;
29 IL_EACH(g_items, it.weapon == this.m_id && (!it.team || (it.ItemStatus & ITS_AVAILABLE)),
31 if (Item_IsLoot(it) && (autocvar_g_showweaponspawns < 2))
35 entity wp = WaypointSprite_Spawn(
38 NULL, it.origin + ('0 0 1' * it.maxs.z) * 1.2,
44 wp.wp_extra = this.m_id;
48 bool client_hasweapon(entity this, Weapon wpn, .entity weaponentity, float andammo, bool complain)
52 if (time < CS(this).hasweapon_complain_spam)
55 // ignore hook button when using other offhand equipment
56 if (this.offhand != OFFHAND_HOOK)
57 if (wpn == WEP_HOOK && !((STAT(WEAPONS, this) | weaponsInMap) & WepSet_FromWeapon(wpn)))
61 CS(this).hasweapon_complain_spam = time + 0.2;
66 sprint(this, "Invalid weapon\n");
69 if (autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0 && !(wpn.spawnflags & WEP_FLAG_DUALWIELD) && !(PS(this).dual_weapons & wpn.m_wepset))
70 return false; // no complaints needed
71 if (STAT(WEAPONS, this) & WepSet_FromWeapon(wpn))
75 if(this.items & IT_UNLIMITED_AMMO)
81 f = wpn.wr_checkammo1(wpn, this, weaponentity) + wpn.wr_checkammo2(wpn, this, weaponentity);
83 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
84 if(wpn == WEP_MINE_LAYER)
85 IL_EACH(g_mines, it.owner == this && it.weaponentity_fld == weaponentity,
88 break; // no need to continue
94 if(IS_REAL_CLIENT(this))
96 play2(this, SND(UNAVAILABLE));
97 Send_WeaponComplain (this, wpn.m_id, 0);
107 // Report Proper Weapon Status / Modified Weapon Ownership Message
108 if (weaponsInMap & WepSet_FromWeapon(wpn))
110 Send_WeaponComplain(this, wpn.m_id, 1);
111 if(autocvar_g_showweaponspawns < 3)
112 Weapon_whereis(wpn, this);
115 FOREACH(Weapons, it.impulse == wpn.impulse,
117 Weapon_whereis(it, this);
123 Send_WeaponComplain (this, wpn.m_id, 2);
126 play2(this, SND(UNAVAILABLE));
131 float W_GetCycleWeapon(entity this, string weaponorder, float dir, float imp, float complain, float skipmissing, .entity weaponentity)
133 // We cannot tokenize in this function, as GiveItems calls this
134 // function. Thus we must use car/cdr.
135 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
136 WepSet wepset = '0 0 0';
137 switchtonext = switchtolast = 0;
138 first_valid = prev_valid = 0;
142 if(skipmissing || this.(weaponentity).selectweapon == 0)
143 weaponcur = this.(weaponentity).m_switchweapon.m_id;
145 weaponcur = this.(weaponentity).selectweapon;
152 string rest = weaponorder;
155 weaponwant = stof(car(rest)); rest = cdr(rest);
156 wep = REGISTRY_GET(Weapons, weaponwant);
157 wepset = wep.m_wepset;
159 if(wep.impulse != imp)
162 bool have_other = false;
163 FOREACH(Weapons, it != WEP_Null, {
165 if(it.impulse == imp || imp < 0)
166 if((STAT(WEAPONS, this) & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
170 // skip weapons we don't own that aren't normal and aren't in the map
171 if(!(STAT(WEAPONS, this) & wepset))
172 if(!(weaponsInMap & wepset))
173 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
178 if(!skipmissing || client_hasweapon(this, wep, weaponentity, true, false))
183 first_valid = weaponwant;
184 if(weaponwant == weaponcur)
193 prev_valid = weaponwant;
203 // complain (but only for one weapon on the button that has been pressed)
206 this.weaponcomplainindex += 1;
207 c = (this.weaponcomplainindex % c) + 1;
211 weaponwant = stof(car(rest)); rest = cdr(rest);
212 wep = REGISTRY_GET(Weapons, weaponwant);
213 wepset = wep.m_wepset;
215 if(wep.impulse != imp)
218 bool have_other = false;
219 FOREACH(Weapons, it != WEP_Null, {
221 if(it.impulse == imp || imp < 0)
222 if((STAT(WEAPONS, this) & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
226 // skip weapons we don't own that aren't normal and aren't in the map
227 if(!(STAT(WEAPONS, this) & wepset))
228 if(!(weaponsInMap & wepset))
229 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
235 client_hasweapon(this, wep, weaponentity, true, true);
243 void W_SwitchWeapon_Force(Player this, Weapon wep, .entity weaponentity)
246 this.(weaponentity).cnt = this.(weaponentity).m_switchweapon.m_id;
247 this.(weaponentity).m_switchweapon = wep;
248 this.(weaponentity).selectweapon = wep.m_id;
251 // perform weapon to attack (weaponstate and attack_finished check is here)
252 void W_SwitchToOtherWeapon(entity this, .entity weaponentity)
254 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
256 WepSet set = WepSet_FromWeapon(this.(weaponentity).m_weapon);
257 if (STAT(WEAPONS, this) & set)
259 STAT(WEAPONS, this) &= ~set;
260 ww = w_getbestweapon(this, weaponentity);
261 STAT(WEAPONS, this) |= set;
265 ww = w_getbestweapon(this, weaponentity);
267 if (ww == WEP_Null) return;
268 W_SwitchWeapon_Force(this, ww, weaponentity);
271 bool W_SwitchWeapon(entity this, Weapon w, .entity weaponentity)
273 if(this.(weaponentity).m_switchweapon != w)
275 if(client_hasweapon(this, w, weaponentity, true, true))
277 W_SwitchWeapon_Force(this, w, weaponentity);
282 this.(weaponentity).selectweapon = w.m_id; // update selectweapon anyway
286 else if(!weaponLocked(this) && CS(this).cvar_cl_weapon_switch_reload)
289 w.wr_reload(w, actor, weaponentity);
292 return true; // player already has the weapon out or needs to reload
295 void W_SwitchWeapon_TryOthers(entity this, Weapon w, .entity weaponentity)
297 if(!W_SwitchWeapon(this, w, weaponentity) && CS(this).cvar_cl_weapon_switch_fallback_to_impulse)
298 W_NextWeaponOnImpulse(this, w.impulse, weaponentity);
301 void W_CycleWeapon(entity this, string weaponorder, float dir, .entity weaponentity)
304 w = W_GetCycleWeapon(this, weaponorder, dir, -1, 1, true, weaponentity);
306 W_SwitchWeapon(this, REGISTRY_GET(Weapons, w), weaponentity);
309 void W_NextWeaponOnImpulse(entity this, float imp, .entity weaponentity)
312 w = W_GetCycleWeapon(this, CS(this).cvar_cl_weaponpriority, +1, imp, 1, (CS(this).cvar_cl_weaponimpulsemode == 0), weaponentity);
314 W_SwitchWeapon(this, REGISTRY_GET(Weapons, w), weaponentity);
318 void W_NextWeapon(entity this, int list, .entity weaponentity)
321 W_CycleWeapon(this, weaponorder_byid, -1, weaponentity);
323 W_CycleWeapon(this, CS(this).weaponorder_byimpulse, -1, weaponentity);
325 W_CycleWeapon(this, CS(this).cvar_cl_weaponpriority, -1, weaponentity);
329 void W_PreviousWeapon(entity this, float list, .entity weaponentity)
332 W_CycleWeapon(this, weaponorder_byid, +1, weaponentity);
334 W_CycleWeapon(this, CS(this).weaponorder_byimpulse, +1, weaponentity);
336 W_CycleWeapon(this, CS(this).cvar_cl_weaponpriority, +1, weaponentity);
339 // previously used if exists and has ammo, (second) best otherwise
340 void W_LastWeapon(entity this, .entity weaponentity)
342 Weapon wep = REGISTRY_GET(Weapons, this.(weaponentity).cnt);
343 if (client_hasweapon(this, wep, weaponentity, true, false))
344 W_SwitchWeapon(this, wep, weaponentity);
346 W_SwitchToOtherWeapon(this, weaponentity);