3 #include "weaponsystem.qh"
4 #include "../resources.qh"
5 #include <server/items/spawning.qh>
6 #include <server/mutators/_mod.qh>
7 #include <server/items/items.qh>
8 #include "../damage.qh"
9 #include <common/items/item.qh>
10 #include <common/mapinfo.qh>
11 #include <common/notifications/all.qh>
12 #include <common/mapobjects/subs.qh>
13 #include <common/util.qh>
14 #include <common/weapons/_all.qh>
15 #include <common/state.qh>
16 #include <common/wepent.qh>
18 void thrown_wep_think(entity this)
20 this.nextthink = time;
21 if(this.oldorigin != this.origin)
23 this.SendFlags |= ISF_LOCATION;
24 this.oldorigin = this.origin;
27 float timeleft = this.savenextthink - time;
29 SUB_SetFade(this, this.savenextthink - 1, 1);
31 SUB_SetFade(this, time, timeleft);
33 SUB_VanishOrRemove(this);
36 // returns amount of ammo used, or -1 for failure, or 0 for no ammo count
37 float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo, .entity weaponentity)
39 Weapon info = REGISTRY_GET(Weapons, wpn);
40 int ammotype = info.ammo_type;
43 Item_SetLoot(wep, true);
46 wep.owner = wep.enemy = own;
47 wep.flags |= FL_TOSSED;
48 wep.colormap = own.colormap;
49 wep.glowmod = weaponentity_glowmod(info, own, own.clientcolors, own.(weaponentity));
50 navigation_dynamicgoal_init(wep, false);
52 W_DropEvent(wr_drop,own,wpn,wep,weaponentity);
54 if(WepSet_FromWeapon(REGISTRY_GET(Weapons, wpn)) & WEPSET_SUPERWEAPONS)
56 Item_SetExpiring(wep, true);
57 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
59 wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
64 FOREACH(Weapons, it != WEP_Null, {
65 WepSet set = it.m_wepset;
66 if((set & WEPSET_SUPERWEAPONS) && (STAT(WEAPONS, own) & set)) ++superweapons;
70 wep.superweapons_finished = STAT(SUPERWEAPONS_FINISHED, own);
71 STAT(SUPERWEAPONS_FINISHED, own) = 0;
75 float timeleft = STAT(SUPERWEAPONS_FINISHED, own) - time;
76 float weptimeleft = timeleft / superweapons;
77 wep.superweapons_finished = time + weptimeleft;
78 STAT(SUPERWEAPONS_FINISHED, own) -= weptimeleft;
83 weapon_defaultspawnfunc(wep, info);
86 setthink(wep, thrown_wep_think);
87 wep.savenextthink = wep.nextthink;
88 wep.nextthink = min(wep.nextthink, time + 0.5);
89 wep.pickup_anyway = true; // these are ALWAYS pickable
91 //wa = W_AmmoItemCode(wpn);
92 if(ammotype == RES_NONE)
98 if(doreduce && g_weapon_stay == 2)
100 // if our weapon is loaded, give its load back to the player
101 int i = own.(weaponentity).m_weapon.m_id;
102 if(own.(weaponentity).(weapon_load[i]) > 0)
104 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
105 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
107 SetResource(wep, ammotype, 0);
111 // if our weapon is loaded, give its load back to the player
112 int i = own.(weaponentity).m_weapon.m_id;
113 if(own.(weaponentity).(weapon_load[i]) > 0)
115 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
116 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
119 float ownderammo = GetResource(own, ammotype);
120 float thisammo = min(ownderammo, GetResource(wep, ammotype));
121 SetResource(wep, ammotype, thisammo);
122 SetResource(own, ammotype, ownderammo - thisammo);
130 bool W_IsWeaponThrowable(entity this, int w)
132 if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this, w))
134 if (!autocvar_g_pickup_items)
138 if (w == WEP_Null.m_id)
141 return (REGISTRY_GET(Weapons, w)).weaponthrowable;
144 // toss current weapon
145 void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
147 Weapon w = this.(weaponentity).m_weapon;
149 return; // just in case
150 if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this, this.(weaponentity)))
152 if(!autocvar_g_weapon_throwable)
154 if(this.(weaponentity).state != WS_READY)
156 if(!W_IsWeaponThrowable(this, w.m_id))
159 WepSet set = WepSet_FromWeapon(w);
160 if(!(STAT(WEAPONS, this) & set)) return;
161 STAT(WEAPONS, this) &= ~set;
163 W_SwitchWeapon_Force(this, w_getbestweapon(this, weaponentity), weaponentity);
164 float a = W_ThrowNewWeapon(this, w.m_id, doreduce, this.origin + delta, velo, weaponentity);
167 Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, w.m_id, a);
170 void SpawnThrownWeapon(entity this, vector org, Weapon wep, .entity weaponentity)
172 //entity wep = this.(weaponentity).m_weapon;
174 if(STAT(WEAPONS, this) & WepSet_FromWeapon(wep))
175 if(W_IsWeaponThrowable(this, wep.m_id))
176 W_ThrowNewWeapon(this, wep.m_id, false, org, randomvec() * 125 + '0 0 200', weaponentity);