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