-- flow={ -- level=1, -- step={0}, -- actiu="", -- pila={}, -- paths={ {flow_safe} }, -- registre={} -- -- sub_path_enable = false, -- -- sub_step = 0, -- -- sub_path={} -- } -- function flow:print() -- print("> STEP= "..self.step.." / "..#self.path) -- if self.sub_path_enable then -- print("> SUB_PATH_ENABLE= TRUE") -- else -- print("> SUB_PATH_ENABLE= FALSE") -- end -- print("> SUB_STEP= "..self.sub_step.." / "..#self.sub_path) -- print("") -- end flow={ registre={}, -- {nom {ptr (a l'estat actual), path (llista de funcions ordenada)} ...} actiu = "", -- nom del registre actiu stack = {}, -- guarda els canvis de registre per a poder tornar a l'estat anterior safe_show = false, add_stack = {}, -- pila per a pasar a stack en quan arribe el següent executar } function flow:safe() if not flow.safe_show then print("[FLOW] No hi ha res en la pila") flow.safe_show = true end end function flow:registrar(nom, _path) print("[FLOW] REGISTRAR => "..nom) self.registre[nom]= {ptr=0, path=_path} print(self.registre[nom].ptr) end function flow:borrar(nom) self.registre[nom].ptr=0 self.registre[nom].path=nil end function flow:add(nom) print("[FLOW] add "..nom) self.add_stack[#self.add_stack+1] = nom end function flow:executar(nom, stacking) print("[FLOW] EXEC "..nom) if stacking then -- table.insert(self.stack, self.actiu) self.stack[#self.stack+1] = self.actiu -- print(" APILAT "..self.actiu.."!") if #self.add_stack>0 then for i, v in ipairs(self.add_stack) do self.stack[#self.stack+1] = v end self.add_stack={} end end self.actiu=nom self.registre[self.actiu].ptr=0 self:next() end function flow:next() print("[FLOW] NEXT "..self.actiu); local ptr = self.registre[self.actiu].ptr local steps = #self.registre[self.actiu].path -- print("FLOW_NEXT= "..self.actiu..", "..ptr..", "..steps) if ptr+1>steps then self:finish() else self.registre[self.actiu].ptr = ptr+1 game_update = self.registre[self.actiu].path[self.registre[self.actiu].ptr] end return self.actiu, self.registre[self.actiu].ptr end function flow:finish() print("[FLOW] FINISH "..self.actiu); self.actiu = table.remove(self.stack) print("[FLOW] ACTIU "..self.actiu); if self.actiu==nil then -- no queda res en la pila self.actiu="flow" self.registre[self.actiu].ptr = 1 else -- ultim element afegit a la pila -- No se fa cap acció lo que implica que se restaura l'estat en el -- que s'estava abans de l'ultima cridada -- Per si es un estat afegit amb add if self.registre[self.actiu].ptr ==0 then self.registre[self.actiu].ptr = 1 end end local curr_flow = self.registre[self.actiu] local ptr = self.registre[self.actiu].ptr game_update = self.registre[self.actiu].path[self.registre[self.actiu].ptr] end flow:registrar("flow",{flow.safe})