]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_rifle.qc
Rename WR_PRECACHE to WR_INIT, which is more appropriate
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / 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 pSpeed, float pLifetime, float pAmmo, float deathtype, float pBulletConstant, 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, autocvar_g_antilag_bullets && pSpeed >= autocvar_g_antilag_bullets, 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                 fireBallisticBullet(w_shotorg, w_shotdir, pSpread, pSpeed, pLifetime, pDamage, pForce, deathtype, (pTracer ? EF_RED : EF_BLUE), 1, pBulletConstant);
36         endFireBallisticBullet();
37
38         if (autocvar_g_casings >= 2)
39                 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);
40 }
41
42 void W_Rifle_Attack()
43 {
44         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_speed, autocvar_g_balance_rifle_primary_lifetime, autocvar_g_balance_rifle_primary_ammo, WEP_RIFLE, autocvar_g_balance_rifle_primary_bulletconstant, autocvar_g_balance_rifle_primary_tracer, autocvar_g_balance_rifle_primary_shots, "weapons/campingrifle_fire.wav");
45 }
46
47 void W_Rifle_Attack2()
48 {
49         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_speed, autocvar_g_balance_rifle_secondary_lifetime, autocvar_g_balance_rifle_secondary_ammo, WEP_RIFLE | HITTYPE_SECONDARY, autocvar_g_balance_rifle_secondary_bulletconstant, autocvar_g_balance_rifle_secondary_tracer, autocvar_g_balance_rifle_secondary_shots, "weapons/campingrifle_fire2.wav");
50 }
51
52 void spawnfunc_weapon_rifle (void)
53 {
54         weapon_defaultspawnfunc(WEP_RIFLE);
55 }
56
57 // compatibility alias
58 void spawnfunc_weapon_campingrifle (void)
59 {
60         spawnfunc_weapon_rifle();
61 }
62 void spawnfunc_weapon_sniperrifle (void)
63 {
64         spawnfunc_weapon_rifle();
65 }
66
67 .void(void) rifle_bullethail_attackfunc;
68 .float rifle_bullethail_frame;
69 .float rifle_bullethail_animtime;
70 .float rifle_bullethail_refire;
71 void W_Rifle_BulletHail_Continue()
72 {
73         float r, sw, af;
74
75         sw = self.switchweapon; // make it not detect weapon changes as reason to abort firing
76         af = ATTACK_FINISHED(self);
77         self.switchweapon = self.weapon;
78         ATTACK_FINISHED(self) = time;
79         print(ftos(self.ammo_nails), "\n");
80         r = weapon_prepareattack(self.rifle_bullethail_frame == WFRAME_FIRE2, self.rifle_bullethail_refire);
81         if(self.switchweapon == self.weapon)
82                 self.switchweapon = sw;
83         if(r)
84         {
85                 self.rifle_bullethail_attackfunc();
86                 weapon_thinkf(self.rifle_bullethail_frame, self.rifle_bullethail_animtime, W_Rifle_BulletHail_Continue);
87                 print("thinkf set\n");
88         }
89         else
90         {
91                 ATTACK_FINISHED(self) = af; // reset attack_finished if we didn't fire, so the last shot enforces the refire time
92                 print("out of ammo... ", ftos(self.weaponentity.state), "\n");
93         }
94 }
95
96 void W_Rifle_BulletHail(float mode, void(void) AttackFunc, float fr, float animtime, float refire)
97 {
98         // if we get here, we have at least one bullet to fire
99         AttackFunc();
100         if(mode)
101         {
102                 // continue hail
103                 self.rifle_bullethail_attackfunc = AttackFunc;
104                 self.rifle_bullethail_frame = fr;
105                 self.rifle_bullethail_animtime = animtime;
106                 self.rifle_bullethail_refire = refire;
107                 weapon_thinkf(fr, animtime, W_Rifle_BulletHail_Continue);
108         }
109         else
110         {
111                 // just one shot
112                 weapon_thinkf(fr, animtime, w_ready);
113         }
114 }
115
116 .float bot_secondary_riflemooth;
117 float w_rifle(float req)
118 {
119         float ammo_amount;
120         
121         switch(req)
122         {
123                 case WR_AIM:
124                 {
125                         self.BUTTON_ATCK=FALSE;
126                         self.BUTTON_ATCK2=FALSE;
127                         if(vlen(self.origin-self.enemy.origin) > 1000)
128                                 self.bot_secondary_riflemooth = 0;
129                         if(self.bot_secondary_riflemooth == 0)
130                         {
131                                 if(bot_aim(autocvar_g_balance_rifle_primary_speed, 0, autocvar_g_balance_rifle_primary_lifetime, FALSE))
132                                 {
133                                         self.BUTTON_ATCK = TRUE;
134                                         if(random() < 0.01) self.bot_secondary_riflemooth = 1;
135                                 }
136                         }
137                         else
138                         {
139                                 if(bot_aim(autocvar_g_balance_rifle_secondary_speed, 0, autocvar_g_balance_rifle_secondary_lifetime, FALSE))
140                                 {
141                                         self.BUTTON_ATCK2 = TRUE;
142                                         if(random() < 0.03) self.bot_secondary_riflemooth = 0;
143                                 }
144                         }
145                         
146                         return TRUE;
147                 }
148                 case WR_THINK:
149                 {
150                         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
151                                 WEP_ACTION(self.weapon, WR_RELOAD);
152                         else
153                         {
154                                 self.rifle_accumulator = bound(time - autocvar_g_balance_rifle_bursttime, self.rifle_accumulator, time);
155                                 if (self.BUTTON_ATCK)
156                                 if (weapon_prepareattack_check(0, autocvar_g_balance_rifle_primary_refire))
157                                 if (time >= self.rifle_accumulator + autocvar_g_balance_rifle_primary_burstcost)
158                                 {
159                                         weapon_prepareattack_do(0, autocvar_g_balance_rifle_primary_refire);
160                                         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);
161                                         self.rifle_accumulator += autocvar_g_balance_rifle_primary_burstcost;
162                                 }
163                                 if (self.BUTTON_ATCK2)
164                                 {
165                                         if (autocvar_g_balance_rifle_secondary)
166                                         {
167                                                 if(autocvar_g_balance_rifle_secondary_reload)
168                                                         WEP_ACTION(self.weapon, WR_RELOAD);
169                                                 else
170                                                 {
171                                                         if (weapon_prepareattack_check(1, autocvar_g_balance_rifle_secondary_refire))
172                                                         if (time >= self.rifle_accumulator + autocvar_g_balance_rifle_secondary_burstcost)
173                                                         {
174                                                                 weapon_prepareattack_do(1, autocvar_g_balance_rifle_secondary_refire);
175                                                                 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);
176                                                                 self.rifle_accumulator += autocvar_g_balance_rifle_secondary_burstcost;
177                                                         }
178                                                 }
179                                         }
180                                 }
181                         }
182                         
183                         return TRUE;
184                 }
185                 case WR_INIT:
186                 {
187                         precache_model ("models/weapons/g_campingrifle.md3");
188                         precache_model ("models/weapons/v_campingrifle.md3");
189                         precache_model ("models/weapons/h_campingrifle.iqm");
190                         precache_sound ("weapons/campingrifle_fire.wav");
191                         precache_sound ("weapons/campingrifle_fire2.wav");
192                         return TRUE;
193                 }
194                 case WR_SETUP:
195                 {
196                         weapon_setup(WEP_RIFLE);
197                         self.current_ammo = ammo_nails;
198                         return TRUE;
199                 }
200                 case WR_CHECKAMMO1:
201                 {
202                         ammo_amount = self.ammo_nails >= autocvar_g_balance_rifle_primary_ammo;
203                         ammo_amount += self.(weapon_load[WEP_RIFLE]) >= autocvar_g_balance_rifle_primary_ammo;
204                         return ammo_amount;
205                 }
206                 case WR_CHECKAMMO2:
207                 {
208                         ammo_amount = self.ammo_nails >= autocvar_g_balance_rifle_secondary_ammo;
209                         ammo_amount += self.(weapon_load[WEP_RIFLE]) >= autocvar_g_balance_rifle_secondary_ammo;
210                         return ammo_amount;
211                 }
212                 case WR_RESETPLAYER:
213                 {
214                         self.rifle_accumulator = time - autocvar_g_balance_rifle_bursttime;
215                         return TRUE;
216                 }
217                 case WR_RELOAD:
218                 {
219                         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");
220                         return TRUE;
221                 }
222                 case WR_SUICIDEMESSAGE:
223                 {
224                         return WEAPON_THINKING_WITH_PORTALS;
225                 }
226                 case WR_KILLMESSAGE:
227                 {
228                         if(w_deathtype & HITTYPE_SECONDARY)
229                         {
230                                 if(w_deathtype & HITTYPE_BOUNCE)
231                                         return WEAPON_RIFLE_MURDER_HAIL_PIERCING;
232                                 else
233                                         return WEAPON_RIFLE_MURDER_HAIL;
234                         }
235                         else
236                         {
237                                 if(w_deathtype & HITTYPE_BOUNCE)
238                                         return WEAPON_RIFLE_MURDER_PIERCING;
239                                 else
240                                         return WEAPON_RIFLE_MURDER;
241                         }
242                 }
243         }
244         return TRUE;
245 }
246 #endif
247 #ifdef CSQC
248 float w_rifle(float req)
249 {
250         switch(req)
251         {
252                 case WR_IMPACTEFFECT:
253                 {
254                         vector org2;
255                         org2 = w_org + w_backoff * 2;
256                         pointparticles(particleeffectnum("machinegun_impact"), org2, w_backoff * 1000, 1);
257                         if(!w_issilent)
258                         {
259                                 if(w_random < 0.2)
260                                         sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTN_NORM);
261                                 else if(w_random < 0.4)
262                                         sound(self, CH_SHOTS, "weapons/ric2.wav", VOL_BASE, ATTN_NORM);
263                                 else if(w_random < 0.5)
264                                         sound(self, CH_SHOTS, "weapons/ric3.wav", VOL_BASE, ATTN_NORM);
265                         }
266                         
267                         return TRUE;
268                 }
269                 case WR_INIT:
270                 {
271                         precache_sound("weapons/ric1.wav");
272                         precache_sound("weapons/ric2.wav");
273                         precache_sound("weapons/ric3.wav");
274                         return TRUE;
275                 }
276         }
277
278         return TRUE;
279 }
280 #endif
281 #endif