aaf4900fe2e683029c66ccd93c2ae932c2bae3ec
[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     if(WEP_CVAR(machinegun, reload_ammo) && actor.(weaponentity).clip_load < min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo))) { // forced reload
194         thiswep.wr_reload(thiswep, actor, weaponentity);
195     } else
196     if(WEP_CVAR(machinegun, mode) == 1)
197     {
198         if(fire & 1)
199         if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
200         {
201             actor.(weaponentity).misc_bulletcounter = 0;
202             W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
203         }
204
205         if(fire & 2)
206         if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
207         {
208             if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
209             if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
210             {
211                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
212                 w_ready(thiswep, actor, weaponentity, fire);
213                 return;
214             }
215
216             W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, burst_ammo), weaponentity);
217
218             actor.(weaponentity).misc_bulletcounter = WEP_CVAR(machinegun, burst) * -1;
219             W_MachineGun_Attack_Burst(thiswep, actor, weaponentity, fire);
220         }
221     }
222     else
223     {
224
225         if(fire & 1)
226         if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
227         {
228             actor.(weaponentity).misc_bulletcounter = 1;
229             W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity); // sets attack_finished
230             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
231         }
232
233         if((fire & 2) && WEP_CVAR(machinegun, first))
234         if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
235         {
236             actor.(weaponentity).misc_bulletcounter = 1;
237             W_MachineGun_Attack(thiswep, thiswep.m_id | HITTYPE_SECONDARY, actor, weaponentity); // sets attack_finished
238             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
239         }
240     }
241 }
242 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
243 {
244     float ammo_amount;
245     if(WEP_CVAR(machinegun, mode) == 1)
246         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
247     else
248         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
249
250     if(WEP_CVAR(machinegun, reload_ammo))
251     {
252         if(WEP_CVAR(machinegun, mode) == 1)
253             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
254         else
255             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
256     }
257     return ammo_amount;
258 }
259 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
260 {
261     float ammo_amount;
262     if(WEP_CVAR(machinegun, mode) == 1)
263         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, burst_ammo);
264     else
265         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
266
267     if(WEP_CVAR(machinegun, reload_ammo))
268     {
269         if(WEP_CVAR(machinegun, mode) == 1)
270             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, burst_ammo);
271         else
272             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
273     }
274     return ammo_amount;
275 }
276 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
277 {
278     W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
279 }
280 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
281 {
282     return WEAPON_THINKING_WITH_PORTALS;
283 }
284 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
285 {
286     if(w_deathtype & HITTYPE_SECONDARY)
287         return WEAPON_MACHINEGUN_MURDER_SNIPE;
288     else
289         return WEAPON_MACHINEGUN_MURDER_SPRAY;
290 }
291
292 #endif
293 #ifdef CSQC
294
295 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
296 {
297     vector org2;
298     org2 = w_org + w_backoff * 2;
299     pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
300     if(!w_issilent)
301         sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);
302 }
303
304 #endif