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/weaponsystem.qh>
17 #include <server/world.qh>
19 void thrown_wep_think(entity this)
21 this.nextthink = time;
22 if(this.oldorigin != this.origin)
24 this.SendFlags |= ISF_LOCATION;
25 this.oldorigin = this.origin;
28 float timeleft = this.savenextthink - time;
30 SUB_SetFade(this, this.savenextthink - 1, 1);
32 SUB_SetFade(this, time, timeleft);
34 SUB_VanishOrRemove(this);
37 // returns amount of ammo used, or -1 for failure, or 0 for no ammo count
38 float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo, .entity weaponentity)
40 Weapon info = REGISTRY_GET(Weapons, wpn);
41 int ammotype = info.ammo_type;
44 Item_SetLoot(wep, true);
47 wep.owner = wep.enemy = own;
48 wep.flags |= FL_TOSSED;
49 wep.colormap = own.colormap;
50 wep.glowmod = weaponentity_glowmod(info, own, own.clientcolors, own.(weaponentity));
51 navigation_dynamicgoal_init(wep, false);
53 W_DropEvent(wr_drop,own,wpn,wep,weaponentity);
55 if(WepSet_FromWeapon(REGISTRY_GET(Weapons, wpn)) & WEPSET_SUPERWEAPONS)
57 Item_SetExpiring(wep, true);
58 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
60 wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
65 FOREACH(Weapons, it != WEP_Null, {
66 WepSet set = it.m_wepset;
67 if((set & WEPSET_SUPERWEAPONS) && (STAT(WEAPONS, own) & set)) ++superweapons;
71 wep.superweapons_finished = STAT(SUPERWEAPONS_FINISHED, own);
72 STAT(SUPERWEAPONS_FINISHED, own) = 0;
76 float timeleft = STAT(SUPERWEAPONS_FINISHED, own) - time;
77 float weptimeleft = timeleft / superweapons;
78 wep.superweapons_finished = time + weptimeleft;
79 STAT(SUPERWEAPONS_FINISHED, own) -= weptimeleft;
84 weapon_defaultspawnfunc(wep, info);
87 setthink(wep, thrown_wep_think);
88 wep.savenextthink = wep.nextthink;
89 wep.nextthink = min(wep.nextthink, time + 0.5);
90 wep.pickup_anyway = true; // these are ALWAYS pickable
92 //wa = W_AmmoItemCode(wpn);
93 if(ammotype == RES_NONE)
99 if(doreduce && g_weapon_stay == 2)
101 // if our weapon is loaded, give its load back to the player
102 int i = own.(weaponentity).m_weapon.m_id;
103 if(own.(weaponentity).(weapon_load[i]) > 0)
105 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
106 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
108 SetResource(wep, ammotype, 0);
112 // if our weapon is loaded, give its load back to the player
113 int i = own.(weaponentity).m_weapon.m_id;
114 if(own.(weaponentity).(weapon_load[i]) > 0)
116 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
117 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
120 float ownderammo = GetResource(own, ammotype);
121 float thisammo = min(ownderammo, GetResource(wep, ammotype));
122 SetResource(wep, ammotype, thisammo);
123 SetResource(own, ammotype, ownderammo - thisammo);
131 bool W_IsWeaponThrowable(entity this, int w)
133 if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this, w))
135 if (!autocvar_g_pickup_items)
139 if (w == WEP_Null.m_id)
142 return (REGISTRY_GET(Weapons, w)).weaponthrowable;
145 // toss current weapon
146 void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
148 Weapon w = this.(weaponentity).m_weapon;
150 return; // just in case
151 if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this, this.(weaponentity)))
153 if(!autocvar_g_weapon_throwable)
155 if(this.(weaponentity).state != WS_READY)
157 if(!W_IsWeaponThrowable(this, w.m_id))
160 WepSet set = WepSet_FromWeapon(w);
161 if(!(STAT(WEAPONS, this) & set)) return;
162 STAT(WEAPONS, this) &= ~set;
164 W_SwitchWeapon_Force(this, w_getbestweapon(this, weaponentity), weaponentity);
165 float a = W_ThrowNewWeapon(this, w.m_id, doreduce, this.origin + delta, velo, weaponentity);
168 Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, w.m_id, a);
171 void SpawnThrownWeapon(entity this, vector org, Weapon wep, .entity weaponentity)
173 //entity wep = this.(weaponentity).m_weapon;
175 if(STAT(WEAPONS, this) & WepSet_FromWeapon(wep))
176 if(W_IsWeaponThrowable(this, wep.m_id))
177 W_ThrowNewWeapon(this, wep.m_id, false, org, randomvec() * 125 + '0 0 200', weaponentity);