-- 관계식의 결점을 결정하기 전에 수행

--[[
 file name :    GameEtNode.lua
 author  :      Clark/   
 created :      8:30:2011
 purpose :     	  
--]]


function Node_NULFun()
	--return nil		--     
	--return false 		--     。
	return true			--     
end

function ToNodeSet(_fun, _tPar)
	function NodeFun()
		if nil == _tPar then
			return _fun()
		end
		local dLen = table.getn(_tPar)
		if 0 == dLen then
			return _fun()
		elseif 1 == dLen then
			return _fun(_tPar[1])
		elseif 2 == dLen then
			return _fun(_tPar[1], _tPar[2])
		elseif 3 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3])
		elseif 4 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4])
		elseif 5 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5])
		elseif 6 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6])
		elseif 7 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7])
		elseif 8 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8])
		elseif 9 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9])
		elseif 10 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9], _tPar[10])
		elseif 11 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9], _tPar[10], _tPar[11])
		elseif 12 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9], _tPar[10], _tPar[11], _tPar[12])
		elseif 13 == dLen then
			return _fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9], _tPar[10], _tPar[11], _tPar[12], _tPar[13])
		else
			myPrint("ToNodeFun   ",1)	--------------------------------------------------------
			return true
		end
	end
	return NodeFun
end

--                     
function ToNodeFun(_fun, _tPar)
	function NodeFun()
		if nil == _fun then
			return true
		end
		if nil == _tPar then
			_fun()
			return true
		end
		local dLen = table.getn(_tPar)
		if 0 == dLen then
			_fun()
		elseif 1 == dLen then
			_fun(_tPar[1])
		elseif 2 == dLen then
			_fun(_tPar[1], _tPar[2])
		elseif 3 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3])
		elseif 4 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4])
		elseif 5 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5])
		elseif 6 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6])
		elseif 7 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7])
		elseif 8 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8])
		elseif 9 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9])
		elseif 10 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9], _tPar[10])
		elseif 11 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9], _tPar[10], _tPar[11])
		elseif 12 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9], _tPar[10], _tPar[11], _tPar[12])
		elseif 13 == dLen then
			_fun(_tPar[1], _tPar[2], _tPar[3], _tPar[4], _tPar[5], _tPar[6], _tPar[7], _tPar[8], _tPar[9], _tPar[10], _tPar[11], _tPar[12], _tPar[13])
		else
			myPrint("ToNodeFun   ",1)	--------------------------------------------------------
		end
		return true
	end
	return NodeFun
end



--             
function s_e_DoingNode(_funGetNode, _tPar)
	--API_Trace("s_e_DoingNode")
	local bDone = false
	local thisProtected = {}
	thisProtected.ndTempS = nil
	thisProtected.ndTempE = nil
	function thisProtected.doNode()
		if bDone then
			--       ,                   
			return
		end
		--myPrint("s_e_DoingNode.doNode",1)
		thisProtected.fun = ToNodeSet(_funGetNode, _tPar)
		bDone = true
		thisProtected.ndTempS, thisProtected.ndTempE = thisProtected.fun()
		--myPrint("fun ndTempS Count:" .. thisProtected.ndTempS.getNode(), 1)
		if nil == thisProtected.ndTempE then
			thisProtected.ndTempE = newFunNode()
			thisProtected.ndTempS.pushChild(thisProtected.ndTempE)
		end
		thisProtected.ndS.pushChild(thisProtected.ndTempS)
		thisProtected.ndTempE.pushChild(thisProtected.ndE)
	end
	thisProtected.ndS = newFunNode(ToNodeFun(thisProtected.doNode))	--  doNode ndS       
	thisProtected.ndE = newFunNode()
	local oldFun = thisProtected.ndS.delete
	function thisProtected.ndS.delete()
		--API_Trace("s_e_DoingNode.delete")
		if false == bDone then
			--API_Trace("s_e_DoingNode.delete bDone == false")
			thisProtected.ndS.pushChild(thisProtected.ndE)
		end
		oldFun()
	end
	return thisProtected.ndS, thisProtected.ndE
end

G_NODE_COUNT = 0
function newBaseNode()
	G_NODE_COUNT = G_NODE_COUNT + 1
	--myPrint(" G_NODE_COUNT: " .. G_NODE_COUNT, 1)
	local m_private = { m_tChildNode = {} }
	local m_protected = {}
	local m_public = {}

	function m_public.getSize()
		local dSum = 0
		local dLen = table.getn(m_private.m_tChildNode)
		for i=1, dLen do
			if nil ~= m_private.m_tChildNode[i] then
				dSum = dSum + m_private.m_tChildNode[i].getSize()
			end
		end
		dSum = dSum + 1
		return dSum
	end


	--   
	function m_public.init()
		--  
		m_protected.initThis()
		m_protected.initChild()
	end
	function m_protected.initChild()
		local dLen = table.getn(m_private.m_tChildNode)
		for i=1, dLen do
			if nil ~= m_private.m_tChildNode[i] then
				m_private.m_tChildNode[i].init()
			end
		end
	end
	function m_protected.initThis()
		myPrint("initThis     ",1)	--------------------------------------------------------
	end

	--  
	function m_public.run()
		--  
		local bRet = m_protected.bool_runThis()
		if bRet then
			--myPrint("        ",1)
			m_protected.runChild()
		else
			--myPrint("           ",1)
		end
	end
	function m_protected.runChild()
		local dLen = table.getn(m_private.m_tChildNode)
		for i=1, dLen do
			if nil ~= m_private.m_tChildNode[i] then
				m_private.m_tChildNode[i].run()
			end
		end
	end
	function m_protected.bool_runThis()
		myPrint("bool_runThis     ",1)	--------------------------------------------------------
		return true
	end

	--     
	function m_public.pushChild(_nextNode)
		local dLen = table.getn(m_private.m_tChildNode)
		m_private.m_tChildNode[dLen+1] = _nextNode
	end
	--  
	--      ,                    ,           (                   )
	function m_public.start()
		--myPrint("     ",1)
		m_public.init()
		m_public.run()
		return true
	end
	function m_public.deleteChild()
		local dLen = table.getn(m_private.m_tChildNode)
		for i=1, dLen do
			if nil ~= m_private.m_tChildNode[i] then
				--myPrint("deleteChild",1)
				m_private.m_tChildNode[i].delete()
				m_private.m_tChildNode[i] = nil
			end
		end
	end
	m_private.bNew = true
	function m_public.delete()
		--myPrint("delete",1)
		if m_private.bNew then
			m_public.deleteChild()
			m_public.init()
			G_NODE_COUNT = G_NODE_COUNT - 1
			if G_NODE_COUNT <= 0 then
				myPrint(" G_NODE_COUNT: " .. G_NODE_COUNT, 1)
			end
			m_private.bNew = false
		end
	end
	function m_public.setChildNull(_nd)
		local dLen = table.getn(m_private.m_tChildNode)
		for i=1, dLen do
			if _nd == m_private.m_tChildNode[i] then
				myPrint("setChildNull",1)
				m_private.m_tChildNode[i] = nil
			end
		end
	end
	function m_public.getNode()
		local iCount = 0
		local dLen = table.getn(m_private.m_tChildNode)
		for i=1, dLen do
			if nil ~= m_private.m_tChildNode[i] then
				iCount = iCount + m_private.m_tChildNode[i].getNode()
			end
		end
		iCount = iCount + 1
		return iCount
	end
	return m_public, m_protected
end

--    
function newFunNode(_onFun)
	_onFun = _onFun  or Node_NULFun
	local m_public, m_protected  = newBaseNode()
	function m_protected.initThis()
	end
	function m_protected.bool_runThis()
		--    
		if nil ~= _onFun then
			local bRet = _onFun()
			if false == bRet then
				return false
			end
			return true
		end
		return true
	end
	return m_public
end


--     
function newTgrNode()
	local m_public, m_protected  = newBaseNode()
	m_protected.tgr = nil

	--  
	function m_public.run()
		--myPrint("newTgrNode run",1)
		m_protected.initThis()
		m_protected.bool_runThis()
	end
	--   
	function m_protected.initThis()
		if nil ~= m_protected.tgr then
			--myPrint("initThis:    tgr",1)
			m_protected.tgr = m_protected.tgr.nil_deleteTrigger()
		end
	end
	function m_protected.bool_runThis()
		myPrint("       ",1)
		return true
	end
	return m_public, m_protected
end



--            (        )
function DieTgrPar(_pSomething)
	function doDieTgrPar()
		return _pSomething
	end
	return doDieTgrPar
end
--       
function newDieTrgNode(_tgrparFun, _onFun)
	local m_public, m_protected = newTgrNode()

	function m_protected.bool_runThis()
		local objSomething = _tgrparFun()
		if nil ~= objSomething then
			m_protected.tgr = trigger_newGDieTrigger(objSomething, m_protected.doRun)
		end
	end

	function m_protected.doRun(_Type, _FastID, _KillerType, _KillerID, _MonsterID, _MapID, _PosX, _PosY)
		--         ,  _onFun         ,         
		m_protected.initThis()
		local bRet = _onFun(_Type, _FastID, _KillerType, _KillerID, _MonsterID, _MapID, _PosX, _PosY)
		if nil == bRet then
			m_protected.bool_runThis()
		elseif bRet then
			m_protected.runChild()
		end
	end

	--                LuA     ,                ----
	--                              ,              
	function m_public.pause()
		m_protected.initThis()
	end
	function m_public.play()
		m_protected.bool_runThis()
	end
	------------------------------------------------------------------------------------

	return m_public
end



--            (        )
function TimeTgrPar(_timeStep)
	function doTimeTgrPar()
		if type(Node_NULFun) == type(_dtTime) then
			_timeStep = _timeStep()
		end
		return _timeStep
	end
	return doTimeTgrPar
end
--     
function newTimeTrgNode(_tgrparFun, _onFun)
	local m_public, m_protected = newTgrNode()
	function m_protected.bool_runThis()
		local dTimestep = _tgrparFun()
		m_protected.tgr = trigger_newGTimerTrigger(dTimestep, m_protected.doRun)
	end
	function m_protected.doRun()
		--         ,  _onFun         ,         
		m_protected.initThis()
		local bRet = _onFun()
		if nil == bRet then
			m_protected.bool_runThis()
		elseif bRet then
			m_protected.runChild()
		end
	end
	return m_public
end


--            (        )
function AreaTgrPar(_dMapObj, _dPosX, _dPosY, _dRadius)
	function doAreaTgrPar()
		return _dMapObj, _dPosX, _dPosY, _dRadius
	end
	return doAreaTgrPar
end
--       
function newHeroToAreaTrgNode(_tgrparFun, _onFun)
	local m_public, m_protected = newTgrNode()
	function m_protected.bool_runThis()
		local dMapObj, dPosX, dPosY, dRadius = _tgrparFun()
		m_protected.tgr = trigger_newGHeroAreaTrigger(dMapObj, dPosX, dPosY, dRadius, m_protected.doRun)
	end
	function m_protected.doRun(_HitID)
		--myPrint("_HitType " .. _HitType , 1)
		--         ,  _onFun         ,         
		m_protected.initThis()
		local bRet = _onFun(_HitID)
		if nil == bRet then
			m_protected.bool_runThis()
		elseif bRet then
			--myPrint("doRun" , 1)
			m_protected.runChild()
		end
	end
	return m_public
end
--       
function newMonsterToAreaTrgNode(_tgrparFun, _onFun)
	local m_public, m_protected = newTgrNode()
	function m_protected.bool_runThis()
		local dMapObj, dPosX, dPosY, dRadius = _tgrparFun()
		m_protected.tgr = trigger_newGMonsterAreaTrigger(dMapObj, dPosX, dPosY, dRadius, m_protected.doRun)
	end
	function m_protected.doRun(_HitID)
		--myPrint("_HitType " .. _HitType, 1)
		--         ,  _onFun         ,         
		m_protected.initThis()
		local bRet = _onFun(_HitID)
		if nil == bRet then
			m_protected.bool_runThis()
		elseif bRet then
			--myPrint("newMonsterToAreaTrgNode",  1)
			m_protected.runChild()
		end
	end
	return m_public
end



--_tNode = {{start1, end1}, {start2, end2}...}
function s_e_AndNode(_tNode)
	local m_dLen = table.getn(_tNode)
	--myPrint("s_e_AndNode m_dLen " .. m_dLen, 1)
	local m_ndCount = {}
	local m_ndAnd = {}

	function m_ndAnd.initCount()
		for i=1, m_dLen do
			m_ndCount[i] = false
		end
		return true
	end

	function m_ndAnd.SiglePass(_ID)
		function doSiglePass()
			m_ndCount[_ID] = true
			return true
		end
		return doSiglePass
	end

	function m_ndAnd.AllPass()
		function doAllPass()
			local bAllPass = true
			for i=1, m_dLen do
				if false == m_ndCount[i] then
					bAllPass = false
					break
				end
			end
			--myPrint("s_e_AndNode m_dLen " .. m_dLen, 1)
			return bAllPass
		end
		return doAllPass
	end

	local ndHead = newFunNode(m_ndAnd.initCount)
	local ndEnd = newFunNode(m_ndAnd.AllPass())
	for i=1, m_dLen do
		local ndSiglePass = newFunNode(m_ndAnd.SiglePass(i))
		ndHead.pushChild(_tNode[i][1])
		_tNode[i][2].pushChild(ndSiglePass)
		ndSiglePass.pushChild(ndEnd)
	end

	return ndHead, ndEnd
end

--[[
--     ,       
--      ,                 
function s_e_OrNode(_tNode)
	local ndHead = newFunNode()
	local ndEnd = newFunNode(ToNodeFun(ndHead.init))
	local m_dLen = table.getn(_tNode)
	for i=1, m_dLen do
		ndHead.pushChild(_tNode[i][1])
		_tNode[i][2].pushChild(ndEnd)
	end
	return ndHead, ndEnd
end
--]]

--      ,                       
function s_e_OrNodeEx(_tNode, _funPass)
	_funPass = _funPass or Node_NULFun
	local thisTemp = {}
	thisTemp.NoPassed = true
	function thisTemp.Init()
		--myPrint("s_e_OrNodeEx Init", 1)
		thisTemp.NoPassed = true
		return true
	end
	function thisTemp.IsPass()
		--myPrint("IsPass", 1)
		if thisTemp.NoPassed then
			if nil ~= _funPass then
				if _funPass() then
					thisTemp.NoPassed = false
					return true
				end
			else
				thisTemp.NoPassed = false
				return true
			end
		end
		return false
	end
	local ndHead = newFunNode(thisTemp.Init)
	local ndEnd = newFunNode(thisTemp.IsPass)
	local dLen = table.getn(_tNode)
	for i=1, dLen do
		if nil ~= _tNode[i][1] and nil ~= _tNode[i][2] then
			ndHead.pushChild(_tNode[i][1])
			_tNode[i][2].pushChild(ndEnd)
		elseif nil ~= _tNode[i] and nil == _tNode[i][1] then
			ndHead.pushChild(_tNode[i])
			_tNode[i].pushChild(ndEnd)
		else
			myPrint("s_e_OrNodeEx: _tNode == nil", 1)
		end
	end
	return ndHead, ndEnd
end

--[[
function fun(_szText)
	function doFun()
		print(_szText, 0)
		return true
	end
	return doFun
end

function funF(_szText)
	function doFun()
		print(_szText, 0)
		return false
	end
	return doFun
end

function myPrint(_szText, _type)
	print(_szText)
end

node1 = newFunNode(fun("1"))
node2 = newFunNode(funF("2"))
node3 = newFunNode(fun("3"))
node4 = newFunNode(fun("4"))

local ndAndS, ndAndE = s_e_AndNode({{node1,node1}, {node2,node2}, {node3,node3}})
ndAndE.pushChild(node4)
ndAndS.start()

ndAndS.delete()
--]]




좋은 웹페이지 즐겨찾기