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 <common/t_items.qh>
9 #include <common/animdecide.qh>
10 #include <common/constants.qh>
11 #include <common/monsters/all.qh>
12 #include <common/notifications/all.qh>
13 #include <common/util.qh>
14 #include <common/weapons/all.qh>
15 #include <common/state.qh>
16 #include <lib/csqcmodel/sv_model.qh>
20 .float weapon_frametime;
22 float W_WeaponRateFactor(entity this)
24 float t = 1.0 / g_weaponratefactor;
26 MUTATOR_CALLHOOK(WeaponRateFactor, t, this);
32 float W_WeaponSpeedFactor(entity this)
34 float t = 1.0 * g_weaponspeedfactor;
36 MUTATOR_CALLHOOK(WeaponSpeedFactor, t, this);
43 bool CL_Weaponentity_CustomizeEntityForClient(entity this)
45 this.viewmodelforclient = this.owner;
46 if (IS_SPEC(other) && other.enemy == this.owner) this.viewmodelforclient = other;
50 vector CL_Weapon_GetShotOrg(int wpn)
52 entity wi = Weapons_from(wpn);
54 CL_WeaponEntity_SetModel(e, wi.mdl, false);
55 vector ret = e.movedir;
56 CL_WeaponEntity_SetModel(e, "", false);
61 ..entity weaponentity_fld;
64 void CL_Weaponentity_Think(entity this)
66 this.nextthink = time;
67 if (intermission_running) this.frame = this.anim_idle.x;
68 .entity weaponentity = this.weaponentity_fld;
69 if (this.owner.(weaponentity) != this)
71 // owner has new gun; remove old one
72 if (this.weaponchild) remove(this.weaponchild);
76 if (IS_DEAD(this.owner))
78 // owner died; disappear
80 if (this.weaponchild) this.weaponchild.model = "";
83 if (this.weaponname != this.owner.weaponname
84 || this.dmg != this.owner.modelindex
85 || this.deadflag != this.owner.deadflag)
87 // owner changed weapons; update appearance
88 this.weaponname = this.owner.weaponname;
89 this.dmg = this.owner.modelindex;
90 this.deadflag = this.owner.deadflag;
92 CL_WeaponEntity_SetModel(this, this.owner.weaponname, true);
95 this.alpha = -1; // TODO: don't render this entity at all
97 if (this.owner.alpha == default_player_alpha) this.m_alpha = default_weapon_alpha;
98 else if (this.owner.alpha != 0) this.m_alpha = this.owner.alpha;
99 else this.m_alpha = 1;
101 if (this.weaponchild)
103 this.weaponchild.alpha = this.alpha;
104 this.weaponchild.effects = this.effects;
108 void CL_ExteriorWeaponentity_Think(entity this)
110 this.nextthink = time;
111 if (this.owner.exteriorweaponentity != this)
116 if (IS_DEAD(this.owner))
121 if (this.weaponname != this.owner.weaponname || this.dmg != this.owner.modelindex
122 || this.deadflag != this.owner.deadflag)
124 this.weaponname = this.owner.weaponname;
125 this.dmg = this.owner.modelindex;
126 this.deadflag = this.owner.deadflag;
127 if (this.owner.weaponname != "")
129 _setmodel(this, W_Model(strcat("v_", this.owner.weaponname, ".md3")));
130 setsize(this, '0 0 0', '0 0 0');
132 else this.model = "";
135 if ((tag_found = gettagindex(this.owner, "tag_weapon")))
137 this.tag_index = tag_found;
138 this.tag_entity = this.owner;
142 setattachment(this, this.owner, "bip01 r hand");
145 this.effects = this.owner.effects;
146 this.effects |= EF_LOWPRECISION;
147 this.effects = this.effects & EFMASK_CHEAP; // eat performance
148 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
149 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
152 Weapon wep = PS(this.owner).m_weapon;
153 if (wep) this.glowmod = weaponentity_glowmod(wep, this.owner.clientcolors);
154 this.colormap = this.owner.colormap;
156 CSQCMODEL_AUTOUPDATE(this);
159 // spawning weaponentity for client
160 void CL_SpawnWeaponentity(entity actor, .entity weaponentity)
162 entity view = actor.(weaponentity) = new(weaponentity);
163 view.solid = SOLID_NOT;
165 setmodel(view, MDL_Null); // precision set when changed
166 setorigin(view, '0 0 0');
167 view.weaponentity_fld = weaponentity;
168 setthink(view, CL_Weaponentity_Think);
169 view.nextthink = time;
170 view.viewmodelforclient = actor;
171 setcefc(view, CL_Weaponentity_CustomizeEntityForClient);
173 if (weaponentity == weaponentities[0])
175 entity exterior = actor.exteriorweaponentity = new(exteriorweaponentity);
176 exterior.solid = SOLID_NOT;
177 exterior.owner = actor;
178 setorigin(exterior, '0 0 0');
179 setthink(exterior, CL_ExteriorWeaponentity_Think);
180 exterior.nextthink = time;
182 CSQCMODEL_AUTOINIT(exterior);
187 void w_clear(Weapon thiswep, entity actor, .entity weaponentity, int fire)
189 PS(actor).m_weapon = WEP_Null;
190 PS(actor).m_switchingweapon = WEP_Null;
191 entity this = actor.(weaponentity);
194 this.state = WS_CLEAR;
199 void w_ready(Weapon thiswep, entity actor, .entity weaponentity, int fire)
201 entity this = actor.(weaponentity);
202 if (this) this.state = WS_READY;
203 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, 1000000, w_ready);
208 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary)
210 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
212 if (secondary) ammo = thiswep.wr_checkammo2(thiswep, actor);
213 else ammo = thiswep.wr_checkammo1(thiswep, actor);
214 if (ammo) return true;
215 // always keep the Mine Layer if we placed mines, so that we can detonate them
216 if (thiswep == WEP_MINE_LAYER)
218 FOREACH_ENTITY_ENT(owner, actor,
220 if(it.classname != "mine") continue;
221 if(it.owner == actor) return false;
225 if (thiswep == WEP_SHOTGUN)
226 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false; // no clicking, just allow
228 if (thiswep == PS(actor).m_switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
230 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
231 actor.prevdryfire = time;
234 // check if the other firing mode has enough ammo
235 bool ammo_other = false;
236 if (secondary) ammo_other = thiswep.wr_checkammo1(thiswep, actor);
237 else ammo_other = thiswep.wr_checkammo2(thiswep, actor);
240 if (time - actor.prevwarntime > 1)
246 ITEM_WEAPON_PRIMORSEC,
252 actor.prevwarntime = time;
254 else // this weapon is totally unable to fire, switch to another one
256 W_SwitchToOtherWeapon(actor);
263 bool weapon_prepareattack_check(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
265 if (actor.weaponentity == NULL) return true;
266 if (!weapon_prepareattack_checkammo(thiswep, actor, secondary)) return false;
268 // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
269 // if all players readied up and the countdown is running
270 if (time < game_starttime || time < actor.race_penalty) return false;
272 if (timeout_status == TIMEOUT_ACTIVE) // don't allow the player to shoot while game is paused
275 // do not even think about shooting if switching
276 if (PS(actor).m_switchweapon != PS(actor).m_weapon) return false;
280 int slot = weaponslot(weaponentity);
281 // don't fire if previous attack is not finished
282 if (ATTACK_FINISHED(actor, slot) > time + actor.weapon_frametime * 0.5) return false;
283 entity this = actor.(weaponentity);
284 // don't fire while changing weapon
285 if (this.state != WS_READY) return false;
290 void weapon_prepareattack_do(entity actor, .entity weaponentity, bool secondary, float attacktime)
292 entity this = actor.(weaponentity);
293 if (this == NULL) return;
294 this.state = WS_INUSE;
296 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
298 // if the weapon hasn't been firing continuously, reset the timer
301 int slot = weaponslot(weaponentity);
302 if (ATTACK_FINISHED(actor, slot) < time - actor.weapon_frametime * 1.5)
304 ATTACK_FINISHED(actor, slot) = time;
305 // dprint("resetting attack finished to ", ftos(time), "\n");
307 ATTACK_FINISHED(actor, slot) = ATTACK_FINISHED(actor, slot) + attacktime * W_WeaponRateFactor(actor);
309 actor.bulletcounter += 1;
310 // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, slot)), "\n");
313 bool weapon_prepareattack(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
315 if (weapon_prepareattack_check(thiswep, actor, weaponentity, secondary, attacktime))
317 weapon_prepareattack_do(actor, weaponentity, secondary, attacktime);
323 void wframe_send(entity actor, entity weaponentity, vector a, bool restartanim);
326 * @param t defer thinking until time + t
327 * @param func next think function
329 void weapon_thinkf(entity actor, .entity weaponentity, WFRAME fr, float t, void(Weapon thiswep, entity actor,
330 .entity weaponentity, int fire) func)
332 entity this = actor.(weaponentity);
333 if (this == NULL) return;
335 if (fr == WFRAME_DONTCHANGE)
342 restartanim = fr != WFRAME_IDLE;
345 vector of = v_forward;
351 if (fr == WFRAME_IDLE) a = this.anim_idle;
352 else if (fr == WFRAME_FIRE1) a = this.anim_fire1;
353 else if (fr == WFRAME_FIRE2) a = this.anim_fire2;
354 else // if (fr == WFRAME_RELOAD)
355 a = this.anim_reload;
356 a.z *= g_weaponratefactor;
362 if (this.weapon_think == w_ready && func != w_ready && this.state == WS_RAISE) backtrace(
363 "Tried to override initial weapon think function - should this really happen?");
365 t *= W_WeaponRateFactor(actor);
367 // VorteX: haste can be added here
368 if (this.weapon_think == w_ready)
370 this.weapon_nextthink = time;
371 // dprint("started firing at ", ftos(time), "\n");
373 if (this.weapon_nextthink < time - actor.weapon_frametime * 1.5
374 || this.weapon_nextthink > time + actor.weapon_frametime * 1.5)
376 this.weapon_nextthink = time;
377 // dprint("reset weapon animation timer at ", ftos(time), "\n");
379 this.weapon_nextthink += t;
380 if (weaponentity == weaponentities[0]) STAT(WEAPON_NEXTTHINK, actor) = this.weapon_nextthink;
381 this.weapon_think = func;
382 // dprint("next ", ftos(this.weapon_nextthink), "\n");
386 FOREACH_CLIENT(true, LAMBDA(
387 if(it == actor || (IS_SPEC(it) && it.enemy == actor))
388 wframe_send(it, this, a, restartanim);
392 if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
394 int act = (fr == WFRAME_FIRE2 && (PS(actor).m_weapon == WEP_SHOCKWAVE || PS(actor).m_weapon == WEP_SHOTGUN))
398 animdecide_setaction(actor, act, restartanim);
400 else if (actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
402 actor.anim_upper_action = 0;
406 bool forbidWeaponUse(entity player)
408 if (time < game_starttime && !autocvar_sv_ready_restart_after_countdown) return true;
409 if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
410 if (player.player_blocked) return true;
411 if (STAT(FROZEN, player)) return true;
412 if (player.weapon_blocked) return true;
416 .bool hook_switchweapon;
418 void W_WeaponFrame(Player actor)
421 TC(PlayerState, PS(actor));
422 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
423 entity this = actor.(weaponentity);
424 if (frametime) actor.weapon_frametime = frametime;
426 if (!this || actor.health < 1) return; // Dead player can't use weapons and injure impulse commands
429 if (forbidWeaponUse(actor))
431 if (actor.(weaponentity).state != WS_CLEAR)
433 Weapon wpn = PS(actor).m_weapon;
434 w_ready(wpn, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
439 if (PS(actor).m_switchweapon == WEP_Null)
441 PS(actor).m_weapon = WEP_Null;
442 PS(actor).m_switchingweapon = WEP_Null;
443 this.state = WS_CLEAR;
444 actor.weaponname = "";
445 // actor.items &= ~IT_AMMO;
449 makevectors(actor.v_angle);
450 vector fo = v_forward; // save them in case the weapon think functions change it
455 if (PS(actor).m_weapon != PS(actor).m_switchweapon)
460 LOG_WARNINGF("unhandled weaponentity (%i) state for player (%i): %d\n", this, actor, this.state);
468 Weapon newwep = PS(actor).m_switchweapon;
469 PS(actor).m_switchingweapon = newwep;
471 // the two weapon entities will notice this has changed and update their models
472 PS(actor).m_weapon = newwep;
473 actor.weaponname = newwep.mdl;
474 actor.bulletcounter = 0;
475 actor.ammo_field = newwep.ammo_field;
476 newwep.wr_setup(newwep, actor);
477 this.state = WS_RAISE;
479 // set our clip load to the load of the weapon we switched to, if it's reloadable
480 if ((newwep.spawnflags & WEP_FLAG_RELOADABLE) && newwep.reloading_ammo) // prevent accessing undefined cvars
482 actor.clip_load = actor.(weapon_load[PS(actor).m_switchweapon.m_id]);
483 actor.clip_size = newwep.reloading_ammo;
487 actor.clip_load = actor.clip_size = 0;
490 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
495 // in dropping phase we can switch at any time
496 PS(actor).m_switchingweapon = PS(actor).m_switchweapon;
502 PS(actor).m_switchingweapon = PS(actor).m_switchweapon;
503 entity oldwep = PS(actor).m_weapon;
505 // set up weapon switch think in the future, and start drop anim
506 if (INDEPENDENT_ATTACK_FINISHED || ATTACK_FINISHED(actor, weaponslot(weaponentity)) <= time + actor.weapon_frametime * 0.5)
508 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
509 this.state = WS_DROP;
510 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
517 // LordHavoc: network timing test code
518 // if (actor.button0)
519 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor, slot)), " >= ", ftos(this.weapon_nextthink), "\n");
521 Weapon w = PS(actor).m_weapon;
523 // call the think code which may fire the weapon
524 // and do so multiple times to resolve framerate dependency issues if the
525 // server framerate is very low and the weapon fire rate very high
526 for (int c = 0; c < W_TICSPERFRAME; ++c)
528 if (w != WEP_Null && !(actor.weapons & WepSet_FromWeapon(w)))
530 if (PS(actor).m_weapon == PS(actor).m_switchweapon) W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
538 bool block_weapon = false;
540 bool key_pressed = PHYS_INPUT_BUTTON_HOOK(actor) && !actor.vehicle;
541 Weapon off = actor.offhand;
542 if (off && !(actor.weapons & WEPSET(HOOK)))
544 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
548 if (key_pressed && PS(actor).m_switchweapon != WEP_HOOK && !actor.hook_switchweapon)
549 W_SwitchWeapon(actor, WEP_HOOK);
550 actor.hook_switchweapon = key_pressed;
552 block_weapon = (PS(actor).m_weapon == h && (PHYS_INPUT_BUTTON_ATCK(actor) || key_pressed));
553 h.wr_think(h, actor, weaponentity, block_weapon ? 1 : 0);
563 Weapon e = PS(actor).m_weapon;
567 e.wr_think(e, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
571 e.wr_gonethink(e, actor);
575 if (time + actor.weapon_frametime * 0.5 >= this.weapon_nextthink)
577 if (this.weapon_think)
582 Weapon wpn = PS(actor).m_weapon;
583 this.weapon_think(wpn, actor, weaponentity,
584 PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
588 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
594 void W_AttachToShotorg(entity actor, entity flash, vector offset)
596 .entity weaponentity = weaponentities[0];
598 flash.angles_z = random() * 360;
600 entity view = actor.(weaponentity);
601 entity exterior = actor.exteriorweaponentity;
603 if (gettagindex(view, "shot")) setattachment(flash, view, "shot");
604 else setattachment(flash, view, "tag_shot");
605 setorigin(flash, offset);
607 entity xflash = spawn();
608 copyentity(flash, xflash);
610 flash.viewmodelforclient = actor;
612 if (view.oldorigin.x > 0)
614 setattachment(xflash, exterior, "");
615 setorigin(xflash, view.oldorigin + offset);
619 if (gettagindex(exterior, "shot")) setattachment(xflash, exterior, "shot");
620 else setattachment(xflash, exterior, "tag_shot");
621 setorigin(xflash, offset);
625 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
627 if (MUTATOR_CALLHOOK(W_DecreaseAmmo, actor)) return;
629 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo) return;
631 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
632 if (wep.reloading_ammo)
634 actor.clip_load -= ammo_use;
635 actor.(weapon_load[PS(actor).m_weapon.m_id]) = actor.clip_load;
637 else if (wep.ammo_field != ammo_none)
639 actor.(wep.ammo_field) -= ammo_use;
640 if (actor.(wep.ammo_field) < 0)
643 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
644 "Please notify Samual immediately with a copy of this backtrace!\n",
647 GetAmmoPicture(wep.ammo_field),
649 actor.(wep.ammo_field)
655 // weapon reloading code
657 .float reload_ammo_amount, reload_ammo_min, reload_time;
658 .float reload_complain;
659 .string reload_sound;
661 void W_ReloadedAndReady(Weapon thiswep, entity actor, .entity weaponentity, int fire)
663 // finish the reloading process, and do the ammo transfer
665 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
667 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
668 if (!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
670 actor.clip_load = actor.reload_ammo_amount;
674 // make sure we don't add more ammo than we have
675 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
676 actor.clip_load += load;
677 actor.(actor.ammo_field) -= load;
679 actor.(weapon_load[PS(actor).m_weapon.m_id]) = actor.clip_load;
681 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
682 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
683 // so your weapon is disabled for a few seconds without reason
685 // ATTACK_FINISHED(actor, slot) -= actor.reload_time - 1;
687 Weapon wpn = Weapons_from(PS(actor).m_weapon.m_id);
688 w_ready(wpn, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
691 void W_Reload(entity actor, float sent_ammo_min, Sound sent_sound)
693 TC(Sound, sent_sound);
694 .entity weaponentity = weaponentities[0];
695 // set global values to work with
696 Weapon e = PS(actor).m_weapon;
698 if (MUTATOR_CALLHOOK(W_Reload, actor)) return;
700 actor.reload_ammo_min = sent_ammo_min;
701 actor.reload_ammo_amount = e.reloading_ammo;
702 actor.reload_time = e.reloading_time;
703 if (actor.reload_sound) strunzone(actor.reload_sound);
704 actor.reload_sound = strzone(Sound_fixpath(sent_sound));
706 // don't reload weapons that don't have the RELOADABLE flag
707 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
710 "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
714 // return if reloading is disabled for this weapon
715 if (!actor.reload_ammo_amount) return;
717 // our weapon is fully loaded, no need to reload
718 if (actor.clip_load >= actor.reload_ammo_amount) return;
720 // no ammo, so nothing to load
721 if (actor.ammo_field != ammo_none)
723 if (!actor.(actor.ammo_field) && actor.reload_ammo_min)
725 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
727 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
729 play2(actor, SND(UNAVAILABLE));
730 sprint(actor, strcat("You don't have enough ammo to reload the ^2", PS(actor).m_weapon.m_name, "\n"));
731 actor.reload_complain = time + 1;
733 // switch away if the amount of ammo is not enough to keep using this weapon
734 Weapon w = PS(actor).m_weapon;
735 if (!(w.wr_checkammo1(w, actor) + w.wr_checkammo2(w, actor)))
737 actor.clip_load = -1; // reload later
738 W_SwitchToOtherWeapon(actor);
745 entity this = actor.(weaponentity);
748 if (this.wframe == WFRAME_RELOAD) return;
750 // allow switching away while reloading, but this will cause a new reload!
751 this.state = WS_READY;
754 // now begin the reloading process
756 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
758 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
759 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
760 // so your weapon is disabled for a few seconds without reason
762 // ATTACK_FINISHED(actor, slot) = max(time, ATTACK_FINISHED(actor, slot)) + actor.reload_time + 1;
764 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
766 if (actor.clip_load < 0) actor.clip_load = 0;
767 actor.old_clip_load = actor.clip_load;
768 actor.clip_load = actor.(weapon_load[PS(actor).m_weapon.m_id]) = -1;
771 void W_DropEvent(.void(Weapon, entity actor) event, entity player, float weapon_type, entity weapon_item)
773 Weapon w = Weapons_from(weapon_type);
774 weapon_dropevent_item = weapon_item;