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