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