Skip to content

Commit

Permalink
[Lua]Add support for XRSound (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGondos authored Aug 16, 2024
1 parent d709860 commit c630b2d
Show file tree
Hide file tree
Showing 7 changed files with 712 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Orbitersdk/samples/Lua/Atlantis/Src/APMFD.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

local AscentApMfd = Class()

local snd = xrsound.create_instance("Atlantis.lua")
snd:load_wav(1, "XRSound/Default/Autopilot On.wav", PlaybackType.InternalOnly)
snd:load_wav(2, "XRSound/Default/Autopilot Off.wav", PlaybackType.InternalOnly)

-- ==============================================================
-- init: called automatically when the MFD instance is created.
-- This is your one chance to save the vessel handle
Expand Down Expand Up @@ -332,6 +336,7 @@ end
function AscentApMfd:on_launch()
if not self.ap.active() then
if self.vessel.get_status() == 0 then
snd:play_wav(1)
self.ap.launch()
self:invalidate_buttons()
end
Expand All @@ -345,6 +350,7 @@ end
-- ==============================================================
function AscentApMfd:on_engage()
if not self.ap.active() then
snd:play_wav(1)
self.ap.engage()
self:invalidate_buttons()
end
Expand All @@ -357,6 +363,7 @@ end
-- ==============================================================
function AscentApMfd:on_disengage()
if self.ap.active() then
snd:play_wav(2)
self.ap.disengage()
self:invalidate_buttons()
end
Expand Down
4 changes: 4 additions & 0 deletions Src/Module/LuaScript/LuaInterpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@ set(BUILD_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/out)
add_library(LuaInterpreter SHARED
Interpreter.cpp
lua_vessel_mtd.cpp
lua_xrsound.cpp
)

target_include_directories(LuaInterpreter
PUBLIC ${ORBITER_SOURCE_SDK_INCLUDE_DIR}
PUBLIC ${ORBITER_BINARY_SDK_DIR}/include
PUBLIC ${ORBITER_BINARY_SDK_DIR}/XRSound/
)

target_link_libraries(LuaInterpreter
${ORBITER_LIB}
${ORBITER_SDK_LIB}
lua
XRSound_lib
)

add_dependencies(LuaInterpreter
${OrbiterTgt}
Orbitersdk
D3D9Client
D3D9Client_Interface
XRSound_lib
)

set_target_properties(LuaInterpreter
Expand Down
40 changes: 38 additions & 2 deletions Src/Module/LuaScript/LuaInterpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ void Interpreter::Initialise ()
LoadSketchpadAPI (); // load Sketchpad methods
LoadAnnotationAPI (); // load screen annotation methods
LoadVesselStatusAPI ();
LoadXRSoundAPI ();
LoadStartupScript (); // load default initialisation script
}

Expand Down Expand Up @@ -375,11 +376,17 @@ const char *Interpreter::lua_tostringex (lua_State *L, int idx, char *cbuf)
/* uses 'key' (at index -2) and 'value' (at index -1) */
char fieldstr[256] = "\0";
if (lua_isstring(L,-2)) sprintf (fieldstr, "%s=", lua_tostring(L,-2));
strcat (fieldstr, lua_tostringex (L,-1));
if(lua_istable(L, -1)) // cut the tree to prevent stack overflow with recursive table
strcat (fieldstr, "[table]");
else
strcat (fieldstr, lua_tostringex (L,-1));
strcat (tbuf, fieldstr); strcat (tbuf, "\n");
lua_pop(L, 1);
}
return tbuf;
} else if (lua_isfunction (L, idx)) {
strcpy (cbuf, "[function]");
return cbuf;
} else {
cbuf[0] = '\0';
return cbuf;
Expand Down Expand Up @@ -513,6 +520,7 @@ void Interpreter::lua_pushvessel (lua_State *L, VESSEL *v)
lua_pop(L,1); // pop nil
VESSEL **pv = (VESSEL**)lua_newuserdata(L,sizeof(VESSEL*));
*pv = v;
knownVessels.insert(pv);
luaL_getmetatable (L, "VESSEL.vtable"); // retrieve metatable
lua_setmetatable (L,-2); // and attach to new object
LoadVesselExtensions(L,v); // vessel environment
Expand All @@ -522,6 +530,17 @@ void Interpreter::lua_pushvessel (lua_State *L, VESSEL *v)
// note that now the object is on top of the stack
}
}
int Interpreter::lua_isvessel(lua_State *L, int idx)
{
if(lua_isuserdata(L, idx)) {
void *ud = lua_touserdata(L, idx);
if(knownVessels.find(ud)!=knownVessels.end()) {
return true;
}
}
luaL_error(L, "Invalid parameter %d, vessel expected", idx);
return false;
}

void Interpreter::lua_pushmfd (lua_State *L, MFD2 *mfd)
{
Expand Down Expand Up @@ -1037,6 +1056,13 @@ void Interpreter::LoadAPI ()
};
luaL_openlib (L, "term", termLib, 0);

// Load XRSound library
static const struct luaL_reg XRSoundLib[] = {
{"create_instance", xrsound_create_instance},
{NULL, NULL}
};
luaL_openlib (L, "xrsound", XRSoundLib, 0);

// Set up global tables of constants

// Key ID table
Expand Down Expand Up @@ -5756,7 +5782,17 @@ int Interpreter::oapi_setup_customcamera(lua_State *L)
cc = (CustomCamera_Lua *)luaL_checkudata(L, 1, "CustomCamera.vtable");
hCam = cc->hCam;
}
VESSEL *hVessel = (VESSEL *)lua_touserdata(L,2); //FIXME: use lua_tovessel when merging with xrsound branch
OBJHANDLE hVessel = nullptr;
void *ud = lua_touserdata(L,2);
if(oapiIsVessel(ud)) {
hVessel = (OBJHANDLE)ud;
} else if(lua_isvessel(L, 2)) {
VESSEL *v = (VESSEL *)lua_tovessel(L, 2);
hVessel = v->GetHandle();
} else {
luaL_error(L, "vessel of vessel handle expected");
}

VECTOR3 pos = lua_tovector(L, 3);
VECTOR3 dir = lua_tovector(L, 4);
VECTOR3 up = lua_tovector(L, 5);
Expand Down
33 changes: 33 additions & 0 deletions Src/Module/LuaScript/LuaInterpreter/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {

#include "OrbiterAPI.h"
#include "VesselAPI.h" // for TOUCHDOWNVTX
#include <unordered_set>

class gcCore;

Expand Down Expand Up @@ -57,6 +58,7 @@ class gcCore;

class VESSEL;
class MFD2;
class XRSound;

struct AirfoilContext {
lua_State *L;
Expand Down Expand Up @@ -307,6 +309,7 @@ class INTERPRETERLIB Interpreter {

// type extraction with checks
static VESSEL *lua_tovessel_safe (lua_State *L, int idx, const char *funcname);
static int lua_isvessel(lua_State *L, int idx);

static int lua_tointeger_safe (lua_State *L, int idx, const char *funcname);
static double lua_tonumber_safe (lua_State *L, int idx, const char *funcname);
Expand Down Expand Up @@ -1097,6 +1100,33 @@ class INTERPRETERLIB Interpreter {

friend int OpenHelp (void *context);

// -------------------------------------------
// XRSound
// -------------------------------------------
virtual void LoadXRSoundAPI ();
static int lua_isxrsound(lua_State *L, int idx);
static XRSound *lua_toxrsound(lua_State *L, int idx);
static int xrsound_create_instance(lua_State *L);
static int xrsound_is_present(lua_State *L);
static int xrsound_get_version(lua_State *L);
static int xrsound_load_wav(lua_State *L);
static int xrsound_play_wav(lua_State *L);
static int xrsound_stop_wav(lua_State *L);
static int xrsound_is_wavplaying(lua_State *L);
static int xrsound_set_paused(lua_State *L);
static int xrsound_is_paused(lua_State *L);
static int xrsound_set_defaultsoundenabled(lua_State *L);
static int xrsound_get_defaultsoundenabled(lua_State *L);
static int xrsound_set_defaultsoundgroupfolder(lua_State *L);
static int xrsound_get_defaultsoundgroupfolder(lua_State *L);
static int xrsound_set_pan(lua_State *L);
static int xrsound_get_pan(lua_State *L);
static int xrsound_set_playbackspeed(lua_State *L);
static int xrsound_get_playbackspeed(lua_State *L);
static int xrsound_set_playposition(lua_State *L);
static int xrsound_get_playposition(lua_State *L);
static int xrsound_collect(lua_State *L);

private:
HANDLE hExecMutex; // flow control synchronisation
HANDLE hWaitMutex;
Expand All @@ -1114,6 +1144,9 @@ class INTERPRETERLIB Interpreter {
int (*postfunc)(void*);
void *postcontext;

static inline std::unordered_set<void *>knownVessels; // for lua_isvessel


static int lua_tointeger_safe (lua_State *L, int idx, int prmno, const char *funcname);
static double lua_tonumber_safe (lua_State *L, int idx, int prmno, const char *funcname);
static bool lua_toboolean_safe (lua_State *L, int idx, int prmno, const char *funcname);
Expand Down
2 changes: 1 addition & 1 deletion Src/Module/LuaScript/LuaInterpreter/config.ld
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ format='markdown'
backtick_references=false
readme='intro.md'
wrap=true
file={'./interpreter.cpp','./lua_vessel_mtd.cpp','./types.lua'}
file={'./interpreter.cpp','./lua_vessel_mtd.cpp','./lua_xrsound.cpp','./types.lua'}
dir='out'
style='!fixed'
kind_names={topic='Manual',script='Programs'}
Expand Down
Loading

0 comments on commit c630b2d

Please sign in to comment.