2 ===========================================================================
4 CLIENT WEAPONSYSTEM CODE
5 Bring back W_Weaponframe
7 ===========================================================================
10 .float weapon_frametime;
12 float W_WeaponRateFactor()
15 t = 1.0 / g_weaponratefactor;
20 // VorteX: static frame globals
21 float WFRAME_DONTCHANGE = -1;
22 float WFRAME_FIRE1 = 0;
23 float WFRAME_FIRE2 = 1;
24 float WFRAME_IDLE = 2;
25 float WFRAME_RELOAD = 3;
28 void(float fr, float t, void() func) weapon_thinkf;
30 .float prevstrengthsound;
31 .float prevstrengthsoundattempt;
32 void W_PlayStrengthSound(entity player) // void W_PlayStrengthSound
34 if((player.items & IT_STRENGTH)
35 && ((time > player.prevstrengthsound + autocvar_sv_strengthsound_antispam_time) // prevent insane sound spam
36 || (time > player.prevstrengthsoundattempt + autocvar_sv_strengthsound_antispam_refire_threshold)))
38 sound(player, CH_TRIGGER, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
39 player.prevstrengthsound = time;
41 player.prevstrengthsoundattempt = time;
44 float CL_Weaponentity_CustomizeEntityForClient()
46 self.viewmodelforclient = self.owner;
48 if(other.enemy == self.owner)
49 self.viewmodelforclient = other;
56 * 1. simple animated model, muzzle flash handling on h_ model:
57 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
59 * shot = muzzle end (shot origin, also used for muzzle flashes)
60 * shell = casings ejection point (must be on the right hand side of the gun)
61 * weapon = attachment for v_tuba.md3
62 * v_tuba.md3 - first and third person model
63 * g_tuba.md3 - pickup model
65 * 2. simple animated model, muzzle flash handling on v_ model:
66 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
68 * weapon = attachment for v_tuba.md3
69 * v_tuba.md3 - first and third person model
71 * shot = muzzle end (shot origin, also used for muzzle flashes)
72 * shell = casings ejection point (must be on the right hand side of the gun)
73 * g_tuba.md3 - pickup model
75 * 3. fully animated model, muzzle flash handling on h_ model:
76 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
78 * shot = muzzle end (shot origin, also used for muzzle flashes)
79 * shell = casings ejection point (must be on the right hand side of the gun)
80 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
81 * v_tuba.md3 - third person model
82 * g_tuba.md3 - pickup model
84 * 4. fully animated model, muzzle flash handling on v_ model:
85 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
87 * shot = muzzle end (shot origin)
88 * shell = casings ejection point (must be on the right hand side of the gun)
89 * v_tuba.md3 - third person model
91 * shot = muzzle end (for muzzle flashes)
92 * g_tuba.md3 - pickup model
96 // self.origin, self.angles
98 // self.movedir, self.view_ofs
102 // call again with ""
104 void CL_WeaponEntity_SetModel(string name)
109 // if there is a child entity, hide it until we're sure we use it
110 if (self.weaponentity)
111 self.weaponentity.model = "";
112 setmodel(self, strcat("models/weapons/v_", name, ".md3")); // precision set below
113 v_shot_idx = gettagindex(self, "shot"); // used later
115 v_shot_idx = gettagindex(self, "tag_shot");
117 setmodel(self, strcat("models/weapons/h_", name, ".iqm")); // precision set below
118 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
119 self.anim_fire1 = animfixfps(self, '0 1 0.01', '0 0 0');
120 self.anim_fire2 = animfixfps(self, '1 1 0.01', '0 0 0');
121 self.anim_idle = animfixfps(self, '2 1 0.01', '0 0 0');
122 self.anim_reload = animfixfps(self, '3 1 0.01', '0 0 0');
124 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
125 // if we don't, this is a "real" animated model
126 if(gettagindex(self, "weapon"))
128 if (!self.weaponentity)
129 self.weaponentity = spawn();
130 setmodel(self.weaponentity, strcat("models/weapons/v_", name, ".md3")); // precision does not matter
131 setattachment(self.weaponentity, self, "weapon");
133 else if(gettagindex(self, "tag_weapon"))
135 if (!self.weaponentity)
136 self.weaponentity = spawn();
137 setmodel(self.weaponentity, strcat("models/weapons/v_", name, ".md3")); // precision does not matter
138 setattachment(self.weaponentity, self, "tag_weapon");
142 if(self.weaponentity)
143 remove(self.weaponentity);
144 self.weaponentity = world;
147 setorigin(self,'0 0 0');
148 self.angles = '0 0 0';
150 self.viewmodelforclient = world;
154 if(v_shot_idx) // v_ model attached to invisible h_ model
156 self.movedir = gettaginfo(self.weaponentity, v_shot_idx);
160 idx = gettagindex(self, "shot");
162 idx = gettagindex(self, "tag_shot");
164 self.movedir = gettaginfo(self, idx);
167 print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
168 self.movedir = '0 0 0';
172 if(self.weaponentity) // v_ model attached to invisible h_ model
174 idx = gettagindex(self.weaponentity, "shell");
176 idx = gettagindex(self.weaponentity, "tag_shell");
178 self.spawnorigin = gettaginfo(self.weaponentity, idx);
184 idx = gettagindex(self, "shell");
186 idx = gettagindex(self, "tag_shell");
188 self.spawnorigin = gettaginfo(self, idx);
191 print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
192 self.spawnorigin = self.movedir;
198 self.oldorigin = '0 0 0'; // use regular attachment
202 if(self.weaponentity)
204 idx = gettagindex(self, "weapon");
206 idx = gettagindex(self, "tag_weapon");
210 idx = gettagindex(self, "handle");
212 idx = gettagindex(self, "tag_handle");
216 self.oldorigin = self.movedir - gettaginfo(self, idx);
220 print("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");
221 self.oldorigin = '0 0 0'; // there is no way to recover from this
225 self.viewmodelforclient = self.owner;
230 if(self.weaponentity)
231 remove(self.weaponentity);
232 self.weaponentity = world;
233 self.movedir = '0 0 0';
234 self.spawnorigin = '0 0 0';
235 self.oldorigin = '0 0 0';
236 self.anim_fire1 = '0 1 0.01';
237 self.anim_fire2 = '0 1 0.01';
238 self.anim_idle = '0 1 0.01';
239 self.anim_reload = '0 1 0.01';
242 self.view_ofs = '0 0 0';
244 if(self.movedir_x >= 0)
248 self.movedir = shotorg_adjust(v0, FALSE, FALSE);
249 self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
251 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
252 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
254 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
256 // check if an instant weapon switch occurred
257 setorigin(self, self.view_ofs);
258 // reset animstate now
259 self.wframe = WFRAME_IDLE;
260 setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
263 vector CL_Weapon_GetShotOrg(float wpn)
267 wi = get_weaponinfo(wpn);
270 CL_WeaponEntity_SetModel(wi.mdl);
272 CL_WeaponEntity_SetModel("");
278 void CL_Weaponentity_Think()
281 self.nextthink = time;
282 if (intermission_running)
283 self.frame = self.anim_idle_x;
284 if (self.owner.weaponentity != self)
286 if (self.weaponentity)
287 remove(self.weaponentity);
291 if (self.owner.deadflag != DEAD_NO)
294 if (self.weaponentity)
295 self.weaponentity.model = "";
298 if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
300 self.weaponname = self.owner.weaponname;
301 self.dmg = self.owner.modelindex;
302 self.deadflag = self.owner.deadflag;
304 CL_WeaponEntity_SetModel(self.owner.weaponname);
307 tb = (self.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
308 self.effects = self.owner.effects & EFMASK_CHEAP;
309 self.effects &~= EF_LOWPRECISION;
310 self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
311 self.effects &~= EF_TELEPORT_BIT;
312 self.effects &~= EF_RESTARTANIM_BIT;
315 if(self.owner.alpha == default_player_alpha)
316 self.alpha = default_weapon_alpha;
317 else if(self.owner.alpha != 0)
318 self.alpha = self.owner.alpha;
322 self.glowmod = self.owner.weaponentity_glowmod;
323 self.colormap = self.owner.colormap;
324 if (self.weaponentity)
326 self.weaponentity.effects = self.effects;
327 self.weaponentity.alpha = self.alpha;
328 self.weaponentity.colormap = self.colormap;
329 self.weaponentity.glowmod = self.glowmod;
332 self.angles = '0 0 0';
334 float f = (self.owner.weapon_nextthink - time);
335 if (self.state == WS_RAISE && !intermission_running)
337 entity newwep = get_weaponinfo(self.owner.switchweapon);
338 f = f * g_weaponratefactor / max(f, newwep.switchdelay_raise);
339 //print(sprintf("CL_Weaponentity_Think(): cvar: %s, value: %f, nextthink: %f\n", sprintf("g_balance_%s_switchdelay_raise", newwep.netname), cvar(sprintf("g_balance_%s_switchdelay_raise", newwep.netname)), (self.owner.weapon_nextthink - time)));
340 self.angles_x = -90 * f * f;
342 else if (self.state == WS_DROP && !intermission_running)
344 entity oldwep = get_weaponinfo(self.owner.weapon);
345 f = 1 - f * g_weaponratefactor / max(f, oldwep.switchdelay_drop);
346 //print(sprintf("CL_Weaponentity_Think(): cvar: %s, value: %f, nextthink: %f\n", sprintf("g_balance_%s_switchdelay_drop", oldwep.netname), cvar(sprintf("g_balance_%s_switchdelay_drop", oldwep.netname)), (self.owner.weapon_nextthink - time)));
347 self.angles_x = -90 * f * f;
349 else if (self.state == WS_CLEAR)
352 self.angles_x = -90 * f * f;
356 void CL_ExteriorWeaponentity_Think()
359 self.nextthink = time;
360 if (self.owner.exteriorweaponentity != self)
365 if (self.owner.deadflag != DEAD_NO)
370 if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
372 self.weaponname = self.owner.weaponname;
373 self.dmg = self.owner.modelindex;
374 self.deadflag = self.owner.deadflag;
375 if (self.owner.weaponname != "")
376 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
380 if((tag_found = gettagindex(self.owner, "tag_weapon")))
382 self.tag_index = tag_found;
383 self.tag_entity = self.owner;
386 setattachment(self, self.owner, "bip01 r hand");
388 self.effects = self.owner.effects;
389 self.effects |= EF_LOWPRECISION;
390 self.effects = self.effects & EFMASK_CHEAP; // eat performance
391 if(self.owner.alpha == default_player_alpha)
392 self.alpha = default_weapon_alpha;
393 else if(self.owner.alpha != 0)
394 self.alpha = self.owner.alpha;
398 self.glowmod = self.owner.weaponentity_glowmod;
399 self.colormap = self.owner.colormap;
401 CSQCMODEL_AUTOUPDATE();
404 // spawning weaponentity for client
405 void CL_SpawnWeaponentity()
407 self.weaponentity = spawn();
408 self.weaponentity.classname = "weaponentity";
409 self.weaponentity.solid = SOLID_NOT;
410 self.weaponentity.owner = self;
411 setmodel(self.weaponentity, ""); // precision set when changed
412 setorigin(self.weaponentity, '0 0 0');
413 self.weaponentity.angles = '0 0 0';
414 self.weaponentity.viewmodelforclient = self;
415 self.weaponentity.flags = 0;
416 self.weaponentity.think = CL_Weaponentity_Think;
417 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
418 self.weaponentity.nextthink = time;
420 self.exteriorweaponentity = spawn();
421 self.exteriorweaponentity.classname = "exteriorweaponentity";
422 self.exteriorweaponentity.solid = SOLID_NOT;
423 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
424 self.exteriorweaponentity.owner = self;
425 setorigin(self.exteriorweaponentity, '0 0 0');
426 self.exteriorweaponentity.angles = '0 0 0';
427 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
428 self.exteriorweaponentity.nextthink = time;
431 entity oldself = self;
432 self = self.exteriorweaponentity;
433 CSQCMODEL_AUTOINIT();
441 if (self.weapon != -1)
444 self.switchingweapon = 0;
446 if (self.weaponentity)
448 self.weaponentity.state = WS_CLEAR;
449 self.weaponentity.effects = 0;
455 if (self.weaponentity)
456 self.weaponentity.state = WS_READY;
457 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
462 float weapon_prepareattack_checkammo(float secondary)
464 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
465 if (!WEP_ACTION(self.weapon, WR_CHECKAMMO1 + secondary))
467 // always keep the Mine Layer if we placed mines, so that we can detonate them
469 if(self.weapon == WEP_MINE_LAYER)
470 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
473 if(self.weapon == self.switchweapon && time - self.prevdryfire > 1) // only play once BEFORE starting to switch weapons
475 sound (self, CH_WEAPON_A, "weapons/dryfire.wav", VOL_BASE, ATTN_NORM);
476 self.prevdryfire = time;
479 if(WEP_ACTION(self.weapon, WR_CHECKAMMO2 - secondary)) // check if the other firing mode has enough ammo
481 if(time - self.prevwarntime > 1)
487 ITEM_WEAPON_PRIMORSEC,
493 self.prevwarntime = time;
495 else // this weapon is totally unable to fire, switch to another one
497 W_SwitchToOtherWeapon(self);
505 float weapon_prepareattack_check(float secondary, float attacktime)
507 if(!weapon_prepareattack_checkammo(secondary))
510 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
511 //if all players readied up and the countdown is running
512 if(time < game_starttime || time < self.race_penalty) {
516 if (timeout_status == TIMEOUT_ACTIVE) //don't allow the player to shoot while game is paused
519 // do not even think about shooting if switching
520 if(self.switchweapon != self.weapon)
525 // don't fire if previous attack is not finished
526 if (ATTACK_FINISHED(self) > time + self.weapon_frametime * 0.5)
528 // don't fire while changing weapon
529 if (self.weaponentity.state != WS_READY)
535 float weapon_prepareattack_do(float secondary, float attacktime)
537 self.weaponentity.state = WS_INUSE;
539 self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
541 // if the weapon hasn't been firing continuously, reset the timer
544 if (ATTACK_FINISHED(self) < time - self.weapon_frametime * 1.5)
546 ATTACK_FINISHED(self) = time;
547 //dprint("resetting attack finished to ", ftos(time), "\n");
549 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponRateFactor();
551 self.bulletcounter += 1;
552 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
555 float weapon_prepareattack(float secondary, float attacktime)
557 if(weapon_prepareattack_check(secondary, attacktime))
559 weapon_prepareattack_do(secondary, attacktime);
566 void weapon_thinkf(float fr, float t, void() func)
572 if(fr == WFRAME_DONTCHANGE)
574 fr = self.weaponentity.wframe;
577 else if (fr == WFRAME_IDLE)
586 if (self.weaponentity)
588 self.weaponentity.wframe = fr;
590 if (fr == WFRAME_IDLE)
591 a = self.weaponentity.anim_idle;
592 else if (fr == WFRAME_FIRE1)
593 a = self.weaponentity.anim_fire1;
594 else if (fr == WFRAME_FIRE2)
595 a = self.weaponentity.anim_fire2;
596 else // if (fr == WFRAME_RELOAD)
597 a = self.weaponentity.anim_reload;
598 a_z *= g_weaponratefactor;
599 setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
606 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
608 backtrace("Tried to override initial weapon think function - should this really happen?");
611 t *= W_WeaponRateFactor();
613 // VorteX: haste can be added here
614 if (self.weapon_think == w_ready)
616 self.weapon_nextthink = time;
617 //dprint("started firing at ", ftos(time), "\n");
619 if (self.weapon_nextthink < time - self.weapon_frametime * 1.5 || self.weapon_nextthink > time + self.weapon_frametime * 1.5)
621 self.weapon_nextthink = time;
622 //dprint("reset weapon animation timer at ", ftos(time), "\n");
624 self.weapon_nextthink = self.weapon_nextthink + t;
625 self.weapon_think = func;
626 //dprint("next ", ftos(self.weapon_nextthink), "\n");
628 if((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
630 if(self.weapon == WEP_SHOTGUN && fr == WFRAME_FIRE2)
631 animdecide_setaction(self, ANIMACTION_MELEE, restartanim);
633 animdecide_setaction(self, ANIMACTION_SHOOT, restartanim);
637 if(self.anim_upper_action == ANIMACTION_SHOOT || self.anim_upper_action == ANIMACTION_MELEE)
638 self.anim_upper_action = 0;
642 float forbidWeaponUse()
644 if(time < game_starttime && !autocvar_sv_ready_restart_after_countdown)
646 if(round_handler_IsActive() && !round_handler_IsRoundStarted())
648 if(self.player_blocked)
650 if(self.freezetag_frozen)
660 self.weapon_frametime = frametime;
662 if (!self.weaponentity || self.health < 1)
663 return; // Dead player can't use weapons and injure impulse commands
665 if(forbidWeaponUse())
666 if(self.weaponentity.state != WS_CLEAR)
672 if(!self.switchweapon)
675 self.switchingweapon = 0;
676 self.weaponentity.state = WS_CLEAR;
677 self.weaponname = "";
678 self.items &~= IT_AMMO;
682 makevectors(self.v_angle);
683 fo = v_forward; // save them in case the weapon think functions change it
688 if (self.weapon != self.switchweapon)
690 if (self.weaponentity.state == WS_CLEAR)
693 self.switchingweapon = self.switchweapon;
694 entity newwep = get_weaponinfo(self.switchweapon);
696 self.items &~= IT_AMMO;
697 self.items = self.items | (newwep.items & IT_AMMO);
699 // the two weapon entities will notice this has changed and update their models
700 self.weapon = self.switchweapon;
701 self.weaponname = newwep.mdl;
702 self.bulletcounter = 0; // WEAPONTODO
703 WEP_ACTION(self.switchweapon, WR_SETUP);
704 self.weaponentity.state = WS_RAISE;
706 // set our clip load to the load of the weapon we switched to, if it's reloadable
707 if(newwep.spawnflags & WEP_FLAG_RELOADABLE && cvar(strcat("g_balance_", newwep.netname, "_reload_ammo"))) // prevent accessing undefined cvars
709 self.clip_load = self.(weapon_load[self.switchweapon]);
710 self.clip_size = cvar(strcat("g_balance_", newwep.netname, "_reload_ammo"));
713 self.clip_load = self.clip_size = 0;
715 // VorteX: add player model weapon select frame here
716 // setcustomframe(PlayerWeaponRaise);
717 weapon_thinkf(WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
718 //print(sprintf("W_WeaponFrame(): cvar: %s, value: %f\n", sprintf("g_balance_%s_switchdelay_raise", newwep.netname), cvar(sprintf("g_balance_%s_switchdelay_raise", newwep.netname))));
720 else if (self.weaponentity.state == WS_DROP)
722 // in dropping phase we can switch at any time
723 self.switchingweapon = self.switchweapon;
725 else if (self.weaponentity.state == WS_READY)
728 self.switchingweapon = self.switchweapon;
730 entity oldwep = get_weaponinfo(self.weapon);
732 #ifndef INDEPENDENT_ATTACK_FINISHED
733 if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
736 sound (self, CH_WEAPON_SINGLE, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
737 self.weaponentity.state = WS_DROP;
738 // set up weapon switch think in the future, and start drop anim
739 weapon_thinkf(WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
740 //print(sprintf("W_WeaponFrame(): cvar: %s, value: %f\n", sprintf("g_balance_%s_switchdelay_drop", oldwep.netname), cvar(sprintf("g_balance_%s_switchdelay_drop", oldwep.netname))));
741 #ifndef INDEPENDENT_ATTACK_FINISHED
747 // LordHavoc: network timing test code
749 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
754 // call the think code which may fire the weapon
755 // and do so multiple times to resolve framerate dependency issues if the
756 // server framerate is very low and the weapon fire rate very high
759 while (c < W_TICSPERFRAME)
762 if(w && !WEPSET_CONTAINS_EW(self, w))
764 if(self.weapon == self.switchweapon)
765 W_SwitchWeapon_Force(self, w_getbestweapon(self));
774 WEP_ACTION(self.weapon, WR_THINK);
776 WEP_ACTION(self.weapon, WR_GONETHINK);
778 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
780 if(self.weapon_think)
788 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
793 if (self.items & IT_CELLS)
794 self.currentammo = self.ammo_cells;
795 else if (self.items & IT_ROCKETS)
796 self.currentammo = self.ammo_rockets;
797 else if (self.items & IT_NAILS)
798 self.currentammo = self.ammo_nails;
799 else if (self.items & IT_SHELLS)
800 self.currentammo = self.ammo_shells;
802 self.currentammo = 1;
806 void W_AttachToShotorg(entity flash, vector offset)
810 flash.angles_z = random() * 360;
812 if(gettagindex(self.weaponentity, "shot"))
813 setattachment(flash, self.weaponentity, "shot");
815 setattachment(flash, self.weaponentity, "tag_shot");
816 setorigin(flash, offset);
819 copyentity(flash, xflash);
821 flash.viewmodelforclient = self;
823 if(self.weaponentity.oldorigin_x > 0)
825 setattachment(xflash, self.exteriorweaponentity, "");
826 setorigin(xflash, self.weaponentity.oldorigin + offset);
830 if(gettagindex(self.exteriorweaponentity, "shot"))
831 setattachment(xflash, self.exteriorweaponentity, "shot");
833 setattachment(xflash, self.exteriorweaponentity, "tag_shot");
834 setorigin(xflash, offset);
838 void W_DecreaseAmmo(.float ammo_type, float ammo_use, float ammo_reload) // WEAPONTODO: why does this have ammo_type?
840 if((self.items & IT_UNLIMITED_WEAPON_AMMO) && !ammo_reload)
843 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
846 self.clip_load -= ammo_use;
847 self.(weapon_load[self.weapon]) = self.clip_load;
850 self.(self.current_ammo) -= ammo_use;
853 // weapon reloading code
855 .float reload_ammo_amount, reload_ammo_min, reload_time;
856 .float reload_complain;
857 .string reload_sound;
859 void W_ReloadedAndReady()
861 // finish the reloading process, and do the ammo transfer
863 self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
865 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
866 if(!self.reload_ammo_min || self.items & IT_UNLIMITED_WEAPON_AMMO)
867 self.clip_load = self.reload_ammo_amount;
870 while(self.clip_load < self.reload_ammo_amount && self.(self.current_ammo)) // make sure we don't add more ammo than we have
873 self.(self.current_ammo) -= 1;
876 self.(weapon_load[self.weapon]) = self.clip_load;
878 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
879 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
880 // so your weapon is disabled for a few seconds without reason
882 //ATTACK_FINISHED(self) -= self.reload_time - 1;
887 void W_Reload(float sent_ammo_min, float sent_ammo_amount, float sent_time, string sent_sound)
889 // set global values to work with
891 self.reload_ammo_min = sent_ammo_min;
892 self.reload_ammo_amount = sent_ammo_amount;
893 self.reload_time = sent_time;
894 self.reload_sound = sent_sound;
896 // check if we meet the necessary conditions to reload
899 e = get_weaponinfo(self.weapon);
901 // don't reload weapons that don't have the RELOADABLE flag
902 if not(e.spawnflags & WEP_FLAG_RELOADABLE)
904 dprint("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
908 // return if reloading is disabled for this weapon
909 if(!self.reload_ammo_amount)
912 // our weapon is fully loaded, no need to reload
913 if (self.clip_load >= self.reload_ammo_amount)
916 // no ammo, so nothing to load
917 if(!self.(self.current_ammo) && self.reload_ammo_min)
918 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
920 if(IS_REAL_CLIENT(self) && self.reload_complain < time)
922 play2(self, "weapons/unavailable.wav");
923 sprint(self, strcat("You don't have enough ammo to reload the ^2", W_Name(self.weapon), "\n"));
924 self.reload_complain = time + 1;
926 // switch away if the amount of ammo is not enough to keep using this weapon
927 if not(WEP_ACTION(self.weapon, WR_CHECKAMMO1) + WEP_ACTION(self.weapon, WR_CHECKAMMO2))
929 self.clip_load = -1; // reload later
930 W_SwitchToOtherWeapon(self);
935 if (self.weaponentity)
937 if (self.weaponentity.wframe == WFRAME_RELOAD)
940 // allow switching away while reloading, but this will cause a new reload!
941 self.weaponentity.state = WS_READY;
944 // now begin the reloading process
946 sound (self, CH_WEAPON_SINGLE, self.reload_sound, VOL_BASE, ATTN_NORM);
948 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
949 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
950 // so your weapon is disabled for a few seconds without reason
952 //ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self)) + self.reload_time + 1;
954 weapon_thinkf(WFRAME_RELOAD, self.reload_time, W_ReloadedAndReady);
956 if(self.clip_load < 0)
958 self.old_clip_load = self.clip_load;
959 self.clip_load = self.(weapon_load[self.weapon]) = -1;