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