5a55b7e8b51257d1a1e3fec9c941f3fd33c2172f
[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             // We don't want to shoot 3 rounds if there's 2 left in the mag, so we'll use a fraction.
222             // Also keep the fraction <= 1 otherwise we'd mag dump in one burst.
223             float burst_fraction = min(1, actor.(weaponentity).clip_load / WEP_CVAR(machinegun, burst_ammo));
224             int to_shoot = floor(WEP_CVAR(machinegun, burst) * burst_fraction);
225
226             // We also don't want to use 3 rounds if there's only 2 left.
227             int to_use = min(WEP_CVAR(machinegun, burst_ammo), actor.(weaponentity).clip_load);
228             W_DecreaseAmmo(thiswep, actor, to_use, weaponentity);
229
230             // Bursting counts up to 0 from a negative.
231             actor.(weaponentity).misc_bulletcounter = -to_shoot;
232             W_MachineGun_Attack_Burst(thiswep, actor, weaponentity, fire);
233         }
234     }
235     else
236     {
237
238         if(fire & 1)
239         if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
240         {
241             actor.(weaponentity).misc_bulletcounter = 1;
242             W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity); // sets attack_finished
243             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
244         }
245
246         if((fire & 2) && WEP_CVAR(machinegun, first))
247         if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
248         {
249             actor.(weaponentity).misc_bulletcounter = 1;
250             W_MachineGun_Attack(thiswep, thiswep.m_id | HITTYPE_SECONDARY, actor, weaponentity); // sets attack_finished
251             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
252         }
253     }
254 }
255 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
256 {
257     float ammo_amount;
258     if(WEP_CVAR(machinegun, mode) == 1)
259         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
260     else
261         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
262
263     if(WEP_CVAR(machinegun, reload_ammo))
264     {
265         if(WEP_CVAR(machinegun, mode) == 1)
266             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
267         else
268             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
269     }
270     return ammo_amount;
271 }
272 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
273 {
274     float ammo_amount;
275     float burst_ammo_per_shot = WEP_CVAR(machinegun, burst_ammo) / WEP_CVAR(machinegun, burst);
276     if(WEP_CVAR(machinegun, mode) == 1)
277         ammo_amount = GetResource(actor, thiswep.ammo_type) >= burst_ammo_per_shot;
278     else
279         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
280
281     if(WEP_CVAR(machinegun, reload_ammo))
282     {
283         if(WEP_CVAR(machinegun, mode) == 1)
284             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= burst_ammo_per_shot;
285         else
286             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
287     }
288     return ammo_amount;
289 }
290 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
291 {
292         if(actor.(weaponentity).misc_bulletcounter < 0)
293                 return;
294     W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
295 }
296 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
297 {
298     return WEAPON_THINKING_WITH_PORTALS;
299 }
300 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
301 {
302     if(w_deathtype & HITTYPE_SECONDARY)
303         return WEAPON_MACHINEGUN_MURDER_SNIPE;
304     else
305         return WEAPON_MACHINEGUN_MURDER_SPRAY;
306 }
307
308 #endif
309 #ifdef CSQC
310
311 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
312 {
313     vector org2;
314     org2 = w_org + w_backoff * 2;
315     pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
316     if(!w_issilent)
317         sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);
318 }
319
320 #endif