Author Topic: Highlight or hotkey individual turret  (Read 6132 times)

0 Members and 1 Guest are viewing this topic.

Offline General Battuta

  • Poe's Law In Action
  • 214
  • i wonder when my postcount will exceed my iq
Highlight or hotkey individual turret
Can we do this? I feel like someone might've written a script and I missed it.

I want players to be able to press F9 and have a particular turret on a Deimos targeted. Ideally I'd like them to be able to press F9 and cycle through four turrets on this Deimos, but all of them would be highlighted with gray brackets.

 

Offline MatthTheGeek

  • Captain Obvious
  • 212
  • Frenchie McFrenchface
Re: Highlight or hotkey individual turret
This would give a huuuuuuugely useful tool for FREDers. Instead of actually requiring people to go open the pof and look what turret carries what.
People are stupid, therefore anything popular is at best suspicious.

Homeworld: Blue Planet     -     Help us help you     -     FSO Installer     -     BP extras portal     -     My legacy

666maslo666: Releasing a finished product is not a good thing! It is a modern fad.

SpardaSon21: it seems like you exist in a permanent state of half-joking misanthropy

Axem: when you put it like that, i sound like an insane person

bigchunk1: it's not retarded it's american!
bigchunk1: ...

batwota: steele's maneuvering for the coup de gras
MatthTheGeek: you mispelled grĂ¢ce
Awaesaar: grace
batwota: oh right :P
Darius: ah!
Darius: yes, i like that
MatthTheGeek: the way you just spelled it it means fat
Awaesaar: +accent I forgot how to keyboard
MatthTheGeek: or grease
Darius: the killing fat!
Axem: jabba does the coup de gras
MatthTheGeek: XD
Axem: bring me solo and a cookie

 

Offline Nighteyes

  • 211
Re: Highlight or hotkey individual turret
Deimos Basestar
needless to say, something like this would be awesome

 

Offline mjn.mixael

  • Cutscene Master
  • 212
  • Anims: 418, Cutscenes: 8, Mainhalls: 7, Logos: 52
    • Steam
    • Twitter
    • Mix-Hai Productions
Re: Highlight or hotkey individual turret
+1
Cutscene Upgrade Project - Mainhall Remakes - MixaelANITools - Between the Ashes - MjnMixael's Render Boutique - Mix-Hai Productions
Youtube Channel - P3D Model Box - Photobucket Albums - Model Releases - Downloads
Between the Ashes is looking for committed testers, PM me for details.
Report MediaVP issues, now on the MediaVP Mantis! Read all about it Here!

 

Offline m!m

  • 210
Re: Highlight or hotkey individual turret
Here's a script for this:
Code: (turretHotkey-sct.tbm) [Select]
#Conditional Hooks

$Application: FS2_Open
$On Gameplay Start:
[
turretHotkeys = {}
turretHotkeys.key = "F9"

turretHotkeys.ship = nil
turretHotkeys.subsystemNames = {}
turretHotkeys.lastIndex = 0

function turretHotkeys:keyPressed(key)
    if (key == self.key) then
        if (self.ship ~= nil and self.ship:isValid()) then
            local nextIndex = self.lastIndex + 1

            if (nextIndex > #self.subsystemNames) then
                nextIndex = 1
            end

            local subsys = self.ship[self.subsystemNames[nextIndex]]

            if (subsys:isValid()) then
                hv.Player.Target = self.ship
                hv.Player.TargetSubsystem = subsys
            end

            self.lastIndex = nextIndex
        end
    end
end

function thkKey(key)
    if (type(key) ~= "string") then
        ba.warning("Invalid argument type for thkKey. Need a string, got " .. type(key) .. ".")
        return
    end

    turretHotkeys.key = key
end

function thkShip(name)
    if (name == nil) then
        turretHotkeys.ship = nil
    else
        if (type(name) ~= "string") then
            ba.warning("Invalid argument type for thkShip. Need a string, got " .. type(name) .. ".")
            return
        end

        local ship = mn.Ships[name]

        if (not ship:isValid()) then
            ba.warning("No ship with name \"" .. name .. "\" could be found in the current mission.")
            return
        end

        turretHotkeys.ship = ship
    end
end

function thkAdd(name)
    if (type(name) ~= "string") then
        ba.warning("Invalid argument type for thkAdd. Need a string, got " .. type(name) .. ".")
        return
    end

    if (turretHotkeys.ship == nil or not turretHotkeys.ship:isValid()) then
        ba.warning("Invalid ship in turret hotkey state. You need to specify a ship before adding subsystems")
        return
    end

    if (turretHotkeys.ship[name]:isValid()) then
        table.insert(turretHotkeys.subsystemNames, name)
    end
end

function thkRemove(name)
    if (type(name) ~= "string") then
        ba.warning("Invalid argument type for thkRemove. Need a string, got " .. type(name) .. ".")
        return
    end

    local index = 1
    local done = false

    while (not done) do
        for i = index, #turretHotkeys.subsystemNames do
            if (turretHotkeys.subsystemNames[i] == name) then
                table.remove(turretHotkeys.subsystemNames, i)
                index = i
                break
            end
        end

        done = true
    end
end

function thkClear()
    turretHotkeys.subsystemNames = {}
end
]

$State: GS_STATE_GAME_PLAY
$On Key Pressed:
[
turretHotkeys:keyPressed(hv.Key)
]

#End

To enable it in a mission execute the script-eval SEXP with the argument thkShip('<name>') and add the desired subsystems with thkAdd('<subsys name>').
You can also remove a subsystem with thkRemove('<name>') when it has been added before. If you want to clear this list then call thkClear() and to completely disable it at some point call thkShip() without any name.
You can also change the key that has to be pressed with thkKey('<key>'), the default key if F9.

 

Offline General Battuta

  • Poe's Law In Action
  • 214
  • i wonder when my postcount will exceed my iq
Re: Highlight or hotkey individual turret
Will you marry me?

 

Offline General Battuta

  • Poe's Law In Action
  • 214
  • i wonder when my postcount will exceed my iq
Re: Highlight or hotkey individual turret
m!m this is awesome (once I realized you couldn't add data new arguments to script eval, you had to add new script-evals.) Could I ask for two expanded features, if they're not ridiculously hard:

Allow this to work for more than one ship at a time (I've got two Deimos, and I'd like to cycle between eight turrets, four on each of them)

Draw little escort brackets around every turret on the list, even when it's not actually targeted?

You're the best

 

Offline m!m

  • 210
Re: Highlight or hotkey individual turret
(once I realized you couldn't add data new arguments to script eval, you had to add new script-evals.)
I know that that bug exists since some time although I'm not sure why I haven't fixed it :doubt:

Allow this to work for more than one ship at a time (I've got two Deimos, and I'd like to cycle between eight turrets, four on each of them)
I thought about that when I wrote the script but discarded the feature as a proper FRED interface would have been quite work intensive because of the limited space in the SEXP argument but I'll see if I can come up with something...

Draw little escort brackets around every turret on the list, even when it's not actually targeted?
That existed somewhere before this, I'll see if I can still find it...

 

Offline General Battuta

  • Poe's Law In Action
  • 214
  • i wonder when my postcount will exceed my iq
Re: Highlight or hotkey individual turret
Here's a possibly dumb workaround: could I just copy the script to a second tbm, rename that one 'turretscript2.tbm', and run that in a separate event for the second Deimos?

 

Offline m!m

  • 210
Re: Highlight or hotkey individual turret
No, that would not work because it would use the same function names in the lua system and would generally do lots of weird things.

 

Offline General Battuta

  • Poe's Law In Action
  • 214
  • i wonder when my postcount will exceed my iq
Re: Highlight or hotkey individual turret
yeah I just realized that  :nervous:

(ps i am posting from stats class lab)

 

Offline Aardwolf

  • 211
  • Posts: 16,384
    • Minecraft
Re: Highlight or hotkey individual turret
This sounds like a job for gr.drawTargetingBrackets!

 
Re: Highlight or hotkey individual turret
 gr.drawTargetingBrackets needs a target that is an object. I think subsystems and turrets in scripting aren't objects and it shouldn't be possible to use that function on them.
Instead it's always possible to draw a bunch of lines around the turrets position.
Here goes scripting and copy paste coding
Freespace RTS Mod
Checkpoint/Shipsaveload script

 

Offline m!m

  • 210
Re: Highlight or hotkey individual turret
Exactly, I guess I'll just use the radius of the subsystem and try to draw the targeting brackets that way.

 

Offline Aardwolf

  • 211
  • Posts: 16,384
    • Minecraft
Re: Highlight or hotkey individual turret
Oh. Right then.

Well, the whole reason gr.drawTargetingBrackets exists is because drawing lines manually from lua was too slow (or seemed to be, anyway)... might be worthwhile to modify it or create a new function that adds support for a target subsystem.

 

Offline General Battuta

  • Poe's Law In Action
  • 214
  • i wonder when my postcount will exceed my iq
Re: Highlight or hotkey individual turret
Hey guys, we're holding up a patch for possible developments here. Should I keep waiting or ditch the script for now?

 

Offline m!m

  • 210
Re: Highlight or hotkey individual turret
I think it's working correctly now but the drawing of the subsystem brackets need a new scripting function so you will have to wait until that patch is in trunk...

 

Offline General Battuta

  • Poe's Law In Action
  • 214
  • i wonder when my postcount will exceed my iq
Re: Highlight or hotkey individual turret
I'm okay if it's just hotkeys - all we need is the ability to have the F9 key cycle through turret1 and turret2 on Ship A, then turret1 and turret2 on ship B, then back around again.

 

Offline m!m

  • 210
Re: Highlight or hotkey individual turret
Alright then, here you go:
Code: [Select]
#Conditional Hooks

$Application: FS2_Open
$On Gameplay Start:
[
turretHotkeys = {}
turretHotkeys.key = "F9"
turretHotkeys.enabled = false

-- Subsystem indicator settings
turretHotkeys.indicator = {}
turretHotkeys.indicator.width = 20
turretHotkeys.indicator.height = 20
turretHotkeys.indicator.color = {
    r = 255,
    g = 255,
    b = 255,
    a = 200
}

turretHotkeys.subsystems = {}
turretHotkeys.lastIndex = {1, 0}

function turretHotkeys:keyPressed(key)
    if (self:isEnabled() and key == self.key) then
        local tblIndex, subsysIndex = self:getNextIndex()

        if (tblIndex ~= false) then
            -- This is ugly code, don't look at it or it won't work
            local subsys = self.subsystems[tblIndex].ship[self.subsystems[tblIndex][subsysIndex]]

            if (subsys:isValid()) then
                hv.Player.Target = self.subsystems[tblIndex].ship
                hv.Player.TargetSubsystem = subsys
            end

            self.lastIndex = { tblIndex, subsysIndex }
        end
    end
end

function turretHotkeys:checkIndexes(tblIndex, subsysIndex)
    if (tblIndex > 0 and tblIndex <= #self.subsystems) then
        if (subsysIndex > 0 and subsysIndex <= #self.subsystems[tblIndex]) then
            return tblIndex, subsysIndex
        else
            return tblIndex, math.max(1, math.min(subsysIndex, #self.subsystems[tblIndex]))
        end
    else
        -- If we're here it means that we've got an invalid index...
        return math.max(1, math.min(tblIndex, #self.subsystems)), math.max(1, math.min(1, #self.subsystems[tblIndex]))
    end
end

function turretHotkeys:getNextIndex()
    local tblIndex = self.lastIndex[1]

    local tbl = self.subsystems[tblIndex]

    if (tbl == nil) then
        if (tblIndex == 1) then
            return false
        else
            return self:checkIndexes(1, 1)
        end
    end

    local subsystemIndex = self.lastIndex[2] + 1

    if (subsystemIndex > #tbl) then
        subsystemIndex = 1
        tblIndex = tblIndex + 1

        tbl = self.subsystems[tblIndex]
    end

    if (tblIndex > #self.subsystems) then
        tblIndex = 1

        if (tblIndex > #self.subsystems) then
            return self:checkIndexes(tblIndex, 1)
        end
    end

    return self:checkIndexes(tblIndex, subsystemIndex)
end

function turretHotkeys:onFrame()
    if (self:isEnabled()) then
        self:checkShips()

        -- self:drawSubsystems()
    end
end

function turretHotkeys:checkShips()
    local index = 1
    local done = fale

    while (not done) do
        for i = index, #self.subsystems do
            if (not self.subsystems[index].ship:isValid()) then
                table.remove(self.subsystems, index)
                index = i
            end
        end

        done = true
    end
end

function turretHotkeys:drawSubsystems()
    local target = hv.Player.Target
    local subsysName = hv.Player.TargetSubsystem:getModelName()

    for i = 1, #self.subsystems do
        local tbl = self.subsystems[i]
        for j = 1, #tbl do
            local current = tbl.ship[tbl[j]]
            if (tbl.ship ~= target or current:getModelName() ~= subsysName) then
                self:drawSubsystem(current)
            end
        end
    end
end

function turretHotkeys:drawSubsystem(subsys)
    local indicator = self.indicator

    gr.setColor(indicator.color.r, indicator.color.g, indicator.color.b, indicator.color.a)

    gr.drawSubsystemTargetingBrackets(subsys)
end

function turretHotkeys:addShip(ship)
    table.insert(self.subsystems, {
        name = ship.Name,
        ship = ship
    })
end

function turretHotkeys:isValidIndex(index)
    return self.subsystems[index] ~= nil and self.subsystems[index].ship:isValid()
end

function turretHotkeys:addSubsystem(index, name)
    local tbl = self.subsystems[index]

    if (not tbl.ship[name]:isValid()) then
        ba.warning(string.format("Unknown subsystem %q on ship %q of class %q.", name, tbl.ship.name, tbl.ship.class))
        return false
    end

    table.insert(tbl, name)

    return true
end

function turretHotkeys:removeSubsystem(index, name)
    local tbl = self.subsystems[index]

    local newIndex = 1
    local done = false

    while (not done) do
        for i = newIndex, #turretHotkeys.subsystemNames do
            if (turretHotkeys.subsystemNames[i] == name) then
                table.remove(turretHotkeys.subsystemNames, i)
                newIndex = i
                break
            end
        end

        done = true
    end
end

function turretHotkeys:isEnabled()
    return self.enabled and hv.Player:getBreedName() == "Ship"
end

function thkEnable(bool)
    if (bool == nil) then
        bool = true
    end

    if (type(bool) ~= "boolean") then
        ba.warning("Invalid argument type for thkKey. Need a boolean or no argument, got " .. type(bool) .. ".")
        return
    end

    turretHotkeys.enabled = bool
end

function thkKey(key)
    if (type(key) ~= "string") then
        ba.warning("Invalid argument type for thkKey. Need a string, got " .. type(key) .. ".")
        return
    end

    turretHotkeys.key = key
end

function thkShip(...)
    local names = { ... }

    for i = 1, #names do
        if (type(names[i]) ~= "string") then
            ba.warning("Invalid argument type for thkShip. Need a string, got " .. type(names[i]) .. ".")
            return
        end

        local ship = mn.Ships[names[i]]

        if (not ship:isValid()) then
            ba.warning("No ship with name \"" .. names[i] .. "\" could be found in the current mission.")
            return
        end

        turretHotkeys:addShip(ship)
    end
end

function thkAdd(index, name)
    if (type(index) ~= "number") then
        ba.warning("Invalid first argument type for thkAdd. Need a string, got " .. type(index) .. ".")
        return
    end

    if (type(name) ~= "string") then
        ba.warning("Invalid second argument type for thkAdd. Need a string, got " .. type(name) .. ".")
        return
    end

    if (not turretHotkeys:isValidIndex(index)) then
        ba.warning("Invalid ship index \"" .. tostring(index) .. "\"!")
        return
    end

    turretHotkeys:addSubsystem(index, name)
end

function thkRemove(index, name)
    if (type(index) ~= "number") then
        ba.warning("Invalid first argument type for thkRemove. Need a string, got " .. type(index) .. ".")
        return
    end

    if (type(name) ~= "string") then
        ba.warning("Invalid second argument type for thkRemove. Need a string, got " .. type(name) .. ".")
        return
    end

    if (not turretHotkeys:isValidIndex(index)) then
        ba.warning("Invalid ship index \"" .. tostring(index) .. "\"!")
        return
    end

    turretHotkeys:removeSubsystem(index, name)
end

function thkClear()
    turretHotkeys.subsystems = {}
end
]

$State: GS_STATE_GAME_PLAY
$On Key Pressed:
[
turretHotkeys:keyPressed(hv.Key)
]

$State: GS_STATE_GAME_PLAY
$On Frame:
[
turretHotkeys:onFrame()
]

#End

There are a few modifications to the way the script works now.
First you have to call thkEnable() when you want to enable the script. You can disable it again with thkEnable(false).
To specify the ships you can call thkShip(<...>) with the ship names you want to have subsystems on. Every function call adds the specified ships to the list of ships.
To specify a subsystem you still use thkAdd but you will have to specify which ship to use as the fist argument which should be the number of the ship. That means if you cann thkShip('Ship A', 'Ship B', 'Ship C'), thkAdd(2, 'turret-42') will mark that subsystem on Ship B as being able to be selected using the hotkey.

 

Offline General Battuta

  • Poe's Law In Action
  • 214
  • i wonder when my postcount will exceed my iq
Re: Highlight or hotkey individual turret
Oh my god you are so cool