b9a0dba0cf4b0eb2a76a15377c45c4301b90d628
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_shotgun.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(SHOTGUN, w_shotgun, IT_SHELLS, 2, WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_TYPE_HITSCAN, BOT_PICKUP_RATING_LOW, "shotgun", "shotgun", _("Shotgun"))
3 #else
4 #ifdef SVQC
5
6 void W_Shotgun_Attack (void)
7 {
8         float   sc;
9         float   ammoamount;
10         float   bullets;
11         float   d;
12         float   f;
13         float   spread;
14         float   bulletspeed;
15         float   bulletconstant;
16         entity flash;
17
18         ammoamount = autocvar_g_balance_shotgun_primary_ammo;
19         bullets = autocvar_g_balance_shotgun_primary_bullets;
20         d = autocvar_g_balance_shotgun_primary_damage;
21         f = autocvar_g_balance_shotgun_primary_force;
22         spread = autocvar_g_balance_shotgun_primary_spread;
23         bulletspeed = autocvar_g_balance_shotgun_primary_speed;
24         bulletconstant = autocvar_g_balance_shotgun_primary_bulletconstant;
25
26         W_DecreaseAmmo(ammo_shells, ammoamount, autocvar_g_balance_shotgun_reload_ammo);
27
28         W_SetupShot (self, autocvar_g_antilag_bullets && bulletspeed >= autocvar_g_antilag_bullets, 5, "weapons/shotgun_fire.wav", CH_WEAPON_A, d * bullets);
29         for (sc = 0;sc < bullets;sc = sc + 1)
30                 fireBallisticBullet(w_shotorg, w_shotdir, spread, bulletspeed, 5, d, f, WEP_SHOTGUN, 0, 1, bulletconstant);
31         endFireBallisticBullet();
32
33         pointparticles(particleeffectnum("shotgun_muzzleflash"), w_shotorg, w_shotdir * 1000, autocvar_g_balance_shotgun_primary_ammo);
34
35         // casing code
36         if (autocvar_g_casings >= 1)
37                 for (sc = 0;sc < ammoamount;sc = sc + 1)
38                         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);
39
40         // muzzle flash for 1st person view
41         flash = spawn();
42         setmodel(flash, "models/uziflash.md3"); // precision set below
43         flash.think = SUB_Remove;
44         flash.nextthink = time + 0.06;
45         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
46         W_AttachToShotorg(flash, '5 0 0');
47 }
48
49 .float swing_prev;
50 .entity swing_alreadyhit;
51 void shotgun_meleethink (void)
52 {
53         // declarations
54         float i, f, swing, swing_factor, swing_damage, meleetime, is_player;
55         entity target_victim;
56         vector targpos;
57
58         if(!self.cnt) // set start time of melee
59         {
60                 self.cnt = time; 
61                 W_PlayStrengthSound(self.realowner);
62         }
63
64         makevectors(self.realowner.v_angle); // update values for v_* vectors
65         
66         // calculate swing percentage based on time
67         meleetime = autocvar_g_balance_shotgun_secondary_melee_time * W_WeaponRateFactor();
68         swing = bound(0, (self.cnt + meleetime - time) / meleetime, 10);
69         f = ((1 - swing) * autocvar_g_balance_shotgun_secondary_melee_traces);
70         
71         // check to see if we can still continue, otherwise give up now
72         if((self.realowner.deadflag != DEAD_NO) && autocvar_g_balance_shotgun_secondary_melee_no_doubleslap)
73         {
74                 remove(self);
75                 return;
76         }
77         
78         // if okay, perform the traces needed for this frame 
79         for(i=self.swing_prev; i < f; ++i)
80         {
81                 swing_factor = ((1 - (i / autocvar_g_balance_shotgun_secondary_melee_traces)) * 2 - 1);
82                 
83                 targpos = (self.realowner.origin + self.realowner.view_ofs 
84                         + (v_forward * autocvar_g_balance_shotgun_secondary_melee_range)
85                         + (v_up * swing_factor * autocvar_g_balance_shotgun_secondary_melee_swing_up)
86                         + (v_right * swing_factor * autocvar_g_balance_shotgun_secondary_melee_swing_side));
87
88                 WarpZone_traceline_antilag(self, self.realowner.origin + self.realowner.view_ofs, targpos, FALSE, self, ANTILAG_LATENCY(self.realowner));
89                 
90                 // draw lightning beams for debugging
91                 //te_lightning2(world, targpos, self.realowner.origin + self.realowner.view_ofs + v_forward * 5 - v_up * 5); 
92                 //te_customflash(targpos, 40,  2, '1 1 1');
93                 
94                 is_player = (IS_PLAYER(trace_ent) || trace_ent.classname == "body");
95
96                 if((trace_fraction < 1) // if trace is good, apply the damage and remove self
97                         && (trace_ent.takedamage == DAMAGE_AIM)  
98                         && (trace_ent != self.swing_alreadyhit)
99                         && (is_player || autocvar_g_balance_shotgun_secondary_melee_nonplayerdamage))
100                 {
101                         target_victim = trace_ent; // so it persists through other calls
102                         
103                         if(is_player) // this allows us to be able to nerf the non-player damage done in e.g. assault or onslaught.
104                                 swing_damage = (autocvar_g_balance_shotgun_secondary_damage * min(1, swing_factor + 1));
105                         else
106                                 swing_damage = (autocvar_g_balance_shotgun_secondary_melee_nonplayerdamage * min(1, swing_factor + 1));
107                         
108                         //print(strcat(self.realowner.netname, " hitting ", target_victim.netname, " with ", strcat(ftos(swing_damage), " damage (factor: ", ftos(swing_factor), ") at "), ftos(time), " seconds.\n"));
109                         
110                         Damage(target_victim, self.realowner, self.realowner, 
111                                 swing_damage, WEP_SHOTGUN | HITTYPE_SECONDARY, 
112                                 self.realowner.origin + self.realowner.view_ofs, 
113                                 v_forward * autocvar_g_balance_shotgun_secondary_force);
114                                 
115                         if(accuracy_isgooddamage(self.realowner, target_victim)) { accuracy_add(self.realowner, WEP_SHOTGUN, 0, swing_damage); }
116                                 
117                         // draw large red flash for debugging
118                         //te_customflash(targpos, 200, 2, '15 0 0');
119                         
120                         if(autocvar_g_balance_shotgun_secondary_melee_multihit) // allow multiple hits with one swing, but not against the same player twice.
121                         {
122                                 self.swing_alreadyhit = target_victim;
123                                 continue; // move along to next trace
124                         }
125                         else
126                         {
127                                 remove(self);
128                                 return;
129                         }
130                 }
131         }
132         
133         if(time >= self.cnt + meleetime)
134         {
135                 // melee is finished
136                 remove(self);
137                 return;
138         }
139         else
140         {
141                 // set up next frame 
142                 self.swing_prev = i;
143                 self.nextthink = time;
144         }
145 }
146
147 void W_Shotgun_Attack2 (void)
148 {
149         sound (self, CH_WEAPON_A, "weapons/shotgun_melee.wav", VOL_BASE, ATTN_NORM);
150         weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_shotgun_secondary_animtime, w_ready);
151
152         entity meleetemp;
153         meleetemp = spawn();
154         meleetemp.realowner = self;
155         meleetemp.think = shotgun_meleethink;
156         meleetemp.nextthink = time + autocvar_g_balance_shotgun_secondary_melee_delay * W_WeaponRateFactor();
157         W_SetupShot_Range(self, TRUE, 0, "", 0, autocvar_g_balance_shotgun_secondary_damage, autocvar_g_balance_shotgun_secondary_melee_range);
158 }
159
160 void spawnfunc_weapon_shotgun(); // defined in t_items.qc
161
162 .float shotgun_primarytime;
163
164 float w_shotgun(float req)
165 {
166         float ammo_amount;
167         if (req == WR_AIM)
168                 if(vlen(self.origin-self.enemy.origin) <= autocvar_g_balance_shotgun_secondary_melee_range)
169                         self.BUTTON_ATCK2 = bot_aim(1000000, 0, 0.001, FALSE);
170                 else
171                 {
172                         if(autocvar_g_antilag_bullets)
173                                 self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, FALSE);
174                         else
175                                 self.BUTTON_ATCK = bot_aim(autocvar_g_balance_shotgun_primary_speed, 0, 0.001, FALSE);
176                 }
177
178         else if (req == WR_THINK)
179         {
180                 if(autocvar_g_balance_shotgun_reload_ammo && self.clip_load < autocvar_g_balance_shotgun_primary_ammo) // forced reload
181                 {
182                         // don't force reload an empty shotgun if its melee attack is active
183                         if not(autocvar_g_balance_shotgun_secondary && self.ammo_shells < autocvar_g_balance_shotgun_primary_ammo)
184                                 weapon_action(self.weapon, WR_RELOAD);
185                 }
186                 else
187                 {
188                         if (self.BUTTON_ATCK)
189                         {
190                                 if (time >= self.shotgun_primarytime) // handle refire separately so the secondary can be fired straight after a primary
191                                 {
192                                         if(weapon_prepareattack(0, autocvar_g_balance_shotgun_primary_animtime))
193                                         {
194                                                 W_Shotgun_Attack();
195                                                 self.shotgun_primarytime = time + autocvar_g_balance_shotgun_primary_refire * W_WeaponRateFactor();
196                                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_shotgun_primary_animtime, w_ready);
197                                         }
198                                 }
199                         }
200                 }
201                 if (self.clip_load >= 0) // we are not currently reloading
202                 if (!self.crouch) // no crouchmelee please
203                 if (self.BUTTON_ATCK2 && autocvar_g_balance_shotgun_secondary)
204                 if (weapon_prepareattack(1, autocvar_g_balance_shotgun_secondary_refire))
205                 {
206                         // attempt forcing playback of the anim by switching to another anim (that we never play) here...
207                         weapon_thinkf(WFRAME_FIRE1, 0, W_Shotgun_Attack2);
208                 }
209         }
210         else if (req == WR_PRECACHE)
211         {
212                 precache_model ("models/uziflash.md3");
213                 precache_model ("models/weapons/g_shotgun.md3");
214                 precache_model ("models/weapons/v_shotgun.md3");
215                 precache_model ("models/weapons/h_shotgun.iqm");
216                 precache_sound ("misc/itempickup.wav");
217                 precache_sound ("weapons/shotgun_fire.wav");
218                 precache_sound ("weapons/shotgun_melee.wav");
219                 //precache_sound ("weapons/reload.wav"); // until weapons have individual reload sounds, precache the reload sound somewhere else
220         }
221         else if (req == WR_SETUP)
222         {
223                 weapon_setup(WEP_SHOTGUN);
224                 self.current_ammo = ammo_shells;
225         }
226         else if (req == WR_CHECKAMMO1)
227         {
228                 ammo_amount = self.ammo_shells >= autocvar_g_balance_shotgun_primary_ammo;
229                 ammo_amount += self.(weapon_load[WEP_SHOTGUN]) >= autocvar_g_balance_shotgun_primary_ammo;
230                 return ammo_amount;
231         }
232         else if (req == WR_CHECKAMMO2)
233         {
234                 // melee attack is always available
235                 return TRUE;
236         }
237         else if (req == WR_RELOAD)
238         {
239                 W_Reload(autocvar_g_balance_shotgun_primary_ammo, autocvar_g_balance_shotgun_reload_ammo, autocvar_g_balance_shotgun_reload_time, "weapons/reload.wav");
240         }
241         else if (req == WR_SUICIDEMESSAGE)
242         {
243                 return WEAPON_THINKING_WITH_PORTALS;
244         }
245         else if (req == WR_KILLMESSAGE)
246         {
247                 if(w_deathtype & HITTYPE_SECONDARY)
248                         return WEAPON_SHOTGUN_MURDER_SLAP;
249                 else
250                         return WEAPON_SHOTGUN_MURDER;
251         }
252         return TRUE;
253 }
254 #endif
255 #ifdef CSQC
256 .float prevric;
257 float w_shotgun(float req)
258 {
259         if(req == WR_IMPACTEFFECT)
260         {
261                 vector org2;
262                 org2 = w_org + w_backoff * 2;
263                 pointparticles(particleeffectnum("shotgun_impact"), org2, w_backoff * 1000, 1);
264                 if(!w_issilent && time - self.prevric > 0.25)
265                 {
266                         if(w_random < 0.0165)
267                                 sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTN_NORM);
268                         else if(w_random < 0.033)
269                                 sound(self, CH_SHOTS, "weapons/ric2.wav", VOL_BASE, ATTN_NORM);
270                         else if(w_random < 0.05)
271                                 sound(self, CH_SHOTS, "weapons/ric3.wav", VOL_BASE, ATTN_NORM);
272                         self.prevric = time;
273                 }
274         }
275         else if(req == WR_PRECACHE)
276         {
277                 precache_sound("weapons/ric1.wav");
278                 precache_sound("weapons/ric2.wav");
279                 precache_sound("weapons/ric3.wav");
280         }
281         return TRUE;
282 }
283 #endif
284 #endif