--  ***************************************************************************************************** --
--  code by http://www.Cgdev.me
--  ͼMapʾű
--                   #־#
--  TYPE ***** TIME ***** Editor ***** Text 
--  CREATE  2013/01/19     Free
--  ADD     2013/01/20     Free        MapĺʾʾȫʹȫֵTalk¼
--  FIX     2013/01/27     Free        ޸ֺ/﷨

--                   ##
--	LuaͼMAPID޸
local LUAMAPID = %ͼ_LUAMAP%;
--					 #ʹ˵#
--	Ϸɴ

--	copymap,ͼ,ͼID						--> ָͼ
--	mazemap,x귶Χ,y귶Χ,ͼ			--> ָСͼ
--	delmap,ͼID								--> ɾָLuaͼ
--	getpos,ͼID								--> ȡָLuaɵͼеõ
--	getimage									--> ȡǰĵذͼźͼ
--	setimage									--> õǰĵذ/ͼ
--	dumpmap,·,ͼID,дID					--> ָLuaͼɳɵͼļָ·

--					 #ע#
--	ͼʱ򣬻ͣϷбͼɣͼù󣬿Ҫܳʱɺõͼвԡ
--	makeMazeMapеõMap.MakeMazeMapĸĵв鿴Ҳ̳/QQȺн
--	ýűδȫĲԣ⣬Բϡ档
--  ***************************************************************************************************** --

--ע¼ί
Delegate.RegDelTalkEvent("MapProc_TalkEvent");

--	Ƶͼ
--		index: Ҷ
--				c_mapid: ҪƵĵͼMap ID
--				c_floor: ҪƵĵͼFloor ID
function makeCopyMap(index, c_mapid, c_floor)
	local newFloorID = Map.MakeCopyMap(c_mapid, c_floor);
	if(newFloorID == -1)then
		NLG.SystemMessage(index, "ͼʧܡ");
	else
		NLG.SystemMessage(index, "ͼ"..c_mapid..","..c_floor.."ѾɹƵ"..LUAMAPID..","..newFloorID);
		NLG.SystemMessage(index, "ʹGM[warp "..LUAMAPID.." "..newFloorID.." x y] ƶȥŶ");
	end
	return;
end

--	ͼ
--		index: Ҷ
--				xsiz: Ҫɵĵͼx귶Χ
--				ysiz: Ҫɵĵͼy귶Χ
--				mapName: ͼ
--	˵MakeMazeMapҪصģϺ󣬻ᴥĻصɵĵͼǷ
function makeMazeMap(index, xsiz, ysiz, mapName)
	local newFloorID = Map.MakeMazeMap(nil,"mazeMapDoneCall",xsiz,ysiz,mapName,2,30,30,30,30,30,9491,100,0,0,0,0,0,0);
	if(newFloorID == -1)then
		NLG.SystemMessage(index, "ͼʧܡ");
	else
		NLG.SystemMessage(index, "ͼ"..LUAMAPID..","..newFloorID.."Ѿʼ...Ժ");
	end
	return;
end

--	ͼĻص
--		floorID: ɵĵͼFloor ID
--				doneflg: ɽ
function mazeMapDoneCall(floorID, doneflg)
	if(doneflg == 1)then
		NLG.SystemMessage(-1,"ɵͼ"..LUAMAPID..","..floorID.."ɹ");
		NLG.SystemMessage(index, "ͨʹgetpos,"..floorID.."ȡһϷ");
	else
		NLG.SystemMessage(-1,"ɵͼ"..LUAMAPID..","..floorID.."ʧܣ");
	end
end

--	ȡͼõ
--		index: Ҷ
--				floorID: ͼFloor ID
function getPosition(index, floorID)
	local mapx, mapy = Map.GetAvailablePos(floorID);
	if(mapx == 0 and mapy == 0)then
		NLG.SystemMessage(index,"ȡͼʧܣ");
	else
		NLG.SystemMessage(index,"ȡͼ"..LUAMAPID..","..floorID..""..mapx..","..mapy);
		NLG.SystemMessage(index, "ʹGM[warp "..LUAMAPID.." "..floorID.." "..mapx.." "..mapy.."] ƶȥŶ");
	end
	return;
end

--	ȡǰĵذͼobjͼ
--		index: Ҷ
function getTileandObj(index)
	local nowMap = Char.GetData(index, %_MAP%);
	local nowFloor = Char.GetData(index, %_ͼ%);
	local nowXpos = Char.GetData(index, %_X%);
	local nowYpos = Char.GetData(index, %_Y%);
	local tile, obj = Map.GetImage(nowMap,nowFloor,nowXpos,nowYpos);
	NLG.SystemMessage(index,"ȡͼ"..nowMap..","..nowFloor..","..nowXpos..","..nowYpos.."ĵͼԪ[ذ:"..tile.."],[:"..obj.."]");
	return;
end

--	õǰĵذͼobjͼţԶжͼڵذ廹
--		index: Ҷ
--				image: ͼ
function setTileandObj(index, image)
	local nowMap = Char.GetData(index, %_MAP%);
	local nowFloor = Char.GetData(index, %_ͼ%);
	local nowXpos = Char.GetData(index, %_X%);
	local nowYpos = Char.GetData(index, %_Y%);
	local ori_tile, ori_obj = Map.GetImage(nowMap,nowFloor,nowXpos,nowYpos);
	Map.SetImage(nowMap,nowFloor,nowXpos,nowYpos, image);
	local now_tile, now_obj = Map.GetImage(nowMap,nowFloor,nowXpos,nowYpos);
	NLG.SystemMessage(index,"ͼ"..nowMap..","..nowFloor..","..nowXpos..","..nowYpos.."ĵͼԪر[ذ:"..ori_tile.."->"..now_tile.."],[:"..ori_obj.."->"..now_obj.."]");
	return;
end

--	ͼļ·
--		index: Ҷ
--				path: ·ļļ"\"β
--				floorID: LuaͼĵͼFloor
--				writeAsFloor: дļжĵͼ
function dumpMapFile(index, path, floorID, writeAsFloor)
	local ret = Map.DumpLuaMap(path, floorID, writeAsFloor);
	if (ret == 0) then
		NLG.SystemMessage(index,"ͼ"..LUAMAPID..","..floorID.."ļʧܣ");
	else
		NLG.SystemMessage(index,"ͼ"..LUAMAPID..","..floorID.."Ѿļ:["..path..LUAMAPID.."_"..floorID.."]");
	end
	return;
end

--	ɾLuaɵĵͼͷŵͼ
--		floorID: LuaͼĵͼFloor
function deleteLuaMap(index, floorID)
	local ret = Map.DelLuaMap(floorID);
	if (ret == 0) then
		NLG.SystemMessage(index,"ͼ"..LUAMAPID..","..floorID.."ɾʧܣ");
	else
		NLG.SystemMessage(index,"ͼ"..LUAMAPID..","..floorID.."Ѿɹɾ");
	end
	return;
end

--	NL.AllTaklEventCallBackίе
--		http://lua.cgdev.me/doku.php?id=lua:nl:callback:talkcallback
function MapProc_TalkEvent(index, msg, col, range, size)
	if(string.find(msg,","))then
		local token = Split(msg,",")
	end
	if(string.find(msg,"copymap"))then
		local m = tonumber(token[2]);
		local f = tonumber(token[3]);
		makeCopyMap(index, m ,f);
		return 1;
	end
	if(string.find(msg,"mazemap"))then
		local xsiz = tonumber(token[2]);
		local ysiz = tonumber(token[3]);
		local MapName = token[4];
		makeMazeMap(index, xsiz, ysiz, MapName);
		return 1;
	end
	if(string.find(msg,"delmap"))then
		local f = tonumber(token[2]);
		deleteLuaMap(index, f);
		return 1;
	end
	if(string.find(msg,"getpos"))then
		local f = tonumber(token[2]);
		getPosition(index, f);
		return 1;
	end
	if(msg=="getimage")then
		getTileandObj(index);
		return 1;
	end
	if(string.find(msg,"setimage"))then
		image = tonumber(token[2]);
		setTileandObj(index, image);
		return 1;
	end
	if(string.find(msg,"dumpmap"))then
		path = token[2];
		floorID = tonumber(token[3]);
		writeAsFloor = tonumber(token[4]);
		dumpMapFile(index, path, floorID, writeAsFloor);
		return 1;
	end
end
