]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_machinegun.qc
commit message goes here
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / w_machinegun.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(
3 /* WEP_##id  */ MACHINEGUN,
4 /* function  */ W_MachineGun,
5 /* ammotype  */ ammo_nails,
6 /* impulse   */ 3,
7 /* flags     */ WEP_FLAG_RELOADABLE | WEP_TYPE_HITSCAN,
8 /* rating    */ BOT_PICKUP_RATING_MID,
9 /* color     */ '1 1 0',
10 /* model     */ "uzi",
11 /* crosshair */ "gfx/crosshairuzi 0.6",
12 /* refname   */ "uzi",
13 /* wepname   */ _("Machine Gun")
14 );
15
16 #define MACHINEGUN_SETTINGS(w_cvar,w_prop) MACHINEGUN_SETTINGS_LIST(w_cvar, w_prop, MACHINEGUN, machinegun)
17 #define MACHINEGUN_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
18         w_cvar(id, sn, NONE, spread_min) \
19         w_cvar(id, sn, NONE, spread_max) \
20         w_cvar(id, sn, NONE, spread_add) \
21         w_cvar(id, sn, NONE, mode) \
22         w_cvar(id, sn, NONE, first) \
23         w_cvar(id, sn, NONE, first_damage) \
24         w_cvar(id, sn, NONE, first_force) \
25         w_cvar(id, sn, NONE, first_refire) \
26         w_cvar(id, sn, NONE, first_spread) \
27         w_cvar(id, sn, NONE, first_ammo) \
28         w_cvar(id, sn, NONE, solidpenetration) \
29         w_cvar(id, sn, NONE, sustained_damage) \
30         w_cvar(id, sn, NONE, sustained_force) \
31         w_cvar(id, sn, NONE, sustained_refire) \
32         w_cvar(id, sn, NONE, sustained_spread) \
33         w_cvar(id, sn, NONE, sustained_ammo) \
34         w_cvar(id, sn, NONE, burst) \
35         w_cvar(id, sn, NONE, burst_refire) \
36         w_cvar(id, sn, NONE, burst_refire2) \
37         w_cvar(id, sn, NONE, burst_animtime) \
38         w_cvar(id, sn, NONE, burst_speed) \
39         w_cvar(id, sn, NONE, burst_ammo) \
40         w_prop(id, sn, float,  reloading_ammo, reload_ammo) \
41         w_prop(id, sn, float,  reloading_time, reload_time) \
42         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
43         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
44         w_prop(id, sn, string, weaponreplace, weaponreplace) \
45         w_prop(id, sn, float,  weaponstart, weaponstart) \
46         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride)
47
48 #ifdef SVQC
49 MACHINEGUN_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
50 #endif
51 #else
52 #ifdef SVQC
53
54 void spawnfunc_weapon_machinegun()
55 {
56         if(autocvar_sv_q3acompat_machineshotgunswap)
57         if(self.classname != "droppedweapon")
58         {
59                 weapon_defaultspawnfunc(WEP_SHOCKWAVE);
60                 return;
61         }
62         weapon_defaultspawnfunc(WEP_MACHINEGUN);
63 }
64 void spawnfunc_weapon_uzi() { spawnfunc_weapon_machinegun(); }
65
66 void W_MachineGun_MuzzleFlash_Think()
67 {
68         self.frame = self.frame + 2;
69         self.scale = self.scale * 0.5;
70         self.alpha = self.alpha - 0.25;
71         self.nextthink = time + 0.05;
72
73         if (self.alpha <= 0)
74         {
75                 self.think = SUB_Remove;
76                 self.nextthink = time;
77                 self.realowner.muzzle_flash = world;
78                 return;
79         }
80
81 }
82
83 void W_MachineGun_MuzzleFlash()
84 {
85         if (self.muzzle_flash == world)
86                 self.muzzle_flash = spawn();
87
88         // muzzle flash for 1st person view
89         setmodel(self.muzzle_flash, "models/uziflash.md3"); // precision set below
90
91         self.muzzle_flash.scale = 0.75;
92         self.muzzle_flash.think = W_MachineGun_MuzzleFlash_Think;
93         self.muzzle_flash.nextthink = time + 0.02;
94         self.muzzle_flash.frame = 2;
95         self.muzzle_flash.alpha = 0.75;
96         self.muzzle_flash.angles_z = random() * 180;
97         self.muzzle_flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
98         self.muzzle_flash.owner = self.muzzle_flash.realowner = self;
99 }
100
101 void W_MachineGun_Attack(float deathtype)
102 {
103         W_SetupShot (self, TRUE, 0, "weapons/uzi_fire.wav", CH_WEAPON_A, ((self.misc_bulletcounter == 1) ? WEP_CVAR(machinegun, first_damage) : WEP_CVAR(machinegun, sustained_damage)));
104         if (!autocvar_g_norecoil)
105         {
106                 self.punchangle_x = random () - 0.5;
107                 self.punchangle_y = random () - 0.5;
108         }
109
110         // this attack_finished just enforces a cooldown at the end of a burst
111         ATTACK_FINISHED(self) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor();
112
113         if (self.misc_bulletcounter == 1)
114                 fireBullet(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);
115         else
116                 fireBullet(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);
117
118         pointparticles(particleeffectnum("uzi_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
119
120         W_MachineGun_MuzzleFlash();
121         W_AttachToShotorg(self.muzzle_flash, '5 0 0');
122
123         // casing code
124         if (autocvar_g_casings >= 2)
125                 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, self);
126
127         if (self.misc_bulletcounter == 1)
128                 W_DecreaseAmmo(WEP_CVAR(machinegun, first_ammo));
129         else
130                 W_DecreaseAmmo(WEP_CVAR(machinegun, sustained_ammo));
131 }
132
133 // weapon frames
134 void W_MachineGun_Attack_Frame()
135 {
136         if(self.weapon != self.switchweapon) // abort immediately if switching
137         {
138                 w_ready();
139                 return;
140         }
141         if (self.BUTTON_ATCK)
142         {
143                 if (!WEP_ACTION(self.weapon, WR_CHECKAMMO2))
144                 if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
145                 {
146                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
147                         w_ready();
148                         return;
149                 }
150                 self.misc_bulletcounter = self.misc_bulletcounter + 1;
151                 W_MachineGun_Attack(WEP_MACHINEGUN);
152                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
153         }
154         else
155                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), w_ready);
156 }
157
158
159 void W_MachineGun_Attack_Auto()
160 {
161         float machinegun_spread;
162
163         if (!self.BUTTON_ATCK)
164         {
165                 w_ready();
166                 return;
167         }
168
169         if (!WEP_ACTION(self.weapon, WR_CHECKAMMO1))
170         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
171         {
172                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
173                 w_ready();
174                 return;
175         }
176
177         W_DecreaseAmmo(WEP_CVAR(machinegun, sustained_ammo));
178
179         W_SetupShot (self, TRUE, 0, "weapons/uzi_fire.wav", CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage));
180         if (!autocvar_g_norecoil)
181         {
182                 self.punchangle_x = random () - 0.5;
183                 self.punchangle_y = random () - 0.5;
184         }
185
186         machinegun_spread = bound(WEP_CVAR(machinegun, spread_min), WEP_CVAR(machinegun, spread_min) + (WEP_CVAR(machinegun, spread_add) * self.misc_bulletcounter), WEP_CVAR(machinegun, spread_max));
187         fireBullet(w_shotorg, w_shotdir, machinegun_spread, WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), WEP_CVAR(machinegun, sustained_force), WEP_MACHINEGUN, 0);
188
189         self.misc_bulletcounter = self.misc_bulletcounter + 1;
190
191         pointparticles(particleeffectnum("uzi_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
192
193         W_MachineGun_MuzzleFlash();
194         W_AttachToShotorg(self.muzzle_flash, '5 0 0');
195
196         if (autocvar_g_casings >= 2) // casing code
197                 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, self);
198
199         ATTACK_FINISHED(self) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor();
200         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Auto);
201 }
202
203 void W_MachineGun_Attack_Burst()
204 {
205         W_SetupShot (self, TRUE, 0, "weapons/uzi_fire.wav", CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage));
206         if (!autocvar_g_norecoil)
207         {
208                 self.punchangle_x = random () - 0.5;
209                 self.punchangle_y = random () - 0.5;
210         }
211
212         fireBullet(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, 0);
213
214         pointparticles(particleeffectnum("uzi_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
215
216         W_MachineGun_MuzzleFlash();
217         W_AttachToShotorg(self.muzzle_flash, '5 0 0');
218
219         if (autocvar_g_casings >= 2) // casing code
220                 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, self);
221
222         self.misc_bulletcounter = self.misc_bulletcounter + 1;
223         if (self.misc_bulletcounter == 0)
224         {
225                 ATTACK_FINISHED(self) = time + WEP_CVAR(machinegun, burst_refire2) * W_WeaponRateFactor();
226                 weapon_thinkf(WFRAME_FIRE2, WEP_CVAR(machinegun, burst_animtime), w_ready);
227         }
228         else
229         {
230                 weapon_thinkf(WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
231         }
232
233 }
234
235 float W_MachineGun(float req)
236 {
237         float ammo_amount;
238         switch(req)
239         {
240                 case WR_AIM:
241                 {
242                         if(vlen(self.origin-self.enemy.origin) < 3000 - bound(0, skill, 10) * 200)
243                                 self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, FALSE);
244                         else
245                                 self.BUTTON_ATCK2 = bot_aim(1000000, 0, 0.001, FALSE);
246                         
247                         return TRUE;
248                 }
249                 case WR_THINK:
250                 {
251                         if(WEP_CVAR(machinegun, reload_ammo) && self.clip_load < min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo))) // forced reload
252                                 WEP_ACTION(self.weapon, WR_RELOAD);
253                         else if(WEP_CVAR(machinegun, mode) == 1)
254                         {
255                                 if (self.BUTTON_ATCK)
256                                 if (weapon_prepareattack(0, 0))
257                                 {
258                                         self.misc_bulletcounter = 0;
259                                         W_MachineGun_Attack_Auto();
260                                 }
261
262                                 if(self.BUTTON_ATCK2)
263                                 if(weapon_prepareattack(1, 0))
264                                 {
265                                         if (!WEP_ACTION(self.weapon, WR_CHECKAMMO2))
266                                         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
267                                         {
268                                                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
269                                                 w_ready();
270                                                 return FALSE;
271                                         }
272
273                                         W_DecreaseAmmo(WEP_CVAR(machinegun, burst_ammo));
274
275                                         self.misc_bulletcounter = WEP_CVAR(machinegun, burst) * -1;
276                                         W_MachineGun_Attack_Burst();
277                                 }
278                         }
279                         else
280                         {
281
282                                 if (self.BUTTON_ATCK)
283                                 if (weapon_prepareattack(0, 0))
284                                 {
285                                         self.misc_bulletcounter = 1;
286                                         W_MachineGun_Attack(WEP_MACHINEGUN); // sets attack_finished
287                                         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
288                                 }
289
290                                 if (self.BUTTON_ATCK2 && WEP_CVAR(machinegun, first))
291                                 if (weapon_prepareattack(1, 0))
292                                 {
293                                         self.misc_bulletcounter = 1;
294                                         W_MachineGun_Attack(WEP_MACHINEGUN | HITTYPE_SECONDARY); // sets attack_finished
295                                         weapon_thinkf(WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
296                                 }
297                         }
298                         
299                         return TRUE;
300                 }
301                 case WR_INIT:
302                 {
303                         precache_model ("models/uziflash.md3");
304                         precache_model ("models/weapons/g_uzi.md3");
305                         precache_model ("models/weapons/v_uzi.md3");
306                         precache_model ("models/weapons/h_uzi.iqm");
307                         precache_sound ("weapons/uzi_fire.wav");
308                         MACHINEGUN_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP)
309                         return TRUE;
310                 }
311                 case WR_CHECKAMMO1:
312                 {
313                         if(WEP_CVAR(machinegun, mode) == 1)
314                                 ammo_amount = self.WEP_AMMO(MACHINEGUN) >= WEP_CVAR(machinegun, sustained_ammo);
315                         else
316                                 ammo_amount = self.WEP_AMMO(MACHINEGUN) >= WEP_CVAR(machinegun, first_ammo);
317
318                         if(WEP_CVAR(machinegun, reload_ammo))
319                         {
320                                 if(WEP_CVAR(machinegun, mode) == 1)
321                                         ammo_amount += self.(weapon_load[WEP_MACHINEGUN]) >= WEP_CVAR(machinegun, sustained_ammo);
322                                 else
323                                         ammo_amount += self.(weapon_load[WEP_MACHINEGUN]) >= WEP_CVAR(machinegun, first_ammo);
324                         }
325                         return ammo_amount;
326                 }
327                 case WR_CHECKAMMO2:
328                 {
329                         if(WEP_CVAR(machinegun, mode) == 1)
330                                 ammo_amount = self.WEP_AMMO(MACHINEGUN) >= WEP_CVAR(machinegun, burst_ammo);
331                         else
332                                 ammo_amount = self.WEP_AMMO(MACHINEGUN) >= WEP_CVAR(machinegun, first_ammo);
333
334                         if(WEP_CVAR(machinegun, reload_ammo))
335                         {
336                                 if(WEP_CVAR(machinegun, mode) == 1)
337                                         ammo_amount += self.(weapon_load[WEP_MACHINEGUN]) >= WEP_CVAR(machinegun, burst_ammo);
338                                 else
339                                         ammo_amount += self.(weapon_load[WEP_MACHINEGUN]) >= WEP_CVAR(machinegun, first_ammo);
340                         }
341                         return ammo_amount;
342                 }
343                 case WR_CONFIG:
344                 {
345                         MACHINEGUN_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
346                         return TRUE;
347                 }
348                 case WR_RELOAD:
349                 {
350                         W_Reload(min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), "weapons/reload.wav");
351                         return TRUE;
352                 }
353                 case WR_SUICIDEMESSAGE:
354                 {
355                         return WEAPON_THINKING_WITH_PORTALS;
356                 }
357                 case WR_KILLMESSAGE:
358                 {
359                         if(w_deathtype & HITTYPE_SECONDARY)
360                                 return WEAPON_MACHINEGUN_MURDER_SNIPE;
361                         else
362                                 return WEAPON_MACHINEGUN_MURDER_SPRAY;
363                 }
364         }
365         return TRUE;
366 }
367 #endif
368 #ifdef CSQC
369 float W_MachineGun(float req)
370 {
371         switch(req)
372         {
373                 case WR_IMPACTEFFECT:
374                 {
375                         vector org2;
376                         org2 = w_org + w_backoff * 2;
377                         pointparticles(particleeffectnum("machinegun_impact"), org2, w_backoff * 1000, 1);
378                         if(!w_issilent)
379                                 if(w_random < 0.05)
380                                         sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTN_NORM);
381                                 else if(w_random < 0.1)
382                                         sound(self, CH_SHOTS, "weapons/ric2.wav", VOL_BASE, ATTN_NORM);
383                                 else if(w_random < 0.2)
384                                         sound(self, CH_SHOTS, "weapons/ric3.wav", VOL_BASE, ATTN_NORM);
385                                         
386                         return TRUE;
387                 }
388                 case WR_INIT:
389                 {
390                         precache_sound("weapons/ric1.wav");
391                         precache_sound("weapons/ric2.wav");
392                         precache_sound("weapons/ric3.wav");
393                         return TRUE;
394                 }
395                 case WR_ZOOMRETICLE:
396                 {
397                         // no weapon specific image for this weapon
398                         return FALSE;
399                 }
400         }
401         return TRUE;
402 }
403 #endif
404 #endif