Hard Light Productions Forums

Modding, Mission Design, and Coding => The FRED Workshop => Topic started by: killface on August 26, 2008, 05:30:45 pm

Title: Turret ammunition limit?
Post by: killface on August 26, 2008, 05:30:45 pm

Is it possible to use a SEXP or a table edit to limit the ammunition in a cap ship turret?  I would love cap ship turrets that run out of ammo.
Title: Re: Turret ammunition limit?
Post by: karajorma on August 26, 2008, 05:34:13 pm
Lock them after x seconds. :p
Title: Re: Turret ammunition limit?
Post by: killface on August 26, 2008, 05:49:25 pm

Well, there is always that.   :p   Any way to ACTUALLY limit ammunition on turrets?
Title: Re: Turret ammunition limit?
Post by: blowfish on August 26, 2008, 05:54:08 pm
Yeah, there's a way.  You have to give all of the weapons a cargo size in the table, and then give the turret pbank ammo or sbank ammo.  Note that how much ammo a turret has can be modified in any mission.
Title: Re: Turret ammunition limit?
Post by: killface on August 26, 2008, 11:35:45 pm

Hmmm, I knew about the cargo size thing, but the pbank / sbank ammo is new to me.  What about this: is it possible to define a SEXP that can check for a turret's ammunition level?  Basically I want a capital ship to have to warp out once its turrets are depleted or near-depleted.
Title: Re: Turret ammunition limit?
Post by: WMCoolmon on August 27, 2008, 11:06:57 pm
Untested, aside from checking for any real big syntax errors.

turretsLow-sct.tbm
Code: [Select]
#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.