#library "STARTWEP"
#include "zcommon.acs"
#include "AACS.acs"
#import "QCDE.bcs"
#import "WEAPCNST.bcs"

//=============================================================================
//=============================================================================
//
//		STARTING WEAPON
//
//=============================================================================
//=============================================================================

script "QCDE_StartingWeapon_Apply" (void)
{
	//PrintBold(s:"Applying starting weapon");

	if (GetCVar("qc_defrag"))
	{
		// Take away all weapons, let maps decide what weapons are available
		TakeInventory("QCGauntlet",1);			TakeInventory("QCGauntletDM",1);
		TakeInventory("QCMachinegun",1);		TakeInventory("QCMachinegunDM",1);		TakeInventory("QCBulletAmmo",	999);
		TakeInventory("QCShotgun",1);			TakeInventory("QCShotgunDM",1);			TakeInventory("QCShellAmmo",	999);
		TakeInventory("QCNailgun",1);			TakeInventory("QCNailgunDM",1);			TakeInventory("QCNailAmmo",		999);
		TakeInventory("QCTribolt",1);			TakeInventory("QCTriboltDM",1);			TakeInventory("QCBoltAmmo", 	999);
		TakeInventory("QCRocketLauncher",1);	TakeInventory("QCRocketLauncherDM",1);	TakeInventory("QCRocketAmmo",	999);
		TakeInventory("QCLightningGun",1);		TakeInventory("QCLightningGunDM",1);	TakeInventory("QCCoilAmmo", 	999);
		TakeInventory("QCRailgun",1);			TakeInventory("QCRailgunDM",1);			TakeInventory("QCSlugAmmo", 	999);
		TakeInventory("QCBFG",1);				TakeInventory("QCBFGDM",1);				TakeInventory("QCBFGAmmo",		999);
		terminate;
	}
	if (GetCVar("qc_dm"))
	{
	GiveInventory("QCHeavyMachinegunDM", 1);	SetInventory("QCBulletAmmo",   getGenericDMAmmoCapacity(0));
	GiveInventory("QCSuperShotgunDM",    1);	SetInventory("QCShellAmmo",    getGenericDMAmmoCapacity(1));
	GiveInventory("QCSuperNailgunDM",    1);	SetInventory("QCNailAmmo",     getGenericDMAmmoCapacity(2));
	GiveInventory("QCTriboltDM",         1);	SetInventory("QCBoltAmmo",     getGenericDMAmmoCapacity(3));
	GiveInventory("QCRocketLauncherDM",  1);	SetInventory("QCRocketAmmo",   getGenericDMAmmoCapacity(4));
	GiveInventory("QCLightningGunDM",    1);	SetInventory("QCCoilAmmo",     getGenericDMAmmoCapacity(5));
	GiveInventory("QCRailgunDM",         1);	SetInventory("QCSlugAmmo",     getGenericDMAmmoCapacity(6));
		terminate;
	}

	// Give DM version if starting in a DM game
	str weapon1 = "QCMachinegun"; 	if(GameType() >= 2 || InCampaign()) weapon1 = "QCMachinegunDM";
	str weapon2 = "QCMachinegun"; 	if(GameType() >= 2 || InCampaign()) weapon2 = "QCShotgunDM";
	str weapon3 = "QCNailgun"; 		if(GameType() >= 2 || InCampaign()) weapon3 = "QCNailgunDM";

	if (GameType() >= 2 || InCampaign())
	{
		SetAmmoCapacity("QCBulletAmmo",	getGenericDMAmmoCapacity(0));
		SetAmmoCapacity("QCShellAmmo",	getGenericDMAmmoCapacity(1));
		SetAmmoCapacity("QCNailAmmo",	getGenericDMAmmoCapacity(2));
		SetAmmoCapacity("QCBoltAmmo", 	getGenericDMAmmoCapacity(3));
		SetAmmoCapacity("QCRocketAmmo",	getGenericDMAmmoCapacity(4));
		SetAmmoCapacity("QCCoilAmmo", 	getGenericDMAmmoCapacity(5));
		SetAmmoCapacity("QCSlugAmmo", 	getGenericDMAmmoCapacity(6));
		SetAmmoCapacity("QCBFGAmmo",	getGenericDMAmmoCapacity(7));
	}

	int rocketarena = GetCvar("rocketarena");
	if (rocketarena) GiveRocketArena(rocketarena);
	else if (GetCvar("instagib"))                  { giveInstagibItems(); GiveInventory("EndlessInfiniteAmmo",1); }
	else if (GetCvar("buckshot"))                  { giveBuckshotItems(); GiveInventory("EndlessInfiniteAmmo",1); }
	else if (GetCvar("lastmanstanding") || GetCvar("teamlms") || GetCvar("qc_freezetag")) { GiveLMS(); }
	else if (CheckInventory("Class_Sam"))          { ACS("QCDE_Passive_Sam"); }
	else
	{
		if (GetCVar("allstarterweapons"))
		{
			if (!CheckInventory(weapon1)) GiveInventory(weapon1, 1);
			if (!CheckInventory(weapon2)) GiveInventory(weapon2, 1);
			if (!CheckInventory(weapon3)) GiveInventory(weapon3, 1);
		}
		else
		{
			int cvar = GetCvar("cl_startingWeapon");
			switch(cvar)
			{
			default: // go to case 1
			case 1: if (!CheckInventory(weapon1)) GiveInventory(weapon1, 1);	break;
			case 2: if (!CheckInventory(weapon2)) GiveInventory(weapon2, 1);	break;
			case 3: if (!CheckInventory(weapon3)) GiveInventory(weapon3, 1);	break;
			}
		}
	}

	// Gauntlet is also a starting weapon
	TakeInventory("QCGauntlet",1);
	delay(1);
	GiveInventory("QCGauntlet",1);

	// Don't re-give weak weapons if stronger ones are present in coop
	if(CheckInventory("QCHeavyMachinegun")) TakeInventory(weapon1,1);
	if(CheckInventory("QCSuperNailgun"   )) TakeInventory(weapon3,1);
}

script "QCDE_SelectUpgradedWeapon" (int weap)
{
	if (weap == 1 && StrCmp(GetWeapon(), "QCMachinegun") == 0)		SetWeapon("QCHeavyMachinegun");
	if (weap == 1 && StrCmp(GetWeapon(), "QCMachinegunDM") == 0)	SetWeapon("QCHeavyMachinegunDM");
	if (weap == 2 && StrCmp(GetWeapon(), "QCShotgun") == 0)			SetWeapon("QCSuperShotgun");
	if (weap == 2 && StrCmp(GetWeapon(), "QCShotgunDM") == 0)		SetWeapon("QCSuperShotgunDM");
	if (weap == 3 && StrCmp(GetWeapon(), "QCNailgun") == 0)			SetWeapon("QCSuperNailgun");
	if (weap == 3 && StrCmp(GetWeapon(), "QCNailgunDM") == 0)		SetWeapon("QCSuperNailgunDM");

	SetResultValue(0);
}

int map_random_weapons;
function void GiveRocketArena(int ra)
{
	// Reduce ammo cap to have nicely filled icons in weapon bar
	SetAmmoCapacity("QCBulletAmmo", getGenericDMAmmoCapacity(0));	SetInventory("QCBulletAmmo",   getGenericDMAmmoCapacity(0));
	SetAmmoCapacity("QCShellAmmo" , getGenericDMAmmoCapacity(1));	SetInventory("QCShellAmmo",    getGenericDMAmmoCapacity(1));
	SetAmmoCapacity("QCNailAmmo"  , getGenericDMAmmoCapacity(2));	SetInventory("QCNailAmmo",     getGenericDMAmmoCapacity(2));
	SetAmmoCapacity("QCBoltAmmo"  , getGenericDMAmmoCapacity(3));	SetInventory("QCBoltAmmo",     getGenericDMAmmoCapacity(3));
	SetAmmoCapacity("QCRocketAmmo", getGenericDMAmmoCapacity(4));	SetInventory("QCRocketAmmo",   getGenericDMAmmoCapacity(4));
	SetAmmoCapacity("QCCoilAmmo"  , getGenericDMAmmoCapacity(5));	SetInventory("QCCoilAmmo",     getGenericDMAmmoCapacity(5));
	SetAmmoCapacity("QCSlugAmmo"  , getGenericDMAmmoCapacity(6));	SetInventory("QCSlugAmmo",     getGenericDMAmmoCapacity(6));
	SetAmmoCapacity("QCBFGAmmo"   , getGenericDMAmmoCapacity(7));	SetInventory("QCBFGAmmo",      getGenericDMAmmoCapacity(7));

		 if(ra == 1)	giveRocketArenaItems();		// ROCKET arena
	else if(ra == 2)	giveBuckshotItems();		// Buckshot
	else if(ra == 3)	giveUnholyTrinityItems();	// Unholy Trinity
	else if(ra == 4)	giveLightningStormItems();	// Lightning Storm
	else if(ra == 5) // Random Weapons (per map)
	{
		if(!map_random_weapons)
		{
			int digit = 7;
			while(digit --> 0)  map_random_weapons = map_random_weapons * 10 + random(0,1);

			// Ensure at least 2 weapons
			map_random_weapons += power(10, random(0,3));
			map_random_weapons += power(10, random(4,6));
		}

		ra = map_random_weapons;
	}
	else if(ra == 6) // Random Weapons (per life)
	{
		ra = 0;
		digit = 7;
		while(digit --> 0) 	ra = ra * 10 + random(0,1);

		// Ensure at least 2 weapons
		ra += power(10, random(0,3));
		ra += power(10, random(4,6));
	}

	if(ra > 8) // Custom bitfield (in decimals (bruh))
	{
		if (ACS_NamedExecuteWithResult("MultiplayerCheck", 0, 0) == 2)
		{
			if(ra % 10) GiveInventory("QCHeavyMachinegunDM", 1); ra /= 10; // __ _ ___ _ __X
			if(ra % 10) GiveInventory("QCSuperShotgunDM"   , 1); ra /= 10; // __ _ ___ _ _X_
			if(ra % 10) GiveInventory("QCSuperNailgunDM"   , 1); ra /= 10; // __ _ ___ _ X__
			if(ra % 10) GiveInventory("QCTriboltDM"        , 1); ra /= 10; // __ _ ___ X ___
			if(ra % 10) GiveInventory("QCRocketLauncherDM" , 1); ra /= 10; // __ _ __X _ ___
			if(ra % 10) GiveInventory("QCLightningGunDM"   , 1); ra /= 10; // __ _ _X_ _ ___
			if(ra % 10) GiveInventory("QCRailgunDM"        , 1); ra /= 10; // __ _ X__ _ ___
			if(ra % 10) GiveInventory("QCBFGDM"            , 1); ra /= 10; // __ X ___ _ ___
		}
		else
		{
			if(ra % 10) GiveInventory("QCHeavyMachinegun"  , 1); ra /= 10; // __ _ ___ _ __X
			if(ra % 10) GiveInventory("QCSuperShotgun"     , 1); ra /= 10; // __ _ ___ _ _X_
			if(ra % 10) GiveInventory("QCSuperNailgun"     , 1); ra /= 10; // __ _ ___ _ X__
			if(ra % 10) GiveInventory("QCTribolt"          , 1); ra /= 10; // __ _ ___ X ___
			if(ra % 10) GiveInventory("QCRocketLauncher"   , 1); ra /= 10; // __ _ __X _ ___
			if(ra % 10) GiveInventory("QCLightningGun"     , 1); ra /= 10; // __ _ _X_ _ ___
			if(ra % 10) GiveInventory("QCRailgun"          , 1); ra /= 10; // __ _ X__ _ ___
			if(ra % 10) GiveInventory("QCBFG"              , 1); ra /= 10; // __ X ___ _ ___
		}

		if(ra % 10) GiveInventory("QCMegaMega"         , 1); ra /= 10; // _X _ ___ _ ___
		if(ra % 10) GiveInventory("QCMegaMega"         , 1); ra /= 10; // X_ _ ___ _ ___
		// Impossible to have more flags with this system, can be redone later to allow.
	}

	int cvar = GetCvar("cl_startingWeapon");
	switch(cvar)
	{
	default: // go to case 1
	case 1: if (CheckInventory("QCHeavyMachinegunDM")) SetWeapon("QCHeavyMachinegunDM");	break;
	case 2: if (CheckInventory("QCSuperShotgunDM")) SetWeapon("QCSuperShotgunDM");			break;
	case 3: if (CheckInventory("QCSuperNailgunDM")) SetWeapon("QCSuperNailgunDM");			break;
	}

	GiveInventory("EndlessInfiniteAmmo",1);
	GiveInventory("NoAmmoCount",1);
}

function void GiveLMS()
{
	GiveInventory("QCGauntlet",1);

	GiveInventory("QCHeavyMachinegunDM", 1);	SetInventory("QCBulletAmmo",   getGenericDMAmmoCapacity(0));
	GiveInventory("QCSuperShotgunDM",    1);	SetInventory("QCShellAmmo",    getGenericDMAmmoCapacity(1));
	GiveInventory("QCSuperNailgunDM",    1);	SetInventory("QCNailAmmo",     getGenericDMAmmoCapacity(2));
	GiveInventory("QCTriboltDM",         1);	SetInventory("QCBoltAmmo",     getGenericDMAmmoCapacity(3));
	GiveInventory("QCRocketLauncherDM",  1);	SetInventory("QCRocketAmmo",   getGenericDMAmmoCapacity(4));
	GiveInventory("QCLightningGunDM",    1);	SetInventory("QCCoilAmmo",     getGenericDMAmmoCapacity(5));
	GiveInventory("QCRailgunDM",         1);	SetInventory("QCSlugAmmo",     getGenericDMAmmoCapacity(6));

	int cvar = GetCvar("cl_startingWeapon");
	switch(cvar)
	{
	default: // go to case 1
	case 1: SetWeapon("QCHeavyMachinegunDM");	break;
	case 2: SetWeapon("QCSuperShotgunDM");		break;
	case 3: SetWeapon("QCSuperNailgunDM");		break;
	}
}

function int power(int a, int b)
{
	int res = 1;
	while(b --> 0) res *= a;
	return res;
}
