]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_shotgun.qc
3cf527d398eccdb9d99cc0f5eebfb9240d286b88
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / w_shotgun.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(
3 /* WEP_##id  */ SHOTGUN,
4 /* function  */ W_Shotgun,
5 /* ammotype  */ ammo_shells,
6 /* impulse   */ 2,
7 /* flags     */ WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_TYPE_HITSCAN,
8 /* rating    */ BOT_PICKUP_RATING_LOW,
9 /* color     */ '0.5 0.25 0',
10 /* modelname */ "shotgun",
11 /* simplemdl */ "foobar",
12 /* crosshair */ "gfx/crosshairshotgun 0.65",
13 /* wepimg    */ "weaponshotgun",
14 /* refname   */ "shotgun",
15 /* wepname   */ _("Shotgun")
16 );
17
18 #define SHOTGUN_SETTINGS(w_cvar,w_prop) SHOTGUN_SETTINGS_LIST(w_cvar, w_prop, SHOTGUN, shotgun)
19 #define SHOTGUN_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
20         w_cvar(id, sn, PRI,  ammo) \
21         w_cvar(id, sn, BOTH, animtime) \
22         w_cvar(id, sn, BOTH, refire) \
23         w_cvar(id, sn, PRI,  bullets) \
24         w_cvar(id, sn, BOTH, damage) \
25         w_cvar(id, sn, BOTH, force) \
26         w_cvar(id, sn, PRI,  solidpenetration) \
27         w_cvar(id, sn, PRI,  spread) \
28         w_cvar(id, sn, NONE, secondary) \
29         w_cvar(id, sn, SEC,  melee_time) \
30         w_cvar(id, sn, SEC,  melee_no_doubleslap) \
31         w_cvar(id, sn, SEC,  melee_traces) \
32         w_cvar(id, sn, SEC,  melee_swing_up) \
33         w_cvar(id, sn, SEC,  melee_swing_side) \
34         w_cvar(id, sn, SEC,  melee_nonplayerdamage) \
35         w_cvar(id, sn, SEC,  melee_multihit) \
36         w_cvar(id, sn, SEC,  melee_delay) \
37         w_cvar(id, sn, SEC,  melee_range) \
38         w_cvar(id, sn, SEC,  alt_animtime) \
39         w_cvar(id, sn, SEC,  alt_refire) \
40         w_prop(id, sn, float,  reloading_ammo, reload_ammo) \
41         w_prop(id, sn, float,  reloading_time, reload_time) \
42         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
43         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
44         w_prop(id, sn, string, weaponreplace, weaponreplace) \
45         w_prop(id, sn, float,  weaponstart, weaponstart) \
46         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride) \
47         w_prop(id, sn, float,  weaponthrowable, weaponthrowable)
48
49 #ifdef SVQC
50 SHOTGUN_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
51 #endif
52 #else
53 #ifdef SVQC
54 void spawnfunc_weapon_shotgun(void) { weapon_defaultspawnfunc(WEP_SHOTGUN); }
55
56 void W_Shotgun_Attack(float isprimary)
57 {
58         float   sc;
59         entity flash;
60
61         W_DecreaseAmmo(WEP_CVAR_PRI(shotgun, ammo));
62
63         W_SetupShot(self, TRUE, 5, "weapons/shotgun_fire.wav", ((isprimary) ? CH_WEAPON_A : CH_WEAPON_SINGLE), WEP_CVAR_PRI(shotgun, damage) * WEP_CVAR_PRI(shotgun, bullets));
64         for(sc = 0;sc < WEP_CVAR_PRI(shotgun, bullets);sc = sc + 1)
65                 fireBullet(w_shotorg, w_shotdir, WEP_CVAR_PRI(shotgun, spread), WEP_CVAR_PRI(shotgun, solidpenetration), WEP_CVAR_PRI(shotgun, damage), WEP_CVAR_PRI(shotgun, force), WEP_SHOTGUN, 0);
66
67         pointparticles(particleeffectnum("shotgun_muzzleflash"), w_shotorg, w_shotdir * 1000, WEP_CVAR_PRI(shotgun, ammo));
68
69         // casing code
70         if(autocvar_g_casings >= 1)
71                 for(sc = 0;sc < WEP_CVAR_PRI(shotgun, ammo);sc = sc + 1)
72                         SpawnCasing(((random() * 50 + 50) * v_right) - (v_forward * (random() * 25 + 25)) - ((random() * 5 - 30) * v_up), 2, vectoangles(v_forward),'0 250 0', 100, 1, self);
73
74         // muzzle flash for 1st person view
75         flash = spawn();
76         setmodel(flash, "models/uziflash.md3"); // precision set below
77         flash.think = SUB_Remove;
78         flash.nextthink = time + 0.06;
79         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
80         W_AttachToShotorg(flash, '5 0 0');
81 }
82
83 .float swing_prev;
84 .entity swing_alreadyhit;
85 void W_Shotgun_Melee_Think(void)
86 {
87         // declarations
88         float i, f, swing, swing_factor, swing_damage, meleetime, is_player;
89         entity target_victim;
90         vector targpos;
91
92         if(!self.cnt) // set start time of melee
93         {
94                 self.cnt = time;
95                 W_PlayStrengthSound(self.realowner);
96         }
97
98         makevectors(self.realowner.v_angle); // update values for v_* vectors
99
100         // calculate swing percentage based on time
101         meleetime = WEP_CVAR_SEC(shotgun, melee_time) * W_WeaponRateFactor();
102         swing = bound(0, (self.cnt + meleetime - time) / meleetime, 10);
103         f = ((1 - swing) * WEP_CVAR_SEC(shotgun, melee_traces));
104
105         // check to see if we can still continue, otherwise give up now
106         if((self.realowner.deadflag != DEAD_NO) && WEP_CVAR_SEC(shotgun, melee_no_doubleslap))
107         {
108                 remove(self);
109                 return;
110         }
111
112         // if okay, perform the traces needed for this frame
113         for(i=self.swing_prev; i < f; ++i)
114         {
115                 swing_factor = ((1 - (i / WEP_CVAR_SEC(shotgun, melee_traces))) * 2 - 1);
116
117                 targpos = (self.realowner.origin + self.realowner.view_ofs
118                         + (v_forward * WEP_CVAR_SEC(shotgun, melee_range))
119                         + (v_up * swing_factor * WEP_CVAR_SEC(shotgun, melee_swing_up))
120                         + (v_right * swing_factor * WEP_CVAR_SEC(shotgun, melee_swing_side)));
121
122                 WarpZone_traceline_antilag(self, self.realowner.origin + self.realowner.view_ofs, targpos, FALSE, self, ANTILAG_LATENCY(self.realowner));
123
124                 // draw lightning beams for debugging
125                 //te_lightning2(world, targpos, self.realowner.origin + self.realowner.view_ofs + v_forward * 5 - v_up * 5);
126                 //te_customflash(targpos, 40,  2, '1 1 1');
127
128                 is_player = (IS_PLAYER(trace_ent) || trace_ent.classname == "body" || (trace_ent.flags & FL_MONSTER));
129
130                 if((trace_fraction < 1) // if trace is good, apply the damage and remove self
131                         && (trace_ent.takedamage == DAMAGE_AIM)
132                         && (trace_ent != self.swing_alreadyhit)
133                         && (is_player || WEP_CVAR_SEC(shotgun, melee_nonplayerdamage)))
134                 {
135                         target_victim = trace_ent; // so it persists through other calls
136
137                         if(is_player) // this allows us to be able to nerf the non-player damage done in e.g. assault or onslaught.
138                                 swing_damage = (WEP_CVAR_SEC(shotgun, damage) * min(1, swing_factor + 1));
139                         else
140                                 swing_damage = (WEP_CVAR_SEC(shotgun, melee_nonplayerdamage) * min(1, swing_factor + 1));
141
142                         //print(strcat(self.realowner.netname, " hitting ", target_victim.netname, " with ", strcat(ftos(swing_damage), " damage (factor: ", ftos(swing_factor), ") at "), ftos(time), " seconds.\n"));
143
144                         Damage(target_victim, self.realowner, self.realowner,
145                                 swing_damage, WEP_SHOTGUN | HITTYPE_SECONDARY,
146                                 self.realowner.origin + self.realowner.view_ofs,
147                                 v_forward * WEP_CVAR_SEC(shotgun, force));
148
149                         if(accuracy_isgooddamage(self.realowner, target_victim)) { accuracy_add(self.realowner, WEP_SHOTGUN, 0, swing_damage); }
150
151                         // draw large red flash for debugging
152                         //te_customflash(targpos, 200, 2, '15 0 0');
153
154                         if(WEP_CVAR_SEC(shotgun, melee_multihit)) // allow multiple hits with one swing, but not against the same player twice.
155                         {
156                                 self.swing_alreadyhit = target_victim;
157                                 continue; // move along to next trace
158                         }
159                         else
160                         {
161                                 remove(self);
162                                 return;
163                         }
164                 }
165         }
166
167         if(time >= self.cnt + meleetime)
168         {
169                 // melee is finished
170                 remove(self);
171                 return;
172         }
173         else
174         {
175                 // set up next frame
176                 self.swing_prev = i;
177                 self.nextthink = time;
178         }
179 }
180
181 void W_Shotgun_Attack2(void)
182 {
183         sound(self, CH_WEAPON_A, "weapons/shotgun_melee.wav", VOL_BASE, ATTEN_NORM);
184         weapon_thinkf(WFRAME_FIRE2, WEP_CVAR_SEC(shotgun, animtime), w_ready);
185
186         entity meleetemp;
187         meleetemp = spawn();
188         meleetemp.realowner = self;
189         meleetemp.think = W_Shotgun_Melee_Think;
190         meleetemp.nextthink = time + WEP_CVAR_SEC(shotgun, melee_delay) * W_WeaponRateFactor();
191         W_SetupShot_Range(self, TRUE, 0, "", 0, WEP_CVAR_SEC(shotgun, damage), WEP_CVAR_SEC(shotgun, melee_range));
192 }
193
194 // alternate secondary weapon frames
195 void W_Shotgun_Attack3_Frame2()
196 {
197         if (!WEP_ACTION(self.weapon, WR_CHECKAMMO2))
198         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
199         {
200                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
201                 w_ready();
202                 return;
203         }
204
205         sound(self, CH_WEAPON_SINGLE, "misc/null.wav", VOL_BASE, ATTN_NORM); // kill previous sound
206         W_Shotgun_Attack(TRUE); // actually is secondary, but we trick the last shot into playing full reload sound
207         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_SEC(shotgun, alt_animtime), w_ready);
208 }
209 void W_Shotgun_Attack3_Frame1()
210 {
211         if (!WEP_ACTION(self.weapon, WR_CHECKAMMO2))
212         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
213         {
214                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
215                 w_ready();
216                 return;
217         }
218
219         W_Shotgun_Attack(FALSE);
220         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_SEC(shotgun, alt_animtime), W_Shotgun_Attack3_Frame2);
221 }
222
223 .float shotgun_primarytime;
224
225 float W_Shotgun(float req)
226 {
227         float ammo_amount;
228         switch(req)
229         {
230                 case WR_AIM:
231                 {
232                         if(vlen(self.origin-self.enemy.origin) <= WEP_CVAR_SEC(shotgun, melee_range))
233                                 self.BUTTON_ATCK2 = bot_aim(1000000, 0, 0.001, FALSE);
234                         else
235                                 self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, FALSE);
236
237                         return TRUE;
238                 }
239                 case WR_THINK:
240                 {
241                         if(WEP_CVAR(shotgun, reload_ammo) && self.clip_load < WEP_CVAR_PRI(shotgun, ammo)) // forced reload
242                         {
243                                 // don't force reload an empty shotgun if its melee attack is active
244                                 if(WEP_CVAR(shotgun, secondary) < 2)
245                                         WEP_ACTION(self.weapon, WR_RELOAD);
246                         }
247                         else
248                         {
249                                 if(self.BUTTON_ATCK)
250                                 {
251                                         if(time >= self.shotgun_primarytime) // handle refire separately so the secondary can be fired straight after a primary
252                                         {
253                                                 if(weapon_prepareattack(0, WEP_CVAR_PRI(shotgun, animtime)))
254                                                 {
255                                                         W_Shotgun_Attack(TRUE);
256                                                         self.shotgun_primarytime = time + WEP_CVAR_PRI(shotgun, refire) * W_WeaponRateFactor();
257                                                         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(shotgun, animtime), w_ready);
258                                                 }
259                                         }
260                                 }
261                                 else if(self.BUTTON_ATCK2 && WEP_CVAR(shotgun, secondary) == 2)
262                                 {
263                                         if(time >= self.shotgun_primarytime) // handle refire separately so the secondary can be fired straight after a primary
264                                         {
265                                                 if(weapon_prepareattack(0, WEP_CVAR_SEC(shotgun, alt_animtime)))
266                                                 {
267                                                         W_Shotgun_Attack(FALSE);
268                                                         self.shotgun_primarytime = time + WEP_CVAR_SEC(shotgun, alt_refire) * W_WeaponRateFactor();
269                                                         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_SEC(shotgun, alt_animtime), W_Shotgun_Attack3_Frame1);
270                                                 }
271                                         }
272                                 }
273                         }
274                         if(self.clip_load >= 0) // we are not currently reloading
275                         if(!self.crouch) // no crouchmelee please
276                         if(WEP_CVAR(shotgun, secondary) == 1)
277                         if((self.BUTTON_ATCK && self.WEP_AMMO(SHOTGUN) <= 0 && !(self.items & IT_UNLIMITED_WEAPON_AMMO)) || self.BUTTON_ATCK2)
278                         if(weapon_prepareattack(1, WEP_CVAR_SEC(shotgun, refire)))
279                         {
280                                 // attempt forcing playback of the anim by switching to another anim (that we never play) here...
281                                 weapon_thinkf(WFRAME_FIRE1, 0, W_Shotgun_Attack2);
282                         }
283                         
284                         return TRUE;
285                 }
286                 case WR_INIT:
287                 {
288                         precache_model("models/uziflash.md3");
289                         precache_model("models/weapons/g_shotgun.md3");
290                         precache_model("models/weapons/v_shotgun.md3");
291                         precache_model("models/weapons/h_shotgun.iqm");
292                         precache_sound("misc/itempickup.wav");
293                         precache_sound("weapons/shotgun_fire.wav");
294                         precache_sound("weapons/shotgun_melee.wav");
295                         SHOTGUN_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP)
296                         return TRUE;
297                 }
298                 case WR_SETUP:
299                 {
300                         self.ammo_field = ammo_none;
301                         return TRUE;
302                 }
303                 case WR_CHECKAMMO1:
304                 {
305                         ammo_amount = self.WEP_AMMO(SHOTGUN) >= WEP_CVAR_PRI(shotgun, ammo);
306                         ammo_amount += self.(weapon_load[WEP_SHOTGUN]) >= WEP_CVAR_PRI(shotgun, ammo);
307                         return ammo_amount;
308                 }
309                 case WR_CHECKAMMO2:
310                 {
311                         switch(WEP_CVAR(shotgun, secondary))
312                         {
313                                 case 1: return TRUE; // melee does not use ammo
314                                 case 2: // secondary triple shot
315                                 {
316                                         ammo_amount = self.WEP_AMMO(SHOTGUN) >= WEP_CVAR_PRI(shotgun, ammo);
317                                         ammo_amount += self.(weapon_load[WEP_SHOTGUN]) >= WEP_CVAR_PRI(shotgun, ammo);
318                                         return ammo_amount;
319                                 }
320                                 default: return FALSE; // secondary unavailable
321                         }
322                 }
323                 case WR_CONFIG:
324                 {
325                         SHOTGUN_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
326                         return TRUE;
327                 }
328                 case WR_RELOAD:
329                 {
330                         W_Reload(WEP_CVAR_PRI(shotgun, ammo), "weapons/reload.wav"); // WEAPONTODO
331                         return TRUE;
332                 }
333                 case WR_SUICIDEMESSAGE:
334                 {
335                         return WEAPON_THINKING_WITH_PORTALS;
336                 }
337                 case WR_KILLMESSAGE:
338                 {
339                         if(w_deathtype & HITTYPE_SECONDARY)
340                                 return WEAPON_SHOTGUN_MURDER_SLAP;
341                         else
342                                 return WEAPON_SHOTGUN_MURDER;
343                 }
344         }
345         return FALSE;
346 }
347 #endif
348 #ifdef CSQC
349 .float prevric;
350 float W_Shotgun(float req)
351 {
352         switch(req)
353         {
354                 case WR_IMPACTEFFECT:
355                 {
356                         vector org2;
357                         org2 = w_org + w_backoff * 2;
358                         pointparticles(particleeffectnum("shotgun_impact"), org2, w_backoff * 1000, 1);
359                         if(!w_issilent && time - self.prevric > 0.25)
360                         {
361                                 if(w_random < 0.0165)
362                                         sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTEN_NORM);
363                                 else if(w_random < 0.033)
364                                         sound(self, CH_SHOTS, "weapons/ric2.wav", VOL_BASE, ATTEN_NORM);
365                                 else if(w_random < 0.05)
366                                         sound(self, CH_SHOTS, "weapons/ric3.wav", VOL_BASE, ATTEN_NORM);
367                                 self.prevric = time;
368                         }
369
370                         return TRUE;
371                 }
372                 case WR_INIT:
373                 {
374                         precache_sound("weapons/ric1.wav");
375                         precache_sound("weapons/ric2.wav");
376                         precache_sound("weapons/ric3.wav");
377                         return TRUE;
378                 }
379                 case WR_ZOOMRETICLE:
380                 {
381                         // no weapon specific image for this weapon
382                         return FALSE;
383                 }
384         }
385         return FALSE;
386 }
387 #endif
388 #endif