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