Xonotic
machinegun.qc
Go to the documentation of this file.
1 #include "machinegun.qh"
2 
3 #ifdef SVQC
4 
5 void W_MachineGun_Attack(Weapon thiswep, int deathtype, entity actor, .entity weaponentity)
6 {
7  W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, ((actor.(weaponentity).misc_bulletcounter == 1) ? WEP_CVAR(machinegun, first_damage) : WEP_CVAR(machinegun, sustained_damage)), deathtype);
9  {
10  actor.punchangle_x = random() - 0.5;
11  actor.punchangle_y = random() - 0.5;
12  }
13  // this attack_finished just enforces a cooldown at the end of a burst
14  ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
15 
16  if(actor.(weaponentity).misc_bulletcounter == 1)
17  fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, first_spread), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, first_damage), 0, WEP_CVAR(machinegun, first_force), deathtype, EFFECT_BULLET);
18  else
19  fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, sustained_spread), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), 0, WEP_CVAR(machinegun, sustained_force), deathtype, EFFECT_BULLET);
20 
21  W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
22 
23  // casing code
24  if(autocvar_g_casings >= 2)
25  {
26  makevectors(actor.v_angle); // for some reason, this is lost
27  SpawnCasing(((random() * 50 + 50) * v_right) - (v_forward * (random() * 25 + 25)) - ((random() * 5 - 70) * v_up), 2, vectoangles(v_forward),'0 250 0', 100, 3, actor, weaponentity);
28  }
29 
30  if(actor.(weaponentity).misc_bulletcounter == 1)
31  W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, first_ammo), weaponentity);
32  else
33  W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
34 }
35 
36 // weapon frames
37 void W_MachineGun_Attack_Frame(Weapon thiswep, entity actor, .entity weaponentity, int fire)
38 {
39  if(actor.(weaponentity).m_weapon != actor.(weaponentity).m_switchweapon || !weapon_prepareattack_check(thiswep, actor, weaponentity, (fire & 2), -1)) // abort immediately if switching
40  {
41  w_ready(thiswep, actor, weaponentity, fire);
42  return;
43  }
44  if(PHYS_INPUT_BUTTON_ATCK(actor))
45  {
46  if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
47  if(!(actor.items & IT_UNLIMITED_AMMO))
48  {
49  W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
50  w_ready(thiswep, actor, weaponentity, fire);
51  return;
52  }
53  actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
54  W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity);
55  weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
56  }
57  else
58  weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), w_ready);
59 }
60 
61 
62 void W_MachineGun_Attack_Auto(Weapon thiswep, entity actor, .entity weaponentity, int fire)
63 {
64  float machinegun_spread;
65 
66  if(!(fire & 1) || !weapon_prepareattack_check(thiswep, actor, weaponentity, false, -1))
67  {
68  w_ready(thiswep, actor, weaponentity, fire);
69  return;
70  }
71 
72  if(!thiswep.wr_checkammo1(thiswep, actor, weaponentity))
73  if(!(actor.items & IT_UNLIMITED_AMMO))
74  {
75  W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
76  w_ready(thiswep, actor, weaponentity, fire);
77  return;
78  }
79 
80  W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
81 
82  W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
84  {
85  actor.punchangle_x = random() - 0.5;
86  actor.punchangle_y = random() - 0.5;
87  }
88 
89  machinegun_spread = bound(WEP_CVAR(machinegun, spread_min), WEP_CVAR(machinegun, spread_min) + (WEP_CVAR(machinegun, spread_add) * actor.(weaponentity).misc_bulletcounter), WEP_CVAR(machinegun, spread_max));
90  fireBullet(actor, weaponentity, w_shotorg, w_shotdir, machinegun_spread, WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), 0, WEP_CVAR(machinegun, sustained_force), thiswep.m_id, EFFECT_BULLET);
91 
92  actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
93 
94  W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
95 
96  if(autocvar_g_casings >= 2) // casing code
97  {
98  makevectors(actor.v_angle); // for some reason, this is lost
99  SpawnCasing(((random() * 50 + 50) * v_right) - (v_forward * (random() * 25 + 25)) - ((random() * 5 - 70) * v_up), 2, vectoangles(v_forward),'0 250 0', 100, 3, actor, weaponentity);
100  }
101 
102  ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
103  weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Auto);
104 }
105 
106 void W_MachineGun_Attack_Burst(Weapon thiswep, entity actor, .entity weaponentity, int fire)
107 {
108  W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
110  {
111  actor.punchangle_x = random() - 0.5;
112  actor.punchangle_y = random() - 0.5;
113  }
114 
115  fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, burst_spread), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), 0, WEP_CVAR(machinegun, sustained_force), thiswep.m_id, EFFECT_BULLET);
116 
117  W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
118 
119  if(autocvar_g_casings >= 2) // casing code
120  {
121  makevectors(actor.v_angle); // for some reason, this is lost
122  SpawnCasing(((random() * 50 + 50) * v_right) - (v_forward * (random() * 25 + 25)) - ((random() * 5 - 70) * v_up), 2, vectoangles(v_forward),'0 250 0', 100, 3, actor, weaponentity);
123  }
124 
125  actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
126  if(actor.(weaponentity).misc_bulletcounter == 0)
127  {
128  ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, burst_refire2) * W_WeaponRateFactor(actor);
129  weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_animtime), w_ready);
130  }
131  else
132  {
133  weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
134  }
135 
136 }
137 
138 METHOD(MachineGun, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
139 {
140  if(vdist(actor.origin - actor.enemy.origin, <, 3000 - bound(0, skill, 10) * 200))
141  PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
142  else
143  PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
144 }
145 METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
146 {
147  // forced reload - wait until the bulletcounter is 0 so a burst loop can finish
148  if(WEP_CVAR(machinegun, reload_ammo)
149  && actor.(weaponentity).clip_load < min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo))
150  && actor.(weaponentity).misc_bulletcounter >= 0)
151  {
152  thiswep.wr_reload(thiswep, actor, weaponentity);
153  }
154  else if(WEP_CVAR(machinegun, mode) == 1)
155  {
156  if(fire & 1)
157  if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
158  {
159  actor.(weaponentity).misc_bulletcounter = 0;
160  W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
161  }
162 
163  // You can "shoot" more rounds than what's "used", and vice versa.
164  if(fire & 2)
165  if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
166  {
167  if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
168  if(!(actor.items & IT_UNLIMITED_AMMO))
169  {
170  W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
171  w_ready(thiswep, actor, weaponentity, fire);
172  return;
173  }
174 
175  float ammo_available;
176  if (WEP_CVAR(machinegun, reload_ammo) > 0)
177  {
178  ammo_available = actor.(weaponentity).clip_load;
179  }
180  else
181  {
182  ammo_available = GetResource(actor, thiswep.ammo_type);
183  }
184 
185  // We don't want to shoot 3 rounds if there's 2 left in the mag, so we'll use a fraction.
186  // Also keep the fraction <= 1 otherwise we'd mag dump in one burst.
187  float burst_fraction = min(1, ammo_available / WEP_CVAR(machinegun, burst_ammo));
188  int to_shoot = floor(WEP_CVAR(machinegun, burst) * burst_fraction);
189 
190  // We also don't want to use 3 rounds if there's only 2 left.
191  int to_use = min(WEP_CVAR(machinegun, burst_ammo), ammo_available);
192  W_DecreaseAmmo(thiswep, actor, to_use, weaponentity);
193 
194  // Bursting counts up to 0 from a negative.
195  actor.(weaponentity).misc_bulletcounter = -to_shoot;
196  W_MachineGun_Attack_Burst(thiswep, actor, weaponentity, fire);
197  }
198  }
199  else
200  {
201 
202  if(fire & 1)
203  if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
204  {
205  actor.(weaponentity).misc_bulletcounter = 1;
206  W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity); // sets attack_finished
207  weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
208  }
209 
210  if((fire & 2) && WEP_CVAR(machinegun, first))
211  if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
212  {
213  actor.(weaponentity).misc_bulletcounter = 1;
214  W_MachineGun_Attack(thiswep, thiswep.m_id | HITTYPE_SECONDARY, actor, weaponentity); // sets attack_finished
215  weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
216  }
217  }
218 }
219 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
220 {
221  float ammo_amount;
222  if(WEP_CVAR(machinegun, mode) == 1)
223  ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
224  else
225  ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
226 
227  if(WEP_CVAR(machinegun, reload_ammo))
228  {
229  if(WEP_CVAR(machinegun, mode) == 1)
230  ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
231  else
232  ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
233  }
234  return ammo_amount;
235 }
236 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
237 {
238  float ammo_amount;
239  float burst_ammo_per_shot = WEP_CVAR(machinegun, burst_ammo) / WEP_CVAR(machinegun, burst);
240  if(WEP_CVAR(machinegun, mode) == 1)
241  ammo_amount = GetResource(actor, thiswep.ammo_type) >= burst_ammo_per_shot;
242  else
243  ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
244 
245  if(WEP_CVAR(machinegun, reload_ammo))
246  {
247  if(WEP_CVAR(machinegun, mode) == 1)
248  ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= burst_ammo_per_shot;
249  else
250  ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
251  }
252  return ammo_amount;
253 }
254 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
255 {
256  if(actor.(weaponentity).misc_bulletcounter < 0)
257  return;
258  W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
259 }
260 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
261 {
262  return WEAPON_THINKING_WITH_PORTALS;
263 }
264 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
265 {
267  return WEAPON_MACHINEGUN_MURDER_SNIPE;
268  else
269  return WEAPON_MACHINEGUN_MURDER_SPRAY;
270 }
271 
272 #endif
273 #ifdef CSQC
274 
275 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
276 {
277  vector org2;
278  org2 = w_org + w_backoff * 2;
279  pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
280  if(!w_issilent)
282 }
283 
284 #endif
#define PHYS_INPUT_BUTTON_ATCK2(s)
Definition: player.qh:148
bool bot_aim(entity this,.entity weaponentity, float shotspeed, float shotspeedupward, float maxshottime, float applygravity)
float weapon_load[REGISTRY_MAX(Weapons)]
Definition: weaponsystem.qh:29
vector w_shotorg
Definition: tracing.qh:18
void W_SwitchWeapon_Force(Player this, Weapon w,.entity weaponentity)
Definition: selection.qc:243
#define w_getbestweapon(ent, wepent)
Definition: selection.qh:23
entity() spawn
void w_ready(Weapon thiswep, entity actor,.entity weaponentity, int fire)
float skill
Definition: api.qh:35
#define WEP_CVAR(wepname, name)
Definition: all.qh:299
Sound SND_RIC_RANDOM()
Definition: all.inc:84
vector w_shotdir
Definition: tracing.qh:19
bool weapon_prepareattack_check(Weapon thiswep, entity actor,.entity weaponentity, bool secondary, float attacktime)
#define METHOD(cname, name, prototype)
Definition: oo.qh:257
int clip_load
Definition: wepent.qh:14
const float ATTN_NORM
Definition: csprogsdefs.qc:226
const int CH_WEAPON_A
Definition: sound.qh:7
void fireBullet(entity this,.entity weaponentity, vector start, vector dir, float spread, float max_solid_penetration, float damage, float headshot_multiplier, float force, float dtype, entity tracer_effect)
Definition: tracing.qc:503
vector v_up
Definition: csprogsdefs.qc:31
bool weapon_prepareattack(Weapon thiswep, entity actor,.entity weaponentity, bool secondary, float attacktime)
float misc_bulletcounter
Definition: common.qh:18
#define pointparticles
Definition: csprogsdefs.qh:13
const float VOL_BASE
Definition: sound.qh:36
#define PHYS_INPUT_BUTTON_ATCK(s)
Definition: player.qh:146
const int HITTYPE_SECONDARY
Definition: all.qh:25
#define W_SetupShot(ent, wepent, antilag, recoil, snd, chan, maxdamage, deathtype)
Definition: tracing.qh:33
const int CH_SHOTS
Definition: sound.qh:14
float w_deathtype
Definition: damage.qh:97
vector(float skel, float bonenum) _skel_get_boneabs_hidden
float GetResource(entity e, Resource res_type)
Returns the current amount of resource the given entity has.
Definition: cl_resources.qc:10
#define vdist(v, cmp, f)
Vector distance comparison, avoids sqrt()
Definition: vector.qh:8
entity Notification
always last
Definition: all.qh:82
void weapon_thinkf(entity actor,.entity weaponentity, WFRAME fr, float t, void(Weapon thiswep, entity actor,.entity weaponentity, int fire) func)
vector v_right
Definition: csprogsdefs.qc:31
#define ATTACK_FINISHED(ent, w)
Definition: weaponsystem.qh:42
void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use,.entity weaponentity)
#define sound(e, c, s, v, a)
Definition: sound.qh:52
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
int m_id
Definition: weapon.qh:42
#define makevectors
Definition: post.qh:21
float W_WeaponRateFactor(entity this)
Definition: weaponsystem.qc:33
bool autocvar_g_norecoil
Definition: tracing.qh:15
vector v_forward
Definition: csprogsdefs.qc:31
void W_Reload(entity actor,.entity weaponentity, float sent_ammo_min, Sound sent_sound)