d874304432182434b9be139c905ea9a8ae2865f1
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / overkill / okmachinegun.qc
1 #include "okmachinegun.qh"
2
3 #ifdef SVQC
4
5 spawnfunc(weapon_okmachinegun)
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_OVERKILL_MACHINEGUN);
14 }
15
16 void W_OverkillMachineGun_Attack(Weapon thiswep, int deathtype, entity actor, .entity weaponentity)
17 {
18         W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, ((actor.(weaponentity).misc_bulletcounter == 1) ? WEP_CVAR(okmachinegun, first_damage) : WEP_CVAR(okmachinegun, sustained_damage)));
19         if(!autocvar_g_norecoil)
20         {
21                 actor.punchangle_x = random() - 0.5;
22                 actor.punchangle_y = random() - 0.5;
23         }
24         int slot = weaponslot(weaponentity);
25         // this attack_finished just enforces a cooldown at the end of a burst
26         ATTACK_FINISHED(actor, slot) = time + WEP_CVAR(okmachinegun, first_refire) * W_WeaponRateFactor(actor);
27
28         if(actor.(weaponentity).misc_bulletcounter == 1)
29                 fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(okmachinegun, first_spread), WEP_CVAR(okmachinegun, solidpenetration), WEP_CVAR(okmachinegun, first_damage), WEP_CVAR(okmachinegun, first_force), deathtype, 0);
30         else
31                 fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(okmachinegun, sustained_spread), WEP_CVAR(okmachinegun, solidpenetration), WEP_CVAR(okmachinegun, sustained_damage), WEP_CVAR(okmachinegun, sustained_force), deathtype, 0);
32
33         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
34
35         W_MachineGun_MuzzleFlash(actor, weaponentity);
36         W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
37
38         // casing code
39         if(autocvar_g_casings >= 2)
40         {
41                 makevectors(actor.v_angle); // for some reason, this is lost
42                 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);
43         }
44
45         if(actor.(weaponentity).misc_bulletcounter == 1)
46                 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(okmachinegun, first_ammo), weaponentity);
47         else
48                 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(okmachinegun, sustained_ammo), weaponentity);
49 }
50
51 // weapon frames
52 void W_OverkillMachineGun_Attack_Frame(Weapon thiswep, entity actor, .entity weaponentity, int fire)
53 {
54         if(actor.(weaponentity).m_weapon != actor.(weaponentity).m_switchweapon) // abort immediately if switching
55         {
56                 w_ready(thiswep, actor, weaponentity, fire);
57                 return;
58         }
59         if(PHYS_INPUT_BUTTON_ATCK(actor))
60         {
61                 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
62                 if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
63                 {
64                         W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
65                         w_ready(thiswep, actor, weaponentity, fire);
66                         return;
67                 }
68                 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
69                 W_OverkillMachineGun_Attack(WEP_OVERKILL_MACHINEGUN, WEP_OVERKILL_MACHINEGUN.m_id, actor, weaponentity);
70                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(okmachinegun, sustained_refire), W_OverkillMachineGun_Attack_Frame);
71         }
72         else
73                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(okmachinegun, sustained_refire), w_ready);
74 }
75
76 void W_OverkillMachineGun_Attack_Auto(Weapon thiswep, entity actor, .entity weaponentity, int fire)
77 {
78         float okmachinegun_spread;
79
80         if(!(fire & 1))
81         {
82                 w_ready(thiswep, actor, weaponentity, fire);
83                 return;
84         }
85
86         if(!thiswep.wr_checkammo1(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
94         W_DecreaseAmmo(WEP_OVERKILL_MACHINEGUN, actor, WEP_CVAR(okmachinegun, sustained_ammo), weaponentity);
95
96         W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(okmachinegun, sustained_damage));
97         if(!autocvar_g_norecoil)
98         {
99                 actor.punchangle_x = random() - 0.5;
100                 actor.punchangle_y = random() - 0.5;
101         }
102
103         okmachinegun_spread = bound(WEP_CVAR(okmachinegun, spread_min), WEP_CVAR(okmachinegun, spread_min) + (WEP_CVAR(okmachinegun, spread_add) * actor.(weaponentity).misc_bulletcounter), WEP_CVAR(okmachinegun, spread_max));
104         fireBullet(actor, weaponentity, w_shotorg, w_shotdir, okmachinegun_spread, WEP_CVAR(okmachinegun, solidpenetration), WEP_CVAR(okmachinegun, sustained_damage), WEP_CVAR(okmachinegun, sustained_force), WEP_OVERKILL_MACHINEGUN.m_id, 0);
105
106         actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
107
108         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
109
110         W_MachineGun_MuzzleFlash(actor, weaponentity);
111         W_AttachToShotorg(actor, weaponentity, actor.(weaponentity).muzzle_flash, '5 0 0');
112
113         if(autocvar_g_casings >= 2) // casing code
114         {
115                 makevectors(actor.v_angle); // for some reason, this is lost
116                 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);
117         }
118
119         int slot = weaponslot(weaponentity);
120         ATTACK_FINISHED(actor, slot) = time + WEP_CVAR(okmachinegun, first_refire) * W_WeaponRateFactor(actor);
121         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(okmachinegun, sustained_refire), W_OverkillMachineGun_Attack_Auto);
122 }
123
124 METHOD(OverkillMachineGun, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
125 {
126         if(vdist(actor.origin - actor.enemy.origin, <, 3000 - bound(0, skill, 10) * 200))
127                 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
128         else
129                 PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
130 }
131
132 METHOD(OverkillMachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
133 {
134         if ((WEP_CVAR_SEC(okmachinegun, refire_type) == 1) && (fire & 2) && (time >= actor.jump_interval))
135         {
136                 // Secondary uses it's own refire timer if refire_type is 1.
137                 actor.jump_interval = time + WEP_CVAR_SEC(okmachinegun, refire) * W_WeaponRateFactor(actor);
138                 // Ugly hack to reuse the fire mode of the blaster.
139                 makevectors(actor.v_angle);
140                 Weapon oldwep = actor.(weaponentity).m_weapon; // we can't avoid this hack
141                 actor.(weaponentity).m_weapon = WEP_BLASTER;
142                 W_Blaster_Attack(
143                         actor,
144                         weaponentity,
145                         WEP_BLASTER.m_id | HITTYPE_SECONDARY,
146                         WEP_CVAR_SEC(okmachinegun, shotangle),
147                         WEP_CVAR_SEC(okmachinegun, damage),
148                         WEP_CVAR_SEC(okmachinegun, edgedamage),
149                         WEP_CVAR_SEC(okmachinegun, radius),
150                         WEP_CVAR_SEC(okmachinegun, force),
151                         WEP_CVAR_SEC(okmachinegun, speed),
152                         WEP_CVAR_SEC(okmachinegun, spread),
153                         WEP_CVAR_SEC(okmachinegun, delay),
154                         WEP_CVAR_SEC(okmachinegun, lifetime)
155                 );
156                 actor.(weaponentity).m_weapon = oldwep;
157                 if ((actor.(weaponentity).wframe == WFRAME_IDLE) ||
158                         (actor.(weaponentity).wframe == WFRAME_FIRE2))
159                 {
160                         // Set secondary fire animation.
161                         vector a = '0 0 0';
162                         actor.(weaponentity).wframe = WFRAME_FIRE2;
163                         a = actor.(weaponentity).anim_fire2;
164                         a.z *= g_weaponratefactor;
165                         FOREACH_CLIENT(true, LAMBDA(
166                                 if (it == actor || (IS_SPEC(it) && it.enemy == actor))
167                                 {
168                                         wframe_send(it, actor.(weaponentity), a, true);
169                                 }
170                         ));
171                         animdecide_setaction(actor, ANIMACTION_SHOOT, true);
172                 }
173         }
174         if (WEP_CVAR(okmachinegun, reload_ammo) && actor.(weaponentity).clip_load < min(max(WEP_CVAR(okmachinegun, sustained_ammo), WEP_CVAR(okmachinegun, first_ammo)), WEP_CVAR(okmachinegun, burst_ammo)))
175         {
176                 // Forced reload
177                 thiswep.wr_reload(thiswep, actor, weaponentity);
178                 return;
179         }
180         if (WEP_CVAR(okmachinegun, mode) == 1)
181         {
182                 if(fire & 1) // Primary attack
183                 {
184                         if (!weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
185                         {
186                                 return;
187                         }
188                         actor.(weaponentity).misc_bulletcounter = 0;
189                         W_OverkillMachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
190                         return;
191                 }
192                 if ((fire & 2) && (WEP_CVAR_SEC(okmachinegun, refire_type) == 0)) // Secondary attack
193                 {
194                         if (!weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(okmachinegun, refire)))
195                         {
196                                 return;
197                         }
198                         // ugly instagib hack to reuse the fire mode of the laser
199                         makevectors(actor.v_angle);
200                         Weapon oldwep = actor.(weaponentity).m_weapon; // we can't avoid this hack
201                         actor.(weaponentity).m_weapon = WEP_BLASTER;
202                         W_Blaster_Attack(
203                                 actor,
204                                 weaponentity,
205                                 WEP_BLASTER.m_id | HITTYPE_SECONDARY,
206                                 WEP_CVAR_SEC(okmachinegun, shotangle),
207                                 WEP_CVAR_SEC(okmachinegun, damage),
208                                 WEP_CVAR_SEC(okmachinegun, edgedamage),
209                                 WEP_CVAR_SEC(okmachinegun, radius),
210                                 WEP_CVAR_SEC(okmachinegun, force),
211                                 WEP_CVAR_SEC(okmachinegun, speed),
212                                 WEP_CVAR_SEC(okmachinegun, spread),
213                                 WEP_CVAR_SEC(okmachinegun, delay),
214                                 WEP_CVAR_SEC(okmachinegun, lifetime)
215                         );
216                         actor.(weaponentity).m_weapon = oldwep;
217                         weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(okmachinegun, animtime), w_ready);
218                 }
219         }
220         else
221         {
222                 if (fire & 1) // Primary attack
223                 {
224                         if (!weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
225                         {
226                                 return;
227                         }
228                         actor.(weaponentity).misc_bulletcounter = 1;
229                         W_OverkillMachineGun_Attack(WEP_OVERKILL_MACHINEGUN, WEP_OVERKILL_MACHINEGUN.m_id, actor, weaponentity); // sets attack_finished
230                         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(okmachinegun, sustained_refire), W_OverkillMachineGun_Attack_Frame);
231                         return;
232                 }
233                 if ((fire & 2) && (WEP_CVAR_SEC(okmachinegun, refire_type) == 0)) // Secondary attack
234                 {
235                         if (!weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(okmachinegun, refire)))
236                         {
237                                 return;
238                         }
239                         // ugly instagib hack to reuse the fire mode of the laser
240                         makevectors(actor.v_angle);
241                         Weapon oldwep = actor.(weaponentity).m_weapon; // we can't avoid this hack
242                         actor.(weaponentity).m_weapon = WEP_BLASTER;
243                         W_Blaster_Attack(
244                                 actor,
245                                 weaponentity,
246                                 WEP_BLASTER.m_id | HITTYPE_SECONDARY,
247                                 WEP_CVAR_SEC(okmachinegun, shotangle),
248                                 WEP_CVAR_SEC(okmachinegun, damage),
249                                 WEP_CVAR_SEC(okmachinegun, edgedamage),
250                                 WEP_CVAR_SEC(okmachinegun, radius),
251                                 WEP_CVAR_SEC(okmachinegun, force),
252                                 WEP_CVAR_SEC(okmachinegun, speed),
253                                 WEP_CVAR_SEC(okmachinegun, spread),
254                                 WEP_CVAR_SEC(okmachinegun, delay),
255                                 WEP_CVAR_SEC(okmachinegun, lifetime)
256                         );
257                         actor.(weaponentity).m_weapon = oldwep;
258                         weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(okmachinegun, animtime), w_ready);
259                 }
260         }
261 }
262
263 METHOD(OverkillMachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
264 {
265         float ammo_amount;
266         if (WEP_CVAR(okmachinegun, mode) == 1)
267         {
268                 ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR(okmachinegun, sustained_ammo);
269         }
270         else
271         {
272                 ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR(okmachinegun, first_ammo);
273         }
274         if (WEP_CVAR(okmachinegun, reload_ammo))
275         {
276                 if (WEP_CVAR(okmachinegun, mode) == 1)
277                 {
278                         ammo_amount += actor.(weaponentity).(weapon_load[WEP_OVERKILL_MACHINEGUN.m_id]) >= WEP_CVAR(okmachinegun, sustained_ammo);
279                 }
280                 else
281                 {
282                         ammo_amount += actor.(weaponentity).(weapon_load[WEP_OVERKILL_MACHINEGUN.m_id]) >= WEP_CVAR(okmachinegun, first_ammo);
283                 }
284         }
285         return ammo_amount;
286 }
287
288 METHOD(OverkillMachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
289 {
290         float ammo_amount;
291         if (WEP_CVAR(okmachinegun, mode) == 1)
292         {
293                 ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR(okmachinegun, burst_ammo);
294         }
295         else
296         {
297                 ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR(okmachinegun, first_ammo);
298         }
299         if (WEP_CVAR(okmachinegun, reload_ammo))
300         {
301                 if (WEP_CVAR(okmachinegun, mode) == 1)
302                 {
303                         ammo_amount += actor.(weaponentity).(weapon_load[WEP_OVERKILL_MACHINEGUN.m_id]) >= WEP_CVAR(okmachinegun, burst_ammo);
304                 }
305                 else
306                 {
307                         ammo_amount += actor.(weaponentity).(weapon_load[WEP_OVERKILL_MACHINEGUN.m_id]) >= WEP_CVAR(okmachinegun, first_ammo);
308                 }
309         }
310         return ammo_amount;
311 }
312
313 METHOD(OverkillMachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
314 {
315         W_Reload(actor, weaponentity, min(max(WEP_CVAR(okmachinegun, sustained_ammo), WEP_CVAR(okmachinegun, first_ammo)), WEP_CVAR(okmachinegun, burst_ammo)), SND_RELOAD);
316 }
317
318 METHOD(OverkillMachineGun, wr_suicidemessage, Notification(entity thiswep))
319 {
320         return WEAPON_THINKING_WITH_PORTALS;
321 }
322
323 METHOD(OverkillMachineGun, wr_killmessage, Notification(entity thiswep))
324 {
325         if(w_deathtype & HITTYPE_SECONDARY)
326                 return WEAPON_MACHINEGUN_MURDER_SNIPE;
327         else
328                 return WEAPON_MACHINEGUN_MURDER_SPRAY;
329 }
330
331 #endif
332 #ifdef CSQC
333
334 METHOD(OverkillMachineGun, wr_impacteffect, void(entity thiswep, entity actor))
335 {
336         vector org2;
337         org2 = w_org + w_backoff * 2;
338         pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
339         if(!w_issilent)
340                 sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);
341 }
342
343 #endif
344