--执行时才确定关系的结点

--[[
 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()
--]]




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值