#Conditional Hooks

$Application: FS2_Open

$On Game Init:
[
	--[[
	WC Saga Hardpoints Gauge script by WC Saga Team
	Modified to work with standard FSO builds by Talon 1024
	Modifications/extensions by Talon 1024
	]]
	
	
	MODE_ONE_GAUGE_PER_BANK = 1
	MODE_TWO_GAUGES_PER_BANK = 2
	
	-- Optimize the script by setting the "mode" so that it doesn't run code it doesn't need to.
	getScriptMode = function(shipClassName)
		if (shipClassName == "F-27B Arrow") then			return MODE_ONE_GAUGE_PER_BANK end
		if (shipClassName == "P-64C Ferret") then			return MODE_ONE_GAUGE_PER_BANK end
		if (shipClassName == "F-86C Hellcat V") then		return MODE_ONE_GAUGE_PER_BANK end
		if (shipClassName == "F-44G Rapier II") then		return MODE_ONE_GAUGE_PER_BANK end
		if (shipClassName == "F-66A Thunderbolt VII")then	return MODE_ONE_GAUGE_PER_BANK end
		if (shipClassName == "F-57B Sabre") then			return MODE_ONE_GAUGE_PER_BANK end
		if (shipClassName == "F-57B Sabre#4Banks") then		return MODE_TWO_GAUGES_PER_BANK end
		if (shipClassName == "F-103A Excalibur") then		return MODE_ONE_GAUGE_PER_BANK end
		if (shipClassName == "F/A-76A Longbow") then		return MODE_ONE_GAUGE_PER_BANK end
		if (shipClassName == "F/A-76A Longbow#4Banks") then	return MODE_TWO_GAUGES_PER_BANK end
		ba.warning("Unknown ship class: " .. shipClassName)
		return 0
	end
	
	-- Frame Helper Functions
	getPrimaryFrame = function(weaponClassName)    
		if (weaponClassName == "Plasma") then return 1 end
		if (weaponClassName == "Meson") then return 2 end
		if (weaponClassName == "Photon") then return 3 end
		if (weaponClassName == "Ion") then return 4 end
		if (weaponClassName == "Laser") then return 5 end
		if (weaponClassName == "Neutron") then return 6 end
		if (weaponClassName == "Tachyon") then return 7 end
		if (weaponClassName == "Reaper") then return 8 end
		if (weaponClassName == "Mass Driver") then return 9 end
		if (weaponClassName == "Particle") then return 10 end
		ba.warning("Unknown Primary Weapon Class: " .. weaponClassName)
		return 0
	end

	getSecondaryFrame = function(weaponClassName)    
		if (weaponClassName == "Javelin HS") then return 1 end
		if (weaponClassName == "Spiculum IR") then return 2 end
		if (weaponClassName == "Pilum FF") then return 3 end
		if (weaponClassName == "Lance") then return 4 end
		if (weaponClassName == "Dart Dumbfire") then return 5 end
		if (weaponClassName == "Porcupine") then return 6 end
		if (weaponClassName == "Leech") then return 7 end
		ba.warning("Unknown Secondary Weapon Class: " .. weaponClassName)
		return 0
	end

	-- Color Helper Functions
	getPrimaryColor = function(weaponClassName)    
		if (weaponClassName == "Plasma") then return {165, 109, 193} end
		if (weaponClassName == "Meson") then return {132, 197, 105} end
		if (weaponClassName == "Photon") then return {240, 179, 76} end
		if (weaponClassName == "Ion") then return {165, 109, 193} end
		if (weaponClassName == "Laser") then return {203, 63, 33} end
		if (weaponClassName == "Neutron") then return {39, 93, 237} end
		if (weaponClassName == "Tachyon") then return {230, 207, 63} end
		if (weaponClassName == "Reaper") then return {101, 151, 186} end
		if (weaponClassName == "Mass Driver") then return {182, 172, 191} end
		if (weaponClassName == "Particle") then return {229, 168, 0} end
		ba.warning("Unknown Primary Weapon Class: " .. weaponClassName)
		return {255, 255, 255}
	end

	getSecondaryColor = function(weaponClassName)    
		if (weaponClassName == "Javelin HS") then return {240, 76, 39} end
		if (weaponClassName == "Spiculum IR") then return {230, 207, 63} end
		if (weaponClassName == "Pilum FF") then return {39, 93, 237} end
		if (weaponClassName == "Lance") then return {240, 147, 67} end
		if (weaponClassName == "Dart Dumbfire") then return {138, 81, 158} end
		if (weaponClassName == "Porcupine") then return {239, 56, 24} end
		if (weaponClassName == "Leech") then return {113, 186, 85} end
		ba.warning("Unknown Secondary Weapon Class: " .. weaponClassName)
		return {255, 255, 255}
	end
	
	makePrimarySelection = function(plr)
		 for i=1, #plr.PrimaryBanks do
			if (plr.PrimaryBanks[i].Armed or plr.PrimaryBanks.Linked) then
				mn.runSEXP('( hud-set-color "WCSHp_PBank' .. i .. 'L" ' .. primaryColor[i][1] .. ' ' .. primaryColor[i][2] .. ' ' .. primaryColor[i][3] .. ' )')
				mn.runSEXP('( hud-set-color "WCSHp_PBank' .. i .. 'R" ' .. primaryColor[i][1] .. ' ' .. primaryColor[i][2] .. ' ' .. primaryColor[i][3] .. ' )')
			else
				mn.runSEXP('( hud-set-color "WCSHp_PBank' .. i .. 'L" 128 128 128 )')
				mn.runSEXP('( hud-set-color "WCSHp_PBank' .. i .. 'R" 128 128 128 )')
			end
		end
	end
	
	makeSecondarySelection = function(plr)
		 for i=1, #plr.SecondaryBanks do
		 
		 
		 
			if scriptMode == MODE_ONE_GAUGE_PER_BANK then
				if plr.SecondaryBanks[i].AmmoLeft == 0 then	--If the secondary bank is empty, don't show it
					mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. '" 0 )')
				else
					mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. '" ' .. secondaryFrame[i] .. ')')	--Take reloading into consideration
				end
				if plr.SecondaryBanks[i].Armed then	--If the secondary bank is armed, set the color of the missile graphic
					mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. '" ' .. secondaryColor[i][1] .. ' ' .. secondaryColor[i][2] .. ' ' .. secondaryColor[i][3] .. ' )')
				else
					mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. '" 128 128 128 )')
				end
			end
			
			
			
			if scriptMode == MODE_TWO_GAUGES_PER_BANK then
				if i <= 2 then
					secAmmoRatio = plr.SecondaryBanks[i].AmmoLeft / plr.SecondaryBanks[i].AmmoMax
					if secAmmoRatio == 0 then -- No ammo left
						mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. 'L" 0 )')
						mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. 'R" 0 )')
					elseif secAmmoRatio > 0.5 then -- Greater than half max ammo
						mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. 'L" ' .. secondaryFrame[i] .. ' )')
						mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. 'R" ' .. secondaryFrame[i] .. ' )')
						if plr.SecondaryBanks[i].Armed then
							mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. 'L" ' .. secondaryColor[i][1] .. ' ' .. secondaryColor[i][2] .. ' ' .. secondaryColor[i][3] .. ' )')
							mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. 'R" 128 128 128 )')
						else
							mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. 'L" 128 128 128 )')
							mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. 'R" 128 128 128 )')
						end
					elseif secAmmoRatio <= 0.5 then -- Less than half max ammo
						mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. 'L" 0 )')
						mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. 'R" ' .. secondaryFrame[i] .. ' )')
						if plr.SecondaryBanks[i].Armed then
							mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. 'R" ' .. secondaryColor[i][1] .. ' ' .. secondaryColor[i][2] .. ' ' .. secondaryColor[i][3] .. ' )')
						else
							mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. 'L" 128 128 128 )')
							mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. 'R" 128 128 128 )')
						end
					end
				else
					if plr.SecondaryBanks[i].AmmoLeft == 0 then -- No ammo left
						mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. '" 0 )')
					else -- Take reloading into consideration
						mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. '" ' .. secondaryFrame[i] .. ' )')
					end
					if plr.SecondaryBanks[i].Armed then
						mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. '" ' .. secondaryColor[i][1] .. ' ' .. secondaryColor[i][2] .. ' ' .. secondaryColor[i][3] .. ' )')
					else
						mn.runSEXP('( hud-set-color "WCSHp_SBank' .. i .. '" 128 128 128 )')
					end
				end
			end
			
			
			
		end
	end
	
	scriptMode = 0
	playerShip = nil
	primaryFrame = {}
	primaryColor = {}
	secondaryFrame = {}
	secondaryColor = {}
	hudInitialized = false
]

$State: GS_STATE_GAME_PLAY

$On HUD Draw:
[
	-- Initialization Section
	
	
	if (not hudInitialized) then
		playerShip = hv.Player
		
		if (playerShip:isValid()) then

			-- Set script mode
			scriptMode = getScriptMode(playerShip.Class.Name)
		
			-- Set Primary Bank Frames
			for i=1, #playerShip.PrimaryBanks do
				primaryFrame[i] = getPrimaryFrame(playerShip.PrimaryBanks[i].WeaponClass.Name)
				primaryColor[i] = getPrimaryColor(playerShip.PrimaryBanks[i].WeaponClass.Name)
				mn.runSEXP('( hud-set-frame "WCSHp_PBank' .. i .. 'L" ' .. primaryFrame[i] .. ')')
				mn.runSEXP('( hud-set-frame "WCSHp_PBank' .. i .. 'R" ' .. primaryFrame[i] .. ')')
			end

			-- Set Secondary Bank Frames
			for i=1, #playerShip.SecondaryBanks do
				secondaryFrame[i] = getSecondaryFrame(playerShip.SecondaryBanks[i].WeaponClass.Name)
				secondaryColor[i] = getSecondaryColor(playerShip.SecondaryBanks[i].WeaponClass.Name)
				mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. '" ' .. secondaryFrame[i] .. ' )')
				mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. 'L" ' .. secondaryFrame[i] .. ' )')
				mn.runSEXP('( hud-set-frame "WCSHp_SBank' .. i .. 'R" ' .. secondaryFrame[i] .. ' )')
			end
		end
	 
		hudInitialized = true
	end
	
	-- Weapon Selection Section
	if missiontime == nil then
		missiontime = mn.getMissionTime()
		oldmissiontime = missiontime
	end
	
	if (playerShip:isValid()) then
		if missiontime ~= nil then
			if hu.HUDDrawn then
				missiontime = mn.getMissionTime()
				
				if oldmissiontime ~= missiontime then
					makePrimarySelection(playerShip)
					makeSecondarySelection(playerShip)
				end
			end
			
			oldmissiontime = missiontime
		end
	end
]

$On Mission End:
[
	hudInitialized = false
]

#End