1 #include "weaponsystem.qh"
3 #include "selection.qh"
5 #include "../command/common.qh"
6 #include "../mutators/all.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;
24 case 1: case 2: vecs.y = 0;
32 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
38 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
40 else if (autocvar_g_shootfromeye)
44 else if (autocvar_g_shootfromcenter)
49 else if ((s = autocvar_g_shootfromfixedorigin) != "")
52 if (y_is_right) v.y = -v.y;
53 if (v.x != 0) vecs.x = v.x;
57 else // just do the same as top
59 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
65 vector shotorg_adjust(vector vecs, bool y_is_right, bool visual, int algn)
67 return shotorg_adjust_values(vecs, y_is_right, visual, algn);
72 .float weapon_frametime;
74 float W_WeaponRateFactor()
76 float t = 1.0 / g_weaponratefactor;
78 MUTATOR_CALLHOOK(WeaponRateFactor, t);
84 float W_WeaponSpeedFactor()
86 float t = 1.0 * g_weaponspeedfactor;
88 MUTATOR_CALLHOOK(WeaponSpeedFactor, t);
95 void weapon_thinkf(entity actor, .entity weaponentity, float fr, float t, void(Weapon thiswep, entity actor,
96 .entity weaponentity, int fire) func);
98 bool CL_Weaponentity_CustomizeEntityForClient()
101 this.viewmodelforclient = this.owner;
102 if (IS_SPEC(other) && other.enemy == this.owner) this.viewmodelforclient = other;
109 * 1. simple animated model, muzzle flash handling on h_ model:
110 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
112 * shot = muzzle end (shot origin, also used for muzzle flashes)
113 * shell = casings ejection point (must be on the right hand side of the gun)
114 * weapon = attachment for v_tuba.md3
115 * v_tuba.md3 - first and third person model
116 * g_tuba.md3 - pickup model
118 * 2. simple animated model, muzzle flash handling on v_ model:
119 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
121 * weapon = attachment for v_tuba.md3
122 * v_tuba.md3 - first and third person model
124 * shot = muzzle end (shot origin, also used for muzzle flashes)
125 * shell = casings ejection point (must be on the right hand side of the gun)
126 * g_tuba.md3 - pickup model
128 * 3. fully animated model, muzzle flash handling on h_ model:
129 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
131 * shot = muzzle end (shot origin, also used for muzzle flashes)
132 * shell = casings ejection point (must be on the right hand side of the gun)
133 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
134 * v_tuba.md3 - third person model
135 * g_tuba.md3 - pickup model
137 * 4. fully animated model, muzzle flash handling on v_ model:
138 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
140 * shot = muzzle end (shot origin)
141 * shell = casings ejection point (must be on the right hand side of the gun)
142 * v_tuba.md3 - third person model
144 * shot = muzzle end (for muzzle flashes)
145 * g_tuba.md3 - pickup model
149 // this.origin, this.angles
151 // this.movedir, this.view_ofs
155 // call again with ""
157 void CL_WeaponEntity_SetModel(entity this, .entity weaponentity, string name)
161 // if there is a child entity, hide it until we're sure we use it
162 if (this.(weaponentity)) this.(weaponentity).model = "";
163 _setmodel(this, W_Model(strcat("v_", name, ".md3")));
164 int v_shot_idx = gettagindex(this, "shot"); // used later
165 if (!v_shot_idx) v_shot_idx = gettagindex(this, "tag_shot");
167 _setmodel(this, W_Model(strcat("h_", name, ".iqm")));
168 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
169 this.anim_fire1 = animfixfps(this, '0 1 0.01', '0 0 0');
170 this.anim_fire2 = animfixfps(this, '1 1 0.01', '0 0 0');
171 this.anim_idle = animfixfps(this, '2 1 0.01', '0 0 0');
172 this.anim_reload = animfixfps(this, '3 1 0.01', '0 0 0');
174 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
175 // if we don't, this is a "real" animated model
176 if (gettagindex(this, "weapon"))
178 if (!this.(weaponentity)) this.(weaponentity) = new(weaponentity);
179 _setmodel(this.(weaponentity), W_Model(strcat("v_", name, ".md3")));
180 setattachment(this.(weaponentity), this, "weapon");
182 else if (gettagindex(this, "tag_weapon"))
184 if (!this.(weaponentity)) this.(weaponentity) = new(weaponentity);
185 _setmodel(this.(weaponentity), W_Model(strcat("v_", name, ".md3")));
186 setattachment(this.(weaponentity), this, "tag_weapon");
190 if (this.(weaponentity)) remove(this.(weaponentity));
191 this.(weaponentity) = NULL;
194 setorigin(this, '0 0 0');
195 this.angles = '0 0 0';
197 this.viewmodelforclient = NULL;
201 if (v_shot_idx) // v_ model attached to invisible h_ model
203 this.movedir = gettaginfo(this.(weaponentity), v_shot_idx);
207 idx = gettagindex(this, "shot");
208 if (!idx) idx = gettagindex(this, "tag_shot");
211 this.movedir = gettaginfo(this, idx);
215 LOG_INFO("WARNING: weapon model ", this.model,
216 " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
217 this.movedir = '0 0 0';
221 if (this.(weaponentity)) // v_ model attached to invisible h_ model
223 idx = gettagindex(this.(weaponentity), "shell");
224 if (!idx) idx = gettagindex(this.(weaponentity), "tag_shell");
225 if (idx) this.spawnorigin = gettaginfo(this.(weaponentity), idx);
233 idx = gettagindex(this, "shell");
234 if (!idx) idx = gettagindex(this, "tag_shell");
237 this.spawnorigin = gettaginfo(this, idx);
241 LOG_INFO("WARNING: weapon model ", this.model,
242 " does not support the 'shell' tag, will display casings wrong\n");
243 this.spawnorigin = this.movedir;
249 this.oldorigin = '0 0 0'; // use regular attachment
253 if (this.(weaponentity))
255 idx = gettagindex(this, "weapon");
256 if (!idx) idx = gettagindex(this, "tag_weapon");
260 idx = gettagindex(this, "handle");
261 if (!idx) idx = gettagindex(this, "tag_handle");
265 this.oldorigin = this.movedir - gettaginfo(this, idx);
269 LOG_INFO("WARNING: weapon model ", this.model,
270 " does not support the 'handle' tag and neither does the v_ model support the 'shot' tag, will display muzzle flashes TOTALLY wrong\n");
271 this.oldorigin = '0 0 0'; // there is no way to recover from this
275 this.viewmodelforclient = this.owner;
280 if (this.(weaponentity)) remove(this.(weaponentity));
281 this.(weaponentity) = NULL;
282 this.movedir = '0 0 0';
283 this.spawnorigin = '0 0 0';
284 this.oldorigin = '0 0 0';
285 this.anim_fire1 = '0 1 0.01';
286 this.anim_fire2 = '0 1 0.01';
287 this.anim_idle = '0 1 0.01';
288 this.anim_reload = '0 1 0.01';
291 this.view_ofs = '0 0 0';
293 if (this.movedir.x >= 0)
295 vector v0 = this.movedir;
296 this.movedir = shotorg_adjust(v0, false, false, this.owner.cvar_cl_gunalign);
297 this.view_ofs = shotorg_adjust(v0, false, true, this.owner.cvar_cl_gunalign) - v0;
299 this.owner.stat_shotorg = compressShotOrigin(this.movedir);
300 this.movedir = decompressShotOrigin(this.owner.stat_shotorg); // make them match perfectly
302 this.spawnorigin += this.view_ofs; // offset the casings origin by the same amount
304 // check if an instant weapon switch occurred
305 setorigin(this, this.view_ofs);
306 // reset animstate now
307 this.wframe = WFRAME_IDLE;
308 setanim(this, this.anim_idle, true, false, true);
311 vector CL_Weapon_GetShotOrg(float wpn)
313 entity wi = Weapons_from(wpn);
315 .entity weaponentity = weaponentities[0];
316 CL_WeaponEntity_SetModel(e, weaponentity, wi.mdl);
317 vector ret = e.movedir;
318 CL_WeaponEntity_SetModel(e, weaponentity, "");
323 void CL_Weaponentity_Think()
326 this.nextthink = time;
327 if (intermission_running) this.frame = this.anim_idle.x;
328 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
329 if (this.owner.(weaponentity) != this)
331 if (this.(weaponentity)) remove(this.(weaponentity));
335 if (this.owner.deadflag != DEAD_NO)
338 if (this.(weaponentity)) this.(weaponentity).model = "";
341 if (this.weaponname != this.owner.weaponname || this.dmg != this.owner.modelindex
342 || this.deadflag != this.owner.deadflag)
344 this.weaponname = this.owner.weaponname;
345 this.dmg = this.owner.modelindex;
346 this.deadflag = this.owner.deadflag;
348 CL_WeaponEntity_SetModel(this, weaponentity, this.owner.weaponname);
351 int tb = (this.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
352 this.effects = this.owner.effects & EFMASK_CHEAP;
353 this.effects &= ~EF_LOWPRECISION;
354 this.effects &= ~EF_FULLBRIGHT; // can mask team color, so get rid of it
355 this.effects &= ~EF_TELEPORT_BIT;
356 this.effects &= ~EF_RESTARTANIM_BIT;
359 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
360 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
363 this.glowmod = this.owner.weaponentity_glowmod;
364 this.colormap = this.owner.colormap;
365 if (this.(weaponentity))
367 this.(weaponentity).effects = this.effects;
368 this.(weaponentity).alpha = this.alpha;
369 this.(weaponentity).colormap = this.colormap;
370 this.(weaponentity).glowmod = this.glowmod;
373 this.angles = '0 0 0';
375 float f = (this.owner.weapon_nextthink - time);
376 if (this.state == WS_RAISE && !intermission_running)
378 entity newwep = Weapons_from(this.owner.switchweapon);
379 f = f * g_weaponratefactor / max(f, newwep.switchdelay_raise);
380 this.angles_x = -90 * f * f;
382 else if (this.state == WS_DROP && !intermission_running)
384 entity oldwep = Weapons_from(this.owner.weapon);
385 f = 1 - f * g_weaponratefactor / max(f, oldwep.switchdelay_drop);
386 this.angles_x = -90 * f * f;
388 else if (this.state == WS_CLEAR)
391 this.angles_x = -90 * f * f;
395 void CL_ExteriorWeaponentity_Think()
398 this.nextthink = time;
399 if (this.owner.exteriorweaponentity != this)
404 if (this.owner.deadflag != DEAD_NO)
409 if (this.weaponname != this.owner.weaponname || this.dmg != this.owner.modelindex
410 || this.deadflag != this.owner.deadflag)
412 this.weaponname = this.owner.weaponname;
413 this.dmg = this.owner.modelindex;
414 this.deadflag = this.owner.deadflag;
415 if (this.owner.weaponname != "") _setmodel(this, W_Model(strcat("v_", this.owner.weaponname, ".md3")));
416 else this.model = "";
419 if ((tag_found = gettagindex(this.owner, "tag_weapon")))
421 this.tag_index = tag_found;
422 this.tag_entity = this.owner;
426 setattachment(this, this.owner, "bip01 r hand");
429 this.effects = this.owner.effects;
430 this.effects |= EF_LOWPRECISION;
431 this.effects = this.effects & EFMASK_CHEAP; // eat performance
432 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
433 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
436 this.glowmod = this.owner.weaponentity_glowmod;
437 this.colormap = this.owner.colormap;
439 CSQCMODEL_AUTOUPDATE(this);
442 // spawning weaponentity for client
443 void CL_SpawnWeaponentity(entity e, .entity weaponentity)
445 entity view = e.(weaponentity) = new(weaponentity);
447 view.solid = SOLID_NOT;
449 setmodel(view, MDL_Null); // precision set when changed
450 setorigin(view, '0 0 0');
451 view.angles = '0 0 0';
452 view.viewmodelforclient = e;
454 view.think = CL_Weaponentity_Think;
455 view.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
456 view.nextthink = time;
458 if (weaponentity == weaponentities[0])
460 entity exterior = e.exteriorweaponentity = new(exteriorweaponentity);
462 exterior.solid = SOLID_NOT;
463 exterior.exteriorweaponentity = exterior;
465 setorigin(exterior, '0 0 0');
466 exterior.angles = '0 0 0';
467 exterior.think = CL_ExteriorWeaponentity_Think;
468 exterior.nextthink = time;
470 CSQCMODEL_AUTOINIT(exterior);
475 void w_clear(Weapon thiswep, entity actor, .entity weaponentity, int fire)
477 if (actor.weapon != -1)
480 actor.switchingweapon = 0;
482 if (actor.(weaponentity))
484 actor.(weaponentity).state = WS_CLEAR;
485 actor.(weaponentity).effects = 0;
489 void w_ready(Weapon thiswep, entity actor, .entity weaponentity, int fire)
491 if (actor.(weaponentity)) actor.(weaponentity).state = WS_READY;
492 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, 1000000, w_ready);
497 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary)
499 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
501 if (secondary) WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
502 else WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
503 if (ammo) return true;
504 // always keep the Mine Layer if we placed mines, so that we can detonate them
505 if (thiswep == WEP_MINE_LAYER)
506 for (entity mine; (mine = find(mine, classname, "mine")); )
507 if (mine.owner == actor) return false;
509 if (thiswep == WEP_SHOTGUN)
510 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false; // no clicking, just allow
512 if (thiswep == Weapons_from(actor.switchweapon) && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
514 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
515 actor.prevdryfire = time;
518 // check if the other firing mode has enough ammo
519 bool ammo_other = false;
520 if (secondary) WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo1(thiswep));
521 else WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo2(thiswep));
524 if (time - actor.prevwarntime > 1)
530 ITEM_WEAPON_PRIMORSEC,
536 actor.prevwarntime = time;
538 else // this weapon is totally unable to fire, switch to another one
540 W_SwitchToOtherWeapon(actor);
547 bool weapon_prepareattack_check(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
549 if (!weapon_prepareattack_checkammo(thiswep, actor, secondary)) return false;
551 // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
552 // if all players readied up and the countdown is running
553 if (time < game_starttime || time < actor.race_penalty) return false;
555 if (timeout_status == TIMEOUT_ACTIVE) // don't allow the player to shoot while game is paused
558 // do not even think about shooting if switching
559 if (actor.switchweapon != actor.weapon) return false;
563 int slot = weaponslot(weaponentity);
564 // don't fire if previous attack is not finished
565 if (ATTACK_FINISHED(actor, slot) > time + actor.weapon_frametime * 0.5) return false;
566 // don't fire while changing weapon
567 if (actor.(weaponentity).state != WS_READY) return false;
572 void weapon_prepareattack_do(entity actor, .entity weaponentity, bool secondary, float attacktime)
574 actor.(weaponentity).state = WS_INUSE;
576 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
578 // if the weapon hasn't been firing continuously, reset the timer
581 int slot = weaponslot(weaponentity);
582 if (ATTACK_FINISHED(actor, slot) < time - actor.weapon_frametime * 1.5)
584 ATTACK_FINISHED(actor, slot) = time;
585 // dprint("resetting attack finished to ", ftos(time), "\n");
587 ATTACK_FINISHED(actor, slot) = ATTACK_FINISHED(actor, slot) + attacktime * W_WeaponRateFactor();
589 actor.bulletcounter += 1;
590 // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, slot)), "\n");
593 bool weapon_prepareattack(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
595 if (weapon_prepareattack_check(thiswep, actor, weaponentity, secondary, attacktime))
597 weapon_prepareattack_do(actor, weaponentity, secondary, attacktime);
603 void weapon_thinkf(entity actor, .entity weaponentity, float fr, float t, void(Weapon thiswep, entity actor,
604 .entity weaponentity, int fire) func)
607 if (fr == WFRAME_DONTCHANGE)
609 fr = actor.(weaponentity).wframe;
612 else if (fr == WFRAME_IDLE)
621 vector of = v_forward;
625 if (actor.(weaponentity))
627 actor.(weaponentity).wframe = fr;
629 if (fr == WFRAME_IDLE) a = actor.(weaponentity).anim_idle;
630 else if (fr == WFRAME_FIRE1) a = actor.(weaponentity).anim_fire1;
631 else if (fr == WFRAME_FIRE2) a = actor.(weaponentity).anim_fire2;
632 else // if (fr == WFRAME_RELOAD)
633 a = actor.(weaponentity).anim_reload;
634 a.z *= g_weaponratefactor;
635 setanim(actor.(weaponentity), a, restartanim == false, restartanim, restartanim);
642 if (actor.weapon_think == w_ready && func != w_ready && actor.(weaponentity).state == WS_RAISE) backtrace(
643 "Tried to override initial weapon think function - should this really happen?");
645 t *= W_WeaponRateFactor();
647 // VorteX: haste can be added here
648 if (actor.weapon_think == w_ready)
650 actor.weapon_nextthink = time;
651 // dprint("started firing at ", ftos(time), "\n");
653 if (actor.weapon_nextthink < time - actor.weapon_frametime * 1.5
654 || actor.weapon_nextthink > time + actor.weapon_frametime * 1.5)
656 actor.weapon_nextthink = time;
657 // dprint("reset weapon animation timer at ", ftos(time), "\n");
659 actor.weapon_nextthink = actor.weapon_nextthink + t;
660 actor.weapon_think = func;
661 // dprint("next ", ftos(actor.weapon_nextthink), "\n");
663 if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
665 if ((actor.weapon == WEP_SHOCKWAVE.m_id || actor.weapon == WEP_SHOTGUN.m_id)
666 && fr == WFRAME_FIRE2) animdecide_setaction(actor, ANIMACTION_MELEE, restartanim);
667 else animdecide_setaction(actor, ANIMACTION_SHOOT, restartanim);
671 if (actor.anim_upper_action == ANIMACTION_SHOOT
672 || actor.anim_upper_action == ANIMACTION_MELEE) actor.anim_upper_action = 0;
676 bool forbidWeaponUse(entity player)
678 if (time < game_starttime && !autocvar_sv_ready_restart_after_countdown) return true;
679 if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
680 if (player.player_blocked) return true;
681 if (player.frozen) return true;
682 if (player.weapon_blocked) return true;
686 .bool hook_switchweapon;
688 void W_WeaponFrame(entity actor)
690 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
691 if (frametime) actor.weapon_frametime = frametime;
693 if (!actor.(weaponentity) || actor.health < 1) return; // Dead player can't use weapons and injure impulse commands
695 if (forbidWeaponUse(actor))
697 if (actor.(weaponentity).state != WS_CLEAR)
699 Weapon wpn = Weapons_from(actor.weapon);
700 w_ready(wpn, actor, weaponentity, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
705 if (actor.switchweapon == 0)
708 actor.switchingweapon = 0;
709 actor.(weaponentity).state = WS_CLEAR;
710 actor.weaponname = "";
711 // actor.items &= ~IT_AMMO;
715 makevectors(actor.v_angle);
716 vector fo = v_forward; // save them in case the weapon think functions change it
721 if (actor.weapon != actor.switchweapon)
723 switch (actor.(weaponentity).state)
726 LOG_WARNINGF("unhandled weaponentity (%i) state for player (%i): %d\n", actor.(weaponentity), actor, actor.(weaponentity).state);
734 actor.switchingweapon = actor.switchweapon;
735 entity newwep = Weapons_from(actor.switchweapon);
737 // the two weapon entities will notice this has changed and update their models
738 actor.weapon = actor.switchweapon;
739 actor.weaponname = newwep.mdl;
740 actor.bulletcounter = 0;
741 actor.ammo_field = newwep.ammo_field;
742 newwep.wr_setup(newwep);
743 actor.(weaponentity).state = WS_RAISE;
745 // set our clip load to the load of the weapon we switched to, if it's reloadable
746 if ((newwep.spawnflags & WEP_FLAG_RELOADABLE) && newwep.reloading_ammo) // prevent accessing undefined cvars
748 actor.clip_load = actor.(weapon_load[actor.switchweapon]);
749 actor.clip_size = newwep.reloading_ammo;
753 actor.clip_load = actor.clip_size = 0;
756 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
761 // in dropping phase we can switch at any time
762 actor.switchingweapon = actor.switchweapon;
768 actor.switchingweapon = actor.switchweapon;
769 entity oldwep = Weapons_from(actor.weapon);
771 // set up weapon switch think in the future, and start drop anim
773 #if INDEPENDENT_ATTACK_FINISHED
776 ATTACK_FINISHED(actor, slot) <= time + actor.weapon_frametime * 0.5
780 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
781 actor.(weaponentity).state = WS_DROP;
782 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
789 // LordHavoc: network timing test code
790 // if (actor.button0)
791 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor, slot)), " >= ", ftos(actor.weapon_nextthink), "\n");
793 int w = actor.weapon;
795 // call the think code which may fire the weapon
796 // and do so multiple times to resolve framerate dependency issues if the
797 // server framerate is very low and the weapon fire rate very high
799 while (c < W_TICSPERFRAME)
802 if (w && !(actor.weapons & WepSet_FromWeapon(w)))
804 if (actor.weapon == actor.switchweapon) W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
812 bool block_weapon = false;
814 bool key_pressed = actor.BUTTON_HOOK && !actor.vehicle;
815 Weapon off = actor.offhand;
816 if (off && !(actor.weapons & WEPSET(HOOK)))
818 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
822 if (key_pressed && actor.switchweapon != WEP_HOOK.m_id && !actor.hook_switchweapon) W_SwitchWeapon(
824 actor.hook_switchweapon = key_pressed;
826 block_weapon = (actor.weapon == h.m_id && (actor.BUTTON_ATCK || key_pressed));
827 h.wr_think(h, actor, weaponentity, block_weapon ? 1 : 0);
839 Weapon e = Weapons_from(actor.weapon);
840 e.wr_think(e, actor, weaponentity, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
844 Weapon w = Weapons_from(actor.weapon);
849 if (time + actor.weapon_frametime * 0.5 >= actor.weapon_nextthink)
851 if (actor.weapon_think)
856 Weapon wpn = Weapons_from(actor.weapon);
857 actor.weapon_think(wpn, actor, weaponentity,
858 (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
862 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
868 void W_AttachToShotorg(entity actor, entity flash, vector offset)
870 .entity weaponentity = weaponentities[0];
872 flash.angles_z = random() * 360;
874 if (gettagindex(actor.(weaponentity), "shot")) setattachment(flash, actor.(weaponentity), "shot");
875 else setattachment(flash, actor.(weaponentity), "tag_shot");
876 setorigin(flash, offset);
878 entity xflash = spawn();
879 copyentity(flash, xflash);
881 flash.viewmodelforclient = actor;
883 if (actor.(weaponentity).oldorigin.x > 0)
885 setattachment(xflash, actor.exteriorweaponentity, "");
886 setorigin(xflash, actor.(weaponentity).oldorigin + offset);
890 if (gettagindex(actor.exteriorweaponentity, "shot")) setattachment(xflash, actor.exteriorweaponentity, "shot");
891 else setattachment(xflash, actor.exteriorweaponentity, "tag_shot");
892 setorigin(xflash, offset);
896 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
898 if (cvar("g_overkill"))
900 if (actor.ok_use_ammocharge)
902 ok_DecreaseCharge(actor, actor.weapon);
907 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo) return;
909 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
910 if (wep.reloading_ammo)
912 actor.clip_load -= ammo_use;
913 actor.(weapon_load[actor.weapon]) = actor.clip_load;
915 else if (wep.ammo_field != ammo_none)
917 actor.(wep.ammo_field) -= ammo_use;
918 if (actor.(wep.ammo_field) < 0)
921 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
922 "Please notify Samual immediately with a copy of this backtrace!\n",
925 GetAmmoPicture(wep.ammo_field),
927 actor.(wep.ammo_field)
933 // weapon reloading code
935 .float reload_ammo_amount, reload_ammo_min, reload_time;
936 .float reload_complain;
937 .string reload_sound;
939 void W_ReloadedAndReady(Weapon thiswep, entity actor, .entity weaponentity, int fire)
941 // finish the reloading process, and do the ammo transfer
943 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
945 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
946 if (!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
948 actor.clip_load = actor.reload_ammo_amount;
952 // make sure we don't add more ammo than we have
953 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
954 actor.clip_load += load;
955 actor.(actor.ammo_field) -= load;
957 actor.(weapon_load[actor.weapon]) = actor.clip_load;
959 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
960 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
961 // so your weapon is disabled for a few seconds without reason
963 // ATTACK_FINISHED(actor, slot) -= actor.reload_time - 1;
965 Weapon wpn = Weapons_from(actor.weapon);
966 w_ready(wpn, actor, weaponentity, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
969 void W_Reload(entity actor, float sent_ammo_min, string sent_sound)
971 .entity weaponentity = weaponentities[0];
972 // set global values to work with
973 entity e = Weapons_from(actor.weapon);
975 if (cvar("g_overkill"))
976 if (actor.ok_use_ammocharge) return;
979 actor.reload_ammo_min = sent_ammo_min;
980 actor.reload_ammo_amount = e.reloading_ammo;
981 actor.reload_time = e.reloading_time;
982 actor.reload_sound = sent_sound;
984 // don't reload weapons that don't have the RELOADABLE flag
985 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
988 "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
992 // return if reloading is disabled for this weapon
993 if (!actor.reload_ammo_amount) return;
995 // our weapon is fully loaded, no need to reload
996 if (actor.clip_load >= actor.reload_ammo_amount) return;
998 // no ammo, so nothing to load
999 if (actor.ammo_field != ammo_none)
1001 if (!actor.(actor.ammo_field) && actor.reload_ammo_min)
1003 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
1005 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
1007 play2(actor, SND(UNAVAILABLE));
1008 sprint(actor, strcat("You don't have enough ammo to reload the ^2", WEP_NAME(actor.weapon), "\n"));
1009 actor.reload_complain = time + 1;
1011 // switch away if the amount of ammo is not enough to keep using this weapon
1012 Weapon w = Weapons_from(actor.weapon);
1013 if (!(w.wr_checkammo1(w) + w.wr_checkammo2(w)))
1015 actor.clip_load = -1; // reload later
1016 W_SwitchToOtherWeapon(actor);
1023 if (actor.(weaponentity))
1025 if (actor.(weaponentity).wframe == WFRAME_RELOAD) return;
1027 // allow switching away while reloading, but this will cause a new reload!
1028 actor.(weaponentity).state = WS_READY;
1031 // now begin the reloading process
1033 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
1035 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
1036 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
1037 // so your weapon is disabled for a few seconds without reason
1039 // ATTACK_FINISHED(actor, slot) = max(time, ATTACK_FINISHED(actor, slot)) + actor.reload_time + 1;
1041 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
1043 if (actor.clip_load < 0) actor.clip_load = 0;
1044 actor.old_clip_load = actor.clip_load;
1045 actor.clip_load = actor.(weapon_load[actor.weapon]) = -1;
1048 void W_DropEvent(.void(Weapon) event, entity player, float weapon_type, entity weapon_item)
1050 Weapon w = Weapons_from(weapon_type);
1051 weapon_dropevent_item = weapon_item;
1052 WITH(entity, self, player, w.event(w));