Orbiter-Forum  

Go Back   Orbiter-Forum > Orbiter Addons > Orbiter Lua Scripting
Register Blogs Orbinauts List Social Groups FAQ Projects Mark Forums Read

Orbiter Lua Scripting Find support, share snippets and general discussion around using the Lua engine in Orbiter.

Reply
 
Thread Tools
Old 09-28-2018, 08:46 AM   #1
dgatsoulis
Orbinaut
 
dgatsoulis's Avatar
Default Addition of oapiCreateVesselEx

With vessel deletion via oapi.del_vessel and vessel status manipulation via v_defset_status, one thing that's missing is vessel creation in lua.

I've been trying to add this function in the interpreter without much success.
I'd like it to work in a similar way that vessel creation works with the Scenario Editor:
-Enter name
-Enter Classname
-Vessel gets created in orbit around Earth:
R = 1.1 planet radius
Inc = 0 (ecl)
etc...

Once the vessel is created then manipulating its state is easy, either via set_elements or v_defset_status.

I believe that the addition of this function would open up a range of possibilities in lua scripting, so is there anyone who can do this?

Thanks in advance



---------- Post added 28th Sep 2018 at 11:46 ---------- Previous post was 27th Sep 2018 at 22:21 ----------

I am making some progress with this, but still having some trouble.

So far I've been able to spawn a vessel in Orbiter with lua.

I entered this in the Interpreter.h
Code:
...
static int oapiSetFocusObject (lua_State *L);
static int oapiCreateVessel(lua_State *L); -- added here
static int oapiGetObjectHandle (lua_State *L);
...
These in Interpreter.cpp
Code:
...
{"set_focusobj", oapiSetFocusObject},
{"create_vessel", oapiCreateVessel}, --added
{"get_objhandle", oapiGetObjectHandle},
...
Borrowed from the ScnEditor example
Code:
int Interpreter::oapiCreateVessel(lua_State *L)
{
	OBJHANDLE hVessel;
	const char *name = ("Jim"); // lua_tostring(L, 1);
	const char *classname = ("xr2ravenstar"); //lua_tostring(L, 1);
	VESSELSTATUS2 vs;
	memset(&vs, 0, sizeof(vs));
	vs.version = 2;
	vs.rbody = oapiGetGbodyByName("Earth");
	if (!vs.rbody) vs.rbody = oapiGetGbodyByIndex(0);
	double rad = 1.1 * oapiGetSize(vs.rbody);
	double vel = sqrt(GGRAV * oapiGetMass(vs.rbody) / rad);
	vs.rpos = _V(rad, 0, 0);
	vs.rvel = _V(0, 0, vel);
	hVessel = oapiCreateVesselEx(name, classname, &vs);
	return 1;
}
It compiles without errors and when I call oapi.create_vessel() in the script,
an XR2 Ravenstar (named "Jim") gets placed in orbit around Earth.

When I recompile with
Code:
const char *classname = ("deltaglider");
I get a freeze and a few seconds later an "orbiter.exe is not responding" message. Same thing happens with classname = ("DG-S").

With classname = ("Atlantis"), I also get a freeze, but it lasts only for 2 seconds and the vessel spawns ok.

Does anyone have any idea why this is happening?

Last edited by dgatsoulis; 09-28-2018 at 10:15 AM. Reason: typ, thanks fred18!
dgatsoulis is offline   Reply With Quote
Old 09-28-2018, 10:08 AM   #2
fred18
Addon Developer

Default

classname, should be DG-S not GL-S, am I right?

---------- Post added at 11:08 ---------- Previous post was at 11:07 ----------

I don't know if it's case sensitive, did you try also "Deltaglider" ?
fred18 is offline   Reply With Quote
Thanked by:
Old 09-28-2018, 10:13 AM   #3
dgatsoulis
Orbinaut
 
dgatsoulis's Avatar
Default

Quote:
Originally Posted by fred18 View Post
 classname, should be DG-S not GL-S, am I right?

Yeah, my mistake sorry. DG-S is the correct classname which I used in the .cpp

I'll edit the OP.

---------- Post added at 11:08 ---------- Previous post was at 11:07 ----------

I don't know if it's case sensitive, did you try also "Deltaglider" ?
I did with no result. Don't think it's case sensitive. "Atlantis" and "atlantis" produce the same result and "xr2ravenstar"/"XR2Ravenstar" work ok too.
Same goes for "Carina"/"carina", "HST"/"hst" and so on.
dgatsoulis is offline   Reply With Quote
Old 09-28-2018, 05:14 PM   #4
dgatsoulis
Orbinaut
 
dgatsoulis's Avatar
Default

ok, so I cleaned it up a little bit, so that I can enter the name and class name of the vessel in lua and the function creates the vessel that I asked and returns a handle to it.

Code:
int Interpreter::oapiCreateVessel(lua_State *L)
{
	OBJHANDLE hVessel;
	const char *name = lua_tostring(L, 1); 
	const char *classname = lua_tostring(L, 2); 
	VESSELSTATUS2 vs;
	memset(&vs, 0, sizeof(vs));
	vs.version = 2;
	vs.rbody = oapiGetGbodyByName("Earth");
	if (!vs.rbody) vs.rbody = oapiGetGbodyByIndex(0);
	double rad = 1.1 * oapiGetSize(vs.rbody);
	double vel = sqrt(GGRAV * oapiGetMass(vs.rbody) / rad);
	vs.rpos = _V(rad, 0, 0);
	vs.rvel = _V(0, 0, vel);
	hVessel = oapiCreateVesselEx(name, classname, &vs);
	if (hVessel) lua_pushlightuserdata(L, hVessel);
	else lua_pushnil(L);
	return 1;
}
The scenario starts in a Deltaglider landed at KSC.
This is my script:

Code:
note = oapi.create_annotation()
note:set_pos (0.8,0.06,1,1)
note:set_colour ({r=0,g=1,b=0})
note:set_size (0.75)

v = vessel.get_focusinterface() -- Get the interface of the focused ship at the start of the scenario
h = v:get_handle() -- Get its handle

name = proc.wait_input("vessel name?") -- String with new vessel's name
classname = proc.wait_input("vessel class?") -- String with new vessel's classname. CAREFUL! No error controls yet!

hvessel = oapi.create_vessel(name,classname) -- Create new vessel and return handle

v2 = vessel.get_interface(hvessel) -- Get new vessel's interface 

name = v2:get_name() -- Get new vessel's name

cname = v2:get_classname() -- Get new vessel's classname

msg = string.format("Name: %s\nClass: %s",name,cname) -- Show name and classname on screen 
note:set_text(msg)

oapi.set_cameratarget(hvessel,1) -- Switch to external camera of new vessel (see what we got).

proc.wait_simdt(3) -- Wait 3 seconds 

oapi.set_cameratarget(h,0) -- Return to internal camera of starting vessel

--loop to reset script 
anim = 0
while anim < 1 do
 anim = v:get_animation(17) -- Deltaglider VC undock lever
 proc.skip()
 if anim ~= 0 then
 end
end
-- If I pull the undock lever the rest follows
oapi.del_vessel(hvessel) -- Delete the created vessel
oapi.del_annotation(note) -- Delete the note
package.loaded["Script/createtest"] = nil -- Unload the script
proc.wait_simdt(1) -- Wait 1 second
require ("Script/createtest") -- Reload the script


I tried to spawn each end every vessel in the default Orbiter installation.
Here are the results:
Code:
Atlantis -- Freeze for ~2 seconds but spawns ok
Atlantis_SRB -- ok
Atlantis_Tank -- ok
Carina --ok
Deltaglider -- Freeze!
DG-S -- Freeze!
Dragonfly -- ok
Galileo -- ok (but no mesh because of empty file)
HST -- ok
ISS -- ok
LDEF -- ok
Leonardo_mplm --ok
MG_Atlantis -- Freeze for ~2 seconds but spawns ok
Mir -- ok
Module1 -- ok
Module2 -- ok
mplm -- ok
nasa_mmu -- ok
ProjectAlpha_ISS -- ok
ScriptPB -- ok
ShuttleA -- ok
ShuttleA_pl -- ok
ShuttlePB -- ok
SolarSail -- ok
Wheel -- ok
The classname of the vessels that did spawn was NOT case sensitive.
(Carina, CARINA, carina, CaRiNa...etc) spawned just fine. Same for all other vessels except the Deltagliders.

I am really at a loss here, can't figure out why this is happening.

Can someone please test this also and report their results?

I have attached here the LuaInterpreter.dll, script and scenario. Unzip in your Orbiter directory and run the CreateTest scenario.

Keep a backup of your Orbiterroot\LuaInterpreter.dll because it will be overwritten!

You start in a Deltaglider at KSC.

Upon scenario Start you will get a prompt asking you for a name. Enter a name for the vessel that will be created.

Then you'll be asked a classname. Enter a classname from the vessels located in your Orbiterrroot\Config\Vessels folder

The vessel will be created, a text showing its name and classname will be shown in the upper right corner and the camera will switch to it. After 3 seconds, the camera will switch back to the starting Deltaglider.
If you pull the VC undock lever (red one on the lower right) the script, deletes the spawned vessel and resets asking again for a new vessel name.

Please report your findings here.

Thanks in advance
Attached Files
File Type: zip CreateTest.zip (70.5 KB, 3 views)
dgatsoulis is offline   Reply With Quote
Old 09-29-2018, 03:59 AM   #5
turtle91
Orbinaut
Default

Even if it might not help you much, but I have tested this.

-created a carina vessel, with a custom name (sat1) = no problem
-created a Deltaglider vessel (name DG7777) = freeze

However, my setup is not the best for general Orbiter troubleshooting.
I am running all the stuff in Linux using Wine (with DXD9-client).

Btw...I am really interested in your customized GV-module...:-)
turtle91 is offline   Reply With Quote
Thanked by:
Old 09-29-2018, 04:40 AM   #6
dgatsoulis
Orbinaut
 
dgatsoulis's Avatar
Default

Thank you for testing! At least I know that it isn't just me. It's really weird though, I don't see any reason why it doesn't spawn the Deltagliders. I'll give it a shot with oapiCreateVessel instead of oapiCreateVesselEx and see if it also freezes.

About the GV-Module, I'll PM you shortly.
dgatsoulis is offline   Reply With Quote
Thanked by:
Old 09-29-2018, 05:30 AM   #7
turtle91
Orbinaut
Default

I tested a bit more, but again...it's Linux..not the best platform to troubleshoot Windows apps using wine....
First I compared the i.e Dragonfly with the Deltaglider class-files.
So..just for testing I removed the "Editormodule"-entry from the class-file, to match what I have in the DragonFly-class. (only the moduleentry, but no EditorModule-entry)

However, still hangs.

So I used strace against the hanging Orbiter process, just to find out, that it hangs in a READ of a FD.
This FD points to a FIFO...and this FIFO points to /dev/null...:-(

Edit:

Did another test:
I typed-in a non-existing class-name, and the issue is the same...Orbiter hangs...strace points to the READ to a FIFO..
Could it be, that for whatever reason, the class-call is "sometimes" missing Orbiter directory-structure (=file not found...not searching in VESSELS directory ?)

Last edited by turtle91; 09-29-2018 at 05:50 AM. Reason: Some more testing....
turtle91 is offline   Reply With Quote
Thanked by:
Old 09-29-2018, 06:45 AM   #8
fred18
Addon Developer

Default

Why not compiling the DG with a special Log call in the startup somewhere and see in the log if it actually gets created or not?
Can I ask what GV-module stands for?
fred18 is offline   Reply With Quote
Thanked by:
Old 09-29-2018, 07:07 AM   #9
turtle91
Orbinaut
Default

>Can I ask what GV-module
It stands for your great General-Vehicle module
The idea was to convert a GV-vessel in a standard (RCS-controlled) vessel when not on ground...on the fly (i.e. UMMU-mesh walks on ground, but can jetpack/RCS in space).
turtle91 is offline   Reply With Quote
Thanked by:
Old 09-29-2018, 07:19 AM   #10
fred18
Addon Developer

Default

Quote:
Originally Posted by turtle91 View Post
 >Can I ask what GV-module
It stands for your great General-Vehicle module
The idea was to convert a GV-vessel in a standard (RCS-controlled) vessel when not on ground...on the fly (i.e. UMMU-mesh walks on ground, but can jetpack/RCS in space).
Ah I see. It shouldn t be too hard to do: it should be enough to enclose the status update which forces the vehicle on the ground and then enabling/disabling it on command. Let me know if you want any help on that
fred18 is offline   Reply With Quote
Thanked by:
Old 09-29-2018, 04:10 PM   #11
turtle91
Orbinaut
Default

Maybe it's a LUA vs LUA issue ?
I mean, the Deltaglider has LUA included (for the atmosphere autopilot)
Maybe there is a problem to call a vessel with LUA, if the vessel has LUA compiled-in.

I can confirm the issue with stock Atlantis, too.
It freezes about 3-4 seconds, and than spawns without further issues.
And there seems to be no LUA included...strange...

XR2...no issues...spawns fine...
However as a test for the DG, maybe trying to remove the LUA depencies within DG's code ?
turtle91 is offline   Reply With Quote
Thanked by:
Old 09-29-2018, 06:41 PM   #12
dgatsoulis
Orbinaut
 
dgatsoulis's Avatar
Default

Maybe you are on to something there.
Every other vessel I've spawned hasn't had any problems. I've even spawned a DGIV without a glitch.
XR2s, various vessels from Wo2001 and everything I've tried so far gets created just fine. Only the default Deltagliders (DeltaGlider and DG-S) seem to have this problem.
The Atlantis delays a bit, but I think that's because of textures loading. If you replace the Deltaglider in the scenario with Atlantis, rerun it and spawn another Atlantis, the new Atlantis spawns as fast as any other ship.

The reason I wanted vessel creation was to handle EVAs and such with lua, so this isn't a complete show-stopper for me.
I can add a check and if the classname of the new vessel is DeltaGlider or DG-S return nil.
Still it would be nice if there was something to workaround this. Even if it works with a recompiled DG without the lua stuff, I can't ask everyone that uses this function to recompile their Deltagliders.


----------------EDIT-------------------
I recompiled the Deltaglider, removing the lua references but the problem remains.
I can create a DeltaGlider or DG-S with the scenario editor just fine, but I cannot create it with the lua function posted above. All other default Orbiter vessels spawn just fine... I think I need some coffee.

Last edited by dgatsoulis; 09-29-2018 at 07:41 PM.
dgatsoulis is offline   Reply With Quote
Thanked by:
Old 09-29-2018, 08:01 PM   #13
turtle91
Orbinaut
Default

If you check the "DGLua.cpp", I could imagine, that this LUA-init might interfear with an allready running LUA-interpreter, which deals with the same vessel.

But I agree, there are not many LUA-driven vessels around. So if it's really the DG's LUA-init, this should be not a show-stopper.
turtle91 is offline   Reply With Quote
Thanked by:
Reply

  Orbiter-Forum > Orbiter Addons > Orbiter Lua Scripting

Tags
lua


Thread Tools

Posting Rules
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Jump


All times are GMT. The time now is 07:57 AM.

Quick Links Need Help?


About Us | Rules & Guidelines | TOS Policy | Privacy Policy

Orbiter-Forum is hosted at Orbithangar.com
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright 2007 - 2017, Orbiter-Forum.com. All rights reserved.