Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/thoda-dev/du-lua-framework. A framework to simplify coding in Dual Universe. Permit to create most of the code in Unit > onStart
dual-universe lua
Something went wrong. Try again.
15 kB · 408 lines
Lua
at commit 824db275
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408--[[ DU-Nested-Coroutines by Jericho Permit to easier avoid CPU Load Errors by using nested coroutines and adapt cycles on the FPS Source unminified available here: https://github.com/Jericho1060/du-nested-coroutines]]--
local print = system.printlocal n_error = errorlocal n_pcall = pcalllocal n_assert = assertlocal cor = coroutinelocal co_create = cor.createlocal co_status = cor.statuslocal co_resume = cor.resumelocal co_status_dead = "dead"local co_status_suspended = "suspended"
function runFunction(fn, errorPrefix, ...) local s, err = n_pcall(fn, ...) if not s then n_error(errorPrefix .. err) endend
local DU_System = { __index = { cos = { update = {}, flush = {} }, fns = { update = {}, flush = {}, action = { start = {}, stop = {}, loop = {} }, inputText = nil, start = nil, stop = nil }, ACTIONS = { FORWARD = "forward", BACKWARD = "backward", YAW_LEFT = "yawleft", YAW_RIGHT = "yawright", STRAFE_LEFT = "strafeleft", STRAFE_RIGHT = "straferight", LEFT = "left", RIGHT = "right", UP = "up", DOWN = "down", GROUND_ALTITUDE_UP = "groundaltitudeup", GROUND_ALTITUDE_DOWN = "groundaltitudedown", LEFT_ALT = "lalt", LEFT_SHIFT = "lshift", GEAR = "gear", LIGHT = "light", BRAKE = "brake", OPTION_1 = "option1", OPTION_2 = "option2", OPTION_3 = "option3", OPTION_4 = "option4", OPTION_5 = "option5", OPTION_6 = "option6", OPTION_7 = "option7", OPTION_8 = "option8", OPTION_9 = "option9", LEFT_MOUSE = "leftmouse", STOP_ENGINES = "stopengines", SPEED_UP = "speedup", SPEED_DOWN = "speeddown", ANTIGRAVITY = "antigravity", BOOSTER = "booster" }, main = { update = co_create(function() end), flush = co_create(function() end), }, update = function(self) local status = co_status(self.main.update) if status == co_status_dead then self.main.update = co_create(function() self:runUpdate() end) elseif status == co_status_suspended then n_assert(co_resume(self.main.update)) end end, flush = function(self) local status = co_status(self.main.flush) if status == co_status_dead then self.main.flush = co_create(function() self:runFlush() end) elseif status == co_status_suspended then n_assert(co_resume(self.main.flush)) end end, action = function(self, event, action) if self.fns.action[event][action] then runFunction(self.fns.action[event][action], "System Action " .. event .. " Error: ") end end, actionStart = function(self, action) self:action('start', action) end, actionStop = function(self, action) self:action('stop', action) end, actionLoop = function(self, action) self:action('loop', action) end, inputText = function(self, text) if self.fns.inputText then runFunction(self.fns.inputText, "System Input Text Error: ", text) end end, runUpdate = function(self) for k,co in pairs(self.cos.update) do local s = co_status(co) if s == co_status_dead then self.cos.update[k] = co_create(self.fns.update[k]) elseif s == co_status_suspended then n_assert(co_resume(co)) end end end, runFlush = function(self) for k,co in pairs(self.cos.flush) do local s = co_status(co) if s == co_status_dead then self.cos.flush[k] = co_create(self.fns.flush[k]) elseif s == co_status_suspended then n_assert(co_resume(co)) end end end, onUpdate = function(self, fns) for k,f in pairs(fns) do self.fns.update[k] = f self.cos.update[k] = co_create(f) end end, onFlush = function(self, fns) for k,f in pairs(fns) do self.fns.flush[k] = f self.cos.flush[k] = co_create(f) end end, onAction = function(self, event, fns) for k,f in pairs(fns) do self.fns.action[event][k] = f end end, onActionStart = function(self, fns) self:onAction("start", fns) end, onActionStop = function(self, fns) self:onAction("stop", fns) end, onActionLoop = function(self, fns) self:onAction("loop", fns) end, onInputText = function(self, fn) self.fns.inputText = fn end }}
local DU_Unit = { __index = { timers = {}, stopFn = function() end, timer = function (self, tag) if self.timers[tag] then runFunction(self.timers[tag], "Unit Timer " .. tag .. " Error: ") end end, setTimer = function(self, tag, seconds, fn) self.timers[tag] = fn unit.setTimer(tag, seconds) end, stopTimer = function(self, tag) unit.stopTimer(tag) self.timers[tag]=nil end, onStop = function (self, fn) self.stopFn = fn end, stop = function(self) if self.stopFn then runFunction(self.stopFn, "Unit Stop Error: ") end end }}
local DU_Player = { __index = { parentChangedFn = function(oldId, newId) end, onParentChange = function (self, fn) self.parentChangedFn = fn end, parentChanged = function (self, oldId, newId) if self.parentChangedFn then runFunction(self.parentChangedFn, "Player Parent Changed Error: ", oldId, newId) end end, }}
local DU_Construct = { __index = { dockedFn = function(id) end, onDocked = function (self, fn) self.dockedFn = fn end, docked = function (self, id) if self.dockedFn then runFunction(self.dockedFn, "Construct Docked Error: ", id) end end, undockedFn = function(id) end, onUndocked = function (self, fn) self.undockedFn = fn end, undocked = function (self, id) if self.undockedFn then runFunction(self.undockedFn, "Construct Undocked Error: ", id) end end, playerBoardedFn = function(id) end, onPlayerBoarded = function (self, fn) self.playerBoardedFn = fn end, playerBoarded = function (self, id) if self.playerBoardedFn then runFunction(self.playerBoardedFn, "Construct Player Boarded Error: ", id) end end, VRStationEnteredFn = function(id) end, onVRStationEntered = function (self, fn) self.VRStationEnteredFn = fn end, VRStationEntered = function (self, id) if self.VRStationEnteredFn then runFunction(self.VRStationEnteredFn, "Construct VR Station Entered Error: ", id) end end, constructDockedFn = function(id) end, onConstructDocked = function (self, fn) self.constructDockedFn = fn end, constructDocked = function (self, id) if self.constructDockedFn then runFunction(self.constructDockedFn, "Construct Construct Docked Error: ", id) end end, PvPTimerFn = function(active) end, onPvPTimer = function (self, fn) self.PvPTimerFn = fn end, PvPTimer = function (self, active) if self.PvPTimerFn then runFunction(self.PvPTimerFn, "Construct PvP Timer Error: ", active) end end, }}
DU_Framework = { __index = { system = setmetatable({}, DU_System), unit = setmetatable({}, DU_Unit), player = setmetatable({}, DU_Player), construct = setmetatable({}, DU_Construct), }}Script = {}setmetatable(Script, DU_Framework)
--[[ You can declare here all the functions you want to call in the diffent events from system, unit, player and construct. Major changes compared to the base game concept: - System onUpdate and onFlush events are now builded with coroutines, in the framework, onUpdate and onFlush are loading a table of functions that will be runned as coroutines. This way you can have multiple functions running at the same time, and you use the yield function to pause the coroutine and let the other coroutines run. Each coroutine will be resume or restarted if dead each time the system onUpdate or onFlush event is called.
]]
--Functions to load as coroutines that will be runned in system > onUpdate (based on FPS)local system_update = {}system_update.co1 = function () for i=0, 10 do system.print("coroutine 1 --- update --- "..i) coroutine.yield() -- pause the coroutine 1, it will wait till the next onUpdate event to be resumed endendsystem_update.co2 = function () for i=0, 10 do system.print("coroutine 2 --- update --- "..i) coroutine.yield() -- pause the coroutine 2, it will wait till the next onUpdate event to be resumed endend
--Functions to load as coroutines that will be runned in system > onFlush (60 times / s)local system_flush = {}system_flush.co1 = function () for i=0, 10 do system.print("coroutine 1 --- flush --- "..i) coroutine.yield() -- pause the coroutine 1, it will wait till the next onFlush event to be resumed endendsystem_flush.co2 = function () for i=0, 10 do system.print("coroutine 2 --- flush --- "..i) coroutine.yield() -- pause the coroutine 2, it will wait till the next onFlush event to be resumed endend
--Function to run on actionslocal system_action_start = {}system_action_start[Script.system.ACTIONS.BRAKE] = function() system.print("I'm braking");endlocal system_action_stop = {}system_action_stop[Script.system.ACTIONS.BRAKE] = function() system.print("I stopped braking");endlocal system_action_loop = {}system_action_loop[Script.system.ACTIONS.BRAKE] = function() system.print("I'm still braking");end
--Function to run when input text to the lua chatlocal system_inputText = function (text) system.print("Input: " .. text)end
--Function to run when the program is stoppinglocal unit_onstop = function () system.print("Program is stopping")end
--Function to run when the player change parentlocal player_onparentchanged = function (oldParent, newParent) system.print("Player changed parent from ID "..oldParent.." to ID "..newParent)end
--Function to run when the construct is docked or undockedlocal construct_onDocked = function(id) system.print("Construct docked on ID "..id)endlocal construct_onUndocked = function(id) system.print("Construct undocked from ID "..id)end
--Function to run when a player board the constructlocal construct_onplayerboarded = function(id) system.print("Player with ID " .. id .. " boarded construct")end
--Function to run when a player enter the VR Stationlocal construct_vrstationentered = function(id) system.print("Player with ID " .. id .. " entered VR Station")end
--Function to run when another construct is docked on this constructlocal construct_constructdocked = function(id) system.print("Construct with ID " .. id .. " docked on this construct")end
--Function to run when pvp timer is changing statelocal construct_onpvptimer = function(active) if active then system.print("PVP is now active") else system.print("PVP is now inactive") endend
--[[ Here how to load the functions in the framework]]Script.system:onUpdate(system_update) --loading coroutines for system > onUpdateScript.system:onFlush(system_flush) --loading coroutines for system > onFlush
Script.system:onActionStart(system_action_start) --loading all "actionStart" functionsScript.system:onActionStop(system_action_stop) --loading all "actionStop" functionsScript.system:onActionLoop(system_action_loop) --loading all "actionLoop" functions
Script.system:onInputText(system_inputText) --loading the function to trigger when input text in lua chat
--[[ Here how to add a timer @param name: the name of the timer, used to remove it with Script.unit:stopTimer(name) @param delay: the delay between each call of the function in seconds @param func: the function to call]]Script.unit:setTimer("hello", 1, function()system.print("hello")end) --add a timer displaying "hello" every secondsScript.unit:setTimer("hello5", 5, function()system.print("hello 5")end) --add a timer displaying "hello 5" every 5 seconds
Script.unit:onStop(unit_onstop) --loading the function to trigger when the program is stopping
Script.player:onParentChanged(player_onparentchanged) --loading the function to trigger when the player change parent
Script.construct:onDocked(construct_onDocked) --loading the function to trigger when the construct is dockedScript.construct:onUndocked(construct_onUndocked) --loading the function to trigger when the construct is undockedScript.construct:onPlayerBoarded(construct_onplayerboarded) --loading the function to trigger when a player board the constructScript.construct:onVRStationEntered(construct_vrstationentered) --loading the function to trigger when a player enter the VR StationScript.construct:onConstructDocked(construct_constructdocked) --loading the function to trigger when another construct is docked on this constructScript.construct:onPvPTimer(construct_onpvptimer) --loading the function to trigger when pvp timer is changing state