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