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.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 bool CL_Weaponentity_CustomizeEntityForClient()
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);
55 vector ret = e.movedir;
56 CL_WeaponEntity_SetModel(e, "");
61 ..entity weaponentity_fld;
64 void CL_Weaponentity_Think()
67 this.nextthink = time;
68 if (intermission_running) this.frame = this.anim_idle.x;
69 .entity weaponentity = this.weaponentity_fld;
70 if (this.owner.(weaponentity) != 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 this.alpha = -1; // TODO: don't render this entity at all
98 if (this.owner.alpha == default_player_alpha) this.m_alpha = default_weapon_alpha;
99 else if (this.owner.alpha != 0) this.m_alpha = this.owner.alpha;
100 else this.m_alpha = 1;
102 if (this.weaponchild)
104 this.weaponchild.alpha = this.alpha;
105 this.weaponchild.effects = this.effects;
109 void CL_ExteriorWeaponentity_Think()
112 this.nextthink = time;
113 if (this.owner.exteriorweaponentity != this)
118 if (this.owner.deadflag != DEAD_NO)
123 if (this.weaponname != this.owner.weaponname || this.dmg != this.owner.modelindex
124 || this.deadflag != this.owner.deadflag)
126 this.weaponname = this.owner.weaponname;
127 this.dmg = this.owner.modelindex;
128 this.deadflag = this.owner.deadflag;
129 if (this.owner.weaponname != "") _setmodel(this, W_Model(strcat("v_", this.owner.weaponname, ".md3")));
130 else this.model = "";
133 if ((tag_found = gettagindex(this.owner, "tag_weapon")))
135 this.tag_index = tag_found;
136 this.tag_entity = this.owner;
140 setattachment(this, this.owner, "bip01 r hand");
143 this.effects = this.owner.effects;
144 this.effects |= EF_LOWPRECISION;
145 this.effects = this.effects & EFMASK_CHEAP; // eat performance
146 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
147 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
150 this.glowmod = this.owner.weaponentity_glowmod;
151 this.colormap = this.owner.colormap;
153 CSQCMODEL_AUTOUPDATE(this);
156 // spawning weaponentity for client
157 void CL_SpawnWeaponentity(entity actor, .entity weaponentity)
159 entity view = actor.(weaponentity) = new(weaponentity);
161 view.solid = SOLID_NOT;
163 setmodel(view, MDL_Null); // precision set when changed
164 setorigin(view, '0 0 0');
165 view.weaponentity_fld = weaponentity;
166 view.think = CL_Weaponentity_Think;
167 view.nextthink = time;
168 view.viewmodelforclient = actor;
169 view.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
171 if (weaponentity == weaponentities[0])
173 entity exterior = actor.exteriorweaponentity = new(exteriorweaponentity);
175 exterior.solid = SOLID_NOT;
176 exterior.owner = actor;
177 setorigin(exterior, '0 0 0');
178 exterior.think = CL_ExteriorWeaponentity_Think;
179 exterior.nextthink = time;
181 CSQCMODEL_AUTOINIT(exterior);
186 void w_clear(Weapon thiswep, entity actor, .entity weaponentity, int fire)
188 PS(actor).m_weapon = WEP_Null;
189 PS(actor).m_switchingweapon = WEP_Null;
190 entity this = actor.(weaponentity);
193 this.state = WS_CLEAR;
198 void w_ready(Weapon thiswep, entity actor, .entity weaponentity, int fire)
200 entity this = actor.(weaponentity);
201 if (this) this.state = WS_READY;
202 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, 1000000, w_ready);
207 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary)
209 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
211 if (secondary) WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
212 else WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
213 if (ammo) return true;
214 // always keep the Mine Layer if we placed mines, so that we can detonate them
215 if (thiswep == WEP_MINE_LAYER)
216 for (entity mine; (mine = find(mine, classname, "mine")); )
217 if (mine.owner == actor) return false;
219 if (thiswep == WEP_SHOTGUN)
220 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false; // no clicking, just allow
222 if (thiswep == PS(actor).m_switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
224 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
225 actor.prevdryfire = time;
228 // check if the other firing mode has enough ammo
229 bool ammo_other = false;
230 if (secondary) WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo1(thiswep));
231 else WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo2(thiswep));
234 if (time - actor.prevwarntime > 1)
240 ITEM_WEAPON_PRIMORSEC,
246 actor.prevwarntime = time;
248 else // this weapon is totally unable to fire, switch to another one
250 W_SwitchToOtherWeapon(actor);
257 bool weapon_prepareattack_check(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
259 if (actor.weaponentity == NULL) return true;
260 if (!weapon_prepareattack_checkammo(thiswep, actor, secondary)) return false;
262 // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
263 // if all players readied up and the countdown is running
264 if (time < game_starttime || time < actor.race_penalty) return false;
266 if (timeout_status == TIMEOUT_ACTIVE) // don't allow the player to shoot while game is paused
269 // do not even think about shooting if switching
270 if (PS(actor).m_switchweapon != PS(actor).m_weapon) return false;
274 int slot = weaponslot(weaponentity);
275 // don't fire if previous attack is not finished
276 if (ATTACK_FINISHED(actor, slot) > time + actor.weapon_frametime * 0.5) return false;
277 entity this = actor.(weaponentity);
278 // don't fire while changing weapon
279 if (this.state != WS_READY) return false;
284 void weapon_prepareattack_do(entity actor, .entity weaponentity, bool secondary, float attacktime)
286 entity this = actor.(weaponentity);
287 if (this == NULL) return;
288 this.state = WS_INUSE;
290 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
292 // if the weapon hasn't been firing continuously, reset the timer
295 int slot = weaponslot(weaponentity);
296 if (ATTACK_FINISHED(actor, slot) < time - actor.weapon_frametime * 1.5)
298 ATTACK_FINISHED(actor, slot) = time;
299 // dprint("resetting attack finished to ", ftos(time), "\n");
301 ATTACK_FINISHED(actor, slot) = ATTACK_FINISHED(actor, slot) + attacktime * W_WeaponRateFactor();
303 actor.bulletcounter += 1;
304 // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, slot)), "\n");
307 bool weapon_prepareattack(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
309 if (weapon_prepareattack_check(thiswep, actor, weaponentity, secondary, attacktime))
311 weapon_prepareattack_do(actor, weaponentity, secondary, attacktime);
317 void wframe_send(entity actor, entity weaponentity, vector a, bool restartanim);
320 * @param t defer thinking until time + t
321 * @param func next think function
323 void weapon_thinkf(entity actor, .entity weaponentity, WFRAME fr, float t, void(Weapon thiswep, entity actor,
324 .entity weaponentity, int fire) func)
326 entity this = actor.(weaponentity);
327 if (this == NULL) return;
329 if (fr == WFRAME_DONTCHANGE)
336 restartanim = fr != WFRAME_IDLE;
339 vector of = v_forward;
345 if (fr == WFRAME_IDLE) a = this.anim_idle;
346 else if (fr == WFRAME_FIRE1) a = this.anim_fire1;
347 else if (fr == WFRAME_FIRE2) a = this.anim_fire2;
348 else // if (fr == WFRAME_RELOAD)
349 a = this.anim_reload;
350 a.z *= g_weaponratefactor;
356 if (this.weapon_think == w_ready && func != w_ready && this.state == WS_RAISE) backtrace(
357 "Tried to override initial weapon think function - should this really happen?");
359 t *= W_WeaponRateFactor();
361 // VorteX: haste can be added here
362 if (this.weapon_think == w_ready)
364 this.weapon_nextthink = time;
365 // dprint("started firing at ", ftos(time), "\n");
367 if (this.weapon_nextthink < time - actor.weapon_frametime * 1.5
368 || this.weapon_nextthink > time + actor.weapon_frametime * 1.5)
370 this.weapon_nextthink = time;
371 // dprint("reset weapon animation timer at ", ftos(time), "\n");
373 this.weapon_nextthink += t;
374 if (weaponentity == weaponentities[0]) STAT(WEAPON_NEXTTHINK, actor) = this.weapon_nextthink;
375 this.weapon_think = func;
376 // dprint("next ", ftos(this.weapon_nextthink), "\n");
380 FOREACH_CLIENT(true, LAMBDA(
381 if(it == actor || (IS_SPEC(it) && it.enemy == actor))
382 wframe_send(it, this, a, restartanim);
386 if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
388 int act = (fr == WFRAME_FIRE2 && (PS(actor).m_weapon == WEP_SHOCKWAVE || PS(actor).m_weapon == WEP_SHOTGUN))
392 animdecide_setaction(actor, act, restartanim);
394 else if (actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
396 actor.anim_upper_action = 0;
400 bool forbidWeaponUse(entity player)
402 if (time < game_starttime && !autocvar_sv_ready_restart_after_countdown) return true;
403 if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
404 if (player.player_blocked) return true;
405 if (player.frozen) return true;
406 if (player.weapon_blocked) return true;
410 .bool hook_switchweapon;
412 void W_WeaponFrame(entity actor)
414 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
415 entity this = actor.(weaponentity);
416 if (frametime) actor.weapon_frametime = frametime;
418 if (!this || actor.health < 1) return; // Dead player can't use weapons and injure impulse commands
421 if (forbidWeaponUse(actor))
423 if (actor.(weaponentity).state != WS_CLEAR)
425 Weapon wpn = PS(actor).m_weapon;
426 w_ready(wpn, actor, weaponentity, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
431 if (PS(actor).m_switchweapon == WEP_Null)
433 PS(actor).m_weapon = WEP_Null;
434 PS(actor).m_switchingweapon = WEP_Null;
435 this.state = WS_CLEAR;
436 actor.weaponname = "";
437 // actor.items &= ~IT_AMMO;
441 makevectors(actor.v_angle);
442 vector fo = v_forward; // save them in case the weapon think functions change it
447 if (PS(actor).m_weapon != PS(actor).m_switchweapon)
452 LOG_WARNINGF("unhandled weaponentity (%i) state for player (%i): %d\n", this, actor, this.state);
460 Weapon newwep = PS(actor).m_switchweapon;
461 PS(actor).m_switchingweapon = newwep;
463 // the two weapon entities will notice this has changed and update their models
464 PS(actor).m_weapon = newwep;
465 actor.weaponname = newwep.mdl;
466 actor.bulletcounter = 0;
467 actor.ammo_field = newwep.ammo_field;
468 newwep.wr_setup(newwep);
469 this.state = WS_RAISE;
471 // set our clip load to the load of the weapon we switched to, if it's reloadable
472 if ((newwep.spawnflags & WEP_FLAG_RELOADABLE) && newwep.reloading_ammo) // prevent accessing undefined cvars
474 actor.clip_load = actor.(weapon_load[PS(actor).m_switchweapon.m_id]);
475 actor.clip_size = newwep.reloading_ammo;
479 actor.clip_load = actor.clip_size = 0;
482 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
487 // in dropping phase we can switch at any time
488 PS(actor).m_switchingweapon = PS(actor).m_switchweapon;
494 PS(actor).m_switchingweapon = PS(actor).m_switchweapon;
495 entity oldwep = PS(actor).m_weapon;
497 // set up weapon switch think in the future, and start drop anim
498 if (INDEPENDENT_ATTACK_FINISHED || ATTACK_FINISHED(actor, weaponslot(weaponentity)) <= time + actor.weapon_frametime * 0.5)
500 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
501 this.state = WS_DROP;
502 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
509 // LordHavoc: network timing test code
510 // if (actor.button0)
511 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor, slot)), " >= ", ftos(this.weapon_nextthink), "\n");
513 int w = PS(actor).m_weapon.m_id;
515 // call the think code which may fire the weapon
516 // and do so multiple times to resolve framerate dependency issues if the
517 // server framerate is very low and the weapon fire rate very high
518 for (int c = 0; c < W_TICSPERFRAME; ++c)
520 if (w && !(actor.weapons & WepSet_FromWeapon(Weapons_from(w))))
522 if (PS(actor).m_weapon == PS(actor).m_switchweapon) W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
530 bool block_weapon = false;
532 bool key_pressed = actor.BUTTON_HOOK && !actor.vehicle;
533 Weapon off = actor.offhand;
534 if (off && !(actor.weapons & WEPSET(HOOK)))
536 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
540 if (key_pressed && PS(actor).m_switchweapon != WEP_HOOK && !actor.hook_switchweapon)
541 W_SwitchWeapon(WEP_HOOK);
542 actor.hook_switchweapon = key_pressed;
544 block_weapon = (PS(actor).m_weapon == h && (actor.BUTTON_ATCK || key_pressed));
545 h.wr_think(h, actor, weaponentity, block_weapon ? 1 : 0);
557 Weapon e = PS(actor).m_weapon;
558 e.wr_think(e, actor, weaponentity, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
562 Weapon w = PS(actor).m_weapon;
567 if (time + actor.weapon_frametime * 0.5 >= this.weapon_nextthink)
569 if (this.weapon_think)
574 Weapon wpn = PS(actor).m_weapon;
575 this.weapon_think(wpn, actor, weaponentity,
576 (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
580 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
586 void W_AttachToShotorg(entity actor, entity flash, vector offset)
588 .entity weaponentity = weaponentities[0];
590 flash.angles_z = random() * 360;
592 entity view = actor.(weaponentity);
593 entity exterior = actor.exteriorweaponentity;
595 if (gettagindex(view, "shot")) setattachment(flash, view, "shot");
596 else setattachment(flash, view, "tag_shot");
597 setorigin(flash, offset);
599 entity xflash = spawn();
600 copyentity(flash, xflash);
602 flash.viewmodelforclient = actor;
604 if (view.oldorigin.x > 0)
606 setattachment(xflash, exterior, "");
607 setorigin(xflash, view.oldorigin + offset);
611 if (gettagindex(exterior, "shot")) setattachment(xflash, exterior, "shot");
612 else setattachment(xflash, exterior, "tag_shot");
613 setorigin(xflash, offset);
617 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
619 if (MUTATOR_CALLHOOK(W_DecreaseAmmo, actor)) return;
621 if ((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo) return;
623 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
624 if (wep.reloading_ammo)
626 actor.clip_load -= ammo_use;
627 actor.(weapon_load[PS(actor).m_weapon.m_id]) = actor.clip_load;
629 else if (wep.ammo_field != ammo_none)
631 actor.(wep.ammo_field) -= ammo_use;
632 if (actor.(wep.ammo_field) < 0)
635 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
636 "Please notify Samual immediately with a copy of this backtrace!\n",
639 GetAmmoPicture(wep.ammo_field),
641 actor.(wep.ammo_field)
647 // weapon reloading code
649 .float reload_ammo_amount, reload_ammo_min, reload_time;
650 .float reload_complain;
651 .string reload_sound;
653 void W_ReloadedAndReady(Weapon thiswep, entity actor, .entity weaponentity, int fire)
655 // finish the reloading process, and do the ammo transfer
657 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
659 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
660 if (!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
662 actor.clip_load = actor.reload_ammo_amount;
666 // make sure we don't add more ammo than we have
667 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
668 actor.clip_load += load;
669 actor.(actor.ammo_field) -= load;
671 actor.(weapon_load[PS(actor).m_weapon.m_id]) = actor.clip_load;
673 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
674 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
675 // so your weapon is disabled for a few seconds without reason
677 // ATTACK_FINISHED(actor, slot) -= actor.reload_time - 1;
679 Weapon wpn = Weapons_from(PS(actor).m_weapon.m_id);
680 w_ready(wpn, actor, weaponentity, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
683 void W_Reload(entity actor, float sent_ammo_min, string sent_sound)
685 .entity weaponentity = weaponentities[0];
686 // set global values to work with
687 Weapon e = PS(actor).m_weapon;
689 if (MUTATOR_CALLHOOK(W_Reload, actor)) return;
691 actor.reload_ammo_min = sent_ammo_min;
692 actor.reload_ammo_amount = e.reloading_ammo;
693 actor.reload_time = e.reloading_time;
694 actor.reload_sound = sent_sound;
696 // don't reload weapons that don't have the RELOADABLE flag
697 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
700 "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
704 // return if reloading is disabled for this weapon
705 if (!actor.reload_ammo_amount) return;
707 // our weapon is fully loaded, no need to reload
708 if (actor.clip_load >= actor.reload_ammo_amount) return;
710 // no ammo, so nothing to load
711 if (actor.ammo_field != ammo_none)
713 if (!actor.(actor.ammo_field) && actor.reload_ammo_min)
715 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
717 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
719 play2(actor, SND(UNAVAILABLE));
720 sprint(actor, strcat("You don't have enough ammo to reload the ^2", PS(actor).m_weapon.m_name, "\n"));
721 actor.reload_complain = time + 1;
723 // switch away if the amount of ammo is not enough to keep using this weapon
724 Weapon w = PS(actor).m_weapon;
725 if (!(w.wr_checkammo1(w) + w.wr_checkammo2(w)))
727 actor.clip_load = -1; // reload later
728 W_SwitchToOtherWeapon(actor);
735 entity this = actor.(weaponentity);
738 if (this.wframe == WFRAME_RELOAD) return;
740 // allow switching away while reloading, but this will cause a new reload!
741 this.state = WS_READY;
744 // now begin the reloading process
746 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
748 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
749 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
750 // so your weapon is disabled for a few seconds without reason
752 // ATTACK_FINISHED(actor, slot) = max(time, ATTACK_FINISHED(actor, slot)) + actor.reload_time + 1;
754 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
756 if (actor.clip_load < 0) actor.clip_load = 0;
757 actor.old_clip_load = actor.clip_load;
758 actor.clip_load = actor.(weapon_load[PS(actor).m_weapon.m_id]) = -1;
761 void W_DropEvent(.void(Weapon) event, entity player, float weapon_type, entity weapon_item)
763 Weapon w = Weapons_from(weapon_type);
764 weapon_dropevent_item = weapon_item;
765 WITH(entity, self, player, w.event(w));