1 #include "weaponsystem.qh"
4 #include "selection.qh"
6 #include "../command/common.qh"
7 #include "../mutators/mutators_include.qh"
8 #include "../round_handler.qh"
9 #include "../t_items.qh"
10 #include "../../common/animdecide.qh"
11 #include "../../common/constants.qh"
12 #include "../../common/monsters/all.qh"
13 #include "../../common/notifications.qh"
14 #include "../../common/util.qh"
15 #include "../../common/weapons/all.qh"
16 #include "../../csqcmodellib/sv_model.qh"
19 ===========================================================================
21 CLIENT WEAPONSYSTEM CODE
22 Bring back W_Weaponframe
24 ===========================================================================
27 .float weapon_frametime;
29 float W_WeaponRateFactor()
31 float t = 1.0 / g_weaponratefactor;
33 MUTATOR_CALLHOOK(WeaponRateFactor, t);
39 float W_WeaponSpeedFactor()
41 float t = 1.0 * g_weaponspeedfactor;
43 MUTATOR_CALLHOOK(WeaponSpeedFactor, t);
50 void weapon_thinkf(entity actor, float fr, float t, void(Weapon thiswep, entity actor, bool fire1, bool fire2) func);
52 float CL_Weaponentity_CustomizeEntityForClient()
54 self.viewmodelforclient = self.owner;
56 if(other.enemy == self.owner)
57 self.viewmodelforclient = other;
64 * 1. simple animated model, muzzle flash handling on h_ model:
65 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
67 * shot = muzzle end (shot origin, also used for muzzle flashes)
68 * shell = casings ejection point (must be on the right hand side of the gun)
69 * weapon = attachment for v_tuba.md3
70 * v_tuba.md3 - first and third person model
71 * g_tuba.md3 - pickup model
73 * 2. simple animated model, muzzle flash handling on v_ model:
74 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
76 * weapon = attachment for v_tuba.md3
77 * v_tuba.md3 - first and third person model
79 * shot = muzzle end (shot origin, also used for muzzle flashes)
80 * shell = casings ejection point (must be on the right hand side of the gun)
81 * g_tuba.md3 - pickup model
83 * 3. fully animated model, muzzle flash handling on h_ model:
84 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
86 * shot = muzzle end (shot origin, also used for muzzle flashes)
87 * shell = casings ejection point (must be on the right hand side of the gun)
88 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
89 * v_tuba.md3 - third person model
90 * g_tuba.md3 - pickup model
92 * 4. fully animated model, muzzle flash handling on v_ model:
93 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
95 * shot = muzzle end (shot origin)
96 * shell = casings ejection point (must be on the right hand side of the gun)
97 * v_tuba.md3 - third person model
99 * shot = muzzle end (for muzzle flashes)
100 * g_tuba.md3 - pickup model
104 // self.origin, self.angles
106 // self.movedir, self.view_ofs
110 // call again with ""
112 void CL_WeaponEntity_SetModel(string name)
117 // if there is a child entity, hide it until we're sure we use it
118 if (self.weaponentity)
119 self.weaponentity.model = "";
120 _setmodel(self, W_Model(strcat("v_", name, ".md3")));
121 v_shot_idx = gettagindex(self, "shot"); // used later
123 v_shot_idx = gettagindex(self, "tag_shot");
125 _setmodel(self, W_Model(strcat("h_", name, ".iqm")));
126 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
127 self.anim_fire1 = animfixfps(self, '0 1 0.01', '0 0 0');
128 self.anim_fire2 = animfixfps(self, '1 1 0.01', '0 0 0');
129 self.anim_idle = animfixfps(self, '2 1 0.01', '0 0 0');
130 self.anim_reload = animfixfps(self, '3 1 0.01', '0 0 0');
132 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
133 // if we don't, this is a "real" animated model
134 if(gettagindex(self, "weapon"))
136 if (!self.weaponentity)
137 self.weaponentity = spawn();
138 _setmodel(self.weaponentity, W_Model(strcat("v_", name, ".md3")));
139 setattachment(self.weaponentity, self, "weapon");
141 else if(gettagindex(self, "tag_weapon"))
143 if (!self.weaponentity)
144 self.weaponentity = spawn();
145 _setmodel(self.weaponentity, W_Model(strcat("v_", name, ".md3")));
146 setattachment(self.weaponentity, self, "tag_weapon");
150 if(self.weaponentity)
151 remove(self.weaponentity);
152 self.weaponentity = world;
155 setorigin(self,'0 0 0');
156 self.angles = '0 0 0';
158 self.viewmodelforclient = world;
162 if(v_shot_idx) // v_ model attached to invisible h_ model
164 self.movedir = gettaginfo(self.weaponentity, v_shot_idx);
168 idx = gettagindex(self, "shot");
170 idx = gettagindex(self, "tag_shot");
172 self.movedir = gettaginfo(self, idx);
175 LOG_INFO("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
176 self.movedir = '0 0 0';
180 if(self.weaponentity) // v_ model attached to invisible h_ model
182 idx = gettagindex(self.weaponentity, "shell");
184 idx = gettagindex(self.weaponentity, "tag_shell");
186 self.spawnorigin = gettaginfo(self.weaponentity, idx);
192 idx = gettagindex(self, "shell");
194 idx = gettagindex(self, "tag_shell");
196 self.spawnorigin = gettaginfo(self, idx);
199 LOG_INFO("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
200 self.spawnorigin = self.movedir;
206 self.oldorigin = '0 0 0'; // use regular attachment
210 if(self.weaponentity)
212 idx = gettagindex(self, "weapon");
214 idx = gettagindex(self, "tag_weapon");
218 idx = gettagindex(self, "handle");
220 idx = gettagindex(self, "tag_handle");
224 self.oldorigin = self.movedir - gettaginfo(self, idx);
228 LOG_INFO("WARNING: weapon model ", self.model, " does not support the 'handle' tag and neither does the v_ model support the 'shot' tag, will display muzzle flashes TOTALLY wrong\n");
229 self.oldorigin = '0 0 0'; // there is no way to recover from this
233 self.viewmodelforclient = self.owner;
238 if(self.weaponentity)
239 remove(self.weaponentity);
240 self.weaponentity = world;
241 self.movedir = '0 0 0';
242 self.spawnorigin = '0 0 0';
243 self.oldorigin = '0 0 0';
244 self.anim_fire1 = '0 1 0.01';
245 self.anim_fire2 = '0 1 0.01';
246 self.anim_idle = '0 1 0.01';
247 self.anim_reload = '0 1 0.01';
250 self.view_ofs = '0 0 0';
252 if(self.movedir.x >= 0)
256 self.movedir = shotorg_adjust(v0, false, false);
257 self.view_ofs = shotorg_adjust(v0, false, true) - v0;
259 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
260 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
262 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
264 // check if an instant weapon switch occurred
265 setorigin(self, self.view_ofs);
266 // reset animstate now
267 self.wframe = WFRAME_IDLE;
268 setanim(self, self.anim_idle, true, false, true);
271 vector CL_Weapon_GetShotOrg(float wpn)
273 entity wi = get_weaponinfo(wpn);
275 CL_WeaponEntity_SetModel(wi.mdl);
276 vector ret = self.movedir;
277 CL_WeaponEntity_SetModel("");
283 void CL_Weaponentity_Think()
286 self.nextthink = time;
287 if (intermission_running)
288 self.frame = self.anim_idle.x;
289 if (self.owner.weaponentity != self)
291 if (self.weaponentity)
292 remove(self.weaponentity);
296 if (self.owner.deadflag != DEAD_NO)
299 if (self.weaponentity)
300 self.weaponentity.model = "";
303 if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
305 self.weaponname = self.owner.weaponname;
306 self.dmg = self.owner.modelindex;
307 self.deadflag = self.owner.deadflag;
309 CL_WeaponEntity_SetModel(self.owner.weaponname);
312 tb = (self.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
313 self.effects = self.owner.effects & EFMASK_CHEAP;
314 self.effects &= ~EF_LOWPRECISION;
315 self.effects &= ~EF_FULLBRIGHT; // can mask team color, so get rid of it
316 self.effects &= ~EF_TELEPORT_BIT;
317 self.effects &= ~EF_RESTARTANIM_BIT;
320 if(self.owner.alpha == default_player_alpha)
321 self.alpha = default_weapon_alpha;
322 else if(self.owner.alpha != 0)
323 self.alpha = self.owner.alpha;
327 self.glowmod = self.owner.weaponentity_glowmod;
328 self.colormap = self.owner.colormap;
329 if (self.weaponentity)
331 self.weaponentity.effects = self.effects;
332 self.weaponentity.alpha = self.alpha;
333 self.weaponentity.colormap = self.colormap;
334 self.weaponentity.glowmod = self.glowmod;
337 self.angles = '0 0 0';
339 float f = (self.owner.weapon_nextthink - time);
340 if (self.state == WS_RAISE && !intermission_running)
342 entity newwep = get_weaponinfo(self.owner.switchweapon);
343 f = f * g_weaponratefactor / max(f, newwep.switchdelay_raise);
344 self.angles_x = -90 * f * f;
346 else if (self.state == WS_DROP && !intermission_running)
348 entity oldwep = get_weaponinfo(self.owner.weapon);
349 f = 1 - f * g_weaponratefactor / max(f, oldwep.switchdelay_drop);
350 self.angles_x = -90 * f * f;
352 else if (self.state == WS_CLEAR)
355 self.angles_x = -90 * f * f;
359 void CL_ExteriorWeaponentity_Think()
362 self.nextthink = time;
363 if (self.owner.exteriorweaponentity != self)
368 if (self.owner.deadflag != DEAD_NO)
373 if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
375 self.weaponname = self.owner.weaponname;
376 self.dmg = self.owner.modelindex;
377 self.deadflag = self.owner.deadflag;
378 if (self.owner.weaponname != "")
379 _setmodel(self, W_Model(strcat("v_", self.owner.weaponname, ".md3")));
383 if((tag_found = gettagindex(self.owner, "tag_weapon")))
385 self.tag_index = tag_found;
386 self.tag_entity = self.owner;
389 setattachment(self, self.owner, "bip01 r hand");
391 self.effects = self.owner.effects;
392 self.effects |= EF_LOWPRECISION;
393 self.effects = self.effects & EFMASK_CHEAP; // eat performance
394 if(self.owner.alpha == default_player_alpha)
395 self.alpha = default_weapon_alpha;
396 else if(self.owner.alpha != 0)
397 self.alpha = self.owner.alpha;
401 self.glowmod = self.owner.weaponentity_glowmod;
402 self.colormap = self.owner.colormap;
404 CSQCMODEL_AUTOUPDATE(self);
407 // spawning weaponentity for client
408 void CL_SpawnWeaponentity(entity e)
410 entity view = e.weaponentity = spawn();
411 view.classname = "weaponentity";
412 view.solid = SOLID_NOT;
414 setmodel(view, MDL_Null); // precision set when changed
415 setorigin(view, '0 0 0');
416 view.angles = '0 0 0';
417 view.viewmodelforclient = e;
419 view.think = CL_Weaponentity_Think;
420 view.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
421 view.nextthink = time;
423 entity exterior = e.exteriorweaponentity = spawn();
424 exterior.classname = "exteriorweaponentity";
425 exterior.solid = SOLID_NOT;
426 exterior.exteriorweaponentity = exterior;
428 setorigin(exterior, '0 0 0');
429 exterior.angles = '0 0 0';
430 exterior.think = CL_ExteriorWeaponentity_Think;
431 exterior.nextthink = time;
433 CSQCMODEL_AUTOINIT(exterior);
437 void w_clear(Weapon thiswep, entity actor, bool fire1, bool fire2)
439 if (actor.weapon != -1)
442 actor.switchingweapon = 0;
444 if (actor.weaponentity)
446 actor.weaponentity.state = WS_CLEAR;
447 actor.weaponentity.effects = 0;
451 void w_ready(Weapon thiswep, entity actor, bool fire1, bool fire2)
453 if (actor.weaponentity)
454 actor.weaponentity.state = WS_READY;
455 weapon_thinkf(actor, WFRAME_IDLE, 1000000, w_ready);
460 bool weapon_prepareattack_checkammo(entity actor, float secondary)
462 Weapon w = get_weaponinfo(actor.weapon);
463 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
464 if (!w.(secondary ? wr_checkammo2 : wr_checkammo1)(w))
466 // always keep the Mine Layer if we placed mines, so that we can detonate them
468 if(actor.weapon == WEP_MINE_LAYER.m_id)
469 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == actor)
472 if(actor.weapon == WEP_SHOTGUN.m_id)
473 if(!secondary && WEP_CVAR(shotgun, secondary) == 1)
474 return false; // no clicking, just allow
476 if(actor.weapon == actor.switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
478 sound (actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
479 actor.prevdryfire = time;
482 if(w.(secondary ? wr_checkammo1 : wr_checkammo2)(w)) // check if the other firing mode has enough ammo
484 if(time - actor.prevwarntime > 1)
490 ITEM_WEAPON_PRIMORSEC,
496 actor.prevwarntime = time;
498 else // this weapon is totally unable to fire, switch to another one
500 W_SwitchToOtherWeapon(actor);
508 bool weapon_prepareattack_check(entity actor, bool secondary, float attacktime)
510 if(!weapon_prepareattack_checkammo(actor, secondary))
513 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
514 //if all players readied up and the countdown is running
515 if(time < game_starttime || time < actor.race_penalty) {
519 if (timeout_status == TIMEOUT_ACTIVE) //don't allow the player to shoot while game is paused
522 // do not even think about shooting if switching
523 if(actor.switchweapon != actor.weapon)
528 // don't fire if previous attack is not finished
529 if (ATTACK_FINISHED(actor) > time + actor.weapon_frametime * 0.5)
531 // don't fire while changing weapon
532 if (actor.weaponentity.state != WS_READY)
537 void weapon_prepareattack_do(entity actor, bool secondary, float attacktime)
539 actor.weaponentity.state = WS_INUSE;
541 actor.spawnshieldtime = min(actor.spawnshieldtime, time); // kill spawn shield when you fire
543 // if the weapon hasn't been firing continuously, reset the timer
546 if (ATTACK_FINISHED(actor) < time - actor.weapon_frametime * 1.5)
548 ATTACK_FINISHED(actor) = time;
549 //dprint("resetting attack finished to ", ftos(time), "\n");
551 ATTACK_FINISHED(actor) = ATTACK_FINISHED(actor) + attacktime * W_WeaponRateFactor();
553 actor.bulletcounter += 1;
554 //dprint("attack finished ", ftos(ATTACK_FINISHED(actor)), "\n");
556 bool weapon_prepareattack(entity actor, bool secondary, float attacktime)
558 if (weapon_prepareattack_check(actor, secondary, attacktime)) {
559 weapon_prepareattack_do(actor, secondary, attacktime);
565 void weapon_thinkf(entity actor, float fr, float t, void(Weapon thiswep, entity actor, bool fire1, bool fire2) func)
571 if(fr == WFRAME_DONTCHANGE)
573 fr = actor.weaponentity.wframe;
576 else if (fr == WFRAME_IDLE)
585 if (actor.weaponentity)
587 actor.weaponentity.wframe = fr;
589 if (fr == WFRAME_IDLE)
590 a = actor.weaponentity.anim_idle;
591 else if (fr == WFRAME_FIRE1)
592 a = actor.weaponentity.anim_fire1;
593 else if (fr == WFRAME_FIRE2)
594 a = actor.weaponentity.anim_fire2;
595 else // if (fr == WFRAME_RELOAD)
596 a = actor.weaponentity.anim_reload;
597 a.z *= g_weaponratefactor;
598 setanim(actor.weaponentity, a, restartanim == false, restartanim, restartanim);
605 if(actor.weapon_think == w_ready && func != w_ready && actor.weaponentity.state == WS_RAISE)
607 backtrace("Tried to override initial weapon think function - should this really happen?");
610 t *= W_WeaponRateFactor();
612 // VorteX: haste can be added here
613 if (actor.weapon_think == w_ready)
615 actor.weapon_nextthink = time;
616 //dprint("started firing at ", ftos(time), "\n");
618 if (actor.weapon_nextthink < time - actor.weapon_frametime * 1.5 || actor.weapon_nextthink > time + actor.weapon_frametime * 1.5)
620 actor.weapon_nextthink = time;
621 //dprint("reset weapon animation timer at ", ftos(time), "\n");
623 actor.weapon_nextthink = actor.weapon_nextthink + t;
624 actor.weapon_think = func;
625 //dprint("next ", ftos(actor.weapon_nextthink), "\n");
627 if((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
629 if((actor.weapon == WEP_SHOCKWAVE.m_id || actor.weapon == WEP_SHOTGUN.m_id) && fr == WFRAME_FIRE2)
630 animdecide_setaction(actor, ANIMACTION_MELEE, restartanim);
632 animdecide_setaction(actor, ANIMACTION_SHOOT, restartanim);
636 if(actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
637 actor.anim_upper_action = 0;
641 float forbidWeaponUse(entity player)
643 if(time < game_starttime && !autocvar_sv_ready_restart_after_countdown)
645 if(round_handler_IsActive() && !round_handler_IsRoundStarted())
647 if(player.player_blocked)
651 if(player.weapon_blocked)
656 void W_WeaponFrame(entity actor)
661 actor.weapon_frametime = frametime;
663 if (!actor.weaponentity || actor.health < 1)
664 return; // Dead player can't use weapons and injure impulse commands
666 if(forbidWeaponUse(actor))
667 if(actor.weaponentity.state != WS_CLEAR)
669 Weapon wpn = get_weaponinfo(actor.weapon);
670 w_ready(wpn, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
674 if(!actor.switchweapon)
677 actor.switchingweapon = 0;
678 actor.weaponentity.state = WS_CLEAR;
679 actor.weaponname = "";
680 //actor.items &= ~IT_AMMO;
684 makevectors(actor.v_angle);
685 fo = v_forward; // save them in case the weapon think functions change it
690 if (actor.weapon != actor.switchweapon)
692 if (actor.weaponentity.state == WS_CLEAR)
695 actor.switchingweapon = actor.switchweapon;
696 entity newwep = get_weaponinfo(actor.switchweapon);
698 // the two weapon entities will notice this has changed and update their models
699 actor.weapon = actor.switchweapon;
700 actor.weaponname = newwep.mdl;
701 actor.bulletcounter = 0;
702 actor.ammo_field = newwep.ammo_field;
703 Weapon w = get_weaponinfo(actor.switchweapon);
705 actor.weaponentity.state = WS_RAISE;
707 // set our clip load to the load of the weapon we switched to, if it's reloadable
708 if(newwep.spawnflags & WEP_FLAG_RELOADABLE && newwep.reloading_ammo) // prevent accessing undefined cvars
710 actor.clip_load = actor.(weapon_load[actor.switchweapon]);
711 actor.clip_size = newwep.reloading_ammo;
714 actor.clip_load = actor.clip_size = 0;
716 weapon_thinkf(actor, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
718 else if (actor.weaponentity.state == WS_DROP)
720 // in dropping phase we can switch at any time
721 actor.switchingweapon = actor.switchweapon;
723 else if (actor.weaponentity.state == WS_READY)
726 actor.switchingweapon = actor.switchweapon;
727 entity oldwep = get_weaponinfo(actor.weapon);
729 // set up weapon switch think in the future, and start drop anim
730 #ifndef INDEPENDENT_ATTACK_FINISHED
731 if(ATTACK_FINISHED(actor) <= time + actor.weapon_frametime * 0.5)
734 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
735 actor.weaponentity.state = WS_DROP;
736 weapon_thinkf(actor, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
737 #ifndef INDEPENDENT_ATTACK_FINISHED
743 // LordHavoc: network timing test code
745 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor)), " >= ", ftos(actor.weapon_nextthink), "\n");
750 // call the think code which may fire the weapon
751 // and do so multiple times to resolve framerate dependency issues if the
752 // server framerate is very low and the weapon fire rate very high
755 while (c < W_TICSPERFRAME)
758 if(w && !(actor.weapons & WepSet_FromWeapon(w)))
760 if(actor.weapon == actor.switchweapon)
761 W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
770 bool key_pressed = actor.BUTTON_HOOK;
771 Weapon wpn = actor.offhand;
772 if (wpn.offhand_think) wpn.offhand_think(wpn, actor, key_pressed);
776 Weapon e = get_weaponinfo(actor.weapon);
777 e.wr_think(e, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
779 Weapon w = get_weaponinfo(actor.weapon);
783 if (time + actor.weapon_frametime * 0.5 >= actor.weapon_nextthink)
785 if(actor.weapon_think)
790 Weapon wpn = get_weaponinfo(actor.weapon);
791 actor.weapon_think(wpn, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
794 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
799 void W_AttachToShotorg(entity actor, entity flash, vector offset)
803 flash.angles_z = random() * 360;
805 if(gettagindex(actor.weaponentity, "shot"))
806 setattachment(flash, actor.weaponentity, "shot");
808 setattachment(flash, actor.weaponentity, "tag_shot");
809 setorigin(flash, offset);
812 copyentity(flash, xflash);
814 flash.viewmodelforclient = actor;
816 if(actor.weaponentity.oldorigin.x > 0)
818 setattachment(xflash, actor.exteriorweaponentity, "");
819 setorigin(xflash, actor.weaponentity.oldorigin + offset);
823 if(gettagindex(actor.exteriorweaponentity, "shot"))
824 setattachment(xflash, actor.exteriorweaponentity, "shot");
826 setattachment(xflash, actor.exteriorweaponentity, "tag_shot");
827 setorigin(xflash, offset);
831 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
834 if(cvar("g_overkill"))
835 if(actor.ok_use_ammocharge)
837 ok_DecreaseCharge(actor, actor.weapon);
841 if((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo)
844 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
845 if(wep.reloading_ammo)
847 actor.clip_load -= ammo_use;
848 actor.(weapon_load[actor.weapon]) = actor.clip_load;
850 else if(wep.ammo_field != ammo_none)
852 actor.(wep.ammo_field) -= ammo_use;
853 if(actor.(wep.ammo_field) < 0)
856 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
857 "Please notify Samual immediately with a copy of this backtrace!\n",
860 GetAmmoPicture(wep.ammo_field),
862 actor.(wep.ammo_field)
868 // weapon reloading code
870 .float reload_ammo_amount, reload_ammo_min, reload_time;
871 .float reload_complain;
872 .string reload_sound;
874 void W_ReloadedAndReady(Weapon thiswep, entity actor, bool fire1, bool fire2)
876 // finish the reloading process, and do the ammo transfer
878 actor.clip_load = actor.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
880 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
881 if(!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
882 actor.clip_load = actor.reload_ammo_amount;
885 // make sure we don't add more ammo than we have
886 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
887 actor.clip_load += load;
888 actor.(actor.ammo_field) -= load;
890 actor.(weapon_load[actor.weapon]) = actor.clip_load;
892 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
893 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
894 // so your weapon is disabled for a few seconds without reason
896 //ATTACK_FINISHED(actor) -= actor.reload_time - 1;
898 Weapon wpn = get_weaponinfo(actor.weapon);
899 w_ready(wpn, actor, actor.BUTTON_ATCK, actor.BUTTON_ATCK2);
902 void W_Reload(entity actor, float sent_ammo_min, string sent_sound)
904 // set global values to work with
906 e = get_weaponinfo(actor.weapon);
908 if(cvar("g_overkill"))
909 if(actor.ok_use_ammocharge)
912 actor.reload_ammo_min = sent_ammo_min;
913 actor.reload_ammo_amount = e.reloading_ammo;
914 actor.reload_time = e.reloading_time;
915 actor.reload_sound = sent_sound;
917 // don't reload weapons that don't have the RELOADABLE flag
918 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
920 LOG_TRACE("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
924 // return if reloading is disabled for this weapon
925 if(!actor.reload_ammo_amount)
928 // our weapon is fully loaded, no need to reload
929 if (actor.clip_load >= actor.reload_ammo_amount)
932 // no ammo, so nothing to load
933 if(actor.ammo_field != ammo_none)
934 if(!actor.(actor.ammo_field) && actor.reload_ammo_min)
935 if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
937 if(IS_REAL_CLIENT(actor) && actor.reload_complain < time)
939 play2(actor, SND(UNAVAILABLE));
940 sprint(actor, strcat("You don't have enough ammo to reload the ^2", WEP_NAME(actor.weapon), "\n"));
941 actor.reload_complain = time + 1;
943 // switch away if the amount of ammo is not enough to keep using this weapon
944 Weapon w = get_weaponinfo(actor.weapon);
945 if (!(w.wr_checkammo1(w) + w.wr_checkammo2(w)))
947 actor.clip_load = -1; // reload later
948 W_SwitchToOtherWeapon(actor);
953 if (actor.weaponentity)
955 if (actor.weaponentity.wframe == WFRAME_RELOAD)
958 // allow switching away while reloading, but this will cause a new reload!
959 actor.weaponentity.state = WS_READY;
962 // now begin the reloading process
964 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
966 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
967 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
968 // so your weapon is disabled for a few seconds without reason
970 //ATTACK_FINISHED(actor) = max(time, ATTACK_FINISHED(actor)) + actor.reload_time + 1;
972 weapon_thinkf(actor, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
974 if(actor.clip_load < 0)
976 actor.old_clip_load = actor.clip_load;
977 actor.clip_load = actor.(weapon_load[actor.weapon]) = -1;
980 void W_DropEvent(.void(Weapon) event, entity player, float weapon_type, entity weapon_item)
983 weapon_dropevent_item = weapon_item;
984 Weapon w = get_weaponinfo(weapon_type);