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;
130 machinegun_spread = bound(WEP_CVAR(machinegun, spread_min), WEP_CVAR(machinegun, spread_min) + actor.(weaponentity).machinegun_spread_accumulation, WEP_CVAR(machinegun, spread_max));
131 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);
133 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
134 actor.(weaponentity).machinegun_spread_accumulation = actor.(weaponentity).machinegun_spread_accumulation + WEP_CVAR(machinegun, spread_add);
135 if(actor.(weaponentity).machinegun_spread_accumulation > WEP_CVAR(machinegun, spread_max)){
136 actor.(weaponentity).machinegun_spread_accumulation = WEP_CVAR(machinegun, spread_max);
139 W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
141 if(autocvar_g_casings >= 2) // casing code
143 makevectors(actor.v_angle); // for some reason, this is lost
144 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);
147 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
148 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Auto);
151 void W_MachineGun_Attack_Burst(Weapon thiswep, entity actor, .entity weaponentity, int fire)
153 W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
154 if(!autocvar_g_norecoil)
156 actor.punchangle_x = random() - 0.5;
157 actor.punchangle_y = random() - 0.5;
160 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);
162 W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
164 if(autocvar_g_casings >= 2) // casing code
166 makevectors(actor.v_angle); // for some reason, this is lost
167 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);
170 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
171 actor.(weaponentity).machinegun_spread_accumulation = actor.(weaponentity).machinegun_spread_accumulation + (WEP_CVAR(machinegun, spread_add) * 0.5);
172 if(actor.(weaponentity).machinegun_spread_accumulation > WEP_CVAR(machinegun, spread_max)){
173 actor.(weaponentity).machinegun_spread_accumulation = WEP_CVAR(machinegun, spread_max);
175 if(actor.(weaponentity).misc_bulletcounter == 0)
177 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, burst_refire2) * W_WeaponRateFactor(actor);
178 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_animtime), w_ready);
182 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
187 METHOD(MachineGun, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
189 if(vdist(actor.origin - actor.enemy.origin, <, 3000 - bound(0, skill, 10) * 200))
190 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
192 PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
194 METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
196 // forced reload - wait until the bulletcounter is 0 so a burst loop can finish
197 if(WEP_CVAR(machinegun, reload_ammo)
198 && actor.(weaponentity).clip_load < min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo))
199 && actor.(weaponentity).misc_bulletcounter >= 0)
201 thiswep.wr_reload(thiswep, actor, weaponentity);
203 else if(WEP_CVAR(machinegun, mode) == 1)
206 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
208 actor.(weaponentity).misc_bulletcounter = 0;
209 W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
212 // You can "shoot" more rounds than what's "used", and vice versa.
214 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
216 actor.(weaponentity).machinegun_spread_accumulation = max(actor.(weaponentity).machinegun_spread_accumulation - ((time - actor.(weaponentity).oldTime) * WEP_CVAR(machinegun, spread_decay)), 0);
217 actor.(weaponentity).oldTime = time;
218 if(actor.(weaponentity).machinegun_spread_accumulation > 0){
219 w_ready(thiswep, actor, weaponentity, fire);
223 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
224 if(!(actor.items & IT_UNLIMITED_AMMO))
226 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
227 w_ready(thiswep, actor, weaponentity, fire);
231 float ammo_available;
232 if (WEP_CVAR(machinegun, reload_ammo) > 0)
234 ammo_available = actor.(weaponentity).clip_load;
238 ammo_available = GetResource(actor, thiswep.ammo_type);
241 // We don't want to shoot 3 rounds if there's 2 left in the mag, so we'll use a fraction.
242 // Also keep the fraction <= 1 otherwise we'd mag dump in one burst.
243 float burst_fraction = min(1, ammo_available / WEP_CVAR(machinegun, burst_ammo));
244 int to_shoot = floor(WEP_CVAR(machinegun, burst) * burst_fraction);
246 // We also don't want to use 3 rounds if there's only 2 left.
247 int to_use = min(WEP_CVAR(machinegun, burst_ammo), ammo_available);
248 W_DecreaseAmmo(thiswep, actor, to_use, weaponentity);
250 // Bursting counts up to 0 from a negative.
251 actor.(weaponentity).misc_bulletcounter = -to_shoot;
252 W_MachineGun_Attack_Burst(thiswep, actor, weaponentity, fire);
259 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
261 actor.(weaponentity).misc_bulletcounter = 1;
262 W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity); // sets attack_finished
263 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
266 if((fire & 2) && WEP_CVAR(machinegun, first))
267 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
269 actor.(weaponentity).misc_bulletcounter = 1;
270 W_MachineGun_Attack(thiswep, thiswep.m_id | HITTYPE_SECONDARY, actor, weaponentity); // sets attack_finished
271 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
275 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
278 if(WEP_CVAR(machinegun, mode) == 1)
279 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
281 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
283 if(WEP_CVAR(machinegun, reload_ammo))
285 if(WEP_CVAR(machinegun, mode) == 1)
286 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
288 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
292 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
295 float burst_ammo_per_shot = WEP_CVAR(machinegun, burst_ammo) / WEP_CVAR(machinegun, burst);
296 if(WEP_CVAR(machinegun, mode) == 1)
297 ammo_amount = GetResource(actor, thiswep.ammo_type) >= burst_ammo_per_shot;
299 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
301 if(WEP_CVAR(machinegun, reload_ammo))
303 if(WEP_CVAR(machinegun, mode) == 1)
304 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= burst_ammo_per_shot;
306 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
310 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
312 if(actor.(weaponentity).misc_bulletcounter < 0)
314 W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
316 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
318 return WEAPON_THINKING_WITH_PORTALS;
320 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
322 if(w_deathtype & HITTYPE_SECONDARY)
323 return WEAPON_MACHINEGUN_MURDER_SNIPE;
325 return WEAPON_MACHINEGUN_MURDER_SPRAY;
331 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
334 org2 = w_org + w_backoff * 2;
335 pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
337 sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);