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