diff --git a/README.md b/README.md index a10bedb..d7d793d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,192 @@ # du-lua-framework - A framework to simplidy coding in Dual Universe. Permit to add create most of the code in Unit > onStart + A framework to simplidy coding in Dual Universe. + + Permit to add create most of the code in Unit > onStart + +# Guilded Server (better than Discord) + +You can join me on Guilded for help or suggestions or requests by following that link : https://guilded.jericho.dev + + +# Support or donation + +if you like it, [](https://ko-fi.com/jericho1060) or you can sponsor me directly on [Github](https://github.com/sponsors/Jericho1060) + +# Documentation and examples + +## How to use it + +Copy the content of the file [`config.json`](https://raw.githubusercontent.com/Jericho1060/du-lua-framework/main/config.json) and paste it on any control unit by right clicking on it and selecting "Advanced" and then "Paste Lua Configuration from clipboard". + +*Warning 1: It can happens that the paste is not working in DU, it'f it's your case, please, restart the game and try again.* + +*Warning 2: Paste may not work in GeForce Now, please install it from a real PC or ask someone else to install it for you.* + +## Major Changes compared to the game logic + +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. + +## Code Examples + +```lua +--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 + end +end +system_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 + end +end + +--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 + end +end +system_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 + end +end + +--Function to run on actions +local system_action_start = {} +system_action_start[Script.system.ACTIONS.BRAKE] = function() + system.print("I'm braking"); +end +local system_action_stop = {} +system_action_stop[Script.system.ACTIONS.BRAKE] = function() + system.print("I stopped braking"); +end +local 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 chat +local system_inputText = function (text) + system.print("Input: " .. text) +end + +--Function to run when the program is stopping +local unit_onstop = function () + system.print("Program is stopping") +end + +--Function to run when the player change parent +local 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 undocked +local construct_onDocked = function(id) + system.print("Construct docked on ID "..id) +end +local construct_onUndocked = function(id) + system.print("Construct undocked from ID "..id) +end + +--Function to run when a player board the construct +local construct_onplayerboarded = function(id) + system.print("Player with ID " .. id .. " boarded construct") +end + +--Function to run when a player enter the VR Station +local construct_vrstationentered = function(id) + system.print("Player with ID " .. id .. " entered VR Station") +end + +--Function to run when another construct is docked on this construct +local construct_constructdocked = function(id) + system.print("Construct with ID " .. id .. " docked on this construct") +end + +--Function to run when pvp timer is changing state +local construct_onpvptimer = function(active) + if active then + system.print("PVP is now active") + else + system.print("PVP is now inactive") + end +end + +--[[ + Here how to load the functions in the framework +]] +Script.system:onUpdate(system_update) --loading coroutines for system > onUpdate +Script.system:onFlush(system_flush) --loading coroutines for system > onFlush + +Script.system:onActionStart(system_action_start) --loading all "actionStart" functions +Script.system:onActionStop(system_action_stop) --loading all "actionStop" functions +Script.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 seconds +Script.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 docked +Script.construct:onUndocked(construct_onUndocked) --loading the function to trigger when the construct is undocked +Script.construct:onPlayerBoarded(construct_onplayerboarded) --loading the function to trigger when a player board the construct +Script.construct:onVRStationEntered(construct_vrstationentered) --loading the function to trigger when a player enter the VR Station +Script.construct:onConstructDocked(construct_constructdocked) --loading the function to trigger when another construct is docked on this construct +Script.construct:onPvPTimer(construct_onpvptimer) --loading the function to trigger when pvp timer is changing state +``` + +## System Actions: + +to use with `Script.system.ACTIONS[ActionKey]` + +```lua +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" +``` diff --git a/config.json b/config.json new file mode 100644 index 0000000..a4299e5 --- /dev/null +++ b/config.json @@ -0,0 +1 @@ +{"events":[],"handlers":[{"code":"Script.system:update()","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"10"},{"code":"Script.system:inputText(text)","filter":{"args":[{"variable":"*"}],"signature":"onInputText(text)","slotKey":"-4"},"key":"11"},{"code":"Script.system:flush()","filter":{"args":[],"signature":"onFlush()","slotKey":"-4"},"key":"12"},{"code":"Script.system:actionStop(action)","filter":{"args":[{"variable":"*"}],"signature":"onActionStop(action)","slotKey":"-4"},"key":"13"},{"code":"Script.system:actionStart(action)","filter":{"args":[{"variable":"*"}],"signature":"onActionStart(action)","slotKey":"-4"},"key":"14"},{"code":"Script.system:actionLoop(action)","filter":{"args":[{"variable":"*"}],"signature":"onActionLoop(action)","slotKey":"-4"},"key":"15"},{"code":"Script.player:parentChanged(oldId, newId)","filter":{"args":[{"variable":"*"},{"variable":"*"}],"signature":"onParentChanged(oldId,newId)","slotKey":"-3"},"key":"3"},{"code":"Script.construct:VRStationEntered(id)","filter":{"args":[{"variable":"*"}],"signature":"onVRStationEntered(id)","slotKey":"-2"},"key":"6"},{"code":"Script.construct:undocked(id)","filter":{"args":[{"variable":"*"}],"signature":"onUndocked(id)","slotKey":"-2"},"key":"8"},{"code":"Script.construct:PvPTimer(active)","filter":{"args":[{"variable":"*"}],"signature":"onPvPTimer(active)","slotKey":"-2"},"key":"4"},{"code":"Script.construct:playerBoarded(id)","filter":{"args":[{"variable":"*"}],"signature":"onPlayerBoarded(id)","slotKey":"-2"},"key":"7"},{"code":"","filter":{"args":[{"variable":"*"}],"signature":"onDocked(id)","slotKey":"-2"},"key":"9"},{"code":"Script.construct:constructDocked(id)","filter":{"args":[{"variable":"*"}],"signature":"onConstructDocked(id)","slotKey":"-2"},"key":"5"},{"code":"Script.unit:timer(tag)","filter":{"args":[{"variable":"*"}],"signature":"onTimer(tag)","slotKey":"-1"},"key":"1"},{"code":"Script.unit:stop()","filter":{"args":[],"signature":"onStop()","slotKey":"-1"},"key":"2"},{"code":"--[[\n DU-LUA-Framework by Jericho\n Permit to code easier by grouping most of the code in a single event Unit > Start\n Unminified Source available here: https://github.com/Jericho1060/du-lua-framework\n]]--\nlocal a=system.print;local b=error;local c=pcall;local d=assert;local e=coroutine;local f=e.create;local g=e.status;local h=e.resume;local i=\"dead\"local j=\"suspended\"function runFunction(k,l,...)local m,n=c(k,...)if not m then b(l..n)end end;local o={__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=f(function()end),flush=f(function()end)},update=function(self)local p=g(self.main.update)if p==i then self.main.update=f(function()self:runUpdate()end)elseif p==j then d(h(self.main.update))end end,flush=function(self)local p=g(self.main.flush)if p==i then self.main.flush=f(function()self:runFlush()end)elseif p==j then d(h(self.main.flush))end end,action=function(self,q,r)if self.fns.action[q][r]then runFunction(self.fns.action[q][r],\"System Action \"..q..\" Error: \")end end,actionStart=function(self,r)self:action('start',r)end,actionStop=function(self,r)self:action('stop',r)end,actionLoop=function(self,r)self:action('loop',r)end,inputText=function(self,s)if self.fns.inputText then runFunction(self.fns.inputText,\"System Input Text Error: \",s)end end,runUpdate=function(self)for t,u in pairs(self.cos.update)do local m=g(u)if m==i then self.cos.update[t]=f(self.fns.update[t])elseif m==j then d(h(u))end end end,runFlush=function(self)for t,u in pairs(self.cos.flush)do local m=g(u)if m==i then self.cos.flush[t]=f(self.fns.flush[t])elseif m==j then d(h(u))end end end,onUpdate=function(self,v)for t,w in pairs(v)do self.fns.update[t]=w;self.cos.update[t]=f(w)end end,onFlush=function(self,v)for t,w in pairs(v)do self.fns.flush[t]=w;self.cos.flush[t]=f(w)end end,onAction=function(self,q,v)for t,w in pairs(v)do self.fns.action[q][t]=w end end,onActionStart=function(self,v)self:onAction(\"start\",v)end,onActionStop=function(self,v)self:onAction(\"stop\",v)end,onActionLoop=function(self,v)self:onAction(\"loop\",v)end,onInputText=function(self,k)self.fns.inputText=k end}}local x={__index={timers={},stopFn=function()end,timer=function(self,y)if self.timers[y]then runFunction(self.timers[y],\"Unit Timer \"..y..\" Error: \")end end,setTimer=function(self,y,z,k)self.timers[y]=k;unit.setTimer(y,z)end,stopTimer=function(self,y)unit.stopTimer(y)self.timers[y]=nil end,onStop=function(self,k)self.stopFn=k end,stop=function(self)if self.stopFn then runFunction(self.stopFn,\"Unit Stop Error: \")end end}}local A={__index={parentChangedFn=function(B,C)end,onParentChange=function(self,k)self.parentChangedFn=k end,parentChanged=function(self,B,C)if self.parentChangedFn then runFunction(self.parentChangedFn,\"Player Parent Changed Error: \",B,C)end end}}local D={__index={dockedFn=function(E)end,onDocked=function(self,k)self.dockedFn=k end,docked=function(self,E)if self.dockedFn then runFunction(self.dockedFn,\"Construct Docked Error: \",E)end end,undockedFn=function(E)end,onUndocked=function(self,k)self.undockedFn=k end,undocked=function(self,E)if self.undockedFn then runFunction(self.undockedFn,\"Construct Undocked Error: \",E)end end,playerBoardedFn=function(E)end,onPlayerBoarded=function(self,k)self.playerBoardedFn=k end,playerBoarded=function(self,E)if self.playerBoardedFn then runFunction(self.playerBoardedFn,\"Construct Player Boarded Error: \",E)end end,VRStationEnteredFn=function(E)end,onVRStationEntered=function(self,k)self.VRStationEnteredFn=k end,VRStationEntered=function(self,E)if self.VRStationEnteredFn then runFunction(self.VRStationEnteredFn,\"Construct VR Station Entered Error: \",E)end end,constructDockedFn=function(E)end,onConstructDocked=function(self,k)self.constructDockedFn=k end,constructDocked=function(self,E)if self.constructDockedFn then runFunction(self.constructDockedFn,\"Construct Construct Docked Error: \",E)end end,PvPTimerFn=function(F)end,onPvPTimer=function(self,k)self.PvPTimerFn=k end,PvPTimer=function(self,F)if self.PvPTimerFn then runFunction(self.PvPTimerFn,\"Construct PvP Timer Error: \",F)end end}}DU_Framework={__index={system=setmetatable({},o),unit=setmetatable({},x),player=setmetatable({},A),construct=setmetatable({},D)}}\n\nScript = {}\nsetmetatable(Script, DU_Framework)\n\n\n--[[\n You can declare here all the functions you want to call in the diffent events from system, unit, player and construct.\n Major changes compared to the base game concept:\n - 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.\n\n]]\n\n--Functions to load as coroutines that will be runned in system > onUpdate (based on FPS)\nlocal system_update = {}\nsystem_update.co1 = function ()\n for i=0, 10 do\n system.print(\"coroutine 1 --- update --- \"..i)\n coroutine.yield() -- pause the coroutine 1, it will wait till the next onUpdate event to be resumed\n end\nend\nsystem_update.co2 = function ()\n for i=0, 10 do\n system.print(\"coroutine 2 --- update --- \"..i)\n coroutine.yield() -- pause the coroutine 2, it will wait till the next onUpdate event to be resumed\n end\nend\n\n--Functions to load as coroutines that will be runned in system > onFlush (60 times / s)\nlocal system_flush = {}\nsystem_flush.co1 = function ()\n for i=0, 10 do\n system.print(\"coroutine 1 --- flush --- \"..i)\n coroutine.yield() -- pause the coroutine 1, it will wait till the next onFlush event to be resumed\n end\nend\nsystem_flush.co2 = function ()\n for i=0, 10 do\n system.print(\"coroutine 2 --- flush --- \"..i)\n coroutine.yield() -- pause the coroutine 2, it will wait till the next onFlush event to be resumed\n end\nend\n\n--Function to run on actions\nlocal system_action_start = {}\nsystem_action_start[Script.system.ACTIONS.BRAKE] = function()\n system.print(\"I'm braking\");\nend\nlocal system_action_stop = {}\nsystem_action_stop[Script.system.ACTIONS.BRAKE] = function()\n system.print(\"I stopped braking\");\nend\nlocal system_action_loop = {}\nsystem_action_loop[Script.system.ACTIONS.BRAKE] = function()\n system.print(\"I'm still braking\");\nend\n\n--Function to run when input text to the lua chat\nlocal system_inputText = function (text)\n system.print(\"Input: \" .. text)\nend\n\n--Function to run when the program is stopping\nlocal unit_onstop = function ()\n system.print(\"Program is stopping\")\nend\n\n--Function to run when the player change parent\nlocal player_onparentchanged = function (oldParent, newParent)\n system.print(\"Player changed parent from ID \"..oldParent..\" to ID \"..newParent)\nend\n\n--Function to run when the construct is docked or undocked\nlocal construct_onDocked = function(id)\n system.print(\"Construct docked on ID \"..id)\nend\nlocal construct_onUndocked = function(id)\n system.print(\"Construct undocked from ID \"..id)\nend\n\n--Function to run when a player board the construct\nlocal construct_onplayerboarded = function(id)\n system.print(\"Player with ID \" .. id .. \" boarded construct\")\nend\n\n--Function to run when a player enter the VR Station\nlocal construct_vrstationentered = function(id)\n system.print(\"Player with ID \" .. id .. \" entered VR Station\")\nend\n\n--Function to run when another construct is docked on this construct\nlocal construct_constructdocked = function(id)\n system.print(\"Construct with ID \" .. id .. \" docked on this construct\")\nend\n\n--Function to run when pvp timer is changing state\nlocal construct_onpvptimer = function(active)\n if active then\n system.print(\"PVP is now active\")\n else\n system.print(\"PVP is now inactive\")\n end\nend\n\n--[[\n Here how to load the functions in the framework\n]]\nScript.system:onUpdate(system_update) --loading coroutines for system > onUpdate\nScript.system:onFlush(system_flush) --loading coroutines for system > onFlush\n\nScript.system:onActionStart(system_action_start) --loading all \"actionStart\" functions\nScript.system:onActionStop(system_action_stop) --loading all \"actionStop\" functions\nScript.system:onActionLoop(system_action_loop) --loading all \"actionLoop\" functions\n\nScript.system:onInputText(system_inputText) --loading the function to trigger when input text in lua chat\n\nScript.unit:setTimer(\"hello\", 1, function()system.print(\"hello\")end) --add a timer displaying \"hello\" every seconds\nScript.unit:setTimer(\"hello5\", 5, function()system.print(\"hello 5\")end) --add a timer displaying \"hello 5\" every 5 seconds\nScript.unit:onStop(unit_onstop) --loading the function to trigger when the program is stopping\n\nScript.player:onParentChanged(player_onparentchanged) --loading the function to trigger when the player change parent\n\nScript.construct:onDocked(construct_onDocked) --loading the function to trigger when the construct is docked\nScript.construct:onUndocked(construct_onUndocked) --loading the function to trigger when the construct is undocked\nScript.construct:onPlayerBoarded(construct_onplayerboarded) --loading the function to trigger when a player board the construct\nScript.construct:onVRStationEntered(construct_vrstationentered) --loading the function to trigger when a player enter the VR Station\nScript.construct:onConstructDocked(construct_constructdocked) --loading the function to trigger when another construct is docked on this construct\nScript.construct:onPvPTimer(construct_onpvptimer) --loading the function to trigger when pvp timer is changing state","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"0"}],"methods":[],"slots":{"0":{"name":"slot1","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}}}} \ No newline at end of file diff --git a/source/player/onParentChanged.lua b/source/player/onParentChanged.lua new file mode 100644 index 0000000..479e8f4 --- /dev/null +++ b/source/player/onParentChanged.lua @@ -0,0 +1 @@ +Script.player:parentChanged(oldId, newId) \ No newline at end of file diff --git a/source/system/onActionLoop.lua b/source/system/onActionLoop.lua new file mode 100644 index 0000000..09d6fc2 --- /dev/null +++ b/source/system/onActionLoop.lua @@ -0,0 +1 @@ +Script.system:actionLoop(action) \ No newline at end of file diff --git a/source/system/onActionStart.lua b/source/system/onActionStart.lua new file mode 100644 index 0000000..be355e2 --- /dev/null +++ b/source/system/onActionStart.lua @@ -0,0 +1 @@ +Script.system:actionStart(action) \ No newline at end of file diff --git a/source/system/onActionStop.lua b/source/system/onActionStop.lua new file mode 100644 index 0000000..2e3522f --- /dev/null +++ b/source/system/onActionStop.lua @@ -0,0 +1 @@ +Script.system:actionStop(action) \ No newline at end of file diff --git a/source/system/onFlush.lua b/source/system/onFlush.lua new file mode 100644 index 0000000..da8b76b --- /dev/null +++ b/source/system/onFlush.lua @@ -0,0 +1 @@ +Script.system:flush() \ No newline at end of file diff --git a/source/system/onInputText.lua b/source/system/onInputText.lua new file mode 100644 index 0000000..fc140c1 --- /dev/null +++ b/source/system/onInputText.lua @@ -0,0 +1 @@ +Script.system:inputText(text) \ No newline at end of file diff --git a/source/system/onUpdate.lua b/source/system/onUpdate.lua new file mode 100644 index 0000000..ca28d61 --- /dev/null +++ b/source/system/onUpdate.lua @@ -0,0 +1 @@ +Script.system:update() \ No newline at end of file diff --git a/source/unit/onStart.lua b/source/unit/onStart.lua new file mode 100644 index 0000000..e3d2904 --- /dev/null +++ b/source/unit/onStart.lua @@ -0,0 +1,408 @@ +--[[ + 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.print +local n_error = error +local n_pcall = pcall +local n_assert = assert +local cor = coroutine +local co_create = cor.create +local co_status = cor.status +local co_resume = cor.resume +local 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) + end +end + +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 + end +end +system_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 + end +end + +--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 + end +end +system_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 + end +end + +--Function to run on actions +local system_action_start = {} +system_action_start[Script.system.ACTIONS.BRAKE] = function() + system.print("I'm braking"); +end +local system_action_stop = {} +system_action_stop[Script.system.ACTIONS.BRAKE] = function() + system.print("I stopped braking"); +end +local 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 chat +local system_inputText = function (text) + system.print("Input: " .. text) +end + +--Function to run when the program is stopping +local unit_onstop = function () + system.print("Program is stopping") +end + +--Function to run when the player change parent +local 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 undocked +local construct_onDocked = function(id) + system.print("Construct docked on ID "..id) +end +local construct_onUndocked = function(id) + system.print("Construct undocked from ID "..id) +end + +--Function to run when a player board the construct +local construct_onplayerboarded = function(id) + system.print("Player with ID " .. id .. " boarded construct") +end + +--Function to run when a player enter the VR Station +local construct_vrstationentered = function(id) + system.print("Player with ID " .. id .. " entered VR Station") +end + +--Function to run when another construct is docked on this construct +local construct_constructdocked = function(id) + system.print("Construct with ID " .. id .. " docked on this construct") +end + +--Function to run when pvp timer is changing state +local construct_onpvptimer = function(active) + if active then + system.print("PVP is now active") + else + system.print("PVP is now inactive") + end +end + +--[[ + Here how to load the functions in the framework +]] +Script.system:onUpdate(system_update) --loading coroutines for system > onUpdate +Script.system:onFlush(system_flush) --loading coroutines for system > onFlush + +Script.system:onActionStart(system_action_start) --loading all "actionStart" functions +Script.system:onActionStop(system_action_stop) --loading all "actionStop" functions +Script.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 seconds +Script.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 docked +Script.construct:onUndocked(construct_onUndocked) --loading the function to trigger when the construct is undocked +Script.construct:onPlayerBoarded(construct_onplayerboarded) --loading the function to trigger when a player board the construct +Script.construct:onVRStationEntered(construct_vrstationentered) --loading the function to trigger when a player enter the VR Station +Script.construct:onConstructDocked(construct_constructdocked) --loading the function to trigger when another construct is docked on this construct +Script.construct:onPvPTimer(construct_onpvptimer) --loading the function to trigger when pvp timer is changing state \ No newline at end of file diff --git a/source/unit/onStop.lua b/source/unit/onStop.lua new file mode 100644 index 0000000..e5ab2df --- /dev/null +++ b/source/unit/onStop.lua @@ -0,0 +1 @@ +Script.unit:stop() \ No newline at end of file diff --git a/source/unit/onTimer.lua b/source/unit/onTimer.lua new file mode 100644 index 0000000..e22cc5b --- /dev/null +++ b/source/unit/onTimer.lua @@ -0,0 +1 @@ +Script.unit:timer(tag) \ No newline at end of file