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, float fr, float t, void(Weapon thiswep, entity actor, int fire) func);
97 bool CL_Weaponentity_CustomizeEntityForClient()
100 this.viewmodelforclient = this.owner;
101 if (IS_SPEC(other) && other.enemy == this.owner) this.viewmodelforclient = other;
108 * 1. simple animated model, muzzle flash handling on h_ model:
109 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
111 * shot = muzzle end (shot origin, also used for muzzle flashes)
112 * shell = casings ejection point (must be on the right hand side of the gun)
113 * weapon = attachment for v_tuba.md3
114 * v_tuba.md3 - first and third person model
115 * g_tuba.md3 - pickup model
117 * 2. simple animated model, muzzle flash handling on v_ model:
118 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
120 * weapon = attachment for v_tuba.md3
121 * v_tuba.md3 - first and third person model
123 * shot = muzzle end (shot origin, also used for muzzle flashes)
124 * shell = casings ejection point (must be on the right hand side of the gun)
125 * g_tuba.md3 - pickup model
127 * 3. fully animated model, muzzle flash handling on h_ model:
128 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
130 * shot = muzzle end (shot origin, also used for muzzle flashes)
131 * shell = casings ejection point (must be on the right hand side of the gun)
132 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
133 * v_tuba.md3 - third person model
134 * g_tuba.md3 - pickup model
136 * 4. fully animated model, muzzle flash handling on v_ model:
137 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
139 * shot = muzzle end (shot origin)
140 * shell = casings ejection point (must be on the right hand side of the gun)
141 * v_tuba.md3 - third person model
143 * shot = muzzle end (for muzzle flashes)
144 * g_tuba.md3 - pickup model
148 // this.origin, this.angles
150 // this.movedir, this.view_ofs
154 // call again with ""
156 void CL_WeaponEntity_SetModel(entity this, string name)
160 // if there is a child entity, hide it until we're sure we use it
161 if (this.weaponentity) this.weaponentity.model = "";
162 _setmodel(this, W_Model(strcat("v_", name, ".md3")));
163 int v_shot_idx = gettagindex(this, "shot"); // used later
164 if (!v_shot_idx) v_shot_idx = gettagindex(this, "tag_shot");
166 _setmodel(this, W_Model(strcat("h_", name, ".iqm")));
167 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
168 this.anim_fire1 = animfixfps(this, '0 1 0.01', '0 0 0');
169 this.anim_fire2 = animfixfps(this, '1 1 0.01', '0 0 0');
170 this.anim_idle = animfixfps(this, '2 1 0.01', '0 0 0');
171 this.anim_reload = animfixfps(this, '3 1 0.01', '0 0 0');
173 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
174 // if we don't, this is a "real" animated model
175 if (gettagindex(this, "weapon"))
177 if (!this.weaponentity) this.weaponentity = spawn();
178 _setmodel(this.weaponentity, W_Model(strcat("v_", name, ".md3")));
179 setattachment(this.weaponentity, this, "weapon");
181 else if (gettagindex(this, "tag_weapon"))
183 if (!this.weaponentity) this.weaponentity = spawn();
184 _setmodel(this.weaponentity, W_Model(strcat("v_", name, ".md3")));
185 setattachment(this.weaponentity, this, "tag_weapon");
189 if (this.weaponentity) remove(this.weaponentity);
190 this.weaponentity = world;
193 setorigin(this, '0 0 0');
194 this.angles = '0 0 0';
196 this.viewmodelforclient = world;
200 if (v_shot_idx) // v_ model attached to invisible h_ model
202 this.movedir = gettaginfo(this.weaponentity, v_shot_idx);
206 idx = gettagindex(this, "shot");
207 if (!idx) idx = gettagindex(this, "tag_shot");
210 this.movedir = gettaginfo(this, idx);
214 LOG_INFO("WARNING: weapon model ", this.model,
215 " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
216 this.movedir = '0 0 0';
220 if (this.weaponentity) // v_ model attached to invisible h_ model
222 idx = gettagindex(this.weaponentity, "shell");
223 if (!idx) idx = gettagindex(this.weaponentity, "tag_shell");
224 if (idx) this.spawnorigin = gettaginfo(this.weaponentity, idx);
232 idx = gettagindex(this, "shell");
233 if (!idx) idx = gettagindex(this, "tag_shell");
236 this.spawnorigin = gettaginfo(this, idx);
240 LOG_INFO("WARNING: weapon model ", this.model,
241 " does not support the 'shell' tag, will display casings wrong\n");
242 this.spawnorigin = this.movedir;
248 this.oldorigin = '0 0 0'; // use regular attachment
252 if (this.weaponentity)
254 idx = gettagindex(this, "weapon");
255 if (!idx) idx = gettagindex(this, "tag_weapon");
259 idx = gettagindex(this, "handle");
260 if (!idx) idx = gettagindex(this, "tag_handle");
264 this.oldorigin = this.movedir - gettaginfo(this, idx);
268 LOG_INFO("WARNING: weapon model ", this.model,
269 " does not support the 'handle' tag and neither does the v_ model support the 'shot' tag, will display muzzle flashes TOTALLY wrong\n");
270 this.oldorigin = '0 0 0'; // there is no way to recover from this
274 this.viewmodelforclient = this.owner;
279 if (this.weaponentity) remove(this.weaponentity);
280 this.weaponentity = world;
281 this.movedir = '0 0 0';
282 this.spawnorigin = '0 0 0';
283 this.oldorigin = '0 0 0';
284 this.anim_fire1 = '0 1 0.01';
285 this.anim_fire2 = '0 1 0.01';
286 this.anim_idle = '0 1 0.01';
287 this.anim_reload = '0 1 0.01';
290 this.view_ofs = '0 0 0';
292 if (this.movedir.x >= 0)
294 vector v0 = this.movedir;
295 this.movedir = shotorg_adjust(v0, false, false, this.owner.cvar_cl_gunalign);
296 this.view_ofs = shotorg_adjust(v0, false, true, this.owner.cvar_cl_gunalign) - v0;
298 this.owner.stat_shotorg = compressShotOrigin(this.movedir);
299 this.movedir = decompressShotOrigin(this.owner.stat_shotorg); // make them match perfectly
301 this.spawnorigin += this.view_ofs; // offset the casings origin by the same amount
303 // check if an instant weapon switch occurred
304 setorigin(this, this.view_ofs);
305 // reset animstate now
306 this.wframe = WFRAME_IDLE;
307 setanim(this, this.anim_idle, true, false, true);
310 vector CL_Weapon_GetShotOrg(float wpn)
312 entity wi = get_weaponinfo(wpn);
314 CL_WeaponEntity_SetModel(e, wi.mdl);
315 vector ret = e.movedir;
316 CL_WeaponEntity_SetModel(e, "");
321 void CL_Weaponentity_Think()
324 this.nextthink = time;
325 if (intermission_running) this.frame = this.anim_idle.x;
326 if (this.owner.weaponentity != this)
328 if (this.weaponentity) remove(this.weaponentity);
332 if (this.owner.deadflag != DEAD_NO)
335 if (this.weaponentity) this.weaponentity.model = "";
338 if (this.weaponname != this.owner.weaponname || this.dmg != this.owner.modelindex
339 || this.deadflag != this.owner.deadflag)
341 this.weaponname = this.owner.weaponname;
342 this.dmg = this.owner.modelindex;
343 this.deadflag = this.owner.deadflag;
345 CL_WeaponEntity_SetModel(this, this.owner.weaponname);
348 int tb = (this.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
349 this.effects = this.owner.effects & EFMASK_CHEAP;
350 this.effects &= ~EF_LOWPRECISION;
351 this.effects &= ~EF_FULLBRIGHT; // can mask team color, so get rid of it
352 this.effects &= ~EF_TELEPORT_BIT;
353 this.effects &= ~EF_RESTARTANIM_BIT;
356 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
357 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
360 this.glowmod = this.owner.weaponentity_glowmod;
361 this.colormap = this.owner.colormap;
362 if (this.weaponentity)
364 this.weaponentity.effects = this.effects;
365 this.weaponentity.alpha = this.alpha;
366 this.weaponentity.colormap = this.colormap;
367 this.weaponentity.glowmod = this.glowmod;
370 this.angles = '0 0 0';
372 float f = (this.owner.weapon_nextthink - time);
373 if (this.state == WS_RAISE && !intermission_running)
375 entity newwep = get_weaponinfo(this.owner.switchweapon);
376 f = f * g_weaponratefactor / max(f, newwep.switchdelay_raise);
377 this.angles_x = -90 * f * f;
379 else if (this.state == WS_DROP && !intermission_running)
381 entity oldwep = get_weaponinfo(this.owner.weapon);
382 f = 1 - f * g_weaponratefactor / max(f, oldwep.switchdelay_drop);
383 this.angles_x = -90 * f * f;
385 else if (this.state == WS_CLEAR)
388 this.angles_x = -90 * f * f;
392 void CL_ExteriorWeaponentity_Think()
395 this.nextthink = time;
396 if (this.owner.exteriorweaponentity != this)
401 if (this.owner.deadflag != DEAD_NO)
406 if (this.weaponname != this.owner.weaponname || this.dmg != this.owner.modelindex
407 || this.deadflag != this.owner.deadflag)
409 this.weaponname = this.owner.weaponname;
410 this.dmg = this.owner.modelindex;
411 this.deadflag = this.owner.deadflag;
412 if (this.owner.weaponname != "") _setmodel(this, W_Model(strcat("v_", this.owner.weaponname, ".md3")));
413 else this.model = "";
416 if ((tag_found = gettagindex(this.owner, "tag_weapon")))
418 this.tag_index = tag_found;
419 this.tag_entity = this.owner;
423 setattachment(this, this.owner, "bip01 r hand");
426 this.effects = this.owner.effects;
427 this.effects |= EF_LOWPRECISION;
428 this.effects = this.effects & EFMASK_CHEAP; // eat performance
429 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
430 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
433 this.glowmod = this.owner.weaponentity_glowmod;
434 this.colormap = this.owner.colormap;
436 CSQCMODEL_AUTOUPDATE(this);
439 // spawning weaponentity for client
440 void CL_SpawnWeaponentity(entity e)
442 entity view = e.weaponentity = spawn();
443 view.classname = "weaponentity";
444 view.solid = SOLID_NOT;
446 setmodel(view, MDL_Null); // precision set when changed
447 setorigin(view, '0 0 0');
448 view.angles = '0 0 0';
449 view.viewmodelforclient = e;
451 view.think = CL_Weaponentity_Think;
452 view.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
453 view.nextthink = time;
455 entity exterior = e.exteriorweaponentity = spawn();
456 exterior.classname = "exteriorweaponentity";
457 exterior.solid = SOLID_NOT;
458 exterior.exteriorweaponentity = exterior;
460 setorigin(exterior, '0 0 0');
461 exterior.angles = '0 0 0';
462 exterior.think = CL_ExteriorWeaponentity_Think;
463 exterior.nextthink = time;
465 CSQCMODEL_AUTOINIT(exterior);
469 void w_clear(Weapon thiswep, entity actor, int fire)
471 if (actor.weapon != -1)
474 actor.switchingweapon = 0;
476 if (actor.weaponentity)
478 actor.weaponentity.state = WS_CLEAR;
479 actor.weaponentity.effects = 0;
483 void w_ready(Weapon thiswep, entity actor, int fire)
485 if (actor.weaponentity) actor.weaponentity.state = WS_READY;
486 weapon_thinkf(actor, WFRAME_IDLE, 1000000, w_ready);
491 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary)
493 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
495 if (secondary) WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
496 else WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
497 if (ammo) return true;
498 // always keep the Mine Layer if we placed mines, so that we can detonate them
499 if (thiswep == WEP_MINE_LAYER)
500 for (entity mine; (mine = find(mine, classname, "mine")); )
501 if (mine.owner == actor) return false;
503 if (thiswep == WEP_SHOTGUN)
504 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false; // no clicking, just allow
506 if (thiswep == get_weaponinfo(actor.switchweapon) && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
508 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
509 actor.prevdryfire = time;
512 // check if the other firing mode has enough ammo
513 bool ammo_other = false;
514 if (secondary) WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo1(thiswep));
515 else WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo2(thiswep));
518 if (time - actor.prevwarntime > 1)
524 ITEM_WEAPON_PRIMORSEC,
530 actor.prevwarntime = time;
532 else // this weapon is totally unable to fire, switch to another one
534 W_SwitchToOtherWeapon(actor);
541 bool weapon_prepareattack_check(Weapon thiswep, entity actor, bool secondary, float attacktime)
543 if (!weapon_prepareattack_checkammo(thiswep, actor, secondary)) return false;
545 // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
546 // if all players readied up and the countdown is running
547 if (time < game_starttime || time < actor.race_penalty) return false;
549 if (timeout_status == TIMEOUT_ACTIVE) // don't allow the player to shoot while game is paused
552 // do not even think about shooting if switching
553 if (actor.switchweapon != actor.weapon) return false;
557 // don't fire if previous attack is not finished
558 if (ATTACK_FINISHED(actor) > time + actor.weapon_frametime * 0.5) return false;
559 // don't fire while changing weapon
560 if (actor.weaponentity.state != WS_READY) return false;
565 void weapon_prepareattack_do(entity actor, bool secondary, float attacktime)
567 actor.weaponentity.state = WS_INUSE;
569 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
571 // if the weapon hasn't been firing continuously, reset the timer
574 if (ATTACK_FINISHED(actor) < time - actor.weapon_frametime * 1.5)
576 ATTACK_FINISHED(actor) = time;
577 // dprint("resetting attack finished to ", ftos(time), "\n");
579 ATTACK_FINISHED(actor) = ATTACK_FINISHED(actor) + attacktime * W_WeaponRateFactor();
581 actor.bulletcounter += 1;
582 // dprint("attack finished ", ftos(ATTACK_FINISHED(actor)), "\n");
585 bool weapon_prepareattack(Weapon thiswep, entity actor, bool secondary, float attacktime)
587 if (weapon_prepareattack_check(thiswep, actor, secondary, attacktime))
589 weapon_prepareattack_do(actor, secondary, attacktime);
595 void weapon_thinkf(entity actor, float fr, float t, void(Weapon thiswep, entity actor, int fire) func)
598 if (fr == WFRAME_DONTCHANGE)
600 fr = actor.weaponentity.wframe;
603 else if (fr == WFRAME_IDLE)
612 vector of = v_forward;
616 if (actor.weaponentity)
618 actor.weaponentity.wframe = fr;
620 if (fr == WFRAME_IDLE) a = actor.weaponentity.anim_idle;
621 else if (fr == WFRAME_FIRE1) a = actor.weaponentity.anim_fire1;
622 else if (fr == WFRAME_FIRE2) a = actor.weaponentity.anim_fire2;
623 else // if (fr == WFRAME_RELOAD)
624 a = actor.weaponentity.anim_reload;
625 a.z *= g_weaponratefactor;
626 setanim(actor.weaponentity, a, restartanim == false, restartanim, restartanim);
633 if (actor.weapon_think == w_ready && func != w_ready && actor.weaponentity.state == WS_RAISE) backtrace(
634 "Tried to override initial weapon think function - should this really happen?");
636 t *= W_WeaponRateFactor();
638 // VorteX: haste can be added here
639 if (actor.weapon_think == w_ready)
641 actor.weapon_nextthink = time;
642 // dprint("started firing at ", ftos(time), "\n");
644 if (actor.weapon_nextthink < time - actor.weapon_frametime * 1.5
645 || actor.weapon_nextthink > time + actor.weapon_frametime * 1.5)
647 actor.weapon_nextthink = time;
648 // dprint("reset weapon animation timer at ", ftos(time), "\n");
650 actor.weapon_nextthink = actor.weapon_nextthink + t;
651 actor.weapon_think = func;
652 // dprint("next ", ftos(actor.weapon_nextthink), "\n");
654 if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
656 if ((actor.weapon == WEP_SHOCKWAVE.m_id || actor.weapon == WEP_SHOTGUN.m_id)
657 && fr == WFRAME_FIRE2) animdecide_setaction(actor, ANIMACTION_MELEE, restartanim);
658 else animdecide_setaction(actor, ANIMACTION_SHOOT, restartanim);
662 if (actor.anim_upper_action == ANIMACTION_SHOOT
663 || actor.anim_upper_action == ANIMACTION_MELEE) actor.anim_upper_action = 0;
667 bool forbidWeaponUse(entity player)
669 if (time < game_starttime && !autocvar_sv_ready_restart_after_countdown) return true;
670 if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
671 if (player.player_blocked) return true;
672 if (player.frozen) return true;
673 if (player.weapon_blocked) return true;
677 .bool hook_switchweapon;
679 void W_WeaponFrame(entity actor)
681 if (frametime) actor.weapon_frametime = frametime;
683 if (!actor.weaponentity || actor.health < 1) return; // Dead player can't use weapons and injure impulse commands
685 if (forbidWeaponUse(actor))
687 if (actor.weaponentity.state != WS_CLEAR)
689 Weapon wpn = get_weaponinfo(actor.weapon);
690 w_ready(wpn, actor, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
695 if (!actor.switchweapon)
698 actor.switchingweapon = 0;
699 actor.weaponentity.state = WS_CLEAR;
700 actor.weaponname = "";
701 // actor.items &= ~IT_AMMO;
705 makevectors(actor.v_angle);
706 vector fo = v_forward; // save them in case the weapon think functions change it
711 if (actor.weapon != actor.switchweapon)
713 if (actor.weaponentity.state == WS_CLEAR)
716 actor.switchingweapon = actor.switchweapon;
717 entity newwep = get_weaponinfo(actor.switchweapon);
719 // the two weapon entities will notice this has changed and update their models
720 actor.weapon = actor.switchweapon;
721 actor.weaponname = newwep.mdl;
722 actor.bulletcounter = 0;
723 actor.ammo_field = newwep.ammo_field;
724 Weapon w = get_weaponinfo(actor.switchweapon);
726 actor.weaponentity.state = WS_RAISE;
728 // set our clip load to the load of the weapon we switched to, if it's reloadable
729 if (newwep.spawnflags & WEP_FLAG_RELOADABLE && newwep.reloading_ammo) // prevent accessing undefined cvars
731 actor.clip_load = actor.(weapon_load[actor.switchweapon]);
732 actor.clip_size = newwep.reloading_ammo;
736 actor.clip_load = actor.clip_size = 0;
739 weapon_thinkf(actor, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
741 else if (actor.weaponentity.state == WS_DROP)
743 // in dropping phase we can switch at any time
744 actor.switchingweapon = actor.switchweapon;
746 else if (actor.weaponentity.state == WS_READY)
749 actor.switchingweapon = actor.switchweapon;
750 entity oldwep = get_weaponinfo(actor.weapon);
752 // set up weapon switch think in the future, and start drop anim
754 #if INDEPENDENT_ATTACK_FINISHED
757 ATTACK_FINISHED(actor) <= time + actor.weapon_frametime * 0.5
761 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
762 actor.weaponentity.state = WS_DROP;
763 weapon_thinkf(actor, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
768 // LordHavoc: network timing test code
769 // if (actor.button0)
770 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor)), " >= ", ftos(actor.weapon_nextthink), "\n");
772 int w = actor.weapon;
774 // call the think code which may fire the weapon
775 // and do so multiple times to resolve framerate dependency issues if the
776 // server framerate is very low and the weapon fire rate very high
778 while (c < W_TICSPERFRAME)
781 if (w && !(actor.weapons & WepSet_FromWeapon(w)))
783 if (actor.weapon == actor.switchweapon) W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
791 bool block_weapon = false;
793 bool key_pressed = actor.BUTTON_HOOK && !actor.vehicle;
794 Weapon off = actor.offhand;
795 if (off && !(actor.weapons & WEPSET(HOOK)))
797 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
801 if (key_pressed && actor.switchweapon != WEP_HOOK.m_id && !actor.hook_switchweapon) W_SwitchWeapon(
803 actor.hook_switchweapon = key_pressed;
805 block_weapon = (actor.weapon == h.m_id && (actor.BUTTON_ATCK || key_pressed));
806 h.wr_think(h, actor, block_weapon ? 1 : 0);
814 Weapon e = get_weaponinfo(actor.weapon);
815 e.wr_think(e, actor, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
819 Weapon w = get_weaponinfo(actor.weapon);
824 if (time + actor.weapon_frametime * 0.5 >= actor.weapon_nextthink)
826 if (actor.weapon_think)
831 Weapon wpn = get_weaponinfo(actor.weapon);
832 actor.weapon_think(wpn, actor, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
836 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
842 void W_AttachToShotorg(entity actor, entity flash, vector offset)
845 flash.angles_z = random() * 360;
847 if (gettagindex(actor.weaponentity, "shot")) setattachment(flash, actor.weaponentity, "shot");
848 else setattachment(flash, actor.weaponentity, "tag_shot");
849 setorigin(flash, offset);
851 entity xflash = spawn();
852 copyentity(flash, xflash);
854 flash.viewmodelforclient = actor;
856 if (actor.weaponentity.oldorigin.x > 0)
858 setattachment(xflash, actor.exteriorweaponentity, "");
859 setorigin(xflash, actor.weaponentity.oldorigin + offset);
863 if (gettagindex(actor.exteriorweaponentity, "shot")) setattachment(xflash, actor.exteriorweaponentity, "shot");
864 else setattachment(xflash, actor.exteriorweaponentity, "tag_shot");
865 setorigin(xflash, offset);
869 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
871 if (cvar("g_overkill"))
873 if (actor.ok_use_ammocharge)
875 ok_DecreaseCharge(actor, actor.weapon);
880 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo) return;
882 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
883 if (wep.reloading_ammo)
885 actor.clip_load -= ammo_use;
886 actor.(weapon_load[actor.weapon]) = actor.clip_load;
888 else if (wep.ammo_field != ammo_none)
890 actor.(wep.ammo_field) -= ammo_use;
891 if (actor.(wep.ammo_field) < 0)
894 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
895 "Please notify Samual immediately with a copy of this backtrace!\n",
898 GetAmmoPicture(wep.ammo_field),
900 actor.(wep.ammo_field)
906 // weapon reloading code
908 .float reload_ammo_amount, reload_ammo_min, reload_time;
909 .float reload_complain;
910 .string reload_sound;
912 void W_ReloadedAndReady(Weapon thiswep, entity actor, int fire)
914 // finish the reloading process, and do the ammo transfer
916 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
918 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
919 if (!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
921 actor.clip_load = actor.reload_ammo_amount;
925 // make sure we don't add more ammo than we have
926 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
927 actor.clip_load += load;
928 actor.(actor.ammo_field) -= load;
930 actor.(weapon_load[actor.weapon]) = actor.clip_load;
932 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
933 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
934 // so your weapon is disabled for a few seconds without reason
936 // ATTACK_FINISHED(actor) -= actor.reload_time - 1;
938 Weapon wpn = get_weaponinfo(actor.weapon);
939 w_ready(wpn, actor, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
942 void W_Reload(entity actor, float sent_ammo_min, string sent_sound)
944 // set global values to work with
945 entity e = get_weaponinfo(actor.weapon);
947 if (cvar("g_overkill"))
948 if (actor.ok_use_ammocharge) return;
951 actor.reload_ammo_min = sent_ammo_min;
952 actor.reload_ammo_amount = e.reloading_ammo;
953 actor.reload_time = e.reloading_time;
954 actor.reload_sound = sent_sound;
956 // don't reload weapons that don't have the RELOADABLE flag
957 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
960 "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
964 // return if reloading is disabled for this weapon
965 if (!actor.reload_ammo_amount) return;
967 // our weapon is fully loaded, no need to reload
968 if (actor.clip_load >= actor.reload_ammo_amount) return;
970 // no ammo, so nothing to load
971 if (actor.ammo_field != ammo_none)
973 if (!actor.(actor.ammo_field) && actor.reload_ammo_min)
975 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
977 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
979 play2(actor, SND(UNAVAILABLE));
980 sprint(actor, strcat("You don't have enough ammo to reload the ^2", WEP_NAME(actor.weapon), "\n"));
981 actor.reload_complain = time + 1;
983 // switch away if the amount of ammo is not enough to keep using this weapon
984 Weapon w = get_weaponinfo(actor.weapon);
985 if (!(w.wr_checkammo1(w) + w.wr_checkammo2(w)))
987 actor.clip_load = -1; // reload later
988 W_SwitchToOtherWeapon(actor);
995 if (actor.weaponentity)
997 if (actor.weaponentity.wframe == WFRAME_RELOAD) return;
999 // allow switching away while reloading, but this will cause a new reload!
1000 actor.weaponentity.state = WS_READY;
1003 // now begin the reloading process
1005 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
1007 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
1008 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
1009 // so your weapon is disabled for a few seconds without reason
1011 // ATTACK_FINISHED(actor) = max(time, ATTACK_FINISHED(actor)) + actor.reload_time + 1;
1013 weapon_thinkf(actor, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
1015 if (actor.clip_load < 0) actor.clip_load = 0;
1016 actor.old_clip_load = actor.clip_load;
1017 actor.clip_load = actor.(weapon_load[actor.weapon]) = -1;
1020 void W_DropEvent(.void(Weapon) event, entity player, float weapon_type, entity weapon_item)
1022 Weapon w = get_weaponinfo(weapon_type);
1023 weapon_dropevent_item = weapon_item;
1024 WITH(entity, self, player, w.event(w));