#include ww2x\_wwx_sprintsystem;
#include maps\mp\gametypes\_weapons;
#include ww2x\_drm;

loadout()
{
	// mainly required for roundbased games!
	self setWeaponArray();

	// weapon checks, if weapon = ignore or the weapon is undfefined, set to "none"
	if((isDefined(self.pers["weapon"]) && self.pers["weapon"] == "ignore") || !isDefined(self.pers["weapon"])) self.pers["weapon"] = "none";
	if((isDefined(self.pers["weapon1"]) && self.pers["weapon1"] == "ignore") || !isDefined(self.pers["weapon1"])) self.pers["weapon1"] = "none";
	if(level.wwx_wepo_secondary && (isDefined(self.pers["weapon2"]) && self.pers["weapon2"] == "ignore") || !isDefined(self.pers["weapon2"])) self.pers["weapon2"] = "none";

	self.pers["spawnweapon"] = self.pers["weapon"];

	// pistol allowed?
	allowpistol = true;
	if(level.wwx_wepo_secondary && !level.wwx_wepo_class) allowpistol = false; 	// secondary weapon system on, all weapons available - pistol is handled differently
	else if(!level.wwx_wepo_secondary && !level.wwx_wepo_class && !level.wwx_wepo_sidearm) allowpistol = false; // secondary weapons off, all weapons available - no pistols allowed
	else if(level.wwx_wepo_class == 1) allowpistol = false;	// weapon classbased pistols only - pistol is handled differently
	else if(level.wwx_wepo_class >= 2 && !level.wwx_wepo_sidearm) allowpistol = false;	// weapon class based with sidearm - pistols allowed

	// give them the pistol is it's allowed
	if(allowpistol) self givePistol();

	// set the client status for the weapons
	self setWeaponClientStatus(false);

	// set the ammo for the weapons
	self setAmmo("primary", false, true);
	self setAmmo("primaryb", false, true);

	// self explanitory!
	self giveFirstAid();
      if(level.wwx_firenades)
      {
	self giveFireGrenades();
      }
      else if(level.wwx_gasnades)
      {
      self giveGasGrenades();
      }
      else if(level.wwx_stickynades)
      {
      self giveStickyGrenades();
      }
      else if(level.wwx_satchels)
      {
      self giveSatchelGrenades();
      }
      else
      {
	self giveGrenades();
      }

	self giveBinoculars();

	// set up the spawning weapons
	self setSpawnWeapons();

	// start weapon/slot monitors
	self thread weaponChangeMonitor();
	self thread oldWeaponMonitor();
	self thread slotWeaponMonitor("primary");
	self thread slotWeaponMonitor("primaryb");

		self thread slotDummyChecker("primary");
		self thread slotDummyChecker("primaryb");
}

setAmmo(slot, gts, spawning)
{
	self endon("disconnect");

	// if not spawning, default false
	if(!isDefined(spawning)) spawning = false;

	// if not gametype start delay, default false
	if(!isDefined(gts)) gts = false;

	weapon = self getWeaponSlotWeapon(slot);

	// sprinting, none or ignore?
	if(!isValidWeapon(weapon)) return;

	clip = self getWeaponSlotClipAmmoDefault(weapon);
	reserve = self getWeaponSlotAmmoDefault(weapon);

	// rank system reserve ammo override
	if(level.wwx_wepo_loadout == 1)
	{
		if(!isWeaponType(weapon, "pistol")) rank_suffix = game["rank_ammo_gunclips_" + self.pers["rank"]];
		else rank_suffix = game["rank_ammo_pistolclips_" + self.pers["rank"]];

		reserve = clip * rank_suffix;
	}

	if(spawning)
	{
		// do nothing
	}
	else if(!gts)
	{
		// compare the ammo the weapon already has, if its greater, just fill the clip!
		reserve_check = self getWeaponSlotAmmo(slot);
		if(reserve_check > reserve) reserve = reserve_check;
	}

	self setWeaponSlotAmmo(slot, reserve);
	self setWeaponSlotClipAmmo(slot, clip);
}

setSpawnWeapons()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	if(!isDefined(self.pers["weapon"]) || !isDefined(self.pers["weapon1"])) wait 0.05;

	primary = self getWeaponSlotWeapon("primary");

	// save primary	
	if(isDefined(primary) && primary != "none")
	{
		self.weapon["primary"].name = primary;
		self.weapon["primary"].clip = self getWeaponSlotClipAmmo("primary");
		self.weapon["primary"].reserve= self getWeaponSlotAmmo("primary");
		self.weapon["primary"].maxammo = self.weapon["primary"].clip + self.weapon["primary"].reserve;
	}
	else
	{
		self.weapon["primary"].name = "ignore";
		self.weapon["primary"].clip = 0;
		self.weapon["primary"].reserve= 0;
		self.weapon["primary"].maxammo = 0;
	}		

	primaryb = self getWeaponSlotWeapon("primaryb");

	// save secondary	
	if(isDefined(primaryb) && primaryb != "none")
	{
		self.weapon["primaryb"].name = primaryb;
		self.weapon["primaryb"].clip = self getWeaponSlotClipAmmo("primaryb");
		self.weapon["primaryb"].reserve = self getWeaponSlotAmmo("primaryb");
		self.weapon["primaryb"].maxammo = self.weapon["primaryb"].clip + self.weapon["primaryb"].reserve;
	}
	else
	{
		self.weapon["primaryb"].name = "ignore";
		self.weapon["primaryb"].clip = 0;
		self.weapon["primaryb"].reserve= 0;
		self.weapon["primaryb"].maxammo = 0;
	}

	// if using secondary weapons with pistols, give them a pistol
	if(level.wwx_wepo_secondary &&  level.wwx_wepo_sec_pistols)
	{
		self setWeaponSlotWeapon("primaryb", "none");
		self givePistol();
	
		// save pistol	
		self.weapon["virtual"].name = self getWeaponSlotWeapon("primaryb");
		self.weapon["virtual"].clip = self getWeaponSlotClipAmmo("primaryb");
		self.weapon["virtual"].reserve = self getWeaponSlotAmmo("primaryb");
		self.weapon["virtual"].maxammo = self.weapon["virtual"].clip + self.weapon["virtual"].reserve;
	
		// put the original secondary to the primaryb slot
		if(self.weapon["primaryb"].name != "none" && self.weapon["primaryb"].name != "ignore")
		{
			self setWeaponSlotWeapon("primaryb", self.weapon["primaryb"].name);	
			self setWeaponSlotAmmo("primaryb", self.weapon["primaryb"].reserve);
			self setWeaponSlotClipAmmo("primaryb", self.weapon["primaryb"].clip);
		}
	}
	else
	{
		self.weapon["virtual"].name = "ignore";
		self.weapon["virtual"].clip = 0;
		self.weapon["virtual"].reserve = 0;
		self.weapon["virtual"].maxammo = 0;
	}

	// setup the current primary weapon
	self.weapon["oldprimary"].name = self getWeaponSlotWeapon("primary");
	self.weapon["oldprimary"].clip = self getWeaponSlotClipAmmo("primary");
	self.weapon["oldprimary"].reserve = self getWeaponSlotAmmo("primary");

	// setup the current primaryb weapon
	self.weapon["oldprimaryb"].name = self getWeaponSlotWeapon("primaryb");
	self.weapon["oldprimaryb"].clip = self getWeaponSlotClipAmmo("primaryb");
	self.weapon["oldprimaryb"].reserve = self getWeaponSlotAmmo("primaryb");

	self.weaponin["primary"].slot = "primary";
	self.weaponin["primaryb"].slot = "primaryb";

	if(!isDefined(self.pers["pistol"])) self.pers["pistol"] = "";
}

weaponChangeMonitor()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	for(;;)
	{
		while(isPlayer(self) && !self.wwx_stopwepmon && self.sessionstate == "playing")
		{
			wait 0.05;

			// set the ammo for these slots
			self thread checkAmmo("primary");
			self thread checkAmmo("primaryb");

			// get the current weapon
			current = self getCurrentWeapon();

			if(current == "sprint_mp" || current == self.weapon["current"].name) continue;

			// get the current primary and primaryb
			primary = self whatsInSlot("primary");
			primaryb = self whatsInSlot("primaryb");

			if(current != self.weapon["current"].name)
			{
				if(primary != self.weapon[self.weaponin["primary"].slot].name)
				{
					self notify("primary_dummycheck");
					wait 0.05;
					if(isPlayer(self)) self notify("primary_changed");
				}

				if(primaryb != self.weapon[self.weaponin["primaryb"].slot].name)
				{
					self notify("primaryb_dummycheck");
					wait 0.05;
					if(isPlayer(self)) self notify("primaryb_changed");
				}
			}

			if(isPlayer(self) && isDefined(current))
			{
				if(current != self.weapon["current"].name && current != "ignore" && (current == self.weapon["oldprimary"].name || current == self.weapon["oldprimaryb"].name))
				{
					// if secondary weapons with pistols, switch the weapons around
					if(level.wwx_wepo_secondary &&  level.wwx_wepo_sec_pistols) self thread switchWeapons(self.weapon["current"].name, current);
					else self notify("weaponsave");
				}
			}
		}

		wait 0.05;
	}
}

whatsInSlot(slot)
{
	slotweapon = self getWeaponSlotWeapon(slot);

	if(slotweapon != "none" && slotweapon != "sprint_mp")
	{
		if(slotweapon == self.weapon["primary"].name) self.weaponin[slot].slot = "primary";
		else if(slotweapon == self.weapon["primaryb"].name) self.weaponin[slot].slot = "primaryb";
		else if(slotweapon == self.weapon["virtual"].name) self.weaponin[slot].slot = "virtual";
	}

	return slotweapon;
}

slotDummyChecker(slot)
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	for(;;)
	{
		while(isPlayer(self) && !self.wwx_stopwepmon)
		{
			self waittill(slot + "_dummycheck");

			weapon = self getWeaponSlotWeapon(slot);

			// have they dropped their weapon?
			if(weapon == "none")
			{
				self saveNewSlot(slot, self.weaponin[slot].slot, true);
				continue;
			}
		}

		wait 0.05;
	}
}

switchWeapons(current, newcurrent)
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	// ignore weapon cannot be switch so return
	if(current == "ignore" || newcurrent == "ignore") return;

	if(level.wwx_roundbased)
	{
		game[self.name]["weapon1"] = self.weapon["primary"].name;
		game[self.name]["weapon2"] = self.weapon["primaryb"].name;
	}

	if(current == self.weapon["primary"].name && isPrimary(self.weapon["primary"].name) && newcurrent == self.weapon["primaryb"].name && isSecondary(self.weapon["primaryb"].name)) self changeWeaponInSlot("primary", "virtual");
	else if(current == self.weapon["primaryb"].name && isSecondary(self.weapon["primaryb"].name) && newcurrent == self.weapon["virtual"].name && isPrimary(self.weapon["virtual"].name)) self changeWeaponInSlot("primaryb", "primary");
	else if(current == self.weapon["virtual"].name && isPrimary(self.weapon["virtual"].name) && newcurrent == self.weapon["primary"].name && isSecondary(self.weapon["primary"].name)) self changeWeaponInSlot("primary", "primaryb");
	else if(current == self.weapon["primary"].name && isSecondary(self.weapon["primary"].name) && newcurrent == self.weapon["primaryb"].name && isPrimary(self.weapon["primaryb"].name)) self changeWeaponInSlot("primaryb", "virtual");
	else if(current == self.weapon["primaryb"].name && isPrimary(self.weapon["primaryb"].name) && newcurrent == self.weapon["virtual"].name && isSecondary(self.weapon["virtual"].name)) self changeWeaponInSlot("primary", "primary");
	else if(current == self.weapon["virtual"].name && isSecondary(self.weapon["virtual"].name) && newcurrent == self.weapon["primary"].name && isPrimary(self.weapon["primary"].name)) self changeWeaponInSlot("primaryb", "primaryb");
}

slotWeaponMonitor(slot)
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	wepname = "";
	oldweapon = self.weapon["current"].name;
	wasdummy = false;
	changepistol = false;

	oldammo = self.weapon[self.weaponin[slot].slot].clip + self.weapon[self.weaponin[slot].slot].reserve;

	for(;;)
	{
		wait 0.05;
		
		while(isPlayer(self) && self.sessionstate == "playing" && !self.wwx_stopwepmon)
		{
			self waittill(slot + "_changed");
	
			weapon = self getWeaponSlotWeapon(slot);

			if(!isDefined(weapon) || weapon == "none" || weapon == "ignore" || weapon == "sprint_mp") continue;
	
			if(self.wwx_stopwepmon || self.wwx_sprinting || weapon == self.weapon["oldprimary"].name || weapon == self.weapon["oldprimaryb"].name)
		 	{
		 		self notify(slot + "_checked");
				oldammo = self.weapon[self.weaponin[slot].slot].clip + self.weapon[self.weaponin[slot].slot].reserve;
		 		continue;
		 	}
	
			enemyweapon = false; // default status - we'll asume its not an enemy weapon
			allowedweapon = true; // default status - we'll asume this is allowed for now
		
			// is this an enemy weapon?
			for(i = 0; i < level.weaponnames.size; i++)
			{
				if(weapon == level.weaponnames[i])
				{
					if(self.pers["team"] != level.weapons[level.weaponnames[i]].team && level.weapons[level.weaponnames[i]].team != "all")
					{
						enemyweapon = true;
						break;
					}
				}
			}
	
			// it is an enemy weapon, are we allowed to have it?
			if(enemyweapon)
			{
				// enemy weapons are allowed period
				if(level.wwx_wepo_enemy == 1) // enemy weapons are allowed only if you are low on ammo
				{
					// this is an enemy weapon and is allowed if last weapon was low was low on ammo
					if(oldammo > int(self.weapon[self.weaponin[slot].slot].maxammo / 100) * level.wwx_wepo_cent) allowedweapon = false;
	
					// check for dummy
					if(isDummy(self.weapon[self.weaponin[slot].slot].name))
					{
						wasdummy = true;
						allowedweapon = false;
					}
					else
					{
						oldweapon = self.weapon["current"].name;
						wasdummy = false;
					}
				}
				else if(level.wwx_wepo_enemy == 2) allowedweapon = false;	// enemy weapons are not allowed
			}
			// check if this weapon is a pistol, and is allowed to be exchanged
			if(level.wwx_wepo_sidearm == 1 && weapon != self.pers["pistol"])
			{
				if(!level.wwx_wepo_secondary && self.weaponin[slot].slot == "primaryb" || level.wwx_wepo_secondary && self.weaponin[slot].slot == "virtual")
				{
					enemyweapon = false;
					changepistol = false;
					allowedweapon = false;
					oldweapon = self.pers["pistol"];
				}
				else changepistol = true;
			}
			else changepistol = true;

			if(!allowedweapon)
			{
				// drop the weapon and replace with dummy weapon
				self dropItem(self getWeaponSlotWeapon(slot));
				self saveNewSlot(slot, self.weaponin[slot].slot, true);
	
				// get the name of the weapon they dropped
				if(level.wwx_wepo_enemy == 1 || level.wwx_wepo_sidearm == 1 && !changepistol)
				{
					if(wasdummy) wepname = getWeaponName(oldweapon);
					else wepname = getWeaponName(oldweapon);
				}
	
				// show them a message why they can't have that weapon
				if(!changepistol && oldweapon == self.pers["pistol"])
				{
					self iprintlnbold(&"CUSTOM_ADMIN_NAME", &"WEAPON_PISTOL_SWAP_NO_MSG1");
					self iprintlnbold(&"WEAPON_PISTOL_SWAP_NO_MSG2", wepname);
				}
				else if(level.wwx_wepo_enemy == 1)
				{
					self iprintlnbold(&"CUSTOM_ADMIN_NAME", &"EWEAPON_AMMO_MSG1");
					self iprintlnbold(&"EWEAPON_AMMO_MSG2", wepname);
				}
				else if(level.wwx_wepo_enemy == 2) self iprintlnbold(&"EWEAPON_DISABLED");
			}
			else self saveNewSlot(slot, self.weaponin[slot].slot, false);
	
	 		self notify(slot + "_checked");
		}
	}
}

dropCurrentWeapon()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	if(isPlayer(self))
	{
		current = self getCurrentWeapon();
		if(isDummy(current) || current == "none" || current == "ignore") return;
		self dropItem(current);
	}
}

saveNewSlot(slot, wepinslot, dummy)
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	// double check!
	new_weapon = self getWeaponSlotWeapon(slot);

	if(dummy || new_weapon == "none")
	{
		dummywep = getDummy();

		// if the current slot contains a dummy weapon, give them it back!
		if(!isDefined(dummywep) && isDummy(self getWeaponSlotWeapon(slot)))
			dummywep = self getWeaponSlotWeapon(slot);

		// set the empty slot to a dummy weapon if secondary weapons enabled with pistol
		self setWeaponSlotWeapon(slot, dummywep);	
		self setWeaponSlotClipAmmo(slot, 0);
		self setWeaponSlotAmmo(slot, 0);

		// save the dummy to the weapon array	
		self.weapon[wepinslot].name = dummywep;
		self.weapon[wepinslot].clip = 0;
		self.weapon[wepinslot].reserve = 0;
		self.weapon[wepinslot].maxammo = 0;

		// switch to new dummy weapon and save as old current
		self switchToWeapon(dummywep);
		self.weapon["current"].name = dummywep;
	}
	else
	{
		// add new weapon to the weapon array
		self.weapon[wepinslot].name = self getWeaponSlotWeapon(slot);
		self.weapon[wepinslot].clip = self getWeaponSlotClipAmmo(slot);
		self.weapon[wepinslot].reserve = self getWeaponSlotAmmo(slot);
		self.weapon[wepinslot].maxammo = self.weapon[wepinslot].clip + self.weapon[wepinslot].reserve;

		// switch to the new weapon and save as old current
		self switchToWeapon(self.weapon[wepinslot].name);
		self.weapon["current"].name = self.weapon[wepinslot].name;
	}

	// save the new weapons for sprint system
	self notify("weaponsave");
}

oldWeaponMonitor()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	// setup/save the current weapon
	self.weapon["current"].name = self getCurrentWeapon();

	while(isPlayer(self) && self.sessionstate == "playing")
	{
		self waittill("weaponsave");
		
		cw = self getCurrentWeapon();
		
		if(cw != "sprint_mp" && cw != "none")
		{
			self.weapon["current"].name = cw;
		}

		// save the current primary for safekeeping
		primary = self getWeaponSlotWeapon("primary");
		if(primary != "sprint_mp" && primary != "none")
		{
			self.weapon["oldprimary"].name = primary;
			self.weapon["oldprimary"].clip = self getWeaponSlotClipAmmo("primary");
			self.weapon["oldprimary"].reserve = self getWeaponSlotAmmo("primary");
		}

		// save the current primaryb for safekeeping
		primaryb = self getWeaponSlotWeapon("primaryb");
		if(primaryb != "sprint_mp" && primaryb != "none")
		{	
			self.weapon["oldprimaryb"].name = primaryb;
			self.weapon["oldprimaryb"].clip = self getWeaponSlotClipAmmo("primaryb");
			self.weapon["oldprimaryb"].reserve = self getWeaponSlotAmmo("primaryb");
		}

		self notify("weaponsaved");
	}
}

changeWeaponInSlot(slot, weaponslot)
{
	// ignore weapon cannot be switch so return
	if(self.weapon[weaponslot].name == "ignore" || self.weapon[weaponslot].name == "none") return;

	self setWeaponSlotWeapon(slot, self.weapon[weaponslot].name);
	self setWeaponSlotClipAmmo(slot, self.weapon[weaponslot].clip);
	self setWeaponSlotAmmo(slot, self.weapon[weaponslot].reserve);

	// save the new weapons for sprint system
	self notify("weaponsave");
}

checkAmmo(slot)
{
	weapon = self getWeaponSlotWeapon(slot);

	if(weapon == "sprint_mp" || isSprinting(self)) return;

	if(weapon == self.weapon["primary"].name)
	{
		self.weapon["primary"].clip = self getWeaponSlotClipAmmo(slot);
		self.weapon["primary"].reserve = self getWeaponSlotAmmo(slot);
	}

	if(weapon == self.weapon["primaryb"].name)
	{
		self.weapon["primaryb"].clip = self getWeaponSlotClipAmmo(slot);
		self.weapon["primaryb"].reserve = self getWeaponSlotAmmo(slot);
	}

	if(weapon == self.weapon["virtual"].name)
	{
		self.weapon["virtual"].clip = self getWeaponSlotClipAmmo(slot);
		self.weapon["virtual"].reserve = self getWeaponSlotAmmo(slot);
	}
}

giveFirstAid()
{
	// set the default firstaid kit value
	firstaidcount = level.wwx_firstaidkits;

	// check if random is on
	if(level.wwx_firstaid_random) firstaidcount = randomInt(level.wwx_firstaidkits);

	// check if ranksystem is on
	if(level.wwx_ranksystem) firstaidcount = game["rank_firstaid_kits_" + self.pers["rank"]];

	// give them at least one firstaid kit
	if(firstaidcount < 1) firstaidcount = 1;

	// check if the player has more than whats on offer, if not set number of first aid kits for player
	if(!isDefined(self.wwx_firstaidkits) || isDefined(self.wwx_firstaidkits) && firstaidcount > self.wwx_firstaidkits) self.wwx_firstaidkits = firstaidcount;

	// check the player is allowed to heal, if healing is revoked don't allow them to heal
	if(!isDefined(self.wwx_noheal)) self.wwx_canheal = true;
}


givePistol()
{
	weapon2 = self getWeaponSlotWeapon("primaryb");

	if(weapon2 != "none") return;

	pistoltype = getPistolType();
	self.pers["pistol"] = pistoltype;

	if(pistoltype == "ignore") return;

	self takeWeapon("colt_mp");
	self takeWeapon("webley_mp");
	self takeWeapon("TT30_mp");
	self takeWeapon("luger_mp");
	self takeWeapon("knife_mp");

	self setWeaponSlotWeapon("primaryb", pistoltype);

      if(level.wwx_wepo_loadout == 1)
	{
		// set primaryb
		clip = self getWeaponSlotClipAmmo("primaryb");
		ammo =  clip * game["rank_ammo_pistolclips_" + self.pers["rank"]];
		self setWeaponSlotAmmo("primaryb", ammo);
	}
	else 	self setWeaponSlotAmmo("primaryb", self getWeaponSlotAmmoDefault(pistoltype));
}

getPistolType()
{
if(drm_getcvar("scr_allow_knife") != "" && drm_getcvarint("scr_allow_knife") < 1) 
{
	self endon("disconnect");

	pistoltype = undefined;

	if(self.pers["team"] == "allies")
	{
		switch(game["allies"])
		{
			case "american":	pistoltype = "colt_mp";		break;
			case "british":		pistoltype = "webley_mp";	break;
			default:		assert(game["allies"] == "russian");	pistoltype = "TT30_mp";	break;
		}
	}
	else
	{
		assert(self.pers["team"] == "axis");
		switch(game["axis"])
		{
			default:		assert(game["axis"] == "german");	pistoltype = "luger_mp";	break;
		}
	}
}
else
{
	self endon("disconnect");

	pistoltype = undefined;

	if(self.pers["team"] == "allies")
	{
		switch(game["allies"])
		{
			case "american":	pistoltype = "knife_mp";		break;
			case "british":		pistoltype = "knife_mp";	break;
			default:		assert(game["allies"] == "russian");	pistoltype = "knife_mp";	break;
		}
	}
	else
	{
		assert(self.pers["team"] == "axis");
		switch(game["axis"])
		{
			default:		assert(game["axis"] == "german");	pistoltype = "knife_mp";	break;
		}
}
}

	// weapon limiter
	if(isDefined(level.weapons[pistoltype].allow) && level.weapons[pistoltype].allow != 1) return "ignore";
	else return pistoltype;
}

giveGrenades(frags, smokes)
{
	self endon("disconnect");

	if(self.pers["team"] == "allies")
	{
		switch(game["allies"])
		{
		case "american":
			grenadetype = "frag_grenade_american_mp";
			smokegrenadetype = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
			break;

		case "british":
			grenadetype = "frag_grenade_british_mp";
			smokegrenadetype = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
			break;

		default:
			assert(game["allies"] == "russian");
			grenadetype = "frag_grenade_russian_mp";
			smokegrenadetype = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
			break;
		}
	}
	else
	{
		assert(self.pers["team"] == "axis");
		switch(game["axis"])
		{
		default:
			assert(game["axis"] == "german");
			grenadetype = "frag_grenade_german_mp";
			smokegrenadetype = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
			break;
		}
	}

	self takeWeapon("frag_grenade_" + game["allies"] + "_mp");
	self takeWeapon("frag_grenade_german_mp");

	self takeWeapon("smoke_grenade_" + game["allies"] + getSmokeColour(level.wwx_smoke[game["allies"]]) + "mp");
	self takeWeapon("smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp");

	self.pers["fragtype"] = grenadetype;
	self.pers["smoketype"] = smokegrenadetype;

	// set the enemy grenade types
	self thread setEnemyGrenades();

	// weapon limiter
	if(isDefined(level.weapons["fraggrenade"].allow) && level.weapons["fraggrenade"].allow != 1) return;

	if(drm_getcvarint("scr_allow_fraggrenades"))
	{
		fraggrenadecount = 0;

		switch(level.wwx_frag_loadout)
		{
			case 1:	// WW2X rank system settings
			fraggrenadecount = game["rank_ammo_grenades_" + self.pers["rank"]];
			break;

			case 2:	// WW2X fixed settings
			fraggrenadecount = level.wwx_wepo_frag;
			break;

			case 3: // WW2X random settings
			fraggrenadecount = randomInt(level.wwx_wepo_frag_random + 1);
			if(fraggrenadecount < 1) fraggrenadecount = 1;
			break;

			default: // WW2X weapon class settings
			fraggrenadecount = getWeaponBasedGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(fraggrenadecount)) fraggrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(frags) && frags > fraggrenadecount) fraggrenadecount = frags;

		if(fraggrenadecount)
		{
			self giveWeapon(grenadetype);
			self setWeaponClipAmmo(grenadetype, fraggrenadecount);
		}
	}

	// weapon limiter
	if(isDefined(level.weapons["smokegrenade"].allow) && level.weapons["smokegrenade"].allow != 1)
	{
		self switchtooffhand(grenadetype);
		return;
	}

	if(drm_getcvarint("scr_allow_smokegrenades"))
	{
		smokegrenadecount = 0;

		switch(level.wwx_smoke_loadout)
		{
			case 1:	// WW2X rank system settings
			smokegrenadecount = game["rank_ammo_smoke_grenades_" + self.pers["rank"]];
			break;

			case 2: // WW2X fixed settings
			smokegrenadecount = level.wwx_wepo_smoke;
			break;

			case 3:	// WW2X random settings
			smokegrenadecount = randomInt(level.wwx_wepo_smoke_random + 1);
			if(smokegrenadecount < 1) smokegrenadecount = 1;
			break;

			default:	 // WW2X weapon class settings
			smokegrenadecount = getWeaponBasedSmokeGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(smokegrenadecount)) smokegrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(smokes) && smokes > smokegrenadecount) smokegrenadecount = smokes;

		if(smokegrenadecount)
		{
			self giveWeapon(smokegrenadetype);
			self setWeaponClipAmmo(smokegrenadetype, smokegrenadecount);
		}
	}

	self switchtooffhand(grenadetype);
}

giveFireGrenades(frags, smokes)
{
	self endon("disconnect");

	if(self.pers["team"] == "allies")
	{
		switch(game["allies"])
		{
		case "american":
			grenadetype = "fire_mp";
			smokegrenadetype = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
			break;

		case "british":
			grenadetype = "fire_mp";
			smokegrenadetype = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
			break;

		default:
			assert(game["allies"] == "russian");
			grenadetype = "fire_mp";
			smokegrenadetype = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
			break;
		}
	}
	else
	{
		assert(self.pers["team"] == "axis");
		switch(game["axis"])
		{
		default:
			assert(game["axis"] == "german");
			grenadetype = "fire_mp";
			smokegrenadetype = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
			break;
		}
	}

	self takeWeapon("fire_mp");
	self takeWeapon("smoke_grenade_" + game["allies"] + getSmokeColour(level.wwx_smoke[game["allies"]]) + "mp");
	self takeWeapon("smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp");

	self.pers["fragtype"] = grenadetype;
	self.pers["smoketype"] = smokegrenadetype;

	// set the enemy grenade types
	self thread setEnemyFireGrenades();

	// weapon limiter
	if(isDefined(level.weapons["fraggrenade"].allow) && level.weapons["fraggrenade"].allow != 1) return;

	if(drm_getcvarint("scr_allow_fraggrenades"))
	{
		fraggrenadecount = 0;

		switch(level.wwx_frag_loadout)
		{
			case 1:	// WW2X rank system settings
			fraggrenadecount = game["rank_ammo_grenades_" + self.pers["rank"]];
			break;

			case 2:	// WW2X fixed settings
			fraggrenadecount = level.wwx_wepo_frag;
			break;

			case 3: // WW2X random settings
			fraggrenadecount = randomInt(level.wwx_wepo_frag_random + 1);
			if(fraggrenadecount < 1) fraggrenadecount = 1;
			break;

			default: // WW2X weapon class settings
			fraggrenadecount = getWeaponBasedGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(fraggrenadecount)) fraggrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(frags) && frags > fraggrenadecount) fraggrenadecount = frags;

		if(fraggrenadecount)
		{
			self giveWeapon(grenadetype);
			self setWeaponClipAmmo(grenadetype, fraggrenadecount);
		}
	}

	// weapon limiter
	if(isDefined(level.weapons["smokegrenade"].allow) && level.weapons["smokegrenade"].allow != 1)
	{
		self switchtooffhand(grenadetype);
		return;
	}

	if(drm_getcvarint("scr_allow_smokegrenades"))
	{
		smokegrenadecount = 0;

		switch(level.wwx_smoke_loadout)
		{
			case 1:	// WW2X rank system settings
			smokegrenadecount = game["rank_ammo_smoke_grenades_" + self.pers["rank"]];
			break;

			case 2: // WW2X fixed settings
			smokegrenadecount = level.wwx_wepo_smoke;
			break;

			case 3:	// WW2X random settings
			smokegrenadecount = randomInt(level.wwx_wepo_smoke_random + 1);
			if(smokegrenadecount < 1) smokegrenadecount = 1;
			break;

			default:	 // WW2X weapon class settings
			smokegrenadecount = getWeaponBasedSmokeGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(smokegrenadecount)) smokegrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(smokes) && smokes > smokegrenadecount) smokegrenadecount = smokes;

		if(smokegrenadecount)
		{
			self giveWeapon(smokegrenadetype);
			self setWeaponClipAmmo(smokegrenadetype, smokegrenadecount);
		}
	}

	self switchtooffhand(grenadetype);
}

giveGasGrenades(frags, smokes)
{
	self endon("disconnect");

	if(self.pers["team"] == "allies")
	{
		switch(game["allies"])
		{
		case "american":
			grenadetype = "gas_mp";
			smokegrenadetype = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
			break;

		case "british":
			grenadetype = "gas_mp";
			smokegrenadetype = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
			break;

		default:
			assert(game["allies"] == "russian");
			grenadetype = "gas_mp";
			smokegrenadetype = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
			break;
		}
	}
	else
	{
		assert(self.pers["team"] == "axis");
		switch(game["axis"])
		{
		default:
			assert(game["axis"] == "german");
			grenadetype = "gas_mp";
			smokegrenadetype = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
			break;
		}
	}

	self takeWeapon("gas_mp");
	self takeWeapon("smoke_grenade_" + game["allies"] + getSmokeColour(level.wwx_smoke[game["allies"]]) + "mp");
	self takeWeapon("smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp");

	self.pers["fragtype"] = grenadetype;
	self.pers["smoketype"] = smokegrenadetype;

	// set the enemy grenade types
	self thread setEnemyGasGrenades();

	// weapon limiter
	if(isDefined(level.weapons["fraggrenade"].allow) && level.weapons["fraggrenade"].allow != 1) return;

	if(drm_getcvarint("scr_allow_fraggrenades"))
	{
		fraggrenadecount = 0;

		switch(level.wwx_frag_loadout)
		{
			case 1:	// WW2X rank system settings
			fraggrenadecount = game["rank_ammo_grenades_" + self.pers["rank"]];
			break;

			case 2:	// WW2X fixed settings
			fraggrenadecount = level.wwx_wepo_frag;
			break;

			case 3: // WW2X random settings
			fraggrenadecount = randomInt(level.wwx_wepo_frag_random + 1);
			if(fraggrenadecount < 1) fraggrenadecount = 1;
			break;

			default: // WW2X weapon class settings
			fraggrenadecount = getWeaponBasedGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(fraggrenadecount)) fraggrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(frags) && frags > fraggrenadecount) fraggrenadecount = frags;

		if(fraggrenadecount)
		{
			self giveWeapon(grenadetype);
			self setWeaponClipAmmo(grenadetype, fraggrenadecount);
		}
	}

	// weapon limiter
	if(isDefined(level.weapons["smokegrenade"].allow) && level.weapons["smokegrenade"].allow != 1)
	{
		self switchtooffhand(grenadetype);
		return;
	}

	if(drm_getcvarint("scr_allow_smokegrenades"))
	{
		smokegrenadecount = 0;

		switch(level.wwx_smoke_loadout)
		{
			case 1:	// WW2X rank system settings
			smokegrenadecount = game["rank_ammo_smoke_grenades_" + self.pers["rank"]];
			break;

			case 2: // WW2X fixed settings
			smokegrenadecount = level.wwx_wepo_smoke;
			break;

			case 3:	// WW2X random settings
			smokegrenadecount = randomInt(level.wwx_wepo_smoke_random + 1);
			if(smokegrenadecount < 1) smokegrenadecount = 1;
			break;

			default:	 // WW2X weapon class settings
			smokegrenadecount = getWeaponBasedSmokeGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(smokegrenadecount)) smokegrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(smokes) && smokes > smokegrenadecount) smokegrenadecount = smokes;

		if(smokegrenadecount)
		{
			self giveWeapon(smokegrenadetype);
			self setWeaponClipAmmo(smokegrenadetype, smokegrenadecount);
		}
	}

	self switchtooffhand(grenadetype);
}

giveStickyGrenades(frags, smokes)
{
	self endon("disconnect");

	if(self.pers["team"] == "allies")
	{
		switch(game["allies"])
		{
		case "american":
			grenadetype = "sticky_mp";
			smokegrenadetype = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
			break;

		case "british":
			grenadetype = "sticky_mp";
			smokegrenadetype = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
			break;

		default:
			assert(game["allies"] == "russian");
			grenadetype = "sticky_mp";
			smokegrenadetype = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
			break;
		}
	}
	else
	{
		assert(self.pers["team"] == "axis");
		switch(game["axis"])
		{
		default:
			assert(game["axis"] == "german");
			grenadetype = "sticky_mp";
			smokegrenadetype = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
			break;
		}
	}

	self takeWeapon("sticky_mp");
	self takeWeapon("smoke_grenade_" + game["allies"] + getSmokeColour(level.wwx_smoke[game["allies"]]) + "mp");
	self takeWeapon("smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp");

	self.pers["fragtype"] = grenadetype;
	self.pers["smoketype"] = smokegrenadetype;

	// set the enemy grenade types
	self thread setEnemyStickyGrenades();

	// weapon limiter
	if(isDefined(level.weapons["fraggrenade"].allow) && level.weapons["fraggrenade"].allow != 1) return;

	if(drm_getcvarint("scr_allow_fraggrenades"))
	{
		fraggrenadecount = 0;

		switch(level.wwx_frag_loadout)
		{
			case 1:	// WW2X rank system settings
			fraggrenadecount = game["rank_ammo_grenades_" + self.pers["rank"]];
			break;

			case 2:	// WW2X fixed settings
			fraggrenadecount = level.wwx_wepo_frag;
			break;

			case 3: // WW2X random settings
			fraggrenadecount = randomInt(level.wwx_wepo_frag_random + 1);
			if(fraggrenadecount < 1) fraggrenadecount = 1;
			break;

			default: // WW2X weapon class settings
			fraggrenadecount = getWeaponBasedGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(fraggrenadecount)) fraggrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(frags) && frags > fraggrenadecount) fraggrenadecount = frags;

		if(fraggrenadecount)
		{
			self giveWeapon(grenadetype);
			self setWeaponClipAmmo(grenadetype, fraggrenadecount);
		}
	}

	// weapon limiter
	if(isDefined(level.weapons["smokegrenade"].allow) && level.weapons["smokegrenade"].allow != 1)
	{
		self switchtooffhand(grenadetype);
		return;
	}

	if(drm_getcvarint("scr_allow_smokegrenades"))
	{
		smokegrenadecount = 0;

		switch(level.wwx_smoke_loadout)
		{
			case 1:	// WW2X rank system settings
			smokegrenadecount = game["rank_ammo_smoke_grenades_" + self.pers["rank"]];
			break;

			case 2: // WW2X fixed settings
			smokegrenadecount = level.wwx_wepo_smoke;
			break;

			case 3:	// WW2X random settings
			smokegrenadecount = randomInt(level.wwx_wepo_smoke_random + 1);
			if(smokegrenadecount < 1) smokegrenadecount = 1;
			break;

			default:	 // WW2X weapon class settings
			smokegrenadecount = getWeaponBasedSmokeGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(smokegrenadecount)) smokegrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(smokes) && smokes > smokegrenadecount) smokegrenadecount = smokes;

		if(smokegrenadecount)
		{
			self giveWeapon(smokegrenadetype);
			self setWeaponClipAmmo(smokegrenadetype, smokegrenadecount);
		}
	}

	self switchtooffhand(grenadetype);
}

giveSatchelGrenades(frags, smokes)
{
	self endon("disconnect");

	if(self.pers["team"] == "allies")
	{
		switch(game["allies"])
		{
		case "american":
			grenadetype = "satchel_mp";
			smokegrenadetype = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
			break;

		case "british":
			grenadetype = "satchel_mp";
			smokegrenadetype = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
			break;

		default:
			assert(game["allies"] == "russian");
			grenadetype = "satchel_mp";
			smokegrenadetype = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
			break;
		}
	}
	else
	{
		assert(self.pers["team"] == "axis");
		switch(game["axis"])
		{
		default:
			assert(game["axis"] == "german");
			grenadetype = "satchel_mp";
			smokegrenadetype = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
			break;
		}
	}

	self takeWeapon("satchel_mp");
	self takeWeapon("smoke_grenade_" + game["allies"] + getSmokeColour(level.wwx_smoke[game["allies"]]) + "mp");
	self takeWeapon("smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp");

	self.pers["fragtype"] = grenadetype;
	self.pers["smoketype"] = smokegrenadetype;

	// set the enemy grenade types
	self thread setEnemySatchelGrenades();

	// weapon limiter
	if(isDefined(level.weapons["fraggrenade"].allow) && level.weapons["fraggrenade"].allow != 1) return;

	if(drm_getcvarint("scr_allow_fraggrenades"))
	{
		fraggrenadecount = 0;

		switch(level.wwx_frag_loadout)
		{
			case 1:	// WW2X rank system settings
			fraggrenadecount = game["rank_ammo_grenades_" + self.pers["rank"]];
			break;

			case 2:	// WW2X fixed settings
			fraggrenadecount = level.wwx_wepo_frag;
			break;

			case 3: // WW2X random settings
			fraggrenadecount = randomInt(level.wwx_wepo_frag_random + 1);
			if(fraggrenadecount < 1) fraggrenadecount = 1;
			break;

			default: // WW2X weapon class settings
			fraggrenadecount = getWeaponBasedGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(fraggrenadecount)) fraggrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(frags) && frags > fraggrenadecount) fraggrenadecount = frags;

		if(fraggrenadecount)
		{
			self giveWeapon(grenadetype);
			self setWeaponClipAmmo(grenadetype, fraggrenadecount);
		}
	}

	// weapon limiter
	if(isDefined(level.weapons["smokegrenade"].allow) && level.weapons["smokegrenade"].allow != 1)
	{
		self switchtooffhand(grenadetype);
		return;
	}

	if(drm_getcvarint("scr_allow_smokegrenades"))
	{
		smokegrenadecount = 0;

		switch(level.wwx_smoke_loadout)
		{
			case 1:	// WW2X rank system settings
			smokegrenadecount = game["rank_ammo_smoke_grenades_" + self.pers["rank"]];
			break;

			case 2: // WW2X fixed settings
			smokegrenadecount = level.wwx_wepo_smoke;
			break;

			case 3:	// WW2X random settings
			smokegrenadecount = randomInt(level.wwx_wepo_smoke_random + 1);
			if(smokegrenadecount < 1) smokegrenadecount = 1;
			break;

			default:	 // WW2X weapon class settings
			smokegrenadecount = getWeaponBasedSmokeGrenadeCount(self.pers["weapon"]);
			break;
		}

		// if all fails, give them 1 grenade
		if(!isDefined(smokegrenadecount)) smokegrenadecount = 1;

		// check how many nades thay have already, if the new count is less, don't bother
		if(isDefined(smokes) && smokes > smokegrenadecount) smokegrenadecount = smokes;

		if(smokegrenadecount)
		{
			self giveWeapon(smokegrenadetype);
			self setWeaponClipAmmo(smokegrenadetype, smokegrenadecount);
		}
	}

	self switchtooffhand(grenadetype);
}

giveBinoculars()
{
	self giveWeapon("binoculars_mp");

	if(level.wwx_ranksystem)
	{
		if(level.wwx_wepo_limiter)
		{
			if(isDefined(level.weapons["binoculars_mp"].allow) && level.weapons["binoculars_mp"].allow == 0)
			{
				self.wwx_haswmdbinocs = false;
				return;
			}
		}

		self.wwx_haswmdbinocs = true;
	}
}

getWeaponBasedGrenadeCount(weapon)
{
	if(!isValidWeapon(weapon)) return;

	switch(weapon)
	{
		case "springfield_mp":
		case "enfield_scope_mp":
		case "mosin_nagant_sniper_mp":
		case "kar98k_sniper_mp":
		case "g43_sniper":
			return level.wwx_wepo_frag_stock_sniper;

		case "m1carbine_mp":
		case "m1garand_mp":
		case "SVT40_mp":
		case "g43_mp":
		case "enfield_mp":
		case "mosin_nagant_mp":
		case "kar98k_mp":
			return level.wwx_wepo_frag_stock_rifle;

		case "bar_mp":
		case "bren_mp":
		case "mp44_mp":
		case "mobile_30cal":
		case "30cal_mp":
		case "mobile_mg42":
		case "mg42_mp":
			return level.wwx_wepo_frag_stock_mg;
	
		case "thompson_mp":
		case "sten_mp":
		case "ppsh_mp":
		case "mp40_mp":
		case "PPS42_mp":
		case "greasegun_mp":
			return level.wwx_wepo_frag_stock_smg;

		case "knive_mp":
		case "shotgun_mp":
			return level.wwx_wepo_frag_stock_shot;

		case "panzerschreck_mp":
            case "panzerschreck_allies":
			return level.wwx_wepo_frag_stock_rl;

		default:
			return 0;
	}
}

getWeaponBasedSmokeGrenadeCount(weapon)
{
	switch(weapon)
	{
		case "springfield_mp":
		case "enfield_scope_mp":
		case "mosin_nagant_sniper_mp":
		case "kar98k_sniper_mp":
		case "g43_sniper":
			return level.wwx_wepo_smoke_stock_sniper;

		case "m1carbine_mp":
		case "m1garand_mp":
		case "SVT40_mp":
		case "g43_mp":
		case "enfield_mp":
		case "mosin_nagant_mp":
		case "kar98k_mp":
			return level.wwx_wepo_smoke_stock_rifle;

		case "bar_mp":
		case "bren_mp":
		case "mp44_mp":
		case "mobile_30cal":
		case "mobile_mg42":
		case "mg42_mp":
		case "30cal_mp":
			return level.wwx_wepo_smoke_stock_mg;
	
		case "thompson_mp":
		case "sten_mp":
		case "ppsh_mp":
		case "mp40_mp":
		case "PPS42_mp":
		case "greasegun_mp":
			return level.wwx_wepo_smoke_stock_smg;

		case "knive_mp":
		case "shotgun_mp":
			return level.wwx_wepo_smoke_stock_shot;

		case "panzerschreck_mp":
		case "panzerschreck_allies":
			return level.wwx_wepo_smoke_stock_rl;

		default:
			return 0;
	}
}

isValidWeapon(weapon)
{
	if(!isDefined(weapon)) return false;

	switch(weapon)
	{
		case "none":
		case "sprint_mp":
		case "ignore":
		return false;
	}

	return true;
}

isWeaponType(weapon, type)
{
	if(!isDefined(weapon)) return false;

	switch(type)
	{
		case "turret":
		switch(weapon)
		{
			case "mg42_bipod_duck_mp":
			case "mg42_bipod_prone_mp":
			case "mg42_bipod_stand_mp":
			case "30cal_prone_mp":
		      case "mobile_30cal":
		      case "mobile_mg42":
		      case "30cal_mp":
		      case "mg42_mp":
			case "30cal_stand_mp": return true;
			default: return false;
		}

		case "deployable":
		switch(weapon)
		{
			case "mg42_bipod_duck_mp":
			case "mg42_bipod_prone_mp":
			case "mg42_bipod_stand_mp":
			case "30cal_prone_mp":
		       case "30cal_mp":
		       case "mg42_mp":
			case "30cal_stand_mp": return true;
			default: return false;
		}

		case "rocket":
		switch(weapon)
		{
			case "panzerfaust_mp":
			case "panzerschreck_allies":
			case "panzerschreck_mp": return true;
			default: return false;
		}
		break;

		case "common":
		switch(weapon)
		{
			case "panzerfaust_mp":
			case "panzerschreck_mp":
			case "panzerschreck_allies":
			case "binoculars_mp":
                  case "knive_mp":
			case "shotgun_mp": return true;
			default: return false;
		}

		// Check if weapon is a grenade
		case "grenade":
		switch(weapon)
		{
			case "frag_grenade_american_mp":
			case "frag_grenade_british_mp":
			case "frag_grenade_german_mp":
			case "fire_mp":
			case "gas_mp":
			case "sticky_mp":
			case "satchel_mp":
			case "frag_grenade_russian_mp": return true;
			default: return false;
		}

		// Check if weapon is smoke grenade
		case "smokegrenade":
		switch(weapon)
		{
			case "smoke_grenade_american_mp":
			case "smoke_grenade_american_blue_mp":
			case "smoke_grenade_american_green_mp":
			case "smoke_grenade_american_orange_mp":
			case "smoke_grenade_american_pink_mp":
			case "smoke_grenade_american_red_mp":
			case "smoke_grenade_american_yellow_mp":
			case "smoke_grenade_american_fire_mp":
			case "smoke_grenade_american_gas_mp":
			case "smoke_grenade_british_mp":
			case "smoke_grenade_british_blue_mp":
			case "smoke_grenade_british_green_mp":
			case "smoke_grenade_british_orange_mp":
			case "smoke_grenade_british_pink_mp":
			case "smoke_grenade_british_red_mp":
			case "smoke_grenade_british_yellow_mp":
			case "smoke_grenade_british_fire_mp":
			case "smoke_grenade_british_gas_mp":
			case "smoke_grenade_german_mp":
			case "smoke_grenade_german_blue_mp":
			case "smoke_grenade_german_green_mp":
			case "smoke_grenade_german_orange_mp":
			case "smoke_grenade_german_pink_mp":
			case "smoke_grenade_german_red_mp":
			case "smoke_grenade_german_yellow_mp":
			case "smoke_grenade_german_fire_mp":
			case "smoke_grenade_german_gas_mp":
			case "smoke_grenade_russian_mp":
			case "smoke_grenade_russian_blue_mp":
			case "smoke_grenade_russian_green_mp":
			case "smoke_grenade_russian_orange_mp":
			case "smoke_grenade_russian_pink_mp":
			case "smoke_grenade_russian_red_mp":
			case "smoke_grenade_russian_yellow_mp":
			case "smoke_grenade_russian_fire_mp":
			case "smoke_grenade_russian_gas_mp": return true;
			default: return false;
		}

		// Check if weapon is a rifle
		case "rifle":
		switch(weapon)
		{
			case "enfield_mp":
			case "g43_mp":
			case "kar98k_mp":
			case "m1carbine_mp":
			case "m1garand_mp":
			case "mosin_nagant_mp":
			case "SVT40_mp": return true;
			default: return false;
		}

		// Check if weapon is a bolt action rifle
		case "boltrifle":
		switch(weapon)
		{
			case "mosin_nagant_mp":
			case "kar98k_mp":
			case "enfield_mp": return true;
			default: return false;
		}

		// Check if weapon is a semi automatic rifle
		case "semirifle":
		switch(weapon)
		{
			case "g43_mp":
			case "m1garand_mp":
			case "SVT40_mp": return true;
			default: return false;
		}

		// Check if weapon is smg
		case "smg":
		switch(weapon)
		{
			case "greasegun_mp":
			case "mp40_mp":
			case "sten_mp":
			case "thompson_mp":
			case "ppsh_mp":
			case "PPS42_mp": return true;
			default: return false;
		}

		// Check if weapon is mg
		case "mg":
		switch(weapon)
		{
			case "mp44_mp":
			case "bar_mp":
			case "bren_mp":
		      case "mobile_30cal":
		      case "mobile_mg42":
		      case "30cal_mp":
		      case "mg42_mp":
			case "ppsh_mp": return true;
			default: return false;
		}

		// Check if weapon is sniper
		case "sniper":
		switch(weapon)
		{
			case "mosin_nagant_sniper_mp":
			case "springfield_mp":
			case "kar98k_sniper_mp":
			case "enfield_scope_mp":
			case "g43_sniper": return true;
			default: return false;
		}

		// Check if weapon is rocket launcher
		case "rl":
		switch(weapon)
		{
			case "panzerschreck_mp":
			case "panzerschreck_allies": return true;
			default: return false;
		}

		// Check if weapon is shotgun
		case "shotgun":
		switch(weapon)
		{
			case "shotgun_mp": return true;
			default: return false;
		}

		// Check if weapon is pistol
		case "pistol":
		switch(weapon)
		{
			case "colt_mp":
			case "luger_mp":
			case "TT30_mp":
			case "knife_mp":
			case "webley_mp": return true;
			default: return false;
		}

		// Check if weapon is knife
		case "knife":
		switch(weapon)
		{
                        case "knive_mp":
			case "knife_mp": return true;
			default: return false;
		}

		// Check if weapon is american
		case "american":
		switch(weapon)
		{
			case "frag_grenade_american_mp":
			case "smoke_grenade_american_mp":
			case "colt_mp":
			case "knife_mp":
			case "fire_mp":
			case "gas_mp":
			case "sticky_mp":
			case "satchel_mp":
		      case "mobile_30cal":
		      case "30cal_mp":
			case "m1carbine_mp":
			case "m1garand_mp":
			case "greasegun_mp":
			case "thompson_mp":
			case "bar_mp":
			case "springfield_mp":
			case "shotgun_mp":
                  case "knive_mp":
                  case "panzerschreck_allies":
			case "enfield_mp": return true;
			default: return false;
		}

		// Check if weapon is british
		case "british":
		switch(weapon)
		{
			case "frag_grenade_british_mp":
			case "smoke_grenade_british_mp":
			case "webley_mp":
			case "knife_mp":
			case "fire_mp":
			case "gas_mp":
			case "sticky_mp":
			case "satchel_mp":
                  case "knive_mp":
		      case "mobile_30cal":
		      case "30cal_mp":
			case "m1garand_mp":
			case "enfield_mp":
			case "sten_mp":
			case "bren_mp":
			case "thompson_mp":
			case "enfield_scope_mp":
                  case "panzerschreck_allies":
			case "shotgun_mp": return true;
			default: return false;
		}

		// Check if weapon is russian
		case "russian":
		switch(weapon)
		{
			case "frag_grenade_russian_mp":
			case "smoke_grenade_russian_mp":
			case "TT30_mp":
			case "knife_mp":
		      case "mobile_30cal":
		      case "30cal_mp":
			case "fire_mp":
			case "gas_mp":
			case "sticky_mp":
			case "satchel_mp":
			case "mosin_nagant_mp":
			case "SVT40_mp":
                  case "knive_mp":
			case "PPS42_mp":
			case "ppsh_mp":
			case "mosin_nagant_sniper_mp":
                  case "panzerschreck_allies":
			case "shotgun_mp": return true;
			default: return false;
		}

		// Check if weapon is german
		case "german":
		switch(weapon)
		{
			case "frag_grenade_german_mp":
			case "smoke_grenade_german_mp":
			case "luger_mp":
			case "knife_mp":
			case "kar98k_mp":
		      case "mobile_mg42":
		      case "mg42_mp":
			case "fire_mp":
			case "gas_mp":
			case "sticky_mp":
			case "satchel_mp":
			case "g43_mp":
                  case "knive_mp":
			case "g43_sniper":
			case "mp40_mp":
			case "mp44_mp":
			case "kar98k_sniper_mp":
                  case "panzerschreck_mp":
			case "shotgun_mp": return true;
			default: return false;
		}
	}
	
	return false;
}

getWeaponSlotAmmoDefault(weapon)
{
	if(isDefined(weapon))
	{
		switch(weapon)
		{
			case "sprint_mp":
			case "none":
			case "ignore":
			if(isDefined(weapon)) logPrint("This weapon was not found >>>>>>> " + weapon + "<<<<<<<<< \n");
			return 0;

			default:
			if(isDefined(level.weapons[weapon])) return level.weapons[weapon].ammo_limit;
			else logPrint("This weapon was not found >>>>>>> " + weapon + "<<<<<<<<< \n");
			return 0;
		}
	}
}

getWeaponSlotClipAmmoDefault(weapon)
{
	if(!isDefined(weapon)) return 0;

	switch(weapon)
	{
		case "panzerfaust_mp":
		case "panzerschreck_allies":
		case "panzerschreck_mp":				return 1;

		case "shotgun_mp":
		case "webley_mp":							return 6;

		case "colt_mp":								return 7;
		case "knife_mp":								return 20;
		case "knive_mp":								return 20;

		case "m1garand_mp":
		case "luger_mp":
		case "TT30_mp":								return 8;

		case "kar98k_mp":
		case "kar98k_sniper_mp":
		case "mosin_nagant_mp":
		case "mosin_nagant_sniper_mp":
		case "springfield_mp":						return 5;

		case "g43_mp":
		case "g43_sniper":
		case "SVT40_mp":
		case "enfield_mp":
		case "enfield_scope_mp":				return 10;

		case "m1carbine_mp":						return 15;

		
		case "bar_mp":								return 20;

		case "bren_mp":
		case "thompson_mp":
		case "mp44_mp":								return 30;

		case "greasegun_mp":	
		case "mp40_mp":
		case "sten_mp":								return 32;
		case "mobile_mg42":
            case "mobile_30cal":                                        return 100;
		case "mg42_mp":
            case "30cal_mp":                                        return 250;

		case "PPS42_mp":							return 35;
		case "ppsh_mp":								return 71;

		default:												return 0; // unknown weapon
	}
}

getSmokeColour(num)
{
	switch(num)
	{
		case 1: return "_blue_";
		case 2: return "_green_";
		case 3: return "_orange_";
		case 4: return "_pink_";
		case 5: return "_red_";
		case 6: return "_yellow_";
		case 7: return "_knife_";
		case 8: return "_fire_";
		case 9: return "_gas_";
		case 10: return "_sticky_";
		case 11: return "_satchel_";

		case 0:
		default: return "_";
	}
}

getAmmoOffhandGun()
{
	self endon("disconnect");

	offHandWeapon = undefined;

	current = self getCurrentWeapon();
	primary = self getWeaponSlotWeapon("primary");

	// check for sprinting
	if(current == "sprint_mp" || primary == "sprint_mp") return;

	primaryb = self getWeaponSlotWeapon("primaryb");

	if(current != primary) offHandWeapon = "primaryb";
	else offHandWeapon = "primary";

	ammoInOffHand = getSlotAmmo(offHandWeapon);

	return ammoInOffHand;
}	

getSlotAmmo(slot)
{
	self endon("disconnect");

	weapon = self getWeaponSlotWeapon(slot);

	// sprinting, none or ignore?
	if(!isValidWeapon(weapon)) return;

	clip = self getWeaponSlotClipAmmoDefault(weapon);
	reserve = self getWeaponSlotAmmoDefault(weapon);
	ammo = clip + reserve;
	return ammo;
}

setEnemyGrenades()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	if(isPlayer(self))
	{
		switch(game["allies"])
		{
			case "american":
				allies_frag = "frag_grenade_american_mp";
				allies_smoke = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
				break;
	
			case "british":
				allies_frag = "frag_grenade_british_mp";
				allies_smoke = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
				break;
	
			default:
				allies_frag = "frag_grenade_russian_mp";
				allies_smoke = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
				break;
		}
	
		if(self.pers["team"] == "allies")
		{
			self.pers["enemy_fragtype"] = "frag_grenade_german_mp";
			self.pers["enemy_smoketype"] = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
		}
		else
		{
			self.pers["enemy_fragtype"] = allies_frag;
			self.pers["enemy_smoketype"] = allies_smoke;
		}
	}
}

setEnemyFireGrenades()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	if(isPlayer(self))
	{
		switch(game["allies"])
		{
			case "american":
				allies_frag = "fire_mp";
				allies_smoke = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
				break;
	
			case "british":
				allies_frag = "fire_mp";
				allies_smoke = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
				break;
	
			default:
				allies_frag = "fire_mp";
				allies_smoke = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
				break;
		}
	
		if(self.pers["team"] == "allies")
		{
			self.pers["enemy_fragtype"] = "fire_mp";
			self.pers["enemy_smoketype"] = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
		}
		else
		{
			self.pers["enemy_fragtype"] = allies_frag;
			self.pers["enemy_smoketype"] = allies_smoke;
		}
	}
}

setEnemyGasGrenades()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	if(isPlayer(self))
	{
		switch(game["allies"])
		{
			case "american":
				allies_frag = "gas_mp";
				allies_smoke = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
				break;
	
			case "british":
				allies_frag = "gas_mp";
				allies_smoke = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
				break;
	
			default:
				allies_frag = "gas_mp";
				allies_smoke = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
				break;
		}
	
		if(self.pers["team"] == "allies")
		{
			self.pers["enemy_fragtype"] = "gas_mp";
			self.pers["enemy_smoketype"] = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
		}
		else
		{
			self.pers["enemy_fragtype"] = allies_frag;
			self.pers["enemy_smoketype"] = allies_smoke;
		}
	}
}

setEnemyStickyGrenades()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	if(isPlayer(self))
	{
		switch(game["allies"])
		{
			case "american":
				allies_frag = "sticky_mp";
				allies_smoke = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
				break;
	
			case "british":
				allies_frag = "sticky_mp";
				allies_smoke = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
				break;
	
			default:
				allies_frag = "sticky_mp";
				allies_smoke = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
				break;
		}
	
		if(self.pers["team"] == "allies")
		{
			self.pers["enemy_fragtype"] = "sticky_mp";
			self.pers["enemy_smoketype"] = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
		}
		else
		{
			self.pers["enemy_fragtype"] = allies_frag;
			self.pers["enemy_smoketype"] = allies_smoke;
		}
	}
}

setEnemySatchelGrenades()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	if(isPlayer(self))
	{
		switch(game["allies"])
		{
			case "american":
				allies_frag = "satchel_mp";
				allies_smoke = "smoke_grenade_american" + getSmokeColour(level.wwx_smoke["american"]) + "mp";
				break;
	
			case "british":
				allies_frag = "satchel_mp";
				allies_smoke = "smoke_grenade_british" + getSmokeColour(level.wwx_smoke["british"]) + "mp";
				break;
	
			default:
				allies_frag = "satchel_mp";
				allies_smoke = "smoke_grenade_russian" + getSmokeColour(level.wwx_smoke["russian"]) + "mp";
				break;
		}
	
		if(self.pers["team"] == "allies")
		{
			self.pers["enemy_fragtype"] = "satchel_mp";
			self.pers["enemy_smoketype"] = "smoke_grenade_german" + getSmokeColour(level.wwx_smoke["german"]) + "mp";
		}
		else
		{
			self.pers["enemy_fragtype"] = allies_frag;
			self.pers["enemy_smoketype"] = allies_smoke;
		}
	}
}

setWeaponArray()
{
	// create the arrays
	if(!isDefined(self.weapon)) self.weapon = [];
	if(!isDefined(self.weaponin)) self.weaponin = [];

	// clear weapon primary
	if(!isDefined(self.weapon["primary"]))
	{
		self.weapon["primary"] = spawnstruct();
		self.weapon["primary"].name = undefined;
		self.weapon["primary"].clip = undefined;
		self.weapon["primary"].reserve = undefined;
		self.weapon["primary"].maxammo = undefined;
	}

	// clear weapon primaryb
	if(!isDefined(self.weapon["primaryb"]))
	{
		self.weapon["primaryb"] = spawnstruct();
		self.weapon["primaryb"].name = undefined;
		self.weapon["primaryb"].clip = undefined;
		self.weapon["primaryb"].reserve = undefined;
		self.weapon["primaryb"].maxammo = undefined;
	}

	// clear weapon virtual
	if(!isDefined(self.weapon["virtual"]))
	{
		self.weapon["virtual"] = spawnstruct();
		self.weapon["virtual"].name = undefined;
		self.weapon["virtual"].clip = undefined;
		self.weapon["virtual"].reserve = undefined;
		self.weapon["virtual"].maxammo = undefined;
	}

	// clear old weapon primary
	if(!isDefined(self.weapon["oldprimary"]))
	{
		self.weapon["oldprimary"] = spawnstruct();
		self.weapon["oldprimary"].name = undefined;
		self.weapon["oldprimary"].clip = undefined;
		self.weapon["oldprimary"].reserve = undefined;
	}

	// clear old weapon primaryb
	if(!isDefined(self.weapon["oldprimaryb"]))
	{
		self.weapon["oldprimaryb"] = spawnstruct();
		self.weapon["oldprimaryb"].name = undefined;
		self.weapon["oldprimaryb"].clip = undefined;
		self.weapon["oldprimaryb"].reserve = undefined;
	}

	// clear old current weapon
	if(!isDefined(self.weapon["current"]))
	{
		self.weapon["current"] = spawnstruct();
		self.weapon["current"].name = undefined;
	}

	// clear weapon in slots
	if(!isDefined(self.weaponin["primary"]))
	{
		self.weaponin["primary"] = spawnstruct();
		self.weaponin["primary"].slot = undefined;
	}

	if(!isDefined(self.weaponin["primaryb"]))
	{
		self.weaponin["primaryb"] = spawnstruct();
		self.weaponin["primaryb"].slot = undefined;
	}
}

setWeaponClientStatus(status)
{
	self endon("disconnect");

	if(!isDefined(status)) status = false;

	self updateAllAllowedSingleClient();

	weapon1 = self getWeaponSlotWeapon("primary");
	weapon2 = self getWeaponSlotWeapon("primaryb");

	self.pers["weapon1"] = weapon1;
	self.pers["weapon2"] = weapon2;

	if(!status)
	{
		if(isDefined(weapon1) && weapon1 != "none" && weapon1 != "ignore") self updateDisabledSingleClient(weapon1);
		if(isDefined(weapon2) && weapon2 != "none" && weapon2 != "ignore") self updateDisabledSingleClient(weapon2);
	}
	else
	{
		if(isDefined(weapon1) && weapon1 != "none" && weapon1 != "ignore") self updateAllowedSingleClient(weapon1);
		if(isDefined(weapon2) && weapon2 != "none" && weapon2 != "ignore") self updateAllowedSingleClient(weapon2);
	}
}

isSprinting(player)
{
	if(player getCurrentWeapon("primary") == "sprint_mp") return true;
	else return false;
}

isPrimary(weapon)
{
	if(isDefined(weapon) && weapon == self getWeaponSlotWeapon("primary")) return true;
	else return false;
}

isSecondary(weapon)
{
	if(isDefined(weapon) && weapon == self getWeaponSlotWeapon("primaryb")) return true;
	else return false;
}

isDummy(weapon)
{
	if(isDefined(weapon) && weapon == "dummy1_mp" || weapon == "dummy2_mp" || weapon == "dummy3_mp" || weapon == "dummy4_mp") return true;
	else return false;
}

getDummy()
{
	self endon("disconnect");

	if(self.weapon["primary"].name != "dummy1_mp" && self.weapon["primaryb"].name != "dummy1_mp" && self.weapon["virtual"].name != "dummy1_mp") return "dummy1_mp";
	else if(self.weapon["primary"].name != "dummy2_mp" && self.weapon["primaryb"].name != "dummy2_mp" && self.weapon["virtual"].name != "dummy2_mp") return "dummy2_mp";
	else if(self.weapon["primary"].name != "dummy3_mp" && self.weapon["primaryb"].name != "dummy3_mp" && self.weapon["virtual"].name != "dummy3_mp") return "dummy3_mp";

	return "dummy4_mp"; // shouldn't happen, but you never know!
}

replenishWeapons(gts)
{
	thread ww2x\_wwx_logfile::Log("replenishWeapons");

	level endon("wwx_gameover");
	level endon("intermission");
	level endon("map_ended");

	self endon("disconnect");
	self endon("spawned");

	if(!isDefined(gts)) gts = false;

	if(!gts)
	{
		self disableWeapon();

		// stop the weapon monitor
		self.wwx_stopwepmon = true;

		// play reload sound for effect
		self playsound("weap_bar_reload");

		wait 0.25;
	}

	if(isPlayer(self))
	{
		if(level.wwx_wepo_class)
		{
			self setAmmo("primary", gts);
			self setAmmo("primaryb", gts);
			return;
		}
		else
		{
			// replenish all slots
			self refillWeapon("primary", gts);
			self refillWeapon("primaryb", gts);
			self refillWeapon("virtual", gts);
		}

		if(!gts)
		{
			// save the new weapon variables
			self notify("weaponsave");

			// start the weapon monitor
			self.wwx_stopwepmon = false;
		}
	}

	if(!gts)
	{
		wait 3;
		if(isPlayer(self)) self enableWeapon();
	}
}

replenishGrenades(gts)
{
	thread ww2x\_wwx_logfile::Log("replenishGrenades");

	level endon("wwx_gameover");
	level endon("intermission");
	level endon("map_ended");

	self endon("disconnect");
	self endon("spawned");

	if(!isDefined(gts)) gts = false;

	if(!gts)
	{
		// play reload sound for effect
		self playlocalsound("grenade_pickup");

		wait 0.25;

		// replenish grenades
		frags = self getammocount(self.pers["fragtype"]) + self getammocount(self.pers["enemy_fragtype"]);
		smokes = self getammocount(self.pers["smoketype"]) + self getammocount(self.pers["enemy_smoketype"]);
	}
	else
	{
		frags = 0;
		smokes = 0;
	}

	if(isPlayer(self))
	{
		if(level.wwx_firenades) self giveFireGrenades();
			else if(level.wwx_gasnades) self giveGasGrenades();
			else if(level.wwx_stickynades) self giveStickyGrenades();
			else if(level.wwx_satchels) self giveSatchelGrenades();
				else self giveGrenades();
	}
}

replenishFirstaid(gts)
{
	thread ww2x\_wwx_logfile::Log("replenishFirstaid");

	level endon("wwx_gameover");
	level endon("intermission");
	level endon("map_ended");

	self endon("disconnect");
	self endon("spawned");

	if(!isDefined(gts)) gts = false;

	if(!gts)
	{
		// play reload sound for effect
		self playlocalsound("health_pickup_large");

		wait 0.25;

	}
	else self.wwx_firstaidkits = 0;

	// replenish firstaid
	if(isPlayer(self)) self giveFirstAid();

	// refresh the number of firstaid kits on screen
	if(isDefined(self.wwx_firstaidval)) self.wwx_firstaidval setValue(self.wwx_firstaidkits);

	if(!gts)
	{
		// make up for the extra second, temp fix :)
		self thread ww2x\_wwx_utils::cleanBarGraphic();
	}
}

refillWeapon(slot, gts)
{
	thread ww2x\_wwx_logfile::Log("refillWeapon");

	if(!isDefined(self.weapon) || !isDefined(self.weapon[slot].name)) return;

	if(!isDefined(gts)) gts = false;

	// refill all WW2X slots
	weapon = self.weapon[slot].name;

	// sprinting, none or ignore?
	if(!isValidWeapon(weapon)) return;

	clip = self getWeaponSlotClipAmmoDefault(weapon);
	reserve = self getWeaponSlotAmmoDefault(weapon);

	// rank system reserve ammo override
	if(level.wwx_wepo_loadout == 1)
	{
		if(isWeaponType(weapon, "pistol")) rank_suffix = game["rank_ammo_pistolclips_" + self.pers["rank"]];
		else rank_suffix = game["rank_ammo_gunclips_" + self.pers["rank"]];

		reserve = clip * rank_suffix;
	}

	if(!gts)
	{
		// compare the ammo the weapon already has, if its greater, just fill the clip!
		reserve_check = self.weapon[slot].reserve;
		if(reserve_check > reserve) reserve = reserve_check;
	}

	self.weapon[slot].clip = clip;
	self.weapon[slot].reserve = reserve;
	self.weapon[slot].maxammo = self.weapon[slot].clip + self.weapon[slot].reserve;

	// now do the real slots if this weapon is in them!
	if(weapon == self getWeaponSlotWeapon("primary")) self setAmmo("primary", gts);
	else if(weapon == self getWeaponSlotWeapon("primaryb")) self setAmmo("primaryb", gts);
}

updateLoadout(addnew)
{
      if(!isDefined(addnew)) addnew = false;

	// set the ammo for the weapons
	self setAmmo("primary", false, false);
	self setAmmo("primaryb", false, false);

	self giveFirstAid();
	self giveBinoculars();

	currentfrags = 0;
	currentsmokes = 0;
	
	if(addnew)
	{


	// teams share the same weapon file for fire and gas nades, so if one them is enabled, only count own type
	if(level.wwx_firenades || level.wwx_gasnades || level.wwx_stickynades || level.wwx_satchels) currentfrags = self getammocount(self.pers["fragtype"]);
		else currentfrags = self getammocount(self.pers["fragtype"]) + self getammocount(self.pers["enemy_fragtype"]);

	currentsmokes = self getammocount(self.pers["smoketype"]) + self getammocount(self.pers["enemy_smoketype"]);
      }

	if(level.wwx_firenades) self giveFireGrenades();
		else if(level.wwx_gasnades) self giveGasGrenades();
		else if(level.wwx_stickynades) self giveStickyGrenades();
		else if(level.wwx_satchels) self giveSatchelGrenades();
			else self giveGrenades();

	// teams share the same weapon file for fire and gas nades, so if one them is enabled, only count own type
	if(level.wwx_firenades || level.wwx_gasnades || level.wwx_stickynades || level.wwx_satchels) newfrags = self getammocount(self.pers["fragtype"]);
		else newfrags = self getammocount(self.pers["fragtype"]) + self getammocount(self.pers["enemy_fragtype"]);

	newsmokes = self getammocount(self.pers["smoketype"]) + self getammocount(self.pers["enemy_smoketype"]);

	totalfrags = currentfrags + newfrags;
	if(totalfrags > 9) totalfrags = 9;
	totalsmokes = currentsmokes + newsmokes;
	if(totalsmokes > 9) totalsmokes = 9;

	if(totalfrags)
		self setWeaponClipAmmo(self.pers["fragtype"], totalfrags);
	if(totalsmokes)
		self setWeaponClipAmmo(self.pers["smoketype"], totalsmokes);

	if(level.wwx_landmines && level.wwx_landmines_loadout)
	{
		currentlandmines = 0;
		if(addnew) currentlandmines = self.mine_ammo;
		newlandmines = game["rank_ammo_landmines_" + self.pers["rank"]];
		totallandmines = currentlandmines + newlandmines;
		if(totallandmines > 9) totallandmines = 9;
		self thread maps\mp\gametypes\_landmines::giveLandmines(totallandmines);
	}
}

specialLoadout()
{
	self.pers["spawnweapon"] = self.pers["weapon"];

	// mainly required for roundbased games!
	self setWeaponArray();
	self setSpawnWeapons();

	// set the client status for the weapons
	self setWeaponClientStatus(false);

	// set the ammo for the weapons - primary
	self setAmmo("primary", false, true);

	// start weapon/slot monitors
	self thread weaponChangeMonitor();
	self thread oldWeaponMonitor();
	self thread slotWeaponMonitor("primary");
	self thread slotWeaponMonitor("primaryb");

	// pistol allowed?
	allowpistol = true;
	if(!level.wwx_wepo_class && !level.wwx_wepo_sidearm) allowpistol = false; // all weapons available - no pistols allowed
		else if(level.wwx_wepo_class == 1) allowpistol = false;	// weapon classbased pistols only - pistol is handled differently
			else if(level.wwx_wepo_class >= 2 && !level.wwx_wepo_sidearm) allowpistol = false;	// weapon class based with sidearm - pistols allowed

	// give them the pistol is it's allowed
	if(allowpistol) self givePistol();

	// set the client status for the weapons
	self setWeaponClientStatus(false);

	// set the ammo for the weapons - secondary (pistol)
	self setAmmo("primaryb", false, true);

	// self explanitory!
	self giveFirstAid();
	if(level.wwx_firenades) self giveFireGrenades();
		else if(level.wwx_gasnades) self giveGasGrenades();
		else if(level.wwx_stickynades) self giveStickyGrenades();
		else if(level.wwx_satchels) self giveSatchelGrenades();
			else self giveGrenades();

	self giveBinoculars();

	if(!level.wwx_wepo_class)
	{
		self thread slotDummyChecker("primary");
		self thread slotDummyChecker("primaryb");
	}
}
