// weapon selection functions and tables

global WeaponTable = {};
global PrimaryWeaponChoices = {};
PrimaryWeaponChoices[TEAM.AXIS] = {};
PrimaryWeaponChoices[TEAM.ALLIES] = {};

// mod detection
modName = GetModName();
isNq = modName == "noquarter";
isEtnam = modName == "etpub" && GetCvar("g_etnamFlags") > 0;
isSilentMod = modName == "silEnT";
isJaymod = modName == "Jaymod";
isNitmod = modName == "nitmod";
isETblight = modName == "etblight";
isBastard = modName == "bastardmod";
isLegacy = modname == "legacy";

///////////////////////////////////////////////////////////////////////////////////////////
// TABLES
///////////////////////////////////////////////////////////////////////////////////////////

////////////////////
// secondary weapons
////////////////////
Util.TeamSideArms =
{
	AXIS =
	{
		WEAPON.LUGER,
		WEAPON.LUGER_SILENCED,
		WEAPON.LUGER_AKIMBO,
		WEAPON.LUGER_AKIMBO_SILENCED,
		WEAPON.REVOLVER,
	},

	ALLIES =
	{
		WEAPON.COLT,
		WEAPON.COLT_SILENCED,
		WEAPON.COLT_AKIMBO,
		WEAPON.COLT_AKIMBO_SILENCED,
		WEAPON.REVOLVER,
	},
};

////////////////////
// sniper weapons
////////////////////
Util.SniperWeapons =
{
	//these values are for bot.HasWeapon
	WEAPON.FG42_SCOPE,
	WEAPON.K43_SCOPE,
	WEAPON.GARAND_SCOPE,
};

Util.TeamSniperWeapons =
{
	//these values are for bot.ChangePrimaryWeapon
	AXIS =
	{
		WEAPON.K43,
	},

	ALLIES =
	{
		WEAPON.GARAND,
	},
};
if (!isNq) {
	Util.TeamSniperWeapons["ALLIES"][1] = WEAPON.FG42;
}
if (GetModName() == "etpub" && GetCvar("g_etnamFlags") > 0) {
	Util.AddToTable(Util.SniperWeapons, WEAPON.SAK47SCOPED);
	Util.TeamSniperWeapons["AXIS"][1] = WEAPON.SAK47;
} else {
	Util.TeamSniperWeapons["AXIS"][1] = WEAPON.FG42;
}
if (GetModName() == "etblight") {
	Util.AddToTable(Util.SniperWeapons, WEAPON.PROTO);
	Util.AddToTable(Util.SniperWeapons, WEAPON.MP40SS);
	// TeamSniperWeapons are only for COVERTOPS
	Util.TeamSniperWeapons["AXIS"][2] = WEAPON.PROTO;
	Util.TeamSniperWeapons["ALLIES"][2] = WEAPON.PROTO;
}
if (GetModName() == "bastardmod") {
	Util.AddToTable(Util.SniperWeapons, WEAPON.BASTARD_FG42_SCOPE);
}

////////////////////
// soldiers weapons
////////////////////
tbl1 = {};
tbl1[WEAPON.PANZERFAUST] = true; //RPG
tbl1[WEAPON.MOBILE_MG42] = true;
tbl1[WEAPON.FLAMETHROWER] = true;
tbl1[WEAPON.MORTAR] = true;
tbl2 = {};
tbl2[WEAPON.MOBILE_MG42] = true; //MOBILE_BROWNING
tbl2[WEAPON.FLAMETHROWER] = true;
tbl2[WEAPON.MORTAR] = true;

if (isNq) {
	if (GetCvar("jp_insanity") & 256) {
		// if allowed, add venom to the list.
		tbl1[WEAPON.VENOM] = true;
		tbl2[WEAPON.VENOM] = true;
	}
	tbl1[WEAPON.STG44] = true;
	tbl2[WEAPON.BAR] = true;
}else{
	if (isEtnam) {
		tbl1[WEAPON.M79] = true;
		tbl2[WEAPON.M79] = true;
		tbl1[WEAPON.ITHACA] = false;
		tbl2[WEAPON.ITHACA] = false;
		WeaponTable.AlliedPanzer = WEAPON.LAW;
	}else{
		WeaponTable.AlliedPanzer = WEAPON.PANZERFAUST;
	}
	tbl1[WEAPON.MP40] = false;
	tbl2[WEAPON.THOMPSON] = false;
}
if(WEAPON.BAZOOKA){ WeaponTable.AlliedPanzer = WEAPON.BAZOOKA; } //NoQuarter or Legacy

tbl2[WeaponTable.AlliedPanzer] = true;

if (isJaymod) {
	// cs: shouldn't these be enabled?
	tbl1[WEAPON.M97] = false;
	tbl2[WEAPON.M97] = false;
}
if (isSilentMod && GetCvar("g_classWeapons") & 1) {
	// TODO: handle mod version with next release of silEnT
	tbl1[WEAPON.PPSH] = true;
	tbl2[WEAPON.PPSH] = true;
}
if (isETblight) {
	tbl1[WEAPON.DESTROYER] = true;
	tbl2[WEAPON.DESTROYER] = true;
	tbl1[WEAPON.SAW] = false;
	tbl2[WEAPON.SAW] = false;
}
if (isBastard) {
	tbl1[WEAPON.BASTARD_FG42] = true;
	tbl2[WEAPON.BASTARD_FG42] = true;
	tbl1[WEAPON.MINIGUN] = true;
	tbl2[WEAPON.MINIGUN] = true;
}
PrimaryWeaponChoices[TEAM.AXIS][CLASS.SOLDIER] = tbl1;
PrimaryWeaponChoices[TEAM.ALLIES][CLASS.SOLDIER] = tbl2;

/////////////////////
// covertops weapons
/////////////////////
tbl1 = {};
tbl1[WEAPON.K43] = true; //DRAGUNOV
tbl2 = {};
tbl2[WEAPON.GARAND] = true; //M21 RIFLE
tbl2[WEAPON.STEN] = true;

if (isEtnam) {
	tbl1[WEAPON.SAK47] = true;
	tbl1[WEAPON.RPD] = true;
	tbl2[WEAPON.M3A1] = true;
}else{
	tbl1[WEAPON.FG42] = true;
}
if (isNq) {
	tbl1[WEAPON.MP34] = true;
	tbl2[WEAPON.BAR] = true;
}else{
	tbl1[WEAPON.STEN] = true;
	tbl2[WEAPON.FG42] = true; //M16 SCOPED
}
if (isETblight) {
	tbl1[WEAPON.PROTO] = true;
	tbl2[WEAPON.PROTO] = true;
	tbl1[WEAPON.BSMG2] = true;
	tbl2[WEAPON.BSMG2] = true;
}
if (isBastard) {
	tbl1[WEAPON.MP28] = true;
	tbl2[WEAPON.MP28] = true;
	tbl1[WEAPON.MADSEN] = true;
	tbl2[WEAPON.MADSEN] = true;
}
PrimaryWeaponChoices[TEAM.AXIS][CLASS.COVERTOPS] = tbl1;
PrimaryWeaponChoices[TEAM.ALLIES][CLASS.COVERTOPS] = tbl2;

///////////////////
// common weapons
//////////////////
foreach (class in { CLASS.MEDIC, CLASS.ENGINEER, CLASS.FIELDOPS } )
{
	tbl1 = {};
	tbl1[WEAPON.MP40] = true; //AK-47
	tbl2 = {};
	tbl2[WEAPON.THOMPSON] = true; //M16

	if (isEtnam) {
		WeaponTable.Shotgun = WEAPON.ITHACA;
		tbl1[WEAPON.ITHACA] = true;
		tbl2[WEAPON.ITHACA] = true;
		tbl1[WEAPON.RPD] = true;
		tbl2[WEAPON.M3A1] = true;
	}
	if (isNq && class!=CLASS.MEDIC) {
		WeaponTable.Shotgun = WEAPON.SHOTGUN;
		tbl1[WEAPON.SHOTGUN] = true;
		tbl2[WEAPON.SHOTGUN] = true;
	}
	if (isJaymod) {
		WeaponTable.Shotgun = WEAPON.M97;
		tbl1[WEAPON.M97] = true;
		tbl2[WEAPON.M97] = true;
	}
	if (isSilentMod || isNitmod) {
		tbl1[WEAPON.THOMPSON] = true;
		tbl2[WEAPON.MP40] = true;
	}
	if (isNitmod && (GetCvar("g_weapons") & 512) && class!=CLASS.ENGINEER) {
		tbl1[WEAPON.STEN] = true;
		tbl2[WEAPON.STEN] = true;
	}
	if (isETblight) {
		tbl1[WEAPON.MP41] = true;
		tbl2[WEAPON.MP41] = true;
		tbl1[WEAPON.BSMG2] = true;
		tbl2[WEAPON.BSMG2] = true;
		tbl1[WEAPON.MADSEN] = true;
		tbl2[WEAPON.MADSEN] = true;
		if (class!=CLASS.ENGINEER) {
			tbl1[WEAPON.THOMPSON] = true;
			tbl2[WEAPON.MP40] = true;
		}
	}
	if (isBastard) {
		tbl1[WEAPON.MP28] = true;
		tbl2[WEAPON.MP28] = true;
		tbl1[WEAPON.SCROOGE] = true;
		tbl2[WEAPON.SCROOGE] = true;
		tbl1[WEAPON.MADSEN] = true;
		tbl2[WEAPON.MADSEN] = true;
	}
	PrimaryWeaponChoices[TEAM.AXIS][class] = tbl1;
	PrimaryWeaponChoices[TEAM.ALLIES][class] = tbl2;
}

/////////////////////
// engineer weapons
/////////////////////
tbl1 = PrimaryWeaponChoices[TEAM.AXIS][CLASS.ENGINEER];
tbl2 = PrimaryWeaponChoices[TEAM.ALLIES][CLASS.ENGINEER];
tbl1[WEAPON.KAR98] = true;
tbl2[WEAPON.CARBINE] = true;

if (isEtnam) {
	tbl1[WEAPON.M79] = true;
	tbl2[WEAPON.M79] = true;
}
if (isSilentMod && GetCvar("g_classWeapons") & 2 ) {
	tbl1[WEAPON.PPSH] = true;
	tbl2[WEAPON.PPSH] = true;
}
if (isETblight) {
	tbl1[WEAPON.PPSH41] = true;
	tbl2[WEAPON.PPSH41] = true;
}
if (isBastard) {
	tbl1[WEAPON.TOKAREV] = true;
	tbl2[WEAPON.TOKAREV] = true;
}

/////////////////////
// fieldops weapons
/////////////////////
tbl1 = PrimaryWeaponChoices[TEAM.AXIS][CLASS.FIELDOPS];
tbl2 = PrimaryWeaponChoices[TEAM.ALLIES][CLASS.FIELDOPS];
if (isSilentMod && GetCvar("g_classWeapons") & 4 ) {
	tbl1[WEAPON.PPSH] = true;
	tbl2[WEAPON.PPSH] = true;
}


if (isETblight)
{
	/////////////////////
	// scientist weapons
	/////////////////////
	tbl1 = {};
	tbl1[WEAPON.Q3X1] = true;
	tbl1[WEAPON.PPSH41] = true;
	tbl1[WEAPON.STEN] = true;
	tbl1[WEAPON.MP40SS] = true;
	PrimaryWeaponChoices[TEAM.AXIS][CLASS.SCIENTIST] = tbl1;
	PrimaryWeaponChoices[TEAM.ALLIES][CLASS.SCIENTIST] = tableDuplicate(tbl1);

	/////////////////////
	// super soldier weapons
	/////////////////////
	tbl1 = {};
	tbl1[WEAPON.Q3X1] = true;
	tbl1[WEAPON.MINI_BROWNING] = true;
	PrimaryWeaponChoices[TEAM.AXIS][CLASS.SUPER_SOLDIER] = tbl1;
	PrimaryWeaponChoices[TEAM.ALLIES][CLASS.SUPER_SOLDIER] = tableDuplicate(tbl1);
}

///////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////

WeaponTable.SetWeaponTeamAvailability = function(team, class, weapon, enable)
{
	if (IsNull(class)) {
		foreach (classId in Util.PlayerClassTable) {
			this.SetWeaponTeamAvailability(team, classId, weapon, enable);
		}
		return;
	}

	classTable = PrimaryWeaponChoices[team][class];
	if (!classTable) {
		Util.MapDebugPrint("WeaponTable.SetWeaponAvailabilty: No weapon table for class " + Util.ClassName(class), true);
		return;
	}

	if ( typeName(weapon) == "table" ) {
		foreach ( wpn in weapon ) {
			this.SetWeaponTeamAvailability(team, class, wpn, enable);
		}
		return;
	}

	if (weapon == WEAPON.PANZERFAUST && team == TEAM.ALLIES) {
		weapon = WeaponTable.AlliedPanzer;
	}
	if (GetModName() == "noquarter") {
		if (team == TEAM.AXIS && weapon == WEAPON.STEN) {
			weapon = WEAPON.MP34;
		}
	}

	if (!IsNull(classTable[weapon])) { // enable only weapons which are permitted for this team and class
		classTable[weapon] = enable;
	}
};

/*
	WeaponTable.SetWeaponAvailability( CLASS.SOLDIER, WEAPON.FLAMETHROWER, true );
	WeaponTable.SetWeaponAvailability( CLASS.SOLDIER, {WEAPON.MORTAR, WEAPON.MOBILE_MG42}, false);
*/
WeaponTable.SetWeaponAvailability = function(class, weapon, enable)
{
	WeaponTable.SetWeaponTeamAvailability(TEAM.AXIS, class, weapon, enable);
	WeaponTable.SetWeaponTeamAvailability(TEAM.ALLIES, class, weapon, enable);
};

WeaponTable.GetPrimaryWeapons = function(team, class)
{
	return PrimaryWeaponChoices[team][class];
};

// for debug
WeaponTable.ListWeaponAvailability = function()
{
	print("\n//////////// Weapon Availability ////////////");
	foreach ( team and tbl in PrimaryWeaponChoices )
	{
		print(format("\n Team: %s", Util.TeamName(team)));
		foreach ( id and class in tbl) {
			print(format("\n------ %s ------", Util.ClassName(id)));

			foreach ( w and e in class ) {
				print(format("%s = %d", Util.WeaponName(w), e));
			}
		}
	}
};
