1 #include "machinegun.qh"
5 void W_MachineGun_MuzzleFlash_Think(entity this)
10 this.nextthink = time + 0.05;
14 setthink(this, SUB_Remove);
15 this.nextthink = time;
16 this.realowner.muzzle_flash = NULL;
22 void W_MachineGun_MuzzleFlash(entity actor, .entity weaponentity)
24 entity wepent = actor.(weaponentity);
26 if(wepent.muzzle_flash == NULL)
27 wepent.muzzle_flash = spawn();
29 // muzzle flash for 1st person view
30 setmodel(wepent.muzzle_flash, MDL_MACHINEGUN_MUZZLEFLASH); // precision set below
32 wepent.muzzle_flash.scale = 0.75;
33 setthink(wepent.muzzle_flash, W_MachineGun_MuzzleFlash_Think);
34 wepent.muzzle_flash.nextthink = time + 0.02;
35 wepent.muzzle_flash.frame = 2;
36 wepent.muzzle_flash.alpha = 0.75;
37 wepent.muzzle_flash.angles_z = random() * 180;
38 wepent.muzzle_flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
39 wepent.muzzle_flash.owner = wepent.muzzle_flash.realowner = wepent;
42 void W_MachineGun_Attack(Weapon thiswep, int deathtype, entity actor, .entity weaponentity)
44 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);
45 if(!autocvar_g_norecoil)
47 actor.punchangle_x = random() - 0.5;
48 actor.punchangle_y = random() - 0.5;
50 // this attack_finished just enforces a cooldown at the end of a burst
51 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
53 if(actor.(weaponentity).misc_bulletcounter == 1)
54 fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, first_spread), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, first_damage), WEP_CVAR(machinegun, first_force), deathtype, EFFECT_BULLET);
56 fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, sustained_spread), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), WEP_CVAR(machinegun, sustained_force), deathtype, EFFECT_BULLET);
58 Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
60 W_MachineGun_MuzzleFlash(actor, weaponentity);
61 W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
64 if(autocvar_g_casings >= 2)
66 makevectors(actor.v_angle); // for some reason, this is lost
67 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);
70 if(actor.(weaponentity).misc_bulletcounter == 1)
71 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, first_ammo), weaponentity);
73 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
77 void W_MachineGun_Attack_Frame(Weapon thiswep, entity actor, .entity weaponentity, int fire)
79 if(actor.(weaponentity).m_weapon != actor.(weaponentity).m_switchweapon || !weapon_prepareattack_check(thiswep, actor, weaponentity, (fire & 2), -1)) // abort immediately if switching
81 w_ready(thiswep, actor, weaponentity, fire);
84 if(PHYS_INPUT_BUTTON_ATCK(actor))
86 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
87 if(!(actor.items & IT_UNLIMITED_AMMO))
89 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
90 w_ready(thiswep, actor, weaponentity, fire);
93 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
94 W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity);
95 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
98 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), w_ready);
102 void W_MachineGun_Attack_Auto(Weapon thiswep, entity actor, .entity weaponentity, int fire)
104 float machinegun_spread;
106 if(!(fire & 1) || !weapon_prepareattack_check(thiswep, actor, weaponentity, false, -1))
108 w_ready(thiswep, actor, weaponentity, fire);
112 if(!thiswep.wr_checkammo1(thiswep, actor, weaponentity))
113 if(!(actor.items & IT_UNLIMITED_AMMO))
115 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
116 w_ready(thiswep, actor, weaponentity, fire);
120 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
122 W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
123 if(!autocvar_g_norecoil)
125 actor.punchangle_x = random() - 0.5;
126 actor.punchangle_y = random() - 0.5;
129 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));
130 fireBullet(actor, weaponentity, w_shotorg, w_shotdir, machinegun_spread, WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), WEP_CVAR(machinegun, sustained_force), thiswep.m_id, EFFECT_BULLET);
132 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
134 Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
136 W_MachineGun_MuzzleFlash(actor, weaponentity);
137 W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
139 if(autocvar_g_casings >= 2) // casing code
141 makevectors(actor.v_angle); // for some reason, this is lost
142 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);
145 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
146 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Auto);
149 void W_MachineGun_Attack_Burst(Weapon thiswep, entity actor, .entity weaponentity, int fire)
151 W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
152 if(!autocvar_g_norecoil)
154 actor.punchangle_x = random() - 0.5;
155 actor.punchangle_y = random() - 0.5;
158 fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, burst_spread), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), WEP_CVAR(machinegun, sustained_force), thiswep.m_id, EFFECT_BULLET);
160 Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
162 W_MachineGun_MuzzleFlash(actor, weaponentity);
163 W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
165 if(autocvar_g_casings >= 2) // casing code
167 makevectors(actor.v_angle); // for some reason, this is lost
168 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);
171 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
172 if(actor.(weaponentity).misc_bulletcounter == 0)
174 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, burst_refire2) * W_WeaponRateFactor(actor);
175 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_animtime), w_ready);
179 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
184 METHOD(MachineGun, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
186 if(vdist(actor.origin - actor.enemy.origin, <, 3000 - bound(0, skill, 10) * 200))
187 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
189 PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
191 METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
193 // forced reload - wait until the bulletcounter is 0 so a burst loop can finish
194 if(WEP_CVAR(machinegun, reload_ammo)
195 && actor.(weaponentity).clip_load < min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo))
196 && actor.(weaponentity).misc_bulletcounter >= 0)
198 thiswep.wr_reload(thiswep, actor, weaponentity);
200 else if(WEP_CVAR(machinegun, mode) == 1)
203 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
205 actor.(weaponentity).misc_bulletcounter = 0;
206 W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
209 // You can "shoot" more rounds than what's "used", and vice versa.
211 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
213 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
214 if(!(actor.items & IT_UNLIMITED_AMMO))
216 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
217 w_ready(thiswep, actor, weaponentity, fire);
221 float ammo_available;
222 if (WEP_CVAR(machinegun, reload_ammo) > 0)
224 ammo_available = actor.(weaponentity).clip_load;
228 ammo_available = GetResource(actor, thiswep.ammo_type);
231 // We don't want to shoot 3 rounds if there's 2 left in the mag, so we'll use a fraction.
232 // Also keep the fraction <= 1 otherwise we'd mag dump in one burst.
233 float burst_fraction = min(1, ammo_available / WEP_CVAR(machinegun, burst_ammo));
234 int to_shoot = floor(WEP_CVAR(machinegun, burst) * burst_fraction);
236 // We also don't want to use 3 rounds if there's only 2 left.
237 int to_use = min(WEP_CVAR(machinegun, burst_ammo), ammo_available);
238 W_DecreaseAmmo(thiswep, actor, to_use, weaponentity);
240 // Bursting counts up to 0 from a negative.
241 actor.(weaponentity).misc_bulletcounter = -to_shoot;
242 W_MachineGun_Attack_Burst(thiswep, actor, weaponentity, fire);
249 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
251 actor.(weaponentity).misc_bulletcounter = 1;
252 W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity); // sets attack_finished
253 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
256 if((fire & 2) && WEP_CVAR(machinegun, first))
257 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
259 actor.(weaponentity).misc_bulletcounter = 1;
260 W_MachineGun_Attack(thiswep, thiswep.m_id | HITTYPE_SECONDARY, actor, weaponentity); // sets attack_finished
261 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
265 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
268 if(WEP_CVAR(machinegun, mode) == 1)
269 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
271 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
273 if(WEP_CVAR(machinegun, reload_ammo))
275 if(WEP_CVAR(machinegun, mode) == 1)
276 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
278 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
282 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
285 float burst_ammo_per_shot = WEP_CVAR(machinegun, burst_ammo) / WEP_CVAR(machinegun, burst);
286 if(WEP_CVAR(machinegun, mode) == 1)
287 ammo_amount = GetResource(actor, thiswep.ammo_type) >= burst_ammo_per_shot;
289 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
291 if(WEP_CVAR(machinegun, reload_ammo))
293 if(WEP_CVAR(machinegun, mode) == 1)
294 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= burst_ammo_per_shot;
296 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
300 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
302 if(actor.(weaponentity).misc_bulletcounter < 0)
304 W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
306 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
308 return WEAPON_THINKING_WITH_PORTALS;
310 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
312 if(w_deathtype & HITTYPE_SECONDARY)
313 return WEAPON_MACHINEGUN_MURDER_SNIPE;
315 return WEAPON_MACHINEGUN_MURDER_SPRAY;
321 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
324 org2 = w_org + w_backoff * 2;
325 pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
327 sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);