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"
17 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float algn)
21 default: case 3: break; // right alignment
22 case 4: vecs.y = -vecs.y; break; // left
23 case 1: case 2: vecs.y = 0; vecs.z -= 2; break; // center
29 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
34 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
35 else if(autocvar_g_shootfromeye)
37 else if(autocvar_g_shootfromcenter)
42 else if((s = autocvar_g_shootfromfixedorigin) != "")
45 if(y_is_right) { v.y = -v.y; }
46 if(v.x != 0) { vecs.x = v.x; }
50 else // just do the same as top
51 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
56 vector shotorg_adjust(vector vecs, bool y_is_right, bool visual, int algn)
58 return shotorg_adjust_values(vecs, y_is_right, visual, algn);
63 .float weapon_frametime;
65 float W_WeaponRateFactor()
67 float t = 1.0 / g_weaponratefactor;
69 MUTATOR_CALLHOOK(WeaponRateFactor, t);
75 float W_WeaponSpeedFactor()
77 float t = 1.0 * g_weaponspeedfactor;
79 MUTATOR_CALLHOOK(WeaponSpeedFactor, t);
86 void weapon_thinkf(entity actor, float fr, float t, void(Weapon thiswep, entity actor, bool fire1, bool fire2) func);
88 float CL_Weaponentity_CustomizeEntityForClient()
90 self.viewmodelforclient = self.owner;
92 if(other.enemy == self.owner)
93 self.viewmodelforclient = other;
100 * 1. simple animated model, muzzle flash handling on h_ model:
101 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
103 * shot = muzzle end (shot origin, also used for muzzle flashes)
104 * shell = casings ejection point (must be on the right hand side of the gun)
105 * weapon = attachment for v_tuba.md3
106 * v_tuba.md3 - first and third person model
107 * g_tuba.md3 - pickup model
109 * 2. simple animated model, muzzle flash handling on v_ model:
110 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
112 * weapon = attachment for v_tuba.md3
113 * v_tuba.md3 - first and third person model
115 * shot = muzzle end (shot origin, also used for muzzle flashes)
116 * shell = casings ejection point (must be on the right hand side of the gun)
117 * g_tuba.md3 - pickup model
119 * 3. fully animated model, muzzle flash handling on h_ model:
120 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
122 * shot = muzzle end (shot origin, also used for muzzle flashes)
123 * shell = casings ejection point (must be on the right hand side of the gun)
124 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
125 * v_tuba.md3 - third person model
126 * g_tuba.md3 - pickup model
128 * 4. fully animated model, muzzle flash handling on v_ model:
129 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
131 * shot = muzzle end (shot origin)
132 * shell = casings ejection point (must be on the right hand side of the gun)
133 * v_tuba.md3 - third person model
135 * shot = muzzle end (for muzzle flashes)
136 * g_tuba.md3 - pickup model
140 // self.origin, self.angles
142 // self.movedir, self.view_ofs
146 // call again with ""
148 void CL_WeaponEntity_SetModel(string name)
153 // if there is a child entity, hide it until we're sure we use it
154 if (self.weaponentity)
155 self.weaponentity.model = "";
156 _setmodel(self, W_Model(strcat("v_", name, ".md3")));
157 v_shot_idx = gettagindex(self, "shot"); // used later
159 v_shot_idx = gettagindex(self, "tag_shot");
161 _setmodel(self, W_Model(strcat("h_", name, ".iqm")));
162 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
163 self.anim_fire1 = animfixfps(self, '0 1 0.01', '0 0 0');
164 self.anim_fire2 = animfixfps(self, '1 1 0.01', '0 0 0');
165 self.anim_idle = animfixfps(self, '2 1 0.01', '0 0 0');
166 self.anim_reload = animfixfps(self, '3 1 0.01', '0 0 0');
168 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
169 // if we don't, this is a "real" animated model
170 if(gettagindex(self, "weapon"))
172 if (!self.weaponentity)
173 self.weaponentity = spawn();
174 _setmodel(self.weaponentity, W_Model(strcat("v_", name, ".md3")));
175 setattachment(self.weaponentity, self, "weapon");
177 else if(gettagindex(self, "tag_weapon"))
179 if (!self.weaponentity)
180 self.weaponentity = spawn();
181 _setmodel(self.weaponentity, W_Model(strcat("v_", name, ".md3")));
182 setattachment(self.weaponentity, self, "tag_weapon");
186 if(self.weaponentity)
187 remove(self.weaponentity);
188 self.weaponentity = world;
191 setorigin(self,'0 0 0');
192 self.angles = '0 0 0';
194 self.viewmodelforclient = world;
198 if(v_shot_idx) // v_ model attached to invisible h_ model
200 self.movedir = gettaginfo(self.weaponentity, v_shot_idx);
204 idx = gettagindex(self, "shot");
206 idx = gettagindex(self, "tag_shot");
208 self.movedir = gettaginfo(self, idx);
211 LOG_INFO("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
212 self.movedir = '0 0 0';
216 if(self.weaponentity) // v_ model attached to invisible h_ model
218 idx = gettagindex(self.weaponentity, "shell");
220 idx = gettagindex(self.weaponentity, "tag_shell");
222 self.spawnorigin = gettaginfo(self.weaponentity, idx);
228 idx = gettagindex(self, "shell");
230 idx = gettagindex(self, "tag_shell");
232 self.spawnorigin = gettaginfo(self, idx);
235 LOG_INFO("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
236 self.spawnorigin = self.movedir;
242 self.oldorigin = '0 0 0'; // use regular attachment
246 if(self.weaponentity)
248 idx = gettagindex(self, "weapon");
250 idx = gettagindex(self, "tag_weapon");
254 idx = gettagindex(self, "handle");
256 idx = gettagindex(self, "tag_handle");
260 self.oldorigin = self.movedir - gettaginfo(self, idx);
264 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");
265 self.oldorigin = '0 0 0'; // there is no way to recover from this
269 self.viewmodelforclient = self.owner;
274 if(self.weaponentity)
275 remove(self.weaponentity);
276 self.weaponentity = world;
277 self.movedir = '0 0 0';
278 self.spawnorigin = '0 0 0';
279 self.oldorigin = '0 0 0';
280 self.anim_fire1 = '0 1 0.01';
281 self.anim_fire2 = '0 1 0.01';
282 self.anim_idle = '0 1 0.01';
283 self.anim_reload = '0 1 0.01';
286 self.view_ofs = '0 0 0';
288 if(self.movedir.x >= 0)
292 self.movedir = shotorg_adjust(v0, false, false, self.owner.cvar_cl_gunalign);
293 self.view_ofs = shotorg_adjust(v0, false, true, self.owner.cvar_cl_gunalign) - v0;
295 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
296 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
298 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
300 // check if an instant weapon switch occurred
301 setorigin(self, self.view_ofs);
302 // reset animstate now
303 self.wframe = WFRAME_IDLE;
304 setanim(self, self.anim_idle, true, false, true);
307 vector CL_Weapon_GetShotOrg(float wpn)
309 entity wi = get_weaponinfo(wpn);
311 CL_WeaponEntity_SetModel(wi.mdl);
312 vector ret = self.movedir;
313 CL_WeaponEntity_SetModel("");
319 void CL_Weaponentity_Think()
322 self.nextthink = time;
323 if (intermission_running)
324 self.frame = self.anim_idle.x;
325 if (self.owner.weaponentity != self)
327 if (self.weaponentity)
328 remove(self.weaponentity);
332 if (self.owner.deadflag != DEAD_NO)
335 if (self.weaponentity)
336 self.weaponentity.model = "";
339 if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
341 self.weaponname = self.owner.weaponname;
342 self.dmg = self.owner.modelindex;
343 self.deadflag = self.owner.deadflag;
345 CL_WeaponEntity_SetModel(self.owner.weaponname);
348 tb = (self.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
349 self.effects = self.owner.effects & EFMASK_CHEAP;
350 self.effects &= ~EF_LOWPRECISION;
351 self.effects &= ~EF_FULLBRIGHT; // can mask team color, so get rid of it
352 self.effects &= ~EF_TELEPORT_BIT;
353 self.effects &= ~EF_RESTARTANIM_BIT;
356 if(self.owner.alpha == default_player_alpha)
357 self.alpha = default_weapon_alpha;
358 else if(self.owner.alpha != 0)
359 self.alpha = self.owner.alpha;
363 self.glowmod = self.owner.weaponentity_glowmod;
364 self.colormap = self.owner.colormap;
365 if (self.weaponentity)
367 self.weaponentity.effects = self.effects;
368 self.weaponentity.alpha = self.alpha;
369 self.weaponentity.colormap = self.colormap;
370 self.weaponentity.glowmod = self.glowmod;
373 self.angles = '0 0 0';
375 float f = (self.owner.weapon_nextthink - time);
376 if (self.state == WS_RAISE && !intermission_running)
378 entity newwep = get_weaponinfo(self.owner.switchweapon);
379 f = f * g_weaponratefactor / max(f, newwep.switchdelay_raise);
380 self.angles_x = -90 * f * f;
382 else if (self.state == WS_DROP && !intermission_running)
384 entity oldwep = get_weaponinfo(self.owner.weapon);
385 f = 1 - f * g_weaponratefactor / max(f, oldwep.switchdelay_drop);
386 self.angles_x = -90 * f * f;
388 else if (self.state == WS_CLEAR)
391 self.angles_x = -90 * f * f;
395 void CL_ExteriorWeaponentity_Think()
398 self.nextthink = time;
399 if (self.owner.exteriorweaponentity != self)
404 if (self.owner.deadflag != DEAD_NO)
409 if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
411 self.weaponname = self.owner.weaponname;
412 self.dmg = self.owner.modelindex;
413 self.deadflag = self.owner.deadflag;
414 if (self.owner.weaponname != "")
415 _setmodel(self, W_Model(strcat("v_", self.owner.weaponname, ".md3")));
419 if((tag_found = gettagindex(self.owner, "tag_weapon")))
421 self.tag_index = tag_found;
422 self.tag_entity = self.owner;
425 setattachment(self, self.owner, "bip01 r hand");
427 self.effects = self.owner.effects;
428 self.effects |= EF_LOWPRECISION;
429 self.effects = self.effects & EFMASK_CHEAP; // eat performance
430 if(self.owner.alpha == default_player_alpha)
431 self.alpha = default_weapon_alpha;
432 else if(self.owner.alpha != 0)
433 self.alpha = self.owner.alpha;
437 self.glowmod = self.owner.weaponentity_glowmod;
438 self.colormap = self.owner.colormap;
440 CSQCMODEL_AUTOUPDATE(self);
443 // spawning weaponentity for client
444 void CL_SpawnWeaponentity(entity e)
446 entity view = e.weaponentity = spawn();
447 view.classname = "weaponentity";
448 view.solid = SOLID_NOT;
450 setmodel(view, MDL_Null); // precision set when changed
451 setorigin(view, '0 0 0');
452 view.angles = '0 0 0';
453 view.viewmodelforclient = e;
455 view.think = CL_Weaponentity_Think;
456 view.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
457 view.nextthink = time;
459 entity exterior = e.exteriorweaponentity = spawn();
460 exterior.classname = "exteriorweaponentity";
461 exterior.solid = SOLID_NOT;
462 exterior.exteriorweaponentity = exterior;
464 setorigin(exterior, '0 0 0');
465 exterior.angles = '0 0 0';
466 exterior.think = CL_ExteriorWeaponentity_Think;
467 exterior.nextthink = time;
469 CSQCMODEL_AUTOINIT(exterior);
473 void w_clear(Weapon thiswep, entity actor, bool fire1, bool fire2)
475 if (actor.weapon != -1)
478 actor.switchingweapon = 0;
480 if (actor.weaponentity)
482 actor.weaponentity.state = WS_CLEAR;
483 actor.weaponentity.effects = 0;
487 void w_ready(Weapon thiswep, entity actor, bool fire1, bool fire2)
489 if (actor.weaponentity)
490 actor.weaponentity.state = WS_READY;
491 weapon_thinkf(actor, WFRAME_IDLE, 1000000, w_ready);
496 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, float secondary)
498 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
501 WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
503 WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
505 if (ammo) return true;
506 // always keep the Mine Layer if we placed mines, so that we can detonate them
508 if(actor.weapon == WEP_MINE_LAYER.m_id)
509 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == actor)
512 if(actor.weapon == WEP_SHOTGUN.m_id)
513 if(!secondary && WEP_CVAR(shotgun, secondary) == 1)
514 return false; // no clicking, just allow
516 if(actor.weapon == actor.switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
518 sound (actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
519 actor.prevdryfire = time;
522 // check if the other firing mode has enough ammo
523 bool ammo_other = false;
525 WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
527 WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
531 if (time - actor.prevwarntime > 1)
537 ITEM_WEAPON_PRIMORSEC,
543 actor.prevwarntime = time;
545 else // this weapon is totally unable to fire, switch to another one
547 W_SwitchToOtherWeapon(actor);
553 bool weapon_prepareattack_check(Weapon thiswep, entity actor, bool secondary, float attacktime)
555 if(!weapon_prepareattack_checkammo(thiswep, actor, secondary))
558 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
559 //if all players readied up and the countdown is running
560 if(time < game_starttime || time < actor.race_penalty) {
564 if (timeout_status == TIMEOUT_ACTIVE) //don't allow the player to shoot while game is paused
567 // do not even think about shooting if switching
568 if(actor.switchweapon != actor.weapon)
573 // don't fire if previous attack is not finished
574 if (ATTACK_FINISHED(actor) > time + actor.weapon_frametime * 0.5)
576 // don't fire while changing weapon
577 if (actor.weaponentity.state != WS_READY)
582 void weapon_prepareattack_do(entity actor, bool secondary, float attacktime)
584 actor.weaponentity.state = WS_INUSE;
586 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
588 // if the weapon hasn't been firing continuously, reset the timer
591 if (ATTACK_FINISHED(actor) < time - actor.weapon_frametime * 1.5)
593 ATTACK_FINISHED(actor) = time;
594 //dprint("resetting attack finished to ", ftos(time), "\n");
596 ATTACK_FINISHED(actor) = ATTACK_FINISHED(actor) + attacktime * W_WeaponRateFactor();
598 actor.bulletcounter += 1;
599 //dprint("attack finished ", ftos(ATTACK_FINISHED(actor)), "\n");
601 bool weapon_prepareattack(Weapon thiswep, entity actor, bool secondary, float attacktime)
603 if (weapon_prepareattack_check(thiswep, actor, secondary, attacktime)) {
604 weapon_prepareattack_do(actor, secondary, attacktime);
610 void weapon_thinkf(entity actor, float fr, float t, void(Weapon thiswep, entity actor, bool fire1, bool fire2) func)
616 if(fr == WFRAME_DONTCHANGE)
618 fr = actor.weaponentity.wframe;
621 else if (fr == WFRAME_IDLE)
630 if (actor.weaponentity)
632 actor.weaponentity.wframe = fr;
634 if (fr == WFRAME_IDLE)
635 a = actor.weaponentity.anim_idle;
636 else if (fr == WFRAME_FIRE1)
637 a = actor.weaponentity.anim_fire1;
638 else if (fr == WFRAME_FIRE2)
639 a = actor.weaponentity.anim_fire2;
640 else // if (fr == WFRAME_RELOAD)
641 a = actor.weaponentity.anim_reload;
642 a.z *= g_weaponratefactor;
643 setanim(actor.weaponentity, a, restartanim == false, restartanim, restartanim);
650 if(actor.weapon_think == w_ready && func != w_ready && actor.weaponentity.state == WS_RAISE)
652 backtrace("Tried to override initial weapon think function - should this really happen?");
655 t *= W_WeaponRateFactor();
657 // VorteX: haste can be added here
658 if (actor.weapon_think == w_ready)
660 actor.weapon_nextthink = time;
661 //dprint("started firing at ", ftos(time), "\n");
663 if (actor.weapon_nextthink < time - actor.weapon_frametime * 1.5 || actor.weapon_nextthink > time + actor.weapon_frametime * 1.5)
665 actor.weapon_nextthink = time;
666 //dprint("reset weapon animation timer at ", ftos(time), "\n");
668 actor.weapon_nextthink = actor.weapon_nextthink + t;
669 actor.weapon_think = func;
670 //dprint("next ", ftos(actor.weapon_nextthink), "\n");
672 if((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
674 if((actor.weapon == WEP_SHOCKWAVE.m_id || actor.weapon == WEP_SHOTGUN.m_id) && fr == WFRAME_FIRE2)
675 animdecide_setaction(actor, ANIMACTION_MELEE, restartanim);
677 animdecide_setaction(actor, ANIMACTION_SHOOT, restartanim);
681 if(actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
682 actor.anim_upper_action = 0;
686 float forbidWeaponUse(entity player)
688 if(time < game_starttime && !autocvar_sv_ready_restart_after_countdown)
690 if(round_handler_IsActive() && !round_handler_IsRoundStarted())
692 if(player.player_blocked)
696 if(player.weapon_blocked)
701 .bool hook_switchweapon;
703 void W_WeaponFrame(entity actor)
708 actor.weapon_frametime = frametime;
710 if (!actor.weaponentity || actor.health < 1)
711 return; // Dead player can't use weapons and injure impulse commands
713 if(forbidWeaponUse(actor))
714 if(actor.weaponentity.state != WS_CLEAR)
716 Weapon wpn = get_weaponinfo(actor.weapon);
717 w_ready(wpn, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
721 if(!actor.switchweapon)
724 actor.switchingweapon = 0;
725 actor.weaponentity.state = WS_CLEAR;
726 actor.weaponname = "";
727 //actor.items &= ~IT_AMMO;
731 makevectors(actor.v_angle);
732 fo = v_forward; // save them in case the weapon think functions change it
737 if (actor.weapon != actor.switchweapon)
739 if (actor.weaponentity.state == WS_CLEAR)
742 actor.switchingweapon = actor.switchweapon;
743 entity newwep = get_weaponinfo(actor.switchweapon);
745 // the two weapon entities will notice this has changed and update their models
746 actor.weapon = actor.switchweapon;
747 actor.weaponname = newwep.mdl;
748 actor.bulletcounter = 0;
749 actor.ammo_field = newwep.ammo_field;
750 Weapon w = get_weaponinfo(actor.switchweapon);
752 actor.weaponentity.state = WS_RAISE;
754 // set our clip load to the load of the weapon we switched to, if it's reloadable
755 if(newwep.spawnflags & WEP_FLAG_RELOADABLE && newwep.reloading_ammo) // prevent accessing undefined cvars
757 actor.clip_load = actor.(weapon_load[actor.switchweapon]);
758 actor.clip_size = newwep.reloading_ammo;
761 actor.clip_load = actor.clip_size = 0;
763 weapon_thinkf(actor, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
765 else if (actor.weaponentity.state == WS_DROP)
767 // in dropping phase we can switch at any time
768 actor.switchingweapon = actor.switchweapon;
770 else if (actor.weaponentity.state == WS_READY)
773 actor.switchingweapon = actor.switchweapon;
774 entity oldwep = get_weaponinfo(actor.weapon);
776 // set up weapon switch think in the future, and start drop anim
777 #ifndef INDEPENDENT_ATTACK_FINISHED
778 if(ATTACK_FINISHED(actor) <= time + actor.weapon_frametime * 0.5)
781 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
782 actor.weaponentity.state = WS_DROP;
783 weapon_thinkf(actor, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
784 #ifndef INDEPENDENT_ATTACK_FINISHED
790 // LordHavoc: network timing test code
792 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor)), " >= ", ftos(actor.weapon_nextthink), "\n");
797 // call the think code which may fire the weapon
798 // and do so multiple times to resolve framerate dependency issues if the
799 // server framerate is very low and the weapon fire rate very high
802 while (c < W_TICSPERFRAME)
805 if(w && !(actor.weapons & WepSet_FromWeapon(w)))
807 if(actor.weapon == actor.switchweapon)
808 W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
816 bool block_weapon = false;
818 bool key_pressed = actor.BUTTON_HOOK && !actor.vehicle;
819 Weapon off = actor.offhand;
820 if (off && !(actor.weapons & WEPSET(HOOK))) {
821 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
823 if (key_pressed && actor.switchweapon != WEP_HOOK.m_id && !actor.hook_switchweapon) {
824 W_SwitchWeapon(WEP_HOOK.m_id);
826 actor.hook_switchweapon = key_pressed;
828 block_weapon = (actor.weapon == h.m_id && (actor.BUTTON_ATCK || key_pressed));
829 h.wr_think(h, actor, block_weapon, false);
835 Weapon e = get_weaponinfo(actor.weapon);
836 e.wr_think(e, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
838 Weapon w = get_weaponinfo(actor.weapon);
842 if (time + actor.weapon_frametime * 0.5 >= actor.weapon_nextthink)
844 if(actor.weapon_think)
849 Weapon wpn = get_weaponinfo(actor.weapon);
850 actor.weapon_think(wpn, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
853 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
858 void W_AttachToShotorg(entity actor, entity flash, vector offset)
862 flash.angles_z = random() * 360;
864 if(gettagindex(actor.weaponentity, "shot"))
865 setattachment(flash, actor.weaponentity, "shot");
867 setattachment(flash, actor.weaponentity, "tag_shot");
868 setorigin(flash, offset);
871 copyentity(flash, xflash);
873 flash.viewmodelforclient = actor;
875 if(actor.weaponentity.oldorigin.x > 0)
877 setattachment(xflash, actor.exteriorweaponentity, "");
878 setorigin(xflash, actor.weaponentity.oldorigin + offset);
882 if(gettagindex(actor.exteriorweaponentity, "shot"))
883 setattachment(xflash, actor.exteriorweaponentity, "shot");
885 setattachment(xflash, actor.exteriorweaponentity, "tag_shot");
886 setorigin(xflash, offset);
890 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
893 if(cvar("g_overkill"))
894 if(actor.ok_use_ammocharge)
896 ok_DecreaseCharge(actor, actor.weapon);
900 if((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo)
903 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
904 if(wep.reloading_ammo)
906 actor.clip_load -= ammo_use;
907 actor.(weapon_load[actor.weapon]) = actor.clip_load;
909 else if(wep.ammo_field != ammo_none)
911 actor.(wep.ammo_field) -= ammo_use;
912 if(actor.(wep.ammo_field) < 0)
915 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
916 "Please notify Samual immediately with a copy of this backtrace!\n",
919 GetAmmoPicture(wep.ammo_field),
921 actor.(wep.ammo_field)
927 // weapon reloading code
929 .float reload_ammo_amount, reload_ammo_min, reload_time;
930 .float reload_complain;
931 .string reload_sound;
933 void W_ReloadedAndReady(Weapon thiswep, entity actor, bool fire1, bool fire2)
935 // finish the reloading process, and do the ammo transfer
937 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
939 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
940 if(!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
941 actor.clip_load = actor.reload_ammo_amount;
944 // make sure we don't add more ammo than we have
945 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
946 actor.clip_load += load;
947 actor.(actor.ammo_field) -= load;
949 actor.(weapon_load[actor.weapon]) = actor.clip_load;
951 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
952 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
953 // so your weapon is disabled for a few seconds without reason
955 //ATTACK_FINISHED(actor) -= actor.reload_time - 1;
957 Weapon wpn = get_weaponinfo(actor.weapon);
958 w_ready(wpn, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
961 void W_Reload(entity actor, float sent_ammo_min, string sent_sound)
963 // set global values to work with
965 e = get_weaponinfo(actor.weapon);
967 if(cvar("g_overkill"))
968 if(actor.ok_use_ammocharge)
971 actor.reload_ammo_min = sent_ammo_min;
972 actor.reload_ammo_amount = e.reloading_ammo;
973 actor.reload_time = e.reloading_time;
974 actor.reload_sound = sent_sound;
976 // don't reload weapons that don't have the RELOADABLE flag
977 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
979 LOG_TRACE("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
983 // return if reloading is disabled for this weapon
984 if(!actor.reload_ammo_amount)
987 // our weapon is fully loaded, no need to reload
988 if (actor.clip_load >= actor.reload_ammo_amount)
991 // no ammo, so nothing to load
992 if(actor.ammo_field != ammo_none)
993 if(!actor.(actor.ammo_field) && actor.reload_ammo_min)
994 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
996 if(IS_REAL_CLIENT(actor) && actor.reload_complain < time)
998 play2(actor, SND(UNAVAILABLE));
999 sprint(actor, strcat("You don't have enough ammo to reload the ^2", WEP_NAME(actor.weapon), "\n"));
1000 actor.reload_complain = time + 1;
1002 // switch away if the amount of ammo is not enough to keep using this weapon
1003 Weapon w = get_weaponinfo(actor.weapon);
1004 if (!(w.wr_checkammo1(w) + w.wr_checkammo2(w)))
1006 actor.clip_load = -1; // reload later
1007 W_SwitchToOtherWeapon(actor);
1012 if (actor.weaponentity)
1014 if (actor.weaponentity.wframe == WFRAME_RELOAD)
1017 // allow switching away while reloading, but this will cause a new reload!
1018 actor.weaponentity.state = WS_READY;
1021 // now begin the reloading process
1023 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
1025 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
1026 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
1027 // so your weapon is disabled for a few seconds without reason
1029 //ATTACK_FINISHED(actor) = max(time, ATTACK_FINISHED(actor)) + actor.reload_time + 1;
1031 weapon_thinkf(actor, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
1033 if(actor.clip_load < 0)
1034 actor.clip_load = 0;
1035 actor.old_clip_load = actor.clip_load;
1036 actor.clip_load = actor.(weapon_load[actor.weapon]) = -1;
1039 void W_DropEvent(.void(Weapon) event, entity player, float weapon_type, entity weapon_item)
1042 weapon_dropevent_item = weapon_item;
1043 Weapon w = get_weaponinfo(weapon_type);