1 #include "machinegun.qh"
5 .float machinegun_spread_accumulation;
9 void W_MachineGun_Attack(Weapon thiswep, int deathtype, entity actor, .entity weaponentity)
11 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);
12 if(!autocvar_g_norecoil)
14 actor.punchangle_x = random() - 0.5;
15 actor.punchangle_y = random() - 0.5;
17 // this attack_finished just enforces a cooldown at the end of a burst
18 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
20 if(actor.(weaponentity).misc_bulletcounter == 1)
21 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);
23 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);
25 W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
28 if(autocvar_g_casings >= 2)
30 makevectors(actor.v_angle); // for some reason, this is lost
31 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);
34 if(actor.(weaponentity).misc_bulletcounter == 1)
35 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, first_ammo), weaponentity);
37 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
41 void W_MachineGun_Attack_Frame(Weapon thiswep, entity actor, .entity weaponentity, int fire)
43 if(actor.(weaponentity).m_weapon != actor.(weaponentity).m_switchweapon || !weapon_prepareattack_check(thiswep, actor, weaponentity, (fire & 2), -1)) // abort immediately if switching
45 w_ready(thiswep, actor, weaponentity, fire);
48 if(PHYS_INPUT_BUTTON_ATCK(actor))
50 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
51 if(!(actor.items & IT_UNLIMITED_AMMO))
53 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
54 w_ready(thiswep, actor, weaponentity, fire);
57 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
58 W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity);
59 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
62 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), w_ready);
66 void W_MachineGun_Attack_Auto(Weapon thiswep, entity actor, .entity weaponentity, int fire)
68 float machinegun_spread;
70 if(!(fire & 1) || !weapon_prepareattack_check(thiswep, actor, weaponentity, false, -1))
72 w_ready(thiswep, actor, weaponentity, fire);
76 if(!thiswep.wr_checkammo1(thiswep, actor, weaponentity))
77 if(!(actor.items & IT_UNLIMITED_AMMO))
79 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
80 w_ready(thiswep, actor, weaponentity, fire);
84 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
86 W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
87 if(!autocvar_g_norecoil)
89 actor.punchangle_x = random() - 0.5;
90 actor.punchangle_y = random() - 0.5;
93 // Step by step calculations for machinegun_spread_accumulation with optional debug printing
96 //print("\n Current time: ");
97 //print(sprintf("%f ", time));
99 // seconds passed since the last shot
100 // oldTime is given value at the end of the calculations
101 //float seconds = (time - actor.(weaponentity).oldTime);
102 //print("\n seconds between last time and this time: ");
103 //print(sprintf("%f ", seconds));
105 // use seconds passed since last shot to calculate how much spread should have decayed since then
106 //float spreadReduction = seconds * WEP_CVAR(machinegun, spread_decay);
107 //print("\n reduced spread: ");
108 //print(sprintf("%f ", spreadReduction));
110 // reduce spread by the calculated amount
111 //actor.(weaponentity).machinegun_spread_accumulation = actor.(weaponentity).machinegun_spread_accumulation - spreadReduction;
112 //print("\n current spread: ");
113 //print(sprintf("%f ", actor.(weaponentity).machinegun_spread_accumulation));
115 // if spread is reduced to the negatives make it 0 instead
116 // the following if block is replaced by max(accumulation, 0);
117 //if(actor.(weaponentity).machinegun_spread_accumulation < 0){
118 // print("\n spread was <0, was reset to 0 before firing");
119 // actor.(weaponentity).machinegun_spread_accumulation = 0;
122 // store the time of the last shot
123 //actor.(weaponentity).oldTime = time;
124 //print("\n Old time: ");
125 //print(sprintf("%f ", actor.(weaponentity).oldTime));
127 actor.(weaponentity).machinegun_spread_accumulation = max(actor.(weaponentity).machinegun_spread_accumulation - ((time - actor.(weaponentity).oldTime) * WEP_CVAR(machinegun, spread_decay)), 0);
128 actor.(weaponentity).oldTime = time;
129 if(WEP_CVAR(machinegun, spread_decay) < 0){
130 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));
132 machinegun_spread = bound(WEP_CVAR(machinegun, spread_min), WEP_CVAR(machinegun, spread_min) + actor.(weaponentity).machinegun_spread_accumulation, WEP_CVAR(machinegun, spread_max));
134 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);
136 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
137 actor.(weaponentity).machinegun_spread_accumulation = actor.(weaponentity).machinegun_spread_accumulation + WEP_CVAR(machinegun, spread_add);
138 if(actor.(weaponentity).machinegun_spread_accumulation > WEP_CVAR(machinegun, spread_max)){
139 actor.(weaponentity).machinegun_spread_accumulation = WEP_CVAR(machinegun, spread_max);
142 W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
144 if(autocvar_g_casings >= 2) // casing code
146 makevectors(actor.v_angle); // for some reason, this is lost
147 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);
150 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
151 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Auto);
154 void W_MachineGun_Attack_Burst(Weapon thiswep, entity actor, .entity weaponentity, int fire)
156 W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
157 if(!autocvar_g_norecoil)
159 actor.punchangle_x = random() - 0.5;
160 actor.punchangle_y = random() - 0.5;
163 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);
165 W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
167 if(autocvar_g_casings >= 2) // casing code
169 makevectors(actor.v_angle); // for some reason, this is lost
170 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);
173 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
174 actor.(weaponentity).machinegun_spread_accumulation = actor.(weaponentity).machinegun_spread_accumulation + (WEP_CVAR(machinegun, spread_add) * 0.5);
175 if(actor.(weaponentity).machinegun_spread_accumulation > WEP_CVAR(machinegun, spread_max)){
176 actor.(weaponentity).machinegun_spread_accumulation = WEP_CVAR(machinegun, spread_max);
178 if(actor.(weaponentity).misc_bulletcounter == 0)
180 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, burst_refire2) * W_WeaponRateFactor(actor);
181 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_animtime), w_ready);
185 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
190 METHOD(MachineGun, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
192 if(vdist(actor.origin - actor.enemy.origin, <, 3000 - bound(0, skill, 10) * 200))
193 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
195 PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
197 METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
199 // forced reload - wait until the bulletcounter is 0 so a burst loop can finish
200 if(WEP_CVAR(machinegun, reload_ammo)
201 && actor.(weaponentity).clip_load < min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo))
202 && actor.(weaponentity).misc_bulletcounter >= 0)
204 thiswep.wr_reload(thiswep, actor, weaponentity);
206 else if(WEP_CVAR(machinegun, mode) == 1)
209 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
211 actor.(weaponentity).misc_bulletcounter = 0;
212 W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
215 // You can "shoot" more rounds than what's "used", and vice versa.
217 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
219 if(WEP_CVAR(machinegun, spread_decay) >= 0){
220 actor.(weaponentity).machinegun_spread_accumulation = max(actor.(weaponentity).machinegun_spread_accumulation - ((time - actor.(weaponentity).oldTime) * WEP_CVAR(machinegun, spread_decay)), 0);
221 actor.(weaponentity).oldTime = time;
222 if(actor.(weaponentity).machinegun_spread_accumulation > 0){
223 w_ready(thiswep, actor, weaponentity, fire);
228 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
229 if(!(actor.items & IT_UNLIMITED_AMMO))
231 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
232 w_ready(thiswep, actor, weaponentity, fire);
236 float ammo_available;
237 if (WEP_CVAR(machinegun, reload_ammo) > 0)
239 ammo_available = actor.(weaponentity).clip_load;
243 ammo_available = GetResource(actor, thiswep.ammo_type);
246 // We don't want to shoot 3 rounds if there's 2 left in the mag, so we'll use a fraction.
247 // Also keep the fraction <= 1 otherwise we'd mag dump in one burst.
248 float burst_fraction = min(1, ammo_available / WEP_CVAR(machinegun, burst_ammo));
249 int to_shoot = floor(WEP_CVAR(machinegun, burst) * burst_fraction);
251 // We also don't want to use 3 rounds if there's only 2 left.
252 int to_use = min(WEP_CVAR(machinegun, burst_ammo), ammo_available);
253 W_DecreaseAmmo(thiswep, actor, to_use, weaponentity);
255 // Bursting counts up to 0 from a negative.
256 actor.(weaponentity).misc_bulletcounter = -to_shoot;
257 W_MachineGun_Attack_Burst(thiswep, actor, weaponentity, fire);
264 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
266 actor.(weaponentity).misc_bulletcounter = 1;
267 W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity); // sets attack_finished
268 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
271 if((fire & 2) && WEP_CVAR(machinegun, first))
272 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
274 actor.(weaponentity).misc_bulletcounter = 1;
275 W_MachineGun_Attack(thiswep, thiswep.m_id | HITTYPE_SECONDARY, actor, weaponentity); // sets attack_finished
276 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
280 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
283 if(WEP_CVAR(machinegun, mode) == 1)
284 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
286 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
288 if(WEP_CVAR(machinegun, reload_ammo))
290 if(WEP_CVAR(machinegun, mode) == 1)
291 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
293 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
297 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
300 float burst_ammo_per_shot = WEP_CVAR(machinegun, burst_ammo) / WEP_CVAR(machinegun, burst);
301 if(WEP_CVAR(machinegun, mode) == 1)
302 ammo_amount = GetResource(actor, thiswep.ammo_type) >= burst_ammo_per_shot;
304 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
306 if(WEP_CVAR(machinegun, reload_ammo))
308 if(WEP_CVAR(machinegun, mode) == 1)
309 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= burst_ammo_per_shot;
311 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
315 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
317 if(actor.(weaponentity).misc_bulletcounter < 0)
319 W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
321 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
323 return WEAPON_THINKING_WITH_PORTALS;
325 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
327 if(w_deathtype & HITTYPE_SECONDARY)
328 return WEAPON_MACHINEGUN_MURDER_SNIPE;
330 return WEAPON_MACHINEGUN_MURDER_SPRAY;
336 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
339 org2 = w_org + w_backoff * 2;
340 pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
342 sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);