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()
24 float t = 1.0 / g_weaponratefactor;
26 MUTATOR_CALLHOOK(WeaponRateFactor, t);
32 float W_WeaponSpeedFactor()
34 float t = 1.0 * g_weaponspeedfactor;
36 MUTATOR_CALLHOOK(WeaponSpeedFactor, t);
43 bool CL_Weaponentity_CustomizeEntityForClient()
46 this.viewmodelforclient = this.owner;
47 if (IS_SPEC(other) && other.enemy == this.owner) this.viewmodelforclient = other;
51 vector CL_Weapon_GetShotOrg(int wpn)
53 entity wi = Weapons_from(wpn);
55 CL_WeaponEntity_SetModel(e, wi.mdl);
56 vector ret = e.movedir;
57 CL_WeaponEntity_SetModel(e, "");
62 ..entity weaponentity_fld;
65 void CL_Weaponentity_Think()
68 this.nextthink = time;
69 if (intermission_running) this.frame = this.anim_idle.x;
70 .entity weaponentity = this.weaponentity_fld;
71 if (this.owner.(weaponentity) != this)
73 // owner has new gun; remove self
74 if (this.weaponchild) remove(this.weaponchild);
78 if (IS_DEAD(self.owner))
80 // owner died; disappear
82 if (this.weaponchild) this.weaponchild.model = "";
85 if (this.weaponname != this.owner.weaponname
86 || this.dmg != this.owner.modelindex
87 || this.deadflag != this.owner.deadflag)
89 // owner changed weapons; update appearance
90 this.weaponname = this.owner.weaponname;
91 this.dmg = this.owner.modelindex;
92 this.deadflag = this.owner.deadflag;
94 CL_WeaponEntity_SetModel(this, this.owner.weaponname);
97 this.alpha = -1; // TODO: don't render this entity at all
99 if (this.owner.alpha == default_player_alpha) this.m_alpha = default_weapon_alpha;
100 else if (this.owner.alpha != 0) this.m_alpha = this.owner.alpha;
101 else this.m_alpha = 1;
103 if (this.weaponchild)
105 this.weaponchild.alpha = this.alpha;
106 this.weaponchild.effects = this.effects;
110 void CL_ExteriorWeaponentity_Think()
113 this.nextthink = time;
114 if (this.owner.exteriorweaponentity != this)
119 if (IS_DEAD(self.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 this.glowmod = weaponentity_glowmod(PS(this.owner).m_weapon, this.owner.clientcolors);
156 this.colormap = this.owner.colormap;
158 CSQCMODEL_AUTOUPDATE(this);
161 // spawning weaponentity for client
162 void CL_SpawnWeaponentity(entity actor, .entity weaponentity)
164 entity view = actor.(weaponentity) = new(weaponentity);
165 view.solid = SOLID_NOT;
167 setmodel(view, MDL_Null); // precision set when changed
168 setorigin(view, '0 0 0');
169 view.weaponentity_fld = weaponentity;
170 view.think = CL_Weaponentity_Think;
171 view.nextthink = time;
172 view.viewmodelforclient = actor;
173 view.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
175 if (weaponentity == weaponentities[0])
177 entity exterior = actor.exteriorweaponentity = new(exteriorweaponentity);
178 exterior.solid = SOLID_NOT;
179 exterior.owner = actor;
180 setorigin(exterior, '0 0 0');
181 exterior.think = CL_ExteriorWeaponentity_Think;
182 exterior.nextthink = time;
184 CSQCMODEL_AUTOINIT(exterior);
189 void w_clear(Weapon thiswep, entity actor, .entity weaponentity, int fire)
191 PS(actor).m_weapon = WEP_Null;
192 PS(actor).m_switchingweapon = WEP_Null;
193 entity this = actor.(weaponentity);
196 this.state = WS_CLEAR;
201 void w_ready(Weapon thiswep, entity actor, .entity weaponentity, int fire)
203 entity this = actor.(weaponentity);
204 if (this) this.state = WS_READY;
205 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, 1000000, w_ready);
210 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary)
212 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
214 if (secondary) WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
215 else WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
216 if (ammo) return true;
217 // always keep the Mine Layer if we placed mines, so that we can detonate them
218 if (thiswep == WEP_MINE_LAYER)
219 for (entity mine; (mine = find(mine, classname, "mine")); )
220 if (mine.owner == actor) return false;
222 if (thiswep == WEP_SHOTGUN)
223 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false; // no clicking, just allow
225 if (thiswep == PS(actor).m_switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
227 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
228 actor.prevdryfire = time;
231 // check if the other firing mode has enough ammo
232 bool ammo_other = false;
233 if (secondary) WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo1(thiswep));
234 else WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo2(thiswep));
237 if (time - actor.prevwarntime > 1)
243 ITEM_WEAPON_PRIMORSEC,
249 actor.prevwarntime = time;
251 else // this weapon is totally unable to fire, switch to another one
253 W_SwitchToOtherWeapon(actor);
260 bool weapon_prepareattack_check(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
262 if (actor.weaponentity == NULL) return true;
263 if (!weapon_prepareattack_checkammo(thiswep, actor, secondary)) return false;
265 // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
266 // if all players readied up and the countdown is running
267 if (time < game_starttime || time < actor.race_penalty) return false;
269 if (timeout_status == TIMEOUT_ACTIVE) // don't allow the player to shoot while game is paused
272 // do not even think about shooting if switching
273 if (PS(actor).m_switchweapon != PS(actor).m_weapon) return false;
277 int slot = weaponslot(weaponentity);
278 // don't fire if previous attack is not finished
279 if (ATTACK_FINISHED(actor, slot) > time + actor.weapon_frametime * 0.5) return false;
280 entity this = actor.(weaponentity);
281 // don't fire while changing weapon
282 if (this.state != WS_READY) return false;
287 void weapon_prepareattack_do(entity actor, .entity weaponentity, bool secondary, float attacktime)
289 entity this = actor.(weaponentity);
290 if (this == NULL) return;
291 this.state = WS_INUSE;
293 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
295 // if the weapon hasn't been firing continuously, reset the timer
298 int slot = weaponslot(weaponentity);
299 if (ATTACK_FINISHED(actor, slot) < time - actor.weapon_frametime * 1.5)
301 ATTACK_FINISHED(actor, slot) = time;
302 // dprint("resetting attack finished to ", ftos(time), "\n");
304 ATTACK_FINISHED(actor, slot) = ATTACK_FINISHED(actor, slot) + attacktime * W_WeaponRateFactor();
306 actor.bulletcounter += 1;
307 // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, slot)), "\n");
310 bool weapon_prepareattack(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
312 if (weapon_prepareattack_check(thiswep, actor, weaponentity, secondary, attacktime))
314 weapon_prepareattack_do(actor, weaponentity, secondary, attacktime);
320 void wframe_send(entity actor, entity weaponentity, vector a, bool restartanim);
323 * @param t defer thinking until time + t
324 * @param func next think function
326 void weapon_thinkf(entity actor, .entity weaponentity, WFRAME fr, float t, void(Weapon thiswep, entity actor,
327 .entity weaponentity, int fire) func)
329 entity this = actor.(weaponentity);
330 if (this == NULL) return;
332 if (fr == WFRAME_DONTCHANGE)
339 restartanim = fr != WFRAME_IDLE;
342 vector of = v_forward;
348 if (fr == WFRAME_IDLE) a = this.anim_idle;
349 else if (fr == WFRAME_FIRE1) a = this.anim_fire1;
350 else if (fr == WFRAME_FIRE2) a = this.anim_fire2;
351 else // if (fr == WFRAME_RELOAD)
352 a = this.anim_reload;
353 a.z *= g_weaponratefactor;
359 if (this.weapon_think == w_ready && func != w_ready && this.state == WS_RAISE) backtrace(
360 "Tried to override initial weapon think function - should this really happen?");
362 t *= W_WeaponRateFactor();
364 // VorteX: haste can be added here
365 if (this.weapon_think == w_ready)
367 this.weapon_nextthink = time;
368 // dprint("started firing at ", ftos(time), "\n");
370 if (this.weapon_nextthink < time - actor.weapon_frametime * 1.5
371 || this.weapon_nextthink > time + actor.weapon_frametime * 1.5)
373 this.weapon_nextthink = time;
374 // dprint("reset weapon animation timer at ", ftos(time), "\n");
376 this.weapon_nextthink += t;
377 if (weaponentity == weaponentities[0]) STAT(WEAPON_NEXTTHINK, actor) = this.weapon_nextthink;
378 this.weapon_think = func;
379 // dprint("next ", ftos(this.weapon_nextthink), "\n");
383 FOREACH_CLIENT(true, LAMBDA(
384 if(it == actor || (IS_SPEC(it) && it.enemy == actor))
385 wframe_send(it, this, a, restartanim);
389 if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
391 int act = (fr == WFRAME_FIRE2 && (PS(actor).m_weapon == WEP_SHOCKWAVE || PS(actor).m_weapon == WEP_SHOTGUN))
395 animdecide_setaction(actor, act, restartanim);
397 else if (actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
399 actor.anim_upper_action = 0;
403 bool forbidWeaponUse(entity player)
405 if (time < game_starttime && !autocvar_sv_ready_restart_after_countdown) return true;
406 if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
407 if (player.player_blocked) return true;
408 if (STAT(FROZEN, player)) return true;
409 if (player.weapon_blocked) return true;
413 .bool hook_switchweapon;
415 void W_WeaponFrame(entity actor)
417 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
418 entity this = actor.(weaponentity);
419 if (frametime) actor.weapon_frametime = frametime;
421 if (!this || actor.health < 1) return; // Dead player can't use weapons and injure impulse commands
424 if (forbidWeaponUse(actor))
426 if (actor.(weaponentity).state != WS_CLEAR)
428 Weapon wpn = PS(actor).m_weapon;
429 w_ready(wpn, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
434 if (PS(actor).m_switchweapon == WEP_Null)
436 PS(actor).m_weapon = WEP_Null;
437 PS(actor).m_switchingweapon = WEP_Null;
438 this.state = WS_CLEAR;
439 actor.weaponname = "";
440 // actor.items &= ~IT_AMMO;
444 makevectors(actor.v_angle);
445 vector fo = v_forward; // save them in case the weapon think functions change it
450 if (PS(actor).m_weapon != PS(actor).m_switchweapon)
455 LOG_WARNINGF("unhandled weaponentity (%i) state for player (%i): %d\n", this, actor, this.state);
463 Weapon newwep = PS(actor).m_switchweapon;
464 PS(actor).m_switchingweapon = newwep;
466 // the two weapon entities will notice this has changed and update their models
467 PS(actor).m_weapon = newwep;
468 actor.weaponname = newwep.mdl;
469 actor.bulletcounter = 0;
470 actor.ammo_field = newwep.ammo_field;
471 newwep.wr_setup(newwep);
472 this.state = WS_RAISE;
474 // set our clip load to the load of the weapon we switched to, if it's reloadable
475 if ((newwep.spawnflags & WEP_FLAG_RELOADABLE) && newwep.reloading_ammo) // prevent accessing undefined cvars
477 actor.clip_load = actor.(weapon_load[PS(actor).m_switchweapon.m_id]);
478 actor.clip_size = newwep.reloading_ammo;
482 actor.clip_load = actor.clip_size = 0;
485 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
490 // in dropping phase we can switch at any time
491 PS(actor).m_switchingweapon = PS(actor).m_switchweapon;
497 PS(actor).m_switchingweapon = PS(actor).m_switchweapon;
498 entity oldwep = PS(actor).m_weapon;
500 // set up weapon switch think in the future, and start drop anim
501 if (INDEPENDENT_ATTACK_FINISHED || ATTACK_FINISHED(actor, weaponslot(weaponentity)) <= time + actor.weapon_frametime * 0.5)
503 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
504 this.state = WS_DROP;
505 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
512 // LordHavoc: network timing test code
513 // if (actor.button0)
514 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor, slot)), " >= ", ftos(this.weapon_nextthink), "\n");
516 Weapon w = PS(actor).m_weapon;
518 // call the think code which may fire the weapon
519 // and do so multiple times to resolve framerate dependency issues if the
520 // server framerate is very low and the weapon fire rate very high
521 for (int c = 0; c < W_TICSPERFRAME; ++c)
523 if (w != WEP_Null && !(actor.weapons & WepSet_FromWeapon(w)))
525 if (PS(actor).m_weapon == PS(actor).m_switchweapon) W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
533 bool block_weapon = false;
535 bool key_pressed = PHYS_INPUT_BUTTON_HOOK(actor) && !actor.vehicle;
536 Weapon off = actor.offhand;
537 if (off && !(actor.weapons & WEPSET(HOOK)))
539 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
543 if (key_pressed && PS(actor).m_switchweapon != WEP_HOOK && !actor.hook_switchweapon)
544 W_SwitchWeapon(WEP_HOOK);
545 actor.hook_switchweapon = key_pressed;
547 block_weapon = (PS(actor).m_weapon == h && (PHYS_INPUT_BUTTON_ATCK(actor) || key_pressed));
548 h.wr_think(h, actor, weaponentity, block_weapon ? 1 : 0);
558 Weapon e = PS(actor).m_weapon;
561 e.wr_think(e, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
569 if (time + actor.weapon_frametime * 0.5 >= this.weapon_nextthink)
571 if (this.weapon_think)
576 Weapon wpn = PS(actor).m_weapon;
577 this.weapon_think(wpn, actor, weaponentity,
578 PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
582 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
588 void W_AttachToShotorg(entity actor, entity flash, vector offset)
590 .entity weaponentity = weaponentities[0];
592 flash.angles_z = random() * 360;
594 entity view = actor.(weaponentity);
595 entity exterior = actor.exteriorweaponentity;
597 if (gettagindex(view, "shot")) setattachment(flash, view, "shot");
598 else setattachment(flash, view, "tag_shot");
599 setorigin(flash, offset);
601 entity xflash = spawn();
602 copyentity(flash, xflash);
604 flash.viewmodelforclient = actor;
606 if (view.oldorigin.x > 0)
608 setattachment(xflash, exterior, "");
609 setorigin(xflash, view.oldorigin + offset);
613 if (gettagindex(exterior, "shot")) setattachment(xflash, exterior, "shot");
614 else setattachment(xflash, exterior, "tag_shot");
615 setorigin(xflash, offset);
619 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
621 if (MUTATOR_CALLHOOK(W_DecreaseAmmo, actor)) return;
623 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo) return;
625 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
626 if (wep.reloading_ammo)
628 actor.clip_load -= ammo_use;
629 actor.(weapon_load[PS(actor).m_weapon.m_id]) = actor.clip_load;
631 else if (wep.ammo_field != ammo_none)
633 actor.(wep.ammo_field) -= ammo_use;
634 if (actor.(wep.ammo_field) < 0)
637 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
638 "Please notify Samual immediately with a copy of this backtrace!\n",
641 GetAmmoPicture(wep.ammo_field),
643 actor.(wep.ammo_field)
649 // weapon reloading code
651 .float reload_ammo_amount, reload_ammo_min, reload_time;
652 .float reload_complain;
653 .string reload_sound;
655 void W_ReloadedAndReady(Weapon thiswep, entity actor, .entity weaponentity, int fire)
657 // finish the reloading process, and do the ammo transfer
659 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
661 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
662 if (!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
664 actor.clip_load = actor.reload_ammo_amount;
668 // make sure we don't add more ammo than we have
669 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
670 actor.clip_load += load;
671 actor.(actor.ammo_field) -= load;
673 actor.(weapon_load[PS(actor).m_weapon.m_id]) = actor.clip_load;
675 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
676 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
677 // so your weapon is disabled for a few seconds without reason
679 // ATTACK_FINISHED(actor, slot) -= actor.reload_time - 1;
681 Weapon wpn = Weapons_from(PS(actor).m_weapon.m_id);
682 w_ready(wpn, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
685 void W_Reload(entity actor, float sent_ammo_min, string sent_sound)
687 .entity weaponentity = weaponentities[0];
688 // set global values to work with
689 Weapon e = PS(actor).m_weapon;
691 if (MUTATOR_CALLHOOK(W_Reload, actor)) return;
693 actor.reload_ammo_min = sent_ammo_min;
694 actor.reload_ammo_amount = e.reloading_ammo;
695 actor.reload_time = e.reloading_time;
696 actor.reload_sound = sent_sound;
698 // don't reload weapons that don't have the RELOADABLE flag
699 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
702 "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
706 // return if reloading is disabled for this weapon
707 if (!actor.reload_ammo_amount) return;
709 // our weapon is fully loaded, no need to reload
710 if (actor.clip_load >= actor.reload_ammo_amount) return;
712 // no ammo, so nothing to load
713 if (actor.ammo_field != ammo_none)
715 if (!actor.(actor.ammo_field) && actor.reload_ammo_min)
717 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
719 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
721 play2(actor, SND(UNAVAILABLE));
722 sprint(actor, strcat("You don't have enough ammo to reload the ^2", PS(actor).m_weapon.m_name, "\n"));
723 actor.reload_complain = time + 1;
725 // switch away if the amount of ammo is not enough to keep using this weapon
726 Weapon w = PS(actor).m_weapon;
727 if (!(w.wr_checkammo1(w) + w.wr_checkammo2(w)))
729 actor.clip_load = -1; // reload later
730 W_SwitchToOtherWeapon(actor);
737 entity this = actor.(weaponentity);
740 if (this.wframe == WFRAME_RELOAD) return;
742 // allow switching away while reloading, but this will cause a new reload!
743 this.state = WS_READY;
746 // now begin the reloading process
748 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
750 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
751 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
752 // so your weapon is disabled for a few seconds without reason
754 // ATTACK_FINISHED(actor, slot) = max(time, ATTACK_FINISHED(actor, slot)) + actor.reload_time + 1;
756 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
758 if (actor.clip_load < 0) actor.clip_load = 0;
759 actor.old_clip_load = actor.clip_load;
760 actor.clip_load = actor.(weapon_load[PS(actor).m_weapon.m_id]) = -1;
763 void W_DropEvent(.void(Weapon) event, entity player, float weapon_type, entity weapon_item)
765 Weapon w = Weapons_from(weapon_type);
766 weapon_dropevent_item = weapon_item;
767 WITH(entity, self, player, w.event(w));