1 #include "weaponsystem.qh"
3 #include "selection.qh"
5 #include "../command/common.qh"
6 #include "../mutators/_mod.qh"
7 #include "../round_handler.qh"
8 #include <common/t_items.qh>
9 #include <common/animdecide.qh>
10 #include <common/constants.qh>
11 #include <common/net_linked.qh>
12 #include <common/monsters/_mod.qh>
13 #include <common/notifications/all.qh>
14 #include <common/util.qh>
15 #include <common/weapons/_all.qh>
16 #include <common/state.qh>
17 #include <lib/csqcmodel/sv_model.qh>
21 .float weapon_frametime;
23 float W_WeaponRateFactor(entity this)
26 if(g_weaponratefactor > 0)
27 t = 1.0 / g_weaponratefactor;
29 MUTATOR_CALLHOOK(WeaponRateFactor, t, this);
35 float W_WeaponSpeedFactor(entity this)
37 float t = 1.0 * g_weaponspeedfactor;
39 MUTATOR_CALLHOOK(WeaponSpeedFactor, t, this);
46 bool CL_Weaponentity_CustomizeEntityForClient(entity this, entity client)
48 this.viewmodelforclient = this.owner;
49 if (IS_SPEC(client) && client.enemy == this.owner) this.viewmodelforclient = client;
53 vector CL_Weapon_GetShotOrg(int wpn)
55 entity wi = Weapons_from(wpn);
57 CL_WeaponEntity_SetModel(e, wi.mdl, false);
58 vector ret = e.movedir;
59 CL_WeaponEntity_SetModel(e, "", false);
64 ..entity weaponentity_fld;
67 void CL_Weaponentity_Think(entity this)
69 this.nextthink = time;
70 if (intermission_running) this.frame = this.anim_idle.x;
71 .entity weaponentity = this.weaponentity_fld;
72 if (this.owner.(weaponentity) != this)
74 // owner has new gun; remove old one
75 if (this.weaponchild) delete(this.weaponchild);
79 if (IS_DEAD(this.owner))
81 // owner died; disappear
83 if (this.weaponchild) this.weaponchild.model = "";
86 if (this.weaponname != this.owner.weaponname
87 || this.dmg != this.owner.modelindex
88 || this.deadflag != this.owner.deadflag)
90 // owner changed weapons; update appearance
91 this.weaponname = this.owner.weaponname;
92 this.dmg = this.owner.modelindex;
93 this.deadflag = this.owner.deadflag;
95 CL_WeaponEntity_SetModel(this, this.owner.weaponname, true);
98 this.alpha = -1; // TODO: don't render this entity at all
100 if (this.owner.alpha == default_player_alpha) this.m_alpha = default_weapon_alpha;
101 else if (this.owner.alpha != 0) this.m_alpha = this.owner.alpha;
102 else this.m_alpha = 1;
104 if (this.weaponchild)
106 this.weaponchild.alpha = this.alpha;
107 this.weaponchild.effects = this.effects;
111 void CL_ExteriorWeaponentity_Think(entity this)
113 this.nextthink = time;
114 if (this.owner.exteriorweaponentity != this)
119 if (IS_DEAD(this.owner))
124 if (this.weaponname != this.owner.weaponname || this.dmg != this.owner.modelindex
125 || this.deadflag != this.owner.deadflag)
127 this.weaponname = this.owner.weaponname;
128 this.dmg = this.owner.modelindex;
129 this.deadflag = this.owner.deadflag;
130 if (this.owner.weaponname != "")
132 _setmodel(this, W_Model(strcat("v_", this.owner.weaponname, ".md3")));
133 setsize(this, '0 0 0', '0 0 0');
135 else this.model = "";
138 if ((tag_found = gettagindex(this.owner, "tag_weapon")))
140 this.tag_index = tag_found;
141 this.tag_entity = this.owner;
145 setattachment(this, this.owner, "bip01 r hand");
148 this.effects = this.owner.effects;
149 this.effects |= EF_LOWPRECISION;
150 this.effects = this.effects & EFMASK_CHEAP; // eat performance
151 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
152 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
155 Weapon wep = PS(this.owner).m_weapon;
156 if (wep) this.glowmod = weaponentity_glowmod(wep, this.owner, this.owner.clientcolors);
157 this.colormap = this.owner.colormap;
159 CSQCMODEL_AUTOUPDATE(this);
162 // spawning weaponentity for client
163 void CL_SpawnWeaponentity(entity actor, .entity weaponentity)
165 entity view = actor.(weaponentity) = new(weaponentity);
166 view.solid = SOLID_NOT;
168 setmodel(view, MDL_Null); // precision set when changed
169 setorigin(view, '0 0 0');
170 view.weaponentity_fld = weaponentity;
171 setthink(view, CL_Weaponentity_Think);
172 view.nextthink = time;
173 view.viewmodelforclient = actor;
174 setcefc(view, CL_Weaponentity_CustomizeEntityForClient);
176 if (weaponentity == weaponentities[0])
178 entity exterior = actor.exteriorweaponentity = new(exteriorweaponentity);
179 exterior.solid = SOLID_NOT;
180 exterior.owner = actor;
181 setorigin(exterior, '0 0 0');
182 setthink(exterior, CL_ExteriorWeaponentity_Think);
183 exterior.nextthink = time;
185 CSQCMODEL_AUTOINIT(exterior);
190 void w_clear(Weapon thiswep, entity actor, .entity weaponentity, int fire)
192 PS(actor).m_weapon = WEP_Null;
193 PS(actor).m_switchingweapon = WEP_Null;
194 entity this = actor.(weaponentity);
197 this.state = WS_CLEAR;
202 void w_ready(Weapon thiswep, entity actor, .entity weaponentity, int fire)
204 entity this = actor.(weaponentity);
205 if (this) this.state = WS_READY;
206 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, 1000000, w_ready);
211 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary)
213 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
215 if (secondary) ammo = thiswep.wr_checkammo2(thiswep, actor);
216 else ammo = thiswep.wr_checkammo1(thiswep, actor);
217 if (ammo) return true;
218 // always keep the Mine Layer if we placed mines, so that we can detonate them
219 if (thiswep == WEP_MINE_LAYER)
221 IL_EACH(g_mines, it.owner == actor,
227 if (thiswep == WEP_SHOTGUN)
228 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false; // no clicking, just allow
230 if (thiswep == PS(actor).m_switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
232 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
233 actor.prevdryfire = time;
236 // check if the other firing mode has enough ammo
237 bool ammo_other = false;
238 if (secondary) ammo_other = thiswep.wr_checkammo1(thiswep, actor);
239 else ammo_other = thiswep.wr_checkammo2(thiswep, actor);
242 if (time - actor.prevwarntime > 1)
248 ITEM_WEAPON_PRIMORSEC,
254 actor.prevwarntime = time;
256 else // this weapon is totally unable to fire, switch to another one
258 W_SwitchToOtherWeapon(actor);
265 bool weapon_prepareattack_check(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
267 if (actor.weaponentity == NULL) return true;
268 if (!weapon_prepareattack_checkammo(thiswep, actor, secondary)) return false;
270 // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
271 // if all players readied up and the countdown is running
272 if (time < game_starttime || time < actor.race_penalty) return false;
274 if (timeout_status == TIMEOUT_ACTIVE) // don't allow the player to shoot while game is paused
277 // do not even think about shooting if switching
278 if (PS(actor).m_switchweapon != PS(actor).m_weapon) return false;
282 int slot = weaponslot(weaponentity);
283 // don't fire if previous attack is not finished
284 if (ATTACK_FINISHED(actor, slot) > time + actor.weapon_frametime * 0.5) return false;
285 entity this = actor.(weaponentity);
286 // don't fire while changing weapon
287 if (this.state != WS_READY) return false;
292 void weapon_prepareattack_do(entity actor, .entity weaponentity, bool secondary, float attacktime)
294 entity this = actor.(weaponentity);
295 if (this == NULL) return;
296 this.state = WS_INUSE;
298 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
300 // if the weapon hasn't been firing continuously, reset the timer
303 int slot = weaponslot(weaponentity);
304 if (ATTACK_FINISHED(actor, slot) < time - actor.weapon_frametime * 1.5)
306 ATTACK_FINISHED(actor, slot) = time;
307 // dprint("resetting attack finished to ", ftos(time), "\n");
309 ATTACK_FINISHED(actor, slot) = ATTACK_FINISHED(actor, slot) + attacktime * W_WeaponRateFactor(actor);
311 actor.bulletcounter += 1;
312 // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, slot)), "\n");
315 bool weapon_prepareattack(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
317 if (weapon_prepareattack_check(thiswep, actor, weaponentity, secondary, attacktime))
319 weapon_prepareattack_do(actor, weaponentity, secondary, attacktime);
325 void wframe_send(entity actor, entity weaponentity, vector a, bool restartanim);
328 * @param t defer thinking until time + t
329 * @param func next think function
331 void weapon_thinkf(entity actor, .entity weaponentity, WFRAME fr, float t, void(Weapon thiswep, entity actor,
332 .entity weaponentity, int fire) func)
334 entity this = actor.(weaponentity);
335 if (this == NULL) return;
337 if (fr == WFRAME_DONTCHANGE)
344 restartanim = fr != WFRAME_IDLE;
347 vector of = v_forward;
353 if (fr == WFRAME_IDLE) a = this.anim_idle;
354 else if (fr == WFRAME_FIRE1) a = this.anim_fire1;
355 else if (fr == WFRAME_FIRE2) a = this.anim_fire2;
356 else // if (fr == WFRAME_RELOAD)
357 a = this.anim_reload;
358 a.z *= g_weaponratefactor;
364 if (this.weapon_think == w_ready && func != w_ready && this.state == WS_RAISE) backtrace(
365 "Tried to override initial weapon think function - should this really happen?");
367 t *= W_WeaponRateFactor(actor);
369 // VorteX: haste can be added here
370 if (this.weapon_think == w_ready)
372 this.weapon_nextthink = time;
373 // dprint("started firing at ", ftos(time), "\n");
375 if (this.weapon_nextthink < time - actor.weapon_frametime * 1.5
376 || this.weapon_nextthink > time + actor.weapon_frametime * 1.5)
378 this.weapon_nextthink = time;
379 // dprint("reset weapon animation timer at ", ftos(time), "\n");
381 this.weapon_nextthink += t;
382 if (weaponentity == weaponentities[0]) STAT(WEAPON_NEXTTHINK, actor) = this.weapon_nextthink;
383 this.weapon_think = func;
384 // dprint("next ", ftos(this.weapon_nextthink), "\n");
388 FOREACH_CLIENT(true, LAMBDA(
389 if(it == actor || (IS_SPEC(it) && it.enemy == actor))
390 wframe_send(it, this, a, restartanim);
394 if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
396 bool primary_melee = boolean(fr == WFRAME_FIRE1 && (PS(actor).m_weapon.spawnflags & WEP_TYPE_MELEE_PRI));
397 bool secondary_melee = boolean(fr == WFRAME_FIRE2 && (PS(actor).m_weapon.spawnflags & WEP_TYPE_MELEE_SEC));
398 int act = (primary_melee || secondary_melee)
402 animdecide_setaction(actor, act, restartanim);
404 else if (actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
406 actor.anim_upper_action = 0;
410 bool forbidWeaponUse(entity player)
412 if (time < game_starttime && !autocvar_sv_ready_restart_after_countdown) return true;
413 if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
414 if (player.player_blocked) return true;
415 if (gameover) return true;
416 if (STAT(FROZEN, player)) return true;
417 if (player.weapon_blocked) return true;
418 if (MUTATOR_CALLHOOK(ForbidWeaponUse, player)) return true;
422 .bool hook_switchweapon;
424 void W_WeaponFrame(Player actor, .entity weaponentity)
427 TC(PlayerState, PS(actor));
428 entity this = actor.(weaponentity);
429 if (frametime) actor.weapon_frametime = frametime;
431 if (!this || actor.health < 1) return; // Dead player can't use weapons and injure impulse commands
434 if (forbidWeaponUse(actor))
436 if (actor.(weaponentity).state != WS_CLEAR)
438 Weapon wpn = PS(actor).m_weapon;
439 w_ready(wpn, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
444 if (PS(actor).m_switchweapon == WEP_Null)
446 PS(actor).m_weapon = WEP_Null;
447 PS(actor).m_switchingweapon = WEP_Null;
448 this.state = WS_CLEAR;
449 actor.weaponname = "";
450 // actor.items &= ~IT_AMMO;
454 makevectors(actor.v_angle);
455 vector fo = v_forward; // save them in case the weapon think functions change it
460 if (PS(actor).m_weapon != PS(actor).m_switchweapon)
465 LOG_WARNF("unhandled weaponentity (%i) state for player (%i): %d", this, actor, this.state);
473 Weapon newwep = PS(actor).m_switchweapon;
474 PS(actor).m_switchingweapon = newwep;
476 // the two weapon entities will notice this has changed and update their models
477 PS(actor).m_weapon = newwep;
478 actor.weaponname = newwep.mdl;
479 actor.bulletcounter = 0;
480 actor.ammo_field = newwep.ammo_field;
481 newwep.wr_setup(newwep, actor);
482 this.state = WS_RAISE;
484 // set our clip load to the load of the weapon we switched to, if it's reloadable
485 if ((newwep.spawnflags & WEP_FLAG_RELOADABLE) && newwep.reloading_ammo) // prevent accessing undefined cvars
487 actor.clip_load = actor.(weapon_load[PS(actor).m_switchweapon.m_id]);
488 actor.clip_size = newwep.reloading_ammo;
492 actor.clip_load = actor.clip_size = 0;
495 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
500 // in dropping phase we can switch at any time
501 PS(actor).m_switchingweapon = PS(actor).m_switchweapon;
507 PS(actor).m_switchingweapon = PS(actor).m_switchweapon;
508 entity oldwep = PS(actor).m_weapon;
510 // set up weapon switch think in the future, and start drop anim
511 if (INDEPENDENT_ATTACK_FINISHED || ATTACK_FINISHED(actor, weaponslot(weaponentity)) <= time + actor.weapon_frametime * 0.5)
513 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
514 this.state = WS_DROP;
515 weapon_thinkf(actor, weaponentity, 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(this.weapon_nextthink), "\n");
526 Weapon w = PS(actor).m_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
531 for (int c = 0; c < W_TICSPERFRAME; ++c)
533 if (w != WEP_Null && !(actor.weapons & WepSet_FromWeapon(w)))
535 if (PS(actor).m_weapon == PS(actor).m_switchweapon) W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
543 bool block_weapon = false;
545 bool key_pressed = PHYS_INPUT_BUTTON_HOOK(actor) && !actor.vehicle;
546 Weapon off = actor.offhand;
547 if (off && !(actor.weapons & WEPSET(HOOK)))
549 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
553 if (key_pressed && PS(actor).m_switchweapon != WEP_HOOK && !actor.hook_switchweapon)
554 W_SwitchWeapon(actor, WEP_HOOK);
555 actor.hook_switchweapon = key_pressed;
557 block_weapon = (PS(actor).m_weapon == h && (PHYS_INPUT_BUTTON_ATCK(actor) || key_pressed));
558 h.wr_think(h, actor, weaponentity, block_weapon ? 1 : 0);
568 Weapon e = PS(actor).m_weapon;
572 e.wr_think(e, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
576 e.wr_gonethink(e, actor, weaponentity);
580 if (time + actor.weapon_frametime * 0.5 >= this.weapon_nextthink)
582 if (this.weapon_think)
587 Weapon wpn = PS(actor).m_weapon;
588 this.weapon_think(wpn, actor, weaponentity,
589 PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
593 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
599 void W_AttachToShotorg(entity actor, .entity weaponentity, entity flash, vector offset)
602 flash.angles_z = random() * 360;
604 entity view = actor.(weaponentity);
605 entity exterior = actor.exteriorweaponentity;
607 if (gettagindex(view, "shot")) setattachment(flash, view, "shot");
608 else setattachment(flash, view, "tag_shot");
609 setorigin(flash, offset);
611 entity xflash = spawn();
612 copyentity(flash, xflash);
614 flash.viewmodelforclient = actor;
616 if (view.oldorigin.x > 0)
618 setattachment(xflash, exterior, "");
619 setorigin(xflash, view.oldorigin + offset);
623 if (gettagindex(exterior, "shot")) setattachment(xflash, exterior, "shot");
624 else setattachment(xflash, exterior, "tag_shot");
625 setorigin(xflash, offset);
629 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
631 if (MUTATOR_CALLHOOK(W_DecreaseAmmo, actor)) return;
633 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo) return;
635 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
636 if (wep.reloading_ammo)
638 actor.clip_load -= ammo_use;
639 actor.(weapon_load[PS(actor).m_weapon.m_id]) = actor.clip_load;
641 else if (wep.ammo_field != ammo_none)
643 actor.(wep.ammo_field) -= ammo_use;
644 if (actor.(wep.ammo_field) < 0)
647 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
648 "Please notify Samual immediately with a copy of this backtrace!\n",
651 GetAmmoPicture(wep.ammo_field),
653 actor.(wep.ammo_field)
659 // weapon reloading code
661 .float reload_ammo_amount, reload_ammo_min, reload_time;
662 .float reload_complain;
663 .string reload_sound;
665 void W_ReloadedAndReady(Weapon thiswep, entity actor, .entity weaponentity, int fire)
667 // finish the reloading process, and do the ammo transfer
669 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
671 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
672 if (!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
674 actor.clip_load = actor.reload_ammo_amount;
678 // make sure we don't add more ammo than we have
679 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
680 actor.clip_load += load;
681 actor.(actor.ammo_field) -= load;
683 actor.(weapon_load[PS(actor).m_weapon.m_id]) = actor.clip_load;
685 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
686 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
687 // so your weapon is disabled for a few seconds without reason
689 // ATTACK_FINISHED(actor, slot) -= actor.reload_time - 1;
691 Weapon wpn = Weapons_from(PS(actor).m_weapon.m_id);
692 w_ready(wpn, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
695 void W_Reload(entity actor, .entity weaponentity, float sent_ammo_min, Sound sent_sound)
697 TC(Sound, sent_sound);
698 // set global values to work with
699 Weapon e = PS(actor).m_weapon;
701 if (MUTATOR_CALLHOOK(W_Reload, actor)) return;
703 actor.reload_ammo_min = sent_ammo_min;
704 actor.reload_ammo_amount = e.reloading_ammo;
705 actor.reload_time = e.reloading_time;
706 if (actor.reload_sound) strunzone(actor.reload_sound);
707 actor.reload_sound = strzone(Sound_fixpath(sent_sound));
709 // don't reload weapons that don't have the RELOADABLE flag
710 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
713 "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
717 // return if reloading is disabled for this weapon
718 if (!actor.reload_ammo_amount) return;
720 // our weapon is fully loaded, no need to reload
721 if (actor.clip_load >= actor.reload_ammo_amount) return;
723 // no ammo, so nothing to load
724 if (actor.ammo_field != ammo_none)
726 if (!actor.(actor.ammo_field) && actor.reload_ammo_min)
728 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
730 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
732 play2(actor, SND(UNAVAILABLE));
733 sprint(actor, strcat("You don't have enough ammo to reload the ^2", PS(actor).m_weapon.m_name, "\n"));
734 actor.reload_complain = time + 1;
736 // switch away if the amount of ammo is not enough to keep using this weapon
737 Weapon w = PS(actor).m_weapon;
738 if (!(w.wr_checkammo1(w, actor) + w.wr_checkammo2(w, actor)))
740 actor.clip_load = -1; // reload later
741 W_SwitchToOtherWeapon(actor);
748 entity this = actor.(weaponentity);
751 if (this.wframe == WFRAME_RELOAD) return;
753 // allow switching away while reloading, but this will cause a new reload!
754 this.state = WS_READY;
757 // now begin the reloading process
759 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
761 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
762 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
763 // so your weapon is disabled for a few seconds without reason
765 // ATTACK_FINISHED(actor, slot) = max(time, ATTACK_FINISHED(actor, slot)) + actor.reload_time + 1;
767 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
769 if (actor.clip_load < 0) actor.clip_load = 0;
770 actor.old_clip_load = actor.clip_load;
771 actor.clip_load = actor.(weapon_load[PS(actor).m_weapon.m_id]) = -1;
774 void W_DropEvent(.void(Weapon, entity actor) event, entity player, float weapon_type, entity weapon_item)
776 Weapon w = Weapons_from(weapon_type);
777 weapon_dropevent_item = weapon_item;