cvar cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_rifle.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(
3 /* WEP_##id  */ RIFLE,
4 /* function  */ w_rifle,
5 /* ammotype  */ IT_NAILS,
6 /* impulse   */ 7,
7 /* flags     */ WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_TYPE_HITSCAN,
8 /* rating    */ BOT_PICKUP_RATING_MID,
9 /* model     */ "campingrifle",
10 /* shortname */ "rifle",
11 /* fullname  */ _("Rifle")
12 );
13 #else
14 #ifdef SVQC
15
16 .float rifle_accumulator;
17
18 void W_Rifle_FireBullet(float pSpread, float pDamage, float pForce, float pSolidPenetration, float pAmmo, float deathtype, float pTracer, float pShots, string pSound)
19 {
20         float i;
21
22         W_DecreaseAmmo(ammo_nails, pAmmo, autocvar_g_balance_rifle_reload_ammo);
23
24         W_SetupShot (self, TRUE, 2, pSound, CH_WEAPON_A, pDamage * pShots);
25
26         pointparticles(particleeffectnum("rifle_muzzleflash"), w_shotorg, w_shotdir * 2000, 1);
27
28         if(self.BUTTON_ZOOM | self.BUTTON_ZOOMSCRIPT) // if zoomed, shoot from the eye
29         {
30                 w_shotdir = v_forward;
31                 w_shotorg = self.origin + self.view_ofs + ((w_shotorg - self.origin - self.view_ofs) * v_forward) * v_forward;
32         }
33
34         for(i = 0; i < pShots; ++i)
35                 fireBullet(w_shotorg, w_shotdir, pSpread, pSolidPenetration, pDamage, pForce, deathtype, (pTracer ? EF_RED : EF_BLUE));
36
37         if (autocvar_g_casings >= 2)
38                 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);
39 }
40
41 void W_Rifle_Attack()
42 {
43         W_Rifle_FireBullet(autocvar_g_balance_rifle_primary_spread, autocvar_g_balance_rifle_primary_damage, autocvar_g_balance_rifle_primary_force, autocvar_g_balance_rifle_primary_solidpenetration, autocvar_g_balance_rifle_primary_ammo, WEP_RIFLE, autocvar_g_balance_rifle_primary_tracer, autocvar_g_balance_rifle_primary_shots, "weapons/campingrifle_fire.wav");
44 }
45
46 void W_Rifle_Attack2()
47 {
48         W_Rifle_FireBullet(autocvar_g_balance_rifle_secondary_spread, autocvar_g_balance_rifle_secondary_damage, autocvar_g_balance_rifle_secondary_force, autocvar_g_balance_rifle_secondary_solidpenetration, autocvar_g_balance_rifle_secondary_ammo, WEP_RIFLE | HITTYPE_SECONDARY, autocvar_g_balance_rifle_secondary_tracer, autocvar_g_balance_rifle_secondary_shots, "weapons/campingrifle_fire2.wav");
49 }
50
51 void spawnfunc_weapon_rifle (void)
52 {
53         weapon_defaultspawnfunc(WEP_RIFLE);
54 }
55
56 // compatibility alias
57 void spawnfunc_weapon_campingrifle (void)
58 {
59         spawnfunc_weapon_rifle();
60 }
61 void spawnfunc_weapon_sniperrifle (void)
62 {
63         spawnfunc_weapon_rifle();
64 }
65
66 .void(void) rifle_bullethail_attackfunc;
67 .float rifle_bullethail_frame;
68 .float rifle_bullethail_animtime;
69 .float rifle_bullethail_refire;
70 void W_Rifle_BulletHail_Continue()
71 {
72         float r, sw, af;
73
74         sw = self.switchweapon; // make it not detect weapon changes as reason to abort firing
75         af = ATTACK_FINISHED(self);
76         self.switchweapon = self.weapon;
77         ATTACK_FINISHED(self) = time;
78         print(ftos(self.ammo_nails), "\n");
79         r = weapon_prepareattack(self.rifle_bullethail_frame == WFRAME_FIRE2, self.rifle_bullethail_refire);
80         if(self.switchweapon == self.weapon)
81                 self.switchweapon = sw;
82         if(r)
83         {
84                 self.rifle_bullethail_attackfunc();
85                 weapon_thinkf(self.rifle_bullethail_frame, self.rifle_bullethail_animtime, W_Rifle_BulletHail_Continue);
86                 print("thinkf set\n");
87         }
88         else
89         {
90                 ATTACK_FINISHED(self) = af; // reset attack_finished if we didn't fire, so the last shot enforces the refire time
91                 print("out of ammo... ", ftos(self.weaponentity.state), "\n");
92         }
93 }
94
95 void W_Rifle_BulletHail(float mode, void(void) AttackFunc, float fr, float animtime, float refire)
96 {
97         // if we get here, we have at least one bullet to fire
98         AttackFunc();
99         if(mode)
100         {
101                 // continue hail
102                 self.rifle_bullethail_attackfunc = AttackFunc;
103                 self.rifle_bullethail_frame = fr;
104                 self.rifle_bullethail_animtime = animtime;
105                 self.rifle_bullethail_refire = refire;
106                 weapon_thinkf(fr, animtime, W_Rifle_BulletHail_Continue);
107         }
108         else
109         {
110                 // just one shot
111                 weapon_thinkf(fr, animtime, w_ready);
112         }
113 }
114
115 .float bot_secondary_riflemooth;
116 float w_rifle(float req)
117 {
118         float ammo_amount;
119
120         if (req == WR_AIM)
121         {
122                 self.BUTTON_ATCK=FALSE;
123                 self.BUTTON_ATCK2=FALSE;
124                 if(vlen(self.origin-self.enemy.origin) > 1000)
125                         self.bot_secondary_riflemooth = 0;
126                 if(self.bot_secondary_riflemooth == 0)
127                 {
128                         if(bot_aim(1000000, 0, 0.001, FALSE))
129                         {
130                                 self.BUTTON_ATCK = TRUE;
131                                 if(random() < 0.01) self.bot_secondary_riflemooth = 1;
132                         }
133                 }
134                 else
135                 {
136                         if(bot_aim(1000000, 0, 0.001, FALSE))
137                         {
138                                 self.BUTTON_ATCK2 = TRUE;
139                                 if(random() < 0.03) self.bot_secondary_riflemooth = 0;
140                         }
141                 }
142         }
143         else if (req == WR_THINK)
144         {
145                 if(autocvar_g_balance_rifle_reload_ammo && self.clip_load < min(autocvar_g_balance_rifle_primary_ammo, autocvar_g_balance_rifle_secondary_ammo)) // forced reload
146             weapon_action(self.weapon, WR_RELOAD);
147                 else
148                 {
149                         self.rifle_accumulator = bound(time - autocvar_g_balance_rifle_bursttime, self.rifle_accumulator, time);
150                         if (self.BUTTON_ATCK)
151                         if (weapon_prepareattack_check(0, autocvar_g_balance_rifle_primary_refire))
152                         if (time >= self.rifle_accumulator + autocvar_g_balance_rifle_primary_burstcost)
153                         {
154                                 weapon_prepareattack_do(0, autocvar_g_balance_rifle_primary_refire);
155                                 W_Rifle_BulletHail(autocvar_g_balance_rifle_primary_bullethail, W_Rifle_Attack, WFRAME_FIRE1, autocvar_g_balance_rifle_primary_animtime, autocvar_g_balance_rifle_primary_refire);
156                                 self.rifle_accumulator += autocvar_g_balance_rifle_primary_burstcost;
157                         }
158                         if (self.BUTTON_ATCK2)
159                         {
160                                 if (autocvar_g_balance_rifle_secondary)
161                                 {
162                     if(autocvar_g_balance_rifle_secondary_reload)
163                         weapon_action(self.weapon, WR_RELOAD);
164                     else
165                     {
166                         if (weapon_prepareattack_check(1, autocvar_g_balance_rifle_secondary_refire))
167                         if (time >= self.rifle_accumulator + autocvar_g_balance_rifle_secondary_burstcost)
168                         {
169                             weapon_prepareattack_do(1, autocvar_g_balance_rifle_secondary_refire);
170                             W_Rifle_BulletHail(autocvar_g_balance_rifle_secondary_bullethail, W_Rifle_Attack2, WFRAME_FIRE2, autocvar_g_balance_rifle_secondary_animtime, autocvar_g_balance_rifle_primary_refire);
171                             self.rifle_accumulator += autocvar_g_balance_rifle_secondary_burstcost;
172                         }
173                     }
174                                 }
175                         }
176                 }
177         }
178         else if (req == WR_PRECACHE)
179         {
180                 precache_model ("models/weapons/g_campingrifle.md3");
181                 precache_model ("models/weapons/v_campingrifle.md3");
182                 precache_model ("models/weapons/h_campingrifle.iqm");
183                 precache_sound ("weapons/campingrifle_fire.wav");
184                 precache_sound ("weapons/campingrifle_fire2.wav");
185                 //precache_sound ("weapons/reload.wav"); // until weapons have individual reload sounds, precache the reload sound somewhere else
186         }
187         else if (req == WR_SETUP)
188         {
189                 weapon_setup(WEP_RIFLE);
190                 self.current_ammo = ammo_nails;
191         }
192         else if (req == WR_CHECKAMMO1)
193         {
194                 ammo_amount = self.ammo_nails >= autocvar_g_balance_rifle_primary_ammo;
195                 ammo_amount += self.(weapon_load[WEP_RIFLE]) >= autocvar_g_balance_rifle_primary_ammo;
196                 return ammo_amount;
197         }
198         else if (req == WR_CHECKAMMO2)
199         {
200                 ammo_amount = self.ammo_nails >= autocvar_g_balance_rifle_secondary_ammo;
201                 ammo_amount += self.(weapon_load[WEP_RIFLE]) >= autocvar_g_balance_rifle_secondary_ammo;
202                 return ammo_amount;
203         }
204         else if (req == WR_RESETPLAYER)
205         {
206                 self.rifle_accumulator = time - autocvar_g_balance_rifle_bursttime;
207         }
208         else if (req == WR_RELOAD)
209         {
210                 W_Reload(min(autocvar_g_balance_rifle_primary_ammo, autocvar_g_balance_rifle_secondary_ammo), autocvar_g_balance_rifle_reload_ammo, autocvar_g_balance_rifle_reload_time, "weapons/reload.wav");
211         }
212         else if (req == WR_SUICIDEMESSAGE)
213         {
214                 return WEAPON_THINKING_WITH_PORTALS;
215         }
216         else if (req == WR_KILLMESSAGE)
217         {
218                 if(w_deathtype & HITTYPE_SECONDARY)
219                 {
220                         if(w_deathtype & HITTYPE_BOUNCE)
221                                 return WEAPON_RIFLE_MURDER_HAIL_PIERCING;
222                         else
223                                 return WEAPON_RIFLE_MURDER_HAIL;
224                 }
225                 else
226                 {
227                         if(w_deathtype & HITTYPE_BOUNCE)
228                                 return WEAPON_RIFLE_MURDER_PIERCING;
229                         else
230                                 return WEAPON_RIFLE_MURDER;
231                 }
232         }
233         return TRUE;
234 }
235 #endif
236 #ifdef CSQC
237 float w_rifle(float req)
238 {
239         if(req == WR_IMPACTEFFECT)
240         {
241                 vector org2;
242                 org2 = w_org + w_backoff * 2;
243                 pointparticles(particleeffectnum("machinegun_impact"), org2, w_backoff * 1000, 1);
244                 if(!w_issilent)
245                 {
246                         if(w_random < 0.2)
247                                 sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTEN_NORM);
248                         else if(w_random < 0.4)
249                                 sound(self, CH_SHOTS, "weapons/ric2.wav", VOL_BASE, ATTEN_NORM);
250                         else if(w_random < 0.5)
251                                 sound(self, CH_SHOTS, "weapons/ric3.wav", VOL_BASE, ATTEN_NORM);
252                 }
253         }
254         else if(req == WR_PRECACHE)
255         {
256                 precache_sound("weapons/ric1.wav");
257                 precache_sound("weapons/ric2.wav");
258                 precache_sound("weapons/ric3.wav");
259         }
260
261         return TRUE;
262 }
263 #endif
264 #endif