Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions Orbitersdk/include/OrbiterAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4492,21 +4492,6 @@ OAPIFUNC int oapiDelInterpreter (INTERPRETERHANDLE hInterp);
*/
OAPIFUNC bool oapiExecScriptCmd (INTERPRETERHANDLE hInterp, const char *cmd);

/**
* \brief Passes a command to an interpreter instance for execution.
* \param hInterp interpreter handle
* \param cmd Lua command to be executed
* \return \e false on error (interpreter library not found, or command error)
* \note This function returns immediately. The command is executed during the
* next postStep cycle. If more asynchronous commands are issued before
* execution starts, they are appended to the execution list. If the
* interpreter receives a synchronous request (oapiExecScriptCmd) before the
* asynchrounous commands are executed, the synchronous command is executed
* immediately, while the asynchronous requests continue waiting.
* \sa oapiExecScriptCmd, oapiCreateInterpreter, oapiDelInterpreter
*/
OAPIFUNC bool oapiAsyncScriptCmd (INTERPRETERHANDLE hInterp, const char *cmd);

//typedef struct lua_State lua_State;
OAPIFUNC lua_State *oapiGetLua (INTERPRETERHANDLE hInterp);
//@}
Expand Down
190 changes: 1 addition & 189 deletions Script/oapi_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,195 +33,7 @@ function run_global (script)
dofile(script)
end

-- -------------------------------------------------
-- Branch management
-- Branch threads are stored in table 'branch' with
-- counter 'branch.count'
-- -------------------------------------------------

branch = {}
branch.count = 0
branch.nslot = 0

-- Create a new branch coroutine, store it in the branch
-- table, and execute its first cycle

function proc.bg (func,...)
-- first, collect all dead branches
for i=1,branch.nslot do
if branch[i] ~= nil then
if coroutine.status(branch[i]) == 'dead' then
branch[i] = nil
branch.count = branch.count-1
end
end
end

-- check if there is a free slot
local slot = 0
for i=1,branch.nslot do
if branch[i] == nil then
slot = i
break
end
end

-- no free slot: increase branch counter
if slot == 0 then
branch.nslot = branch.nslot+1
slot = branch.nslot
end

-- create the new branch
local th = coroutine.create (func)
branch[slot] = th
branch.count = branch.count+1
coroutine.resume (th,...)
term.out ('job id='..slot..' ('..branch.count..' jobs)')
return slot
end

-- Remove a branch from the branch table (and keep fingers
-- crossed that the coroutine will be garbage-collected)

function proc.kill (n)
if branch[n] ~= nil then
branch[n] = nil
branch.count = branch.count-1
while (branch.nslot > 0) and (branch[branch.nslot] == nil) do
branch.nslot = branch.nslot-1
end
term.out ('job '..n..' killed ('..branch.count..' jobs left)')
end
end

-- Time skip: branches yield, the main trunk resumes all
-- coroutines for a single cycle, then calls proc.Frameskip
-- to pass control back to orbiter for a new simulation cycle

function proc.skip ()
if coroutine.running() == nil then -- we are in the main trunk
for i=1,branch.nslot do
if branch[i] ~= nil then
coroutine.resume (branch[i])
end
end
proc.Frameskip() -- hand control to orbiter for one cycle
if wait_exit ~= nil then
error("Lua thread terminated") -- return to caller immediately
-- WARNING: then string must be matched in Interpreter::LuaCall
end
else
coroutine.yield()
end
end

-- -------------------------------------------------
-- A few waiting functions
-- -------------------------------------------------

-- wait for simulation time t.
-- Optionally execute a function at each frame while waiting
function proc.wait_simtime (t, f, ...)
while oapi.get_simtime() < t do
if f then
f(unpack(arg))
end
proc.skip()
end
end

-- wait for simulation interval dt
-- Optionally execute a function at each frame while waiting
function proc.wait_simdt (dt, f, ...)
local t1 = oapi.get_simtime()+dt
while oapi.get_simtime() < t1 do
if f then
f(unpack(arg))
end
proc.skip()
end
end

-- wait for system time t
-- Optionally execute a function at each frame while waiting
function proc.wait_systime (t, f, ...)
while oapi.get_systime() < t do
if f then
f(unpack(arg))
end
proc.skip()
end
end

-- wait for system interval dt
-- Optionally execute a function at each frame while waiting
function proc.wait_sysdt (dt, f, ...)
local t1 = oapi.get_systime()+dt
while oapi.get_systime() < t1 do
if f then
f(unpack(arg))
end
proc.skip()
end
end

-- wait for a given number of frames
-- Optionally execute a function at each frame while waiting
function proc.wait_frames(n, f, ...)
local frame = 0
while frame < n do
if f then
f(unpack(arg))
end
frame = frame+1
proc.skip()
end
end

-- wait for f() >= tgt
function proc.wait_ge (f, tgt, ...)
while f(unpack(arg)) < tgt do proc.skip() end
end

-- wait for f() <= tgt
function proc.wait_le (f, tgt, ...)
while f(unpack(arg)) > tgt do proc.skip() end
end

-- wait for input
function proc.wait_input (title)
oapi.open_inputbox (title)
local ans = oapi.receive_input ()
while ans == nil do
proc.skip()
ans = oapi.receive_input ()
end
return ans
end

-- -------------------------------------------------
-- Private functions (should not be directly called)
-- -------------------------------------------------

-- Idle loop (called after command returns, to allow
-- background jobs to continue executing)

function _idle ()
for i=1,branch.nslot do
if branch[i] ~= nil then
coroutine.resume (branch[i])
end
end
return branch.count
end

-- Returns the number of background jobs

function _nbranch ()
return branch.count
end

proc = require("proc")

-- helpers for basic types
function _V(x,y,z)
Expand Down
Loading
Loading