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