Author Topic: Simple script to automatically shut off engine glows  (Read 4672 times)

0 Members and 1 Guest are viewing this topic.

Simple script to automatically shut off engine glows
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
« Last Edit: March 15, 2017, 10:18:00 am by xenocartographer »

 

Offline AdmiralRalwood

  • 211
  • The Cthulhu programmer himself!
    • Skype
    • Steam
    • Twitter
Re: Simple script to automatically shut off engine glows
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.
Ph'nglui mglw'nafh Codethulhu GitHub wgah'nagl fhtagn.

schrödinbug (noun) - a bug that manifests itself in running software after a programmer notices that the code should never have worked in the first place.

When you gaze long into BMPMAN, BMPMAN also gazes into you.

"I am one of the best FREDders on Earth" -General Battuta

<Aesaar> literary criticism is vladimir putin

<MageKing17> "There's probably a reason the code is the way it is" is a very dangerous line of thought. :P
<MageKing17> Because the "reason" often turns out to be "nobody noticed it was wrong".
(the very next day)
<MageKing17> this ****ing code did it to me again
<MageKing17> "That doesn't really make sense to me, but I'll assume it was being done for a reason."
<MageKing17> **** ME
<MageKing17> THE REASON IS PEOPLE ARE STUPID
<MageKing17> ESPECIALLY ME

<MageKing17> God damn, I do not understand how this is breaking.
<MageKing17> Everything points to "this should work fine", and yet it's clearly not working.
<MjnMixael> 2 hours later... "God damn, how did this ever work at all?!"
(...)
<MageKing17> so
<MageKing17> more than two hours
<MageKing17> but once again we have reached the inevitable conclusion
<MageKing17> How did this code ever work in the first place!?

<@The_E> Welcome to OpenGL, where standards compliance is optional, and error reporting inconsistent

<MageKing17> It was all working perfectly until I actually tried it on an actual mission.

<IronWorks> I am useful for FSO stuff again. This is a red-letter day!
* z64555 erases "Thursday" and rewrites it in red ink

<MageKing17> TIL the entire homing code is held up by shoestrings and duct tape, basically.

 
Re: Simple script to automatically shut off engine glows
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 ).
« Last Edit: February 12, 2017, 05:09:00 pm by xenocartographer »

 
Re: Simple script to automatically shut off engine glows
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!

 

Offline m!m

  • 211
Re: Simple script to automatically shut off engine glows
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.

 
Re: Simple script to automatically shut off engine glows
@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.

 
Re: Simple script to automatically shut off engine glows
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!

 
Re: Simple script to automatically shut off engine glows
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

 
Re: Simple script to automatically shut off engine glows
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]
« Last Edit: March 15, 2017, 08:33:45 pm by Nightstorm »

 

Offline coffeesoft

  • 28
  • Bip Bip
Re: Simple script to automatically shut off engine glows
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 !!   :)

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: Simple script to automatically shut off engine glows
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 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.

  

Offline coffeesoft

  • 28
  • Bip Bip
Re: Simple script to automatically shut off engine glows
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