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>
11 #include <common/wepent.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,
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, .entity weaponentity, 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, weaponentity) + wpn.wr_checkammo2(wpn, this, weaponentity);
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 && it.weaponentity_fld == weaponentity,
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, .entity weaponentity)
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.(weaponentity).selectweapon == 0)
135 weaponcur = this.(weaponentity).m_switchweapon.m_id;
137 weaponcur = this.(weaponentity).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, weaponentity, 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, weaponentity, true, true);
235 void W_SwitchWeapon_Force(Player this, Weapon wep, .entity weaponentity)
238 this.(weaponentity).cnt = this.(weaponentity).m_switchweapon.m_id;
239 this.(weaponentity).m_switchweapon = wep;
240 this.(weaponentity).selectweapon = wep.m_id;
243 // perform weapon to attack (weaponstate and attack_finished check is here)
244 void W_SwitchToOtherWeapon(entity this, .entity weaponentity)
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(this.(weaponentity).m_weapon);
249 if (this.weapons & set)
251 this.weapons &= ~set;
252 ww = w_getbestweapon(this, weaponentity);
257 ww = w_getbestweapon(this, weaponentity);
259 if (ww == WEP_Null) return;
260 W_SwitchWeapon_Force(this, ww, weaponentity);
263 void W_SwitchWeapon(entity this, Weapon w, .entity weaponentity)
265 if(this.(weaponentity).m_switchweapon != w)
267 if(client_hasweapon(this, w, weaponentity, true, true))
268 W_SwitchWeapon_Force(this, w, weaponentity);
270 this.(weaponentity).selectweapon = w.m_id; // update selectweapon anyway
272 else if(!forbidWeaponUse(this))
275 w.wr_reload(w, actor, weaponentity);
279 void W_CycleWeapon(entity this, string weaponorder, float dir, .entity weaponentity)
282 w = W_GetCycleWeapon(this, weaponorder, dir, -1, 1, true, weaponentity);
284 W_SwitchWeapon(this, Weapons_from(w), weaponentity);
287 void W_NextWeaponOnImpulse(entity this, float imp, .entity weaponentity)
290 w = W_GetCycleWeapon(this, this.cvar_cl_weaponpriority, +1, imp, 1, (this.cvar_cl_weaponimpulsemode == 0), weaponentity);
292 W_SwitchWeapon(this, Weapons_from(w), weaponentity);
296 void W_NextWeapon(entity this, int list, .entity weaponentity)
299 W_CycleWeapon(this, weaponorder_byid, -1, weaponentity);
301 W_CycleWeapon(this, this.weaponorder_byimpulse, -1, weaponentity);
303 W_CycleWeapon(this, this.cvar_cl_weaponpriority, -1, weaponentity);
307 void W_PreviousWeapon(entity this, float list, .entity weaponentity)
310 W_CycleWeapon(this, weaponorder_byid, +1, weaponentity);
312 W_CycleWeapon(this, this.weaponorder_byimpulse, +1, weaponentity);
314 W_CycleWeapon(this, this.cvar_cl_weaponpriority, +1, weaponentity);
317 // previously used if exists and has ammo, (second) best otherwise
318 void W_LastWeapon(entity this, .entity weaponentity)
320 Weapon wep = Weapons_from(this.(weaponentity).cnt);
321 if (client_hasweapon(this, wep, weaponentity, true, false))
322 W_SwitchWeapon(this, wep, weaponentity);
324 W_SwitchToOtherWeapon(this, weaponentity);