Jump to content

[WIP] Selene - Lua based autopilot & general purpose scripting layer for KSP


BlackNecro

Recommended Posts

While working on the latest version of my PartCatalog (big update inbound soonâ„¢), I ended up having a perfectly fine Lua scripting engine in KSP.

Before letting all of that go to waste I decided to actually write a nice usable wrapper around and ended up creating

Selene

Lua Scripting Layer

The Concept

Selene allows you to use the scripting language Lua for both completely controlling your flight as well as a general purpose scripting engine for writing small plugins.

You can either use the Selene specific API which is optimized for autopiloting related tasks, or utilize the full power of NLua (the C# / lua interface library I used) and use any of KSPs C# classes from lua.

Scripts are organized in processes in order to encapsulate certain functionality and thus ensure reusability.

Each process has it's own variable environment and can register functions to certain hooks to be notified of events in the KSP world.

Implemented Features

  • Non optional toolbar integration
  • Lua backend up and running.
  • Management GUI up and running.
  • The Selene API core is implemented.
  • Hooks:
    • Tick & PhysicsUpdate - Called every frame / every physics iteration
    • Control - Called when updating the control state of the vessel
    • Load & Save - Called when loading / saving the state of the process into persistence

Currently Missing Features

  • Last touchups on the GUI.
  • CelestialBody support
  • Maneuver node support
  • Vessel Part listing and Action Group interfacing
  • Managed GUI library to be used from Lua

Outstanding Questions

  • Resource usage to balance everything?
  • What did I miss yet?

The project can be found at github.

To use it add a Selene Interpreter part to your ship, launch it and click on the Selene button in the toolbar and creating a new process by hitting the "+" on the root process.

For now please refer to the API Reference as well as the rudimentary example scripts for some ideas on how to work with the API.

I'll post some more in detail information about everything after the next weekend, need some holidays first.

If this sparked your interest please leave a comment.

Edited by BlackNecro
Link to comment
Share on other sites

Just by looking at the title, it makes me feel wanting to use and add what I want into this plugin. Is forking and giving pull requests welcomed? Oh, it's MIT license... so yes?

EDIT: Alright, it seems like Selene is also using LuaNET to automatically load the KSP's assembly so these classes (& their member variables/methods) will be accessible in Lua, is that correct?

Edited by HoneyFox
Link to comment
Share on other sites

Just by looking at the title, it makes me feel wanting to use and add what I want into this plugin. Is forking and giving pull requests welcomed? Oh, it's MIT license... so yes?

EDIT: Alright, it seems like Selene is also using LuaNET to automatically load the KSP's assembly so these classes (& their member variables/methods) will be accessible in Lua, is that correct?

Actually that is very welcome yes.

Yes I'm using luanet through NLua, but the main API uses a set of proxy classes. If you don't want to use those you could for example do something like this

FlightGlobals = luanet.import_type('FlightGlobals') 
CurrentVessel = FlightGlobals.ActiveVessel

Link to comment
Share on other sites

Actually that is very welcome yes.

Yes I'm using luanet through NLua, but the main API uses a set of proxy classes. If you don't want to use those you could for example do something like this

FlightGlobals = luanet.import_type('FlightGlobals') 
CurrentVessel = FlightGlobals.ActiveVessel

That looks good. some helper lua scripts that provide different kinds of data, functions should be very useful when later users start to write actual scripts.

Link to comment
Share on other sites

Oh boy I've been wanting this for such a long time. Lua is just the language for me. I'm trying this at the very moment. Expect feedback from me.

Again, thanks :D

Edit: Can't seem to find the download. Probably it's too obvious and therefore I'm missing it.

Edited by aeTIos
Link to comment
Share on other sites

Oh boy I've been wanting this for such a long time. Lua is just the language for me. I'm trying this at the very moment. Expect feedback from me.

Again, thanks :D

Edit: Can't seem to find the download. Probably it's too obvious and therefore I'm missing it.

You can try the "Testing" branch I guess.

The default "Master" branch is empty.

Link to comment
Share on other sites

That looks good. some helper lua scripts that provide different kinds of data, functions should be very useful when later users start to write actual scripts.

That's exactly what the actual Selene API is made for. The luanet backend is mostly for advanced scriptery around some internal stuff.

You can try the "Testing" branch I guess.

The default "Master" branch is empty.

Exactly, fixed the github link.

Okay, found it, tried it. Doesn't have toolbar integration somehow, and I cannot attach the core. Halp!111!

Have a recent version of the toolbar installed, download selene from testing branch, if you downloaded it via zip file kill githubs annoying -testing appendix to the foldername so it's just GameData/Selene and you should be up and running.

Link to comment
Share on other sites


NullReferenceException: Object reference not set to an instance of an object
at SeleneKSP.ModuleSelene.OnSave (.ConfigNode node) [0x00000] in <filename unknown>:0

at PartModule.Save (.ConfigNode node) [0x00000] in <filename unknown>:0

at ShipConstruct.SaveShip () [0x00000] in <filename unknown>:0

at ShipConstruction.CreateBackup (.ShipConstruct ship) [0x00000] in <filename unknown>:0

at EditorLogic.SetBackup () [0x00000] in <filename unknown>:0

at EditorLogic.UpdatePartMode () [0x00000] in <filename unknown>:0

at EditorLogic.Update () [0x00000] in <filename unknown>:0

Oops... it throws this exception when I am trying to attach SeleneCPU onto my ship.

Link to comment
Share on other sites


NullReferenceException: Object reference not set to an instance of an object
at SeleneKSP.ModuleSelene.OnSave (.ConfigNode node) [0x00000] in <filename unknown>:0

at PartModule.Save (.ConfigNode node) [0x00000] in <filename unknown>:0

at ShipConstruct.SaveShip () [0x00000] in <filename unknown>:0

at ShipConstruction.CreateBackup (.ShipConstruct ship) [0x00000] in <filename unknown>:0

at EditorLogic.SetBackup () [0x00000] in <filename unknown>:0

at EditorLogic.UpdatePartMode () [0x00000] in <filename unknown>:0

at EditorLogic.Update () [0x00000] in <filename unknown>:0

Oops... it throws this exception when I am trying to attach SeleneCPU onto my ship.

*facepalm* that's my fault, forgot to sync after merging dev branch into testing, try again after redownloading, should work now

Link to comment
Share on other sites

This sounds like a scripting engine I will actually use! I even like the name. ;)

With all due respect to the valliant efforts of the other script engine writers, but I'm really not interested in learning yet another limited language that lacks absolutely basic features like vectormath.

(Vector math is in.... right? :P )

Link to comment
Share on other sites

Umm... something is still wrong...

Failed to load assembly E:\Kerbal Space Program v0.23\GameData\Selene\KopiLua.dll:
System.Security.SecurityException: Assembly E:\Kerbal Space Program v0.23\GameData\Selene\KopiLua.dll tried to use forbidden type System.Diagnostics.Process!
Allows launching of external processes.

at AssemblyLoader.ScanForBadTypeRefs (System.String file) [0x00000] in <filename unknown>:0

at AssemblyLoader.LoadExternalAssembly (System.String file) [0x00000] in <filename unknown>:0

...
...

Type wasn't correct

Type wasn't Params object.

Type wasn't correct

Type wasn't Params object.

Part seleneStack_4294476404 cannot load module #2. It only has 2 modules defined

I checked the Alt+F12 -> Database -> Assemblies.

There're:

KeraLua, NLua, SeleneCore, SeleneKSP. But I cannot find KopiLua.

Edited by HoneyFox
Link to comment
Share on other sites

Umm... something is still wrong...

Failed to load assembly E:\Kerbal Space Program v0.23\GameData\Selene\KopiLua.dll:
System.Security.SecurityException: Assembly E:\Kerbal Space Program v0.23\GameData\Selene\KopiLua.dll tried to use forbidden type System.Diagnostics.Process!
Allows launching of external processes.

at AssemblyLoader.ScanForBadTypeRefs (System.String file) [0x00000] in <filename unknown>:0

at AssemblyLoader.LoadExternalAssembly (System.String file) [0x00000] in <filename unknown>:0

...
...

Type wasn't correct

Type wasn't Params object.

Type wasn't correct

Type wasn't Params object.

Part seleneStack_4294476404 cannot load module #2. It only has 2 modules defined

I checked the Alt+F12 -> Database -> Assemblies.

There're:

KeraLua, NLua, SeleneCore, SeleneKSP. But I cannot find KopiLua.

Ah the joys of getting WIP stuff up running on any other machine but my own. I tried getting it to work with a vanilla installation, fixed two or three things and it _should_ be working now :D

Redownload Rinse Repeat

Link to comment
Share on other sites

Ah the joys of getting WIP stuff up running on any other machine but my own. I tried getting it to work with a vanilla installation, fixed two or three things and it _should_ be working now :D

Redownload Rinse Repeat

:( No it still doesn't work. And the output_log.txt contains the same error information.

Do you have similar output in your log file? especially the KopiLua.dll assembly.

Link to comment
Share on other sites

I just noticed blizzy removed the default icon from his latest versions of the toolbar, fixed that for the moment.

Yes KopiLua fails to get loaded by the AssemblyLoader but it doesn't really matter as mono fixes that internally (that would only be problematic if that would be the initial plugin to be used by KSP but that's what the SeleneKSP is for)

RRR

If that still doesn't work message me on the ksp modders IRC, steam or somewhere else, still got half an hour before I leave

Link to comment
Share on other sites

Ok my first practical script! :D

timer = 0.0
burnTime = 0.0

function BurnRCSUllage(burnTimeVal)
burnTime = burnTimeVal
timer = 0.0
print("Burn RCS for " .. burnTime .. " seconds.")
end

function MainLoop(delta)
Time = luanet.import_type('UnityEngine.Time')
deltaTime = Time.deltaTime
if(burnTime == 0.0) then
return 0
end

timer = timer + deltaTime
if(timer > burnTime) then
timer = 0.0
burnTime = 0.0
else
FlightGlobals = luanet.import_type('FlightGlobals')
CurrentVessel = FlightGlobals.ActiveVessel
FlightCtrl = CurrentVessel.ctrlState

if(FlightCtrl.mainThrottle == 0.0) then
FlightCtrl.Z = FlightCtrl.Z - 1.0
print("Time: " .. timer .. "/" .. burnTime)
end
end

return 0
end

Selene:RegisterTick(MainLoop)
BurnRCSUllage(10)

Now it seems like we need a way to input commands, otherwise it's not convenient to call that function with the number I want...

Edited by HoneyFox
Link to comment
Share on other sites

  • 3 weeks later...

Hi, I tried to test Selene. So I downloaded the ZIP of the testing and in-dev branch and extracted them into the GameData directory. The result is, that I'm able to add the unit to my craft and the Toolbar allows me to make it visible, but it doesn't show up anyway.

Fabian

Link to comment
Share on other sites

  • 4 weeks later...

YES!

I started using LUA when I found ComputerCraft for Minecraft which adds computers that run LUA.

I'm really looking forward to this plugin maturing!

Is it pretty much a full LUA interpreter? ie In ComputerCraft there are a few non-working functions due to the specific LUA implementation but other than that it's all there.

Will we be able to do stuff like raise/lower gears. Do science? Transfer fuel?

Edited by OminousPenguin
Link to comment
Share on other sites

YES!

I started using LUA when I found ComputerCraft for Minecraft which adds computers that run LUA.

I'm really looking forward to this plugin maturing!

Is it pretty much a full LUA interpreter? ie In ComputerCraft there are a few non-working functions due to the specific LUA implementation but other than that it's all there.

Will we be able to do stuff like raise/lower gears. Do science? Transfer fuel?

You should be able to do everything with how the program is now, if the part of the api you wish to use isn't exposed you can expose it using luanet.import_type. I wanted to stage my rocket, so I imported the staging class and used it to stage my rocket

function stage()	Stag = luanet.import_type('Staging')
if(Stag.CurrentStage == 0) then
return
end
print('Staging')
Stag.ActivateNextStage()
end

You can use the various API references in the wiki to help you, or if you have visual studio, open up the source of the mod and browse the classes in the object browser.

Link to comment
Share on other sites

Ah ok. Thanks.

So I could use Remote Tech 2's API methods like this: ?


RTech = luanet.import_type('RemoteTech.API')
local guid = myVessel.getGuid() // No idea how to get guid yet
local x = RTech.HasFlightComputer(guid)
// ...

I'm away from home so I can't actually start playing around with it yet otherwise I'd try before asking.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...