-- 星曜之门-星耀共鸣
local ObjHuman = require("core.ObjHuman")
local Util = require("common.Util")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local BagLogic = require("bag.BagLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local XingYaoMenExcel = require("excel.xingYaoMen")
local HeroGrid = require("hero.HeroGrid")
local Grid = require("bag.Grid")
local HeroLogic = require("hero.HeroLogic")
local HeroEquip = require("hero.HeroEquip")
local FuwenLogic = require("fuwen.FuwenLogic")
local ItemDefine = require("bag.ItemDefine")
local Log = require("common.Log")
local LogDefine = require("common.LogDefine")

-- human.db.xingYaoGongMing = {
-- 	defList = nil --守卫者
-- 	gridList = nil --保存格子信息
-- 	xingYaoHe = nil --星耀之核
-- }

-- gridList[index] = {
-- 	heroIndex = nil
-- 	cdTime = nil --冷却时间
-- }

-- defList[heroIndex] = heroIndex

XINGYAO_GONGMING_DEFMAX = 5 -- 5个守护者

XYHERO_LV = 1
XYHERO_RESET = 2

local function makeXyGmGridsByIndex(human,net,gridIndex,gridData)
	net.index = gridIndex
	net.oldLV = 0
	net.hero[0] = 0
	local heroIndex = gridData.heroIndex
	if heroIndex then
		local heroGrid = human.db.heroBag[heroIndex]
		if heroGrid then
			net.hero[0] = 1
			HeroGrid.makeHeroSimple(net.hero[1], heroGrid, heroIndex, human)

			net.oldLV = heroGrid.oldLV or 0
		end
	end
	
	net.leftTime = 0
	if gridData.cdTime then
		net.leftTime = math.max(gridData.cdTime - os.time(),0)
	end 
end

local function makeXyGmGrids(human,net,conf)
	net[0] = 0
	if not human.db.xingYaoGongMing or not human.db.xingYaoGongMing.gridList then return end
	for index,data in pairs(human.db.xingYaoGongMing.gridList) do
		net[0] = net[0] + 1
		local index = net[0]
		makeXyGmGridsByIndex(human,net[index],index,data)
	end
end

local function gongMingGridHeroOff(human,heroGrid,heroIndex)
	
    local oldLv = heroGrid.oldLV or 0
    Log.write(Log.LOGID_TEST, "XingYaoGongMing gongMingGridHeroOff hero OldLv: "..oldLv.." Lv: "..heroGrid.lv.." humanUuid: ", human.db._id)

    makeLvQuality(heroGrid)
	HeroEquip.putOffQuick(human, heroGrid.id, heroIndex, true)
	FuwenLogic.putOffQuick(human, heroGrid.id, heroIndex, true)
	ObjHuman.doCalcHero(human, heroIndex)
	HeroLogic.sendHeroGridDynamic(human, heroGrid, heroIndex)
end

local function makeGongMingDef(human)
	local maxIndex = nil
	local maxLV = nil
	local maxZhanDouLi = nil
	for i = 1, human.db.heroBag[0] do
		local heroGrid = human.db.heroBag[i]
		local defList = human.db.xingYaoGongMing.defList
		if heroGrid then
			if not defList or not defList[i] then
				local lv = heroGrid.lv
				if heroGrid.oldLV then
					lv = heroGrid.oldLV
				end

				local set = nil
				if not maxLV and not maxZhanDouLi then
					set = true
				else
					if maxLV < lv then
						set = true
					elseif maxLV == lv and maxZhanDouLi < heroGrid.zhandouli then
						set = true
					end
				end
				--print("i,maxLV,lv,maxZhanDouLi,zhandouli,set",i,maxLV,lv,maxZhanDouLi,heroGrid.zhandouli,set)
				if set then
					maxLV = lv
					maxIndex = i
					maxZhanDouLi = heroGrid.zhandouli
				end
			end
		end
	end

	if not maxIndex then return end

	local gridIndex = isGongMing(human,maxIndex)
	if gridIndex then
		local gridList = human.db.xingYaoGongMing.gridList
		if gridList then
			gridList[gridIndex].heroIndex = nil

		    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_302)
		end

        
        local heroGrid = human.db.heroBag[maxIndex]
		gongMingGridHeroOff(human,heroGrid,maxIndex)
	end

	human.db.xingYaoGongMing.defList = human.db.xingYaoGongMing.defList or {}
	human.db.xingYaoGongMing.defList[maxIndex] = maxIndex
end

local function checkGongMingDB(human)
	--if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302) then
		--return
	--end

	if human.db.xingYaoGongMing then return end

	human.db.xingYaoGongMing = {}
	human.db.xingYaoGongMing.gridList = {}
	human.db.xingYaoGongMing.gridList[1] = {}

	for i=1,XINGYAO_GONGMING_DEFMAX do
		makeGongMingDef(human)
	end
	
	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_302)
end

function CG_XINGYAOMEN_GONGMING_QUERY(human)
	if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302,true) then
		return
	end

	checkGongMingDB(human)

	local msgRet = Msg.gc.GC_XINGYAOMEN_GONGMING_QUERY
	local conf = XingYaoMenExcel.gongMing[1]
	makeXyGmGrids(human,msgRet.grid,conf)
	msgRet.maxGrid = conf.gridMax
	
	msgRet.defList[0] = 0 
	local db = human.db.xingYaoGongMing
	if db and db.defList then
		for heroIndex in pairs(db.defList) do
			local heroGrid = human.db.heroBag[heroIndex]
			if heroGrid then
				msgRet.defList[0] = msgRet.defList[0] + 1
				local index = msgRet.defList[0]
				HeroGrid.makeHeroSimple(msgRet.defList[index], heroGrid, heroIndex, human)
			end
		end
	end
	Grid.makeItem(msgRet.cdClean,conf.cdClean[1],conf.cdClean[2])

	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)

	GC_XINGYAOMEN_GONGMING_REDS(human)
end

local function gridCanOpen(human,gridIndex,send,checkItem)
	checkGongMingDB(human)

	local db = human.db.xingYaoGongMing
	if not db or not db.gridList then return end
	if gridIndex ~= 1 and not db.gridList[gridIndex - 1] then
		if send then
			Broadcast.sendErr(human,Lang.XINGYAOMEN_GONGMING_OPEN_ERR1)
		end
		return 
	elseif db.gridList[gridIndex] then
		if send then
			Broadcast.sendErr(human,Lang.XINGYAOMEN_GONGMING_OPEN_ERR2)
		end
		return
	end

	if checkItem then
		local gongMingConf = XingYaoMenExcel.gongMing[1]
		local conf = XingYaoMenExcel.gongMingGrid
		local needItem = gongMingConf.openGridCostItem
		local needCnt = 0
		if gridIndex > #conf then
			needCnt = conf[#conf].openCost
		else
			needCnt = conf[gridIndex].openCost
		end

		local now = BagLogic.getItemCnt(human, ItemDefine.ITEM_XINGYAOGONGMING_ID)
		if now < needCnt then
			if send then
				Broadcast.sendErr(human,Lang.XINGYAOMEN_GONGMING_OPEN_ERR)
			end
			return 
		end
	end

	return true
end

function CG_XINGYAOMEN_GONGMING_OPEN_QUERY(human,gridIndex)
	if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302,true) then
		return
	end

	local gongMingConf = XingYaoMenExcel.gongMing[1]
	if gridIndex > gongMingConf.gridMax then 
		return Broadcast.sendErr(human,Lang.XINGYAOMEN_GONGMING_OPEN_ERR3)
	end

	if not gridCanOpen(human,gridIndex,true) then return end

	local conf = XingYaoMenExcel.gongMingGrid
	local needItem = gongMingConf.openGridCostItem
	local needCnt = 0
	if gridIndex > #conf then
		needCnt = conf[#conf].openCost
	else
		needCnt = conf[gridIndex].openCost
	end

	local msgRet = Msg.gc.GC_XINGYAOMEN_GONGMING_OPEN_QUERY
	Grid.makeItem(msgRet.cost, needItem, needCnt)
	msgRet.now = BagLogic.getItemCnt(human, ItemDefine.ITEM_XINGYAOGONGMING_ID)

	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)
end

function CG_XINGYAOMEN_GONGMING_OPEN_DO(human,gridIndex)
	if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302,true) then
		return
	end

	local gongMingConf = XingYaoMenExcel.gongMing[1]
	if gridIndex > gongMingConf.gridMax then return end

	if not gridCanOpen(human,gridIndex,true,true) then return end

	local conf = XingYaoMenExcel.gongMingGrid
	local needItem = gongMingConf.openGridCostItem
	local needCnt = 0
	if gridIndex > #conf then
		needCnt = conf[#conf].openCost
	else
		needCnt = conf[gridIndex].openCost
	end

    local now = BagLogic.getItemCnt(human, ItemDefine.ITEM_XINGYAOGONGMING_ID)
	if now < needCnt then
       return
    end

	BagLogic.delItem(human, ItemDefine.ITEM_XINGYAOGONGMING_ID, needCnt, "xingyao_gongming")

	human.db.xingYaoGongMing.gridList[gridIndex] = {}

	local msgRet = Msg.gc.GC_XINGYAOMEN_GONGMING_OPEN_DO
	msgRet.gridIndex = gridIndex
	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)

	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_302)

	GC_XINGYAOMEN_GONGMING_REDS(human)
end

function CG_XINGYAOMEN_GONGMING_PUTON(human,heroIndex,gridIndex)
	if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302,true) then
		return
	end

	checkGongMingDB(human)

	local db = human.db.xingYaoGongMing
	if not db then return end

	local conf = XingYaoMenExcel.gongMing[1]
	if gridIndex > conf.gridMax then return end

	local mingLV,mingIndex = getGongMingLV(human)
	if mingLV == 0 then 
		return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_DEF_ERR)
	end

	local heroGrid = human.db.heroBag[heroIndex]
	local mingGrid = human.db.heroBag[mingIndex]
	if not heroGrid or not mingGrid then return end

	if db.defList[heroIndex] then
		return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_DEF_ERR)
	end

	if heroGrid.lv >= mingLV then
		return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_ON_ERR5)
	end

	if not db.gridList or not db.gridList[gridIndex] then
		return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_GRID_OPEN_ERR)
	end

	local gridData = db.gridList[gridIndex]
	if gridData then
		if gridData.heroIndex then
			return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_ON_ERR2)
		elseif gridData.cdTime then
			local leftTime = math.max(gridData.cdTime - os.time(),0)
			if leftTime > 0 then
				return Broadcast.sendErr(human, Util.format(Lang.XINGYAOMEN_GONGMING_ON_ERR3,leftTime))
			end
		end
	end

	for _,data in pairs(db.gridList) do
		if data.heroIndex == heroIndex then
			return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_ON_ERR1)
		end
	end

	human.db.xingYaoGongMing.gridList[gridIndex].heroIndex = heroIndex

	makeLvQuality(heroGrid,mingGrid)

	HeroLogic.sendHeroGridDynamic(human, heroGrid, heroIndex)

	local msgRet = Msg.gc.GC_XINGYAOMEN_GONGMING_PUTON
	msgRet.heroIndex = heroIndex
	msgRet.gridIndex = gridIndex
	HeroGrid.makeHeroSimple(msgRet.hero,heroGrid,heroIndex,human)
	makeXyGmGridsByIndex(human,msgRet.grid,gridIndex,human.db.xingYaoGongMing.gridList[gridIndex])
	Msg.send(msgRet,human.fd)

	ObjHuman.doCalcHero(human, heroIndex)

	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_302)
end

function CG_XINGYAOMEN_GONGMING_PUTOFF(human,gridIndex)
	if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302,true) then
		return
	end

	checkGongMingDB(human)

	local db = human.db.xingYaoGongMing
	if not db then return end

	if not db.gridList or not db.gridList[gridIndex] then
		return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_GRID_OPEN_ERR)
	end

	if not db.gridList[gridIndex].heroIndex then
		return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_GRID_HERO_NO)
	end

	local heroIndex = db.gridList[gridIndex].heroIndex
	db.gridList[gridIndex].heroIndex = nil

	local heroGrid = human.db.heroBag[heroIndex]
	if not heroGrid then end

	local now = os.time()
	local conf = XingYaoMenExcel.gongMing[1]
	db.gridList[gridIndex].cdTime = now + conf.cdTime
	
	gongMingGridHeroOff(human,heroGrid,heroIndex)

	local msgRet = Msg.gc.GC_XINGYAOMEN_GONGMING_PUTOFF
	msgRet.gridIndex = gridIndex
	msgRet.leftTime = math.max(db.gridList[gridIndex].cdTime - now,0)
	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)

	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_302)
end

function CG_XINGYAOMEN_GONGMING_CDCLEAN(human,gridIndex)
	if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302,true) then
		return
	end

	local db = human.db.xingYaoGongMing
	if not db then return end

	if not db.gridList or not db.gridList[gridIndex] then
		return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_GRID_OPEN_ERR)
	end

	if not db.gridList[gridIndex].cdTime then return end

	local leftTime = math.max(db.gridList[gridIndex].cdTime - os.time(),0)
	if leftTime <= 0 then 
		return Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_CD_CLEAN)
	end

	local conf = XingYaoMenExcel.gongMing[1]
	if not BagLogic.checkItemCnt(human,conf.cdClean[1],conf.cdClean[2]) then
		return
	end
	BagLogic.delItem(human,conf.cdClean[1],conf.cdClean[2],"xingyao_gongming")

	db.gridList[gridIndex].cdTime = nil

	Broadcast.sendErr(human, Lang.XINGYAOMEN_GONGMING_CD_CLEAN_OK)

	local msgRet = Msg.gc.GC_XINGYAOMEN_GONGMING_CDCLEAN
	msgRet.gridIndex = gridIndex
	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)

	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_302)
end

function getGongMingLV(human)
	local db = human.db.xingYaoGongMing
	if not db or not db.defList then return 0,0,0 end

	local mingLV = nil
	local mingIndex = nil
	local defCnt = 0
	for index in pairs(db.defList) do
		local heroGrid = human.db.heroBag[index]
		if heroGrid then
			defCnt = defCnt + 1
			if not mingLV or mingLV > heroGrid.lv then
				mingLV = heroGrid.lv
				mingIndex = index
			end
		end
	end

	if defCnt < XINGYAO_GONGMING_DEFMAX then
		return 0,0,0
	end

	return mingLV,mingIndex,defCnt
end

-- 是否正在共鸣 则不能做常规升级
function isGongMing(human,heroIndex,sendType)
	if not human or not human.db.xingYaoGongMing or not human.db.xingYaoGongMing.gridList then return end

	for index,data in pairs(human.db.xingYaoGongMing.gridList) do
		if data.heroIndex and heroIndex == data.heroIndex then 
			if sendType and sendType == XYHERO_LV then
				Broadcast.sendErr(human,Lang.XINGYAOMEN_GONGMING_LVUP_ERR)
			elseif sendType and sendType == XYHERO_RESET then
				Broadcast.sendErr(human,Lang.XINGYAOMEN_GONGMING_RESET_ERR)
			end

			return index 
		end
	end
end

--刷新入驻英雄共鸣等级
function refreshGongMing(human)
	if not human.db.xingYaoGongMing or not human.db.xingYaoGongMing.gridList then return end

	local mingLV,mingIndex = getGongMingLV(human)
	if mingLV == 0 then return end

	for i,data in pairs(human.db.xingYaoGongMing.gridList) do
		if data.heroIndex then
			local heroGrid = human.db.heroBag[data.heroIndex]
			local mingGrid = human.db.heroBag[mingIndex]
			if heroGrid and mingGrid then
				local send = nil
				if mingLV ~= heroGrid.lv then
					heroGrid.lv = mingLV
					send = true
				end
				if heroGrid.quality ~= mingGrid.quality then
					heroGrid.quality = mingGrid.quality
					send = true
				end

				if send then
					ObjHuman.doCalcHero(human, data.heroIndex)
					HeroLogic.sendHeroGridDynamic(human, heroGrid, data.heroIndex)
				end
			end
		end
	end
end

--增加新的英雄，是否影响到守卫和共鸣等级 heroIndex新增英雄背包index
function onAddHero(human,heroGrid,heroIndex)
	--if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302) then
		--return
	--end
	--print("onAddHero lv heroIndex",heroGrid.lv,heroIndex)
	checkGongMingDB(human)

	local mingLV,minIndex,defCnt = getGongMingLV(human)

	human.db.xingYaoGongMing.defList = human.db.xingYaoGongMing.defList or {}
	local defList = human.db.xingYaoGongMing.defList
	if defList[heroIndex] then return end

	if defCnt >= XINGYAO_GONGMING_DEFMAX then
		if heroGrid.lv > mingLV then
			defList[minIndex] = nil
			defList[heroIndex] = heroIndex
		end

		refreshGongMing(human)
	else
		human.db.xingYaoGongMing.defList[heroIndex] = heroIndex
	end
end

--删除英雄，是否影响到守卫和共鸣等级 heroIndex原英雄背包index
function onDelHero(human, heroGrid, heroIndex)
	--if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302) then
		--return
	--end

	checkGongMingDB(human)

	local defList = human.db.xingYaoGongMing.defList
	if not defList or not defList[heroIndex] then  --删除的英雄不是守卫者，走卸下入驻逻辑 
        putOff(human,heroGrid,heroIndex)
		
		return
	end

	defList[heroIndex] = nil
	makeGongMingDef(human)
	refreshGongMing(human)
end


--英雄升级，是否影响到守卫和共鸣等级 heroIndex原英雄背包index
function onHeroLvUp(human,heroGrid,heroIndex)
	--if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302) then
		--return
	--end

	--print("onHeroLvUp lv heroIndex",heroGrid.lv,heroIndex)
	checkGongMingDB(human)

	local mingLV,minIndex,defCnt = getGongMingLV(human)
	local defList = human.db.xingYaoGongMing.defList
	if not defList then return end

	if defList[heroIndex] then
		refreshGongMing(human)
	else
		if heroGrid.lv > mingLV then
			human.db.xingYaoGongMing.defList[minIndex] = nil
			human.db.xingYaoGongMing.defList[heroIndex] = heroIndex
			refreshGongMing(human)
		end
	end
end

--英雄升阶
function onHeroUpgrade(human,heroGrid,heroIndex)
	--if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302) then
		--return
	--end

	checkGongMingDB(human)

	local defList = human.db.xingYaoGongMing.defList
	if not defList then return end

	if not defList[heroIndex] then return end

	refreshGongMing(human)
end

--英雄分解重生觉醒等等操作，下阵入驻英雄
function putOff(human,heroGrid,heroIndex)
	local gridIndex = isGongMing(human,heroIndex)
	if not gridIndex then return end

    local oldLv = heroGrid.oldLV or 0
    Log.write(Log.LOGID_TEST, "XingYaoGongMing putOff hero OldLv: "..oldLv.." Lv: "..heroGrid.lv.." humanUuid: ", human.db._id)

	makeLvQuality(heroGrid)

	human.db.xingYaoGongMing.gridList[gridIndex] = {}

	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_302)
end

--征战扫荡掉落
local function onSaoDang(human,items)
	local conf = XingYaoMenExcel.gongMing[1]
	local battleExtraItem = conf.battleExtraItem
	local battleExtraSaoDang = conf.battleExtraSaoDang
	local randomCnt = math.random(battleExtraSaoDang[1],battleExtraSaoDang[2])
	items[battleExtraItem] = (items[battleExtraItem] or 0) + randomCnt
end

--征战挂机收益
function getItemOut(human,items,outCnt)
	if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302) then
		return
	end
	
	checkGongMingDB(human)

	if not outCnt then 
		onSaoDang(human,items)
		return
	end

	local conf = XingYaoMenExcel.gongMing[1]
	local battleExtraItem = conf.battleExtraItem
	local battleExtraCnt = conf.battleExtraCnt

	local cnt = math.ceil(math.random(battleExtraCnt[1] * outCnt,battleExtraCnt[2] * outCnt))
	if cnt > 0 then
		items[battleExtraItem] = (items[battleExtraItem] or 0) + cnt
	end
end

--扫荡奖励列表
function getItemOutsShow(human,items)
	if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_302) then
		return
	end

	checkGongMingDB(human)

	local conf = XingYaoMenExcel.gongMing[1]
	local battleExtraItem = conf.battleExtraItem
	items[battleExtraItem] = 1
end

function getGongMingLVByIndex(human,heroIndex)
	if not human then return 0 end
	if not isGongMing(human,heroIndex) then return 0 end

	local mingLV = getGongMingLV(human)
	return mingLV or 0
end

function GC_XINGYAOMEN_GONGMING_REDS(human)
	local msgRet = Msg.gc.GC_XINGYAOMEN_GONGMING_REDS
	msgRet.nextGridRed = 0
	
	local db = human.db.xingYaoGongMing
	if not db or not db.gridList then return end

	local conf = XingYaoMenExcel.gongMing[1]
	local nextGrid = math.min(#db.gridList + 1,conf.gridMax)
	if not db.gridList[nextGrid] then
		if gridCanOpen(human,nextGrid,nil,true) then
			msgRet.nextGridRed = 1
		end
	end
	
	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)
end

function makeLvQuality(heroGrid,targetGrid)
	if targetGrid then
		heroGrid.oldLV = heroGrid.lv
		heroGrid.lv = targetGrid.lv
		heroGrid.oldQuality = heroGrid.quality
		heroGrid.quality = targetGrid.quality
	else
		heroGrid.lv = heroGrid.oldLV
		heroGrid.oldLV = nil
		heroGrid.quality = heroGrid.oldQuality
		heroGrid.oldQuality = nil
	end
end

function isDot(human)
	if not human.db.xingYaoGongMing 
		or not human.db.xingYaoGongMing.gridList 
		then return true 
	end

	local gridList = human.db.xingYaoGongMing.gridList
	local conf = XingYaoMenExcel.gongMing[1]
	local nextGrid = math.min(#gridList + 1,conf.gridMax)
	if not gridList[nextGrid] then
		if gridCanOpen(human,nextGrid,nil,true) then
			return true
		end	
	end

	
end