--[[
Created by Myth ~
	Revision 1 : 06-12-2012
		- Chest Removal
	Revision 2 : 07-12-2012
		- Pillar Spawn
]]
-- Regen Information for Crystal
RegenInfo =
{
	Crystal   = { Index = "KQ_DarkPowerCrystal",               x = 4282, y =  3286, dir =   0 },
}
-- Base AI Function
ReturnAI = {}
ReturnAI.END = 1    -- Return_AI_END = 1;//    -- End AI Script
ReturnAI.CPP = 2    -- Return_AI_CPP = 2;//    -- Unknown

MemBlock = {}

-- Chest Removal
function Common_RemoveTreasure(MapIndex)
cExecCheck "Common_RemoveTreasure"
	-- If Boss is Dead // Skip to Next Func
	if cIsObjectDead(Handle) ~= nil then
		return Var.StepFunc(Var)
	end

	cDebugLog "Common_RemoveTreasure"

	cRegenGroupActiv("BH_DarkTown", "DTBox", 0)	-- Activate the RegenGroup then Remove them
	cVanishAll(MapIndex, "KQ_DarkBox")
end

------------------------------------------------------------------
-----------------------    Boss Part   --------------------------
------------------------------------------------------------------

function KQ_DarkSpiderKing(Handle, MapIndex)
cExecCheck "KQ_DarkSpiderKing"

	if MapIndex ~= "BH_DarkTown" then	-- Check Map
		cAIScriptSet(Handle)	-- Set Default AI
		return ReturnAI.END
	end

	local Var = MemBlock[Handle]
	if cIsObjectDead(Handle) ~= nil then
		if Var ~= nil then   -- Boss Dead // Handle Lost
			cDebugLog("Boss Dead")

			-- Spawn InvisibleMan to monitor chests
			local InvisibleHandle = cMobRegen_Obj("InvisibleMan", Handle)
			cAIScriptSet(InvisibleHandle, Handle) -- Give InvisibleMan default AI
			MemBlock[InvisibleHandle] = {}
			MemBlock[InvisibleHandle].Handle = InvisibleHandle
			MemBlock[InvisibleHandle].MapIndex = MapIndex
			MemBlock[InvisibleHandle].StepFunc = Invisible_Init

			MemBlock[Handle] = nil
		end
		return ReturnAI.END
	end


	if Var == nil then    -- First Loop
		MemBlock[Handle] = {}
		Var = MemBlock[Handle]

		Var.Handle = Handle
		Var.MapIndex = MapIndex

		Var.BoxVanish = 0		-- Increases by 10 each check

		Var.CrystalAlive = nil

		Var.TargetLostSec = 0 	-- Starting Value for LostTarget

		Var.Wait = {}
		Var.Wait.Second = 0;
		Var.Wait.NextFunc = nil

		Var.StepFunc = Boss_WaitInvader
	end

	return Var.StepFunc(Var)
end

function Boss_Wait(Var)  -- Wait for Boss Spawn // Handle
cExecCheck "Boss_Wait"
	if cCurSec() >= Var.Wait.Second then
		Var.StepFunc = Var.Wait.NextFunc
	end
end

function Boss_WaitInvader(Var)
cExecCheck "Boss_WaitInvader"

	if Var.CrystalAlive == nil then
		CrystalSpawn( Var )
	end

	local CurSecond = cCurSec()
	if CurSecond > Var.BoxVanish then
		Common_RemoveTreasure(Var.MapIndex)
		Var.BoxVanish = CurSecond + 10
	end

	return ReturnAI.CPP
end

function CrystalSpawn(Var)
cExecCheck "CrystalSpawn"

	if Var.CrystalAlive == nil then
		Var.CrystalAlive = cMobRegen_XY( Var.MapIndex, RegenInfo.Crystal.Index, RegenInfo.Crystal.x, RegenInfo.Crystal.y, RegenInfo.Crystal.dir )

	Boss_WaitInvader(Var)
	end
end

-------------------------------------------------------------------------
-----------------------    InvisibleMan Part   --------------------------
-------------------------------------------------------------------------
-- Invisible Man to Monitor Chests

function InvisibleMan(Handle, MapIndex)
cExecCheck "InvisibleMan"
	local Var = MemBlock[Handle]
	if Var == nil then
		cDebugLog("InvisibleMan Handle Error:" .. Handle)
		return
	end

	if Var.StepFunc ~= nil then
		Var.StepFunc(Var)
	end

	return ReturnAI.END
end

function Invisible_Init(Var)
cExecCheck "Invisible_Init"
	cDebugLog "Invisible_Init"

	cGroupRegen("BH_DarkTown", "DTBox")	-- Spawn Chests

	Var.Wait = {}
	Var.Wait.Second = cCurSec() + 60
	Var.StepFunc = Invisible_AllVanish
end

function Invisible_AllVanish(Var)
cExecCheck "Invisible_AllVanish"

	if cCurSec() > Var.Wait.Second then
		cDebugLog "Invisible_AllVanish"
		cRegenGroupActiv("BH_DarkTown", "DTBox", 0)	-- Activate the RegenGroup then Remove them

		Common_RemoveTreasure(Var.MapIndex)
		cNPCVanish(Var.Handle)

		Var.StepFunc = nil
	end
end
