Author Topic: Capship Turret Control  (Read 1791 times)

0 Members and 1 Guest are viewing this topic.

Offline Apollo

  • 28
  • Free Market Fascist
Capship Turret Control
Ok, so after playing "The Blade Itself" from Blueplanet, I was curious about how the mission designer managed to give the player control over what warships the Katana's railguns and torpedos fire at, and how they displayed all that targeting information on screen. I've looked through the mission file, and I can't find the events that do that, so I would appreciate either an explanation of that mission's structure, or some advice on how to replicate that effect.
Current Project - Eos: The Coward's Blade. Coming Soon (hopefully.)

 

Offline Darius

  • 211
The mission uses bp2-csc-sct.tbm, found in the tables folder.

 

Offline Apollo

  • 28
  • Free Market Fascist
Thanks! Now I just need to modify it for other warships.
Current Project - Eos: The Coward's Blade. Coming Soon (hopefully.)

 

Offline Apollo

  • 28
  • Free Market Fascist
Ok, I got it to display on screen, but the targeting doesn't seem to have much of an effect-I can change modes with the arrow keys, but it doesn't seem to change the turret's targets like its supposed to.

EDIT: I fixed it! :)
« Last Edit: September 02, 2012, 11:12:42 pm by Apollo »
Current Project - Eos: The Coward's Blade. Coming Soon (hopefully.)

 
Apollo, can you post what you did (and how you fixed the problem)? I'd love to give something like that a shot myself, and it seems likely that I'll run into the same problem.
Delenda Est delenda est.

(Yay gratuitous Latin.)

 

Offline Apollo

  • 28
  • Free Market Fascist
Sure.

Code: [Select]
;The CSC script. Messy and unreadable, but functional enough to allow effective control over capital ship turrets.

#Conditional Hooks

$Application: FS2_Open

$On Key Pressed: [[csc_KeyPressed.lua]]
$On Key Released: [[csc_KeyReleased.lua]]

$On Game Init:
[
ui_keyToggleFunctions = {}
InputStates = {}

ui_keyToggleFunctions["Up Arrow"]    = function(val) InputStates.up    = val end
ui_keyToggleFunctions["Down Arrow"]  = function(val) InputStates.down  = val end
ui_keyToggleFunctions["Left Arrow"]  = function(val) InputStates.left  = val end
ui_keyToggleFunctions["Right Arrow"] = function(val) InputStates.right = val end
ui_keyToggleFunctions["0"] = function(val) InputStates.zero = val end

function getTargetName(target)
local targetname
if target then
local targetship = mn.getObjectFromSignature(target:getSignature())
if targetship:isValid() then
if targetship:getBreedName() == "Ship" then
targetname = targetship.Name
end
else
targetname = "None"
end
else
targetname = "None"
end
return targetname
end

function getTargetClass(target)
local targetname
if target then
local targetship = mn.getObjectFromSignature(target:getSignature())
if targetship:isValid() then
if targetship:getBreedName() == "Ship" then
targetname = targetship.Class.Type.Name
end
else
targetname = "None"
end
else
targetname = "None"
end
return targetname
end

function drawTurretUIOrff()
--Mauls
if csc.acnum == 1 then
gr.setColor(255, 255, 255)
else
gr.setColor(255, 93, 48)
end
gr.drawString("Maul Turbolasers: " .. csc.maulmode[csc.maulstatus], 5, gr.getScreenHeight() * 0.567)
gr.setColor(255, 93, 48)
for i = 1, #csc.maulTurrets, 1 do
local targetname = getTargetName(csc.maulTurrets[i].Target)
if not targetname then targetname = "None" end
gr.drawString("  " .. csc.maulTurrets[i].Name .. ": " .. targetname)
end

--Flak Cannons
if csc.acnum == 2 then
gr.setColor(255, 255, 255)
else
gr.setColor(255, 93, 48)
end
gr.drawString("Flak Cannons: " .. csc.flakmode[csc.flakstatus], 5, gr.getScreenHeight() * 0.6)
gr.setColor(255, 93, 48)
for i = 1, #csc.flakTurrets, 1 do
local targetname = getTargetName(csc.flakTurrets[i].Target)
if not targetname then targetname = "None" end
gr.drawString("  " .. csc.flakTurrets[i].Name .. ": " .. targetname)
end

--AAA Beams
if csc.acnum == 3 then
gr.setColor(255, 255, 255)
else
gr.setColor(255, 187, 53)
end
gr.drawString("AAA Beams: " .. csc.aaafmode[csc.aaafstatus])
gr.setColor(255, 187, 53)
for i = 1, #csc.aaafs, 1 do
local targetname = getTargetName(csc.aaafs[i].Target)
if not targetname then targetname = "None" end
gr.drawString("  " .. csc.aaafs[i].Name .. ": " .. targetname)
end

--Arctic Typhoon
if csc.acnum == 4 then
gr.setColor(255, 255, 255)
else
gr.setColor(255, 255, 53)
end
gr.drawString("Arctic Typhoon: " .. csc.sgstormmode[csc.sgstormstatus])
gr.setColor(255, 255, 53)
for i = 1, #csc.sgstorms, 1 do
local targetname = getTargetName(csc.sgstorms[i].Target)
if not targetname then targetname = "None" end
gr.drawString("  " .. csc.sgstorms[i].Name .. ": " .. targetname)
end

--Megacyclone Torpedos
if csc.acnum == 5 then
gr.setColor(255, 255, 255)
else
gr.setColor(255, 255, 53)
end
gr.drawString("Megacyclone Torpedos: " .. csc.megacyclonemode[csc.megacyclonestatus])
gr.setColor(255, 255, 53)
for i = 1, #csc.megacyclones, 1 do
local targetname = getTargetName(csc.megacyclones[i].Target)
if not targetname then targetname = "None" end
gr.drawString("  " .. csc.megacyclones[i].Name .. ": " .. targetname)
end
gr.setColor(255, 255, 255)
local targetname = getTargetName(csc.CurrentTarget)
local classname = getTargetClass(csc.CurrentTarget)
if not classname then classname = "None" end
if not targetname then targetname = "Invalid" end
gr.drawString("Current Target: " .. targetname .. " Class: " .. classname)
end

function fillturretlist(ship)
local turretnum = 1
ba.print("CSC: Entered fillturretlist\n")
csc.turrets = {}
for i = 1, 60, 1 do
if i < 10 then
turretname = "turret0" .. i
else
turretname = "turret" .. i
end

ba.print("CSC: Looking for turret: " .. turretname .. "\n")

if ship[turretname]:isValid() then
csc.turrets[turretnum] = ship[turretname]
ba.print("CSC: Found Turret.\n")
turretnum = turretnum + 1
end
end
end

function groupTurrets()

local maulnum = 1
local flaknum = 1
local aaafnum = 1
local sgstormnum = 1
local megacyclonenum = 1

ba.print("CSC: Entered GroupTurrets\n")

for i = 1, #csc.turrets, 1 do
if csc.turrets[i].PrimaryBanks[1].WeaponClass:isValid() or csc.turrets[i].SecondaryBanks[1].WeaponClass:isValid() then
if string.find(csc.turrets[i].PrimaryBanks[1].WeaponClass.Name, "GTVA Turbolaser") then
csc.maulTurrets[maulnum] = csc.turrets[i]
ba.print("CSC: Found Turbolaser. Turret Number: " .. i .. " maulnum: " .. maulnum .. "\n")
maulnum = maulnum + 1
elseif string.find(csc.turrets[i].PrimaryBanks[1].WeaponClass.Name, "Standard Flak") then
csc.flakTurrets[flaknum] = csc.turrets[i]
ba.print("CSC: Found Flak Turret. Turret Number: " .. i .. " flaknum: " .. flaknum .. "\n")
flaknum = flaknum + 1
elseif string.find(csc.turrets[i].PrimaryBanks[1].WeaponClass.Name, "AAAf") then
csc.aaafs[aaafnum] = csc.turrets[i]
ba.print("CSC: Found AAAf. Turret Number: " .. i .. " aaafnum: " .. aaafnum .. "\n")
aaafnum = aaafnum + 1
elseif string.find(csc.turrets[i].PrimaryBanks[1].WeaponClass.Name, "HBlue") then
csc.sgstorms[sgstormnum] = csc.turrets[i]
ba.print("CSC: Found HBlue. Turret Number: " .. i .. " sgstormnum: " .. sgstormnum .. "\n")
sgstormnum = sgstormnum + 1
elseif string.find(csc.turrets[i].SecondaryBanks[1].WeaponClass.Name, "Megacyclone") then
csc.megacyclones[megacyclonenum] = csc.turrets[i]
ba.print("CSC: Found Megacyclone. Turret Number: " .. i .. " megacyclonenum: " .. megacyclonenum .. "\n")
megacyclone = megacyclonenum + 1
end
end
end

csc.initComplete = true
end

function drawTargets(turrets, r, g, b, padding)
gr.setColor(r, g, b)
for i = 1, #turrets, 1 do
target = turrets[i].Target
if target:isValid() then
gr.drawTargetingBrackets(target, true, padding)
end
end
end

function doInput()
MissionTime = mn.getMissionTime()

if OldMissionTime ~= MissionTime and MissionTime ~= 0 then
if InputStates.up and not csc.inputstates.keyup then
csc.acnum = csc.acnum - 1
if csc.ship == "Orff" then
if csc.acnum == 0 then
csc.acnum = 1
end

end
csc.inputstates.keyup = true
elseif not InputStates.up then
csc.inputstates.keyup = false
end

if InputStates.down and not csc.inputstates.keydown then
csc.acnum = csc.acnum + 1
if csc.ship == "Orff" then
if csc.acnum == 6 then
csc.acnum = 1
end

end
csc.inputstates.keydown = true
elseif not InputStates.down then
csc.inputstates.keydown = false
end

if InputStates.left and not csc.inputstates.keyleft then
if csc.ship == "Orff" then
if csc.acnum == 1 then
csc.maulstatus = csc.maulstatus - 1
if csc.maulstatus == 0 then
csc.maulstatus = 4
end
end
if csc.acnum == 2 then
csc.flakstatus = csc.flakstatus - 1
if csc.flakstatus == 0 then
csc.flakstatus = 4
end
end
if csc.acnum == 3 then
csc.aaafstatus = csc.aaafstatus - 1
if csc.aaafstatus == 0 then
csc.aaafstatus = 4
end
end
if csc.acnum == 4 then
csc.sgstormstatus = csc.sgstormstatus - 1
if csc.sgstormstatus == 0 then
csc.sgstormstatus = 4
end
end
if csc.acnum == 5 then
csc.megacyclonestatus = csc.megacyclonestatus - 1
if csc.megacyclonestatus == 0 then
csc.megacyclonestatus = 4
end
end

end
csc.inputstates.keyleft = true
elseif not InputStates.left then
csc.inputstates.keyleft = false
end

if InputStates.right and not csc.inputstates.keyright then
if csc.ship == "Orff" then
if csc.acnum == 1 then
csc.maulstatus = csc.maulstatus + 1
if csc.maulstatus == 5 then
csc.maulstatus = 1
end
end
if csc.acnum == 2 then
csc.flakstatus = csc.flakstatus + 1
if csc.flakstatus == 5 then
csc.flakstatus = 1
end
end
if csc.acnum == 3 then
csc.aaafstatus = csc.aaafstatus + 1
if csc.aaafstatus == 5 then
csc.aaafstatus = 1
end
end
if csc.acnum == 4 then
csc.sgstormstatus = csc.sgstormstatus + 1
if csc.sgstormstatus == 5 then
csc.sgstormstatus = 1
end
end
if csc.acnum == 5 then
csc.megacyclonestatus = csc.megacyclonestatus + 1
if csc.megacyclonestatus == 5 then
csc.megacyclonestatus = 1
end
end

end
csc.inputstates.keyright = true
elseif not InputStates.right then
csc.inputstates.keyright = false
end

if InputStates.zero or io.isMouseButtonDown(MOUSE_LEFT_BUTTON) then
csc.CurrentTarget = csc.plrship.Target
csc.inputstates.zero = true
elseif not InputStates.zero or not io.isMouseButtonDown(MOUSE_LEFT_BUTTON) then
csc.inputstates.zero = false
end
OldMissionTime = MissionTime
end
end

function doTurretModeOrff()
if csc.maulstatus == 1 then
for i = 1, #csc.maulTurrets do
csc.maulTurrets[i]:targetingOverride(false)
end
elseif csc.maulstatus == 2 then
for i = 1, #csc.maulTurrets do
csc.maulTurrets[i].Target = csc.plrship.Target
end
elseif csc.maulstatus == 3 then
if csc.CurrentTarget then
if csc.CurrentTarget:isValid() then
for i = 1, #csc.maulTurrets do
csc.maulTurrets[i].Target = csc.CurrentTarget
end
end
end
elseif csc.maulstatus == 4 then
for i = 1, #csc.maulTurrets do
csc.maulTurrets[i]:targetingOverride(true)
end
end

if csc.flakstatus == 1 then
for i = 1, #csc.flakTurrets do
csc.flakTurrets[i]:targetingOverride(false)
end
elseif csc.flakstatus == 2 then
for i = 1, #csc.flakTurrets do
csc.flakTurrets[i].Target = csc.plrship.Target
end
elseif csc.flakstatus == 3 then
if csc.CurrentTarget then
if csc.CurrentTarget:isValid() then
for i = 1, #csc.flakTurrets do
csc.flakTurrets[i].Target = csc.CurrentTarget
end
end
end
elseif csc.flakstatus == 4 then
for i = 1, #csc.flakTurrets do
csc.flakTurrets[i]:targetingOverride(true)
end
end

if csc.aaafstatus == 1 then
for i = 1, #csc.aaafs do
csc.aaafs[i]:targetingOverride(false)
end
elseif csc.aaafstatus == 2 then
for i = 1, #csc.aaafs do
csc.aaafs[i].Target = csc.plrship.Target
end
elseif csc.aaafstatus == 3 then
if csc.CurrentTarget then
if csc.CurrentTarget:isValid() then
for i = 1, #csc.aaafs do
csc.aaafs[i].Target = csc.CurrentTarget
end
end
end
elseif csc.aaafstatus == 4 then
for i = 1, #csc.aaafs do
csc.aaafs[i]:targetingOverride(true)
end
end

if csc.sgstormstatus == 1 then
for i = 1, #csc.sgstorms do
csc.sgstorms[i]:targetingOverride(false)
end
elseif csc.sgstormstatus == 2 then
for i = 1, #csc.sgstorms do
csc.sgstorms[i].Target = csc.plrship.Target
end
elseif csc.sgstormstatus == 3 then
if csc.CurrentTarget then
if csc.CurrentTarget:isValid() then
for i = 1, #csc.sgstorms do
csc.sgstorms[i].Target = csc.CurrentTarget
end
end
end
elseif csc.sgstormstatus == 4 then
for i = 1, #csc.sgstorms do
csc.sgstorms[i]:targetingOverride(true)
end
end

if csc.megacyclonestatus == 1 then
for i = 1, #csc.megacyclones do
csc.megacyclones[i]:targetingOverride(false)
end
elseif csc.megacyclonestatus == 2 then
for i = 1, #csc.megacyclones do
csc.megacyclones[i].Target = csc.plrship.Target
end
elseif csc.megacyclonestatus == 3 then
if csc.CurrentTarget then
if csc.CurrentTarget:isValid() then
for i = 1, #csc.megacyclones do
csc.megacyclones[i].Target = csc.CurrentTarget
end
end
end
elseif csc.megacyclonestatus == 4 then
for i = 1, #csc.megacyclones do
csc.megacyclones[i]:targetingOverride(true)
end
end
end

function drawRangeDisplayOrff()
csc.range = nil
csc.range = {}
csc.range.Shipname = {}
csc.range.Range = {}
local plrship = hv.Player
if plrship:isValid() then
plrship = mn.getObjectFromSignature(csc.plr:getSignature())
end
local turpos1 = (plrship.Position + plrship["turret01"].Position)
local turpos2 = (plrship.Position + plrship["turret02"].Position)
local turpos3 = (plrship.Position + plrship["turret03"].Position)
local turpos4 = (plrship.Position + plrship["turret04"].Position)
items = 1
for i = 1, #mn.Ships do
local tempship = mn.Ships[i]
local class = getTargetClass(tempship)
if class ~= "Fighter" and class ~= "Bomber" then
if tempship ~= csc.plrship then
local temp = tempship.Position
local temprange1 = math.sqrt( (temp[1] - turpos1[1])^2 + (temp[2] - turpos1[2])^2 + (temp[3] - turpos1[3])^2 )
local temprange2 = math.sqrt( (temp[1] - turpos2[1])^2 + (temp[2] - turpos2[2])^2 + (temp[3] - turpos2[3])^2 )
local temprange3 = math.sqrt( (temp[1] - turpos3[1])^2 + (temp[2] - turpos3[2])^2 + (temp[3] - turpos3[3])^2 )
local temprange4 = math.sqrt( (temp[1] - turpos4[1])^2 + (temp[2] - turpos4[2])^2 + (temp[3] - turpos4[3])^2 )
csc.range.Shipname[items] = tempship.Name
csc.range.Range[items] = math.floor( (temprange1 + temprange2 + temprange3 + temprange4) / 4 )
items = items + 1
end
end
end
gr.setColor(255,255,255)
gr.drawString("Range:", gr.getScreenWidth() * 0.17, gr.getScreenHeight() * 0.78)
for i = 1, #csc.range.Shipname do
if csc.range.Range[i] < 1000 then
local c = math.floor((csc.range.Range[i] / 4)) + 5
gr.setColor(255, c, c)
gr.drawString("  " .. csc.range.Shipname[i] .. ": " .. csc.range.Range[i])
gr.setColor(255, 255, 255)
else
gr.drawString("  " .. csc.range.Shipname[i] .. ": " .. csc.range.Range[i])
end
end
end

function cscInit()
csc = {}
if not csc.initComplete then
if not csc.plrship then
csc.plr = hv.Player
if csc.plr:isValid() then
csc.plrship = mn.getObjectFromSignature(csc.plr:getSignature())
end

if csc.plrship:getBreedName() == "Ship" then
if csc.plrship.Class.Name ~= "GTC Orff" then
csc.disable = true
end
if csc.plrship.Class.Name == "GTC Orff" then
csc.ship = "Orff"
end
else
csc.disable = true
end

if not csc.disable then
if not csc.turrets then
csc.maulTurrets = {}
csc.maulmode = {"Automatic", "Track", "Lock", "Offline"}
csc.maulstatus = 1

csc.flakTurrets = {}
                                                csc.flakmode = {"Automatic", "Track", "Lock", "Offline"}
csc.flakstatus = 1

csc.aaafs = {}
csc.aaafmode = {"Automatic", "Track", "Lock", "Offline"}
csc.aaafstatus = 1

csc.sgstorms = {}
csc.sgstormmode = {"Automatic", "Track", "Lock", "Offline"}
csc.sgstormstatus = 1

csc.megacyclones = {}
csc.megacyclonemode = {"Automatic", "Track", "Lock", "Offline"}
csc.megacyclonestatus = 1

csc.active = {"*", " ", " "}
csc.acnum = 1
csc.inputstates = {}

inputlocked = false

fillturretlist(csc.plrship)
groupTurrets()
ShieldmanOverride = true
BettyOverride = true
OldMissionTime = cscMissionTime
end
end
end
end
end
]

$State: GS_STATE_GAME_PLAY

$On Frame:
[
cscMissionTime = mn.getMissionTime()


if cscOldMissionTime ~= cscMissionTime and cscMissionTime ~= 0 then
if not csc then
cscInit()
end

if csc then
if not csc.disable and csc.initComplete then
doInput()
if csc.ship == "Orff" then
doTurretModeOrff()
drawTargets(csc.maulTurrets,   61, 93, 255,  8)
drawTargets(csc.flakTurrets, 61, 93, 255,  8)
drawTargets(csc.aaafs,     255, 93,  48, 11)
drawTargets(csc.sgstorms,   255, 187, 53, 14)
drawTargets(csc.megacyclones, 255, 255, 53, 17)
end
end
end
cscOldMissionTime = cscMissionTime
end
]

$On HUD Draw:
[
if csc then
if not csc.disable and csc.initComplete then
if csc.ship == "Orff" then
drawRangeDisplayOrff()
drawTurretUIOrff()
end
end
end
]

;;$On State End:
;;[
;; csc = nil
;; ShieldmanOverride = false
;; BettyOverride = false
;;]

#End

If I remember correctly, It had to do with csc.acnum (I believe that controls the weapon selection with the up and down arrows). Although, since I modifed the script without any scripting knowledge, I'm not entirely sure. I'm amazed I even got it work right.

EDIT: If you want to use it with another ship (and you probably will, since the Orff's weapons came from a mod I'm working on), make sure that all of the ship's turrets have generic names (turret01, turret02, etc.). Otherwise, it won't detect it.
« Last Edit: September 27, 2012, 03:43:49 pm by Apollo »
Current Project - Eos: The Coward's Blade. Coming Soon (hopefully.)