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