3 #include <common/items/item.qh>
4 #include <common/mapinfo.qh>
5 #include <common/mapobjects/subs.qh>
6 #include <common/mutators/mutator/status_effects/_mod.qh>
7 #include <common/notifications/all.qh>
8 #include <common/resources/sv_resources.qh>
9 #include <common/state.qh>
10 #include <common/util.qh>
11 #include <common/weapons/_all.qh>
12 #include <common/wepent.qh>
13 #include <server/damage.qh>
14 #include <server/items/items.qh>
15 #include <server/items/spawning.qh>
16 #include <server/mutators/_mod.qh>
17 #include <server/weapons/selection.qh>
18 #include <server/weapons/weaponsystem.qh>
19 #include <server/world.qh>
21 void thrown_wep_think(entity this)
23 this.nextthink = time;
24 if(this.oldorigin != this.origin)
26 this.SendFlags |= ISF_LOCATION;
27 this.oldorigin = this.origin;
30 float timeleft = this.savenextthink - time;
32 SUB_SetFade(this, this.savenextthink - 1, 1);
34 SUB_SetFade(this, time, timeleft);
36 SUB_VanishOrRemove(this);
39 // returns amount of ammo used, or -1 for failure, or 0 for no ammo count
40 float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo, .entity weaponentity)
42 Weapon info = REGISTRY_GET(Weapons, wpn);
43 Resource ammotype = info.ammo_type;
46 Item_SetLoot(wep, true);
49 wep.owner = wep.enemy = own;
50 wep.flags |= FL_TOSSED;
51 wep.colormap = own.colormap;
52 wep.glowmod = weaponentity_glowmod(info, own, own.clientcolors, own.(weaponentity));
53 navigation_dynamicgoal_init(wep, false);
55 W_DropEvent(wr_drop,own,wpn,wep,weaponentity);
57 if(WepSet_FromWeapon(REGISTRY_GET(Weapons, wpn)) & WEPSET_SUPERWEAPONS)
59 Item_SetExpiring(wep, true);
60 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
62 wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
67 FOREACH(Weapons, it != WEP_Null, {
68 WepSet set = it.m_wepset;
69 if((set & WEPSET_SUPERWEAPONS) && (STAT(WEAPONS, own) & set)) ++superweapons;
73 wep.superweapons_finished = StatusEffects_gettime(STATUSEFFECT_Superweapons, own);
74 StatusEffects_remove(STATUSEFFECT_Superweapons, own, STATUSEFFECT_REMOVE_CLEAR);
78 float timeleft = StatusEffects_gettime(STATUSEFFECT_Superweapons, own) - time;
79 float weptimeleft = timeleft / superweapons;
80 wep.superweapons_finished = time + weptimeleft;
83 // TODO: this doesn't explicitly enable the effect, use apply here?
84 own.statuseffects.statuseffect_time[STATUSEFFECT_Superweapons.m_id] -= weptimeleft;
85 StatusEffects_update(own);
91 weapon_defaultspawnfunc(wep, info);
94 setthink(wep, thrown_wep_think);
95 wep.savenextthink = wep.nextthink;
96 wep.nextthink = min(wep.nextthink, time + 0.5);
97 wep.pickup_anyway = true; // these are ALWAYS pickable
99 //wa = W_AmmoItemCode(wpn);
100 if(ammotype == RES_NONE)
106 if(doreduce && g_weapon_stay == 2)
108 // if our weapon is loaded, give its load back to the player
109 int i = own.(weaponentity).m_weapon.m_id;
110 if(own.(weaponentity).(weapon_load[i]) > 0)
112 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
113 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
115 SetResource(wep, ammotype, 0);
119 // if our weapon is loaded, give its load back to the player
120 int i = own.(weaponentity).m_weapon.m_id;
121 if(own.(weaponentity).(weapon_load[i]) > 0)
123 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
124 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
127 float ownderammo = GetResource(own, ammotype);
128 float thisammo = min(ownderammo, GetResource(wep, ammotype));
129 SetResource(wep, ammotype, thisammo);
130 SetResource(own, ammotype, ownderammo - thisammo);
138 bool W_IsWeaponThrowable(entity this, int w)
140 if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this, w))
142 if (!autocvar_g_pickup_items)
146 if (w == WEP_Null.m_id)
149 return (REGISTRY_GET(Weapons, w)).weaponthrowable;
152 // toss current weapon
153 void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
155 Weapon w = this.(weaponentity).m_weapon;
157 return; // just in case
158 if (time < game_starttime)
160 if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this, this.(weaponentity)))
162 if(!autocvar_g_weapon_throwable)
164 if(this.(weaponentity).state != WS_READY)
166 if(!W_IsWeaponThrowable(this, w.m_id))
169 WepSet set = WepSet_FromWeapon(w);
170 if(!(STAT(WEAPONS, this) & set)) return;
171 STAT(WEAPONS, this) &= ~set;
173 W_SwitchWeapon_Force(this, w_getbestweapon(this, weaponentity), weaponentity);
174 float a = W_ThrowNewWeapon(this, w.m_id, doreduce, this.origin + delta, velo, weaponentity);
177 Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, w.m_id, a);
180 void SpawnThrownWeapon(entity this, vector org, Weapon wep, .entity weaponentity)
182 //entity wep = this.(weaponentity).m_weapon;
184 if(STAT(WEAPONS, this) & WepSet_FromWeapon(wep))
185 if(W_IsWeaponThrowable(this, wep.m_id))
186 W_ThrowNewWeapon(this, wep.m_id, false, org, randomvec() * 125 + '0 0 200', weaponentity);