// #autoload
// #include = support/weapon_list.cs
// #include = support/loadout.cs
// #author = monkeyhero
// #credit = hybrid for moral support and general brikemannery
// #version = 1.1

// Changes FOV to value set by $mhChainFOV whenever you start shootin dudes with your chaingun
// This script will also track if you switch to chaingun and click to fire while waiting for another weapon's cooldown to elapse and
// change FOV accordingly.

$mhChainFOV = 90;

package mhChainFOV
{	// Called whenever we want to shoot dudes
	function mouseFire(%val)
	{	parent::mouseFire(%val);
		// if chaingun is selected, and we're not zooming, then change the FOV
		if (%val && $chaingunOut && !$zoomOn)
			setFOV($mhChainFOV);
		else if (!%val && !$zoomOn) // and change it back when we're done shooting
			setFOV($pref::player::defaultFov);
	}
	
	// These next functions are all different ways for Tribes 2 players to change their weapons.
	
	function prevWeapon(%val)
	{	parent::prevWeapon(%val);
		mhChainFOVCycleWeapon(-1);
	}
	
	function nextWeapon(%val)
	{	parent::nextWeapon(%val);
		mhChainFOVCycleWeapon(1);
	}
	
	function cycleWeaponAxis(%val)
	{	parent::cycleWeaponAxis(%val);
		if ( %val < 0 ) // This means next weapon
			mhChainFOVCycleWeapon(1);
		else
			mhChainFOVCycleWeapon(-1);
	}
	
	// This seems redundant
	function cycleNextWeaponOnly(%val)
	{	parent::cycleNextWeaponOnly(%val);
		mhChainFOVCycleWeapon(1);
	}
	
	function useFirstWeaponSlot(%val)
	{	parent::useFirstWeaponSlot(%val);
		mhChainFOVChangeWeapon(0);
	}
	
	function useSecondWeaponSlot(%val)
	{	parent::useSecondWeaponSlot(%val);
		mhChainFOVChangeWeapon(1);
	}
	
	function useThirdWeaponSlot(%val)
	{	parent::useThirdWeaponSlot(%val);
		mhChainFOVChangeWeapon(2);
	}
	
	function useFourthWeaponSlot(%val)
	{	parent::useFourthWeaponSlot(%val);
		mhChainFOVChangeWeapon(3);
	}
	
	function useFifthWeaponSlot(%val)
	{	parent::useFifthWeaponSlot(%val);
		mhChainFOVChangeWeapon(4);
	}
	
	function useSixthWeaponSlot(%val)
	{	parent::useSixthWeaponSlot(%val);
		mhChainFOVChangeWeapon(5);
	}
	
	// This should allow us to handle any scripts that use loadout.cs's weapon switching from support.vl2
	// I've only tested it with tapWeapon by uberguy, but it should work fine for other scripts as well.
	function loadout::useWeapon(%this, %weapon)
	{	parent::useWeapon(%this, %weapon);
		if (%weapon $= "Chaingun" || %weapon $= "TR2Chaingun")
			$chaingunOut = true;
		else
			$chaingunOut = false;
	}
};

activatePackage(mhChainFOV);
callback.add(KillCallback, mhChainFOVDeathWatch);

// Functions for tracking weapon changes

function mhChainFOVChangeWeapon(%val)
{	if (weaponList.weaponHudItem(%val) $= "Chaingun" || weaponList.weaponHudItem(%val) $= "TR2Chaingun")
		$chaingunOut = true;
	else
		$chaingunOut = false;
}

function mhChainFOVCycleWeapon(%val)
{	// Find out how many weapons we have (We want our # to be 1 less because our index is 0-based)
	%numWeapons = weaponList.loadoutSize - 1;
	if (loadout.isloadoutWeapon("TargetingLaser")) // Don't count targeting lasers since they're not in your regular weapon rotation
		%numWeapons--;
	
	%num = weaponList.WeaponHudIndex(loadout.getCurrentWeapon()) + %val;
		
	if (%num > %numWeapons)
		%num = 0;
	if (%num < 0)
		%num = %numWeapons;
	
	if (weaponList.weaponHudItem(%num) $= "Chaingun" || weaponList.weaponHudItem(%num) $= "TR2Chaingun")
		$chaingunOut = true;
	else
		$chaingunOut = false;
}

// Simply watches to see if we die and deactivates the FOV switching.  Otherwise it keeps switching FOV any time you click to respawn
// if you died w/ chaingun in your hands.
function mhChainFOVDeathWatch(%type, %killer, %victim, %weapon, %i_die, %i_win, %suicide, %tk)
{	if (%i_die)
		$chaingunOut = false;
}