]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_rifle.qc
Make it so that weapon zoom reticles are in weapon code, not view code
[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 /* color        */ '0.5 1 0',
10 /* model    */ "campingrifle",
11 /* netname  */ "rifle",
12 /* fullname */ _("Rifle")
13 );
14
15 #define RIFLE_SETTINGS(w_cvar,w_prop) RIFLE_SETTINGS_LIST(w_cvar, w_prop, RIFLE, rifle)
16 #define RIFLE_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
17         w_cvar(id, sn, BOTH, ammo) \
18         w_cvar(id, sn, BOTH, animtime) \
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, refire) \
24         w_cvar(id, sn, BOTH, shots) \
25         w_cvar(id, sn, BOTH, solidpenetration) \
26         w_cvar(id, sn, BOTH, spread) \
27         w_cvar(id, sn, BOTH, tracer) \
28         w_cvar(id, sn, NONE, bursttime) \
29         w_cvar(id, sn, NONE, secondary) \
30         w_cvar(id, sn, SEC,  reload) \
31         w_prop(id, sn, float,  reloading_ammo, reload_ammo) \
32         w_prop(id, sn, float,  reloading_time, reload_time) \
33         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
34         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
35         w_prop(id, sn, string, weaponreplace, weaponreplace) \
36         w_prop(id, sn, float,  weaponstart, weaponstart) \
37         w_prop(id, sn, 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 pSolidPenetration, float pAmmo, float deathtype, float pTracer, float pShots, string pSound)
50 {
51         float i;
52
53         W_DecreaseAmmo(pAmmo);
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                 fireBullet(w_shotorg, w_shotdir, pSpread, pSolidPenetration, pDamage, pForce, deathtype, (pTracer ? EF_RED : EF_BLUE));
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, solidpenetration), WEP_CVAR_PRI(rifle, ammo), WEP_RIFLE, 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, solidpenetration), WEP_CVAR_SEC(rifle, ammo), WEP_RIFLE | HITTYPE_SECONDARY, 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_VAL(WEP_RIFLE)), "\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(1000000, 0, 0.001, 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(1000000, 0, 0.001, 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_CHECKAMMO1:
211                 {
212                         ammo_amount = self.AMMO_VAL(WEP_RIFLE) >= WEP_CVAR_PRI(rifle, ammo);
213                         ammo_amount += self.(weapon_load[WEP_RIFLE]) >= WEP_CVAR_PRI(rifle, ammo);
214                         return ammo_amount;
215                 }
216                 case WR_CHECKAMMO2:
217                 {
218                         ammo_amount = self.AMMO_VAL(WEP_RIFLE) >= WEP_CVAR_SEC(rifle, ammo);
219                         ammo_amount += self.(weapon_load[WEP_RIFLE]) >= WEP_CVAR_SEC(rifle, ammo);
220                         return ammo_amount;
221                 }
222                 case WR_CONFIG:
223                 {
224                         RIFLE_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
225                         return TRUE;
226                 }
227                 case WR_RESETPLAYER:
228                 {
229                         self.rifle_accumulator = time - WEP_CVAR(rifle, bursttime);
230                         return TRUE;
231                 }
232                 case WR_RELOAD:
233                 {
234                         W_Reload(min(WEP_CVAR_PRI(rifle, ammo), WEP_CVAR_SEC(rifle, ammo)), "weapons/reload.wav");
235                         return TRUE;
236                 }
237                 case WR_SUICIDEMESSAGE:
238                 {
239                         return WEAPON_THINKING_WITH_PORTALS;
240                 }
241                 case WR_KILLMESSAGE:
242                 {
243                         if(w_deathtype & HITTYPE_SECONDARY)
244                         {
245                                 if(w_deathtype & HITTYPE_BOUNCE)
246                                         return WEAPON_RIFLE_MURDER_HAIL_PIERCING;
247                                 else
248                                         return WEAPON_RIFLE_MURDER_HAIL;
249                         }
250                         else
251                         {
252                                 if(w_deathtype & HITTYPE_BOUNCE)
253                                         return WEAPON_RIFLE_MURDER_PIERCING;
254                                 else
255                                         return WEAPON_RIFLE_MURDER;
256                         }
257                 }
258         }
259         return TRUE;
260 }
261 #endif
262 #ifdef CSQC
263 float W_Rifle(float req)
264 {
265         switch(req)
266         {
267                 case WR_IMPACTEFFECT:
268                 {
269                         vector org2;
270                         org2 = w_org + w_backoff * 2;
271                         pointparticles(particleeffectnum("machinegun_impact"), org2, w_backoff * 1000, 1);
272                         if(!w_issilent)
273                         {
274                                 if(w_random < 0.2)
275                                         sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTN_NORM);
276                                 else if(w_random < 0.4)
277                                         sound(self, CH_SHOTS, "weapons/ric2.wav", VOL_BASE, ATTN_NORM);
278                                 else if(w_random < 0.5)
279                                         sound(self, CH_SHOTS, "weapons/ric3.wav", VOL_BASE, ATTN_NORM);
280                         }
281                         
282                         return TRUE;
283                 }
284                 case WR_INIT:
285                 {
286                         precache_sound("weapons/ric1.wav");
287                         precache_sound("weapons/ric2.wav");
288                         precache_sound("weapons/ric3.wav");
289                         if(autocvar_cl_reticle && autocvar_cl_reticle_weapon)
290                         {
291                                 precache_pic("gfx/reticle_nex");
292                         }
293                         return TRUE;
294                 }
295                 case WR_ZOOMRETICLE:
296                 {
297                         if(button_zoom || zoomscript_caught)
298                         {
299                                 reticle_image = "gfx/reticle_nex";
300                                 return TRUE;
301                         }
302                         else
303                         {
304                                 // no weapon specific image for this weapon
305                                 return FALSE;
306                         }
307                 }
308         }
309
310         return TRUE;
311 }
312 #endif
313 #endif