]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_rifle.qc
No need for that extra prefix
[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 */ ammo_nails,
6 /* impulse  */ 7,
7 /* flags    */ WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_RELOADABLE | WEP_TYPE_HITSCAN,
8 /* rating   */ BOT_PICKUP_RATING_MID,
9 /* model    */ "campingrifle",
10 /* netname  */ "rifle",
11 /* fullname */ _("Rifle")
12 );
13
14 #define RIFLE_SETTINGS(w_cvar,w_prop) RIFLE_SETTINGS_LIST(w_cvar, w_prop, RIFLE, rifle)
15 #define RIFLE_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
16         w_cvar(id, sn, BOTH, ammo) \
17         w_cvar(id, sn, BOTH, animtime) \
18         w_cvar(id, sn, BOTH, bulletconstant) \
19         w_cvar(id, sn, BOTH, bullethail) \
20         w_cvar(id, sn, BOTH, burstcost) \
21         w_cvar(id, sn, BOTH, damage) \
22         w_cvar(id, sn, BOTH, force) \
23         w_cvar(id, sn, BOTH, lifetime) \
24         w_cvar(id, sn, BOTH, refire) \
25         w_cvar(id, sn, BOTH, shots) \
26         w_cvar(id, sn, BOTH, speed) \
27         w_cvar(id, sn, BOTH, spread) \
28         w_cvar(id, sn, BOTH, tracer) \
29         w_cvar(id, sn, NONE, bursttime) \
30         w_cvar(id, sn, NONE, secondary) \
31         w_cvar(id, sn, SEC,  reload) \
32         w_prop(id, sn, float,  reloading_ammo, reload_ammo) \
33         w_prop(id, sn, float,  reloading_time, reload_time) \
34         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
35         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
36         w_prop(id, sn, string, weaponreplace, weaponreplace) \
37         w_prop(id, sn, float,  weaponstart, weaponstart) \
38         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride)
39
40 #ifdef SVQC
41 RIFLE_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
42 .float rifle_accumulator;
43 #endif
44 #else
45 #ifdef SVQC
46 void spawnfunc_weapon_rifle (void) { weapon_defaultspawnfunc(WEP_RIFLE); }
47 void spawnfunc_weapon_campingrifle (void) { spawnfunc_weapon_rifle(); }
48 void spawnfunc_weapon_sniperrifle (void) { spawnfunc_weapon_rifle(); }
49
50 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)
51 {
52         float i;
53
54         W_DecreaseAmmo(pAmmo);
55
56         W_SetupShot (self, TRUE, 2, pSound, CH_WEAPON_A, pDamage * pShots);
57
58         pointparticles(particleeffectnum("rifle_muzzleflash"), w_shotorg, w_shotdir * 2000, 1);
59
60         if(self.BUTTON_ZOOM | self.BUTTON_ZOOMSCRIPT) // if zoomed, shoot from the eye
61         {
62                 w_shotdir = v_forward;
63                 w_shotorg = self.origin + self.view_ofs + ((w_shotorg - self.origin - self.view_ofs) * v_forward) * v_forward;
64         }
65
66         for(i = 0; i < pShots; ++i)
67                 fireBallisticBullet(w_shotorg, w_shotdir, pSpread, pSpeed, pLifetime, pDamage, pForce, deathtype, (pTracer ? EF_RED : EF_BLUE), pBulletConstant);
68         endFireBallisticBullet();
69
70         if (autocvar_g_casings >= 2)
71                 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);
72 }
73
74 void W_Rifle_Attack()
75 {
76         W_Rifle_FireBullet(WEP_CVAR_PRI(rifle, spread), WEP_CVAR_PRI(rifle, damage), WEP_CVAR_PRI(rifle, force), WEP_CVAR_PRI(rifle, speed), WEP_CVAR_PRI(rifle, lifetime), WEP_CVAR_PRI(rifle, ammo), WEP_RIFLE, WEP_CVAR_PRI(rifle, bulletconstant), WEP_CVAR_PRI(rifle, tracer), WEP_CVAR_PRI(rifle, shots), "weapons/campingrifle_fire.wav");
77 }
78
79 void W_Rifle_Attack2()
80 {
81         W_Rifle_FireBullet(WEP_CVAR_SEC(rifle, spread), WEP_CVAR_SEC(rifle, damage), WEP_CVAR_SEC(rifle, force), WEP_CVAR_SEC(rifle, speed), WEP_CVAR_SEC(rifle, lifetime), WEP_CVAR_SEC(rifle, ammo), WEP_RIFLE | HITTYPE_SECONDARY, WEP_CVAR_SEC(rifle, bulletconstant), WEP_CVAR_SEC(rifle, tracer), WEP_CVAR_SEC(rifle, shots), "weapons/campingrifle_fire2.wav");
82 }
83
84 .void(void) rifle_bullethail_attackfunc;
85 .float rifle_bullethail_frame;
86 .float rifle_bullethail_animtime;
87 .float rifle_bullethail_refire;
88 void W_Rifle_BulletHail_Continue()
89 {
90         float r, sw, af;
91
92         sw = self.switchweapon; // make it not detect weapon changes as reason to abort firing
93         af = ATTACK_FINISHED(self);
94         self.switchweapon = self.weapon;
95         ATTACK_FINISHED(self) = time;
96         print(ftos(self.ammo_nails), "\n");
97         r = weapon_prepareattack(self.rifle_bullethail_frame == WFRAME_FIRE2, self.rifle_bullethail_refire);
98         if(self.switchweapon == self.weapon)
99                 self.switchweapon = sw;
100         if(r)
101         {
102                 self.rifle_bullethail_attackfunc();
103                 weapon_thinkf(self.rifle_bullethail_frame, self.rifle_bullethail_animtime, W_Rifle_BulletHail_Continue);
104                 print("thinkf set\n");
105         }
106         else
107         {
108                 ATTACK_FINISHED(self) = af; // reset attack_finished if we didn't fire, so the last shot enforces the refire time
109                 print("out of ammo... ", ftos(self.weaponentity.state), "\n");
110         }
111 }
112
113 void W_Rifle_BulletHail(float mode, void(void) AttackFunc, float fr, float animtime, float refire)
114 {
115         // if we get here, we have at least one bullet to fire
116         AttackFunc();
117         if(mode)
118         {
119                 // continue hail
120                 self.rifle_bullethail_attackfunc = AttackFunc;
121                 self.rifle_bullethail_frame = fr;
122                 self.rifle_bullethail_animtime = animtime;
123                 self.rifle_bullethail_refire = refire;
124                 weapon_thinkf(fr, animtime, W_Rifle_BulletHail_Continue);
125         }
126         else
127         {
128                 // just one shot
129                 weapon_thinkf(fr, animtime, w_ready);
130         }
131 }
132
133 .float bot_secondary_riflemooth;
134 float W_Rifle(float req)
135 {
136         float ammo_amount;
137         
138         switch(req)
139         {
140                 case WR_AIM:
141                 {
142                         self.BUTTON_ATCK=FALSE;
143                         self.BUTTON_ATCK2=FALSE;
144                         if(vlen(self.origin-self.enemy.origin) > 1000)
145                                 self.bot_secondary_riflemooth = 0;
146                         if(self.bot_secondary_riflemooth == 0)
147                         {
148                                 if(bot_aim(WEP_CVAR_PRI(rifle, speed), 0, WEP_CVAR_PRI(rifle, lifetime), FALSE))
149                                 {
150                                         self.BUTTON_ATCK = TRUE;
151                                         if(random() < 0.01) self.bot_secondary_riflemooth = 1;
152                                 }
153                         }
154                         else
155                         {
156                                 if(bot_aim(WEP_CVAR_SEC(rifle, speed), 0, WEP_CVAR_SEC(rifle, lifetime), FALSE))
157                                 {
158                                         self.BUTTON_ATCK2 = TRUE;
159                                         if(random() < 0.03) self.bot_secondary_riflemooth = 0;
160                                 }
161                         }
162                         
163                         return TRUE;
164                 }
165                 case WR_THINK:
166                 {
167                         if(autocvar_g_balance_rifle_reload_ammo && self.clip_load < min(WEP_CVAR_PRI(rifle, ammo), WEP_CVAR_SEC(rifle, ammo))) // forced reload
168                                 WEP_ACTION(self.weapon, WR_RELOAD);
169                         else
170                         {
171                                 self.rifle_accumulator = bound(time - WEP_CVAR(rifle, bursttime), self.rifle_accumulator, time);
172                                 if (self.BUTTON_ATCK)
173                                 if (weapon_prepareattack_check(0, WEP_CVAR_PRI(rifle, refire)))
174                                 if (time >= self.rifle_accumulator + WEP_CVAR_PRI(rifle, burstcost))
175                                 {
176                                         weapon_prepareattack_do(0, WEP_CVAR_PRI(rifle, refire));
177                                         W_Rifle_BulletHail(WEP_CVAR_PRI(rifle, bullethail), W_Rifle_Attack, WFRAME_FIRE1, WEP_CVAR_PRI(rifle, animtime), WEP_CVAR_PRI(rifle, refire));
178                                         self.rifle_accumulator += WEP_CVAR_PRI(rifle, burstcost);
179                                 }
180                                 if (self.BUTTON_ATCK2)
181                                 {
182                                         if (WEP_CVAR(rifle, secondary))
183                                         {
184                                                 if(WEP_CVAR_SEC(rifle, reload))
185                                                         WEP_ACTION(self.weapon, WR_RELOAD);
186                                                 else
187                                                 {
188                                                         if (weapon_prepareattack_check(1, WEP_CVAR_SEC(rifle, refire)))
189                                                         if (time >= self.rifle_accumulator + WEP_CVAR_SEC(rifle, burstcost))
190                                                         {
191                                                                 weapon_prepareattack_do(1, WEP_CVAR_SEC(rifle, refire));
192                                                                 W_Rifle_BulletHail(WEP_CVAR_SEC(rifle, bullethail), W_Rifle_Attack2, WFRAME_FIRE2, WEP_CVAR_SEC(rifle, animtime), WEP_CVAR_PRI(rifle, refire));
193                                                                 self.rifle_accumulator += WEP_CVAR_SEC(rifle, burstcost);
194                                                         }
195                                                 }
196                                         }
197                                 }
198                         }
199                         
200                         return TRUE;
201                 }
202                 case WR_INIT:
203                 {
204                         precache_model ("models/weapons/g_campingrifle.md3");
205                         precache_model ("models/weapons/v_campingrifle.md3");
206                         precache_model ("models/weapons/h_campingrifle.iqm");
207                         precache_sound ("weapons/campingrifle_fire.wav");
208                         precache_sound ("weapons/campingrifle_fire2.wav");
209                         RIFLE_SETTINGS(WEP_SKIPCVAR, WEP_SET_PROP)
210                         return TRUE;
211                 }
212                 case WR_SETUP:
213                 {
214                         self.current_ammo = ammo_nails;
215                         return TRUE;
216                 }
217                 case WR_CHECKAMMO1:
218                 {
219                         ammo_amount = self.ammo_nails >= WEP_CVAR_PRI(rifle, ammo);
220                         ammo_amount += self.(weapon_load[WEP_RIFLE]) >= WEP_CVAR_PRI(rifle, ammo);
221                         return ammo_amount;
222                 }
223                 case WR_CHECKAMMO2:
224                 {
225                         ammo_amount = self.ammo_nails >= WEP_CVAR_SEC(rifle, ammo);
226                         ammo_amount += self.(weapon_load[WEP_RIFLE]) >= WEP_CVAR_SEC(rifle, ammo);
227                         return ammo_amount;
228                 }
229                 case WR_CONFIG:
230                 {
231                         RIFLE_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
232                         return TRUE;
233                 }
234                 case WR_RESETPLAYER:
235                 {
236                         self.rifle_accumulator = time - WEP_CVAR(rifle, bursttime);
237                         return TRUE;
238                 }
239                 case WR_RELOAD:
240                 {
241                         W_Reload(min(WEP_CVAR_PRI(rifle, ammo), WEP_CVAR_SEC(rifle, ammo)), "weapons/reload.wav");
242                         return TRUE;
243                 }
244                 case WR_SUICIDEMESSAGE:
245                 {
246                         return WEAPON_THINKING_WITH_PORTALS;
247                 }
248                 case WR_KILLMESSAGE:
249                 {
250                         if(w_deathtype & HITTYPE_SECONDARY)
251                         {
252                                 if(w_deathtype & HITTYPE_BOUNCE)
253                                         return WEAPON_RIFLE_MURDER_HAIL_PIERCING;
254                                 else
255                                         return WEAPON_RIFLE_MURDER_HAIL;
256                         }
257                         else
258                         {
259                                 if(w_deathtype & HITTYPE_BOUNCE)
260                                         return WEAPON_RIFLE_MURDER_PIERCING;
261                                 else
262                                         return WEAPON_RIFLE_MURDER;
263                         }
264                 }
265         }
266         return TRUE;
267 }
268 #endif
269 #ifdef CSQC
270 float W_Rifle(float req)
271 {
272         switch(req)
273         {
274                 case WR_IMPACTEFFECT:
275                 {
276                         vector org2;
277                         org2 = w_org + w_backoff * 2;
278                         pointparticles(particleeffectnum("machinegun_impact"), org2, w_backoff * 1000, 1);
279                         if(!w_issilent)
280                         {
281                                 if(w_random < 0.2)
282                                         sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTN_NORM);
283                                 else if(w_random < 0.4)
284                                         sound(self, CH_SHOTS, "weapons/ric2.wav", VOL_BASE, ATTN_NORM);
285                                 else if(w_random < 0.5)
286                                         sound(self, CH_SHOTS, "weapons/ric3.wav", VOL_BASE, ATTN_NORM);
287                         }
288                         
289                         return TRUE;
290                 }
291                 case WR_INIT:
292                 {
293                         precache_sound("weapons/ric1.wav");
294                         precache_sound("weapons/ric2.wav");
295                         precache_sound("weapons/ric3.wav");
296                         return TRUE;
297                 }
298         }
299
300         return TRUE;
301 }
302 #endif
303 #endif