1 #include "weaponsystem.qh"
3 #include "selection.qh"
5 #include "../command/common.qh"
6 #include <server/client.qh>
7 #include <server/damage.qh>
8 #include <server/world.qh>
9 #include <server/items/items.qh>
10 #include <server/mutators/_mod.qh>
11 #include "../round_handler.qh"
12 #include <server/cheats.qh>
13 #include <server/resources.qh>
14 #include <common/animdecide.qh>
15 #include <common/constants.qh>
16 #include <common/items/_mod.qh>
17 #include <common/net_linked.qh>
18 #include <common/mapobjects/platforms.qh>
19 #include <common/monsters/_mod.qh>
20 #include <common/notifications/all.qh>
21 #include <common/util.qh>
22 #include <common/weapons/_all.qh>
23 #include <common/state.qh>
24 #include <lib/csqcmodel/sv_model.qh>
25 #include <common/wepent.qh>
29 .float weapon_frametime;
31 float W_WeaponRateFactor(entity this)
34 if(g_weaponratefactor > 0)
35 t = 1.0 / g_weaponratefactor;
37 MUTATOR_CALLHOOK(WeaponRateFactor, t, this);
43 float W_WeaponSpeedFactor(entity this)
45 float t = 1.0 * g_weaponspeedfactor;
47 MUTATOR_CALLHOOK(WeaponSpeedFactor, t, this);
54 bool CL_Weaponentity_CustomizeEntityForClient(entity this, entity client)
56 this.viewmodelforclient = this.owner;
57 if (IS_SPEC(client) && client.enemy == this.owner) this.viewmodelforclient = client;
61 vector CL_Weapon_GetShotOrg(int wpn)
63 entity wi = REGISTRY_GET(Weapons, wpn);
65 CL_WeaponEntity_SetModel(e, wi.mdl, false);
66 vector ret = e.movedir;
67 CL_WeaponEntity_SetModel(e, "", false);
77 void CL_Weaponentity_Think(entity this)
79 this.nextthink = time;
80 if (game_stopped) this.frame = this.anim_idle.x;
81 .entity weaponentity = this.weaponentity_fld;
82 if (this.owner.(weaponentity) != this)
84 // owner has new gun; remove old one
85 if (this.weaponchild) delete(this.weaponchild);
86 if (this.hook) delete(this.hook);
90 if (IS_DEAD(this.owner))
92 // owner died; disappear
94 if (this.weaponchild) this.weaponchild.model = "";
97 if (this.w_weaponname != this.weaponname
98 || this.w_dmg != this.modelindex
99 || this.w_deadflag != this.deadflag)
101 // owner changed weapons; update appearance
102 this.w_weaponname = this.weaponname;
103 this.w_dmg = this.modelindex;
104 this.w_deadflag = this.deadflag;
106 CL_WeaponEntity_SetModel(this, this.weaponname, true);
109 this.alpha = -1; // TODO: don't render this entity at all
111 if (this.owner.alpha == default_player_alpha) this.m_alpha = default_weapon_alpha;
112 else if (this.owner.alpha != 0) this.m_alpha = this.owner.alpha;
113 else this.m_alpha = 1;
115 if (this.weaponchild)
117 this.weaponchild.alpha = this.alpha;
118 this.weaponchild.effects = this.effects;
122 void CL_ExteriorWeaponentity_Think(entity this)
124 this.nextthink = time;
125 .entity weaponentity = this.weaponentity_fld;
126 entity w_ent = this.owner.(weaponentity);
127 if (this.owner.exteriorweaponentity != this)
132 if (IS_DEAD(this.owner))
137 if (this.weaponname != w_ent.weaponname || this.dmg != w_ent.modelindex
138 || this.deadflag != w_ent.deadflag)
140 this.weaponname = w_ent.weaponname;
141 this.dmg = w_ent.modelindex;
142 this.deadflag = w_ent.deadflag;
143 if (w_ent.weaponname != "")
145 _setmodel(this, W_Model(strcat("v_", w_ent.weaponname, ".md3")));
146 setsize(this, '0 0 0', '0 0 0');
148 else this.model = "";
151 if ((tag_found = gettagindex(this.owner, "tag_weapon")))
153 this.tag_index = tag_found;
154 this.tag_entity = this.owner;
158 setattachment(this, this.owner, "bip01 r hand");
161 this.effects = this.owner.effects;
162 this.effects |= EF_LOWPRECISION;
163 this.effects = this.effects & EFMASK_CHEAP; // eat performance
164 if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
165 else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
168 Weapon wep = this.owner.(weaponentity).m_weapon;
169 if (wep) this.glowmod = weaponentity_glowmod(wep, this.owner, this.owner.clientcolors, this.owner.(weaponentity));
170 this.colormap = this.owner.colormap;
171 this.skin = w_ent.skin;
173 CSQCMODEL_AUTOUPDATE(this);
176 // spawning weaponentity for client
177 void CL_SpawnWeaponentity(entity actor, .entity weaponentity)
179 entity view = actor.(weaponentity) = new(weaponentity);
180 view.solid = SOLID_NOT;
182 setmodel(view, MDL_Null); // precision set when changed
183 setorigin(view, '0 0 0');
184 view.weaponentity_fld = weaponentity;
185 setthink(view, CL_Weaponentity_Think);
186 view.nextthink = time;
187 view.viewmodelforclient = actor;
188 view.draggable = drag_undraggable;
189 setcefc(view, CL_Weaponentity_CustomizeEntityForClient);
193 if (weaponentity == weaponentities[0]) // only one exterior model, thank you very much
195 entity exterior = actor.exteriorweaponentity = new(exteriorweaponentity);
196 exterior.solid = SOLID_NOT;
197 exterior.owner = actor;
198 exterior.draggable = drag_undraggable;
199 exterior.weaponentity_fld = weaponentity;
200 setorigin(exterior, '0 0 0');
201 setthink(exterior, CL_ExteriorWeaponentity_Think);
202 exterior.nextthink = time;
204 CSQCMODEL_AUTOINIT(exterior);
209 void w_clear(Weapon thiswep, entity actor, .entity weaponentity, int fire)
211 actor.(weaponentity).m_weapon = WEP_Null;
212 actor.(weaponentity).m_switchingweapon = WEP_Null;
213 entity this = actor.(weaponentity);
216 this.state = WS_CLEAR;
221 void w_ready(Weapon thiswep, entity actor, .entity weaponentity, int fire)
223 entity this = actor.(weaponentity);
224 if (this) this.state = WS_READY;
225 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, 1000000, w_ready);
230 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary, .entity weaponentity)
232 if ((actor.items & IT_UNLIMITED_AMMO)) return true;
234 if (secondary) ammo = thiswep.wr_checkammo2(thiswep, actor, weaponentity);
235 else ammo = thiswep.wr_checkammo1(thiswep, actor, weaponentity);
236 if (ammo) return true;
237 // always keep the Mine Layer if we placed mines, so that we can detonate them
238 if (thiswep == WEP_MINE_LAYER)
240 IL_EACH(g_mines, it.owner == actor && it.weaponentity_fld == weaponentity,
246 if (thiswep == WEP_SHOTGUN)
247 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false; // no clicking, just allow
249 if (thiswep == actor.(weaponentity).m_switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
251 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
252 actor.prevdryfire = time;
255 // check if the other firing mode has enough ammo
256 bool ammo_other = false;
257 if (secondary) ammo_other = thiswep.wr_checkammo1(thiswep, actor, weaponentity);
258 else ammo_other = thiswep.wr_checkammo2(thiswep, actor, weaponentity);
261 if (time - actor.prevwarntime > 1)
267 ITEM_WEAPON_PRIMORSEC,
273 actor.prevwarntime = time;
275 else // this weapon is totally unable to fire, switch to another one
277 W_SwitchToOtherWeapon(actor, weaponentity);
284 bool weapon_prepareattack_check(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
286 if (actor.weaponentity == NULL) return true;
287 if (!weapon_prepareattack_checkammo(thiswep, actor, secondary, weaponentity)) return false;
289 // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
290 // if all players readied up and the countdown is running
291 if (time < game_starttime || time < actor.race_penalty) return false;
293 if (timeout_status == TIMEOUT_ACTIVE) // don't allow the player to shoot while game is paused
296 // do not even think about shooting if switching
297 if (actor.(weaponentity).m_switchweapon != actor.(weaponentity).m_weapon) return false;
301 // don't fire if previous attack is not finished
302 if (ATTACK_FINISHED(actor, weaponentity) > time + actor.(weaponentity).weapon_frametime * 0.5) return false;
303 entity this = actor.(weaponentity);
304 // don't fire while changing weapon
305 if (this.state != WS_READY) return false;
310 void weapon_prepareattack_do(entity actor, .entity weaponentity, bool secondary, float attacktime)
312 entity this = actor.(weaponentity);
313 if (this == NULL) return;
314 this.state = WS_INUSE;
316 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
318 // if the weapon hasn't been firing continuously, reset the timer
321 if (ATTACK_FINISHED(actor, weaponentity) < time - this.weapon_frametime * 1.5)
323 ATTACK_FINISHED(actor, weaponentity) = time;
324 // dprint("resetting attack finished to ", ftos(time), "\n");
326 float arate = W_WeaponRateFactor(actor);
327 ATTACK_FINISHED(actor, weaponentity) = ATTACK_FINISHED(actor, weaponentity) + attacktime * arate;
329 if(autocvar_g_weaponswitch_debug_alternate && W_DualWielding(actor))
331 int slot = weaponslot(weaponentity);
332 for(int wepslot = 0; wepslot < MAX_WEAPONSLOTS; ++wepslot)
336 .entity wepent = weaponentities[wepslot];
337 if(actor.(wepent) && actor.(wepent).m_weapon != WEP_Null)
339 if(ATTACK_FINISHED(actor, wepent) > time + actor.(wepent).weapon_frametime * 0.5)
340 continue; // still cooling down!
341 if (ATTACK_FINISHED(actor, wepent) < time - actor.(wepent).weapon_frametime * 1.5)
342 ATTACK_FINISHED(actor, wepent) = time;
343 ATTACK_FINISHED(actor, wepent) = ATTACK_FINISHED(actor, wepent) + (attacktime * arate) / MAX_WEAPONSLOTS;
348 this.bulletcounter += 1;
349 // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, weaponentity)), "\n");
352 bool weapon_prepareattack(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
354 if (weapon_prepareattack_check(thiswep, actor, weaponentity, secondary, attacktime))
356 weapon_prepareattack_do(actor, weaponentity, secondary, attacktime);
363 * @param t defer thinking until time + t
364 * @param func next think function
366 void weapon_thinkf(entity actor, .entity weaponentity, WFRAME fr, float t, void(Weapon thiswep, entity actor,
367 .entity weaponentity, int fire) func)
369 entity this = actor.(weaponentity);
370 if (this == NULL) return;
372 if (fr == WFRAME_DONTCHANGE)
374 // this can happen when the weapon entity is newly spawned, since it has a clear state and no previous weapon frame
375 if (this.wframe == WFRAME_DONTCHANGE)
376 this.wframe = WFRAME_IDLE;
382 restartanim = fr != WFRAME_IDLE;
387 if (this.weapon_think == w_ready && func != w_ready && this.state == WS_RAISE) backtrace(
388 "Tried to override initial weapon think function - should this really happen?");
390 t *= W_WeaponRateFactor(actor);
392 // VorteX: haste can be added here
393 if (this.weapon_think == w_ready)
395 this.weapon_nextthink = time;
396 // dprint("started firing at ", ftos(time), "\n");
398 if (this.weapon_nextthink < time - this.weapon_frametime * 1.5
399 || this.weapon_nextthink > time + this.weapon_frametime * 1.5)
401 this.weapon_nextthink = time;
402 // dprint("reset weapon animation timer at ", ftos(time), "\n");
404 this.weapon_nextthink += t;
405 this.weapon_think = func;
406 // dprint("next ", ftos(this.weapon_nextthink), "\n");
410 FOREACH_CLIENT(true, {
411 if(it == actor || (IS_SPEC(it) && it.enemy == actor))
412 wframe_send(it, this, fr, g_weaponratefactor, restartanim);
416 if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
418 bool primary_melee = boolean(fr == WFRAME_FIRE1 && (this.m_weapon.spawnflags & WEP_TYPE_MELEE_PRI));
419 bool secondary_melee = boolean(fr == WFRAME_FIRE2 && (this.m_weapon.spawnflags & WEP_TYPE_MELEE_SEC));
420 int act = (primary_melee || secondary_melee)
424 animdecide_setaction(actor, act, restartanim);
426 else if (actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
428 actor.anim_upper_action = 0;
432 bool weaponUseForbidden(entity player)
434 if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
435 if (MUTATOR_CALLHOOK(ForbidWeaponUse, player)) return true;
439 bool weaponLocked(entity player)
441 if (time < game_starttime && !sv_ready_restart_after_countdown) return true;
442 if (player.player_blocked) return true;
443 if (game_stopped) return true;
444 if (STAT(FROZEN, player)) return true;
445 if (MUTATOR_CALLHOOK(LockWeapon, player)) return true;
449 void W_ResetGunAlign(entity player, int preferred_alignment)
451 if(W_DualWielding(player))
452 preferred_alignment = 3; // right align, the second gun will default to left
454 // clear current weapon slots' alignments so we can redo the calculations!
455 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
457 .entity weaponentity = weaponentities[slot];
458 if (player.(weaponentity))
459 player.(weaponentity).m_gunalign = 0;
462 // now set the new values
463 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
465 .entity weaponentity = weaponentities[slot];
466 if (player.(weaponentity))
467 player.(weaponentity).m_gunalign = W_GunAlign(player.(weaponentity), preferred_alignment);
471 .bool hook_switchweapon;
473 void W_WeaponFrame(Player actor, .entity weaponentity)
476 TC(PlayerState, PS(actor));
477 entity this = actor.(weaponentity);
478 if (frametime) this.weapon_frametime = frametime;
480 if (!this || GetResource(actor, RES_HEALTH) < 1) return; // Dead player can't use weapons and injure impulse commands
482 int button_atck = PHYS_INPUT_BUTTON_ATCK(actor);
483 int button_atck2 = PHYS_INPUT_BUTTON_ATCK2(actor);
485 if (weaponUseForbidden(actor))
486 button_atck = button_atck2 = 0; // forbid primary and secondary fire, switching is allowed
488 if (weaponLocked(actor))
490 if (this.state != WS_CLEAR)
492 Weapon wpn = this.m_weapon;
493 w_ready(wpn, actor, weaponentity, button_atck | (button_atck2 << 1));
498 if(autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0)
500 .entity wepe1 = weaponentities[0];
501 entity wep1 = actor.(wepe1);
502 this.m_switchweapon = wep1.m_switchweapon;
503 entity store = IS_PLAYER(actor) ? PS(actor) : actor;
504 if(!(this.m_switchweapon.spawnflags & WEP_FLAG_DUALWIELD) && !(store.dual_weapons & wep1.m_switchweapon.m_wepset))
506 this.m_weapon = WEP_Null;
507 this.m_switchingweapon = WEP_Null;
508 this.m_switchweapon = WEP_Null;
509 this.state = WS_CLEAR;
510 this.weaponname = "";
515 if (this.m_switchweapon == WEP_Null)
517 if (this.state != WS_CLEAR)
518 w_ready(this.m_weapon, actor, weaponentity, button_atck | (button_atck2 << 1));
519 this.m_weapon = WEP_Null;
520 this.m_switchingweapon = WEP_Null;
521 this.state = WS_CLEAR;
522 this.weaponname = "";
527 MAKE_VECTORS(actor.v_angle, fo, ri, up);
530 if (this.m_weapon != this.m_switchweapon)
535 LOG_WARNF("unhandled weaponentity (%i) state for player (%i): %d", this, actor, this.state);
543 Weapon newwep = this.m_switchweapon;
544 this.m_switchingweapon = newwep;
546 // the two weapon entities will notice this has changed and update their models
547 this.m_weapon = newwep;
548 this.weaponname = newwep.mdl;
549 this.bulletcounter = 0;
550 newwep.wr_setup(newwep, actor, weaponentity);
551 this.state = WS_RAISE;
553 // set our clip load to the load of the weapon we switched to, if it's reloadable
554 if ((newwep.spawnflags & WEP_FLAG_RELOADABLE) && newwep.reloading_ammo) // prevent accessing undefined cvars
556 this.clip_load = this.(weapon_load[this.m_switchweapon.m_id]);
557 this.clip_size = newwep.reloading_ammo;
561 this.clip_load = this.clip_size = 0;
564 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, newwep.switchdelay_raise, w_ready);
569 // in dropping phase we can switch at any time
570 this.m_switchingweapon = this.m_switchweapon;
576 this.m_switchingweapon = this.m_switchweapon;
577 entity oldwep = this.m_weapon;
579 // set up weapon switch think in the future, and start drop anim
580 if (INDEPENDENT_ATTACK_FINISHED || ATTACK_FINISHED(actor, weaponentity) <= time + this.weapon_frametime * 0.5)
582 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
583 this.state = WS_DROP;
584 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
591 // LordHavoc: network timing test code
592 // if (actor.button0)
593 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor, weaponentity)), " >= ", ftos(this.weapon_nextthink), "\n");
595 Weapon w = this.m_weapon;
597 // call the think code which may fire the weapon
598 // and do so multiple times to resolve framerate dependency issues if the
599 // server framerate is very low and the weapon fire rate very high
600 for (int c = 0; c < W_TICSPERFRAME; ++c)
602 if (w != WEP_Null && !(STAT(WEAPONS, actor) & WepSet_FromWeapon(w)))
604 if (this.m_weapon == this.m_switchweapon) W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
612 bool block_weapon = false;
614 bool key_pressed = PHYS_INPUT_BUTTON_HOOK(actor) && !actor.vehicle;
615 if (weaponUseForbidden(actor))
618 Weapon off = actor.offhand;
619 if (off && (!(STAT(WEAPONS, actor) & WEPSET(HOOK)) || off != OFFHAND_HOOK))
621 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
625 if (key_pressed && this.m_switchweapon != WEP_HOOK && !actor.hook_switchweapon)
626 W_SwitchWeapon(actor, WEP_HOOK, weaponentity);
627 actor.hook_switchweapon = key_pressed;
629 block_weapon = (this.m_weapon == h && (button_atck || key_pressed));
630 h.wr_think(h, actor, weaponentity, block_weapon ? 1 : 0);
640 Weapon e = this.m_weapon;
644 e.wr_think(e, actor, weaponentity, button_atck | (button_atck2 << 1));
648 e.wr_gonethink(e, actor, weaponentity);
652 if (time + this.weapon_frametime * 0.5 >= this.weapon_nextthink)
654 if (this.weapon_think)
659 Weapon wpn = this.m_weapon;
660 this.weapon_think(wpn, actor, weaponentity,
661 button_atck | (button_atck2 << 1));
665 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
671 void W_AttachToShotorg(entity actor, .entity weaponentity, entity flash, vector offset)
674 flash.angles_z = random() * 360;
676 entity view = actor.(weaponentity);
677 entity exterior = actor.exteriorweaponentity;
679 if (gettagindex(view, "shot")) setattachment(flash, view, "shot");
680 else setattachment(flash, view, "tag_shot");
681 setorigin(flash, offset);
683 entity xflash = spawn();
684 copyentity(flash, xflash);
686 flash.viewmodelforclient = actor;
688 if (view.oldorigin.x > 0)
690 setattachment(xflash, exterior, "");
691 setorigin(xflash, view.oldorigin + offset);
695 if (gettagindex(exterior, "shot")) setattachment(xflash, exterior, "shot");
696 else setattachment(xflash, exterior, "tag_shot");
697 setorigin(xflash, offset);
701 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use, .entity weaponentity)
703 if (MUTATOR_CALLHOOK(W_DecreaseAmmo, actor, actor.(weaponentity), ammo_use)) return;
704 if ((actor.items & IT_UNLIMITED_AMMO) && !wep.reloading_ammo) return;
706 ammo_use = M_ARGV(2, float);
708 entity w_ent = actor.(weaponentity);
710 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
711 if (wep.reloading_ammo)
713 w_ent.clip_load -= ammo_use;
714 w_ent.(weapon_load[w_ent.m_weapon.m_id]) = w_ent.clip_load;
716 else if (wep.ammo_type != RES_NONE)
718 float ammo = GetResource(actor, wep.ammo_type);
722 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
723 "Please notify the developers immediately with a copy of this backtrace!\n",
726 GetAmmoPicture(wep.ammo_type),
731 SetResource(actor, wep.ammo_type, ammo - ammo_use);
735 // weapon reloading code
737 .float reload_ammo_amount, reload_ammo_min, reload_time;
738 .float reload_complain;
739 .string reload_sound;
741 void W_ReloadedAndReady(Weapon thiswep, entity actor, .entity weaponentity, int fire)
743 // finish the reloading process, and do the ammo transfer
745 entity w_ent = actor.(weaponentity);
746 Weapon wpn = w_ent.m_weapon;
748 w_ent.clip_load = w_ent.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
750 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
751 if (!w_ent.reload_ammo_min || (actor.items & IT_UNLIMITED_AMMO) || wpn.ammo_type == RES_NONE)
753 w_ent.clip_load = w_ent.reload_ammo_amount;
757 // make sure we don't add more ammo than we have
758 float ammo = GetResource(actor, wpn.ammo_type);
759 float load = min(w_ent.reload_ammo_amount - w_ent.clip_load, ammo);
760 w_ent.clip_load += load;
761 SetResource(actor, wpn.ammo_type, ammo - load);
763 w_ent.(weapon_load[w_ent.m_weapon.m_id]) = w_ent.clip_load;
765 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
766 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
767 // so your weapon is disabled for a few seconds without reason
769 // ATTACK_FINISHED(actor, weaponentity) -= w_ent.reload_time - 1;
771 w_ready(wpn, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
774 void W_Reload(entity actor, .entity weaponentity, float sent_ammo_min, Sound sent_sound)
776 TC(Sound, sent_sound);
777 // set global values to work with
778 entity this = actor.(weaponentity);
779 Weapon e = this.m_weapon;
781 if (MUTATOR_CALLHOOK(W_Reload, actor)) return;
783 this.reload_ammo_min = sent_ammo_min;
784 this.reload_ammo_amount = e.reloading_ammo;
785 this.reload_time = e.reloading_time;
786 strcpy(actor.reload_sound, Sound_fixpath(sent_sound));
788 // don't reload weapons that don't have the RELOADABLE flag
789 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
792 "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
796 // return if reloading is disabled for this weapon
797 if (!this.reload_ammo_amount) return;
799 // our weapon is fully loaded, no need to reload
800 if (this.clip_load >= this.reload_ammo_amount) return;
802 // no ammo, so nothing to load
803 if (e.ammo_type != RES_NONE)
805 if (!GetResource(actor, e.ammo_type) && this.reload_ammo_min)
807 if (!(actor.items & IT_UNLIMITED_AMMO))
809 if (autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0)
810 return; // in this case the primary weapon will do the switching when it runs out of ammo (TODO: do this same check but for other slots)
811 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
813 play2(actor, SND(UNAVAILABLE));
814 sprint(actor, strcat("You don't have enough ammo to reload the ^2", this.m_weapon.m_name, "\n"));
815 actor.reload_complain = time + 1;
817 // switch away if the amount of ammo is not enough to keep using this weapon
818 if (!(e.wr_checkammo1(e, actor, weaponentity) + e.wr_checkammo2(e, actor, weaponentity)))
820 this.clip_load = -1; // reload later
821 W_SwitchToOtherWeapon(actor, weaponentity);
829 if (this.wframe == WFRAME_RELOAD) return;
831 // allow switching away while reloading, but this will cause a new reload!
832 this.state = WS_READY;
835 // now begin the reloading process
837 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
839 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
840 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
841 // so your weapon is disabled for a few seconds without reason
843 // ATTACK_FINISHED(actor, weaponentity) = max(time, ATTACK_FINISHED(actor, weaponentity)) + this.reload_time + 1;
845 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, this.reload_time, W_ReloadedAndReady);
847 if (this.clip_load < 0) this.clip_load = 0;
848 this.old_clip_load = this.clip_load;
849 this.clip_load = this.(weapon_load[this.m_weapon.m_id]) = -1;
852 void W_DropEvent(.void(Weapon, entity actor, .entity) event, entity player, float weapon_type, entity weapon_item, .entity weaponentity)
854 Weapon w = REGISTRY_GET(Weapons, weapon_type);
855 weapon_dropevent_item = weapon_item;
856 w.event(w, player, weaponentity);