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