;The bitching betty script. Monitors subsystem health status, and queues up audio warnings from sounds.tbl to blast at the player.

#Conditional Hooks

$Application: FS2_Open

$State: GS_STATE_GAME_PLAY

$On State Start:
[
function initbetty()
	if not bettyinited then
		betty = {}
		if not betty.plr then
			betty.plr = hv.Player
			if betty.plr:isValid() then
				betty.plrship = mn.getObjectFromSignature(betty.plr:getSignature())
			end
		end
		if betty.plrship:isValid() then
			betty.subsystems = {}
			betty.subsystems.comms = false
			betty.subsystems.engine = false
			betty.subsystems.engine01 = false
			betty.subsystems.engine02 = false
			betty.subsystems.engine03 = false
			betty.subsystems.engine04 = false
			betty.subsystems.nav = false
			betty.subsystems.sensors = false
			betty.subsystems.weapons = false
			betty.status = {}
			betty.status.secondarywarned = {false, false, false, false}
			betty.status.secondaryemptywarned = {false, false, false, false}
			betty.status.primarywarned = {}
			betty.status.primarywarned = {false, false, false}
			betty.status.primaryemptywarned = {}
			betty.status.primaryemptywarned = {false, false, false}
			betty.queue = {}
			betty.queueprocessdelay = 2 --Minimum delay for queue processing

			local soundOffset = 0
			local shipSpecies = betty.plrship.Class.Species.Name
			if shipSpecies == "Federation" then
				soundOffset = 0
			elseif shipSpecies == "Commonwealth" then
				soundOffset = 1
			elseif shipSpecies == "Combine" then
				soundOffset = 2 -- And so on until all species have been specified
			end 
 
			betty.comms =			500 + (soundOffset * 22) --see sounds.tbl
			betty.engine =			501 + (soundOffset * 22) 
			betty.nav = 			502 + (soundOffset * 22)
			betty.sensors = 		503 + (soundOffset * 22)
			betty.weapons = 		504 + (soundOffset * 22)
			betty.hull = 			505 + (soundOffset * 22)
			betty.eject = 			506 + (soundOffset * 22)
			betty.primary = {}
			betty.primary[1] =		507 + (soundOffset * 22)
			betty.primary[2] =		508 + (soundOffset * 22)
			betty.primary[3] =		509 + (soundOffset * 22)
			betty.secondary = {}
			betty.secondary[1] = 	510 + (soundOffset * 22)
			betty.secondary[2] = 	511 + (soundOffset * 22)
			betty.secondary[3] = 	512 + (soundOffset * 22)
			betty.secondary[4] = 	513 + (soundOffset * 22)
			betty.secondaryempty = {}
			betty.secondaryempty[1] = 514 + (soundOffset * 22)
			betty.secondaryempty[2] = 515 + (soundOffset * 22)
			betty.secondaryempty[3] = 516 + (soundOffset * 22)
			betty.secondaryempty[4] = 517 + (soundOffset * 22)
			betty.primaryempty = {}
			betty.primaryempty[1] = 518 + (soundOffset * 22)
			betty.primaryempty[2] = 519 + (soundOffset * 22)
			betty.primaryempty[3] = 520 + (soundOffset * 22)
			betty.subspace = 		521 + (soundOffset * 22)


			if not betty.subsystems.comms then
				if betty.plrship["communications"]:isValid() then
					betty.subsystems.comms = betty.plrship["communications"]
				end
			end

			if not betty.subsystems.engine then
				if betty.plrship["engine"]:isValid() then
					betty.subsystems.engine = betty.plrship["engine"]
					betty.enginenum = 0
				end
				if betty.plrship["engines"]:isValid() then
					betty.subsystems.engine = betty.plrship["engines"]
					betty.enginenum = 0
				end
			end

			if not betty.subsystems.engine01 then
				if betty.plrship["engine01"]:isValid() then
					betty.subsystems.engine01 = betty.plrship["engine01"]
					betty.enginenum = 1
				end
			end

			if not betty.subsystems.engine02 then
				if betty.plrship["engine02"]:isValid() then
					betty.subsystems.engine02 = betty.plrship["engine02"]
					betty.enginenum = 2
				end
			end

			if not betty.subsystems.engine03 then
				if betty.plrship["engine03"]:isValid() then
					betty.subsystems.engine03 = betty.plrship["engine03"]
					betty.enginenum = 3
				end
			end

			if not betty.subsystems.engine04 then
				if betty.plrship["engine04"]:isValid() then
					betty.subsystems.engine04 = betty.plrship["engine04"]
					betty.enginenum = 4
				end
			end

			if not betty.subsystems.engine01 then
				if betty.plrship["engine01a"]:isValid() then
					betty.subsystems.engine01 = betty.plrship["engine01a"]
					betty.enginenum = 1
				end
			end

			if not betty.subsystems.engine02 then
				if betty.plrship["engine02a"]:isValid() then
					betty.subsystems.engine02 = betty.plrship["engine02a"]
					betty.enginenum = 2
				end
			end

			if not betty.subsystems.engine03 then
				if betty.plrship["engine03a"]:isValid() then
					betty.subsystems.engine03 = betty.plrship["engine03a"]
					betty.enginenum = 3
				end
			end

			if not betty.subsystems.engine04 then
				if betty.plrship["engine04a"]:isValid() then
					betty.subsystems.engine04 = betty.plrship["engine04a"]
					betty.enginenum = 4
				end
			end

			if not betty.subsystems.nav then
				if betty.plrship["navigation"]:isValid() then
					betty.subsystems.nav = betty.plrship["navigation"]
				end
			end

			if not betty.subsystems.sensors then
				if betty.plrship["sensors"]:isValid() then
					betty.subsystems.sensors = betty.plrship["sensors"]
				end
			end

			if not betty.subsystems.weapons then
				if betty.plrship["weapons"]:isValid() then
					betty.subsystems.weapons = betty.plrship["weapons"]
				end
			end

			betty.queue.timestamp = 0
			bettyinited = true
			getenginemaxhealth()
		else
			bettyinited = false
			BettyOverride = true
		end
	end
end

function getenginemaxhealth()
	if betty.enginenum == 0 then
		betty.enginemaxhealth = betty.subsystems.engine.HitpointsMax
	elseif betty.enginenum == 1 then
		betty.enginemaxhealth = betty.subsystems.engine01.HitpointsMax
	elseif betty.enginenum == 2 then
		betty.enginemaxhealth = betty.subsystems.engine01.HitpointsMax + betty.subsystems.engine02.HitpointsMax
	elseif betty.enginenum == 3 then
		betty.enginemaxhealth = betty.subsystems.engine01.HitpointsMax + betty.subsystems.engine02.HitpointsMax + betty.subsystems.engine03.HitpointsMax
	elseif betty.enginenum == 4 then
		betty.enginemaxhealth = betty.subsystems.engine01.HitpointsMax + betty.subsystems.engine02.HitpointsMax + betty.subsystems.engine03.HitpointsMax + betty.subsystems.engine04.HitpointsMax
	end
end

function getenginehealth()
	if betty.enginenum == 0 then
		betty.enginehealth = (betty.subsystems.engine.HitpointsLeft) / betty.enginemaxhealth
	elseif betty.enginenum == 1 then
		betty.enginehealth = (betty.subsystems.engine01.HitpointsLeft) / betty.enginemaxhealth
	elseif betty.enginenum == 2 then
		betty.enginehealth = (betty.subsystems.engine01.HitpointsLeft + betty.subsystems.engine02.HitpointsLeft) / betty.enginemaxhealth
	elseif betty.enginenum == 3 then
		betty.enginehealth = (betty.subsystems.engine01.HitpointsLeft + betty.subsystems.engine02.HitpointsLeft + betty.subsystems.engine03.HitpointsLeft) / betty.enginemaxhealth
	elseif betty.enginenum == 4 then
		betty.enginehealth = (betty.subsystems.engine01.HitpointsLeft + betty.subsystems.engine02.HitpointsLeft + betty.subsystems.engine03.HitpointsLeft + betty.subsystems.engine04.HitpointsLeft) / betty.enginemaxhealth
	end
end

function queueWarning(warning)
	local i = 1
	queued = false
	while not queued do
		if betty.queue[i] == nil then
			betty.queue[i] = warning
			queued = true
		else
			i = i + 1
		end
	end
end

function processQueue()
	local mtime = mn.getMissionTime()

	if mtime ~= 0 then
		if (mtime - betty.queue.timestamp) > betty.queueprocessdelay then
			if betty.queue[1] ~= nil then
				ad.playGameSound(betty.queue[1], 0, 100, 0)

				local i = 1

				local processed = false
				while not processed do
					if betty.queue[i + 1] ~= nil then
						betty.queue[i] = betty.queue[i + 1]
						i = i + 1
					else
						betty.queue[i] = nil
						processed = true
					end
				end

				betty.queue.timestamp = mtime
			end
		end
	end
end

function printQueue()
	local i = 1
	if betty.queue[1] ~= nil then
		while betty.queue[i] ~= nil do
			ba.print("BETTY DEBUG Item: " .. i .. ": " .. betty.queue[i] .. "\n")
			i = i + 1
		end
	end
end

function processTriggers()
	if betty.subsystems.comms then
		if betty.subsystems.comms.HitpointsLeft/betty.subsystems.comms.HitpointsMax < 0.01 and not betty.status.commwarned then
			queueWarning(betty.comms)
			betty.status.commwarned = true
		elseif betty.subsystems.comms.HitpointsLeft/betty.subsystems.comms.HitpointsMax > 0.1 then
			betty.status.commwarned = false
		end
	end

	getenginehealth()

	if betty.enginehealth < 0.01 and not betty.status.enginewarned then
		queueWarning(betty.engine)
		betty.status.enginewarned = true
	elseif betty.enginehealth > 0.1 then
		betty.status.enginewarned = false
	end

	if betty.subsystems.nav then
		if betty.subsystems.nav.HitpointsLeft/betty.subsystems.nav.HitpointsMax < 0.01 and not betty.status.navwarned then
			queueWarning(betty.nav)
			betty.status.navwarned = true
		elseif betty.subsystems.nav.HitpointsLeft/betty.subsystems.nav.HitpointsMax > 0.1 then
			betty.status.navwarned = false
		end
	end

	if betty.subsystems.sensors then
		if betty.subsystems.sensors.HitpointsLeft/betty.subsystems.sensors.HitpointsMax < 0.01 and not betty.status.sensorswarned then
			queueWarning(betty.sensors)
			betty.status.sensorswarned = true
		elseif betty.subsystems.sensors.HitpointsLeft/betty.subsystems.sensors.HitpointsMax > 0.1 then
			betty.status.sensorswarned = false
		end
	end

	if betty.subsystems.weapons then
		if betty.subsystems.weapons.HitpointsLeft/betty.subsystems.weapons.HitpointsMax < 0.01 and not betty.status.weaponswarned then
			queueWarning(betty.weapons)
			betty.status.weaponswarned = true
		elseif betty.subsystems.weapons.HitpointsLeft/betty.subsystems.weapons.HitpointsMax > 0.1 then
			betty.status.weaponswarned = false
		end
	end

	if betty.plrship.HitpointsLeft/betty.plrship.HitpointsMax < 0.41 and not betty.status.hullwarned then
		queueWarning(betty.hull)
		betty.status.hullwarned = true
	elseif betty.plrship.HitpointsLeft/betty.plrship.HitpointsMax > 0.41 then
		betty.status.hullwarned = false
	end

	if betty.plrship.HitpointsLeft/betty.plrship.HitpointsMax < 0.1 and not betty.status.ejectwarned then
		queueWarning(betty.eject)
		betty.status.ejectwarned = true
	elseif betty.plrship.HitpointsLeft/betty.plrship.HitpointsMax > 0.1 then
		betty.status.ejectwarned = false
	end

	for i = 1, 3, 1 do
		if betty.plrship.PrimaryBanks[i]:isValid() then
			if betty.plrship.PrimaryBanks[i].WeaponClass.Name == "Gattler" or betty.plrship.PrimaryBanks[i].WeaponClass.Name == "Archer" or betty.plrship.PrimaryBanks[i].WeaponClass.Name == "UX Accelerator" or betty.plrship.PrimaryBanks[i].WeaponClass.Name == "Redeemer" or betty.plrship.PrimaryBanks[i].WeaponClass.Name == "Vajra" then
				if betty.plrship.PrimaryBanks[i].AmmoLeft/betty.plrship.PrimaryBanks[i].AmmoMax < 0.26 and not betty.status.primarywarned[i] then
					queueWarning(betty.primary[i])
					betty.status.primarywarned[i] = true
				elseif betty.plrship.PrimaryBanks[i].AmmoLeft/betty.plrship.PrimaryBanks[i].AmmoMax > 0.27 then
					betty.status.primarywarned[i] = false
				end

				if betty.plrship.PrimaryBanks[i].AmmoLeft == 0 and not betty.status.primaryemptywarned[i] then
					queueWarning(betty.primaryempty[i])
					betty.status.primaryemptywarned[i] = true
				elseif betty.plrship.PrimaryBanks[i].AmmoLeft > 0 then
					betty.status.primaryemptywarned[i] = false
				end
			end
		end
	end

	for i = 1, 4, 1 do
		if betty.plrship.SecondaryBanks[i]:isValid() then
			if betty.plrship.SecondaryBanks[i].AmmoLeft/betty.plrship.SecondaryBanks[i].AmmoMax < 0.26 and not betty.status.secondarywarned[i] then
				queueWarning(betty.secondary[i])
				betty.status.secondarywarned[i] = true
			elseif betty.plrship.SecondaryBanks[i].AmmoLeft/betty.plrship.SecondaryBanks[i].AmmoMax > 0.27 then
				betty.status.secondarywarned[i] = false
			end

			if betty.plrship.SecondaryBanks[i].AmmoLeft == 0 and not betty.status.secondaryemptywarned[i] then
				queueWarning(betty.secondaryempty[i])
				betty.status.secondaryemptywarned[i] = true
			elseif betty.plrship.SecondaryBanks[i].AmmoLeft > 0 then
				betty.status.secondaryemptywarned[i] = false
			end
		end
	end
end
]

$On Frame:
[
if not BettyOverride then
	ttime = mn.getMissionTime()

	if ttime ~= 0 then
		if not bettyinited then
			initbetty()
		else
			if betty.plrship.Class.Species.Name == "Federation" then
				processTriggers()
				printQueue()
				processQueue()
		else
			if betty.plrship.Class.Species.Name == "Commonwealth" then
				processTriggers()
				printQueue()
				processQueue()
			else
				betty = nil
				bettyinited = false
				BettyOverride = true
			end
		end
		end
	end
end
]

$On State End:
[
	if bettyinited then
		i = 1
		while betty.queue[i] ~= nil do
			betty.queue[i] = nil
			ba.print("BETTY DEBUG: Clearing queue item " .. i .. "\n")
			i = i + 1
		end
		betty = nil
		bettyinited = false
	end
]

#End