Jump to content

[1.2.2] Crew Light : An automatic Light manager [1.0] (12 Jan 2017)


Li0n

Recommended Posts

This thread is now closed, the new one is here

Hi fellow kerbonauts

After years of downloading and enjoying mod from the great KSP community it's time I do something myself, so here it is : Crew Light

At the beginning it was a very simple plugin that turn on the light of any part currently occupied by a Kerbal. It works when you jump to an unlighted ship and when you transfer a Kerbal to an unlighted pod / passenger cabin / laboratory.

Now, I like to see it as an automatic light manager, lights are divided in three group with different behavior :

  1. Light in crew part, they are automatically toggled when a Kerbal enter/leave the part
  2. Light in the Light Action Group, they behave as usual, lighted by clicking the tiny green button or hitting the Light shortcut
  3. Others lights*, and lights on kerbal's helmet, will turn on when the sun falls and off when it rises**. And when reaching 20m below the surface**.

 

For a better experience it also remove the crewable part from the Light action group. So you can turn on the headlight of your rover without your crew cabin going dark. **

Last function : when approaching a vessel its lights will blink, sending you a welcoming message after years of space travel in the dark immensity of space. :wink:**

And as a bonus : Kerbals on EVA can toggle light for every part, except crew-able one.

It should work with every light from every mod but if you find one that don't please report it to me and I'll do what I can to make it work.

 

* : By default all light belongs to the Light Action Group, if you want them to respond to the sun rise/fall you need to remove them manually during construction or with Action Group Extended in flight

 ** : Can be disabled/tweaked, more on that below :

 

Configuration file :

Located in "GameData/CrewLight/PluginData/Settings.cfg", the game has to run once for the file to be created.
Contains the following settings :

Spoiler

"use_morse_code" = true

  • enabled/disabled the blinking light on distant vessel functionality

"only_for_controllable_vessel" = false

  • only controllable vessel will send the morse message

"morse_code" = _._|...|.__.

  • the message sent by the distant vessel, a "dit" is a dot ("."), a "dah" is an underscore ("_"). To separate the letters use a ("|"), to separate word use a space (" "). Morse Code's Chart

"distance" = 200

  • The distance between your craft and the distant one at which the message begin

"dit" = 0.9

  • duration (in seconds) of the "dit".

"dah" = 2

  • duration (in seconds) of the "dah".

"symbol_space" = 1

  • duration (in seconds) of the darkness between two symbols

"letter_space" = 1.3

  • duration (in seconds) of the darkness between two letters

"word_space" = 1.7

  • duration (in seconds) of the darkness between two words

"use_sun_light" = true

  • lights will go on/off as the sun rise/fall

"use_depth_light" = true

  • lights will go on/off when the craft reach a certain depth

"depth_threshold" = 20

  • depth at witch lights get toggled

"only_light_not_in_AG" = true

  • only lights not assigned to an Action Group will be lighted when the sun fall

"use_sunlight_for_EVA" = True

  • kerbal's headlights will go on/off as the sun rise/fall

"always_on_in_space" = True

  • always turn on the headlights when EVA in space

"always_on_landed" = True

  • always turn on the headlights when EVA landed

"disable_light_action_group_for_light_part" = true

  • remove crewable part from the Light Action Group

"disable_action_group_for_light_part" = false

  • remove all the light part from the Light Action Group

 

A great thanks to @Malah for his "Quick Mods" (good code to look at for getting started), all the maintainer of the KSP API and all the modders around here I follow for some time now. And to @Nereid and @Crzyrndm for answering my noob's questions :wink:

 

Download from SpaceDock or GitHub

 

This mod bundle a .version file which can be use to check if a new version is available if the mod KSP-AVC is installed.

Change Log :

Spoiler
    • 0.10 :
    • New Function : Kerbal on EVA can toggle light (only for non-crewable part)
    • Now support SurfaceLight by @Why485, maintained by @IgorZ
    • Landing gear/wheel 's light no longer respond to the sun rise / fall
    • ModuleManager by @sarbian is now required for toggling light on EVA
    • ModuleManager is bundled

    0.9 : Lights on Kerbal's helmet are now toggled as the sun rise/fall, can be tweaked in settings.cfg so lights are always on when in space or landed

    • Better handling of the sunlight feature, mostly for building / modifying vessel with KIS
    • Correct a typo in the .version file, KSP-AVC should now give the right link when a new version comes up

     

  • 0.8 : * Lights who respond to the sun rise/fall will now be toggled when reaching a certain depth
    * Detection of terrain and KSC building shadow when checking if the sun shines on the craft
    * Nearby vessels get theirs lights toggled as the sun rise/fall
    * Added a .version file for KSP-AVC
    * Remove source from the download, only on GitHub now

 

  • 0.7 : * New function : Lights are toggled depending if the sun shine on the vessel or not, only for part without crew capacity and not in the Light action group (tweakable in settings.cfg), thanks @RealGecko for the idea and advice
    * New function : All lights can be removed from the light action group when building a vessel (so they all get toggled by the sun light), off by default
    * Rebuild for KSP 1.2.2
    * Part with multiple light are now correctly lighted, support for MK1 Cabin Hatch by @Skalou
    * Use REGEX to determine if an animation is light related, support for MK1 Cabin Hatch by @Skalou
    * Cleaned up and readability enhancement

 

  • 0.6 : Morse Code : nearby vessel will send you a morse message by toggling theirs lights as you approach
            Settings File : GameData/CrewLight/PluginData/Settings.cfg option to disabled the morse   message and customize it and disabling the auto removal of crewable part from the Light action group
            Remove ModuleManager and all the patch
     
  • 0.5 : KSP 1.2.1 recompile
            Add support for all the Wild Blue Industries mods (M.O.L.E. Buffalo, etc...)
            Enhanced support of the passengers cab from USI Karibou
            Enhanced support of the Orca command module from USI Freight Transport Technologies
            New functionality : Toggle Lights action from all crewable parts are no longer automatically
                                             put in the "Light" action group. Only applied to newly assembled ship.
            From now on ModuleManager (by @sarbian) is a requirement and is bundled
     
  • 0.4 : Add support for Near Future Spacecraft, thanks to @Boomerang
     
  • 0.3 : Code cleanup, thanks to @Nereid
            Kerbals now turn lights off when they leave theirs pod
     
  • 0.2 : code cleanup, thanks to @Diazo
     
  • 0.1 : Initial release

Source code (also include in the archive at SpaceDock)

License (MIT) :

Spoiler

The MIT License (MIT)

Copyright (c) 2016 Li0n

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 

Edited by Li0n
Link to comment
Share on other sites

For your second question, you can use a Unity feature called a Coroutine to delay running a piece of code. Google has tons of examples and I use it in my code here to do exactly that.

For your first question, I'm not sure there is a good answer due to how KSP packs/unpacks vessels and the physic range limitation. I'm pretty sure that the Part class does has a "number of Kerbals in this part" value to go with the "maximum Kerbals that fit in this part" that I know is present. Maybe looking at that could help?

D.

Link to comment
Share on other sites

@Diazo thanks, I deleted the Update() method and add a StartCoroutine(), at the end of Start(), which wait 0.1s before toggling lights. Looks cleaner :wink:

For the Part class, I found a Part.CrewCapacity which return an int (number of seats in this part) but nothing to check if the part is occupied in this class. (well I find Part.RegisterCrew() and Part.UnRegisterCrew() but no comment on those and monodevelop won't let me use them without the right reference or "using", no idea which one I need...)
Imagining this will work, I'll have to check every part of the vessel to find the one occupied, not sure if it is a good idea with a big ship, performance wise.

Link to comment
Share on other sites

Hmm, Part.CrewCapacity is what I knew was present to give the maximum kerbal capacity, I guess there isn't a Part.CurrentCrewCapacity value that I was hoping for.

If you subscribe to the Part.RegisterCrew() event, does it fire when a vessel unpacks and the kerbal is placed in the part? Or only when the Kerbal actually moves in to the part at lauch/when player commands it?

D.

Link to comment
Share on other sites

I'm not sure Part.RegisterCrew() is an event, it doesn't return anything (again I'm new to KSP modding...)

I tried this :

void OtherVesselLight (Vessel otherVessel) {
  for (int i = 0 ; i < otherVessel.parts.Count ; i++) {
    if (otherVessel.parts[i].CrewCapacity != 0) {
      otherVessel.parts [i].RegisterCrew ();
    }
  }
  StartLight (otherVessel);
}

Triggered by GameEvents.OnVesselGoOffRails()
But nothing appends.

Link to comment
Share on other sites

Bah, that's what I get for trying to do this on my mobile from memory. Do agree it probably isn't an event.

I'm out of ideas, somewhere in the ProtoKerbal class it does need to save what part the kerbal is in, just a matter of finding it but it seems to be well hidden.

D.

Edited by Diazo
Link to comment
Share on other sites

There is a ProtoCrewMember.KerbalRef.InPart , this is what I used for the active vessel. But for the distant one KerbalRef == null.
And I haven't found a way to get the part directly from ProtoCrewMember

I'm done for tonight, thanks for the help, I'll continue the investigation later

 

Link to comment
Share on other sites

I have a quick question. I downloaded this mod from SpaceDock. I transferred the file from the GameData folder but there is also a CrewLight.cs file that is outside the GameData folder. What do we do with this file? Does it transfer over into the games GameData folder or does it just transfer over to the base KSP folder? Or do we not do anything with it at all? Other than that I am very excited to use this mod! Thank you for your work! :)

Link to comment
Share on other sites

On 21.10.2016 at 6:39 PM, Li0n said:

Imagining this will work, I'll have to check every part of the vessel to find the one occupied, not sure if it is a good idea with a big ship, performance wise.

It's not. I have made the same mistake in NanoGauges in my first implementations. If you do this, the physics will do strange things with bigger ships. Solution: Scan the vessel ONCE to find all parts that may contain crew. Register a callback for the events that informs you if the vessel was modified and scan again then (or listen just for new/removed parts, if this is possible in KSP 1.2). In the scan a part may still be packed. DO NOT unpack them in the scanning (it will cause other issues). Just register a callback that informs you when its unpacked.

Edited by Nereid
Link to comment
Share on other sites

Well I find a way to illuminate a distant ship :

void DistantVesselLight(Vessel distantVessel) {
	for (int i = 0 ; i < distantVessel.parts.Count ; i++) {
		if (distantVessel.parts[i].CrewCapacity > 0) {
			if (distantVessel.parts[i].protoPartSnapshot.protoCrewNames.Count > 0) {
				Light (distantVessel.parts [i]);
			}
		}
	}
}

Triggered with OnVesselGoOffRails()

As @Nereid says, not a good idea to do this when approaching a huge base/station so I now need a way to store those info. Not sure how to do this, do a check on all vessel when the game starts then create a list of crewed part by ship and update this list when a crew change / a new vessel is lauched etc ?
Should be quick when a ship comes in range but it seems a heavy load at startup for a save with a lot of ship / colony...
I will try this... ...tomorrow...

Link to comment
Share on other sites

4 hours ago, Li0n said:

As @Nereid says, not a good idea to do this when approaching a huge base/station so I now need a way to store those info. Not sure how to do this, do a check on all vessel when the game starts then create a list of crewed part by ship and update this list when a crew change / a new vessel is lauched etc ?
Should be quick when a ship comes in range but it seems a heavy load at startup for a save with a lot of ship / colony...
I will try this... ...tomorrow...

Or you can add this info to the save with a ScenarioModule that keeps track of all parts with crew capabilities to avoid the scanning at each GameStateCreated event. But this is a bit over the top for something like this...

 

And a minor improvement to readability:

         void DistantVesselLight(Vessel distantVessel)
         {
            foreach (Part part in distantVessel.parts) {
               if (part.CrewCapacity > 0) {
                  if (part.protoPartSnapshot.protoCrewNames.Count > 0) {
                     Light(part);
                  }
               }
            }
         }

 

Edited by Nereid
Link to comment
Share on other sites

20 hours ago, Gryffyn said:

I have a quick question. I downloaded this mod from SpaceDock. I transferred the file from the GameData folder but there is also a CrewLight.cs file that is outside the GameData folder. What do we do with this file? Does it transfer over into the games GameData folder or does it just transfer over to the base KSP folder? Or do we not do anything with it at all? Other than that I am very excited to use this mod! Thank you for your work! :)

You do not need this. But all modders must make must have their source code be made publicly available. They can do it by linking to github or somewhere else, or they can include the source in the zip archive of the mod.

Because of this I prefer to include the source in the GameData folder itself and hope this causes no issues with KSP  - and that's not the case as far as I'm aware of it. Inexperienced users that do not know what to do, just unpack all files into the GameData folder and get not confused about other files this way. Experienced users may still delete all cs-files by themself...

Link to comment
Share on other sites

2 hours ago, Nereid said:

You do not need this. But all modders must make must have their source code be made publicly available. They can do it by linking to github or somewhere else, or they can include the source in the zip archive of the mod.

Because of this I prefer to include the source in the GameData folder itself and hope this causes no issues with KSP  - and that's not the case as far as I'm aware of it. Inexperienced users that do not know what to do, just unpack all files into the GameData folder and get not confused about other files this way. Experienced users may still delete all cs-files by themself...

Thank you very much Nereid! I didn't know cs-file was source code. I was already aware that when installing a mod source code was not needed but I have always seen a source folder, as you described, with other mods I use. Again thank you for educating me on this subject. Just wondering though does cs stand for code-source then? No matter. lol!

Link to comment
Share on other sites

This is SUCH a fun little mod! I get an unreasonable amount of glee out of parts that have lights that turn on and off, and it's very fun to connect it to whether or not anyone's on board.

Thanks!

Edit: One thing though... I tried it and it doesn't seem like the lights go off again when the part is empty. Should they? If not, I hope this is planned.

Edited by Rhedd
Link to comment
Share on other sites

@Rhedd Very glad you like my little plugin, wait to see the new feature : distant vessel will light as you approach them, looks very cool. 

For you request about switching lights off when a crew leave a part, I wasn ´t go for it initially but after more testing I agree it will looks cooler :wink: wait and see...

@Nereid About storing the list of crewed part in the persistent file, I need to test the time it takes to check all vessel at startup before going the way of writing it in persistent.sfs

For your second point about the clearity of the code, you change the for loop to a foreach, i readed somewhere in the forum that foreach loop shouldn ´t be used anymore (since 1.1 ?) not sure it is relevant here

Link to comment
Share on other sites

4 minutes ago, Li0n said:

 About storing the list of crewed part in the persistent file, I need to test the time it takes to check all vessel at startup before going the way of writing it in persistent.sfs

For your second point about the clearity of the code, you change the for loop to a foreach, i readed somewhere in the forum that foreach loop shouldn ´t be used anymore (since 1.1 ?) not sure it is relevant here

Any source of this? Because using a regular for on a list is ugly as hell.

Link to comment
Share on other sites

I'm really loving the immersion-factor of this!

I've noticed briefly though that at least the Inline Command Pod from Near Future Spacecraft doesn't function. And the config does contain:

 

Quote

MODULE
    {
        name = ModuleAnimateGeneric
        animationName = InlineCommandPod-Windows
        startEventGUIName = Lights On
        endEventGUIName = Lights Off
    }

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...