]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/machinegun.qc
Notifications: relocate
[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, m_name, string, _("MachineGun"));
17
18 #define X(BEGIN, P, END, class, prefix) \
19         BEGIN(class) \
20                 P(class, prefix, burst, float, NONE) \
21                 P(class, prefix, burst_ammo, float, NONE) \
22                 P(class, prefix, burst_animtime, float, NONE) \
23                 P(class, prefix, burst_refire2, float, NONE) \
24                 P(class, prefix, burst_refire, float, NONE) \
25                 P(class, prefix, burst_speed, float, NONE) \
26                 P(class, prefix, first, float, NONE) \
27                 P(class, prefix, first_ammo, float, NONE) \
28                 P(class, prefix, first_damage, float, NONE) \
29                 P(class, prefix, first_force, float, NONE) \
30                 P(class, prefix, first_refire, float, NONE) \
31                 P(class, prefix, first_spread, float, NONE) \
32                 P(class, prefix, mode, float, NONE) \
33                 P(class, prefix, reload_ammo, float, NONE) \
34         P(class, prefix, reload_time, float, NONE) \
35                 P(class, prefix, solidpenetration, float, NONE) \
36                 P(class, prefix, spread_add, float, NONE) \
37                 P(class, prefix, spread_max, float, NONE) \
38                 P(class, prefix, spread_min, float, NONE) \
39                 P(class, prefix, sustained_ammo, float, NONE) \
40                 P(class, prefix, sustained_damage, float, NONE) \
41                 P(class, prefix, sustained_force, float, NONE) \
42                 P(class, prefix, sustained_refire, float, NONE) \
43                 P(class, prefix, sustained_spread, float, NONE) \
44         P(class, prefix, switchdelay_drop, float, NONE) \
45         P(class, prefix, switchdelay_raise, float, NONE) \
46         P(class, prefix, weaponreplace, string,NONE) \
47         P(class, prefix, weaponstartoverride, float, NONE) \
48         P(class, prefix, weaponstart, float, NONE) \
49         P(class, prefix, weaponthrowable, float, NONE) \
50         END()
51     W_PROPS(X, MachineGun, machinegun)
52 #undef X
53
54 ENDCLASS(MachineGun)
55 REGISTER_WEAPON(MACHINEGUN, machinegun, NEW(MachineGun));
56
57
58 #endif
59 #ifdef IMPLEMENTATION
60 #ifdef SVQC
61
62 spawnfunc(weapon_machinegun)
63 {
64         if(autocvar_sv_q3acompat_machineshotgunswap)
65         if(self.classname != "droppedweapon")
66         {
67                 weapon_defaultspawnfunc(this, WEP_SHOCKWAVE);
68                 return;
69         }
70         weapon_defaultspawnfunc(this, WEP_MACHINEGUN);
71 }
72 spawnfunc(weapon_uzi) { spawnfunc_weapon_machinegun(this); }
73
74 void W_MachineGun_MuzzleFlash_Think()
75 {SELFPARAM();
76         this.frame += 2;
77         this.scale *= 0.5;
78         this.alpha -= 0.25;
79         this.nextthink = time + 0.05;
80
81         if(this.alpha <= 0)
82         {
83                 this.think = SUB_Remove_self;
84                 this.nextthink = time;
85                 this.realowner.muzzle_flash = world;
86                 return;
87         }
88
89 }
90
91 void W_MachineGun_MuzzleFlash()
92 {SELFPARAM();
93         if(self.muzzle_flash == world)
94                 self.muzzle_flash = spawn();
95
96         // muzzle flash for 1st person view
97         setmodel(self.muzzle_flash, MDL_MACHINEGUN_MUZZLEFLASH); // precision set below
98
99         self.muzzle_flash.scale = 0.75;
100         self.muzzle_flash.think = W_MachineGun_MuzzleFlash_Think;
101         self.muzzle_flash.nextthink = time + 0.02;
102         self.muzzle_flash.frame = 2;
103         self.muzzle_flash.alpha = 0.75;
104         self.muzzle_flash.angles_z = random() * 180;
105         self.muzzle_flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
106         self.muzzle_flash.owner = self.muzzle_flash.realowner = self;
107 }
108
109 void W_MachineGun_Attack(Weapon thiswep, int deathtype, .entity weaponentity)
110 {SELFPARAM();
111         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)));
112         if(!autocvar_g_norecoil)
113         {
114                 self.punchangle_x = random() - 0.5;
115                 self.punchangle_y = random() - 0.5;
116         }
117         int slot = weaponslot(weaponentity);
118         // this attack_finished just enforces a cooldown at the end of a burst
119         ATTACK_FINISHED(self, slot) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor();
120
121         if(self.misc_bulletcounter == 1)
122                 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);
123         else
124                 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);
125
126         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
127
128         W_MachineGun_MuzzleFlash();
129         W_AttachToShotorg(self, self.muzzle_flash, '5 0 0');
130
131         // casing code
132         if(autocvar_g_casings >= 2)
133                 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);
134
135         if(self.misc_bulletcounter == 1)
136                 W_DecreaseAmmo(thiswep, self, WEP_CVAR(machinegun, first_ammo));
137         else
138                 W_DecreaseAmmo(thiswep, self, WEP_CVAR(machinegun, sustained_ammo));
139 }
140
141 // weapon frames
142 void W_MachineGun_Attack_Frame(Weapon thiswep, entity actor, .entity weaponentity, int fire)
143 {
144         if(PS(actor).m_weapon != PS(actor).m_switchweapon) // abort immediately if switching
145         {
146                 w_ready(thiswep, actor, weaponentity, fire);
147                 return;
148         }
149         if(actor.BUTTON_ATCK)
150         {
151                 if(!thiswep.wr_checkammo2(thiswep))
152                 if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
153                 {
154                         W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
155                         w_ready(thiswep, actor, weaponentity, fire);
156                         return;
157                 }
158                 actor.misc_bulletcounter = actor.misc_bulletcounter + 1;
159                 W_MachineGun_Attack(WEP_MACHINEGUN, WEP_MACHINEGUN.m_id, weaponentity);
160                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
161         }
162         else
163                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), w_ready);
164 }
165
166
167 void W_MachineGun_Attack_Auto(Weapon thiswep, entity actor, .entity weaponentity, int fire)
168 {
169         float machinegun_spread;
170
171         if(!(fire & 1))
172         {
173                 w_ready(thiswep, actor, weaponentity, fire);
174                 return;
175         }
176
177         if(!thiswep.wr_checkammo1(thiswep))
178         if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
179         {
180                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
181                 w_ready(thiswep, actor, weaponentity, fire);
182                 return;
183         }
184
185         W_DecreaseAmmo(WEP_MACHINEGUN, actor, WEP_CVAR(machinegun, sustained_ammo));
186
187         W_SetupShot(actor, true, 0, SND(UZI_FIRE), CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage));
188         if(!autocvar_g_norecoil)
189         {
190                 actor.punchangle_x = random() - 0.5;
191                 actor.punchangle_y = random() - 0.5;
192         }
193
194         machinegun_spread = bound(WEP_CVAR(machinegun, spread_min), WEP_CVAR(machinegun, spread_min) + (WEP_CVAR(machinegun, spread_add) * actor.misc_bulletcounter), WEP_CVAR(machinegun, spread_max));
195         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);
196
197         actor.misc_bulletcounter = actor.misc_bulletcounter + 1;
198
199         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
200
201         W_MachineGun_MuzzleFlash();
202         W_AttachToShotorg(actor, actor.muzzle_flash, '5 0 0');
203
204         if(autocvar_g_casings >= 2) // casing code
205                 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, actor);
206
207         int slot = weaponslot(weaponentity);
208         ATTACK_FINISHED(actor, slot) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor();
209         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Auto);
210 }
211
212 void W_MachineGun_Attack_Burst(Weapon thiswep, entity actor, .entity weaponentity, int fire)
213 {
214         W_SetupShot(actor, true, 0, SND(UZI_FIRE), CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage));
215         if(!autocvar_g_norecoil)
216         {
217                 actor.punchangle_x = random() - 0.5;
218                 actor.punchangle_y = random() - 0.5;
219         }
220
221         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);
222
223         Send_Effect(EFFECT_MACHINEGUN_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
224
225         W_MachineGun_MuzzleFlash();
226         W_AttachToShotorg(actor, actor.muzzle_flash, '5 0 0');
227
228         if(autocvar_g_casings >= 2) // casing code
229                 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, actor);
230
231         actor.misc_bulletcounter = actor.misc_bulletcounter + 1;
232         if(actor.misc_bulletcounter == 0)
233         {
234                 int slot = weaponslot(weaponentity);
235                 ATTACK_FINISHED(actor, slot) = time + WEP_CVAR(machinegun, burst_refire2) * W_WeaponRateFactor();
236                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_animtime), w_ready);
237         }
238         else
239         {
240                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
241         }
242
243 }
244
245                 METHOD(MachineGun, wr_aim, void(entity thiswep))
246                 {
247                         if(vdist(self.origin - self.enemy.origin, <, 3000 - bound(0, skill, 10) * 200))
248                                 self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, false);
249                         else
250                                 self.BUTTON_ATCK2 = bot_aim(1000000, 0, 0.001, false);
251                 }
252                 METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
253                 {
254                         if(WEP_CVAR(machinegun, reload_ammo) && actor.clip_load < min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo))) { // forced reload
255                                 thiswep.wr_reload(thiswep, actor, weaponentity);
256                         } else
257                         if(WEP_CVAR(machinegun, mode) == 1)
258                         {
259                                 if(fire & 1)
260                                 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
261                                 {
262                                         actor.misc_bulletcounter = 0;
263                                         W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
264                                 }
265
266                                 if(fire & 2)
267                                 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
268                                 {
269                                         if(!thiswep.wr_checkammo2(thiswep))
270                                         if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
271                                         {
272                                                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
273                                                 w_ready(thiswep, actor, weaponentity, fire);
274                                                 return;
275                                         }
276
277                                         W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, burst_ammo));
278
279                                         actor.misc_bulletcounter = WEP_CVAR(machinegun, burst) * -1;
280                                         W_MachineGun_Attack_Burst(thiswep, actor, weaponentity, fire);
281                                 }
282                         }
283                         else
284                         {
285
286                                 if(fire & 1)
287                                 if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
288                                 {
289                                         actor.misc_bulletcounter = 1;
290                                         W_MachineGun_Attack(WEP_MACHINEGUN, WEP_MACHINEGUN.m_id, weaponentity); // sets attack_finished
291                                         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
292                                 }
293
294                                 if((fire & 2) && WEP_CVAR(machinegun, first))
295                                 if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
296                                 {
297                                         actor.misc_bulletcounter = 1;
298                                         W_MachineGun_Attack(WEP_MACHINEGUN, WEP_MACHINEGUN.m_id | HITTYPE_SECONDARY, weaponentity); // sets attack_finished
299                                         weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
300                                 }
301                         }
302                 }
303                 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep))
304                 {
305                         float ammo_amount;
306                         if(WEP_CVAR(machinegun, mode) == 1)
307                                 ammo_amount = self.(thiswep.ammo_field) >= WEP_CVAR(machinegun, sustained_ammo);
308                         else
309                                 ammo_amount = self.(thiswep.ammo_field) >= WEP_CVAR(machinegun, first_ammo);
310
311                         if(WEP_CVAR(machinegun, reload_ammo))
312                         {
313                                 if(WEP_CVAR(machinegun, mode) == 1)
314                                         ammo_amount += self.(weapon_load[WEP_MACHINEGUN.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
315                                 else
316                                         ammo_amount += self.(weapon_load[WEP_MACHINEGUN.m_id]) >= WEP_CVAR(machinegun, first_ammo);
317                         }
318                         return ammo_amount;
319                 }
320                 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep))
321                 {
322                         float ammo_amount;
323                         if(WEP_CVAR(machinegun, mode) == 1)
324                                 ammo_amount = self.(thiswep.ammo_field) >= WEP_CVAR(machinegun, burst_ammo);
325                         else
326                                 ammo_amount = self.(thiswep.ammo_field) >= WEP_CVAR(machinegun, first_ammo);
327
328                         if(WEP_CVAR(machinegun, reload_ammo))
329                         {
330                                 if(WEP_CVAR(machinegun, mode) == 1)
331                                         ammo_amount += self.(weapon_load[WEP_MACHINEGUN.m_id]) >= WEP_CVAR(machinegun, burst_ammo);
332                                 else
333                                         ammo_amount += self.(weapon_load[WEP_MACHINEGUN.m_id]) >= WEP_CVAR(machinegun, first_ammo);
334                         }
335                         return ammo_amount;
336                 }
337                 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
338                 {
339                         W_Reload(self, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND(RELOAD));
340                 }
341                 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
342                 {
343                         return WEAPON_THINKING_WITH_PORTALS;
344                 }
345                 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
346                 {
347                         if(w_deathtype & HITTYPE_SECONDARY)
348                                 return WEAPON_MACHINEGUN_MURDER_SNIPE;
349                         else
350                                 return WEAPON_MACHINEGUN_MURDER_SPRAY;
351                 }
352
353 #endif
354 #ifdef CSQC
355
356                 METHOD(MachineGun, wr_impacteffect, void(entity thiswep))
357                 {
358                         vector org2;
359                         org2 = w_org + w_backoff * 2;
360                         pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
361                         if(!w_issilent)
362                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);
363                 }
364
365 #endif
366 #endif