fix "infinite" burst happening when reload ammo was 0 (as in XDF)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / machinegun.qc
1 #include "machinegun.qh"
2
3 #ifdef SVQC
4
5 void W_MachineGun_MuzzleFlash_Think(entity this)
6 {
7         this.frame += 2;
8         this.scale *= 0.5;
9         this.alpha -= 0.25;
10         this.nextthink = time + 0.05;
11
12         if(this.alpha <= 0)
13         {
14                 setthink(this, SUB_Remove);
15                 this.nextthink = time;
16                 this.realowner.muzzle_flash = NULL;
17                 return;
18         }
19
20 }
21
22 void W_MachineGun_MuzzleFlash(entity actor, .entity weaponentity)
23 {
24         entity wepent = actor.(weaponentity);
25
26         if(wepent.muzzle_flash == NULL)
27                 wepent.muzzle_flash = spawn();
28
29         // muzzle flash for 1st person view
30         setmodel(wepent.muzzle_flash, MDL_MACHINEGUN_MUZZLEFLASH); // precision set below
31
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;
40 }
41
42 void W_MachineGun_Attack(Weapon thiswep, int deathtype, entity actor, .entity weaponentity)
43 {
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)
46         {
47                 actor.punchangle_x = random() - 0.5;
48                 actor.punchangle_y = random() - 0.5;
49         }
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);
52
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);
55         else
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);
57
58         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
59
60         W_MachineGun_MuzzleFlash(actor, weaponentity);
61         W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
62
63         // casing code
64         if(autocvar_g_casings >= 2)
65         {
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);
68         }
69
70         if(actor.(weaponentity).misc_bulletcounter == 1)
71                 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, first_ammo), weaponentity);
72         else
73                 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
74 }
75
76 // weapon frames
77 void W_MachineGun_Attack_Frame(Weapon thiswep, entity actor, .entity weaponentity, int fire)
78 {
79         if(actor.(weaponentity).m_weapon != actor.(weaponentity).m_switchweapon || !weapon_prepareattack_check(thiswep, actor, weaponentity, (fire & 2), -1)) // abort immediately if switching
80         {
81                 w_ready(thiswep, actor, weaponentity, fire);
82                 return;
83         }
84         if(PHYS_INPUT_BUTTON_ATCK(actor))
85         {
86                 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
87                 if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
88                 {
89                         W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
90                         w_ready(thiswep, actor, weaponentity, fire);
91                         return;
92                 }
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);
96         }
97         else
98                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), w_ready);
99 }
100
101
102 void W_MachineGun_Attack_Auto(Weapon thiswep, entity actor, .entity weaponentity, int fire)
103 {
104         float machinegun_spread;
105
106         if(!(fire & 1) || !weapon_prepareattack_check(thiswep, actor, weaponentity, false, -1))
107         {
108                 w_ready(thiswep, actor, weaponentity, fire);
109                 return;
110         }
111
112         if(!thiswep.wr_checkammo1(thiswep, actor, weaponentity))
113         if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
114         {
115                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
116                 w_ready(thiswep, actor, weaponentity, fire);
117                 return;
118         }
119
120         W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
121
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)
124         {
125                 actor.punchangle_x = random() - 0.5;
126                 actor.punchangle_y = random() - 0.5;
127         }
128
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);
131
132         actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
133
134         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
135
136         W_MachineGun_MuzzleFlash(actor, weaponentity);
137         W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
138
139         if(autocvar_g_casings >= 2) // casing code
140         {
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);
143         }
144
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);
147 }
148
149 void W_MachineGun_Attack_Burst(Weapon thiswep, entity actor, .entity weaponentity, int fire)
150 {
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)
153         {
154                 actor.punchangle_x = random() - 0.5;
155                 actor.punchangle_y = random() - 0.5;
156         }
157
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);
159
160         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
161
162         W_MachineGun_MuzzleFlash(actor, weaponentity);
163         W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
164
165         if(autocvar_g_casings >= 2) // casing code
166         {
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);
169         }
170
171         actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
172         if(actor.(weaponentity).misc_bulletcounter == 0)
173         {
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);
176         }
177         else
178         {
179                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
180         }
181
182 }
183
184 METHOD(MachineGun, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
185 {
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);
188     else
189         PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
190 }
191 METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
192 {
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)
197     {
198         thiswep.wr_reload(thiswep, actor, weaponentity);
199     }
200     else if(WEP_CVAR(machinegun, mode) == 1)
201     {
202         if(fire & 1)
203         if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
204         {
205             actor.(weaponentity).misc_bulletcounter = 0;
206             W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
207         }
208
209         // You can "shoot" more rounds than what's "used", and vice versa.
210         if(fire & 2)
211         if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
212         {
213             if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
214             if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
215             {
216                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
217                 w_ready(thiswep, actor, weaponentity, fire);
218                 return;
219             }
220
221             float ammo_available;
222             if (WEP_CVAR(machinegun, reload_ammo) > 0)
223             {
224                 ammo_available = actor.(weaponentity).clip_load;
225             }
226             else
227             {
228                 ammo_available = GetResource(actor, thiswep.ammo_type);
229             }
230
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);
235
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);
239
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);
243         }
244     }
245     else
246     {
247
248         if(fire & 1)
249         if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
250         {
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);
254         }
255
256         if((fire & 2) && WEP_CVAR(machinegun, first))
257         if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
258         {
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);
262         }
263     }
264 }
265 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
266 {
267     float ammo_amount;
268     if(WEP_CVAR(machinegun, mode) == 1)
269         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
270     else
271         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
272
273     if(WEP_CVAR(machinegun, reload_ammo))
274     {
275         if(WEP_CVAR(machinegun, mode) == 1)
276             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
277         else
278             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
279     }
280     return ammo_amount;
281 }
282 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
283 {
284     float ammo_amount;
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;
288     else
289         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
290
291     if(WEP_CVAR(machinegun, reload_ammo))
292     {
293         if(WEP_CVAR(machinegun, mode) == 1)
294             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= burst_ammo_per_shot;
295         else
296             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
297     }
298     return ammo_amount;
299 }
300 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
301 {
302         if(actor.(weaponentity).misc_bulletcounter < 0)
303                 return;
304     W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
305 }
306 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
307 {
308     return WEAPON_THINKING_WITH_PORTALS;
309 }
310 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
311 {
312     if(w_deathtype & HITTYPE_SECONDARY)
313         return WEAPON_MACHINEGUN_MURDER_SNIPE;
314     else
315         return WEAPON_MACHINEGUN_MURDER_SPRAY;
316 }
317
318 #endif
319 #ifdef CSQC
320
321 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
322 {
323     vector org2;
324     org2 = w_org + w_backoff * 2;
325     pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
326     if(!w_issilent)
327         sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);
328 }
329
330 #endif