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