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