1 #include "machinegun.qh"
5 void W_MachineGun_Attack(Weapon thiswep, int deathtype, entity actor, .entity weaponentity)
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);
8 if(!autocvar_g_norecoil)
10 actor.punchangle_x = random() - 0.5;
11 actor.punchangle_y = random() - 0.5;
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);
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);
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);
21 W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
24 if(autocvar_g_casings >= 2)
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);
30 if(actor.(weaponentity).misc_bulletcounter == 1)
31 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, first_ammo), weaponentity);
33 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
37 void W_MachineGun_Attack_Frame(Weapon thiswep, entity actor, .entity weaponentity, int fire)
39 if(actor.(weaponentity).m_weapon != actor.(weaponentity).m_switchweapon || !weapon_prepareattack_check(thiswep, actor, weaponentity, (fire & 2), -1)) // abort immediately if switching
41 w_ready(thiswep, actor, weaponentity, fire);
44 if(PHYS_INPUT_BUTTON_ATCK(actor))
46 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
47 if(!(actor.items & IT_UNLIMITED_AMMO))
49 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
50 w_ready(thiswep, actor, weaponentity, fire);
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);
58 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), w_ready);
62 void W_MachineGun_Attack_Auto(Weapon thiswep, entity actor, .entity weaponentity, int fire)
64 float machinegun_spread;
66 if(!(fire & 1) || !weapon_prepareattack_check(thiswep, actor, weaponentity, false, -1))
68 w_ready(thiswep, actor, weaponentity, fire);
72 if(!thiswep.wr_checkammo1(thiswep, actor, weaponentity))
73 if(!(actor.items & IT_UNLIMITED_AMMO))
75 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
76 w_ready(thiswep, actor, weaponentity, fire);
80 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
82 W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
83 if(!autocvar_g_norecoil)
85 actor.punchangle_x = random() - 0.5;
86 actor.punchangle_y = random() - 0.5;
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);
92 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
94 W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
96 if(autocvar_g_casings >= 2) // casing code
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);
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);
106 void W_MachineGun_Attack_Burst(Weapon thiswep, entity actor, .entity weaponentity, int fire)
108 W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
109 if(!autocvar_g_norecoil)
111 actor.punchangle_x = random() - 0.5;
112 actor.punchangle_y = random() - 0.5;
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);
117 W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
119 if(autocvar_g_casings >= 2) // casing code
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);
125 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
126 if(actor.(weaponentity).misc_bulletcounter == 0)
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);
133 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
138 METHOD(MachineGun, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
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, true);
143 PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false, true);
145 METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
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)
152 thiswep.wr_reload(thiswep, actor, weaponentity);
154 else if(WEP_CVAR(machinegun, mode) == 1)
157 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
159 actor.(weaponentity).misc_bulletcounter = 0;
160 W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
163 // You can "shoot" more rounds than what's "used", and vice versa.
165 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
167 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
168 if(!(actor.items & IT_UNLIMITED_AMMO))
170 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
171 w_ready(thiswep, actor, weaponentity, fire);
175 float ammo_available;
176 if (WEP_CVAR(machinegun, reload_ammo) > 0)
178 ammo_available = actor.(weaponentity).clip_load;
182 ammo_available = GetResource(actor, thiswep.ammo_type);
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);
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);
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);
203 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
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);
210 if((fire & 2) && WEP_CVAR(machinegun, first))
211 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
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);
219 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
222 if(WEP_CVAR(machinegun, mode) == 1)
223 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
225 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
227 if(WEP_CVAR(machinegun, reload_ammo))
229 if(WEP_CVAR(machinegun, mode) == 1)
230 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
232 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
236 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
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;
243 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
245 if(WEP_CVAR(machinegun, reload_ammo))
247 if(WEP_CVAR(machinegun, mode) == 1)
248 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= burst_ammo_per_shot;
250 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
254 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
256 if(actor.(weaponentity).misc_bulletcounter < 0)
258 W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
260 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
262 return WEAPON_THINKING_WITH_PORTALS;
264 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
266 if(w_deathtype & HITTYPE_SECONDARY)
267 return WEAPON_MACHINEGUN_MURDER_SNIPE;
269 return WEAPON_MACHINEGUN_MURDER_SPRAY;
275 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
278 org2 = w_org + w_backoff * 2;
279 pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
281 sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);