--------------------------------------------------------------------------------
--                      Seiren Castle Main File                               --
--------------------------------------------------------------------------------

require( "common" )

require( "ID/Siren/Data/Name" )				-- File path, file name table for the name, the reference station, proceed step
require( "ID/Siren/Data/Chat" )				-- All related chat
require( "ID/Siren/Data/Process" )			-- Progress-related data, such as information and links to various delay times, the quest
require( "ID/Siren/Data/Regen" )			-- Regeneration data (group-related regeneration type, location, and properties of the mob, NPC, statements, items, etc.)
require( "ID/Siren/Data/Stuff" )			-- Stuff related to data processing (information element for causing the NPC action information and action)
require( "ID/Siren/Data/Boss" )				-- Boss-related data processing (Boss Phase Data (including Summon monsters), more than the state, Chief mob-related warning information)

require( "ID/Siren/Functions/SubFunc" )		-- Various Sub Functions necessary for the overall progress
require( "ID/Siren/Functions/Routine" )		-- The AI ??routines attached mob-related, etc.
require( "ID/Siren/Functions/Progress" )	-- Each phase advance function is defined


function Main( Field )
cExecCheck "Main"

	local Var = InstanceField[ Field ]

	if Var == nil
	then

		InstanceField[ Field ] = {}

		Var					= InstanceField[ Field ]
		Var["MapIndex"]		= Field

		Var["Enemy"]		= {}	-- Information enemies that holds memory
		Var["Door"]			= {}	-- Ordering information that holds memory
		Var["Portal"]		= {}	-- Memory that holds information portal
		Var["RoutineTime"] 	= {}	-- Every time the routine memory that stores the handle


--		cSetFieldScript ( Var["MapIndex"], MainLuaScriptPath )
--		cFieldScriptFunc( Var["MapIndex"], "MapLogin", "PlayerMapLogin" )

cAssertLog( "TestEvent passed 2: "..Var["MapIndex"] )

		Var["StepIndex"]	= nil
		Var["StepFunc"] 	= nil


		-- First time entry
		Var["InitialSec"] 	= cCurrentSecond()
		Var["CurSec"] 	  	= cCurrentSecond()


		
--[[
		RegenExitGate  = { Index = "Gate_ID_Complete",	x = 378,	y = 10920,	dir = 100,	scale = 1000, Title = "Exit Gate", Yes = "Exit", No = "Cancel"  }
		RegenExitGate  = { Index = "S_GateOut",	x = 378,	y = 10820,	dir = 100,	scale = 1000 }
		nExitGateHandle = cDoorBuild( Var["MapIndex"], RegenExitGate["Index"], RegenExitGate["x"], RegenExitGate["y"], RegenExitGate["dir"], RegenExitGate["scale"] )
		if nExitGateHandle ~= nil
		then
			if cSetAIScript ( "ID/Siren/Siren", nExitGateHandle ) == nil
			then
				ErrorLog( "InitDungeon::cSetAIScript ( MainLuaScriptPath, nExitGateHandle ) == nil" )
			end

			if cAIScriptFunc( nExitGateHandle, "NPCClick", "Click_ExitGate" ) == nil
			then
				ErrorLog( "InitDungeon::cAIScriptFunc( nExitGateHandle, \"NPCClick\", \"Click_ExitGate\" ) == nil" )
			end
		DebugLog( "Start InitDungeon21" ) -- ########################################
		end
	Var.ExitGate = cMobRegen_XY( Var.MapIndex, RegenExitGate.Index,
													RegenExitGate.x,
													RegenExitGate.y,
													RegenExitGate.dir )

	if Var.ExitGate == nil then
		cDebugLog( "Default_Setting : Fail cMobRegen_XY ExitGate" )
		return
	end

	if cSetAIScript( "ID/Siren/Siren", Var.ExitGate ) == nil then
		cDebugLog( "Default_Setting : Fail cSetAIScript ExitGate" )
		return
	else
	cAssertLog( "DebugLog::SetAiScript completed" )
	end

	if cAIScriptFunc( Var.ExitGate, "NPCClick", "Click_ExitGate" ) == nil then
		cDebugLog( "Default_Setting : Fail cAIScriptFunc ExitGate" )
		return
	else
	cAssertLog( "DebugLog::SetAiScriptFunc completed" )
	end
--]]		

		
		-- The first step
		GoToNextStep( Var )
--Var["StepFunc"] 				= MethodStartScript
--MethodStartScript( Var )

	end


	-- 0.5 It runs every second
	if Var["CurSec"] + 0.5 > cCurrentSecond()
	then
		return
	else
		Var["CurSec"] = cCurrentSecond()
	end

	-- Run step function ( Functions/Progress.lua )
	Var["StepFunc"]( Var )
end

--[[
RegenInfo =
{
	-- 이동 게이트, ( 인덱스 수정시 AdlF_Gate.lua파일의 진입함수 수정 )
	-- 이동 위치는 AdlF_Gate.lua파일에서 cLinkTo 직접 수정
	ExitGate	= { Index = "Gate_ID_Complete",  x =  2208, y =  9966, dir =  48, Title = "Exit Gate", Yes = "Exit", No = "Cancel" },
	BossRoomGate= { Index = "Gate_ID_Exit",      x =  9412, y = 20075, dir =  48, Title = "Exit Gate", Yes = "Exit", No = "Cancel" },
	ExitBossGate= { Index = "Gate_ID_Exit",      x = 12924, y =  6373, dir =  48, Title = "Exit Gate", Yes = "Exit", No = "Cancel" },
	CompleteGate= { Index = "Gate_ID_Complete",  x = 12720, y =  8518, dir =  48, Title = "Exit Gate", Yes = "Exit", No = "Cancel" },
}

function Click_ExitGate( NPCHandle, PlyHandle, RegistNumber )
cExecCheck "Click_ExitGate"


	local Var = GateMemBlock[NPCHandle]

	if Var == nil then
		return
	end

	cServerMenu( PlyHandle, NPCHandle, RegenInfo.CompleteGate.Title,
										RegenInfo.CompleteGate.Yes, "LinkToZone3",
										RegenInfo.CompleteGate.No,  "GateDummy")

end

function GateDummy( NPCHandle, PlyHandle, RegistNumber )
end

function LinkToZone3( NPCHandle, PlyHandle, RegistNumber )
cExecCheck "LinkToZone3"

	local Var = GateMemBlock[NPCHandle]

	if Var == nil then
		return
	end

	cLinkTo( PlyHandle, Var.MapIndex, 9412, 20075 )

end
--]]

-- missing functions that tie to the gate name
-- example 	CompleteGate= { Index = "Gate_ID_Complete",  x = 12720, y =  8518, dir =  48, Title = "Exit Gate", Yes = "Exit", No = "Cancel" },
function Gate_ID_Complete( Handle, MapIndex )
cExecCheck "Gate_ID_Complete"

	return Gate_Main( Handle, MapIndex )
end

function Gate_ID_Exit( Handle, MapIndex )
cExecCheck "Gate_ID_Exit"

	return Gate_Main( Handle, MapIndex )
end

function S_Portal( Handle, MapIndex )
cExecCheck "S_Portal"

	return Gate_Main( Handle, MapIndex )
end

function S_GateOut( Handle, MapIndex )
cExecCheck "S_GateOut"

	return Gate_Main( Handle, MapIndex )
end


GateMemBlock = {}


function Gate_Main( Handle, MapIndex )
cExecCheck "Gate_Main"

	local Var = GateMemBlock[Handle]

	if cIsObjectDead( Handle ) then			-- 죽었음

		cAIScriptSet( Handle )				-- 스크립트 없앰
		GateMemBlock[Handle] = nil			-- 메모리해제

		return ReturnAI.END
	end

	if Var == nil then

		GateMemBlock[Handle] = {}

		Var = GateMemBlock[Handle]

		Var.Handle		= Handle
		Var.MapIndex	= MapIndex
	end

	return ReturnAI.END
end

