local c=require("\084\101\115\116\077\097\105\110")local K=c.game local w=c.habitat local Z=c.ReplicatedStorage local m=Z:GetChildren()for c,K in pairs(m)do print(K.Name)end local D=Z.Nevermore print(D)local C=w.require("\078\101\118\101\114\109\111\114\101")local P=C("\083\116\097\116\101\077\097\099\104\105\110\101\077\097\099\104\105\110\101")local n={ReadyForSecondStateA=false}local N=false local J=P.NewStateMachine()local t=P.NewState("\070\105\114\115\116\083\116\097\116\101")t.NextStatesNames={"\083\101\099\111\110\100\083\116\097\116\101\065";"\083\101\099\111\110\100\083\116\097\116\101\066"}t.InternalData=n function t.CanTransitionFrom(w,c,K)if c~=nil and c.Name~=nil then print(" \080\114\101\118\105\111\117\115 \083\116\097\116\101\058 "..c.Name)end return c.Name=="\070\111\117\114\116\104\083\116\097\116\101"end function t.Action(c)c.InternalData.ReadyForSecondStateA=true c:PrintInternalDataTable()end function t.CanPerformAction(c)return true end function t.StateStoppedCallback(c)N=true print("\070\105\114\115\116 \115\116\097\116\101 \099\111\109\112\108\101\116\101\100\063 "..tostring(N))end local H=P.NewState("\083\101\099\111\110\100\083\116\097\116\101\065")H.NextStatesNames={"\084\104\105\114\100\083\116\097\116\101"}H.InternalData=n function H.CanTransitionFrom(w,c,K)if c.Name=="\070\105\114\115\116\083\116\097\116\101"and K.ReadyForSecondStateA then return true end return false end function H.Action(c)c.InternalData.ReadyForThirdState=true c:PrintInternalDataTable()end function H.CanPerformAction(c)return true end function H.StateStoppedCallback(c)print("\083\101\099\111\110\100\083\116\097\116\101\065 \115\116\111\112\112\101\100")end local T=P.NewState("\083\101\099\111\110\100\083\116\097\116\101\066")T.NextStatesNames={"\084\104\105\114\100\083\116\097\116\101"}T.InternalData=n function T.CanTransitionFrom(w,c,K)if K.ReadyForSecondStateB then return true end return false end function T.Action(c)c.InternalData.ReadyForThirdState=false c:PrintInternalDataTable()end function T.CanPerformAction(c)return true end local I=P.NewState("\084\104\105\114\100\083\116\097\116\101")I.NextStatesNames={"\070\105\114\115\116\083\116\097\116\101"}I.InternalData=n function I.CanTransitionFrom(w,c,K)print(" -- \068\097\116\097 \112\097\115\115\101\100 \116\111 \116\104\105\114\100 \115\116\097\116\101\058")for c,K in pairs(K)do print(tostring(c)..("\058 "..tostring(K)))end if K.ReadyForThirdState then return true end return false end function I.Action(c)c.InternalData.ReadyForFirstState=true c:PrintInternalDataTable()end function I.CanPerformAction(c)return true end function I.StateStoppedCallback(c)print("\084\104\105\114\100 \115\116\097\116\101 \115\116\111\112\112\101\100")end J:AddState(t)J:AddState(H)J:AddState(T)J:AddState(I)J.CurrentState=t local q={}J:Update(q)print("\042\042\042 \082\117\110\110\105\110\103 \115\101\099\111\110\100 \117\112\100\097\116\101 \042\042\042")local u={ReadyForSecondStateA=true}J:Update(u)print("\042\042\042 \082\117\110\110\105\110\103 \116\104\105\114\100 \117\112\100\097\116\101 \042\042\042")J:Update({ReadyForThirdState=true})J:Update(n)assert(J.CurrentState.Name=="\084\104\105\114\100\083\116\097\116\101")
-- This might have been installed in Lua 5.1 .luarocks so make sure to set the version correctly

local _testMain = require("TestMain")
local game = _testMain.game
local habitat = _testMain.habitat
local ReplicatedStorage = _testMain.ReplicatedStorage

local replicatedStorageChildren = ReplicatedStorage:GetChildren()

for _, child in pairs(replicatedStorageChildren) do
    print(child.Name)
end
local modScript = ReplicatedStorage.Nevermore
print(modScript)

local nevermoreRequire = habitat.require("Nevermore")

local StateMachineMachine = nevermoreRequire("StateMachineMachine")

local internalSharedData = {
    ReadyForSecondStateA = false
}
local firstStateCompleted = false

local stateMachine = StateMachineMachine.NewStateMachine()

local firstState = StateMachineMachine.NewState("FirstState")
firstState.NextStatesNames = { "SecondStateA", "SecondStateB" }
firstState.InternalData = internalSharedData
function firstState:CanTransitionFrom(previousState, data)
    if previousState ~= nil and previousState["Name"] ~= nil then
        print(" Previous State: " .. previousState.Name)
    end
    -- This ensures it won't loop infinitely
    return previousState.Name == "FourthState"
end
function firstState:Action()
    self.InternalData.ReadyForSecondStateA = true
    self:PrintInternalDataTable()
end
function firstState:CanPerformAction()
    return true
end
function firstState:StateStoppedCallback()
    firstStateCompleted = true
    print("First state completed? "  .. tostring(firstStateCompleted))
end

local secondStateA = StateMachineMachine.NewState("SecondStateA")
secondStateA.NextStatesNames = { "ThirdState" }
secondStateA.InternalData = internalSharedData
function secondStateA:CanTransitionFrom(previousState, data)
    if previousState.Name == "FirstState" and data["ReadyForSecondStateA"] then
        return true
    end
    return false
end
function secondStateA:Action()
    self.InternalData.ReadyForThirdState = true
    self:PrintInternalDataTable()
end
function secondStateA:CanPerformAction()
    return true
end
function secondStateA:StateStoppedCallback()
    print("SecondStateA stopped")
end

local secondStateB = StateMachineMachine.NewState("SecondStateB")
secondStateB.NextStatesNames = { "ThirdState" }
secondStateB.InternalData = internalSharedData
function secondStateB:CanTransitionFrom(previousState, data)
    if data["ReadyForSecondStateB"] then
        return true
    end
    return false
end
function secondStateB:Action()
    self.InternalData.ReadyForThirdState = false
    self:PrintInternalDataTable()
end
function secondStateB:CanPerformAction()
    return true
end

local thirdState = StateMachineMachine.NewState("ThirdState")
thirdState.NextStatesNames = { "FirstState" }
thirdState.InternalData = internalSharedData
function thirdState:CanTransitionFrom(previousState, data)
    print(" -- Data passed to third state:")
    for name, thing in pairs(data) do
        print(tostring(name) .. ": " .. tostring(thing))
    end
    if data["ReadyForThirdState"] then
        return true
    end
    return false
end
function thirdState:Action()
    self.InternalData.ReadyForFirstState = true
    self:PrintInternalDataTable()
end

function thirdState:CanPerformAction()
    return true
end
function thirdState:StateStoppedCallback()
    print("Third state stopped")
end

stateMachine:AddState(firstState)
stateMachine:AddState(secondStateA)
stateMachine:AddState(secondStateB)
stateMachine:AddState(thirdState)
stateMachine.CurrentState = firstState

local firstRunData = {}
stateMachine:Update(firstRunData)
print("*** Running second update ***")

local inputData = { ReadyForSecondStateA = true }
stateMachine:Update(inputData)
-- Should be ready for third state
print("*** Running third update ***")
stateMachine:Update({ ReadyForThirdState  = true})

-- Should re-run third state
stateMachine:Update(internalSharedData)

assert(stateMachine.CurrentState.Name == "ThirdState")