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