3 #include <common/items/item.qh>
4 #include <common/mapinfo.qh>
5 #include <common/mapobjects/subs.qh>
6 #include <common/notifications/all.qh>
7 #include <common/state.qh>
8 #include <common/util.qh>
9 #include <common/weapons/_all.qh>
10 #include <common/wepent.qh>
11 #include <server/damage.qh>
12 #include <server/items/items.qh>
13 #include <server/items/spawning.qh>
14 #include <server/mutators/_mod.qh>
15 #include <server/resources.qh>
16 #include <server/weapons/selection.qh>
17 #include <server/weapons/weaponsystem.qh>
18 #include <server/world.qh>
20 void thrown_wep_think(entity this)
22 this.nextthink = time;
23 if(this.oldorigin != this.origin)
25 this.SendFlags |= ISF_LOCATION;
26 this.oldorigin = this.origin;
29 float timeleft = this.savenextthink - time;
31 SUB_SetFade(this, this.savenextthink - 1, 1);
33 SUB_SetFade(this, time, timeleft);
35 SUB_VanishOrRemove(this);
38 // returns amount of ammo used, or -1 for failure, or 0 for no ammo count
39 float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo, .entity weaponentity)
41 Weapon info = REGISTRY_GET(Weapons, wpn);
42 int ammotype = info.ammo_type;
45 Item_SetLoot(wep, true);
48 wep.owner = wep.enemy = own;
49 wep.flags |= FL_TOSSED;
50 wep.colormap = own.colormap;
51 wep.glowmod = weaponentity_glowmod(info, own, own.clientcolors, own.(weaponentity));
52 navigation_dynamicgoal_init(wep, false);
54 W_DropEvent(wr_drop,own,wpn,wep,weaponentity);
56 if(WepSet_FromWeapon(REGISTRY_GET(Weapons, wpn)) & WEPSET_SUPERWEAPONS)
58 Item_SetExpiring(wep, true);
59 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
61 wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
66 FOREACH(Weapons, it != WEP_Null, {
67 WepSet set = it.m_wepset;
68 if((set & WEPSET_SUPERWEAPONS) && (STAT(WEAPONS, own) & set)) ++superweapons;
72 wep.superweapons_finished = STAT(SUPERWEAPONS_FINISHED, own);
73 STAT(SUPERWEAPONS_FINISHED, own) = 0;
77 float timeleft = STAT(SUPERWEAPONS_FINISHED, own) - time;
78 float weptimeleft = timeleft / superweapons;
79 wep.superweapons_finished = time + weptimeleft;
80 STAT(SUPERWEAPONS_FINISHED, own) -= weptimeleft;
85 weapon_defaultspawnfunc(wep, info);
88 setthink(wep, thrown_wep_think);
89 wep.savenextthink = wep.nextthink;
90 wep.nextthink = min(wep.nextthink, time + 0.5);
91 wep.pickup_anyway = true; // these are ALWAYS pickable
93 //wa = W_AmmoItemCode(wpn);
94 if(ammotype == RES_NONE)
100 if(doreduce && g_weapon_stay == 2)
102 // if our weapon is loaded, give its load back to the player
103 int i = own.(weaponentity).m_weapon.m_id;
104 if(own.(weaponentity).(weapon_load[i]) > 0)
106 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
107 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
109 SetResource(wep, ammotype, 0);
113 // if our weapon is loaded, give its load back to the player
114 int i = own.(weaponentity).m_weapon.m_id;
115 if(own.(weaponentity).(weapon_load[i]) > 0)
117 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
118 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
121 float ownderammo = GetResource(own, ammotype);
122 float thisammo = min(ownderammo, GetResource(wep, ammotype));
123 SetResource(wep, ammotype, thisammo);
124 SetResource(own, ammotype, ownderammo - thisammo);
132 bool W_IsWeaponThrowable(entity this, int w)
134 if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this, w))
136 if (!autocvar_g_pickup_items)
140 if (w == WEP_Null.m_id)
143 return (REGISTRY_GET(Weapons, w)).weaponthrowable;
146 // toss current weapon
147 void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
149 Weapon w = this.(weaponentity).m_weapon;
151 return; // just in case
152 if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this, this.(weaponentity)))
154 if(!autocvar_g_weapon_throwable)
156 if(this.(weaponentity).state != WS_READY)
158 if(!W_IsWeaponThrowable(this, w.m_id))
161 WepSet set = WepSet_FromWeapon(w);
162 if(!(STAT(WEAPONS, this) & set)) return;
163 STAT(WEAPONS, this) &= ~set;
165 W_SwitchWeapon_Force(this, w_getbestweapon(this, weaponentity), weaponentity);
166 float a = W_ThrowNewWeapon(this, w.m_id, doreduce, this.origin + delta, velo, weaponentity);
169 Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, w.m_id, a);
172 void SpawnThrownWeapon(entity this, vector org, Weapon wep, .entity weaponentity)
174 //entity wep = this.(weaponentity).m_weapon;
176 if(STAT(WEAPONS, this) & WepSet_FromWeapon(wep))
177 if(W_IsWeaponThrowable(this, wep.m_id))
178 W_ThrowNewWeapon(this, wep.m_id, false, org, randomvec() * 125 + '0 0 200', weaponentity);