Hard Light Productions Forums

FreeSpace Releases => Scripting Releases => Topic started by: xenocartographer on February 12, 2017, 04:34:14 pm

Title: Simple script to automatically shut off engine glows
Post by: xenocartographer on February 12, 2017, 04:34:14 pm
Have you ever looked at a docked ship and wonder why the crew always forgets to turn off the engine? disabled an Orion or manually SEXP'd off thruster bank 'cause man, that can't be good for the Ganymede it's parked in? marveled at Alpha wing standing still in space even though there's clearly reaction mass pouring out of the back in complete violation of Newtonian mechanics, as if these vessels are flown not by mere mortals but something perhaps divine, perhaps profane, and inhuman thing deserving of awe and trembling?

Well, no more! The days of idling ships rendering completely redundant thruster glows are OVER!



Code: [Select]
#Conditional Hooks

$Application: FS2_Open

$On Game Init: [
   ThrusterCache = {}
   THRUSTER_ACTIVE_THRESHOLD = .01
]

$On Mission Start: [
   ThrusterCache = {}
]

$State: GS_STATE_GAME_PLAY
$On Frame: [
   local numShips = #mn.Ships
   for i=1, numShips do
      local ship = mn.Ships[i]
      if ship:isValid() then
         local signature = ship:getSignature()
         local thrust = ship.Physics.ForwardThrust
         local engines = (thrust > THRUSTER_ACTIVE_THRESHOLD)
         if engines ~= ThrusterCache[signature] then
            mn.runSEXP("(set-thrusters-status !" .. tostring(engines) .. "! !" .. ship.Name .. "!)")
            ThrusterCache[signature] = engines
         end
      end
   end
]

#End
Title: Re: Simple script to automatically shut off engine glows
Post by: AdmiralRalwood on February 12, 2017, 04:42:59 pm
Hmm. This looks like it executes mn.runSEXP() per ship per frame, which strikes me as... sub-optimal. I'm not sure what the performance savings would be, but I'd try implementing an engine status cache and only executing mn.runSEXP() when the engine status changes.
Title: Re: Simple script to automatically shut off engine glows
Post by: xenocartographer on February 12, 2017, 04:51:20 pm
It does and I'd considered that. A'ight, sec.

Aaaaaaand updated. Turns out Physics handles actually have an entry for forward thrust, so that's cool (...but makes the script look a whole lot less clever :P ).
Title: Re: Simple script to automatically shut off engine glows
Post by: Nightstorm on March 14, 2017, 07:18:38 pm
Hi there.  This is pretty cool, I was wondering if you could modify it to also turn off the glow points too.  In Diaspora it kills the engine glow but your engines stay lit up when they really shouldn't be if the throttle is at zero.

Thanks!
Title: Re: Simple script to automatically shut off engine glows
Post by: m!m on March 15, 2017, 05:47:30 am
I noticed a small bug in how you use the cache index. You use the iteration index as an index for the cache but the iteration index is not guaranteed to always refer to the same ship. Instead you should use the signature of the object (it can be retrieved using getSignature) for the cache which will make sure that you always use the correct value for every ship.
Title: Re: Simple script to automatically shut off engine glows
Post by: xenocartographer on March 15, 2017, 10:21:24 am
@m!m: Ah, damn. That does make sense (I guess I'd been under the impression that destroyed or departed ships would become invalid instead of reshuffling the entire list). Amended.

@Nightstorm: Interesting idea, but that'd also shut off navigation lights and so on, which is undesirable. I'll look into it.
Title: Re: Simple script to automatically shut off engine glows
Post by: Nightstorm on March 15, 2017, 11:03:24 am
True enough, in Diaspora ships don't have navigation lights however.  Take for instance the fighters all have a GlowMap for the cockpit lights and such but they only use GlowPoints for the engine glows, that is different than the aurora glow your script already turns off.

I've written it in a SEXP form in my missions to "activate-glow-points" or "deactivate-glow-points" but I have to write that into every mission.  Having it scripted is a way better option :)

Thank you!
Title: Re: Simple script to automatically shut off engine glows
Post by: xenocartographer on March 15, 2017, 05:28:50 pm
Well, if you're sure that's what you want, this should work:

Code: [Select]
#Conditional Hooks

$Application: FS2_Open

$On Game Init: [
   ThrusterCache = {}
   THRUSTER_ACTIVE_THRESHOLD = .01
]

$On Mission Start: [
   ThrusterCache = {}
]

$State: GS_STATE_GAME_PLAY
$On Frame: [
   local numShips = #mn.Ships
   for i=1, numShips do
      local ship = mn.Ships[i]
      if ship:isValid() then
         local signature = ship:getSignature()
         local thrust = ship.Physics.ForwardThrust
         local engines = (thrust > THRUSTER_ACTIVE_THRESHOLD)
         if engines ~= ThrusterCache[signature] then
            local name = ship.Name
            mn.runSEXP("(set-thrusters-status !" .. tostring(engines) .. "! !" .. name .. "!)")
            if engines then
                mn.runSEXP("(activate-glow-points !" .. name .. "!)")
            else
                mn.runSEXP("(deactivate-glow-points !" .. name .. "!)")
           end
            ThrusterCache[signature] = engines
         end
      end
   end
]

#End
Title: Re: Simple script to automatically shut off engine glows
Post by: Nightstorm on March 15, 2017, 07:53:30 pm
Thank you.  I'm getting an error on game Init.  Unexpected symbol near ? and the question mark has a black diamond around it.  I'll attach a screen cap.

The first one runs and shuts off the glow affect that you originally intended but the second one crashes out with this error.

*EDIT* I got it working.  I don't know what was causing the error, I think it was some formatting change or something happening with Noteapad++.  I reconstructed the new version line by line based on a working copy of the old one and it works now.

Thank you very much!

[attachment stolen by Russian hackers]
Title: Re: Simple script to automatically shut off engine glows
Post by: coffeesoft on March 26, 2017, 08:26:50 am
Do you think it would be possible to modify the script , just to rotate the engines ?

The X-Wings of my game can rotate wings  but the engines do not, so they looks out of position, and i don´t know how to fix them.

Thanks a lot !!   :)
Title: Re: Simple script to automatically shut off engine glows
Post by: zookeeper on March 26, 2017, 08:33:38 am
Do you think it would be possible to modify the script , just to rotate the engines ?

The X-Wings of my game can rotate wings  but the engines do not, so they looks out of position, and i don´t know how to fix them.

Thanks a lot !!   :)

This feature (http://www.hard-light.net/wiki/index.php/Subobject_and_Subsystem_Properties#.24attach_thrusters) is what FotG uses to make X-wings' thrusters stick to the engine subsystems even when they rotate. It was added quite specifically for that purpose. Just make sure each engine is its own separate subsystem with its own thruster bank associated with it.
Title: Re: Simple script to automatically shut off engine glows
Post by: coffeesoft on March 26, 2017, 08:38:52 am
Quote
This feature is what FotG uses to make X-wings' thrusters stick to the engine subsystems even when they rotate. It was added quite specifically for that purpose. Just make sure each engine is its own separate subsystem with its own thruster bank associated with it.

Thanks, I will try, i didn´t know about this feature, you are my hero   :p