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