]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/shotgun.qc
Merge branch 'TimePath/remove_remove'
[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 | WEP_TYPE_MELEE_SEC);
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, entity actor, float isprimary)
62 {
63         W_DecreaseAmmo(thiswep, actor, WEP_CVAR_PRI(shotgun, ammo));
64
65         W_SetupShot(actor, true, 5, SND_SHOTGUN_FIRE, ((isprimary) ? CH_WEAPON_A : CH_WEAPON_SINGLE), WEP_CVAR_PRI(shotgun, damage) * WEP_CVAR_PRI(shotgun, bullets));
66         for(int sc = 0;sc < WEP_CVAR_PRI(shotgun, bullets);sc = sc + 1)
67                 fireBullet(actor, 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         {
74                 makevectors(actor.v_angle); // for some reason, this is lost
75                 //for(int sc = 0;sc < WEP_CVAR_PRI(shotgun, ammo);sc = sc + 1)
76                         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);
77         }
78
79         // muzzle flash for 1st person view
80         entity flash = spawn();
81         setmodel(flash, MDL_SHOTGUN_MUZZLEFLASH); // precision set below
82         setthink(flash, SUB_Remove);
83         flash.nextthink = time + 0.06;
84         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
85         W_AttachToShotorg(actor, flash, '5 0 0');
86 }
87
88 .float swing_prev;
89 .entity swing_alreadyhit;
90 void W_Shotgun_Melee_Think(entity this)
91 {
92         // declarations
93         float i, f, swing, swing_factor, swing_damage, meleetime, is_player;
94         entity target_victim;
95         vector targpos;
96
97         if(!this.cnt) // set start time of melee
98         {
99                 this.cnt = time;
100                 W_PlayStrengthSound(this.realowner);
101         }
102
103         makevectors(this.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(this.realowner);
107         swing = bound(0, (this.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(this.realowner) && WEP_CVAR_SEC(shotgun, melee_no_doubleslap))
112         {
113                 delete(this);
114                 return;
115         }
116
117         // if okay, perform the traces needed for this frame
118         for(i=this.swing_prev; i < f; ++i)
119         {
120                 swing_factor = ((1 - (i / WEP_CVAR_SEC(shotgun, melee_traces))) * 2 - 1);
121
122                 targpos = (this.realowner.origin + this.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(this, this.realowner.origin + this.realowner.view_ofs, targpos, false, this.realowner, ANTILAG_LATENCY(this.realowner));
128
129                 // draw lightning beams for debugging
130                 //te_lightning2(NULL, targpos, this.realowner.origin + this.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 this
136                         && (trace_ent.takedamage == DAMAGE_AIM)
137                         && (trace_ent != this.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(this.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, this.realowner, this.realowner,
150                                 swing_damage, WEP_SHOTGUN.m_id | HITTYPE_SECONDARY,
151                                 this.realowner.origin + this.realowner.view_ofs,
152                                 v_forward * WEP_CVAR_SEC(shotgun, force));
153
154                         if(accuracy_isgooddamage(this.realowner, target_victim)) { accuracy_add(this.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                                 this.swing_alreadyhit = target_victim;
162                                 continue; // move along to next trace
163                         }
164                         else
165                         {
166                                 delete(this);
167                                 return;
168                         }
169                 }
170         }
171
172         if(time >= this.cnt + meleetime)
173         {
174                 // melee is finished
175                 delete(this);
176                 return;
177         }
178         else
179         {
180                 // set up next frame
181                 this.swing_prev = i;
182                 this.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(actor);
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, actor, 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, actor, 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, entity actor))
230 {
231     if(vdist(actor.origin - actor.enemy.origin, <=, WEP_CVAR_SEC(shotgun, melee_range)))
232         PHYS_INPUT_BUTTON_ATCK2(actor) = bot_aim(actor, 1000000, 0, 0.001, false);
233     else
234         PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, 1000000, 0, 0.001, false);
235 }
236 METHOD(Shotgun, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
237 {
238     if(WEP_CVAR(shotgun, reload_ammo) && actor.clip_load < WEP_CVAR_PRI(shotgun, ammo)) // forced reload
239     {
240         // don't force reload an empty shotgun if its melee attack is active
241         if(WEP_CVAR(shotgun, secondary) < 2) {
242             thiswep.wr_reload(thiswep, actor, weaponentity);
243         }
244     }
245     else
246     {
247         if(fire & 1)
248         {
249             if(time >= actor.shotgun_primarytime) // handle refire separately so the secondary can be fired straight after a primary
250             {
251                 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(shotgun, animtime)))
252                 {
253                     W_Shotgun_Attack(thiswep, actor, true);
254                     actor.shotgun_primarytime = time + WEP_CVAR_PRI(shotgun, refire) * W_WeaponRateFactor(actor);
255                     weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(shotgun, animtime), w_ready);
256                 }
257             }
258         }
259         else if((fire & 2) && WEP_CVAR(shotgun, secondary) == 2)
260         {
261             if(time >= actor.shotgun_primarytime) // handle refire separately so the secondary can be fired straight after a primary
262             {
263                 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_SEC(shotgun, alt_animtime)))
264                 {
265                     W_Shotgun_Attack(thiswep, actor, false);
266                     actor.shotgun_primarytime = time + WEP_CVAR_SEC(shotgun, alt_refire) * W_WeaponRateFactor(actor);
267                     weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_SEC(shotgun, alt_animtime), W_Shotgun_Attack3_Frame1);
268                 }
269             }
270         }
271     }
272     if(actor.clip_load >= 0) // we are not currently reloading
273     if(!actor.crouch) // no crouchmelee please
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_setup, void(entity thiswep, entity actor))
283 {
284     actor.ammo_field = ammo_none;
285 }
286 METHOD(Shotgun, wr_checkammo1, bool(entity thiswep, entity actor))
287 {
288     float ammo_amount = actor.(thiswep.ammo_field) >= WEP_CVAR_PRI(shotgun, ammo);
289     ammo_amount += actor.(weapon_load[WEP_SHOTGUN.m_id]) >= WEP_CVAR_PRI(shotgun, ammo);
290     return ammo_amount;
291 }
292 METHOD(Shotgun, wr_checkammo2, bool(entity thiswep, entity actor))
293 {
294     if(IS_BOT_CLIENT(actor))
295     if(vdist(actor.origin - actor.enemy.origin, >, WEP_CVAR_SEC(shotgun, melee_range)))
296         return false; // bots cannot use secondary out of range (fixes constant melee when out of ammo)
297     switch(WEP_CVAR(shotgun, secondary))
298     {
299         case 1: return true; // melee does not use ammo
300         case 2: // secondary triple shot
301         {
302             float ammo_amount = actor.(thiswep.ammo_field) >= WEP_CVAR_PRI(shotgun, ammo);
303             ammo_amount += actor.(weapon_load[WEP_SHOTGUN.m_id]) >= WEP_CVAR_PRI(shotgun, ammo);
304             return ammo_amount;
305         }
306         default: return false; // secondary unavailable
307     }
308 }
309 METHOD(Shotgun, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
310 {
311     W_Reload(actor, WEP_CVAR_PRI(shotgun, ammo), SND_RELOAD); // WEAPONTODO
312 }
313 METHOD(Shotgun, wr_suicidemessage, Notification(entity thiswep))
314 {
315     return WEAPON_THINKING_WITH_PORTALS;
316 }
317 METHOD(Shotgun, wr_killmessage, Notification(entity thiswep))
318 {
319     if(w_deathtype & HITTYPE_SECONDARY)
320         return WEAPON_SHOTGUN_MURDER_SLAP;
321     else
322         return WEAPON_SHOTGUN_MURDER;
323 }
324
325 #endif
326 #ifdef CSQC
327 .float prevric;
328
329 METHOD(Shotgun, wr_impacteffect, void(entity thiswep, entity actor))
330 {
331     vector org2 = w_org + w_backoff * 2;
332     pointparticles(EFFECT_SHOTGUN_IMPACT, org2, w_backoff * 1000, 1);
333     if(!w_issilent && time - actor.prevric > 0.25)
334     {
335         if(w_random < 0.05)
336             sound(actor, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
337         actor.prevric = time;
338     }
339 }
340
341 #endif
342 #endif