]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_sniperrifle.qc
Tidy up code and comments
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_sniperrifle.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(SNIPERRIFLE, w_sniperrifle, IT_NAILS, 7, WEP_FLAG_NORMAL | WEP_TYPE_HITSCAN, BOT_PICKUP_RATING_MID, "campingrifle", "sniperrifle", _("Sniper Rifle"))
3 #else
4 #ifdef SVQC
5 //Sniper rifle Primary mode: manually operated bolt*, Secondary: full automatic**
6 //* Manually operating the bolt means that all the power of the gas is used to propell the bullet. In this mode the bolt is prevented from moving backwards in response to the firing of the bullet.
7 //** In fully automatic mode some of the gas is used to extract and reload the next cartrige, thus there is less power and range.
8
9 .float sniperrifle_accumulator;
10 .float sniperrifle_load;
11
12 void W_SniperRifle_SetAmmoCounter()
13 {
14         // set ammo_counter to the weapon we have switched to, if the gun uses reloading
15         if(!autocvar_g_balance_sniperrifle_reload_ammo)
16                 self.ammo_counter = 0; // also keeps crosshair ammo from displaying
17         else
18                 self.ammo_counter = self.sniperrifle_load;
19 }
20
21 void W_SniperRifle_ReloadedAndReady()
22 {
23         float t;
24
25         // now do the ammo transfer
26         self.ammo_counter = self.old_ammo_counter; // restore the ammo counter, in case we still had ammo in the weapon before reloading
27         while(self.ammo_counter < autocvar_g_balance_sniperrifle_reload_ammo && self.ammo_nails) // make sure we don't add more ammo than we have
28         {
29                 self.ammo_counter += 1;
30                 self.ammo_nails -= 1;
31         }
32         self.sniperrifle_load = self.ammo_counter;
33
34         t = ATTACK_FINISHED(self) - autocvar_g_balance_sniperrifle_reload_time - 1;
35         ATTACK_FINISHED(self) = t;
36         w_ready();
37 }
38
39 void W_SniperRifle_Reload()
40 {
41         // return if reloading is disabled for this weapon
42         if(!autocvar_g_balance_sniperrifle_reload_ammo)
43                 return;
44
45         if(!W_ReloadCheck(self.ammo_nails))
46                 return;
47
48         float t;
49
50         sound (self, CHAN_WEAPON2, "weapons/campingrifle_reload.wav", VOL_BASE, ATTN_NORM);
51
52         t = max(time, ATTACK_FINISHED(self)) + autocvar_g_balance_sniperrifle_reload_time + 1;
53         ATTACK_FINISHED(self) = t;
54
55         weapon_thinkf(WFRAME_RELOAD, autocvar_g_balance_sniperrifle_reload_time, W_SniperRifle_ReloadedAndReady);
56
57         self.old_ammo_counter = self.ammo_counter;
58         self.ammo_counter = -1;
59 }
60
61 void W_SniperRifle_FireBullet(float pSpread, float pDamage, float pHeadshotAddedDamage, float pForce, float pSpeed, float pLifetime, float pAmmo, float deathtype, float pBulletConstant)
62 {
63         if(deathtype & HITTYPE_SECONDARY)
64                 W_SetupShot (self, autocvar_g_antilag_bullets && pSpeed >= autocvar_g_antilag_bullets, 2, "weapons/campingrifle_fire2.wav", CHAN_WEAPON, autocvar_g_balance_sniperrifle_secondary_damage + autocvar_g_balance_sniperrifle_secondary_headshotaddeddamage);
65         else
66                 W_SetupShot (self, autocvar_g_antilag_bullets && pSpeed >= autocvar_g_antilag_bullets, 2, "weapons/campingrifle_fire.wav", CHAN_WEAPON, autocvar_g_balance_sniperrifle_primary_damage + autocvar_g_balance_sniperrifle_primary_headshotaddeddamage);
67
68         pointparticles(particleeffectnum("sniperrifle_muzzleflash"), w_shotorg, w_shotdir * 2000, 1);
69
70         if(self.BUTTON_ZOOM) // if zoomed, shoot from the eye
71         {
72                 w_shotdir = v_forward;
73                 w_shotorg = self.origin + self.view_ofs + ((w_shotorg - self.origin - self.view_ofs) * v_forward) * v_forward;
74         }
75
76         if(deathtype & HITTYPE_SECONDARY)
77                 fireBallisticBullet(w_shotorg, w_shotdir, pSpread, pSpeed, pLifetime, pDamage, pHeadshotAddedDamage / pDamage, pForce, deathtype, (autocvar_g_balance_sniperrifle_secondary_tracer ? EF_RED : EF_BLUE), 1, pBulletConstant);
78         else
79                 fireBallisticBullet(w_shotorg, w_shotdir, pSpread, pSpeed, pLifetime, pDamage, pHeadshotAddedDamage / pDamage, pForce, deathtype, (autocvar_g_balance_sniperrifle_primary_tracer ? EF_RED : EF_BLUE), 1, pBulletConstant);
80         endFireBallisticBullet();
81
82         if (autocvar_g_casings >= 2)
83                 SpawnCasing (((random () * 50 + 50) * v_right) - (v_forward * (random () * 25 + 25)) - ((random () * 5 - 70) * v_up), 2, vectoangles(v_forward),'0 250 0', 100, 3, self);
84
85         // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
86         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
87         {
88                 if(!autocvar_g_balance_sniperrifle_reload_ammo)
89                         self.ammo_nails -= pAmmo;
90                 else
91                 {
92                         self.ammo_counter -= pAmmo;
93                         self.sniperrifle_load = self.ammo_counter;
94                 }
95         }
96 }
97
98 void W_SniperRifle_Attack()
99 {
100         W_SniperRifle_FireBullet(autocvar_g_balance_sniperrifle_primary_spread, autocvar_g_balance_sniperrifle_primary_damage, autocvar_g_balance_sniperrifle_primary_headshotaddeddamage, autocvar_g_balance_sniperrifle_primary_force, autocvar_g_balance_sniperrifle_primary_speed, autocvar_g_balance_sniperrifle_primary_lifetime, autocvar_g_balance_sniperrifle_primary_ammo, WEP_SNIPERRIFLE, autocvar_g_balance_sniperrifle_primary_bulletconstant);
101 }
102
103 void W_SniperRifle_Attack2()
104 {
105         W_SniperRifle_FireBullet(autocvar_g_balance_sniperrifle_secondary_spread, autocvar_g_balance_sniperrifle_secondary_damage, autocvar_g_balance_sniperrifle_secondary_headshotaddeddamage, autocvar_g_balance_sniperrifle_secondary_force, autocvar_g_balance_sniperrifle_secondary_speed, autocvar_g_balance_sniperrifle_secondary_lifetime, autocvar_g_balance_sniperrifle_secondary_ammo, WEP_SNIPERRIFLE | HITTYPE_SECONDARY, autocvar_g_balance_sniperrifle_secondary_bulletconstant);
106 }
107
108 void spawnfunc_weapon_sniperrifle (void)
109 {
110         weapon_defaultspawnfunc(WEP_SNIPERRIFLE);
111 }
112
113 // compatibility alias
114 void spawnfunc_weapon_campingrifle (void)
115 {
116         spawnfunc_weapon_sniperrifle();
117 }
118
119 .void(void) sniperrifle_bullethail_attackfunc;
120 .float sniperrifle_bullethail_frame;
121 .float sniperrifle_bullethail_animtime;
122 .float sniperrifle_bullethail_refire;
123 void W_SniperRifle_BulletHail_Continue()
124 {
125         float r, sw, af;
126
127         if(autocvar_g_balance_sniperrifle_reload_ammo && self.ammo_counter <= 0)
128                 return; // reloading, so we are done
129
130         sw = self.switchweapon; // make it not detect weapon changes as reason to abort firing
131         af = ATTACK_FINISHED(self);
132         self.switchweapon = self.weapon;
133         ATTACK_FINISHED(self) = time;
134         print(ftos(self.ammo_nails), "\n");
135         r = weapon_prepareattack(self.sniperrifle_bullethail_frame == WFRAME_FIRE2, self.sniperrifle_bullethail_refire);
136         if(self.switchweapon == self.weapon)
137                 self.switchweapon = sw;
138         if(r)
139         {
140                 self.sniperrifle_bullethail_attackfunc();
141                 weapon_thinkf(self.sniperrifle_bullethail_frame, self.sniperrifle_bullethail_animtime, W_SniperRifle_BulletHail_Continue);
142                 print("thinkf set\n");
143         }
144         else
145         {
146                 ATTACK_FINISHED(self) = af; // reset attack_finished if we didn't fire, so the last shot enforces the refire time
147                 print("out of ammo... ", ftos(self.weaponentity.state), "\n");
148         }
149 }
150
151 void W_SniperRifle_BulletHail(float mode, void(void) AttackFunc, float fr, float animtime, float refire)
152 {
153         // if we get here, we have at least one bullet to fire
154         AttackFunc();
155         if(mode)
156         {
157                 // continue hail
158                 self.sniperrifle_bullethail_attackfunc = AttackFunc;
159                 self.sniperrifle_bullethail_frame = fr;
160                 self.sniperrifle_bullethail_animtime = animtime;
161                 self.sniperrifle_bullethail_refire = refire;
162                 weapon_thinkf(fr, animtime, W_SniperRifle_BulletHail_Continue);
163         }
164         else
165         {
166                 // just one shot
167                 weapon_thinkf(fr, animtime, w_ready);
168         }
169 }
170
171 .float bot_secondary_sniperriflemooth;
172 float w_sniperrifle(float req)
173 {
174         if (req == WR_AIM)
175         {
176                 self.BUTTON_ATCK=FALSE;
177                 self.BUTTON_ATCK2=FALSE;
178                 if(vlen(self.origin-self.enemy.origin) > 1000)
179                         self.bot_secondary_sniperriflemooth = 0;
180                 if(self.bot_secondary_sniperriflemooth == 0)
181                 {
182                         if(bot_aim(autocvar_g_balance_sniperrifle_primary_speed, 0, autocvar_g_balance_sniperrifle_primary_lifetime, TRUE))
183                         {
184                                 self.BUTTON_ATCK = TRUE;
185                                 if(random() < 0.01) self.bot_secondary_sniperriflemooth = 1;
186                         }
187                 }
188                 else
189                 {
190                         if(bot_aim(autocvar_g_balance_sniperrifle_secondary_speed, 0, autocvar_g_balance_sniperrifle_secondary_lifetime, TRUE))
191                         {
192                                 self.BUTTON_ATCK2 = TRUE;
193                                 if(random() < 0.03) self.bot_secondary_sniperriflemooth = 0;
194                         }
195                 }
196         }
197         else if (req == WR_THINK)
198         {
199                 if(autocvar_g_balance_sniperrifle_reload_ammo && self.ammo_counter <= 0) // forced reload
200             W_SniperRifle_Reload();
201                 else
202                 {
203                         self.sniperrifle_accumulator = bound(time - autocvar_g_balance_sniperrifle_bursttime, self.sniperrifle_accumulator, time);
204                         if (self.BUTTON_ATCK)
205                         if (weapon_prepareattack_check(0, autocvar_g_balance_sniperrifle_primary_refire))
206                         if (time >= self.sniperrifle_accumulator + autocvar_g_balance_sniperrifle_primary_burstcost)
207                         {
208                                 weapon_prepareattack_do(0, autocvar_g_balance_sniperrifle_primary_refire);
209                                 W_SniperRifle_BulletHail(autocvar_g_balance_sniperrifle_primary_bullethail, W_SniperRifle_Attack, WFRAME_FIRE1, autocvar_g_balance_sniperrifle_primary_animtime, autocvar_g_balance_sniperrifle_primary_refire);
210                                 self.sniperrifle_accumulator += autocvar_g_balance_sniperrifle_primary_burstcost;
211                         }
212                         if (self.BUTTON_ATCK2)
213                         {       
214                                 if (autocvar_g_balance_sniperrifle_secondary)
215                                 {
216                     if(autocvar_g_balance_sniperrifle_secondary_reload)
217                         W_SniperRifle_Reload();
218                     else
219                     {
220                         if (weapon_prepareattack_check(1, autocvar_g_balance_sniperrifle_secondary_refire))
221                         if (time >= self.sniperrifle_accumulator + autocvar_g_balance_sniperrifle_secondary_burstcost)
222                         {
223                             weapon_prepareattack_do(1, autocvar_g_balance_sniperrifle_secondary_refire);
224                             W_SniperRifle_BulletHail(autocvar_g_balance_sniperrifle_secondary_bullethail, W_SniperRifle_Attack2, WFRAME_FIRE2, autocvar_g_balance_sniperrifle_secondary_animtime, autocvar_g_balance_sniperrifle_primary_refire);
225                             self.sniperrifle_accumulator += autocvar_g_balance_sniperrifle_secondary_burstcost;
226                         }
227                     }
228                                 }
229                         }
230                 }
231         if(self.wish_reload)
232         {
233             if(self.switchweapon == self.weapon)
234             {
235                 if(self.weaponentity.state == WS_READY)
236                 {
237                     self.wish_reload = 0;
238                     W_SniperRifle_Reload();
239                 }
240             }
241         }
242         }
243         else if (req == WR_PRECACHE)
244         {
245                 precache_model ("models/weapons/g_campingrifle.md3");
246                 precache_model ("models/weapons/v_campingrifle.md3");
247                 precache_model ("models/weapons/h_campingrifle.iqm");
248                 precache_sound ("weapons/campingrifle_reload.wav");
249                 precache_sound ("weapons/campingrifle_fire.wav");
250                 precache_sound ("weapons/campingrifle_fire2.wav");
251         }
252         else if (req == WR_SETUP)
253         {
254                 weapon_setup(WEP_SNIPERRIFLE);
255                 W_SniperRifle_SetAmmoCounter();
256         }
257         else if (req == WR_CHECKAMMO1)
258         {
259                 return self.ammo_nails >= autocvar_g_balance_sniperrifle_primary_ammo;
260         }
261         else if (req == WR_CHECKAMMO2)
262         {
263                 return self.ammo_nails >= autocvar_g_balance_sniperrifle_secondary_ammo;
264         }
265         else if (req == WR_RELOAD)
266         {
267                 W_SniperRifle_Reload();
268         }
269         else if (req == WR_RESETPLAYER)
270         {
271                 self.sniperrifle_accumulator = time - autocvar_g_balance_sniperrifle_bursttime;
272                 self.ammo_counter = autocvar_g_balance_sniperrifle_reload_ammo;
273         }
274         return TRUE;
275 };
276 #endif
277 #ifdef CSQC
278 float w_sniperrifle(float req)
279 {
280         if(req == WR_IMPACTEFFECT)
281         {
282                 vector org2;
283                 org2 = w_org + w_backoff * 2;
284                 pointparticles(particleeffectnum("machinegun_impact"), org2, w_backoff * 1000, 1);
285                 if(!w_issilent)
286                 {
287                         if(w_random < 0.2)
288                                 sound(self, CHAN_PROJECTILE, "weapons/ric1.wav", VOL_BASE, ATTN_NORM);
289                         else if(w_random < 0.4)
290                                 sound(self, CHAN_PROJECTILE, "weapons/ric2.wav", VOL_BASE, ATTN_NORM);
291                         else if(w_random < 0.5)
292                                 sound(self, CHAN_PROJECTILE, "weapons/ric3.wav", VOL_BASE, ATTN_NORM);
293                 }
294         }
295         else if(req == WR_PRECACHE)
296         {
297                 precache_sound("weapons/ric1.wav");
298                 precache_sound("weapons/ric2.wav");
299                 precache_sound("weapons/ric3.wav");
300         }
301         else if (req == WR_SUICIDEMESSAGE)
302         {
303                 if(w_deathtype & HITTYPE_SECONDARY)
304                         w_deathtypestring = "%s shot themself automatically";
305                 else
306                         w_deathtypestring = "%s sniped themself somehow";
307         }
308         else if (req == WR_KILLMESSAGE)
309         {
310                 if(w_deathtype & HITTYPE_SECONDARY)
311                 {
312                         if(w_deathtype & HITTYPE_BOUNCE)
313                                 w_deathtypestring = "%s failed to hide from %s's bullet hail";
314                         else
315                                 w_deathtypestring = "%s died in %s's bullet hail";
316                 }
317                 else
318                 {
319                         if(w_deathtype & HITTYPE_BOUNCE)
320                         {
321                                 // TODO special headshot message here too?
322                                 w_deathtypestring = "%s failed to hide from %s's rifle";
323                         }
324                         else
325                         {
326                                 if(w_deathtype & HITTYPE_HEADSHOT)
327                                         w_deathtypestring = "%s got hit in the head by %s";
328                                 else
329                                         w_deathtypestring = "%s was sniped by %s";
330                         }
331                 }
332         }
333         return TRUE;
334 }
335 #endif
336 #endif