Untested, aside from checking for any real big syntax errors.
turretsLow-sct.tbm
#Conditional Hooks
$Application: Freespace 2
$On Game Init: [
tL_ammoThreshold = 0.2 --How low ammo has to be to be 'low'
tL_subsystemThreshold = 0.8 --How many subsystems need low ammo for ship to be 'low'
function turretsLow(shipName)
local ship = mn.Ships[shipName]
--If ship doesn't exist or is dead,
--it doesn't need ammo
if not ship:isValid() then
return 0
end
local numSubsystems = #ship
local numLowSubsystems = 0
for i=1,numSubsystems do
local subsystem = ship[i]
local primariesLow = false
--Primaries
local numBanks = #subsystem.PrimaryBanks
for j=1,numBanks do
local bank = subsystem.PrimaryBanks[j]
if (bank.AmmoLeft / bank.AmmoMax) <= tL_ammoThreshold then
primariesLow = true
break
end
end
if primariesLow then
numLowSubsystems = numLowSubsystems+1
else
--Secondaries
local numBanks = #subsystem.SecondaryBanks
for j=1,numBanks do
local bank = subsystem.SecondaryBanks[j]
if (bank.AmmoLeft / bank.AmmoMax) <= tL_ammoThreshold then
numLowSubsystems = numLowSubsystems+1
break
end
end
end
--Do we have enough low subsystems now that the ship is low on ammo?
if (numLowSubsystems/numSubsystems) >= tL_subsystemThreshold then
return 1
end
end
return 0
end
]
#End
In FRED:
-- when
--- =
---- script-eval-num "turretsLow("GTD Vindicator")"
---- 1
--- ai-warp-out
---- "GTD Vindicator"
EDIT: I don't know how Lua will handle things if AmmoMax is <= 0, so that would probably need to be updated if some of the turrets on a ship don't have ammo-limited guns.
EDIT 2: Forgot to index by 1 rather than 0. Fixed in the example.