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