mirror of
https://github.com/rayaman/multi.git
synced 2026-09-04 23:17:35 -04:00
Fixed System Threaded Queues!
Faster Added older versions Other bug fixes More examples
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
IsEvent=true
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
setAlarm=function(self,tag,set) event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]]) event:addTracker("_Alarm_"..tag,SetCounter()) end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end event:new("Event_"..fname,condition,also) end,
|
||||
removeAlarm=function(self,tag) event:destroy("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,var) return event.EventTracker[var] end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroy=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do for d=1,#event.Events do if event.Active==true then loadstring(event.Events[d])() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end event.OnClose() end,
|
||||
CManager=function() for d=1,#event.Events do if event.Active==true then loadstring(event.Events[d])() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end for d=1,#event.Events do if event.Active==true then loadstring(event.Events[d])() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
createStep=function(self,tag,reset,endc)
|
||||
if not(endc) then endc=false end
|
||||
if not(reset) then reset=0 end
|
||||
temp={Name=tag,pos=1,endAt=reset,active=true,endc=endc,
|
||||
Step=function(self) if self~=nil then _G.__CStep__=self if self.active==true then loadstring("Step_"..tag.."("..self.pos..",__CStep__)")() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then loadstring("Step_"..self.Name.."_End(__CStep__)")() end end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
IsEvent=true
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
--addAlarm=function(self,tag) end,
|
||||
setAlarm=function(self,tag,set) event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]]) event:addTracker("_Alarm_"..tag,SetCounter()) assert(loadstring("if Alarm_"..tag.."==nil then function Alarm_"..tag.."() print('No function \"Alarm_"..tag.."()\" exists make sure you created it') end end"))() end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end a=string.find(fname,"(",1,true) tempstr=string.sub(fname,1,a-1) event:new("Event_"..fname,condition,also) assert(loadstring("if Event_"..tempstr.."==nil then function Event_"..tempstr.."() print('No function \"Event_"..tempstr.."()\" exists make sure you created it') end end"))() end,
|
||||
removeAlarm=function(self,tag) event:destroy("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,var) return event.EventTracker[var] end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroy=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end event.OnClose() end,
|
||||
CManager=function() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
RManager=function() event.OnCreate() while event.Active==true do event.OnUpdate() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end end event.OnClose() end,
|
||||
createStep=function(self,tag,reset,endc)
|
||||
if not(endc) then endc=false end
|
||||
if not(reset) then reset=0 end
|
||||
temp={Name=tag,pos=1,endAt=reset,active=true,endc=endc,
|
||||
Step=function(self) if self~=nil then _G.__CStep__=self if self.active==true then assert(loadstring("Step_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CStep__)"))() end end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
IsEvent=true
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
--addAlarm=function(self,tag) end,
|
||||
setAlarm=function(self,tag,set) event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]]) event:addTracker("_Alarm_"..tag,SetCounter()) assert(loadstring("if Alarm_"..tag.."==nil then function Alarm_"..tag.."() print('No function \"Alarm_"..tag.."()\" exists make sure you created it') end end"))() end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end a=string.find(fname,"(",1,true) tempstr=string.sub(fname,1,a-1) event:new("Event_"..fname,condition,also) assert(loadstring("if Event_"..tempstr.."==nil then function Event_"..tempstr.."() print('No function \"Event_"..tempstr.."()\" exists make sure you created it') end end"))() end,
|
||||
removeAlarm=function(self,tag) event:destroy("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,var) return event.EventTracker[var] end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroy=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end event.OnClose() end,
|
||||
CManager=function() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
RManager=function() event.OnCreate() while event.Active==true do event.OnUpdate() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end end event.OnClose() end,
|
||||
createStep=function(self,tag,reset,endc)
|
||||
if not(endc) then endc=false end
|
||||
if not(reset) then reset=0 end
|
||||
temp={Name=tag,pos=1,endAt=reset,active=true,endc=endc,
|
||||
Step=function(self) if self~=nil then _G.__CStep__=self if self.active==true then assert(loadstring("Step_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CStep__)"))() end end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
require("Data/BasicCommands")
|
||||
IsEvent=true
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
TSteps={},
|
||||
LoadOrder="/E/S/U",-- types = ESU,EUS,USE,UES,SEU,SUE
|
||||
setLoadOrder=function(self,str) event.LoadOrder=string.upper(str) end,
|
||||
DO_Order=function() LoadOrder=event.LoadOrder LoadOrder=LoadOrder:gsub("/E", "event.RUN_EVENTS(); ") LoadOrder=LoadOrder:gsub("/S", "event.RUN_STEPS(); ") LoadOrder=LoadOrder:gsub("/U", "event.RUN_UPDATES(); ") assert(loadstring(LoadOrder))() end,
|
||||
RUN_EVENTS=function() for d=1,#event.Events do assert(loadstring(event.Events[d]))() end end,
|
||||
RUN_STEPS=function() for s_g=1,#event.Steps do event.Steps[s_g]:Step() end end,
|
||||
RUN_UPDATES=function() event.OnUpdate() end,
|
||||
setAlarm=function(self,tag,set) event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]]) event:addTracker("_Alarm_"..tag,SetCounter()) assert(loadstring("if Alarm_"..tag.."==nil then function Alarm_"..tag.."() print('No function \"Alarm_"..tag.."()\" exists make sure you created it') end end"))() end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end a=string.find(fname,"(",1,true) tempstr=string.sub(fname,1,a-1) event:new("Event_"..fname,condition,also) assert(loadstring("if Event_"..tempstr.."==nil then function Event_"..tempstr.."() print('No function \"Event_"..tempstr.."()\" exists make sure you created it') end end"))() end,
|
||||
removeAlarm=function(self,tag) event:destroy("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,varname) return event.EventTracker[varname] end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroy=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do event.DO_Order() end event.OnClose() end,
|
||||
--Manager=function() event.OnCreate() while event.Active==true do for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end event.OnClose() end,
|
||||
CManager=function() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
createStep=function(self,tag,reset,skip,endc)
|
||||
if not(endc) then endc=false end
|
||||
temp=
|
||||
{
|
||||
Name=tag,
|
||||
pos=1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
endc=endc,
|
||||
skip=skip or 0,
|
||||
spos=0,
|
||||
Step=function(self) if self~=nil then if self.spos==0 then _G.__CStep__=self if self.active==true then assert(loadstring("Step_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CStep__)"))() end end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
FStep=function(self) if self~=nil then if self.spos==0 then _G.__CStep__=self assert(loadstring("Step_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CStep__)"))() end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp end,
|
||||
createTStep=function(self,tag,reset,timer,endc)
|
||||
if not(endc) then endc=false end
|
||||
timer=timer or 1
|
||||
temp=
|
||||
{
|
||||
Name=tag,
|
||||
pos=1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
endc=endc,
|
||||
skip= 0,
|
||||
spos=0,
|
||||
Step=function(self) if self~=nil then if self.spos==0 then _G.__CStep__=self if self.active==true then assert(loadstring("TStep_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("TStep_"..self.Name.."_End(__CStep__)"))() end end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
FStep=function(self) if self~=nil then if self.spos==0 then _G.__CStep__=self assert(loadstring("TStep_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("TStep_"..self.Name.."_End(__CStep__)"))() end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
event:setAlarm("TStep_"..tag,timer)
|
||||
event:addTracker("_TStep_"..tag,temp)
|
||||
table.insert(event.TSteps,temp)
|
||||
assert(loadstring("function Alarm_TStep_"..tag.."(alarm) event:getTracker(\"_\"..alarm):Step() event:setAlarm(alarm,"..timer..") end"))()
|
||||
return temp end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
function dump(t,indent)
|
||||
local names = {}
|
||||
if not indent then indent = "" end
|
||||
for n,g in pairs(t) do
|
||||
table.insert(names,n)
|
||||
end
|
||||
table.sort(names)
|
||||
for i,n in pairs(names) do
|
||||
local v = t[n]
|
||||
if type(v) == "table" then
|
||||
if(v==t) then -- prevent endless loop if table contains reference to itself
|
||||
print(indent..tostring(n)..": <-")
|
||||
else
|
||||
print(indent..tostring(n)..":")
|
||||
dump(v,indent.." ")
|
||||
end
|
||||
else
|
||||
if type(v) == "function" then
|
||||
print(indent..tostring(n).."()")
|
||||
else
|
||||
print(indent..tostring(n)..": "..tostring(v))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function SetCounter()
|
||||
return os.clock()
|
||||
end
|
||||
----------------------------------------------------------------------------------------------------
|
||||
function GetCounter(count)
|
||||
if count~=nil then
|
||||
return os.clock()-count
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end
|
||||
----------------------------------------------------------------------------------------------------
|
||||
clock=os.clock
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
Alarms={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
TSteps={},
|
||||
CTask="",
|
||||
LoadOrder="/E/S/A/U",
|
||||
UpdateObj=
|
||||
{
|
||||
LoadOrder="",
|
||||
Resume=function(self) event.LoadOrder=self.LoadOrder end,
|
||||
Pause=function(self) self.LoadOrder=event.LoadOrder event.LoadOrder=event.LoadOrder:gsub("/U", "") end,
|
||||
},
|
||||
setLoadOrder=function(self,str) event.LoadOrder=string.upper(str) end,
|
||||
DO_Order=function() LoadOrder=event.LoadOrder LoadOrder=LoadOrder:gsub("/A", "event.RUN_ALARMS(); ") LoadOrder=LoadOrder:gsub("/E", "event.RUN_EVENTS(); ") LoadOrder=LoadOrder:gsub("/S", "event.RUN_STEPS(); ") LoadOrder=LoadOrder:gsub("/U", "event.RUN_UPDATES(); ") assert(loadstring(LoadOrder))() end,
|
||||
RUN_EVENTS=function() event.CTask="events" for d=1,#event.Events do assert(loadstring(event.Events[d]))() end end,
|
||||
RUN_STEPS=function() event.CTask="steps" for s_g=1,#event.Steps do event.Steps[s_g]:Step() end end,
|
||||
RUN_UPDATES=function() _G.__CAction__=event.UpdateObj event.CTask="updates" event.OnUpdate() end,
|
||||
RUN_ALARMS=function() for i=1,#event.Alarms do event.Alarms[i]:Tick() end end,
|
||||
--System Used Functions
|
||||
Hold=function(self,task) -- as many conditions and times that you want can be used
|
||||
local action=__CAction__
|
||||
action:Pause()
|
||||
if type(task)=="number" then
|
||||
local func=function() end
|
||||
local alarm=event:newAlarm(task,func,true)
|
||||
while alarm.active==true do
|
||||
event.CManager()
|
||||
end
|
||||
alarm:Destroy()
|
||||
action:Resume()
|
||||
elseif type(task)=="string" then
|
||||
assert(loadstring("while not("..task..") do event.CManager() end"))()
|
||||
action:Resume()
|
||||
end
|
||||
end,
|
||||
newAlarm=function(self,set,func,start)
|
||||
if not(start) then timer=0 active=false else timer=clock() active=true end
|
||||
if not(func) then func=(function() end) end
|
||||
Alarm=
|
||||
{
|
||||
active=active,
|
||||
timer=timer,
|
||||
set=set or 0,
|
||||
func=func,
|
||||
Tick=function(self) _G.__CAction__=self if self.active==true then if clock()-self.timer>=self.set then self:Pause() self:Ring() end end end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Set=function(self,amt) self.set=amt self.timer=clock() self:Resume() end,
|
||||
OnRing=function(self,func) self.func=func end,
|
||||
Ring=function(self) self:func(self) end,
|
||||
Reset=function(self) self.timer=clock() self:Resume() end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Destroy=function(self) for i=1,#event.Alarms do if tostring(event.Alarms[i])==tostring(self) then table.remove(event.Alarms,i) end end end,
|
||||
}
|
||||
table.insert(event.Alarms,Alarm)
|
||||
return Alarm
|
||||
end,
|
||||
setAlarm=function(self,tag,set)
|
||||
if event:eventExist("Alarm_"..tag.."(\""..tag.."\")")==false then
|
||||
event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]])
|
||||
event:addTracker("_Alarm_"..tag,SetCounter())
|
||||
assert(loadstring("if Alarm_"..tag.."==nil then function Alarm_"..tag.."() print('No function \"Alarm_"..tag.."()\" exists make sure you created it') end end"))()
|
||||
else
|
||||
event:addTracker("_Alarm_"..tag,SetCounter())
|
||||
end
|
||||
end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end a=string.find(fname,"(",1,true) tempstr=string.sub(fname,1,a-1) event:new("Event_"..fname,condition,also) assert(loadstring("if Event_"..tempstr.."==nil then function Event_"..tempstr.."() print('No function \"Event_"..tempstr.."()\" exists make sure you created it') end end"))() end,
|
||||
removeAlarm=function(self,tag) event:destroyEvent("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,varname) return event.EventTracker[varname] end,
|
||||
listTrackers=function(self) return event.EventTracker end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroyEvent=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do event.DO_Order() end event.OnClose() end,
|
||||
CManager=function() if event.Active==true then event.DO_Order() end end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end if event.Active==true then event.DO_Order() end end,
|
||||
createStep=function(self,tag,reset,skip,endc)
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
temp=
|
||||
{
|
||||
Name=tag,
|
||||
pos=1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
endc=endc,
|
||||
skip=skip or 0,
|
||||
spos=0,
|
||||
Step=function(self) if self~=nil then if self.spos==0 then _G.__CAction__=self if self.active==true then assert(loadstring("Step_"..tag.."("..self.pos..",__CAction__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CAction__)"))() end end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
FStep=function(self) if self~=nil then if self.spos==0 then _G.__CAction__=self assert(loadstring("Step_"..tag.."("..self.pos..",__CAction__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CAction__)"))() end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) _G.__CAction__=self assert(loadstring("Step_"..self.Name.."_End(__CAction__)"))() self:Reset() end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp
|
||||
end,
|
||||
createTStep=function(self,tag,reset,timer,endc)
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
timer=timer or 1
|
||||
temp=
|
||||
{
|
||||
Name=tag,
|
||||
pos=1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
endc=endc,
|
||||
skip= 0,
|
||||
spos=0,
|
||||
Step=function(self) if self~=nil then if self.spos==0 then _G.__CAction__=self if self.active==true then assert(loadstring("TStep_"..tag.."("..self.pos..",__CAction__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("TStep_"..self.Name.."_End(__CAction__)"))() end end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
FStep=function(self) if self~=nil then if self.spos==0 then _G.__CAction__=self assert(loadstring("TStep_"..tag.."("..self.pos..",__CAction__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("TStep_"..self.Name.."_End(__CAction__)"))() end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
event:setAlarm("TStep_"..tag,timer)
|
||||
event:addTracker("_TStep_"..tag,temp)
|
||||
table.insert(event.TSteps,temp)
|
||||
assert(loadstring("function Alarm_TStep_"..tag.."(alarm) event:getTracker(\"_\"..alarm):Step() event:updateAlarm(alarm,"..timer..") end"))()
|
||||
return temp
|
||||
end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
tstepExist=function(self,tag)
|
||||
for a_s=1,#event.TSteps do
|
||||
if event.TSteps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
@@ -0,0 +1,728 @@
|
||||
function readonlytable(table)
|
||||
return setmetatable({}, {
|
||||
__index = table,
|
||||
__newindex = function(table, key, value)
|
||||
error("Attempt to modify read-only table")
|
||||
end,
|
||||
__metatable = false
|
||||
});
|
||||
end
|
||||
local EventRef=
|
||||
readonlytable{
|
||||
Pause=function(self)
|
||||
self.active=false
|
||||
if not(event.isPaused(self)) then
|
||||
table.insert(event.Paused,self)
|
||||
for _j=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[_j])==tostring(self) then
|
||||
table.remove(event.Mainloop,_j)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
Resume=function(self)
|
||||
self.active=true
|
||||
if event.isPaused(self) then
|
||||
table.insert(event.Mainloop,self)
|
||||
for _j=1,#event.Paused do
|
||||
if tostring(event.Paused[_j])==tostring(self) then
|
||||
table.remove(event.Paused,_j)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
Stop=function(self)
|
||||
self.active=nil
|
||||
end,
|
||||
}
|
||||
|
||||
local StepRef=
|
||||
readonlytable{
|
||||
Step=function(self)
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
_G.__CAction__=self
|
||||
if self.active==true then
|
||||
for i=1,#self.steps do
|
||||
self.steps[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
end
|
||||
if self.endAt+self.count<=self.pos and self.endAt>self.start then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
elseif self.pos<=0 then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end,
|
||||
FStep=function(self)
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
_G.__CAction__=self
|
||||
for i=1,#self.steps do
|
||||
self.steps[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
if self.endAt+self.count<=self.pos and self.endAt>self.start then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
elseif self.pos==0 then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for as=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[as])==tostring(self) then
|
||||
table.remove(event.Mainloop,as)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
Reset=function(self)
|
||||
self.pos=self.start
|
||||
end,
|
||||
Set=function(self,amt)
|
||||
self.pos=amt
|
||||
end,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
Stop=function(self)
|
||||
self:Reset()
|
||||
self.active=nil
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end,
|
||||
End=function(self)
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
self:Reset()
|
||||
end,
|
||||
Update=function(self,start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end,
|
||||
OnEnd=function(self,func)
|
||||
table.insert(self.funcs,func)
|
||||
end,
|
||||
OnStep=function(self,func)
|
||||
table.insert(self.steps,func)
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.funcs={}
|
||||
self.steps={}
|
||||
end,
|
||||
}
|
||||
--thread and run setup
|
||||
if love then
|
||||
function love.run()
|
||||
if love.math then
|
||||
love.math.setRandomSeed(os.time())
|
||||
end
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
end
|
||||
if love.load then love.load(arg) end
|
||||
if love.timer then love.timer.step() end
|
||||
local dt = 0
|
||||
while true do
|
||||
-- Process events.
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
for e,a,b,c,d in love.event.poll() do
|
||||
if e == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
if love.audio then
|
||||
love.audio.stop()
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
love.handlers[e](a,b,c,d)
|
||||
end
|
||||
end
|
||||
if love.timer then
|
||||
love.timer.step()
|
||||
dt = love.timer.getDelta()
|
||||
end
|
||||
if love.update then love.update(dt) end
|
||||
event.uManager(dt)
|
||||
if love.window and love.graphics and love.window.isCreated() then
|
||||
love.graphics.clear()
|
||||
love.graphics.origin()
|
||||
if love.draw then love.draw() end
|
||||
event.dManager()
|
||||
love.graphics.setColor(255,255,255,255)
|
||||
if event.draw then event.draw() end
|
||||
love.graphics.present()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
eThreads={
|
||||
send=function() end,
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
function RunTasks()
|
||||
for i=1,#event.DoTasks do
|
||||
event.DoTasks[i]()
|
||||
end
|
||||
end
|
||||
event={
|
||||
VERSION="1.0.0 (Build Version: 5.7.3)",
|
||||
Priority_Core=1,
|
||||
Priority_High=2,
|
||||
Priority_Above_Normal=4,
|
||||
Priority_Normal=16,
|
||||
Priority_Low=256,
|
||||
Priority_Idle=65536,
|
||||
Start=0,
|
||||
Active=true,
|
||||
CT=false,
|
||||
Tasks={},
|
||||
DoTasks={},
|
||||
garbage={},
|
||||
Paused={},
|
||||
last={},
|
||||
func={},
|
||||
drawF={},
|
||||
pump=false,
|
||||
pumpvar=0,
|
||||
Mainloop={},
|
||||
PEnabled=true,
|
||||
PCount=1,
|
||||
Triggers={},
|
||||
oneTimeObj=
|
||||
{
|
||||
last={},
|
||||
Resume=function(self) end,
|
||||
Pause=function(self) end,
|
||||
},
|
||||
RemoveAll=function()
|
||||
event.Mainloop={}
|
||||
end,
|
||||
GarbageObj=
|
||||
{
|
||||
Resume=function() end,
|
||||
Pause=function() end
|
||||
},
|
||||
isPaused=function(obj)
|
||||
for _j=1,#event.Paused do
|
||||
if tostring(event.Paused[_j])==tostring(obj) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
DO_Order=function()
|
||||
event.oneTime(RunTasks)
|
||||
event.PCount=event.PCount+1
|
||||
for i=1,#event.Mainloop do
|
||||
if event.Mainloop[i]~=nil then
|
||||
local obj = event.Mainloop[i]
|
||||
if event.PCount%obj.Priority==0 and event.PEnabled then
|
||||
obj:Act()
|
||||
elseif event.PEnabled==false then
|
||||
obj:Act()
|
||||
end
|
||||
if event.PCount>event.Priority_Idle then
|
||||
event.PCount=event.Priority_Core
|
||||
end
|
||||
end
|
||||
end
|
||||
event.MANAGE_GARBAGE()
|
||||
end,
|
||||
MANAGE_GARBAGE=function()
|
||||
_G.__CAction__=event.GarbageObj
|
||||
for _i=1,#event.garbage do
|
||||
event.garbage[_i]:Remove()
|
||||
table.remove(event.garbage,_i)
|
||||
end
|
||||
end,
|
||||
oneTime=function(func)
|
||||
event.oneTimeObj.last=_G.__CAction__
|
||||
_G.__CAction__=event.oneTimeObj
|
||||
for _k=1,#event.Tasks do
|
||||
if event.Tasks[_k]==func then
|
||||
_G.__CAction__=event.oneTimeObj.last
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(event.Tasks,func)
|
||||
func()
|
||||
_G.__CAction__=event.oneTimeObj.last
|
||||
return true
|
||||
end,
|
||||
oneETime=function(func)
|
||||
for _k=1,#event.Tasks do
|
||||
if event.Tasks[_k]==string.dump(func) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(event.Tasks,string.dump(func))
|
||||
func()
|
||||
return true
|
||||
end,
|
||||
hold=function(task) -- as many conditions and times that you want can be used
|
||||
local action=__CAction__
|
||||
action:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=event.newAlarm(task,function() end,true)
|
||||
while alarm.active==true do
|
||||
if love then
|
||||
event.lManager()
|
||||
else
|
||||
event.cManager()
|
||||
end
|
||||
end
|
||||
alarm:Remove()
|
||||
action:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=event.newEvent(task,function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.active do
|
||||
if love then
|
||||
event.lManager()
|
||||
else
|
||||
event.cManager()
|
||||
end
|
||||
end
|
||||
env:Remove()
|
||||
action:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end,
|
||||
waitFor=function(obj)
|
||||
local obj=obj
|
||||
event.hold(function() return not(obj.active) end)
|
||||
end,
|
||||
getType=function(obj)
|
||||
if obj.Type~=nil then
|
||||
return obj.Type
|
||||
end
|
||||
end,
|
||||
newEvent=function(test,task)
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Events,
|
||||
Type="Event",
|
||||
active=true,
|
||||
test=test or (function() end),
|
||||
task={task} or {},
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
if self:test(self)==true then
|
||||
self:Pause()
|
||||
self:Stop()
|
||||
for i=1,#self.task do
|
||||
self.task[i](self)
|
||||
end
|
||||
end
|
||||
end,
|
||||
Reset=function(self) self:Resume() end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
OnEvent=function(self,func)
|
||||
table.insert(self.task,func)
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.task={}
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
newAlarm=function(set,func,start)
|
||||
if not(start) then
|
||||
timer=0
|
||||
active=false
|
||||
else
|
||||
timer=os.clock()
|
||||
active=true
|
||||
end
|
||||
if not(func) then
|
||||
func=(function() end)
|
||||
end
|
||||
Alarm=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Alarms,
|
||||
Type="Alarm",
|
||||
active=active,
|
||||
timer=timer,
|
||||
set=set or 0,
|
||||
func={func},
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
if self.active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
self:Stop()
|
||||
self:Ring()
|
||||
end
|
||||
end
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.func={}
|
||||
end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Set=function(self,amt)
|
||||
self.set=amt
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end,
|
||||
OnRing=function(self,func)
|
||||
table.insert(self.func,func)
|
||||
end,
|
||||
Ring=function(self)
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end,
|
||||
Reset=function(self)
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end,
|
||||
Resume=EventRef.Resume,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,Alarm)
|
||||
event.last=temp
|
||||
return Alarm
|
||||
end,
|
||||
newTask=function(func)
|
||||
table.insert(event.DoTasks,func)
|
||||
end,
|
||||
createLoop=function()
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Loops,
|
||||
Type="Loop",
|
||||
active=true,
|
||||
func={},
|
||||
OnLoop=function(self,func)
|
||||
table.insert(self.func,func)
|
||||
end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-event.Start,self)
|
||||
end
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.func={}
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createStep=function(start,reset,count,skip)
|
||||
think=1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
start=start or 1,
|
||||
Parent=event.Steps,
|
||||
Type="Step",
|
||||
pos=start or 1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
skip=skip or 0,
|
||||
spos=0,
|
||||
count=count or 1*think,
|
||||
funcs={},
|
||||
steps={},
|
||||
Act=StepRef.Step,
|
||||
FAct=StepRef.FStep,
|
||||
Remove=StepRef.Remove,
|
||||
Reset=StepRef.Reset,
|
||||
Set=StepRef.Set,
|
||||
Pause=StepRef.Pause,
|
||||
Resume=StepRef.Resume,
|
||||
Stop=StepRef.Stop,
|
||||
End=StepRef.End,
|
||||
Update=StepRef.Update,
|
||||
OnEnd=StepRef.OnEnd,
|
||||
OnStep=StepRef.OnStep,
|
||||
FreeConnections=StepRef.FreeConnections
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createTStep=function(start,reset,timer,count)
|
||||
think=1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
timer=timer or 1
|
||||
local _alarm=event.newAlarm(timer,function(alarm) alarm.Link:Act() alarm:Reset() end,true)
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
start=start or 1,
|
||||
Parent=event.TSteps,
|
||||
Type="TStep",
|
||||
pos=start or 1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
skip= 0,
|
||||
spos=0,
|
||||
count=count or 1*think,
|
||||
funcs={},
|
||||
steps={},
|
||||
alarm=_alarm,
|
||||
Act=StepRef.Step,
|
||||
FAct=StepRef.FStep,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for as=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[as])==tostring(self) then
|
||||
table.remove(event.Mainloop,as)
|
||||
end
|
||||
end
|
||||
self.alarm:Remove()
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
Reset=StepRef.Reset,
|
||||
Set=StepRef.Set,
|
||||
Pause=StepRef.Pause,
|
||||
Resume=StepRef.Resume,
|
||||
Stop=StepRef.Stop,
|
||||
End=StepRef.End,
|
||||
Update=function(self,start,reset,timer,count)
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
if not(count<0) then
|
||||
print("less")
|
||||
count=-count
|
||||
end
|
||||
end
|
||||
end
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
if timer~=nil then
|
||||
self.alarm:Set(timer)
|
||||
end
|
||||
self.count=count or self.count
|
||||
self.pos=self.start
|
||||
self:Resume()
|
||||
end,
|
||||
OnEnd=StepRef.OnEnd,
|
||||
OnStep=StepRef.OnStep,
|
||||
FreeConnections=StepRef.FreeConnections
|
||||
}
|
||||
_alarm.Link=temp
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createTrigger=function(func)
|
||||
temp={
|
||||
active=true,
|
||||
trigfunc=func,
|
||||
Remove=function(self)
|
||||
for i=1,#event.Triggers do
|
||||
if event.Triggers[i]==self then
|
||||
table.remove(event.Triggers,i)
|
||||
end
|
||||
end
|
||||
end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Fire=function(self,...)
|
||||
if self.active==true then
|
||||
local tempA=__CAction__
|
||||
__CAction__=self
|
||||
self:trigfunc(...)
|
||||
__CAction__=tempA
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Triggers,temp)
|
||||
return temp
|
||||
end,
|
||||
stop=function()
|
||||
event.Active=false
|
||||
end,
|
||||
onStart=function() end,
|
||||
onUpdate=function(func)
|
||||
local temp=event.createLoop()
|
||||
temp:OnLoop(func)
|
||||
temp.Priority=1
|
||||
end,
|
||||
onDraw=function(func)
|
||||
table.insert(event.drawF,func)
|
||||
end,
|
||||
onClose=function() end,
|
||||
manager=function()
|
||||
if not(love) then
|
||||
event.onStart()
|
||||
event.Start=os.clock()
|
||||
while event.Active==true do
|
||||
event.DO_Order()
|
||||
end
|
||||
event.onClose()
|
||||
return os.clock()-event.Start
|
||||
else
|
||||
return false
|
||||
end
|
||||
end,
|
||||
cManager=function()
|
||||
if event.Active==true then
|
||||
event.DO_Order()
|
||||
end
|
||||
end,
|
||||
uManager=function(dt)
|
||||
if event.CT==false then
|
||||
if dt then
|
||||
event.pump=true
|
||||
end
|
||||
event.CT=true
|
||||
event.onStart()
|
||||
event.Start=os.clock()
|
||||
end
|
||||
event.pumpvar=dt
|
||||
if event.Active==true then
|
||||
event.DO_Order()
|
||||
end
|
||||
end,
|
||||
dManager=function()
|
||||
for ii=1,#event.drawF do
|
||||
event.drawF[ii]()
|
||||
end
|
||||
end,
|
||||
lManager=function()
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
for e,a,b,c,d in love.event.poll() do
|
||||
if e == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
if love.audio then
|
||||
love.audio.stop()
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
love.handlers[e](a,b,c,d)
|
||||
end
|
||||
end
|
||||
if love.timer then
|
||||
love.timer.step()
|
||||
dt = love.timer.getDelta()
|
||||
end
|
||||
if love.update then love.update(dt) end
|
||||
event.uManager(dt)
|
||||
if love.window and love.graphics and love.window.isCreated() then
|
||||
love.graphics.clear()
|
||||
love.graphics.origin()
|
||||
if love.draw then love.draw() end
|
||||
event.dManager()
|
||||
love.graphics.present()
|
||||
end
|
||||
end,
|
||||
benchMark=function(sec,p)
|
||||
p=p or event.Priority_Normal
|
||||
local temp=event.createStep(10)
|
||||
temp.CC=0
|
||||
temp:OnStep(function(pos,step) step.CC=step.CC+1 end)
|
||||
local Loud=event.newAlarm(sec,nil,true)
|
||||
Loud.Link=temp
|
||||
Loud:OnRing(function(alarm) print((alarm.Link.CC).." steps in "..alarm.set.." second(s)") end)
|
||||
temp.Priority=p
|
||||
Loud.Priority=p
|
||||
return Loud
|
||||
end,
|
||||
}
|
||||
@@ -0,0 +1,728 @@
|
||||
function readonlytable(table)
|
||||
return setmetatable({}, {
|
||||
__index = table,
|
||||
__newindex = function(table, key, value)
|
||||
error("Attempt to modify read-only table")
|
||||
end,
|
||||
__metatable = false
|
||||
});
|
||||
end
|
||||
local EventRef=
|
||||
readonlytable{
|
||||
Pause=function(self)
|
||||
self.active=false
|
||||
if not(event.isPaused(self)) then
|
||||
table.insert(event.Paused,self)
|
||||
for _j=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[_j])==tostring(self) then
|
||||
table.remove(event.Mainloop,_j)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
Resume=function(self)
|
||||
self.active=true
|
||||
if event.isPaused(self) then
|
||||
table.insert(event.Mainloop,self)
|
||||
for _j=1,#event.Paused do
|
||||
if tostring(event.Paused[_j])==tostring(self) then
|
||||
table.remove(event.Paused,_j)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
Stop=function(self)
|
||||
self.active=nil
|
||||
end,
|
||||
}
|
||||
|
||||
local StepRef=
|
||||
readonlytable{
|
||||
Step=function(self)
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
_G.__CAction__=self
|
||||
if self.active==true then
|
||||
for i=1,#self.steps do
|
||||
self.steps[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
end
|
||||
if self.endAt+self.count<=self.pos and self.endAt>self.start then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
elseif self.pos<=0 then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end,
|
||||
FStep=function(self)
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
_G.__CAction__=self
|
||||
for i=1,#self.steps do
|
||||
self.steps[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
if self.endAt+self.count<=self.pos and self.endAt>self.start then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
elseif self.pos==0 then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for as=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[as])==tostring(self) then
|
||||
table.remove(event.Mainloop,as)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
Reset=function(self)
|
||||
self.pos=self.start
|
||||
end,
|
||||
Set=function(self,amt)
|
||||
self.pos=amt
|
||||
end,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
Stop=function(self)
|
||||
self:Reset()
|
||||
self.active=nil
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end,
|
||||
End=function(self)
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
self:Reset()
|
||||
end,
|
||||
Update=function(self,start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end,
|
||||
OnEnd=function(self,func)
|
||||
table.insert(self.funcs,func)
|
||||
end,
|
||||
OnStep=function(self,func)
|
||||
table.insert(self.steps,func)
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.funcs={}
|
||||
self.steps={}
|
||||
end,
|
||||
}
|
||||
--thread and run setup
|
||||
if love then
|
||||
function love.run()
|
||||
if love.math then
|
||||
love.math.setRandomSeed(os.time())
|
||||
end
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
end
|
||||
if love.load then love.load(arg) end
|
||||
if love.timer then love.timer.step() end
|
||||
local dt = 0
|
||||
while true do
|
||||
-- Process events.
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
for e,a,b,c,d in love.event.poll() do
|
||||
if e == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
if love.audio then
|
||||
love.audio.stop()
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
love.handlers[e](a,b,c,d)
|
||||
end
|
||||
end
|
||||
if love.timer then
|
||||
love.timer.step()
|
||||
dt = love.timer.getDelta()
|
||||
end
|
||||
if love.update then love.update(dt) end
|
||||
event.uManager(dt)
|
||||
if love.window and love.graphics and love.window.isCreated() then
|
||||
love.graphics.clear()
|
||||
love.graphics.origin()
|
||||
if love.draw then love.draw() end
|
||||
event.dManager()
|
||||
love.graphics.setColor(255,255,255,255)
|
||||
if event.draw then event.draw() end
|
||||
love.graphics.present()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
eThreads={
|
||||
send=function() end,
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
function RunTasks()
|
||||
for i=1,#event.DoTasks do
|
||||
event.DoTasks[i]()
|
||||
end
|
||||
end
|
||||
event={
|
||||
VERSION="1.0.0 (Build Version: 5.7.3)",
|
||||
Priority_Core=1,
|
||||
Priority_High=2,
|
||||
Priority_Above_Normal=4,
|
||||
Priority_Normal=16,
|
||||
Priority_Low=256,
|
||||
Priority_Idle=65536,
|
||||
Start=0,
|
||||
Active=true,
|
||||
CT=false,
|
||||
Tasks={},
|
||||
DoTasks={},
|
||||
garbage={},
|
||||
Paused={},
|
||||
last={},
|
||||
func={},
|
||||
drawF={},
|
||||
pump=false,
|
||||
pumpvar=0,
|
||||
Mainloop={},
|
||||
PEnabled=true,
|
||||
PCount=1,
|
||||
Triggers={},
|
||||
oneTimeObj=
|
||||
{
|
||||
last={},
|
||||
Resume=function(self) end,
|
||||
Pause=function(self) end,
|
||||
},
|
||||
RemoveAll=function()
|
||||
event.Mainloop={}
|
||||
end,
|
||||
GarbageObj=
|
||||
{
|
||||
Resume=function() end,
|
||||
Pause=function() end
|
||||
},
|
||||
isPaused=function(obj)
|
||||
for _j=1,#event.Paused do
|
||||
if tostring(event.Paused[_j])==tostring(obj) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
DO_Order=function()
|
||||
event.oneTime(RunTasks)
|
||||
event.PCount=event.PCount+1
|
||||
for i=1,#event.Mainloop do
|
||||
if event.Mainloop[i]~=nil then
|
||||
local obj = event.Mainloop[i]
|
||||
if event.PCount%obj.Priority==0 and event.PEnabled then
|
||||
obj:Act()
|
||||
elseif event.PEnabled==false then
|
||||
obj:Act()
|
||||
end
|
||||
if event.PCount>event.Priority_Idle then
|
||||
event.PCount=event.Priority_Core
|
||||
end
|
||||
end
|
||||
end
|
||||
event.MANAGE_GARBAGE()
|
||||
end,
|
||||
MANAGE_GARBAGE=function()
|
||||
_G.__CAction__=event.GarbageObj
|
||||
for _i=1,#event.garbage do
|
||||
event.garbage[_i]:Remove()
|
||||
table.remove(event.garbage,_i)
|
||||
end
|
||||
end,
|
||||
oneTime=function(func)
|
||||
event.oneTimeObj.last=_G.__CAction__
|
||||
_G.__CAction__=event.oneTimeObj
|
||||
for _k=1,#event.Tasks do
|
||||
if event.Tasks[_k]==func then
|
||||
_G.__CAction__=event.oneTimeObj.last
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(event.Tasks,func)
|
||||
func()
|
||||
_G.__CAction__=event.oneTimeObj.last
|
||||
return true
|
||||
end,
|
||||
oneETime=function(func)
|
||||
for _k=1,#event.Tasks do
|
||||
if event.Tasks[_k]==string.dump(func) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(event.Tasks,string.dump(func))
|
||||
func()
|
||||
return true
|
||||
end,
|
||||
hold=function(task) -- as many conditions and times that you want can be used
|
||||
local action=__CAction__
|
||||
action:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=event.newAlarm(task,function() end,true)
|
||||
while alarm.active==true do
|
||||
if love then
|
||||
event.lManager()
|
||||
else
|
||||
event.cManager()
|
||||
end
|
||||
end
|
||||
alarm:Remove()
|
||||
action:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=event.newEvent(task,function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.active do
|
||||
if love then
|
||||
event.lManager()
|
||||
else
|
||||
event.cManager()
|
||||
end
|
||||
end
|
||||
env:Remove()
|
||||
action:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end,
|
||||
waitFor=function(obj)
|
||||
local obj=obj
|
||||
event.hold(function() return not(obj.active) end)
|
||||
end,
|
||||
getType=function(obj)
|
||||
if obj.Type~=nil then
|
||||
return obj.Type
|
||||
end
|
||||
end,
|
||||
newEvent=function(test,task)
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Events,
|
||||
Type="Event",
|
||||
active=true,
|
||||
test=test or (function() end),
|
||||
task={task} or {},
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
if self:test(self)==true then
|
||||
self:Pause()
|
||||
self:Stop()
|
||||
for i=1,#self.task do
|
||||
self.task[i](self)
|
||||
end
|
||||
end
|
||||
end,
|
||||
Reset=function(self) self:Resume() end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
OnEvent=function(self,func)
|
||||
table.insert(self.task,func)
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.task={}
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
newAlarm=function(set,func,start)
|
||||
if not(start) then
|
||||
timer=0
|
||||
active=false
|
||||
else
|
||||
timer=os.clock()
|
||||
active=true
|
||||
end
|
||||
if not(func) then
|
||||
func=(function() end)
|
||||
end
|
||||
Alarm=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Alarms,
|
||||
Type="Alarm",
|
||||
active=active,
|
||||
timer=timer,
|
||||
set=set or 0,
|
||||
func={func},
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
if self.active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
self:Stop()
|
||||
self:Ring()
|
||||
end
|
||||
end
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.func={}
|
||||
end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Set=function(self,amt)
|
||||
self.set=amt
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end,
|
||||
OnRing=function(self,func)
|
||||
table.insert(self.func,func)
|
||||
end,
|
||||
Ring=function(self)
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end,
|
||||
Reset=function(self)
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end,
|
||||
Resume=EventRef.Resume,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,Alarm)
|
||||
event.last=temp
|
||||
return Alarm
|
||||
end,
|
||||
newTask=function(func)
|
||||
table.insert(event.DoTasks,func)
|
||||
end,
|
||||
createLoop=function()
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Loops,
|
||||
Type="Loop",
|
||||
active=true,
|
||||
func={},
|
||||
OnLoop=function(self,func)
|
||||
table.insert(self.func,func)
|
||||
end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-event.Start,self)
|
||||
end
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.func={}
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createStep=function(start,reset,count,skip)
|
||||
think=1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
start=start or 1,
|
||||
Parent=event.Steps,
|
||||
Type="Step",
|
||||
pos=start or 1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
skip=skip or 0,
|
||||
spos=0,
|
||||
count=count or 1*think,
|
||||
funcs={},
|
||||
steps={},
|
||||
Act=StepRef.Step,
|
||||
FAct=StepRef.FStep,
|
||||
Remove=StepRef.Remove,
|
||||
Reset=StepRef.Reset,
|
||||
Set=StepRef.Set,
|
||||
Pause=StepRef.Pause,
|
||||
Resume=StepRef.Resume,
|
||||
Stop=StepRef.Stop,
|
||||
End=StepRef.End,
|
||||
Update=StepRef.Update,
|
||||
OnEnd=StepRef.OnEnd,
|
||||
OnStep=StepRef.OnStep,
|
||||
FreeConnections=StepRef.FreeConnections
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createTStep=function(start,reset,timer,count)
|
||||
think=1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
timer=timer or 1
|
||||
local _alarm=event.newAlarm(timer,function(alarm) alarm.Link:Act() alarm:Reset() end,true)
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
start=start or 1,
|
||||
Parent=event.TSteps,
|
||||
Type="TStep",
|
||||
pos=start or 1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
skip= 0,
|
||||
spos=0,
|
||||
count=count or 1*think,
|
||||
funcs={},
|
||||
steps={},
|
||||
alarm=_alarm,
|
||||
Act=StepRef.Step,
|
||||
FAct=StepRef.FStep,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for as=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[as])==tostring(self) then
|
||||
table.remove(event.Mainloop,as)
|
||||
end
|
||||
end
|
||||
self.alarm:Remove()
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
Reset=StepRef.Reset,
|
||||
Set=StepRef.Set,
|
||||
Pause=StepRef.Pause,
|
||||
Resume=StepRef.Resume,
|
||||
Stop=StepRef.Stop,
|
||||
End=StepRef.End,
|
||||
Update=function(self,start,reset,timer,count)
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
if not(count<0) then
|
||||
print("less")
|
||||
count=-count
|
||||
end
|
||||
end
|
||||
end
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
if timer~=nil then
|
||||
self.alarm:Set(timer)
|
||||
end
|
||||
self.count=count or self.count
|
||||
self.pos=self.start
|
||||
self:Resume()
|
||||
end,
|
||||
OnEnd=StepRef.OnEnd,
|
||||
OnStep=StepRef.OnStep,
|
||||
FreeConnections=StepRef.FreeConnections
|
||||
}
|
||||
_alarm.Link=temp
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createTrigger=function(func)
|
||||
temp={
|
||||
active=true,
|
||||
trigfunc=func,
|
||||
Remove=function(self)
|
||||
for i=1,#event.Triggers do
|
||||
if event.Triggers[i]==self then
|
||||
table.remove(event.Triggers,i)
|
||||
end
|
||||
end
|
||||
end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Fire=function(self,...)
|
||||
if self.active==true then
|
||||
local tempA=__CAction__
|
||||
__CAction__=self
|
||||
self:trigfunc(...)
|
||||
__CAction__=tempA
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Triggers,temp)
|
||||
return temp
|
||||
end,
|
||||
stop=function()
|
||||
event.Active=false
|
||||
end,
|
||||
onStart=function() end,
|
||||
onUpdate=function(func)
|
||||
local temp=event.createLoop()
|
||||
temp:OnLoop(func)
|
||||
temp.Priority=1
|
||||
end,
|
||||
onDraw=function(func)
|
||||
table.insert(event.drawF,func)
|
||||
end,
|
||||
onClose=function() end,
|
||||
manager=function()
|
||||
if not(love) then
|
||||
event.onStart()
|
||||
event.Start=os.clock()
|
||||
while event.Active==true do
|
||||
event.DO_Order()
|
||||
end
|
||||
event.onClose()
|
||||
return os.clock()-event.Start
|
||||
else
|
||||
return false
|
||||
end
|
||||
end,
|
||||
cManager=function()
|
||||
if event.Active==true then
|
||||
event.DO_Order()
|
||||
end
|
||||
end,
|
||||
uManager=function(dt)
|
||||
if event.CT==false then
|
||||
if dt then
|
||||
event.pump=true
|
||||
end
|
||||
event.CT=true
|
||||
event.onStart()
|
||||
event.Start=os.clock()
|
||||
end
|
||||
event.pumpvar=dt
|
||||
if event.Active==true then
|
||||
event.DO_Order()
|
||||
end
|
||||
end,
|
||||
dManager=function()
|
||||
for ii=1,#event.drawF do
|
||||
event.drawF[ii]()
|
||||
end
|
||||
end,
|
||||
lManager=function()
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
for e,a,b,c,d in love.event.poll() do
|
||||
if e == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
if love.audio then
|
||||
love.audio.stop()
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
love.handlers[e](a,b,c,d)
|
||||
end
|
||||
end
|
||||
if love.timer then
|
||||
love.timer.step()
|
||||
dt = love.timer.getDelta()
|
||||
end
|
||||
if love.update then love.update(dt) end
|
||||
event.uManager(dt)
|
||||
if love.window and love.graphics and love.window.isCreated() then
|
||||
love.graphics.clear()
|
||||
love.graphics.origin()
|
||||
if love.draw then love.draw() end
|
||||
event.dManager()
|
||||
love.graphics.present()
|
||||
end
|
||||
end,
|
||||
benchMark=function(sec,p)
|
||||
p=p or event.Priority_Normal
|
||||
local temp=event.createStep(10)
|
||||
temp.CC=0
|
||||
temp:OnStep(function(pos,step) step.CC=step.CC+1 end)
|
||||
local Loud=event.newAlarm(sec,nil,true)
|
||||
Loud.Link=temp
|
||||
Loud:OnRing(function(alarm) print((alarm.Link.CC).." steps in "..alarm.set.." second(s)") end)
|
||||
temp.Priority=p
|
||||
Loud.Priority=p
|
||||
return Loud
|
||||
end,
|
||||
}
|
||||
@@ -0,0 +1,365 @@
|
||||
multi = {}
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
-- System
|
||||
function multi:newBase(ins)
|
||||
local c = {}
|
||||
setmetatable(c, multi)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Act=function() end
|
||||
if ins then
|
||||
table.insert(multi.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(multi.Mainloop,c)
|
||||
end
|
||||
multi.MasterId=multi.MasterId+1
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
--Processor
|
||||
function multi.Do_Order()
|
||||
for _D=#multi.Mainloop,1,-1 do
|
||||
if multi.Mainloop[_D]~=nil then
|
||||
multi.Mainloop[_D].Id=_D
|
||||
multi.Mainloop[_D]:Act()
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=multi:newStep(2)
|
||||
temp.CC=0
|
||||
temp:OnStep(function(pos,step) step.CC=step.CC+1 end)
|
||||
local Loud=multi:newAlarm(sec)
|
||||
Loud.Link=temp
|
||||
Loud:OnRing(function(alarm) alarm.Link.CC=alarm.Link.CC print((alarm.Link.CC).." steps in "..alarm.set.." second(s)") alarm.bench=alarm.Link.CC alarm.Link:Destroy() alarm:Destroy() end)
|
||||
return Loud
|
||||
end
|
||||
--Helpers
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if not(n) then
|
||||
self.Active=false
|
||||
table.remove(multi.Mainloop,self.Id)
|
||||
table.insert(multi.Paused,self)
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self:isPaused() then
|
||||
self.Active=true
|
||||
table.remove(multi.Paused,self.Id)
|
||||
table.insert(multi.Mainloop,self)
|
||||
end
|
||||
end
|
||||
function multi:Remove()
|
||||
self:Pause()
|
||||
self:Destroy()
|
||||
end
|
||||
function multi:Destroy()
|
||||
self:Pause()
|
||||
if self:isPaused() then
|
||||
for i=1,#multi.Paused do
|
||||
if multi.Paused[i]==self then
|
||||
table.remove(multi.Paused,i)
|
||||
return
|
||||
end
|
||||
end
|
||||
else
|
||||
table.remove(multi.Mainloop,self.Id)
|
||||
end
|
||||
self.Act=function() end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=multi:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
multi.lManager()
|
||||
else
|
||||
multi.Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=multi:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
multi.lManager()
|
||||
else
|
||||
multi.Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
for _k=1,#multi.Tasks2 do
|
||||
if multi.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(multi.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c=multi:newBase()
|
||||
c.Type="Event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c=multi:newBase()
|
||||
c.Type="Alarm"
|
||||
c.timer=os.clock()
|
||||
c.set=set or 0
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(multi.Tasks,func)
|
||||
end
|
||||
function multi:newLoop()
|
||||
local c=multi:newBase()
|
||||
c.Type="Loop"
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-multi.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c=multi:newBase()
|
||||
think=1
|
||||
c.Type="Step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
c:OnStep(function(p,s)
|
||||
if s.count>0 and s.endAt==p then
|
||||
for fe=1,#s.funcE do
|
||||
s.funcE[fe](s)
|
||||
end
|
||||
s.pos=s.start-1
|
||||
elseif s.count<0 and s.endAt==p then
|
||||
for fe=1,#s.funcE do
|
||||
s.funcE[fe](s)
|
||||
end
|
||||
s.pos=s.start-1
|
||||
end
|
||||
end)
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c=multi:newBase()
|
||||
think=1
|
||||
c.Type="TStep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=os.clock()
|
||||
c.set=set or 1
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
if self.endAt==self.pos then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start-1
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
function multi:inQueue(func)
|
||||
if self.Id==-1 then
|
||||
print("Error: Can't queue the multi object")
|
||||
return
|
||||
end
|
||||
local c=multi:newBase(self.Id)
|
||||
self.Id=self.Id-1
|
||||
c.Type="Queue"
|
||||
c.Task=func
|
||||
c.Link=self
|
||||
function c:Act()
|
||||
self.Task(self.Link)
|
||||
self:Destroy()
|
||||
end
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c=multi:newBase()
|
||||
c.Type="Trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#multi.Tasks do
|
||||
multi.Tasks[i]()
|
||||
end
|
||||
multi.Start=os.clock()
|
||||
while self.Active do
|
||||
multi.Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(dt)
|
||||
if dt then
|
||||
multi.pump=true
|
||||
end
|
||||
multi.pumpvar=dt
|
||||
multi.Start=os.clock()
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
multi:oneTime(multi._tFunc,dt)
|
||||
multi.Do_Order()
|
||||
end
|
||||
@@ -0,0 +1,506 @@
|
||||
multi = {}
|
||||
multi.Version="4.0.1"
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
multi.Type="mainint"
|
||||
multi.Rest=0
|
||||
multi._type=type
|
||||
--[[function type(v)
|
||||
local t={}
|
||||
if multi._type(v)=="table" then
|
||||
t=getmetatable(v)
|
||||
if v.Type~=nil then
|
||||
if multi._type(v.Type)=="string" then
|
||||
return v.Type
|
||||
end
|
||||
end
|
||||
end
|
||||
if t.__type~=nil then
|
||||
return t.__type
|
||||
else
|
||||
return multi._type(v)
|
||||
end
|
||||
end]]
|
||||
-- System
|
||||
function os.getOS()
|
||||
if package.config:sub(1,1)=="\\" then
|
||||
return "windows"
|
||||
else
|
||||
return "unix"
|
||||
end
|
||||
end
|
||||
if os.getOS()=="windows" then
|
||||
function os.sleep(n)
|
||||
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
|
||||
end
|
||||
else
|
||||
function os.sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
end
|
||||
function multi:newBase(ins)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Act=function() end
|
||||
c.Parent=self
|
||||
if ins then
|
||||
table.insert(self.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(self.Mainloop,c)
|
||||
end
|
||||
self.MasterId=self.MasterId+1
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
self.Mainloop={}
|
||||
self.Tasks={}
|
||||
self.Tasks2={}
|
||||
self.Garbage={}
|
||||
self.Children={}
|
||||
self.Paused={}
|
||||
self.MasterId=0
|
||||
self.Active=true
|
||||
self.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:getChildren()
|
||||
return self.Mainloop
|
||||
end
|
||||
--Processor
|
||||
function multi:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
self.Mainloop[_D]:Act()
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest>0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=self:newLoop(function(t,self)
|
||||
if os.clock()-self.init>self.sec then
|
||||
print(self.c.." steps in "..self.sec.." second(s)")
|
||||
self.tt(self.sec)
|
||||
self:Destroy()
|
||||
else
|
||||
self.c=self.c+1
|
||||
end
|
||||
end)
|
||||
function temp:OnBench(func)
|
||||
self.tt=func
|
||||
end
|
||||
self.tt=function() end
|
||||
temp.sec=sec
|
||||
temp.init=os.clock()
|
||||
temp.c=0
|
||||
return temp
|
||||
end
|
||||
function multi:newInterface()
|
||||
if not(self.Type=="mainint") then error("Can only create an interface on the multi obj") return false end
|
||||
local c = {}
|
||||
setmetatable(c, self)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Type="int"
|
||||
c.Mainloop={}
|
||||
c.Tasks={}
|
||||
c.Tasks2={}
|
||||
c.Garbage={}
|
||||
c.Children={}
|
||||
c.Paused={}
|
||||
c.MasterId=0
|
||||
c.Active=true
|
||||
c.Id=-1
|
||||
c.Rest=0
|
||||
function c:Start()
|
||||
if self.l then
|
||||
self.l:Resume()
|
||||
else
|
||||
self.l=self.Parent:newLoop(function(dt) c:uManager(dt) end)
|
||||
end
|
||||
end
|
||||
function c:Stop()
|
||||
if self.l then
|
||||
self.l:Pause()
|
||||
end
|
||||
end
|
||||
function c:Remove()
|
||||
self:Destroy()
|
||||
self.l:Destroy()
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Helpers
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=false
|
||||
if not(n) then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Pause()
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
else
|
||||
if n==nil then
|
||||
self.Active=false
|
||||
if self.Parent.Mainloop[self.Id]~=nil then
|
||||
table.remove(self.Parent.Mainloop,self.Id)
|
||||
table.insert(self.Parent.Paused,self)
|
||||
self.PId=#self.Parent.Paused
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=true
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Resume()
|
||||
end
|
||||
else
|
||||
if self:isPaused() then
|
||||
table.remove(self.Parent.Paused,self.PId)
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Id=#self.Parent.Mainloop
|
||||
self.Active=true
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Destroy()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Destroy()
|
||||
end
|
||||
else
|
||||
self.rem=true
|
||||
self.Active=false
|
||||
end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=self.Parent:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=self.Parent:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then
|
||||
for _k=1,#self.Parent.Tasks2 do
|
||||
if self.Parent.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Parent.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
else
|
||||
for _k=1,#self.Tasks2 do
|
||||
if self.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c=self:newBase()
|
||||
c.Type="event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c=self:newBase()
|
||||
c.Type="alarm"
|
||||
c.timer=os.clock()
|
||||
c.set=set or 0
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
self.Active=true
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(self.Tasks,func)
|
||||
end
|
||||
function multi:newLoop(func)
|
||||
local c=self:newBase()
|
||||
c.Type="loop"
|
||||
if func then
|
||||
c.func={func}
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-self.Parent.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c=self:newBase()
|
||||
think=1
|
||||
c.Type="step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.funcS={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c=self:newBase()
|
||||
think=1
|
||||
c.Type="tstep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=os.clock()
|
||||
c.set=set or 1
|
||||
c.funcS={}
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c=self:newBase()
|
||||
c.Type="trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
self.Start=os.clock()
|
||||
while self.Active do
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(self,dt)
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
if dt then
|
||||
self.pump=true
|
||||
end
|
||||
self.pumpvar=dt
|
||||
self.Start=os.clock()
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
if self.Active then
|
||||
self:oneTime(self._tFunc,self,dt)
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,596 @@
|
||||
multi = {}
|
||||
multi.Version="4.0.5"
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
multi.Type="mainint"
|
||||
multi.Rest=0
|
||||
multi._type=type
|
||||
multi.Jobs={}
|
||||
multi.queue={}
|
||||
multi.jobUS=2
|
||||
--[[function type(v)
|
||||
local t={}
|
||||
if multi._type(v)=="table" then
|
||||
t=getmetatable(v)
|
||||
if v.Type~=nil then
|
||||
if multi._type(v.Type)=="string" then
|
||||
return v.Type
|
||||
end
|
||||
end
|
||||
end
|
||||
if t.__type~=nil then
|
||||
return t.__type
|
||||
else
|
||||
return multi._type(v)
|
||||
end
|
||||
end]]
|
||||
-- System
|
||||
function os.getOS()
|
||||
if package.config:sub(1,1)=="\\" then
|
||||
return "windows"
|
||||
else
|
||||
return "unix"
|
||||
end
|
||||
end
|
||||
if os.getOS()=="windows" then
|
||||
function os.sleep(n)
|
||||
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
|
||||
end
|
||||
else
|
||||
function os.sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
end
|
||||
function multi:newBase(ins)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Act=function() end
|
||||
c.Parent=self
|
||||
if ins then
|
||||
table.insert(self.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(self.Mainloop,c)
|
||||
end
|
||||
self.MasterId=self.MasterId+1
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
self.Mainloop={}
|
||||
self.Tasks={}
|
||||
self.Tasks2={}
|
||||
self.Garbage={}
|
||||
self.Children={}
|
||||
self.Paused={}
|
||||
self.MasterId=0
|
||||
self.Active=true
|
||||
self.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:getChildren()
|
||||
return self.Mainloop
|
||||
end
|
||||
--Processor
|
||||
function multi:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
self.Mainloop[_D]:Act()
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=self:newLoop(function(t,self)
|
||||
if os.clock()-self.init>self.sec then
|
||||
print(self.c.." steps in "..self.sec.." second(s)")
|
||||
self.tt(self.sec)
|
||||
self:Destroy()
|
||||
else
|
||||
self.c=self.c+1
|
||||
end
|
||||
end)
|
||||
function temp:OnBench(func)
|
||||
self.tt=func
|
||||
end
|
||||
self.tt=function() end
|
||||
temp.sec=sec
|
||||
temp.init=os.clock()
|
||||
temp.c=0
|
||||
return temp
|
||||
end
|
||||
function multi:newInterface()
|
||||
if not(self.Type=="mainint") then error("Can only create an interface on the multi obj") return false end
|
||||
local c = {}
|
||||
setmetatable(c, self)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Type="int"
|
||||
c.Mainloop={}
|
||||
c.Tasks={}
|
||||
c.Tasks2={}
|
||||
c.Garbage={}
|
||||
c.Children={}
|
||||
c.Paused={}
|
||||
c.MasterId=0
|
||||
c.Active=true
|
||||
c.Id=-1
|
||||
c.Rest=0
|
||||
c.Jobs={}
|
||||
c.queue={}
|
||||
c.jobUS=2
|
||||
function c:Start()
|
||||
if self.l then
|
||||
self.l:Resume()
|
||||
else
|
||||
self.l=self.Parent:newLoop(function(dt) c:uManager(dt) end)
|
||||
end
|
||||
end
|
||||
function c:Stop()
|
||||
if self.l then
|
||||
self.l:Pause()
|
||||
end
|
||||
end
|
||||
function c:Remove()
|
||||
self:Destroy()
|
||||
self.l:Destroy()
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Helpers
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=false
|
||||
if not(n) then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Pause()
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
else
|
||||
if n==nil then
|
||||
self.Active=false
|
||||
if self.Parent.Mainloop[self.Id]~=nil then
|
||||
table.remove(self.Parent.Mainloop,self.Id)
|
||||
table.insert(self.Parent.Paused,self)
|
||||
self.PId=#self.Parent.Paused
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=true
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Resume()
|
||||
end
|
||||
else
|
||||
if self:isPaused() then
|
||||
table.remove(self.Parent.Paused,self.PId)
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Id=#self.Parent.Mainloop
|
||||
self.Active=true
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Destroy()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Destroy()
|
||||
end
|
||||
else
|
||||
self.rem=true
|
||||
self.Active=false
|
||||
end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=self.Parent:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=self.Parent:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then
|
||||
for _k=1,#self.Parent.Tasks2 do
|
||||
if self.Parent.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Parent.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
else
|
||||
for _k=1,#self.Tasks2 do
|
||||
if self.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c=self:newBase()
|
||||
c.Type="event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c=self:newBase()
|
||||
c.Type="alarm"
|
||||
c.timer=os.clock()
|
||||
c.set=set or 0
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
self.Active=true
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(self.Tasks,func)
|
||||
end
|
||||
function multi:newLoop(func)
|
||||
local c=self:newBase()
|
||||
c.Type="loop"
|
||||
if func then
|
||||
c.func={func}
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-self.Parent.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c=self:newBase()
|
||||
think=1
|
||||
c.Type="step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.funcS={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c=self:newBase()
|
||||
think=1
|
||||
c.Type="tstep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=os.clock()
|
||||
c.set=set or 1
|
||||
c.funcS={}
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c={}--self:newBase()
|
||||
c.Type="trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newConnection()
|
||||
local c={}
|
||||
c.Type="connector"
|
||||
c.func={}
|
||||
function c:Fire(...)
|
||||
for i=1,#self.func do
|
||||
t,e=pcall(self.func[i],...)
|
||||
if not(t) then
|
||||
print(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:connect(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newJob(func,name)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Parent=self
|
||||
c.Type="job"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Act()
|
||||
self:trigfunc(self)
|
||||
end
|
||||
table.insert(self.Jobs,{c,name})
|
||||
if self.JobRunner==nil then
|
||||
self.JobRunner=self:newAlarm(self.jobUS)
|
||||
self.JobRunner:OnRing(function(self)
|
||||
if #self.Parent.Jobs>0 then
|
||||
if self.Parent.Jobs[1] then
|
||||
self.Parent.Jobs[1][1]:Act()
|
||||
table.remove(self.Parent.Jobs,1)
|
||||
end
|
||||
end
|
||||
self:Reset(self.Parent.jobUS)
|
||||
end)
|
||||
end
|
||||
end
|
||||
function multi:setJobSpeed(n)
|
||||
self.jobUS=n
|
||||
end
|
||||
function multi:hasJobs()
|
||||
return #self.Jobs>0,#self.Jobs
|
||||
end
|
||||
function multi:getJobs()
|
||||
return #self.Jobs
|
||||
end
|
||||
function multi:removeJob(name)
|
||||
for i=#self.Jobs,1,-1 do
|
||||
if self.Jobs[i][2]==name then
|
||||
table.remove(self.Jobs,i)
|
||||
end
|
||||
end
|
||||
end
|
||||
--Incomplete
|
||||
function multi:addToQueue(name,job)
|
||||
if self.queue[name]~=nil then
|
||||
table.insert(self.queue[name],job)
|
||||
else
|
||||
self.queue[name]={}
|
||||
end
|
||||
if self.QRunner==nil then
|
||||
self.QRunner=self:newAlarm(.5)
|
||||
self.QRunner:OnRing(function(self)
|
||||
if #self.Parent.queue>0 then
|
||||
local w=math.random(1,#self.Parent.Jobs)
|
||||
self.Parent.Jobs[w]:Act()
|
||||
table.remove(self.Parent.Jobs,w)
|
||||
end
|
||||
self:Reset()
|
||||
end)
|
||||
end
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
self.Start=os.clock()
|
||||
while self.Active do
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(self,dt)
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
if dt then
|
||||
self.pump=true
|
||||
end
|
||||
self.pumpvar=dt
|
||||
self.Start=os.clock()
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
if self.Active then
|
||||
self:oneTime(self._tFunc,self,dt)
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,767 @@
|
||||
multi = {}
|
||||
multi.Version="5.1.6"
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
multi.Type="mainint"
|
||||
multi.Rest=0
|
||||
multi._type=type
|
||||
multi.Jobs={}
|
||||
multi.queue={}
|
||||
multi.jobUS=2
|
||||
-- System
|
||||
function multi:Stop()
|
||||
self.Active=false
|
||||
end
|
||||
function os.getOS()
|
||||
if package.config:sub(1,1)=="\\" then
|
||||
return "windows"
|
||||
else
|
||||
return "unix"
|
||||
end
|
||||
end
|
||||
if os.getOS()=="windows" then
|
||||
function os.sleep(n)
|
||||
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
|
||||
end
|
||||
else
|
||||
function os.sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
end
|
||||
function multi:newBase(ins)
|
||||
if not(self.Type=="mainint" or self.Type=="int" or self.Type=="stack") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" or self.Type=="stack" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.ender={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Act=function() end
|
||||
c.Parent=self
|
||||
if ins then
|
||||
table.insert(self.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(self.Mainloop,c)
|
||||
end
|
||||
self.MasterId=self.MasterId+1
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
self.Mainloop={}
|
||||
self.Tasks={}
|
||||
self.Tasks2={}
|
||||
self.Garbage={}
|
||||
self.Children={}
|
||||
self.Paused={}
|
||||
self.MasterId=0
|
||||
self.Active=true
|
||||
self.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:getChildren()
|
||||
return self.Mainloop
|
||||
end
|
||||
--Processor
|
||||
function multi:getError()
|
||||
if self.error then
|
||||
return self.error
|
||||
end
|
||||
end
|
||||
function multi:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
self.Mainloop[_D]:Act()
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=self:newLoop(function(t,self)
|
||||
if os.clock()-self.init>self.sec then
|
||||
print(self.c.." steps in "..self.sec.." second(s)")
|
||||
self.tt(self.sec)
|
||||
self:Destroy()
|
||||
else
|
||||
self.c=self.c+1
|
||||
end
|
||||
end)
|
||||
function temp:OnBench(func)
|
||||
self.tt=func
|
||||
end
|
||||
self.tt=function() end
|
||||
temp.sec=sec
|
||||
temp.init=os.clock()
|
||||
temp.c=0
|
||||
return temp
|
||||
end
|
||||
function multi:newInterface()
|
||||
if not(self.Type=="mainint") then error("Can only create an interface on the multi obj") return false end
|
||||
local c = {}
|
||||
setmetatable(c, self)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Type="int"
|
||||
c.Mainloop={}
|
||||
c.Tasks={}
|
||||
c.Tasks2={}
|
||||
c.Garbage={}
|
||||
c.Children={}
|
||||
c.Paused={}
|
||||
c.MasterId=0
|
||||
c.Active=true
|
||||
c.Id=-1
|
||||
c.Rest=0
|
||||
c.Jobs={}
|
||||
c.queue={}
|
||||
c.jobUS=2
|
||||
function c:Start()
|
||||
if self.l then
|
||||
self.l:Resume()
|
||||
else
|
||||
self.l=self.Parent:newLoop(function(dt) c:uManager(dt) end)
|
||||
end
|
||||
end
|
||||
function c:Stop()
|
||||
if self.l then
|
||||
self.l:Pause()
|
||||
end
|
||||
end
|
||||
function c:Remove()
|
||||
self:Destroy()
|
||||
self.l:Destroy()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStack(file)
|
||||
local c=self:newInterface()
|
||||
c.Type="stack"
|
||||
stack=c
|
||||
c.last={}
|
||||
c.funcE={}
|
||||
if file then
|
||||
dofile(file)
|
||||
end
|
||||
function c:OnStackCompleted(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Helpers
|
||||
function multi:protect()
|
||||
function self:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
local status, err=pcall(self.Mainloop[_D].Act,self.Mainloop[_D])
|
||||
if err and not(self.Mainloop[_D].error) then
|
||||
self.Mainloop[_D].error=err
|
||||
print(err..": Ingoring error continuing...")
|
||||
end
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:unProtect()
|
||||
function self:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
self.Mainloop[_D]:Act()
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:setJobSpeed(n)
|
||||
self.jobUS=n
|
||||
end
|
||||
function multi:hasJobs()
|
||||
return #self.Jobs>0,#self.Jobs
|
||||
end
|
||||
function multi:getJobs()
|
||||
return #self.Jobs
|
||||
end
|
||||
function multi:removeJob(name)
|
||||
for i=#self.Jobs,1,-1 do
|
||||
if self.Jobs[i][2]==name then
|
||||
table.remove(self.Jobs,i)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:connectFinal(func)
|
||||
if self.Type=="event" then
|
||||
self:OnEvent(func)
|
||||
elseif self.Type=="alarm" then
|
||||
self:OnRing(func)
|
||||
elseif self.Type=="step" or self.Type=="tstep" then
|
||||
self:OnEnd(func)
|
||||
elseif self.Type=="loop" then
|
||||
self:OnBreak(func)
|
||||
else
|
||||
error("No final event exists for: "..self.Type)
|
||||
end
|
||||
end
|
||||
function multi:Break()
|
||||
self:Pause()
|
||||
self.Active=nil
|
||||
for i=1,#self.ender do
|
||||
self.ender[i](self)
|
||||
end
|
||||
end
|
||||
function multi:OnBreak(func)
|
||||
table.insert(self.ender,func)
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=false
|
||||
if not(n) then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Pause()
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
else
|
||||
if n==nil then
|
||||
self.Active=false
|
||||
if self.Parent.Mainloop[self.Id]~=nil then
|
||||
table.remove(self.Parent.Mainloop,self.Id)
|
||||
table.insert(self.Parent.Paused,self)
|
||||
self.PId=#self.Parent.Paused
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=true
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Resume()
|
||||
end
|
||||
else
|
||||
if self:isPaused() then
|
||||
table.remove(self.Parent.Paused,self.PId)
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Id=#self.Parent.Mainloop
|
||||
self.Active=true
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Destroy()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Destroy()
|
||||
end
|
||||
else
|
||||
self.rem=true
|
||||
self.Active=false
|
||||
end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=self.Parent:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=self.Parent:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then
|
||||
for _k=1,#self.Parent.Tasks2 do
|
||||
if self.Parent.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Parent.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
else
|
||||
for _k=1,#self.Tasks2 do
|
||||
if self.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
end
|
||||
function multi:Reset(n)
|
||||
self:Resume()
|
||||
end
|
||||
function multi:isDone()
|
||||
return self.Active~=true
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="alarm"
|
||||
c.timer=os.clock()
|
||||
c.set=set or 0
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
self.Active=false
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Resume()
|
||||
self.Parent.Resume(self)
|
||||
self.timer=os.clock()
|
||||
self.Active=true
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
self.Active=true
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
table.remove(self.Parent.Mainloop,#self.Parent.Mainloop)
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(self.Tasks,func)
|
||||
end
|
||||
function multi:newLoop(func)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="loop"
|
||||
c.Start=os.clock()
|
||||
if func then
|
||||
c.func={func}
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-self.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
think=1
|
||||
c.Type="step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.funcS={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
self:Pause()
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
think=1
|
||||
c.Type="tstep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=os.clock()
|
||||
c.set=set or 1
|
||||
c.funcS={}
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
self:Pause()
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c={}
|
||||
c.Type="trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newConnection()
|
||||
local c={}
|
||||
c.Type="connector"
|
||||
c.func={}
|
||||
function c:Fire(...)
|
||||
for i=1,#self.func do
|
||||
t,e=pcall(self.func[i],...)
|
||||
if not(t) then
|
||||
print(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:bind(t)
|
||||
self.func=t
|
||||
end
|
||||
function c:connect(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newJob(func,name)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Parent=self
|
||||
c.Type="job"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Act()
|
||||
self:trigfunc(self)
|
||||
end
|
||||
table.insert(self.Jobs,{c,name})
|
||||
if self.JobRunner==nil then
|
||||
self.JobRunner=self:newAlarm(self.jobUS)
|
||||
self.JobRunner:OnRing(function(self)
|
||||
if #self.Parent.Jobs>0 then
|
||||
if self.Parent.Jobs[1] then
|
||||
self.Parent.Jobs[1][1]:Act()
|
||||
table.remove(self.Parent.Jobs,1)
|
||||
end
|
||||
end
|
||||
self:Reset(self.Parent.jobUS)
|
||||
end)
|
||||
end
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
self.Start=os.clock()
|
||||
while self.Active do
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(self,dt)
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
if dt then
|
||||
self.pump=true
|
||||
end
|
||||
self.pumpvar=dt
|
||||
self.Start=os.clock()
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
if self.Active then
|
||||
self:oneTime(self._tFunc,self,dt)
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,929 @@
|
||||
multi = {}
|
||||
multi.Version={6,1,6}-- History: EventManager,EventManager+,MultiManager <-- Current
|
||||
multi.stage="stable"
|
||||
multi.Features=multi.Version[1].."."..multi.Version[2].."."..multi.Version[3].." "..multi.stage..[[
|
||||
Objects:
|
||||
Event
|
||||
Alarm
|
||||
Loop
|
||||
Step
|
||||
TStep
|
||||
Trigger
|
||||
Task
|
||||
Connection
|
||||
Timer
|
||||
Job
|
||||
]]
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
multi.Type="mainint"
|
||||
multi.Rest=0
|
||||
multi._type=type
|
||||
multi.Jobs={}
|
||||
multi.queue={}
|
||||
multi.jobUS=2
|
||||
multi.clock=os.clock
|
||||
multi.time=os.time
|
||||
-- System
|
||||
function multi:Stop()
|
||||
self.Active=false
|
||||
end
|
||||
function os.getOS()
|
||||
if package.config:sub(1,1)=="\\" then
|
||||
return "windows"
|
||||
else
|
||||
return "unix"
|
||||
end
|
||||
end
|
||||
if os.getOS()=="windows" then
|
||||
function os.sleep(n)
|
||||
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
|
||||
end
|
||||
else
|
||||
function os.sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
end
|
||||
function multi.executeFunction(name,...)
|
||||
if type(_G[name])=="function" then
|
||||
_G[name](...)
|
||||
else
|
||||
print("Error: Not a function")
|
||||
end
|
||||
end
|
||||
function multi:newBase(ins)
|
||||
if not(self.Type=="mainint" or self.Type=="int" or self.Type=="stack") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" or self.Type=="stack" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.ender={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Act=function() end
|
||||
c.Parent=self
|
||||
if ins then
|
||||
table.insert(self.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(self.Mainloop,c)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
local before=collectgarbage("count")
|
||||
self.Mainloop={}
|
||||
self.Tasks={}
|
||||
self.Tasks2={}
|
||||
self.Garbage={}
|
||||
self.Children={}
|
||||
self.Paused={}
|
||||
self.Active=true
|
||||
self.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
collectgarbage()
|
||||
local after=collectgarbage("count")
|
||||
print([[Before rebooting total Ram used was ]]..before..[[Kb
|
||||
After rebooting total Ram used is ]]..after..[[ Kb
|
||||
A total of ]]..(before-after)..[[Kb was cleaned up]])
|
||||
end
|
||||
function multi:getChildren()
|
||||
return self.Mainloop
|
||||
end
|
||||
--Processor
|
||||
function multi:getError()
|
||||
if self.error then
|
||||
return self.error
|
||||
end
|
||||
end
|
||||
function multi:Do_Order()
|
||||
local Loop=self.Mainloop
|
||||
for _D=#Loop,1,-1 do
|
||||
if Loop[_D]~=nil then
|
||||
Loop[_D].Id=_D
|
||||
Loop[_D]:Act()
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
function multi:fromfile(path,int)
|
||||
int=int or multi
|
||||
local test2={}
|
||||
local test=bin.load(path)
|
||||
local tp=test:getBlock("s")
|
||||
if tp=="event" then
|
||||
test2=int:newEvent(test:getBlock("f"))
|
||||
local t=test:getBlock("t")
|
||||
for i=1,#t do
|
||||
test2:OnEvent(t[i])
|
||||
end
|
||||
elseif tp=="alarm" then
|
||||
test2=int:newAlarm(test:getBlock("n"))
|
||||
elseif tp=="loop" then
|
||||
test2=int:newLoop(test:getBlock("t")[1])
|
||||
elseif tp=="step" or tp=="tstep" then
|
||||
local func=test:getBlock("t")
|
||||
local funcE=test:getBlock("t")
|
||||
local funcS=test:getBlock("t")
|
||||
local tab=test:getBlock("t")
|
||||
test2=int:newStep()
|
||||
table.merge(test2,tab)
|
||||
test2.funcE=funcE
|
||||
test2.funcS=funcS
|
||||
test2.func=func
|
||||
elseif tp=="trigger" then
|
||||
test2=int:newTrigger(test:getBlock("f"))
|
||||
elseif tp=="connector" then
|
||||
test2=int:newConnection()
|
||||
test2.func=test:getBlock("t")
|
||||
elseif tp=="timer" then
|
||||
test2=int:newTimer()
|
||||
test2.count=tonumber(test:getBlock("n"))
|
||||
else
|
||||
print("Error: The file you selected is not a valid multi file object!")
|
||||
return false
|
||||
end
|
||||
return test2
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=self:newLoop(function(t,self)
|
||||
if multi.clock()-self.init>self.sec then
|
||||
print(self.c.." steps in "..self.sec.." second(s)")
|
||||
self.tt(self.sec)
|
||||
self:Destroy()
|
||||
else
|
||||
self.c=self.c+1
|
||||
end
|
||||
end)
|
||||
function temp:OnBench(func)
|
||||
self.tt=func
|
||||
end
|
||||
self.tt=function() end
|
||||
temp.sec=sec
|
||||
temp.init=multi.clock()
|
||||
temp.c=0
|
||||
return temp
|
||||
end
|
||||
function multi:tofile(path)
|
||||
local items=self:getChildren()
|
||||
io.mkDir(io.getName(path))
|
||||
for i=1,#items do
|
||||
items[i]:tofile(io.getName(path).."\\item"..item[i]..".dat")
|
||||
end
|
||||
local int=bin.new()
|
||||
int:addBlock("int")
|
||||
int:addBlock(io.getName(path))
|
||||
int:addBlock(#self.Mainloop)
|
||||
int:addBlock(self.Active)
|
||||
int:addBlock(self.Rest)
|
||||
int:addBlock(self.Jobs)
|
||||
int:tofile()
|
||||
end
|
||||
function multi:newInterface(file)
|
||||
if not(self.Type=="mainint") then error("Can only create an interface on the multi obj") return false end
|
||||
local c = {}
|
||||
setmetatable(c, self)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Type="int"
|
||||
c.Mainloop={}
|
||||
c.Tasks={}
|
||||
c.Tasks2={}
|
||||
c.Garbage={}
|
||||
c.Children={}
|
||||
c.Paused={}
|
||||
c.Active=true
|
||||
c.Id=-1
|
||||
c.Rest=0
|
||||
c.Jobs={}
|
||||
c.queue={}
|
||||
c.jobUS=2
|
||||
function c:Start()
|
||||
if self.l then
|
||||
self.l:Resume()
|
||||
else
|
||||
self.l=self.Parent:newLoop(function(dt) c:uManager(dt) end)
|
||||
end
|
||||
end
|
||||
function c:Stop()
|
||||
if self.l then
|
||||
self.l:Pause()
|
||||
end
|
||||
end
|
||||
function c:Remove()
|
||||
self:Destroy()
|
||||
self.l:Destroy()
|
||||
end
|
||||
if file then
|
||||
multi.Cself=c
|
||||
loadstring("interface=multi.Cself "..io.open(file,"rb"):read("*all"))()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStack(file)
|
||||
local c=self:newInterface()
|
||||
c.Type="stack"
|
||||
c.last={}
|
||||
c.funcE={}
|
||||
if file then
|
||||
multi.Cself=c
|
||||
loadstring("stack=multi.Cself "..io.open(file,"rb"):read("*all"))()
|
||||
end
|
||||
function c:OnStackCompleted(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Helpers
|
||||
function multi:protect()
|
||||
function self:Do_Order()
|
||||
local Loop=self.Mainloop
|
||||
for _D=#Loop,1,-1 do
|
||||
if Loop[_D]~=nil then
|
||||
Loop[_D].Id=_D
|
||||
local status, err=pcall(Loop[_D].Act,Loop[_D])
|
||||
if err and not(Loop[_D].error) then
|
||||
Loop[_D].error=err
|
||||
print(err..": Ingoring error continuing...")
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:unProtect()
|
||||
function self:Do_Order()
|
||||
for _D=#Loop,1,-1 do
|
||||
if Loop[_D]~=nil then
|
||||
Loop[_D].Id=_D
|
||||
Loop[_D]:Act()
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:setJobSpeed(n)
|
||||
self.jobUS=n
|
||||
end
|
||||
function multi:hasJobs()
|
||||
return #self.Jobs>0,#self.Jobs
|
||||
end
|
||||
function multi:getJobs()
|
||||
return #self.Jobs
|
||||
end
|
||||
function multi:removeJob(name)
|
||||
for i=#self.Jobs,1,-1 do
|
||||
if self.Jobs[i][2]==name then
|
||||
table.remove(self.Jobs,i)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:connectFinal(func)
|
||||
if self.Type=="event" then
|
||||
self:OnEvent(func)
|
||||
elseif self.Type=="alarm" then
|
||||
self:OnRing(func)
|
||||
elseif self.Type=="step" or self.Type=="tstep" then
|
||||
self:OnEnd(func)
|
||||
elseif self.Type=="loop" then
|
||||
self:OnBreak(func)
|
||||
else
|
||||
error("No final event exists for: "..self.Type)
|
||||
end
|
||||
end
|
||||
function multi:Break()
|
||||
self:Pause()
|
||||
self.Active=nil
|
||||
for i=1,#self.ender do
|
||||
self.ender[i](self)
|
||||
end
|
||||
end
|
||||
function multi:OnBreak(func)
|
||||
table.insert(self.ender,func)
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=false
|
||||
if not(n) then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Pause()
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
else
|
||||
if n==nil then
|
||||
self.Active=false
|
||||
if self.Parent.Mainloop[self.Id]~=nil then
|
||||
table.remove(self.Parent.Mainloop,self.Id)
|
||||
table.insert(self.Parent.Paused,self)
|
||||
self.PId=#self.Parent.Paused
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=true
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Resume()
|
||||
end
|
||||
else
|
||||
if self:isPaused() then
|
||||
table.remove(self.Parent.Paused,self.PId)
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Id=#self.Parent.Mainloop
|
||||
self.Active=true
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:resurrect()
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Active=true
|
||||
end
|
||||
function multi:Destroy()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Destroy()
|
||||
end
|
||||
else
|
||||
for i=1,#self.Parent.Mainloop do
|
||||
if self.Parent.Mainloop[i]==self then
|
||||
table.remove(self.Parent.Mainloop,i)
|
||||
break
|
||||
end
|
||||
end
|
||||
self.Active=false
|
||||
end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=self.Parent:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=self.Parent:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then
|
||||
for _k=1,#self.Parent.Tasks2 do
|
||||
if self.Parent.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Parent.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
else
|
||||
for _k=1,#self.Tasks2 do
|
||||
if self.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
end
|
||||
function multi:Reset(n)
|
||||
self:Resume()
|
||||
end
|
||||
function multi:isDone()
|
||||
return self.Active~=true
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.Task)
|
||||
m:addBlock(self.func)
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="alarm"
|
||||
c.timer=self:newTimer()
|
||||
c.set=set or 0
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.set)
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if self.timer:Get()>=self.set then
|
||||
self:Pause()
|
||||
self.Active=false
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Resume()
|
||||
self.Parent.Resume(self)
|
||||
self.timer:Resume()
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self:Resume()
|
||||
self.timer:Reset()
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:Pause()
|
||||
self.timer:Pause()
|
||||
self.Parent.Pause(self)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
c:Pause()
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
table.remove(self.Parent.Mainloop,#self.Parent.Mainloop)
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
else
|
||||
c.timer:Start()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTimer()
|
||||
local c={}
|
||||
c.Type="timer"
|
||||
c.time=0
|
||||
c.count=0
|
||||
function c:Start()
|
||||
self.time=multi.clock()
|
||||
end
|
||||
function c:Get()
|
||||
return (multi.clock()-self.time)+self.count
|
||||
end
|
||||
c.Reset=c.Start
|
||||
function c:Pause()
|
||||
self.time=self:Get()
|
||||
end
|
||||
function c:Resume()
|
||||
self.time=multi.clock()-self.time
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
self.count=self.count+self:Get()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.count)
|
||||
m:tofile(path)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(self.Tasks,func)
|
||||
end
|
||||
function multi:newLoop(func)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="loop"
|
||||
c.Start=multi.clock()
|
||||
if func then
|
||||
c.func={func}
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.func)
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](multi.clock()-self.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
think=1
|
||||
c.Type="step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.funcS={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.func)
|
||||
m:addBlock(self.funcE)
|
||||
m:addBlock(self.funcS)
|
||||
m:addBlock({pos=self.pos,endAt=self.endAt,skip=self.skip,spos=self.spos,count=self.count,start=self.start})
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
self:Pause()
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
think=1
|
||||
c.Type="tstep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=multi.clock()
|
||||
c.set=set or 1
|
||||
c.funcS={}
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=multi.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.func)
|
||||
m:addBlock(self.funcE)
|
||||
m:addBlock(self.funcS)
|
||||
m:addBlock({pos=self.pos,endAt=self.endAt,skip=self.skip,timer=self.timer,count=self.count,start=self.start,set=self.set})
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if multi.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
self:Pause()
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=multi.clock()
|
||||
self:Resume()
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c={}
|
||||
c.Type="trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.trigfunc)
|
||||
m:tofile(path)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newConnection()
|
||||
local c={}
|
||||
c.Type="connector"
|
||||
c.func={}
|
||||
function c:Fire(...)
|
||||
for i=1,#self.func do
|
||||
t,e=pcall(self.func[i],...)
|
||||
if not(t) then
|
||||
print(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:bind(t)
|
||||
self.func=t
|
||||
end
|
||||
function c:connect(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.func)
|
||||
m:tofile(path)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newJob(func,name)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Parent=self
|
||||
c.Type="job"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Act()
|
||||
self:trigfunc(self)
|
||||
end
|
||||
table.insert(self.Jobs,{c,name})
|
||||
if self.JobRunner==nil then
|
||||
self.JobRunner=self:newAlarm(self.jobUS)
|
||||
self.JobRunner:OnRing(function(self)
|
||||
if #self.Parent.Jobs>0 then
|
||||
if self.Parent.Jobs[1] then
|
||||
self.Parent.Jobs[1][1]:Act()
|
||||
table.remove(self.Parent.Jobs,1)
|
||||
end
|
||||
end
|
||||
self:Reset(self.Parent.jobUS)
|
||||
end)
|
||||
end
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
rawset(self,"Start",multi.clock())
|
||||
while self.Active do
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(self,dt)
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
if dt then
|
||||
self.pump=true
|
||||
end
|
||||
self.pumpvar=dt
|
||||
rawset(self,"Start",multi.clock())
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
if self.Active then
|
||||
self:oneTime(self._tFunc,self,dt)
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user