Xonotic
throwing.qc
Go to the documentation of this file.
1 #include "throwing.qh"
2 
3 #include <common/items/item.qh>
4 #include <common/mapinfo.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>
19 #include <server/world.qh>
20 
22 {
23  this.nextthink = time;
24  if(this.oldorigin != this.origin)
25  {
26  this.SendFlags |= ISF_LOCATION;
27  this.oldorigin = this.origin;
28  }
29  this.owner = NULL;
30  float timeleft = this.savenextthink - time;
31  if(timeleft > 1)
32  SUB_SetFade(this, this.savenextthink - 1, 1);
33  else if(timeleft > 0)
34  SUB_SetFade(this, time, timeleft);
35  else
36  SUB_VanishOrRemove(this);
37 }
38 
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)
41 {
42  Weapon info = REGISTRY_GET(Weapons, wpn);
43  Resource ammotype = info.ammo_type;
44 
45  entity wep = spawn();
46  Item_SetLoot(wep, true);
47  setorigin(wep, org);
48  wep.velocity = velo;
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);
54 
55  W_DropEvent(wr_drop,own,wpn,wep,weaponentity);
56 
58  {
59  Item_SetExpiring(wep, true);
60  if(own.items & IT_UNLIMITED_SUPERWEAPONS)
61  {
62  wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
63  }
64  else
65  {
66  int superweapons = 1;
67  FOREACH(Weapons, it != WEP_Null, {
68  WepSet set = it.m_wepset;
69  if((set & WEPSET_SUPERWEAPONS) && (STAT(WEAPONS, own) & set)) ++superweapons;
70  });
71  if(superweapons <= 1)
72  {
73  wep.superweapons_finished = StatusEffects_gettime(STATUSEFFECT_Superweapons, own);
74  StatusEffects_remove(STATUSEFFECT_Superweapons, own, STATUSEFFECT_REMOVE_CLEAR);
75  }
76  else
77  {
78  float timeleft = StatusEffects_gettime(STATUSEFFECT_Superweapons, own) - time;
79  float weptimeleft = timeleft / superweapons;
80  wep.superweapons_finished = time + weptimeleft;
81  if(own.statuseffects)
82  {
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);
86  }
87  }
88  }
89  }
90 
91  weapon_defaultspawnfunc(wep, info);
93  return -1;
95  wep.savenextthink = wep.nextthink;
96  wep.nextthink = min(wep.nextthink, time + 0.5);
97  wep.pickup_anyway = true; // these are ALWAYS pickable
98 
99  //wa = W_AmmoItemCode(wpn);
100  if(ammotype == RES_NONE)
101  {
102  return 0;
103  }
104  else
105  {
106  if(doreduce && g_weapon_stay == 2)
107  {
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)
111  {
112  GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
113  own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
114  }
115  SetResource(wep, ammotype, 0);
116  }
117  else if(doreduce)
118  {
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)
122  {
123  GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
124  own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
125  }
126 
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);
131 
132  return thisammo;
133  }
134  return 0;
135  }
136 }
137 
138 bool W_IsWeaponThrowable(entity this, int w)
139 {
140  if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this, w))
141  return false;
143  return false;
144  if (g_weaponarena)
145  return false;
146  if (w == WEP_Null.m_id)
147  return false;
148 
149  return (REGISTRY_GET(Weapons, w)).weaponthrowable;
150 }
151 
152 // toss current weapon
153 void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
154 {
155  Weapon w = this.(weaponentity).m_weapon;
156  if (w == WEP_Null)
157  return; // just in case
158  if (time < game_starttime)
159  return;
160  if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this, this.(weaponentity)))
161  return;
163  return;
164  if(this.(weaponentity).state != WS_READY)
165  return;
166  if(!W_IsWeaponThrowable(this, w.m_id))
167  return;
168 
169  WepSet set = WepSet_FromWeapon(w);
170  if(!(STAT(WEAPONS, this) & set)) return;
171  STAT(WEAPONS, this) &= ~set;
172 
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);
175 
176  if(a < 0) return;
177  Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, w.m_id, a);
178 }
179 
180 void SpawnThrownWeapon(entity this, vector org, Weapon wep, .entity weaponentity)
181 {
182  //entity wep = this.(weaponentity).m_weapon;
183 
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);
187 }
vector WepSet
Definition: weapon.qh:11
void weapon_defaultspawnfunc(entity this, Weapon wpn)
Definition: spawning.qc:30
WepSet WEPSET_SUPERWEAPONS
Definition: all.qh:307
float weapon_load[REGISTRY_MAX(Weapons)]
Definition: weaponsystem.qh:29
float autocvar_g_balance_superweapons_time
Definition: items.qh:6
void W_SwitchWeapon_Force(Player this, Weapon w,.entity weaponentity)
Definition: selection.qc:243
Header file that describes the resource system.
#define w_getbestweapon(ent, wepent)
Definition: selection.qh:23
vector oldorigin
Definition: csprogsdefs.qc:102
void SUB_VanishOrRemove(entity ent)
Definition: subs.qc:38
entity() spawn
#define REGISTRY_GET(id, i)
Definition: registry.qh:43
void navigation_dynamicgoal_init(entity this, bool initially_static)
Definition: navigation.qc:76
origin
Definition: ent_cs.qc:114
void W_ThrowWeapon(entity this,.entity weaponentity, vector velo, vector delta, float doreduce)
Definition: throwing.qc:153
const int WS_READY
idle frame
Definition: weapon.qh:38
entity owner
Definition: main.qh:73
void W_DropEvent(.void(Weapon, entity actor,.entity) event, entity player, float weapon_type, entity weapon_item,.entity weaponentity)
Effect is being forcibly removed without calling any additional mechanics.
Definition: all.qh:27
void SetResource(entity e, Resource res_type, float amount)
Sets the current amount of resource the given entity will have.
Definition: cl_resources.qc:26
Resource ammo_type
M: ammotype : main ammo type.
Definition: weapon.qh:48
float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo,.entity weaponentity)
Definition: throwing.qc:40
#define NULL
Definition: post.qh:17
void SpawnThrownWeapon(entity this, vector org, Weapon wep,.entity weaponentity)
Definition: throwing.qc:180
float nextthink
Definition: csprogsdefs.qc:121
float savenextthink
Definition: throwing.qh:8
void thrown_wep_think(entity this)
Definition: throwing.qc:21
vector(float skel, float bonenum) _skel_get_boneabs_hidden
void GiveResource(entity receiver, Resource res_type, float amount)
Gives an entity some resource.
bool W_IsWeaponThrowable(entity this, int w)
Definition: throwing.qc:138
float GetResource(entity e, Resource res_type)
Returns the current amount of resource the given entity has.
Definition: cl_resources.qc:10
bool startitem_failed
Definition: spawning.qh:7
float g_weapon_stay
Definition: world.qh:112
#define MUTATOR_CALLHOOK(id,...)
Definition: base.qh:140
setorigin(ent, v)
#define setthink(e, f)
void SUB_SetFade(entity ent, float vanish_time, float fading_time)
Definition: subs.qc:77
bool autocvar_g_weapon_throwable
Definition: throwing.qh:6
int autocvar_g_pickup_items
Definition: items.qh:11
#define WepSet_FromWeapon(it)
Definition: all.qh:38
Header file that describes the functions related to game items.
fields which are explicitly/manually set are marked with "M", fields set automatically are marked wit...
Definition: weapon.qh:41
float time
Definition: csprogsdefs.qc:16
void Item_SetExpiring(entity item, bool expiring)
Sets the item expiring status (i.e.
Definition: spawning.qc:141
int m_id
Definition: weapon.qh:42
#define FOREACH(list, cond, body)
Definition: iter.qh:19
void Item_SetLoot(entity item, bool loot)
Sets the item loot status.
Definition: spawning.qc:126
float g_weaponarena
Definition: world.qh:76