Request Addition of oapiCreateVesselEx

dgatsoulis

ele2png user
Donator
Joined
Dec 2, 2009
Messages
1,924
Reaction score
340
Points
98
Location
Sparta
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

:cheers:

---------- 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); [COLOR="Red"]-- added here[/COLOR]
static int oapiGetObjectHandle (lua_State *L);
...

These in Interpreter.cpp
Code:
...
{"set_focusobj", oapiSetFocusObject},
{"create_vessel", oapiCreateVessel}, [COLOR="Red"]--added[/COLOR]
{"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:

fred18

Addon Developer
Addon Developer
Donator
Joined
Feb 2, 2012
Messages
1,666
Reaction score
100
Points
78
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" ?
 

dgatsoulis

ele2png user
Donator
Joined
Dec 2, 2009
Messages
1,924
Reaction score
340
Points
98
Location
Sparta
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

ele2png user
Donator
Joined
Dec 2, 2009
Messages
1,924
Reaction score
340
Points
98
Location
Sparta
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() [COLOR="SeaGreen"]-- Get the interface of the focused ship at the start of the scenario[/COLOR]
h = v:get_handle() [COLOR="seagreen"]-- Get its handle[/COLOR]

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

hvessel = oapi.create_vessel(name,classname) [COLOR="seagreen"]-- Create new vessel and return handle[/COLOR]

v2 = vessel.get_interface(hvessel) [COLOR="seagreen"]-- Get new vessel's interface [/COLOR]

name = v2:get_name() [COLOR="seagreen"]-- Get new vessel's name[/COLOR]

cname = v2:get_classname() [COLOR="seagreen"]-- Get new vessel's classname[/COLOR]

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

oapi.set_cameratarget(hvessel,1) [COLOR="seagreen"]-- Switch to external camera of new vessel (see what we got).[/COLOR]

proc.wait_simdt(3) [COLOR="seagreen"]-- Wait 3 seconds [/COLOR]

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

I tried to spawn each end every vessel in the default Orbiter installation.
Here are the results:
Code:
Atlantis [COLOR="DarkOrange"]-- Freeze for ~2 seconds but spawns ok[/COLOR]
Atlantis_SRB [COLOR="SeaGreen"]-- ok[/COLOR]
Atlantis_Tank [COLOR="seagreen"]-- ok[/COLOR]
Carina [COLOR="seagreen"]--ok[/COLOR]
Deltaglider [COLOR="Red"]-- Freeze![/COLOR]
DG-S [COLOR="red"]-- Freeze![/COLOR]
Dragonfly [COLOR="seagreen"]-- ok[/COLOR]
Galileo [COLOR="seagreen"]-- ok (but no mesh because of empty file)[/COLOR]
HST [COLOR="seagreen"]-- ok[/COLOR]
ISS [COLOR="seagreen"]-- ok[/COLOR]
LDEF [COLOR="seagreen"]-- ok[/COLOR]
Leonardo_mplm [COLOR="seagreen"]--ok[/COLOR]
MG_Atlantis [COLOR="DarkOrange"]-- Freeze for ~2 seconds but spawns ok[/COLOR]
Mir [COLOR="seagreen"]-- ok[/COLOR]
Module1 [COLOR="seagreen"]-- ok[/COLOR]
Module2 [COLOR="seagreen"]-- ok[/COLOR]
mplm [COLOR="seagreen"]-- ok[/COLOR]
nasa_mmu [COLOR="seagreen"]-- ok[/COLOR]
ProjectAlpha_ISS [COLOR="seagreen"]-- ok[/COLOR]
ScriptPB [COLOR="seagreen"]-- ok[/COLOR]
ShuttleA [COLOR="seagreen"]-- ok[/COLOR]
ShuttleA_pl [COLOR="seagreen"]-- ok[/COLOR]
ShuttlePB [COLOR="seagreen"]-- ok[/COLOR]
SolarSail [COLOR="seagreen"]-- ok[/COLOR]
Wheel [COLOR="seagreen"]-- ok[/COLOR]

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
:cheers:
 

Attachments

  • CreateTest.zip
    70.5 KB · Views: 3

turtle91

Active member
Joined
Nov 1, 2010
Messages
319
Reaction score
7
Points
33
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...:)
 

dgatsoulis

ele2png user
Donator
Joined
Dec 2, 2009
Messages
1,924
Reaction score
340
Points
98
Location
Sparta
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.
 

turtle91

Active member
Joined
Nov 1, 2010
Messages
319
Reaction score
7
Points
33
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:

fred18

Addon Developer
Addon Developer
Donator
Joined
Feb 2, 2012
Messages
1,666
Reaction score
100
Points
78
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?
 

turtle91

Active member
Joined
Nov 1, 2010
Messages
319
Reaction score
7
Points
33
>Can I ask what GV-module
It stands for your great General-Vehicle module :tiphat:
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).
 

fred18

Addon Developer
Addon Developer
Donator
Joined
Feb 2, 2012
Messages
1,666
Reaction score
100
Points
78
>Can I ask what GV-module
It stands for your great General-Vehicle module :tiphat:
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
 

turtle91

Active member
Joined
Nov 1, 2010
Messages
319
Reaction score
7
Points
33
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 ?
 

dgatsoulis

ele2png user
Donator
Joined
Dec 2, 2009
Messages
1,924
Reaction score
340
Points
98
Location
Sparta
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:

turtle91

Active member
Joined
Nov 1, 2010
Messages
319
Reaction score
7
Points
33
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.
 
Top