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"
19 .float weapon_frametime;
21 float W_WeaponRateFactor()
23 float t = 1.0 / g_weaponratefactor;
25 MUTATOR_CALLHOOK(WeaponRateFactor, t);
31 float W_WeaponSpeedFactor()
33 float t = 1.0 * g_weaponspeedfactor;
35 MUTATOR_CALLHOOK(WeaponSpeedFactor, t);
42 void weapon_thinkf(entity actor, int slot, float fr, float t,
43 void(Weapon thiswep, entity actor, int slot, int fire) func);
45 bool CL_Weaponentity_CustomizeEntityForClient()
48 this.viewmodelforclient = this.owner;
49 if (IS_SPEC(other) && other.enemy == this.owner) this.viewmodelforclient = other;
53 vector CL_Weapon_GetShotOrg(int wpn)
55 entity wi = get_weaponinfo(wpn);
57 CL_WeaponEntity_SetModel(e, wi.mdl);
58 vector ret = e.movedir;
59 CL_WeaponEntity_SetModel(e, "");
64 void CL_Weaponentity_Think()
67 this.nextthink = time;
68 if (intermission_running) this.frame = this.anim_idle.x;
69 int slot = 0; // TODO: unhardcode
70 if (this.owner.weaponentity[slot] != this)
72 // owner has new gun; remove self
73 if (this.weaponchild) remove(this.weaponchild);
77 if (this.owner.deadflag != DEAD_NO)
79 // owner died; disappear
81 if (this.weaponchild) this.weaponchild.model = "";
84 if (this.weaponname != this.owner.weaponname
85 || this.dmg != this.owner.modelindex
86 || this.deadflag != this.owner.deadflag)
88 // owner changed weapons; update appearance
89 this.weaponname = this.owner.weaponname;
90 this.dmg = this.owner.modelindex;
91 this.deadflag = this.owner.deadflag;
93 CL_WeaponEntity_SetModel(this, this.owner.weaponname);
96 int tb = (this.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
97 this.effects = this.owner.effects
101 | EF_FULLBRIGHT // can mask team color, so get rid of it
106 // TODO: don't render this entity at all
109 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
110 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
113 this.glowmod = this.owner.weaponentity_glowmod;
114 this.colormap = this.owner.colormap;
115 if (this.weaponchild)
117 this.weaponchild.effects = this.effects;
118 this.weaponchild.alpha = this.alpha;
119 this.weaponchild.colormap = this.colormap;
120 this.weaponchild.glowmod = this.glowmod;
123 this.angles = '0 0 0';
125 float f = this.owner.weapon_nextthink - time;
126 if (this.state == WS_RAISE && !intermission_running)
128 entity newwep = get_weaponinfo(this.owner.switchweapon);
129 f = f * g_weaponratefactor / max(f, newwep.switchdelay_raise);
130 this.angles_x = -90 * f * f;
132 else if (this.state == WS_DROP && !intermission_running)
134 entity oldwep = get_weaponinfo(this.owner.weapon);
135 f = 1 - f * g_weaponratefactor / max(f, oldwep.switchdelay_drop);
136 this.angles_x = -90 * f * f;
138 else if (this.state == WS_CLEAR)
141 this.angles_x = -90 * f * f;
145 void CL_ExteriorWeaponentity_Think()
148 this.nextthink = time;
149 if (this.owner.exteriorweaponentity != this)
154 if (this.owner.deadflag != DEAD_NO)
159 if (this.weaponname != this.owner.weaponname || this.dmg != this.owner.modelindex
160 || this.deadflag != this.owner.deadflag)
162 this.weaponname = this.owner.weaponname;
163 this.dmg = this.owner.modelindex;
164 this.deadflag = this.owner.deadflag;
165 if (this.owner.weaponname != "") _setmodel(this, W_Model(strcat("v_", this.owner.weaponname, ".md3")));
166 else this.model = "";
169 if ((tag_found = gettagindex(this.owner, "tag_weapon")))
171 this.tag_index = tag_found;
172 this.tag_entity = this.owner;
176 setattachment(this, this.owner, "bip01 r hand");
179 this.effects = this.owner.effects;
180 this.effects |= EF_LOWPRECISION;
181 this.effects = this.effects & EFMASK_CHEAP; // eat performance
182 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
183 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
186 this.glowmod = this.owner.weaponentity_glowmod;
187 this.colormap = this.owner.colormap;
189 CSQCMODEL_AUTOUPDATE(this);
192 // spawning weaponentity for client
193 void CL_SpawnWeaponentity(entity actor, int slot)
195 entity view = actor.weaponentity[slot] = new(weaponentity);
196 view.solid = SOLID_NOT;
198 setmodel(view, MDL_Null); // precision set when changed
199 setorigin(view, '0 0 0');
200 view.think = CL_Weaponentity_Think;
201 view.nextthink = time;
202 view.viewmodelforclient = actor;
203 view.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
207 entity exterior = actor.exteriorweaponentity = new(exteriorweaponentity);
208 exterior.solid = SOLID_NOT;
209 exterior.owner = actor;
210 setorigin(exterior, '0 0 0');
211 exterior.think = CL_ExteriorWeaponentity_Think;
212 exterior.nextthink = time;
214 CSQCMODEL_AUTOINIT(exterior);
219 void w_clear(Weapon thiswep, entity actor, int slot, int fire)
221 if (actor.weapon != -1)
224 actor.switchingweapon = 0;
226 if (actor.weaponentity[slot])
228 actor.weaponentity[slot].state = WS_CLEAR;
229 actor.weaponentity[slot].effects = 0;
233 void w_ready(Weapon thiswep, entity actor, int slot, int fire)
235 if (actor.weaponentity[slot]) actor.weaponentity[slot].state = WS_READY;
236 weapon_thinkf(actor, slot, WFRAME_IDLE, 1000000, w_ready);
241 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary)
243 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
245 if (secondary) WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
246 else WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
247 if (ammo) return true;
248 // always keep the Mine Layer if we placed mines, so that we can detonate them
249 if (thiswep == WEP_MINE_LAYER)
250 for (entity mine; (mine = find(mine, classname, "mine")); )
251 if (mine.owner == actor) return false;
253 if (thiswep == WEP_SHOTGUN)
254 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false; // no clicking, just allow
256 if (thiswep == get_weaponinfo(actor.switchweapon) && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
258 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
259 actor.prevdryfire = time;
262 // check if the other firing mode has enough ammo
263 bool ammo_other = false;
264 if (secondary) WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo1(thiswep));
265 else WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo2(thiswep));
268 if (time - actor.prevwarntime > 1)
274 ITEM_WEAPON_PRIMORSEC,
280 actor.prevwarntime = time;
282 else // this weapon is totally unable to fire, switch to another one
284 W_SwitchToOtherWeapon(actor);
291 bool weapon_prepareattack_check(Weapon thiswep, entity actor, int slot, bool secondary, float attacktime)
293 if (!weapon_prepareattack_checkammo(thiswep, actor, secondary)) return false;
295 // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
296 // if all players readied up and the countdown is running
297 if (time < game_starttime || time < actor.race_penalty) return false;
299 if (timeout_status == TIMEOUT_ACTIVE) // don't allow the player to shoot while game is paused
302 // do not even think about shooting if switching
303 if (actor.switchweapon != actor.weapon) return false;
307 // don't fire if previous attack is not finished
308 if (ATTACK_FINISHED(actor, slot) > time + actor.weapon_frametime * 0.5) return false;
309 // don't fire while changing weapon
310 if (actor.weaponentity[slot].state != WS_READY) return false;
315 void weapon_prepareattack_do(entity actor, int slot, bool secondary, float attacktime)
317 actor.weaponentity[slot].state = WS_INUSE;
319 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
321 // if the weapon hasn't been firing continuously, reset the timer
324 if (ATTACK_FINISHED(actor, slot) < time - actor.weapon_frametime * 1.5)
326 ATTACK_FINISHED(actor, slot) = time;
327 // dprint("resetting attack finished to ", ftos(time), "\n");
329 ATTACK_FINISHED(actor, slot) = ATTACK_FINISHED(actor, slot) + attacktime * W_WeaponRateFactor();
331 actor.bulletcounter += 1;
332 // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, slot)), "\n");
335 bool weapon_prepareattack(Weapon thiswep, entity actor, int slot, bool secondary, float attacktime)
337 if (weapon_prepareattack_check(thiswep, actor, slot, secondary, attacktime))
339 weapon_prepareattack_do(actor, slot, secondary, attacktime);
345 void wframe_send(entity actor, vector a, bool restartanim);
347 void weapon_thinkf(entity actor, int slot, float fr, float t,
348 void(Weapon thiswep, entity actor, int slot, int fire) func)
351 if (fr == WFRAME_DONTCHANGE)
353 fr = actor.weaponentity[slot].wframe;
356 else if (fr == WFRAME_IDLE)
365 vector of = v_forward;
369 if (actor.weaponentity[slot])
371 actor.weaponentity[slot].wframe = fr;
373 if (fr == WFRAME_IDLE) a = actor.weaponentity[slot].anim_idle;
374 else if (fr == WFRAME_FIRE1) a = actor.weaponentity[slot].anim_fire1;
375 else if (fr == WFRAME_FIRE2) a = actor.weaponentity[slot].anim_fire2;
376 else // if (fr == WFRAME_RELOAD)
377 a = actor.weaponentity[slot].anim_reload;
378 a.z *= g_weaponratefactor;
379 wframe_send(actor, a, restartanim);
386 if (actor.weapon_think == w_ready && func != w_ready && actor.weaponentity[slot].state == WS_RAISE) backtrace(
387 "Tried to override initial weapon think function - should this really happen?");
389 t *= W_WeaponRateFactor();
391 // VorteX: haste can be added here
392 if (actor.weapon_think == w_ready)
394 actor.weapon_nextthink = time;
395 // dprint("started firing at ", ftos(time), "\n");
397 if (actor.weapon_nextthink < time - actor.weapon_frametime * 1.5
398 || actor.weapon_nextthink > time + actor.weapon_frametime * 1.5)
400 actor.weapon_nextthink = time;
401 // dprint("reset weapon animation timer at ", ftos(time), "\n");
403 actor.weapon_nextthink = actor.weapon_nextthink + t;
404 actor.weapon_think = func;
405 // dprint("next ", ftos(actor.weapon_nextthink), "\n");
407 if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
409 if ((actor.weapon == WEP_SHOCKWAVE.m_id || actor.weapon == WEP_SHOTGUN.m_id)
410 && fr == WFRAME_FIRE2) animdecide_setaction(actor, ANIMACTION_MELEE, restartanim);
411 else animdecide_setaction(actor, ANIMACTION_SHOOT, restartanim);
415 if (actor.anim_upper_action == ANIMACTION_SHOOT
416 || actor.anim_upper_action == ANIMACTION_MELEE) actor.anim_upper_action = 0;
420 bool forbidWeaponUse(entity player)
422 if (time < game_starttime && !autocvar_sv_ready_restart_after_countdown) return true;
423 if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
424 if (player.player_blocked) return true;
425 if (player.frozen) return true;
426 if (player.weapon_blocked) return true;
430 .bool hook_switchweapon;
432 void W_WeaponFrame(entity actor)
434 int slot = 0; // TODO: unhardcode
435 if (frametime) actor.weapon_frametime = frametime;
437 if (!actor.weaponentity[slot] || actor.health < 1) return; // Dead player can't use weapons and injure impulse commands
439 if (forbidWeaponUse(actor))
441 if (actor.weaponentity[slot].state != WS_CLEAR)
443 Weapon wpn = get_weaponinfo(actor.weapon);
444 w_ready(wpn, actor, 0, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
449 if (!actor.switchweapon)
452 actor.switchingweapon = 0;
453 actor.weaponentity[slot].state = WS_CLEAR;
454 actor.weaponname = "";
455 // actor.items &= ~IT_AMMO;
459 makevectors(actor.v_angle);
460 vector fo = v_forward; // save them in case the weapon think functions change it
465 if (actor.weapon != actor.switchweapon)
467 if (actor.weaponentity[slot].state == WS_CLEAR)
470 actor.switchingweapon = actor.switchweapon;
471 entity newwep = get_weaponinfo(actor.switchweapon);
473 // the two weapon entities will notice this has changed and update their models
474 actor.weapon = actor.switchweapon;
475 actor.weaponname = newwep.mdl;
476 actor.bulletcounter = 0;
477 actor.ammo_field = newwep.ammo_field;
478 Weapon w = get_weaponinfo(actor.switchweapon);
480 actor.weaponentity[slot].state = WS_RAISE;
482 // set our clip load to the load of the weapon we switched to, if it's reloadable
483 if (newwep.spawnflags & WEP_FLAG_RELOADABLE && newwep.reloading_ammo) // prevent accessing undefined cvars
485 actor.clip_load = actor.(weapon_load[actor.switchweapon]);
486 actor.clip_size = newwep.reloading_ammo;
490 actor.clip_load = actor.clip_size = 0;
493 weapon_thinkf(actor, slot, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
495 else if (actor.weaponentity[slot].state == WS_DROP)
497 // in dropping phase we can switch at any time
498 actor.switchingweapon = actor.switchweapon;
500 else if (actor.weaponentity[slot].state == WS_READY)
503 actor.switchingweapon = actor.switchweapon;
504 entity oldwep = get_weaponinfo(actor.weapon);
506 // set up weapon switch think in the future, and start drop anim
508 #if INDEPENDENT_ATTACK_FINISHED
511 ATTACK_FINISHED(actor, slot) <= time + actor.weapon_frametime * 0.5
515 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
516 actor.weaponentity[slot].state = WS_DROP;
517 weapon_thinkf(actor, slot, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
522 // LordHavoc: network timing test code
523 // if (actor.button0)
524 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor, slot)), " >= ", ftos(actor.weapon_nextthink), "\n");
526 int w = actor.weapon;
528 // call the think code which may fire the weapon
529 // and do so multiple times to resolve framerate dependency issues if the
530 // server framerate is very low and the weapon fire rate very high
532 while (c < W_TICSPERFRAME)
535 if (w && !(actor.weapons & WepSet_FromWeapon(w)))
537 if (actor.weapon == actor.switchweapon) W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
545 bool block_weapon = false;
547 bool key_pressed = actor.BUTTON_HOOK && !actor.vehicle;
548 Weapon off = actor.offhand;
549 if (off && !(actor.weapons & WEPSET(HOOK)))
551 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
555 if (key_pressed && actor.switchweapon != WEP_HOOK.m_id && !actor.hook_switchweapon) W_SwitchWeapon(
557 actor.hook_switchweapon = key_pressed;
559 block_weapon = (actor.weapon == h.m_id && (actor.BUTTON_ATCK || key_pressed));
560 h.wr_think(h, actor, 0, block_weapon ? 1 : 0);
568 Weapon e = get_weaponinfo(actor.weapon);
569 e.wr_think(e, actor, 0, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
573 Weapon w = get_weaponinfo(actor.weapon);
578 if (time + actor.weapon_frametime * 0.5 >= actor.weapon_nextthink)
580 if (actor.weapon_think)
585 Weapon wpn = get_weaponinfo(actor.weapon);
586 actor.weapon_think(wpn, actor, 0, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
590 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
596 void W_AttachToShotorg(entity actor, entity flash, vector offset)
600 flash.angles_z = random() * 360;
602 entity view = actor.weaponentity[slot];
603 entity exterior = actor.exteriorweaponentity;
605 if (gettagindex(view, "shot")) setattachment(flash, view, "shot");
606 else setattachment(flash, view, "tag_shot");
607 setorigin(flash, offset);
609 entity xflash = spawn();
610 copyentity(flash, xflash);
612 flash.viewmodelforclient = actor;
614 if (view.oldorigin.x > 0)
616 setattachment(xflash, exterior, "");
617 setorigin(xflash, view.oldorigin + offset);
621 if (gettagindex(exterior, "shot")) setattachment(xflash, exterior, "shot");
622 else setattachment(xflash, exterior, "tag_shot");
623 setorigin(xflash, offset);
627 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
629 if (cvar("g_overkill"))
631 if (actor.ok_use_ammocharge)
633 ok_DecreaseCharge(actor, actor.weapon);
638 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo) return;
640 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
641 if (wep.reloading_ammo)
643 actor.clip_load -= ammo_use;
644 actor.(weapon_load[actor.weapon]) = actor.clip_load;
646 else if (wep.ammo_field != ammo_none)
648 actor.(wep.ammo_field) -= ammo_use;
649 if (actor.(wep.ammo_field) < 0)
652 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
653 "Please notify Samual immediately with a copy of this backtrace!\n",
656 GetAmmoPicture(wep.ammo_field),
658 actor.(wep.ammo_field)
664 // weapon reloading code
666 .float reload_ammo_amount, reload_ammo_min, reload_time;
667 .float reload_complain;
668 .string reload_sound;
670 void W_ReloadedAndReady(Weapon thiswep, entity actor, int slot, int fire)
672 // finish the reloading process, and do the ammo transfer
674 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
676 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
677 if (!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
679 actor.clip_load = actor.reload_ammo_amount;
683 // make sure we don't add more ammo than we have
684 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
685 actor.clip_load += load;
686 actor.(actor.ammo_field) -= load;
688 actor.(weapon_load[actor.weapon]) = actor.clip_load;
690 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
691 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
692 // so your weapon is disabled for a few seconds without reason
694 // ATTACK_FINISHED(actor, slot) -= actor.reload_time - 1;
696 Weapon wpn = get_weaponinfo(actor.weapon);
697 w_ready(wpn, actor, slot, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
700 void W_Reload(entity actor, float sent_ammo_min, string sent_sound)
703 // set global values to work with
704 entity e = get_weaponinfo(actor.weapon);
706 if (cvar("g_overkill"))
707 if (actor.ok_use_ammocharge) return;
710 actor.reload_ammo_min = sent_ammo_min;
711 actor.reload_ammo_amount = e.reloading_ammo;
712 actor.reload_time = e.reloading_time;
713 actor.reload_sound = sent_sound;
715 // don't reload weapons that don't have the RELOADABLE flag
716 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
719 "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
723 // return if reloading is disabled for this weapon
724 if (!actor.reload_ammo_amount) return;
726 // our weapon is fully loaded, no need to reload
727 if (actor.clip_load >= actor.reload_ammo_amount) return;
729 // no ammo, so nothing to load
730 if (actor.ammo_field != ammo_none)
732 if (!actor.(actor.ammo_field) && actor.reload_ammo_min)
734 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
736 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
738 play2(actor, SND(UNAVAILABLE));
739 sprint(actor, strcat("You don't have enough ammo to reload the ^2", WEP_NAME(actor.weapon), "\n"));
740 actor.reload_complain = time + 1;
742 // switch away if the amount of ammo is not enough to keep using this weapon
743 Weapon w = get_weaponinfo(actor.weapon);
744 if (!(w.wr_checkammo1(w) + w.wr_checkammo2(w)))
746 actor.clip_load = -1; // reload later
747 W_SwitchToOtherWeapon(actor);
754 if (actor.weaponentity[slot])
756 if (actor.weaponentity[slot].wframe == WFRAME_RELOAD) return;
758 // allow switching away while reloading, but this will cause a new reload!
759 actor.weaponentity[slot].state = WS_READY;
762 // now begin the reloading process
764 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
766 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
767 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
768 // so your weapon is disabled for a few seconds without reason
770 // ATTACK_FINISHED(actor, slot) = max(time, ATTACK_FINISHED(actor, slot)) + actor.reload_time + 1;
772 weapon_thinkf(actor, slot, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
774 if (actor.clip_load < 0) actor.clip_load = 0;
775 actor.old_clip_load = actor.clip_load;
776 actor.clip_load = actor.(weapon_load[actor.weapon]) = -1;
779 void W_DropEvent(.void(Weapon) event, entity player, float weapon_type, entity weapon_item)
781 Weapon w = get_weaponinfo(weapon_type);
782 weapon_dropevent_item = weapon_item;
783 WITH(entity, self, player, w.event(w));