1 #include "selection.qh"
3 #include "weaponsystem.qh"
4 #include <server/items/items.qh>
5 #include <server/items/spawning.qh>
6 #include <common/constants.qh>
7 #include <common/net_linked.qh>
8 #include <common/util.qh>
9 #include <common/items/item.qh>
10 #include <common/weapons/_all.qh>
11 #include <common/state.qh>
12 #include <common/mapobjects/triggers.qh>
13 #include <common/mutators/mutator/waypoints/waypointsprites.qh>
14 #include <common/wepent.qh>
16 // switch between weapons
17 void Send_WeaponComplain(entity e, float wpn, float type)
20 WriteHeader(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
21 WriteByte(MSG_ONE, wpn);
22 WriteByte(MSG_ONE, type);
25 void Weapon_whereis(Weapon this, entity cl)
27 if (!autocvar_g_showweaponspawns) return;
28 IL_EACH(g_items, it.weapon == this.m_id && (!it.team || (it.ItemStatus & ITS_AVAILABLE)),
30 if (Item_IsLoot(it) && (autocvar_g_showweaponspawns < 2))
34 entity wp = WaypointSprite_Spawn(
37 NULL, it.origin + ('0 0 1' * it.maxs.z) * 1.2,
43 wp.wp_extra = this.m_id;
47 bool client_hasweapon(entity this, Weapon wpn, .entity weaponentity, float andammo, bool complain)
51 if (time < CS(this).hasweapon_complain_spam)
54 // ignore hook button when using other offhand equipment
55 if (this.offhand != OFFHAND_HOOK)
56 if (wpn == WEP_HOOK && !((STAT(WEAPONS, this) | weaponsInMap) & WepSet_FromWeapon(wpn)))
60 CS(this).hasweapon_complain_spam = time + 0.2;
65 sprint(this, "Invalid weapon\n");
68 if (autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0 && !(wpn.spawnflags & WEP_FLAG_DUALWIELD) && !(PS(this).dual_weapons & wpn.m_wepset))
69 return false; // no complaints needed
70 if (STAT(WEAPONS, this) & WepSet_FromWeapon(wpn))
74 if(this.items & IT_UNLIMITED_AMMO)
80 f = wpn.wr_checkammo1(wpn, this, weaponentity) + wpn.wr_checkammo2(wpn, this, weaponentity);
82 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
83 if(wpn == WEP_MINE_LAYER)
84 IL_EACH(g_mines, it.owner == this && it.weaponentity_fld == weaponentity,
87 break; // no need to continue
93 if(IS_REAL_CLIENT(this))
95 play2(this, SND(UNAVAILABLE));
96 Send_WeaponComplain (this, wpn.m_id, 0);
106 // Report Proper Weapon Status / Modified Weapon Ownership Message
107 if (weaponsInMap & WepSet_FromWeapon(wpn))
109 Send_WeaponComplain(this, wpn.m_id, 1);
110 if(autocvar_g_showweaponspawns < 3)
111 Weapon_whereis(wpn, this);
114 FOREACH(Weapons, it.impulse == wpn.impulse,
116 Weapon_whereis(it, this);
122 Send_WeaponComplain (this, wpn.m_id, 2);
125 play2(this, SND(UNAVAILABLE));
130 float W_GetCycleWeapon(entity this, string weaponorder, float dir, float imp, float complain, float skipmissing, .entity weaponentity)
132 // We cannot tokenize in this function, as GiveItems calls this
133 // function. Thus we must use car/cdr.
134 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
135 WepSet wepset = '0 0 0';
136 switchtonext = switchtolast = 0;
137 first_valid = prev_valid = 0;
141 if(skipmissing || this.(weaponentity).selectweapon == 0)
142 weaponcur = this.(weaponentity).m_switchweapon.m_id;
144 weaponcur = this.(weaponentity).selectweapon;
151 string rest = weaponorder;
154 weaponwant = stof(car(rest)); rest = cdr(rest);
155 wep = REGISTRY_GET(Weapons, weaponwant);
156 wepset = wep.m_wepset;
158 if(wep.impulse != imp)
161 bool have_other = false;
162 FOREACH(Weapons, it != WEP_Null, {
164 if(it.impulse == imp || imp < 0)
165 if((STAT(WEAPONS, this) & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
169 // skip weapons we don't own that aren't normal and aren't in the map
170 if(!(STAT(WEAPONS, this) & wepset))
171 if(!(weaponsInMap & wepset))
172 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
177 if(!skipmissing || client_hasweapon(this, wep, weaponentity, true, false))
182 first_valid = weaponwant;
183 if(weaponwant == weaponcur)
192 prev_valid = weaponwant;
202 // complain (but only for one weapon on the button that has been pressed)
205 this.weaponcomplainindex += 1;
206 c = (this.weaponcomplainindex % c) + 1;
210 weaponwant = stof(car(rest)); rest = cdr(rest);
211 wep = REGISTRY_GET(Weapons, weaponwant);
212 wepset = wep.m_wepset;
214 if(wep.impulse != imp)
217 bool have_other = false;
218 FOREACH(Weapons, it != WEP_Null, {
220 if(it.impulse == imp || imp < 0)
221 if((STAT(WEAPONS, this) & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
225 // skip weapons we don't own that aren't normal and aren't in the map
226 if(!(STAT(WEAPONS, this) & wepset))
227 if(!(weaponsInMap & wepset))
228 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
234 client_hasweapon(this, wep, weaponentity, true, true);
242 void W_SwitchWeapon_Force(Player this, Weapon wep, .entity weaponentity)
245 this.(weaponentity).cnt = this.(weaponentity).m_switchweapon.m_id;
246 this.(weaponentity).m_switchweapon = wep;
247 this.(weaponentity).selectweapon = wep.m_id;
250 // perform weapon to attack (weaponstate and attack_finished check is here)
251 void W_SwitchToOtherWeapon(entity this, .entity weaponentity)
253 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
255 WepSet set = WepSet_FromWeapon(this.(weaponentity).m_weapon);
256 if (STAT(WEAPONS, this) & set)
258 STAT(WEAPONS, this) &= ~set;
259 ww = w_getbestweapon(this, weaponentity);
260 STAT(WEAPONS, this) |= set;
264 ww = w_getbestweapon(this, weaponentity);
266 if (ww == WEP_Null) return;
267 W_SwitchWeapon_Force(this, ww, weaponentity);
270 bool W_SwitchWeapon(entity this, Weapon w, .entity weaponentity)
272 if(this.(weaponentity).m_switchweapon != w)
274 if(client_hasweapon(this, w, weaponentity, true, true))
276 W_SwitchWeapon_Force(this, w, weaponentity);
281 this.(weaponentity).selectweapon = w.m_id; // update selectweapon anyway
285 else if(!weaponLocked(this) && CS(this).cvar_cl_weapon_switch_reload)
288 w.wr_reload(w, actor, weaponentity);
291 return true; // player already has the weapon out or needs to reload
294 void W_SwitchWeapon_TryOthers(entity this, Weapon w, .entity weaponentity)
296 if(!W_SwitchWeapon(this, w, weaponentity) && CS(this).cvar_cl_weapon_switch_fallback_to_impulse)
297 W_NextWeaponOnImpulse(this, w.impulse, weaponentity);
300 void W_CycleWeapon(entity this, string weaponorder, float dir, .entity weaponentity)
303 w = W_GetCycleWeapon(this, weaponorder, dir, -1, 1, true, weaponentity);
305 W_SwitchWeapon(this, REGISTRY_GET(Weapons, w), weaponentity);
308 void W_NextWeaponOnImpulse(entity this, float imp, .entity weaponentity)
311 w = W_GetCycleWeapon(this, CS(this).cvar_cl_weaponpriority, +1, imp, 1, (CS(this).cvar_cl_weaponimpulsemode == 0), weaponentity);
313 W_SwitchWeapon(this, REGISTRY_GET(Weapons, w), weaponentity);
317 void W_NextWeapon(entity this, int list, .entity weaponentity)
320 W_CycleWeapon(this, weaponorder_byid, -1, weaponentity);
322 W_CycleWeapon(this, CS(this).weaponorder_byimpulse, -1, weaponentity);
324 W_CycleWeapon(this, CS(this).cvar_cl_weaponpriority, -1, weaponentity);
328 void W_PreviousWeapon(entity this, float list, .entity weaponentity)
331 W_CycleWeapon(this, weaponorder_byid, +1, weaponentity);
333 W_CycleWeapon(this, CS(this).weaponorder_byimpulse, +1, weaponentity);
335 W_CycleWeapon(this, CS(this).cvar_cl_weaponpriority, +1, weaponentity);
338 // previously used if exists and has ammo, (second) best otherwise
339 void W_LastWeapon(entity this, .entity weaponentity)
341 Weapon wep = REGISTRY_GET(Weapons, this.(weaponentity).cnt);
342 if (client_hasweapon(this, wep, weaponentity, true, false))
343 W_SwitchWeapon(this, wep, weaponentity);
345 W_SwitchToOtherWeapon(this, weaponentity);