• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

lua版本的一个状态机

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

这个状态机http://www.cnblogs.com/flytrace/p/5587033.html的lua版本

-- LUA 有实现枚举值的好办法么
local sc_enum = 
{
    -- event flow be broke up
    none        = "none",
    -- event flow pass througth next state
    forward     = "forward",
    -- deffer a speciafic event
    --deffer    = 4



    -- select by default(the first position in the list)
    shallow     = "shallow", 
    -- the state will remain actived state that last time was
    deep        = "deep", 



    -- 不实现。该模式定义不良好。多个嵌套的子并行状态相对复杂,流程不清晰,数据结构也不直接。
    -- 还没找到比较良好的并行状态语义。
    -- only one substate can run at one time by default 
    singleman   = "singleman", 
    -- the state will be enabled always, substate in parallel region can't transit to none-parallel region
    parallel    = "parallel", 

}

return sc_enum

 

-- Date 2016-7-13
-- yanfei

--[[
    一个sc_fsm也是一个sc_state,状态机本身也可以作为一个状态,但目前子状态机只能视为状态,未能作为独立的机制运行
    sc_fsm
    {
        type2states         -- save all states accord to their name in a dictionary
    }
--]]
sc_helper = require("fsm.sc_helper")
require("utils.sc_list")
local sc_queue = require("utils.sc_queue")
local sc_enum = require("fsm.sc_enum")
local sc_state = require("fsm.sc_state")
local classprototype = require("utils.middleclass")
-- a fsm is a state 
local fsm = classprototype("sc_fsm", sc_state)

-- 建立类型-状态表
local function buildstates(fsm, astate)
    fsm.type2states[astate.statename] = astate
    if astate.substates then 
        for statename_, state_ in ipairs(astate.substates) do
            assert(fsm.type2states[state_.statename] == nil, string.format("[%s]: has a state named [%s] already!", fsm.statename, state_.statename))
            buildstates(fsm, state_)
        end
    end
end

-- 重载initialize
-- @statename_      状态名字
-- @basestate_      父状态
-- @statemode_      运行模式
-- @operation_      激活操作
function fsm:initialize(statename_, basestate_, statemode_, operation_)
    sc_state.initialize(self, statename_, basestate_, statemode_, operation_)
end

function fsm:init(initstatename)
    assert(type(self) == 'table', "Make sure that you are using 'fsm:init' instead of 'fsm.init'")

    self.type2states = {}
    buildstates(self, self)

    local initstate = self.type2states[initstatename]
    assert(initstate and initstate:isInstanceOf(sc_state), string.format("[%s] hasn't state named [%s]", self.statename, initstatename))

    -- 建立主激活列表
    local astate = initstate
    while astate do
        astate.activesubstate = astate.substates and astate.substates[1] or nil
        self.ineststate = astate.activesubstate and astate.activesubstate or astate
        astate = astate.activesubstate
    end

    astate = initstate
    while astate ~= self do
        astate.basestate.activesubstate = astate
        astate = astate.basestate
    end

    astate = self
    while astate.activesubstate ~= nil do
        if astate.activesubstate.enter then astate.activesubstate:enter(self) end
        astate = astate.activesubstate
    end

    if not self.eventqueue then self.eventqueue = sc_queue:newqueue() end
    self.suspend = false
    self.__lockqueue = false
    self.__transiting = false

    return true
end

-- 跳转到目标状态
function fsm:transit( targetname_ )
    assert(type(self) == 'table', "Make sure that you are using 'fsm:transit' instead of 'fsm.transit'")

    if self.__transiting then
        self.__transiting = false
        return sc_enum.none
    end

    self.__transiting = true;

    local targetstate = self.type2states[targetname_]
    assert(targetstate and targetstate:isInstanceOf(sc_state), string.format("[%s] hasn't state named [%s]", self.statename, targetname_))

    -- detect the whole active state list
    local astate = self.ineststate
    -- 
    local bstate = targetstate
    local dis = astate.depth - bstate.depth
    if dis < 0 then 
        bstate, astate = astate, targetstate
    end -- bstate is topper

    dis = math.abs(dis)
    for i = 1, dis do astate = astate.basestate end --now they are both same depth

    if astate == bstate then -- is family
        self.__transiting = false
        return sc_enum.none
    end

    -- find  nearest ancestor both
    while astate ~= bstate do
        astate, bstate = astate.basestate, bstate.basestate
    end

    -- first we will call exit chain
    while self.ineststate ~= astate do
        if self.ineststate.exit then self.ineststate:exit(self) end
        self.ineststate = self.ineststate.basestate
    end

    -- now we will decide the enter chain

     -- build active chain down side
    local astate = targetstate
    while astate do
        astate.activesubstate =  (astate.substates and astate.statemode == sc_enum.shallow) 
                                 and astate.substates[1] or astate.activesubstate
        self.ineststate = astate.activesubstate and astate.activesubstate or astate
        astate = astate.activesubstate
    end

    -- build active chain up side
    astate = targetstate
    while astate ~= self do
        astate.basestate.activesubstate, astate = astate, astate.basestate
    end

    -- call enter chain
    while bstate.activesubstate ~= nil do
        if bstate.activesubstate.enter then bstate.activesubstate:enter(self) end
        bstate = bstate.activesubstate
    end

    return sc_enum.none
end

function fsm:process_event( eventname_, ...)
    assert(type(self) == 'table', "Make sure that you are using 'fsm:process_event' instead of 'fsm.process_event'")
    if self.suspend then return nil end
    if self.__lockqueue then return nil end
    
    local pevent = {...}
    pevent.eventname = eventname_
    if not self.eventqueue then self.eventqueue = sc_queue:newqueue() end
    local queue = self.eventqueue
    queue:push_back(pevent)

    self.__lockqueue = true
    --local eventcount = queue:count()
    while true do
        --eventcount = eventcount - 1
        local astate = self
        local processflag_ = sc_enum.forward
        pevent = queue:pop_front() 
        if not pevent then break end
        while astate.activesubstate ~= nil do
            processflag_ = astate.activesubstate:_internal_process(self, pevent)
            if processflag_ == sc_enum.none then 
                return processflag_ 
           end
           astate = astate.activesubstate
        end
    end

    self.__lockqueue = false

    return processflag_
end

 -- 
function fsm:post_event( eventname_, ... )
    local pevent = {...}
    pevent.eventname = eventname_
    self.eventqueue:push_back(pevent)
end

function fsm:context( statename_ )
    local targetstate = self.type2states[targetname_]
    --assert(targetstate and targetstate:isInstanceOf(sc_state), string.format("[%s] hasn't state named [%s]", self.statename, targetname_))
    return targetstate
end

return fsm
-- Date 2016-7-13
-- yanfei

--[[
--  sc_state
--  {
        statename,          -- 状态名
        basestate,          -- 父状态
        statemode,          -- 历史模式
        operation,          -- 操作,完成并行,筛选。目前保留字段

        substates,          -- 子状态列表
        activestate,        -- 当前激活状态

        events,,            -- event-callback表
--  }
--  一个状态是一个节点,basestate指向父状态,substates指向子状态列表,所有状态形成一棵树
--]]

require("utils.sc_list")
local sc_enum = require("fsm.sc_enum")
--require("fsm.sc_event")
local classprototype = require("utils.middleclass")
local sc_state = classprototype("sc_state")

local rawget = rawget
local rawset = rawset
local string = string

-- 重载initialize
-- @statename_      状态名字
-- @basestate_      父状态
-- @statemode_      运行模式
-- @operation_      激活操作
function sc_state:initialize(statename_, basestate_, statemode_, operation_)
    assert(type(statename_) == "string","statename must be a string")
    self.statename = statename_
    self.statemode = statemode_ or sc_enum.shallow
    --self.operation = operation_ or sc_enum.singleman

    -- 处理父子关系
    assert(basestate_ and basestate_:isInstanceOf(sc_state) or basestate_ == nil, string.format("[%s]: base state must be a sc_state", statename_))
    self.basestate = basestate_
    if basestate_ then basestate_:addsubstate(self) end

    -- 状态深度 top state is 1
    self.depth = self.basestate and self.basestate.depth + 1 or 1

end

-- 创建该状态的子状态
function sc_state:createstate(statename_, statemode_, operation_)
    return sc_state:new(statename_, self, statemode_, operation_)
end

-- 添加子状态
function sc_state:addsubstate(...)
    -- 填充子状态列表
    self.substates = self.substates or {}
    for i, state_ in ipairs{...} do 
        assert(state_:isInstanceOf(sc_state), "substate must be a sc_state")
        assert(self.substates[state_.statename] == nil, string.format("%s has a substate named [%s] already!", self.statename, state_.statename))
        table.insert(self.substates, state_)
    end
end

-- 绑定事件
-- @eventname_  事件名
-- @callbackname_   事件处理函数名
function sc_state:bind_event(eventname_, callbackname_)
    assert(type(self) == 'table', "Make sure that you are using 'sc_state:process_event' instead of 'sc_state.process_event'")
    assert(type(eventname_) == 'string', "Make sure that callbakname is a string")
    assert(type(callbackname_) == 'string', "Make sure that callbakname is a string")
    --local callback_ =  self[callbackname_]
    local callback_ = rawget(self, callbackname_)
    assert(callback_, string.format("[%s] bind_event event[%s]: hasn't a function named [%s]", self.statename, eventname_, callbackname_))
    assert(type(callback_) == "function", string.format("[%s]:bind_event event[%s]: callback must be a function", self.statename, eventname_))

    self.events = self.events or {}
    self.events[eventname_] = function(fsm, ...) return callback_(self, fsm, ...) end
end

function sc_state:_internal_process(fsm, pevent)
    if not self.events then return sc_enum.forward end
    local callback_ = self.events[pevent.eventname]
    if not callback_ then return sc_enum.forward end
    return callback_(fsm, unpack(pevent))
end

return sc_state

 

--region *.lua
--Date 2016.7.17
-- yanfei

local queue = {}

function queue:newqueue()
    local o = {first = 0, last = -1}
    setmetatable(o, self)
    self.__index = self
    return o
end

function queue:push_front(v)
    local first = self.first - 1
    self.first = first
    self[first] = v
end

function queue:pop_front()
    local first = self.first
    if first > self.last then return nil end
    local v = self[first]
    self[first] = nil
    self.first = first + 1
    return v
end

function queue:push_back(v)
    local last = self.last + 1
    self.last = last
    self[last] = v
end

function queue:pop_back()
    local last = self.last
    if last < self.first then return nil end
    local v = self[last]
    self[last] = nil
    self.last = last - 1
    return v
end

function queue:count()
    return self.last - self.first + 1
end

return queue
--endregion
--region *.lua

module ("sc_list", package.seeall)

--- An iterator over the elements of a list.
-- @param l list to iterate over
-- @return iterator function which returns successive elements of the list
-- @return the list <code>l</code> as above
-- @return <code>true</code>
function elems (l)
  local n = 0
  return function (l)
           n = n + 1
           if n <= #l then
             return l[n]
           end
         end,
  l, true
end

--- An iterator over the elements of a list, in reverse.
-- @param l list to iterate over
-- @return iterator function which returns precessive elements of the list
-- @return the list <code>l</code> as above
-- @return <code>true</code>
function relems (l)
  local n = #l + 1
  return function (l)
           n = n - 1
           if n > 0 then
             return l[n]
           end
         end,
  l, true
end

--- Fold a binary function into an iterator.
-- @param f function
-- @param d initial first argument
-- @param i iterator
-- @return result
function fold_x (f, d, i, t)
  local r = d
  for e in i (t) do
    r = f (r, e)
  end
  return r
end

function fold (f, d, t)
  local r = d
  for e in elems (t) do
    r = f (r, e)
  end
  return r
end

----- Fold a binary function through a list right associatively.
---- @param f function
---- @param e element to place in right-most position
---- @param l list
---- @return result
--function foldr (f, e, l)
--  return _G.fold (function (x, y) return f (y, x) end,
--                  e, relems, l)
--end

function find(f, t)
    for e in elems (t) do
        if f(e) then return e end
    end
    return nil
end

function findall(f, t)
    local result = {}
    for e in elems(t) do
        if f(e) then result[#result+1] = (e) end
    end
    return result
end

function join(a, b)
    return {unpack(a), unpack(b)}
end 

function cons(a, t)
    return {a, unpack(t)}
end

--endregion

 

 

使用

-- 只是一个例子

local sc_enum = require("fsm.sc_enum")
local sc_fsm_pt = require("fsm.sc_fsm")

local gamemain_fsm = sc_fsm_pt:new("gamemain_fsm")

local print = print

--[[
--  gamemain_state
--]]
local gamemain_state = gamemain_fsm:createstate("gamemain_state")
function gamemain_state:enter(fsm)
    print("enter "..self.statename)
end

function gamemain_state:exit(fsm)
    print("exit "..self.statename)
end

function gamemain_state:update(fsm, delta)
    print("update "..self.statename)
    print("timedelta: "..tostring(delta))
end

gamemain_state:bind_event("game_update", "update")

--[[
--  gamemain_init_state
--]]
local gamemain_init_state = gamemain_state:createstate("gamemain_init_state")

function gamemain_init_state:enter(fsm)
    print("enter "..self.statename)
end

function gamemain_init_state:exit(fsm)
    print("exit "..self.statename)
end

--[[
--  gamemain_init_waiting_state
--]]
local gamemain_init_waiting_state = gamemain_init_state:createstate("gamemain_init_waiting_state")

function gamemain_init_waiting_state:enter(fsm)
    print("enter "..self.statename)
end

function gamemain_init_waiting_state:exit(fsm)
    print("exit "..self.statename)
end


--[[
--  gamemain_idle_state
--]]
local gamemain_idle_state = gamemain_state:createstate("gamemain_idle_state")

function gamemain_idle_state:enter(fsm)
    print("enter "..self.statename)
end

function gamemain_idle_state:exit(fsm)
    print("exit "..self.statename)
end

function gamemain_idle_state:update(fsm, delta)
    print("update "..self.statename)
    print("timedelta: "..tostring(delta))
    --fsm:process_event("game_upadte", 0.0001)
end

function gamemain_idle_state:tolobby(fsm, msg1, msg2)
    print(msg1..' '..msg2)
    print("ready to taransit to lobby") 
    return fsm:transit("gamemain_lobby_state")
end

gamemain_idle_state:bind_event("game_update", "update")
gamemain_idle_state:bind_event("event_tolobby", "tolobby")
--[[
--  gamemain_net_state
--]]
local gamemain_net_state = gamemain_state:createstate("gamemain_net_state")
function gamemain_net_state:enter(fsm)
    print("ent 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
OpenResty扩展库之(一)——lua-resty-shell库发布时间:2022-07-22
下一篇:
lua os.date函数定义和示例发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap