]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/machinegun.qc
remove a single tab to remove diff noise
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / machinegun.qc
1 #include "machinegun.qh"
2
3 #ifdef SVQC
4
5 .float machinegun_spread_accumulation;
6
7 .float oldTime;
8
9 void W_MachineGun_Attack(Weapon thiswep, int deathtype, entity actor, .entity weaponentity)
10 {
11         W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, ((actor.(weaponentity).misc_bulletcounter == 1) ? WEP_CVAR(machinegun, first_damage) : WEP_CVAR(machinegun, sustained_damage)), deathtype);
12         if(!autocvar_g_norecoil)
13         {
14                 actor.punchangle_x = random() - 0.5;
15                 actor.punchangle_y = random() - 0.5;
16         }
17         // this attack_finished just enforces a cooldown at the end of a burst
18         ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
19
20         if(actor.(weaponentity).misc_bulletcounter == 1)
21                 fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, first_spread), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, first_damage), 0, WEP_CVAR(machinegun, first_force), deathtype, EFFECT_BULLET);
22         else
23                 fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, sustained_spread), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), 0, WEP_CVAR(machinegun, sustained_force), deathtype, EFFECT_BULLET);
24
25         W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
26
27         // casing code
28         if(autocvar_g_casings >= 2)
29         {
30                 makevectors(actor.v_angle); // for some reason, this is lost
31                 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, weaponentity);
32         }
33
34         if(actor.(weaponentity).misc_bulletcounter == 1)
35                 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, first_ammo), weaponentity);
36         else
37                 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
38 }
39
40 // weapon frames
41 void W_MachineGun_Attack_Frame(Weapon thiswep, entity actor, .entity weaponentity, int fire)
42 {
43         if(actor.(weaponentity).m_weapon != actor.(weaponentity).m_switchweapon || !weapon_prepareattack_check(thiswep, actor, weaponentity, (fire & 2), -1)) // abort immediately if switching
44         {
45                 w_ready(thiswep, actor, weaponentity, fire);
46                 return;
47         }
48         if(PHYS_INPUT_BUTTON_ATCK(actor))
49         {
50                 if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
51                 if(!(actor.items & IT_UNLIMITED_AMMO))
52                 {
53                         W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
54                         w_ready(thiswep, actor, weaponentity, fire);
55                         return;
56                 }
57                 actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
58                 W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity);
59                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
60         }
61         else
62                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), w_ready);
63 }
64
65
66 void W_MachineGun_Attack_Auto(Weapon thiswep, entity actor, .entity weaponentity, int fire)
67 {
68         float machinegun_spread;
69
70         if(!(fire & 1) || !weapon_prepareattack_check(thiswep, actor, weaponentity, false, -1))
71         {
72                 w_ready(thiswep, actor, weaponentity, fire);
73                 return;
74         }
75
76         if(!thiswep.wr_checkammo1(thiswep, actor, weaponentity))
77         if(!(actor.items & IT_UNLIMITED_AMMO))
78         {
79                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
80                 w_ready(thiswep, actor, weaponentity, fire);
81                 return;
82         }
83
84         W_DecreaseAmmo(thiswep, actor, WEP_CVAR(machinegun, sustained_ammo), weaponentity);
85
86         W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
87         if(!autocvar_g_norecoil)
88         {
89                 actor.punchangle_x = random() - 0.5;
90                 actor.punchangle_y = random() - 0.5;
91         }
92         
93         // Step by step calculations for machinegun_spread_accumulation with optional debug printing
94         
95         // check current time
96         //print("\n Current time: ");
97         //print(sprintf("%f ", time));
98         
99         // seconds passed since the last shot
100         // oldTime is given value at the end of the calculations
101         //float seconds = (time - actor.(weaponentity).oldTime);
102         //print("\n seconds between last time and this time: ");
103         //print(sprintf("%f ", seconds));
104         
105         // use seconds passed since last shot to calculate how much spread should have decayed since then
106         //float spreadReduction = seconds * WEP_CVAR(machinegun, spread_decay);
107         //print("\n reduced spread: ");
108         //print(sprintf("%f ", spreadReduction));
109         
110         // reduce spread by the calculated amount
111         //actor.(weaponentity).machinegun_spread_accumulation = actor.(weaponentity).machinegun_spread_accumulation - spreadReduction;
112         //print("\n current spread: ");
113         //print(sprintf("%f ", actor.(weaponentity).machinegun_spread_accumulation));
114         
115         // if spread is reduced to the negatives make it 0 instead
116         // the following if block is replaced by max(accumulation, 0);
117         //if(actor.(weaponentity).machinegun_spread_accumulation < 0){
118         //      print("\n spread was <0, was reset to 0 before firing");
119         //      actor.(weaponentity).machinegun_spread_accumulation = 0;
120         //}
121         
122         // store the time of the last shot
123         //actor.(weaponentity).oldTime = time;
124         //print("\n Old time: ");
125         //print(sprintf("%f ", actor.(weaponentity).oldTime));
126         
127         actor.(weaponentity).machinegun_spread_accumulation = max(actor.(weaponentity).machinegun_spread_accumulation - ((time - actor.(weaponentity).oldTime) * WEP_CVAR(machinegun, spread_decay)), 0);
128         actor.(weaponentity).oldTime = time;
129
130         machinegun_spread = bound(WEP_CVAR(machinegun, spread_min), WEP_CVAR(machinegun, spread_min) + actor.(weaponentity).machinegun_spread_accumulation, WEP_CVAR(machinegun, spread_max));
131         fireBullet(actor, weaponentity, w_shotorg, w_shotdir, machinegun_spread, WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), 0, WEP_CVAR(machinegun, sustained_force), thiswep.m_id, EFFECT_BULLET);
132
133         actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
134         actor.(weaponentity).machinegun_spread_accumulation = actor.(weaponentity).machinegun_spread_accumulation + WEP_CVAR(machinegun, spread_add);
135         if(actor.(weaponentity).machinegun_spread_accumulation > WEP_CVAR(machinegun, spread_max)){
136                 actor.(weaponentity).machinegun_spread_accumulation = WEP_CVAR(machinegun, spread_max);
137         }
138
139         W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
140
141         if(autocvar_g_casings >= 2) // casing code
142         {
143                 makevectors(actor.v_angle); // for some reason, this is lost
144                 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, weaponentity);
145         }
146
147         ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, first_refire) * W_WeaponRateFactor(actor);
148         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Auto);
149 }
150
151 void W_MachineGun_Attack_Burst(Weapon thiswep, entity actor, .entity weaponentity, int fire)
152 {
153         W_SetupShot(actor, weaponentity, true, 0, SND_UZI_FIRE, CH_WEAPON_A, WEP_CVAR(machinegun, sustained_damage), thiswep.m_id);
154         if(!autocvar_g_norecoil)
155         {
156                 actor.punchangle_x = random() - 0.5;
157                 actor.punchangle_y = random() - 0.5;
158         }
159
160         fireBullet(actor, weaponentity, w_shotorg, w_shotdir, WEP_CVAR(machinegun, burst_spread), WEP_CVAR(machinegun, solidpenetration), WEP_CVAR(machinegun, sustained_damage), 0, WEP_CVAR(machinegun, sustained_force), thiswep.m_id, EFFECT_BULLET);
161
162         W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
163
164         if(autocvar_g_casings >= 2) // casing code
165         {
166                 makevectors(actor.v_angle); // for some reason, this is lost
167                 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, weaponentity);
168         }
169
170         actor.(weaponentity).misc_bulletcounter = actor.(weaponentity).misc_bulletcounter + 1;
171         actor.(weaponentity).machinegun_spread_accumulation = actor.(weaponentity).machinegun_spread_accumulation + (WEP_CVAR(machinegun, spread_add) * 0.5);
172         if(actor.(weaponentity).machinegun_spread_accumulation > WEP_CVAR(machinegun, spread_max)){
173                 actor.(weaponentity).machinegun_spread_accumulation = WEP_CVAR(machinegun, spread_max);
174         }
175         if(actor.(weaponentity).misc_bulletcounter == 0)
176         {
177                 ATTACK_FINISHED(actor, weaponentity) = time + WEP_CVAR(machinegun, burst_refire2) * W_WeaponRateFactor(actor);
178                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_animtime), w_ready);
179         }
180         else
181         {
182                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, burst_refire), W_MachineGun_Attack_Burst);
183         }
184
185 }
186
187 METHOD(MachineGun, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
188 {
189     if(vdist(actor.origin - actor.enemy.origin, <, 3000 - bound(0, skill, 10) * 200))
190         PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
191     else
192         PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
193 }
194 METHOD(MachineGun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
195 {
196     // forced reload - wait until the bulletcounter is 0 so a burst loop can finish
197     if(WEP_CVAR(machinegun, reload_ammo)
198         && actor.(weaponentity).clip_load < min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo))
199         && actor.(weaponentity).misc_bulletcounter >= 0)
200     {
201         thiswep.wr_reload(thiswep, actor, weaponentity);
202     }
203     else if(WEP_CVAR(machinegun, mode) == 1)
204     {
205         if(fire & 1)
206         if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
207         {
208             actor.(weaponentity).misc_bulletcounter = 0;
209             W_MachineGun_Attack_Auto(thiswep, actor, weaponentity, fire);
210         }
211
212         // You can "shoot" more rounds than what's "used", and vice versa.
213         if(fire & 2)
214         if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
215         {
216             actor.(weaponentity).machinegun_spread_accumulation = max(actor.(weaponentity).machinegun_spread_accumulation - ((time - actor.(weaponentity).oldTime) * WEP_CVAR(machinegun, spread_decay)), 0);
217             actor.(weaponentity).oldTime = time;
218             if(actor.(weaponentity).machinegun_spread_accumulation > 0){
219                 w_ready(thiswep, actor, weaponentity, fire);
220                 return;
221             }
222             
223             if(!thiswep.wr_checkammo2(thiswep, actor, weaponentity))
224             if(!(actor.items & IT_UNLIMITED_AMMO))
225             {
226                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
227                 w_ready(thiswep, actor, weaponentity, fire);
228                 return;
229             }
230
231             float ammo_available;
232             if (WEP_CVAR(machinegun, reload_ammo) > 0)
233             {
234                 ammo_available = actor.(weaponentity).clip_load;
235             }
236             else
237             {
238                 ammo_available = GetResource(actor, thiswep.ammo_type);
239             }
240
241             // We don't want to shoot 3 rounds if there's 2 left in the mag, so we'll use a fraction.
242             // Also keep the fraction <= 1 otherwise we'd mag dump in one burst.
243             float burst_fraction = min(1, ammo_available / WEP_CVAR(machinegun, burst_ammo));
244             int to_shoot = floor(WEP_CVAR(machinegun, burst) * burst_fraction);
245
246             // We also don't want to use 3 rounds if there's only 2 left.
247             int to_use = min(WEP_CVAR(machinegun, burst_ammo), ammo_available);
248             W_DecreaseAmmo(thiswep, actor, to_use, weaponentity);
249
250             // Bursting counts up to 0 from a negative.
251             actor.(weaponentity).misc_bulletcounter = -to_shoot;
252             W_MachineGun_Attack_Burst(thiswep, actor, weaponentity, fire);
253         }
254     }
255     else
256     {
257
258         if(fire & 1)
259         if(weapon_prepareattack(thiswep, actor, weaponentity, false, 0))
260         {
261             actor.(weaponentity).misc_bulletcounter = 1;
262             W_MachineGun_Attack(thiswep, thiswep.m_id, actor, weaponentity); // sets attack_finished
263             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(machinegun, sustained_refire), W_MachineGun_Attack_Frame);
264         }
265
266         if((fire & 2) && WEP_CVAR(machinegun, first))
267         if(weapon_prepareattack(thiswep, actor, weaponentity, true, 0))
268         {
269             actor.(weaponentity).misc_bulletcounter = 1;
270             W_MachineGun_Attack(thiswep, thiswep.m_id | HITTYPE_SECONDARY, actor, weaponentity); // sets attack_finished
271             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR(machinegun, first_refire), w_ready);
272         }
273     }
274 }
275 METHOD(MachineGun, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
276 {
277     float ammo_amount;
278     if(WEP_CVAR(machinegun, mode) == 1)
279         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, sustained_ammo);
280     else
281         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
282
283     if(WEP_CVAR(machinegun, reload_ammo))
284     {
285         if(WEP_CVAR(machinegun, mode) == 1)
286             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, sustained_ammo);
287         else
288             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
289     }
290     return ammo_amount;
291 }
292 METHOD(MachineGun, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
293 {
294     float ammo_amount;
295     float burst_ammo_per_shot = WEP_CVAR(machinegun, burst_ammo) / WEP_CVAR(machinegun, burst);
296     if(WEP_CVAR(machinegun, mode) == 1)
297         ammo_amount = GetResource(actor, thiswep.ammo_type) >= burst_ammo_per_shot;
298     else
299         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(machinegun, first_ammo);
300
301     if(WEP_CVAR(machinegun, reload_ammo))
302     {
303         if(WEP_CVAR(machinegun, mode) == 1)
304             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= burst_ammo_per_shot;
305         else
306             ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(machinegun, first_ammo);
307     }
308     return ammo_amount;
309 }
310 METHOD(MachineGun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
311 {
312         if(actor.(weaponentity).misc_bulletcounter < 0)
313                 return;
314     W_Reload(actor, weaponentity, min(max(WEP_CVAR(machinegun, sustained_ammo), WEP_CVAR(machinegun, first_ammo)), WEP_CVAR(machinegun, burst_ammo)), SND_RELOAD);
315 }
316 METHOD(MachineGun, wr_suicidemessage, Notification(entity thiswep))
317 {
318     return WEAPON_THINKING_WITH_PORTALS;
319 }
320 METHOD(MachineGun, wr_killmessage, Notification(entity thiswep))
321 {
322     if(w_deathtype & HITTYPE_SECONDARY)
323         return WEAPON_MACHINEGUN_MURDER_SNIPE;
324     else
325         return WEAPON_MACHINEGUN_MURDER_SPRAY;
326 }
327
328 #endif
329 #ifdef CSQC
330
331 METHOD(MachineGun, wr_impacteffect, void(entity thiswep, entity actor))
332 {
333     vector org2;
334     org2 = w_org + w_backoff * 2;
335     pointparticles(EFFECT_MACHINEGUN_IMPACT, org2, w_backoff * 1000, 1);
336     if(!w_issilent)
337         sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTN_NORM);
338 }
339
340 #endif