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