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