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