Fix indenting so it's consistent with the rest of the function.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / machinegun.qc
1 #include "machinegun.qh"
2
3 #ifdef SVQC
4
5 METHOD(MachineGun, m_spawnfunc_hookreplace, Weapon(MachineGun this, entity e))
6 {
7         if (autocvar_sv_q3acompat_machineshotgunswap && !Item_IsLoot(e))
8         {
9                 return WEP_SHOCKWAVE;
10         }
11         return this;
12 }
13
14 void W_MachineGun_MuzzleFlash_Think(entity this)
15 {
16         this.frame += 2;
17         this.scale *= 0.5;
18         this.alpha -= 0.25;
19         this.nextthink = time + 0.05;
20
21         if(this.alpha <= 0)
22         {
23                 setthink(this, SUB_Remove);
24                 this.nextthink = time;
25                 this.realowner.muzzle_flash = NULL;
26                 return;
27         }
28
29 }
30
31 void W_MachineGun_MuzzleFlash(entity actor, .entity weaponentity)
32 {
33         entity wepent = actor.(weaponentity);
34
35         if(wepent.muzzle_flash == NULL)
36                 wepent.muzzle_flash = spawn();
37
38         // muzzle flash for 1st person view
39         setmodel(wepent.muzzle_flash, MDL_MACHINEGUN_MUZZLEFLASH); // precision set below
40
41         wepent.muzzle_flash.scale = 0.75;
42         setthink(wepent.muzzle_flash, W_MachineGun_MuzzleFlash_Think);
43         wepent.muzzle_flash.nextthink = time + 0.02;
44         wepent.muzzle_flash.frame = 2;
45         wepent.muzzle_flash.alpha = 0.75;
46         wepent.muzzle_flash.angles_z = random() * 180;
47         wepent.muzzle_flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
48         wepent.muzzle_flash.owner = wepent.muzzle_flash.realowner = wepent;
49 }
50
51 void W_MachineGun_Attack(Weapon thiswep, int deathtype, entity actor, .entity weaponentity)
52 {
53         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);
54         if(!autocvar_g_norecoil)
55         {
56                 actor.punchangle_x = random() - 0.5;
57                 actor.punchangle_y = random() - 0.5;
58         }
59         // this attack_finished just enforces a cooldown at the end of a burst
60         ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
61
62         if(actor.(weaponentity).misc_bulletcounter == 1)
63                 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);
64         else
65                 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);
66
67         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
68
69         W_MachineGun_MuzzleFlash(actor, weaponentity);
70         W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
71
72         // casing code
73         if(autocvar_g_casings >= 2)
74         {
75                 makevectors(actor.v_angle); // for some reason, this is lost
76                 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);
77         }
78
79         if(actor.(weaponentity).misc_bulletcounter == 1)
80                 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, first_ammo), weaponentity);
81         else
82                 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
83 }
84
85 // weapon frames
86 void W_MachineGun_Attack_Frame(Weapon thiswep, entity actor, .entity weaponentity, int fire)
87 {
88         if(actor.(weaponentity).m_weapon != actor.(weaponentity).m_switchweapon || !weapon_prepareattack_check(thiswep, actor, weaponentity, (fire & 2), -1)) // abort immediately if switching
89         {
90                 w_ready(thiswep, actor, weaponentity, fire);
91                 return;
92         }
93         if(PHYS_INPUT_BUTTON_ATCK(actor))
94         {
95                 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
96                 if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
97                 {
98                         W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
99                         w_ready(thiswep, actor, weaponentity, fire);
100                         return;
101                 }
102                 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
103                 W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity);
104                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
105         }
106         else
107                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), w_ready);
108 }
109
110
111 void W_MachineGun_Attack_Auto(Weapon thiswep, entity actor, .entity weaponentity, int fire)
112 {
113         float machinegun_spread;
114
115         if(!(fire & 1) || !weapon_prepareattack_check(thiswep, actor, weaponentity, false, -1))
116         {
117                 w_ready(thiswep, actor, weaponentity, fire);
118                 return;
119         }
120
121         if(!thiswep.wr_checkammo1(thiswep, actor, weaponentity))
122         if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
123         {
124                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
125                 w_ready(thiswep, actor, weaponentity, fire);
126                 return;
127         }
128
129         W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
130
131         W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
132         if(!autocvar_g_norecoil)
133         {
134                 actor.punchangle_x = random() - 0.5;
135                 actor.punchangle_y = random() - 0.5;
136         }
137
138         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));
139         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);
140
141         actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
142
143         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
144
145         W_MachineGun_MuzzleFlash(actor, weaponentity);
146         W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
147
148         if(autocvar_g_casings >= 2) // casing code
149         {
150                 makevectors(actor.v_angle); // for some reason, this is lost
151                 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);
152         }
153
154         ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
155         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Auto);
156 }
157
158 void W_MachineGun_Attack_Burst(Weapon thiswep, entity actor, .entity weaponentity, int fire)
159 {
160         W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
161         if(!autocvar_g_norecoil)
162         {
163                 actor.punchangle_x = random() - 0.5;
164                 actor.punchangle_y = random() - 0.5;
165         }
166
167         fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, burst_speed), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), WEP_CVAR(machinegun, sustained_force), thiswep.m_id, EFFECT_BULLET);
168
169         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
170
171         W_MachineGun_MuzzleFlash(actor, weaponentity);
172         W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
173
174         if(autocvar_g_casings >= 2) // casing code
175         {
176                 makevectors(actor.v_angle); // for some reason, this is lost
177                 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);
178         }
179
180         actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
181         if(actor.(weaponentity).misc_bulletcounter == 0)
182         {
183                 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, burst_refire2) * W_WeaponRateFactor(actor);
184                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_animtime), w_ready);
185         }
186         else
187         {
188                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
189         }
190
191 }
192
193 METHOD(MachineGun, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
194 {
195     if(vdist(actor.origin - actor.enemy.origin, <, 3000 - bound(0, skill, 10) * 200))
196         PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
197     else
198         PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
199 }
200 METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
201 {
202     // forced reload - wait until the bulletcounter is 0 so a burst loop can finish
203     if(WEP_CVAR(machinegun, reload_ammo)
204     && actor.(weaponentity).clip_load < min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo))
205     && actor.(weaponentity).misc_bulletcounter >= 0)
206     {
207         thiswep.wr_reload(thiswep, actor, weaponentity);
208     }
209     else if(WEP_CVAR(machinegun, mode) == 1)
210     {
211         if(fire & 1)
212         if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
213         {
214             actor.(weaponentity).misc_bulletcounter = 0;
215             W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
216         }
217
218         // You can "shoot" more rounds than what's "used", and vice versa.
219         if(fire & 2)
220         if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
221         {
222             if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
223             if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
224             {
225                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
226                 w_ready(thiswep, actor, weaponentity, fire);
227                 return;
228             }
229  
230             // We don't want to shoot 3 rounds if there's 2 left in the mag, so we'll use a fraction.
231             // Also keep the fraction <= 1 otherwise we'd mag dump in one burst.
232             float burst_fraction = min(1, actor.(weaponentity).clip_load / WEP_CVAR(machinegun, burst_ammo));
233             int to_shoot = floor(WEP_CVAR(machinegun, burst) * burst_fraction);
234
235             // We also don't want to use 3 rounds if there's only 2 left.
236             int to_use = min(WEP_CVAR(machinegun, burst_ammo), actor.(weaponentity).clip_load);
237             W_DecreaseAmmo(thiswep, actor, to_use, weaponentity);
238
239             // Bursting counts up to 0 from a negative.
240             actor.(weaponentity).misc_bulletcounter = -to_shoot;
241             W_MachineGun_Attack_Burst(thiswep, actor, weaponentity, fire);
242         }
243     }
244     else
245     {
246
247         if(fire & 1)
248         if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
249         {
250             actor.(weaponentity).misc_bulletcounter = 1;
251             W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity); // sets attack_finished
252             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
253         }
254
255         if((fire & 2) && WEP_CVAR(machinegun, first))
256         if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
257         {
258             actor.(weaponentity).misc_bulletcounter = 1;
259             W_MachineGun_Attack(thiswep, thiswep.m_id | HITTYPE_SECONDARY, actor, weaponentity); // sets attack_finished
260             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
261         }
262     }
263 }
264 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
265 {
266     float ammo_amount;
267     if(WEP_CVAR(machinegun, mode) == 1)
268         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
269     else
270         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
271
272     if(WEP_CVAR(machinegun, reload_ammo))
273     {
274         if(WEP_CVAR(machinegun, mode) == 1)
275             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
276         else
277             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
278     }
279     return ammo_amount;
280 }
281 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
282 {
283     float ammo_amount;
284     if(WEP_CVAR(machinegun, mode) == 1)
285         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, burst_ammo);
286     else
287         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
288
289     if(WEP_CVAR(machinegun, reload_ammo))
290     {
291         if(WEP_CVAR(machinegun, mode) == 1)
292             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, burst_ammo);
293         else
294             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
295     }
296     return ammo_amount;
297 }
298 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
299 {
300         if(actor.(weaponentity).misc_bulletcounter < 0)
301                 return;
302     W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
303 }
304 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
305 {
306     return WEAPON_THINKING_WITH_PORTALS;
307 }
308 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
309 {
310     if(w_deathtype & HITTYPE_SECONDARY)
311         return WEAPON_MACHINEGUN_MURDER_SNIPE;
312     else
313         return WEAPON_MACHINEGUN_MURDER_SPRAY;
314 }
315
316 #endif
317 #ifdef CSQC
318
319 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
320 {
321     vector org2;
322     org2 = w_org + w_backoff * 2;
323     pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
324     if(!w_issilent)
325         sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);
326 }
327
328 #endif