1 #include "weaponsystem.qh"
3 #include "selection.qh"
5 #include "../command/common.qh"
6 #include "../mutators/mutators_include.qh"
7 #include "../round_handler.qh"
8 #include "../t_items.qh"
9 #include "../../common/animdecide.qh"
10 #include "../../common/constants.qh"
11 #include "../../common/monsters/all.qh"
12 #include "../../common/notifications.qh"
13 #include "../../common/util.qh"
14 #include "../../common/weapons/all.qh"
15 #include "../../lib/csqcmodel/sv_model.qh"
18 ===========================================================================
20 CLIENT WEAPONSYSTEM CODE
21 Bring back W_Weaponframe
23 ===========================================================================
28 .float weapon_frametime;
30 float W_WeaponRateFactor()
32 float t = 1.0 / g_weaponratefactor;
34 MUTATOR_CALLHOOK(WeaponRateFactor, t);
40 float W_WeaponSpeedFactor()
42 float t = 1.0 * g_weaponspeedfactor;
44 MUTATOR_CALLHOOK(WeaponSpeedFactor, t);
51 void weapon_thinkf(entity actor, float fr, float t, void(Weapon thiswep, entity actor, bool fire1, bool fire2) func);
53 float CL_Weaponentity_CustomizeEntityForClient()
55 self.viewmodelforclient = self.owner;
57 if(other.enemy == self.owner)
58 self.viewmodelforclient = other;
65 * 1. simple animated model, muzzle flash handling on h_ model:
66 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
68 * shot = muzzle end (shot origin, also used for muzzle flashes)
69 * shell = casings ejection point (must be on the right hand side of the gun)
70 * weapon = attachment for v_tuba.md3
71 * v_tuba.md3 - first and third person model
72 * g_tuba.md3 - pickup model
74 * 2. simple animated model, muzzle flash handling on v_ model:
75 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
77 * weapon = attachment for v_tuba.md3
78 * v_tuba.md3 - first and third person model
80 * shot = muzzle end (shot origin, also used for muzzle flashes)
81 * shell = casings ejection point (must be on the right hand side of the gun)
82 * g_tuba.md3 - pickup model
84 * 3. fully animated model, muzzle flash handling on h_ model:
85 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
87 * shot = muzzle end (shot origin, also used for muzzle flashes)
88 * shell = casings ejection point (must be on the right hand side of the gun)
89 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
90 * v_tuba.md3 - third person model
91 * g_tuba.md3 - pickup model
93 * 4. fully animated model, muzzle flash handling on v_ model:
94 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
96 * shot = muzzle end (shot origin)
97 * shell = casings ejection point (must be on the right hand side of the gun)
98 * v_tuba.md3 - third person model
100 * shot = muzzle end (for muzzle flashes)
101 * g_tuba.md3 - pickup model
105 // self.origin, self.angles
107 // self.movedir, self.view_ofs
111 // call again with ""
113 void CL_WeaponEntity_SetModel(string name)
118 // if there is a child entity, hide it until we're sure we use it
119 if (self.weaponentity)
120 self.weaponentity.model = "";
121 _setmodel(self, W_Model(strcat("v_", name, ".md3")));
122 v_shot_idx = gettagindex(self, "shot"); // used later
124 v_shot_idx = gettagindex(self, "tag_shot");
126 _setmodel(self, W_Model(strcat("h_", name, ".iqm")));
127 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
128 self.anim_fire1 = animfixfps(self, '0 1 0.01', '0 0 0');
129 self.anim_fire2 = animfixfps(self, '1 1 0.01', '0 0 0');
130 self.anim_idle = animfixfps(self, '2 1 0.01', '0 0 0');
131 self.anim_reload = animfixfps(self, '3 1 0.01', '0 0 0');
133 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
134 // if we don't, this is a "real" animated model
135 if(gettagindex(self, "weapon"))
137 if (!self.weaponentity)
138 self.weaponentity = spawn();
139 _setmodel(self.weaponentity, W_Model(strcat("v_", name, ".md3")));
140 setattachment(self.weaponentity, self, "weapon");
142 else if(gettagindex(self, "tag_weapon"))
144 if (!self.weaponentity)
145 self.weaponentity = spawn();
146 _setmodel(self.weaponentity, W_Model(strcat("v_", name, ".md3")));
147 setattachment(self.weaponentity, self, "tag_weapon");
151 if(self.weaponentity)
152 remove(self.weaponentity);
153 self.weaponentity = world;
156 setorigin(self,'0 0 0');
157 self.angles = '0 0 0';
159 self.viewmodelforclient = world;
163 if(v_shot_idx) // v_ model attached to invisible h_ model
165 self.movedir = gettaginfo(self.weaponentity, v_shot_idx);
169 idx = gettagindex(self, "shot");
171 idx = gettagindex(self, "tag_shot");
173 self.movedir = gettaginfo(self, idx);
176 LOG_INFO("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
177 self.movedir = '0 0 0';
181 if(self.weaponentity) // v_ model attached to invisible h_ model
183 idx = gettagindex(self.weaponentity, "shell");
185 idx = gettagindex(self.weaponentity, "tag_shell");
187 self.spawnorigin = gettaginfo(self.weaponentity, idx);
193 idx = gettagindex(self, "shell");
195 idx = gettagindex(self, "tag_shell");
197 self.spawnorigin = gettaginfo(self, idx);
200 LOG_INFO("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
201 self.spawnorigin = self.movedir;
207 self.oldorigin = '0 0 0'; // use regular attachment
211 if(self.weaponentity)
213 idx = gettagindex(self, "weapon");
215 idx = gettagindex(self, "tag_weapon");
219 idx = gettagindex(self, "handle");
221 idx = gettagindex(self, "tag_handle");
225 self.oldorigin = self.movedir - gettaginfo(self, idx);
229 LOG_INFO("WARNING: weapon model ", self.model, " does not support the 'handle' tag and neither does the v_ model support the 'shot' tag, will display muzzle flashes TOTALLY wrong\n");
230 self.oldorigin = '0 0 0'; // there is no way to recover from this
234 self.viewmodelforclient = self.owner;
239 if(self.weaponentity)
240 remove(self.weaponentity);
241 self.weaponentity = world;
242 self.movedir = '0 0 0';
243 self.spawnorigin = '0 0 0';
244 self.oldorigin = '0 0 0';
245 self.anim_fire1 = '0 1 0.01';
246 self.anim_fire2 = '0 1 0.01';
247 self.anim_idle = '0 1 0.01';
248 self.anim_reload = '0 1 0.01';
251 self.view_ofs = '0 0 0';
253 if(self.movedir.x >= 0)
257 self.movedir = shotorg_adjust(v0, false, false);
258 self.view_ofs = shotorg_adjust(v0, false, true) - v0;
260 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
261 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
263 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
265 // check if an instant weapon switch occurred
266 setorigin(self, self.view_ofs);
267 // reset animstate now
268 self.wframe = WFRAME_IDLE;
269 setanim(self, self.anim_idle, true, false, true);
272 vector CL_Weapon_GetShotOrg(float wpn)
274 entity wi = get_weaponinfo(wpn);
276 CL_WeaponEntity_SetModel(wi.mdl);
277 vector ret = self.movedir;
278 CL_WeaponEntity_SetModel("");
284 void CL_Weaponentity_Think()
287 self.nextthink = time;
288 if (intermission_running)
289 self.frame = self.anim_idle.x;
290 if (self.owner.weaponentity != self)
292 if (self.weaponentity)
293 remove(self.weaponentity);
297 if (self.owner.deadflag != DEAD_NO)
300 if (self.weaponentity)
301 self.weaponentity.model = "";
304 if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
306 self.weaponname = self.owner.weaponname;
307 self.dmg = self.owner.modelindex;
308 self.deadflag = self.owner.deadflag;
310 CL_WeaponEntity_SetModel(self.owner.weaponname);
313 tb = (self.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
314 self.effects = self.owner.effects & EFMASK_CHEAP;
315 self.effects &= ~EF_LOWPRECISION;
316 self.effects &= ~EF_FULLBRIGHT; // can mask team color, so get rid of it
317 self.effects &= ~EF_TELEPORT_BIT;
318 self.effects &= ~EF_RESTARTANIM_BIT;
321 if(self.owner.alpha == default_player_alpha)
322 self.alpha = default_weapon_alpha;
323 else if(self.owner.alpha != 0)
324 self.alpha = self.owner.alpha;
328 self.glowmod = self.owner.weaponentity_glowmod;
329 self.colormap = self.owner.colormap;
330 if (self.weaponentity)
332 self.weaponentity.effects = self.effects;
333 self.weaponentity.alpha = self.alpha;
334 self.weaponentity.colormap = self.colormap;
335 self.weaponentity.glowmod = self.glowmod;
338 self.angles = '0 0 0';
340 float f = (self.owner.weapon_nextthink - time);
341 if (self.state == WS_RAISE && !intermission_running)
343 entity newwep = get_weaponinfo(self.owner.switchweapon);
344 f = f * g_weaponratefactor / max(f, newwep.switchdelay_raise);
345 self.angles_x = -90 * f * f;
347 else if (self.state == WS_DROP && !intermission_running)
349 entity oldwep = get_weaponinfo(self.owner.weapon);
350 f = 1 - f * g_weaponratefactor / max(f, oldwep.switchdelay_drop);
351 self.angles_x = -90 * f * f;
353 else if (self.state == WS_CLEAR)
356 self.angles_x = -90 * f * f;
360 void CL_ExteriorWeaponentity_Think()
363 self.nextthink = time;
364 if (self.owner.exteriorweaponentity != self)
369 if (self.owner.deadflag != DEAD_NO)
374 if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
376 self.weaponname = self.owner.weaponname;
377 self.dmg = self.owner.modelindex;
378 self.deadflag = self.owner.deadflag;
379 if (self.owner.weaponname != "")
380 _setmodel(self, W_Model(strcat("v_", self.owner.weaponname, ".md3")));
384 if((tag_found = gettagindex(self.owner, "tag_weapon")))
386 self.tag_index = tag_found;
387 self.tag_entity = self.owner;
390 setattachment(self, self.owner, "bip01 r hand");
392 self.effects = self.owner.effects;
393 self.effects |= EF_LOWPRECISION;
394 self.effects = self.effects & EFMASK_CHEAP; // eat performance
395 if(self.owner.alpha == default_player_alpha)
396 self.alpha = default_weapon_alpha;
397 else if(self.owner.alpha != 0)
398 self.alpha = self.owner.alpha;
402 self.glowmod = self.owner.weaponentity_glowmod;
403 self.colormap = self.owner.colormap;
405 CSQCMODEL_AUTOUPDATE(self);
408 // spawning weaponentity for client
409 void CL_SpawnWeaponentity(entity e)
411 entity view = e.weaponentity = spawn();
412 view.classname = "weaponentity";
413 view.solid = SOLID_NOT;
415 setmodel(view, MDL_Null); // precision set when changed
416 setorigin(view, '0 0 0');
417 view.angles = '0 0 0';
418 view.viewmodelforclient = e;
420 view.think = CL_Weaponentity_Think;
421 view.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
422 view.nextthink = time;
424 entity exterior = e.exteriorweaponentity = spawn();
425 exterior.classname = "exteriorweaponentity";
426 exterior.solid = SOLID_NOT;
427 exterior.exteriorweaponentity = exterior;
429 setorigin(exterior, '0 0 0');
430 exterior.angles = '0 0 0';
431 exterior.think = CL_ExteriorWeaponentity_Think;
432 exterior.nextthink = time;
434 CSQCMODEL_AUTOINIT(exterior);
438 void w_clear(Weapon thiswep, entity actor, bool fire1, bool fire2)
440 if (actor.weapon != -1)
443 actor.switchingweapon = 0;
445 if (actor.weaponentity)
447 actor.weaponentity.state = WS_CLEAR;
448 actor.weaponentity.effects = 0;
452 void w_ready(Weapon thiswep, entity actor, bool fire1, bool fire2)
454 if (actor.weaponentity)
455 actor.weaponentity.state = WS_READY;
456 weapon_thinkf(actor, WFRAME_IDLE, 1000000, w_ready);
461 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, float secondary)
463 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
466 WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
468 WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
470 if (ammo) return true;
471 // always keep the Mine Layer if we placed mines, so that we can detonate them
473 if(actor.weapon == WEP_MINE_LAYER.m_id)
474 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == actor)
477 if(actor.weapon == WEP_SHOTGUN.m_id)
478 if(!secondary && WEP_CVAR(shotgun, secondary) == 1)
479 return false; // no clicking, just allow
481 if(actor.weapon == actor.switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
483 sound (actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
484 actor.prevdryfire = time;
487 // check if the other firing mode has enough ammo
488 bool ammo_other = false;
490 WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
492 WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
496 if (time - actor.prevwarntime > 1)
502 ITEM_WEAPON_PRIMORSEC,
508 actor.prevwarntime = time;
510 else // this weapon is totally unable to fire, switch to another one
512 W_SwitchToOtherWeapon(actor);
518 bool weapon_prepareattack_check(Weapon thiswep, entity actor, bool secondary, float attacktime)
520 if(!weapon_prepareattack_checkammo(thiswep, actor, secondary))
523 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
524 //if all players readied up and the countdown is running
525 if(time < game_starttime || time < actor.race_penalty) {
529 if (timeout_status == TIMEOUT_ACTIVE) //don't allow the player to shoot while game is paused
532 // do not even think about shooting if switching
533 if(actor.switchweapon != actor.weapon)
538 // don't fire if previous attack is not finished
539 if (ATTACK_FINISHED(actor) > time + actor.weapon_frametime * 0.5)
541 // don't fire while changing weapon
542 if (actor.weaponentity.state != WS_READY)
547 void weapon_prepareattack_do(entity actor, bool secondary, float attacktime)
549 actor.weaponentity.state = WS_INUSE;
551 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
553 // if the weapon hasn't been firing continuously, reset the timer
556 if (ATTACK_FINISHED(actor) < time - actor.weapon_frametime * 1.5)
558 ATTACK_FINISHED(actor) = time;
559 //dprint("resetting attack finished to ", ftos(time), "\n");
561 ATTACK_FINISHED(actor) = ATTACK_FINISHED(actor) + attacktime * W_WeaponRateFactor();
563 actor.bulletcounter += 1;
564 //dprint("attack finished ", ftos(ATTACK_FINISHED(actor)), "\n");
566 bool weapon_prepareattack(Weapon thiswep, entity actor, bool secondary, float attacktime)
568 if (weapon_prepareattack_check(thiswep, actor, secondary, attacktime)) {
569 weapon_prepareattack_do(actor, secondary, attacktime);
575 void weapon_thinkf(entity actor, float fr, float t, void(Weapon thiswep, entity actor, bool fire1, bool fire2) func)
581 if(fr == WFRAME_DONTCHANGE)
583 fr = actor.weaponentity.wframe;
586 else if (fr == WFRAME_IDLE)
595 if (actor.weaponentity)
597 actor.weaponentity.wframe = fr;
599 if (fr == WFRAME_IDLE)
600 a = actor.weaponentity.anim_idle;
601 else if (fr == WFRAME_FIRE1)
602 a = actor.weaponentity.anim_fire1;
603 else if (fr == WFRAME_FIRE2)
604 a = actor.weaponentity.anim_fire2;
605 else // if (fr == WFRAME_RELOAD)
606 a = actor.weaponentity.anim_reload;
607 a.z *= g_weaponratefactor;
608 setanim(actor.weaponentity, a, restartanim == false, restartanim, restartanim);
615 if(actor.weapon_think == w_ready && func != w_ready && actor.weaponentity.state == WS_RAISE)
617 backtrace("Tried to override initial weapon think function - should this really happen?");
620 t *= W_WeaponRateFactor();
622 // VorteX: haste can be added here
623 if (actor.weapon_think == w_ready)
625 actor.weapon_nextthink = time;
626 //dprint("started firing at ", ftos(time), "\n");
628 if (actor.weapon_nextthink < time - actor.weapon_frametime * 1.5 || actor.weapon_nextthink > time + actor.weapon_frametime * 1.5)
630 actor.weapon_nextthink = time;
631 //dprint("reset weapon animation timer at ", ftos(time), "\n");
633 actor.weapon_nextthink = actor.weapon_nextthink + t;
634 actor.weapon_think = func;
635 //dprint("next ", ftos(actor.weapon_nextthink), "\n");
637 if((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
639 if((actor.weapon == WEP_SHOCKWAVE.m_id || actor.weapon == WEP_SHOTGUN.m_id) && fr == WFRAME_FIRE2)
640 animdecide_setaction(actor, ANIMACTION_MELEE, restartanim);
642 animdecide_setaction(actor, ANIMACTION_SHOOT, restartanim);
646 if(actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
647 actor.anim_upper_action = 0;
651 float forbidWeaponUse(entity player)
653 if(time < game_starttime && !autocvar_sv_ready_restart_after_countdown)
655 if(round_handler_IsActive() && !round_handler_IsRoundStarted())
657 if(player.player_blocked)
661 if(player.weapon_blocked)
666 .bool hook_switchweapon;
668 void W_WeaponFrame(entity actor)
673 actor.weapon_frametime = frametime;
675 if (!actor.weaponentity || actor.health < 1)
676 return; // Dead player can't use weapons and injure impulse commands
678 if(forbidWeaponUse(actor))
679 if(actor.weaponentity.state != WS_CLEAR)
681 Weapon wpn = get_weaponinfo(actor.weapon);
682 w_ready(wpn, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
686 if(!actor.switchweapon)
689 actor.switchingweapon = 0;
690 actor.weaponentity.state = WS_CLEAR;
691 actor.weaponname = "";
692 //actor.items &= ~IT_AMMO;
696 makevectors(actor.v_angle);
697 fo = v_forward; // save them in case the weapon think functions change it
702 if (actor.weapon != actor.switchweapon)
704 if (actor.weaponentity.state == WS_CLEAR)
707 actor.switchingweapon = actor.switchweapon;
708 entity newwep = get_weaponinfo(actor.switchweapon);
710 // the two weapon entities will notice this has changed and update their models
711 actor.weapon = actor.switchweapon;
712 actor.weaponname = newwep.mdl;
713 actor.bulletcounter = 0;
714 actor.ammo_field = newwep.ammo_field;
715 Weapon w = get_weaponinfo(actor.switchweapon);
717 actor.weaponentity.state = WS_RAISE;
719 // set our clip load to the load of the weapon we switched to, if it's reloadable
720 if(newwep.spawnflags & WEP_FLAG_RELOADABLE && newwep.reloading_ammo) // prevent accessing undefined cvars
722 actor.clip_load = actor.(weapon_load[actor.switchweapon]);
723 actor.clip_size = newwep.reloading_ammo;
726 actor.clip_load = actor.clip_size = 0;
728 weapon_thinkf(actor, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
730 else if (actor.weaponentity.state == WS_DROP)
732 // in dropping phase we can switch at any time
733 actor.switchingweapon = actor.switchweapon;
735 else if (actor.weaponentity.state == WS_READY)
738 actor.switchingweapon = actor.switchweapon;
739 entity oldwep = get_weaponinfo(actor.weapon);
741 // set up weapon switch think in the future, and start drop anim
742 #ifndef INDEPENDENT_ATTACK_FINISHED
743 if(ATTACK_FINISHED(actor) <= time + actor.weapon_frametime * 0.5)
746 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
747 actor.weaponentity.state = WS_DROP;
748 weapon_thinkf(actor, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
749 #ifndef INDEPENDENT_ATTACK_FINISHED
755 // LordHavoc: network timing test code
757 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor)), " >= ", ftos(actor.weapon_nextthink), "\n");
762 // call the think code which may fire the weapon
763 // and do so multiple times to resolve framerate dependency issues if the
764 // server framerate is very low and the weapon fire rate very high
767 while (c < W_TICSPERFRAME)
770 if(w && !(actor.weapons & WepSet_FromWeapon(w)))
772 if(actor.weapon == actor.switchweapon)
773 W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
781 bool block_weapon = false;
783 bool key_pressed = actor.BUTTON_HOOK && !actor.vehicle;
784 Weapon off = actor.offhand;
785 if (off && !(actor.weapons & WEPSET(HOOK))) {
786 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
788 if (key_pressed && actor.switchweapon != WEP_HOOK.m_id && !actor.hook_switchweapon) {
789 W_SwitchWeapon(WEP_HOOK.m_id);
791 actor.hook_switchweapon = key_pressed;
793 block_weapon = (actor.weapon == h.m_id && (actor.BUTTON_ATCK || key_pressed));
794 h.wr_think(h, actor, block_weapon, false);
800 Weapon e = get_weaponinfo(actor.weapon);
801 e.wr_think(e, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
803 Weapon w = get_weaponinfo(actor.weapon);
807 if (time + actor.weapon_frametime * 0.5 >= actor.weapon_nextthink)
809 if(actor.weapon_think)
814 Weapon wpn = get_weaponinfo(actor.weapon);
815 actor.weapon_think(wpn, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
818 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
823 void W_AttachToShotorg(entity actor, entity flash, vector offset)
827 flash.angles_z = random() * 360;
829 if(gettagindex(actor.weaponentity, "shot"))
830 setattachment(flash, actor.weaponentity, "shot");
832 setattachment(flash, actor.weaponentity, "tag_shot");
833 setorigin(flash, offset);
836 copyentity(flash, xflash);
838 flash.viewmodelforclient = actor;
840 if(actor.weaponentity.oldorigin.x > 0)
842 setattachment(xflash, actor.exteriorweaponentity, "");
843 setorigin(xflash, actor.weaponentity.oldorigin + offset);
847 if(gettagindex(actor.exteriorweaponentity, "shot"))
848 setattachment(xflash, actor.exteriorweaponentity, "shot");
850 setattachment(xflash, actor.exteriorweaponentity, "tag_shot");
851 setorigin(xflash, offset);
855 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
858 if(cvar("g_overkill"))
859 if(actor.ok_use_ammocharge)
861 ok_DecreaseCharge(actor, actor.weapon);
865 if((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo)
868 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
869 if(wep.reloading_ammo)
871 actor.clip_load -= ammo_use;
872 actor.(weapon_load[actor.weapon]) = actor.clip_load;
874 else if(wep.ammo_field != ammo_none)
876 actor.(wep.ammo_field) -= ammo_use;
877 if(actor.(wep.ammo_field) < 0)
880 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
881 "Please notify Samual immediately with a copy of this backtrace!\n",
884 GetAmmoPicture(wep.ammo_field),
886 actor.(wep.ammo_field)
892 // weapon reloading code
894 .float reload_ammo_amount, reload_ammo_min, reload_time;
895 .float reload_complain;
896 .string reload_sound;
898 void W_ReloadedAndReady(Weapon thiswep, entity actor, bool fire1, bool fire2)
900 // finish the reloading process, and do the ammo transfer
902 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
904 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
905 if(!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
906 actor.clip_load = actor.reload_ammo_amount;
909 // make sure we don't add more ammo than we have
910 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
911 actor.clip_load += load;
912 actor.(actor.ammo_field) -= load;
914 actor.(weapon_load[actor.weapon]) = actor.clip_load;
916 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
917 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
918 // so your weapon is disabled for a few seconds without reason
920 //ATTACK_FINISHED(actor) -= actor.reload_time - 1;
922 Weapon wpn = get_weaponinfo(actor.weapon);
923 w_ready(wpn, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
926 void W_Reload(entity actor, float sent_ammo_min, string sent_sound)
928 // set global values to work with
930 e = get_weaponinfo(actor.weapon);
932 if(cvar("g_overkill"))
933 if(actor.ok_use_ammocharge)
936 actor.reload_ammo_min = sent_ammo_min;
937 actor.reload_ammo_amount = e.reloading_ammo;
938 actor.reload_time = e.reloading_time;
939 actor.reload_sound = sent_sound;
941 // don't reload weapons that don't have the RELOADABLE flag
942 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
944 LOG_TRACE("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
948 // return if reloading is disabled for this weapon
949 if(!actor.reload_ammo_amount)
952 // our weapon is fully loaded, no need to reload
953 if (actor.clip_load >= actor.reload_ammo_amount)
956 // no ammo, so nothing to load
957 if(actor.ammo_field != ammo_none)
958 if(!actor.(actor.ammo_field) && actor.reload_ammo_min)
959 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
961 if(IS_REAL_CLIENT(actor) && actor.reload_complain < time)
963 play2(actor, SND(UNAVAILABLE));
964 sprint(actor, strcat("You don't have enough ammo to reload the ^2", WEP_NAME(actor.weapon), "\n"));
965 actor.reload_complain = time + 1;
967 // switch away if the amount of ammo is not enough to keep using this weapon
968 Weapon w = get_weaponinfo(actor.weapon);
969 if (!(w.wr_checkammo1(w) + w.wr_checkammo2(w)))
971 actor.clip_load = -1; // reload later
972 W_SwitchToOtherWeapon(actor);
977 if (actor.weaponentity)
979 if (actor.weaponentity.wframe == WFRAME_RELOAD)
982 // allow switching away while reloading, but this will cause a new reload!
983 actor.weaponentity.state = WS_READY;
986 // now begin the reloading process
988 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
990 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
991 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
992 // so your weapon is disabled for a few seconds without reason
994 //ATTACK_FINISHED(actor) = max(time, ATTACK_FINISHED(actor)) + actor.reload_time + 1;
996 weapon_thinkf(actor, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
998 if(actor.clip_load < 0)
1000 actor.old_clip_load = actor.clip_load;
1001 actor.clip_load = actor.(weapon_load[actor.weapon]) = -1;
1004 void W_DropEvent(.void(Weapon) event, entity player, float weapon_type, entity weapon_item)
1007 weapon_dropevent_item = weapon_item;
1008 Weapon w = get_weaponinfo(weapon_type);