--[[

[author]
descript = "九宫盒任务，一天最多27环。刷第三宫之前要自己手动和一个异性好友组队伍"

]]--

local bStop = false
local g_szNameMiss = {}
	g_szNameMiss[ 1 ] = "神秘拼图"
	g_szNameMiss[ 2 ] = "亦幻亦真"

	g_szNameMiss[ 4 ] = "寻寻觅觅"

	g_szNameMiss[ 3 ] = "众里寻她"
	g_szNameMiss[ 7 ] = "众里寻他"

	g_szNameMiss[ 9 ] = "九宫谜题"

local g_InfoMiss1 = {}
	g_InfoMiss1[ 0x2626bb0 ] = { idMap = 0x04 , x = 196 , y = 245 }
	g_InfoMiss1[ 0x2626bb1 ] = { idMap = 0x08 , x = 244 , y = 180 }
	g_InfoMiss1[ 0x2626bb2 ] = { idMap = 0x1a , x = 200 , y = 125 }
	g_InfoMiss1[ 0x2626bb3 ] = { idMap = 0x019 , x = 236 , y = 54 }
	g_InfoMiss1[ 0x2626bb4 ] = { idMap = 0x1b , x = 266 , y = 54 }
	g_InfoMiss1[ 0x2626bb5 ] = { idMap = 0x14 , x = 177 , y = 250 }
	g_InfoMiss1[ 0x2626bb6 ] = { idMap = 0x18 , x = 96 , y = 121 }
	g_InfoMiss1[ 0x2626bb7 ] = { idMap = 0x12 , x = 237 , y = 88 }
	g_InfoMiss1[ 0x2626bb8 ] = { idMap = 0x1f , x = 133 , y = 186 }
	g_InfoMiss1[ 0x2626baf ] = { idMap = 0x1e , x = 238 , y = 135 }




function isFullBag( nID )
	
	local nResult = PlayerPackage:GetEmptySlot( nID )

	if nResult == 0 then
		ShowMessage( "背包已满" )
		DbgPrintf( "Count items nResult %d " , nResult )
		return true
	end

	return false
end

function isMissionComplete( szNameMiss )

	local nIndex, idMission, idScript = DataPool:GetPlayerMission( szNameMiss )
	local g_nParam = {}

	if nIndex ~= -1 then

--		DbgPrintf(" Index:%d MissionID:%d ScriptID:%d" , g_nIndex , g_idMission , g_idScript )

		-- 取得任务参数
		for i = 0 , 7 do
			nParam[i] = DataPool:GetPlayerMission_Variable( nIndex , i )
			DbgPrintf( "[%d]%d", i , nParam[i] )
		end
		
		return tonumber( nParam[0] )
	end

	return -1
end

function missionComplete2()

	MoveToNPC( 328 , 246 , 0x01 , "魏三爷" )

	while true do

		if isFullBag( 1 ) == false then --or DataPool:UserBag_CountItemByIDTable( 0x13bdb6a ) > 0 then
			break
		end

		ShowMessage( "您的道具包或者材料包已满!" )
		System:Sleep( 1000 )
	end
	System:Sleep( 1000 )
	QuestFrameOptionClicked( "天机任务" , CMP_PART_MATCH)
	QuestFrameMissionComplete()
	QuestFrameAcceptClicked()
	QuestFrameMissionClose()
	System:Sleep( 1000 )
end


function getCurrentMiss()
	local idItemRubik = 0x2626bd0

	local nCurrent
	local nIndexMiss
	local bFlag = false
	local nIndex = 8
	for i = 0 , 8 do
		if DataPool:UserBag_CountItemByIDTable( idItemRubik + nIndex - i ) > 0 then
			nCurrent = idItemRubik + nIndex - i
			nIndexMiss = nIndex - i + 1
			bFlag = true
			break
		end		
	end
	
	if bFlag == true then
		return nCurrent , nIndexMiss
	else
		return -1 , -1
	end

end

function isItemMapMission1()

	local nItemMap = 0x2626bb0
	local nMapCall = -1
		if DataPool:UserBag_CountItemByIDTable( 0x2626baf ) > 0 then
			nMapCall = 0x2626baf
		else
			for i = 0 , 8 do
				if DataPool:UserBag_CountItemByIDTable( nItemMap + i ) > 0 then
					nMapCall = nItemMap + i
					break
				end
			end
		end
	return nMapCall

end

function execMission1( nItem )

	if g_InfoMiss1[ nItem ] == nil then
		DbgPrintf( "g_InfoMiss1[ nItem ] = nil " )
		bStop = true
		return
	end

	MoveTo( g_InfoMiss1[ nItem ].x , g_InfoMiss1[ nItem ].y , g_InfoMiss1[ nItem ].idMap ) 
	Dismount()
	System:Sleep(1000)
	while true do
		UseItemById( nItem )
		System:Sleep( 2000 )
		if DataPool:UserBag_CountItemByIDTable( nItem ) <= 0 then
			break
		end
	end	
	missionComplete2()
end


function getInfoNPCMission2()

	local bFlagFind = false
	local infoObj = { { id , x , y , model } }

	local n = EnumObject(true)

	if n == 0 then
		DbgPrintf( "get Monster miss2 failed" )
		return false
	end
	local nIndex = 1
	for i = 0 , n - 1 do

		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )

		if classObj == "NPC" and nameObj == "幻花妖" then

			bFlagFind = true

			infoObj[ nIndex ] = { id = idObj , x = xObj , y = yObj , model = modelObj }
			DbgPrintf( "id %x , model %x " , infoObj[ nIndex ].id , infoObj[ nIndex ].model )
			nIndex = nIndex + 1

		end
	end
	

	if nIndex - 1 < 3 then
		DbgPrintf( "Find monster different failed : %d " , nIndex )
		return -1 , -1 , -1
	end
	
	for i = 1 , 3 do
		if infoObj[ i ].model == -1 then
			return -1 , -1 , -1
		end
	end

	if bFlagFind == true then

			if infoObj[ 1 ].model == infoObj[ 2 ].model then
				DbgPrintf( "idModel %x " , infoObj[ 3 ].id )
				return infoObj[ 3 ].id , infoObj[ 3 ].x , infoObj[ 3 ].y
			elseif infoObj[ 1 ].model == infoObj[ 3 ].model then
				DbgPrintf( "idModel %x " , infoObj[ 2 ].id )
				return infoObj[ 2 ].id , infoObj[ 2 ].x , infoObj[ 2 ].y
			else
				DbgPrintf( "idModel %x " , infoObj[ 1 ].id )
				return infoObj[ 1 ].id , infoObj[ 1 ].x , infoObj[ 1 ].y
			end

	else
		return -1 , -1 , -1
	end

end
function isMonster()

	local bFlagFind = false

	local n = EnumObject(true)
	if n == 0 then
		DbgPrintf( "isMonster failed" )
		return
	end
	local nIndex = 1
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )

		if classObj == "NPC" and nameObj == "幻花妖" then
			bFlagFind = true
		end
	end
	
	return bFlagFind
	
end

function getInfoMsg( szSubFind )

	local isNext , szMsg = GetDebugMessage()
	while isNext == 1 do
		if string.find( szMsg , szSubFind ) then
			break
		end
		isNext , szMsg = GetDebugMessage()
		System:Sleep( 1000 )
	end
				
	if szMsg == nil then 
		return nil , -1 , -1
	end			
				
	if szMsg == "" then 
		return nil , -1 , -1
	end		
	
	DbgPrintf( "Message debug : %s ", szMsg )

	local nStartCharScene = string.find( szMsg , string.char( 17 ) ) + 1
	local nEndCharScene = string.find( szMsg , string.char( 42 ) , nStartCharScene ) - 1
	local szScene = string.sub( szMsg , nStartCharScene , nEndCharScene )
	DbgPrintf( "Scene: %s " , szScene )

	local nStartCharXpos = nEndCharScene + 3
	local nEndCharXpos = string.find( szMsg , string.char( 42 ) , nStartCharXpos ) - 1
	local xPos = tonumber( string.sub( szMsg , nStartCharXpos , nEndCharXpos ) )


	local nStartCharYpos = nEndCharXpos + 3
	local nEndCharYpos = string.find( szMsg , string.char( 125 ) , nStartCharYpos ) - 1
	local yPos = tonumber( string.sub( szMsg , nStartCharYpos , nEndCharYpos ) )


	DbgPrintf( "InfoMiss2 need: %s(%d,%d) " , szScene , xPos , yPos )

	return szScene , xPos , yPos

end

function execMission2()

	local bFlSpeek = false
	local idObj , x , y
	
	local szScene , xPos , yPos
	while true do
		if DataPool:UserBag_CountItemByIDTable( 0x2626bb9 ) > 0 then
			UseItemById( 0x2626bb9 )
			System:Sleep( 1000 )
			szScene , xPos , yPos = getInfoMsg( "TJRW_100511_13" )
			if xPos ~= -1 and yPos ~= -1 then
				MoveTo( xPos , yPos , DataBase:GetSceneID( szScene ) )
				break
			end
		else
			break
		end
		System:Sleep( 1000 )
	end

	Dismount()

	if DataPool:UserBag_CountItemByIDTable( 0x2626bb9 ) > 0 then
		while true do
			System:Sleep( 1000 )
			UseItemById( 0x2626bb9 )

			idObj , x , y = getInfoNPCMission2()
			if idObj ~= -1 then
				MoveToNPC_ById( x , y , -1 , idObj )
				System:Sleep( 1000 )	
				QuestFrameOptionClicked( "挑战" , CMP_PART_MATCH)
				bFlSpeek = true
				break				
			end
		end
	else
		for i = 0 , 3 do
			System:Sleep( 1000 )
			idObj , x , y = getInfoNPCMission2()
			if idObj ~= -1 then
				MoveToNPC_ById( x , y , -1 , idObj )
				System:Sleep( 1000 )	
				QuestFrameOptionClicked( "挑战" , CMP_PART_MATCH)
				break
			end
		end
	end

	if isMonster() == true then
		System:Sleep( 1000 )
		AI:SetAI("杀怪") 
		AI:SetParameter("目标", "添加" , "幻花妖" ) 
	
		AI:Start(false)
		while true do
			if isMissionComplete( "亦幻亦真" ) > 0 then
--				DbgPrintf( "nParam[0] %d " , isMissionComplete( "亦幻亦真" ) )
				break
			end
			System:Sleep( 1000 )
		end

		AI:Stop()
		System:Sleep( 1000 )
		missionComplete2()
		return true
	else
		return false
	end
end


local g_nIndexElement = -1
function findFriend( nTeam , nIndexFriend )

	if nIndexFriend ~= -1 then
		local nSex = DataPool:GetFriend( nTeam , nIndexFriend , "SEX" )

		if nSex ~= Player:GetMySex() then
	
			if DataPool:GetFriend( nTeam , nIndexFriend , "FRIENDSHIP" ) > 0 then 
				
				local szNameFriend = DataPool:GetFriend( nTeam , nIndexFriend , "NAME" )
				ShowMessage( "请您在好友列表的异性玩家组队伍 %s！是" , tostring( szNameFriend ) )
				System:Sleep( 1000 )
	
			end		
		end
	end
	
end

function execTool( idPlayer )
	local nResult = 0
	if idPlayer ~= -1 then
		Dismount()
		for k = 1 , 2 do
			System:Sleep( 1000 )
			LockTarget( idPlayer )
			System:Sleep( 1000 )
			UseItemById_Target( 0x2626bcf , idPlayer )
			ShowMessage( "使用寻缘镜物品" )
			if DataPool:UserBag_CountItemByIDTable( 0x2626bcf ) <= 0 then
				nResult = 1 
				break
			else
				nResult = 2 
			end
		end
	end
	
	return nResult
end


function _callback( fX , fY , nScene )

	if g_nIndexElement == - 1 then
		return false
	end
	local szName , idPlayer , idMap , xPos , yPos
	while true do
		szName , idPlayer ,  _, idMap , xPos , yPos = DataPool:GetTeamMemInfo( g_nIndexElement )
		if idMap ~= -1 then
			DbgPrintf( "idPlayer2 %x , szName %s , %x(%d,%d) " , idPlayer , szName , idMap , xPos , yPos )
			break
		end
	end

	if nScene ~= idMap then
		return false 
	end

	if nScene == GetActiveDataSceneID()  then
		if fX == 0 and fY == 0 then
			return false
		else
			if fX ~= xPos and fY ~= yPos then
				return false
			end
		end
	end

	return true
end

function gotoFriendMap( nIndexElement )
	local bResult = false
while true do
	System:Sleep( 1000 )	
	local szName , idPlayer , idMap , xPos , yPos	
	szName , idPlayer ,  _, idMap , xPos , yPos = DataPool:GetTeamMemInfo( nIndexElement )

	if szName == "" then
		break
	end

--	DbgPrintf( "idPlayer1 %x , szName %s , %x(%d,%d) " , idPlayer , szName , idMap , xPos , yPos )

	if GetMount() == false then
		DoMount( true )
		System:Sleep( 1000 )
	end
	if idMap ~= -1 then

--		DbgPrintf( "Move to Map %x(%d,%d)" , idMap , xPos , yPos )

		MoveTo( xPos , yPos , idMap , "" , 0 , true , _callback )
		local xCurPos , yCurPos
		szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( nIndexElement )
		if xCurPos == xPos and yCurPos == yPos and xPos ~= 0 and yPos ~= 0 and idPlayer ~= -1 then
			local nExeTool = execTool( idPlayer )
--			ShowMessage( "exeTool %d " , nExeTool )
			if nExeTool == 1 then
				bResult = true
				break
			elseif nExeTool == 2 then
				local nWaitTick = 1
				while true do
					if nWaitTick > 4 then
						break
					end
					szName , idPlayer ,  _, idMap , xPos , yPos = DataPool:GetTeamMemInfo( nIndexElement )
--					DbgPrintf( "Move den Map2 %x(%d,%d)" , idMap , xPos , yPos )
					if szName ~= "" then
						MoveTo( xPos , yPos , idMap , "" , 0 , true , _callback )
						nExeTool = execTool( idPlayer )
						if nExeTool == 1 then
							bResult = true
						end
					end
					System:Sleep( 1000 )
					nWaitTick = nWaitTick + 1
				end
			end
			break
		end
	end
end	
	return bResult
end

function isMemberOk()
	for i = 0 , DataPool:GetTeamMemCount() - 1 do
		local szName , _,  _, _, xPos, yPos = DataPool:GetTeamMemInfo( i )
		if szName ~= "" then
			local nTeamFriend , nIndexFriend = DataPool:GetFriendByName( szName )
			if nTeamFriend >= 1 and nTeamFriend <= 4 then
				local nSex = DataPool:GetFriend( nTeamFriend , nIndexFriend , "SEX" )
				if nSex ~= -1 and nSex ~= Player:GetMySex() and DataPool:GetFriend( nTeamFriend , nIndexFriend , "FRIENDSHIP" ) > 0 then
					return true
				end
			end
		end
	end
	return false
end
function execMission3()
	
	local bOnline = false
	local bMission = false

while true do
	System:Sleep( 1000 )
	local nCountMember = DataPool:GetTeamMemCount()
	local nTick = 1
	if nCountMember <= 1 or isMemberOk() == false then
		for i = 1 , 4 do
			local nCountElement = DataPool:GetFriendOnlineNumber( i )
			if nCountElement ~= 0 then
				for j = 0 , nCountElement do
					findFriend( i , j )
					bOnline = true
				end
			end				
		end
	else
		bOnline = true
	end	
	
	if bOnline == false then
		ShowMessage( "您的好友不在线，请手动找个好友组队来使用任务!" )
	end
	if GetMount() == false then
		DoMount( true )
	end
	
	nCountMember = DataPool:GetTeamMemCount()

	local szName
	local nIndexFriend , nTeamFriend , nSex
	if nCountMember > 1 and isMemberOk() == true then	
		for j = 0 , nCountMember - 1 do
--			DbgPrintf("Current member %d[%d]" , j , tonumber(nCountMember) )

			szName , _,  _, _, xPos, yPos = DataPool:GetTeamMemInfo( j )
			
--			DbgPrintf( "idPlayer %x , szName %s , %x(%d,%d) " , idPlayer , szName , idMap , xPos , yPos )

			nTeamFriend , nIndexFriend = DataPool:GetFriendByName( szName )
			if nTeamFriend > 0 and nTeamFriend < 5 and bMission == false then
				nSex = DataPool:GetFriend( nTeamFriend , nIndexFriend , "SEX" )

				if nSex ~= -1 and nSex ~= Player:GetMySex() then
					if DataPool:GetFriend( nTeamFriend , nIndexFriend , "FRIENDSHIP" ) > 0 then
						ShowMessage( "前往 %s " , szName )
						g_nIndexElement = j
						if gotoFriendMap( j ) == true then
							bMission = true
							break
						end
					end
				end
			end

		end
	end

	if ( isMissionComplete( g_szNameMiss[ 3 ] ) == 1 or isMissionComplete( g_szNameMiss[ 7 ] ) == 1 ) and DataPool:UserBag_CountItemByIDTable( 0x2626bcf ) <= 0 then
		missionComplete2()
		break
	end	

end

end


function buyItemFromShopLest( sItemName , idItem , szNameMiss , max_price , nRow )
	

	local objShop = { nShopID , nPrice , nNum }

	if DataPool:UserBag_CountItemByName( sItemName ) > 0 then
		return 2
	end

	MoveToNPC( 329, 297, 0, "乔复盛" )

	local nResult = 1
	local nIndexOne = -1
	local nIndexBuy = -1
	local now_name , now_shopname , now_shopid , now_num , now_price , now_flag1 , now_flag2
	now_name , now_shopname , now_shopid , now_num , now_price , now_flag1 , now_flag2 = PlayerShop:GetItemPSInfo( 0 )
for b = 0 , 3 do
	System:Sleep( 1000 )
	PlayerShop:SearchItem( 2 , 1 , 1 , tostring( sItemName ) , 1 )
	for i = 0 , nRow - 1 do
		System:Sleep( 100 )
		now_name , now_shopname , now_shopid , now_num , now_price , now_flag1 , now_flag2 = PlayerShop:GetItemPSInfo( i )
				
		if tostring( now_name ) == tostring( sItemName ) then

--			DbgPrintf("Shop[%d] %s , %s, %d, num: %d, price: %d[%d], %d, %d " , i , tostring(now_name) , tostring(now_shopname) , now_shopid , now_num , now_price , max_price , now_flag1, now_flag2 )			

			objShop[ i + 1 ] = { nShopID = now_shopid , nPrice = now_price , nNum = now_num }
			if now_num == 1 then
				nIndexOne = i + 1
				break
			end
		else
			break
		end
	end
	
	if table.getn( objShop ) <= 0 then
		nResult = -1
	end
	if nIndexOne <  0 then
		local nMin = objShop[ 1 ].nNum

		for i = 2 , table.getn( objShop ) do

			if objShop[ i ].nNum < nMin then
				nMin = objShop[ i ].nNum
				nIndexBuy = i
			end
		end
		
	else
		nIndexBuy = nIndexOne

	end


--	DbgPrintf( "nIndexBuy %d , %d " , nIndexBuy , nIndexOne )
	if nIndexBuy == -1 then
		nResult = -1
	else
		nResult = 1
	end

	if nResult ==  1 then
		if objShop[ nIndexBuy ].nPrice > max_price then
			nResult =  -2
		end
		PlayerShop:SearchPageBuyItem( nIndexBuy  ,"item" ) --买
		break
	end
end
	
	local nWaitTick = 1
	while nResult == 1 do 
		System:Sleep( 1000 )
		if idItem == -1 then

			if isMissionComplete( szNameMiss ) == 1 then
--				DbgPrintf( "购买物品成功 " )
				return 2
			end
		else
			if DataPool:UserBag_CountItemByIDTable( idItem ) > 0 then
--				DbgPrintf( "购买物品成功 " )
				return 2
			end
		end
		if nWaitTick > 4 then
			nWaitTick = nWaitTick + 1
			break
		end

	end

	return nResult
end


function buyItemFromShop( sItemName , idItem , max_price , nNumMax )

	if DataPool:UserBag_CountItemByName( sItemName ) > 0 then
		return 2
	end

	MoveToNPC( 329, 297, 0, "乔复盛" )

	local nResult = 0 		--继续
	local now_name , now_shopname , now_shopid , now_num , now_price , now_flag1 , now_flag2

	now_name , now_shopname , now_shopid , now_num , now_price , now_flag1 , now_flag2 = PlayerShop:GetItemPSInfo( 0 )

	PlayerShop:SearchItem( 2 , 1 , 1 , tostring( sItemName ) , 1 )
	local nWaitTick = 0 
	
	while true do

		System:Sleep(1000)
		nWaitTick = nWaitTick + 1	
		local k = 0	
		while true do
			System:Sleep( 1000 )
			now_name , now_shopname , now_shopid , now_num , now_price , now_flag1 , now_flag2 = PlayerShop:GetItemPSInfo( k )

--			DbgPrintf("Shop[%d] %s , %s, %d, num: %d, price: %d[%d], %d, %d " , k , tostring(now_name) , tostring(now_shopname) , now_shopid , now_num , now_price , max_price , now_flag1, now_flag2 )
				
			if tostring( now_name ) == sItemName then
				if now_price < max_price and now_num <= nNumMax and now_num > 0 then
					break
				end
			else
				k = -1
				break
			end
			k = k + 1
		end

		if k < 0 then
			return -1
		end

		if k >= 0 then
			PlayerShop:SearchPageBuyItem( k  ,"item" ) --买
			nResult = 1
			break
		end
		if nWaitTick > 4 then
			break 		--查询超时,也可能是没货
		end
	end

	nWaitTick = 0 
	while nResult == 1 do 
		System:Sleep( 1000 )
		if idItem ~= -1 then
			if DataPool:UserBag_CountItemByIDTable( idItem ) > 0 then
--				DbgPrintf( "物品数量 %s " ,  sItemName )
				return 2
			end
		else
			if isMissionComplete( g_szNameMiss[ 4 ] ) == 1 then
--				DbgPrintf( "购买成功" )
				return 2
			end
		end
		if nWaitTick > 4 then
			break
		end
		nWaitTick = nWaitTick + 1
	end

--	DbgPrintf( "物品数量 %s : %d , nResult %d " ,  sItemName , DataPool:UserBag_CountItemByName( sItemName ) , nResult )

	return nResult
end


function getInfoMission4()

	local nIndex , idMission , dScript
	for i = 1 , 3 do
		nIndex , idMission , dScript = DataPool:GetPlayerMission( g_szNameMiss[ 4 ] )
		if nIndex ~= -1 then
			break
		else
			System:Sleep( 1500 )
		end

	end

	if nIndex == -1 then
		ShowMessage( "Task 4 invalid => the end tesk" )
		System:Sleep( 500 )
		return -1 , -1
	end


	local nParam = {}
	
	for i = 0 , 7 do
		nParam[ i ] = DataPool:GetPlayerMission_Variable( nIndex , i )
--		DbgPrintf( "[%d]%d" , i , nParam[ i ] )
	end
	
	return nIndex , nParam[ 5 ]

end

function execMission4()

	while true do
		if isFullBag( 0 ) == true then
			ShowMessage( "道具包或者材料包已满!" )
		else
			break
		end
		System:Sleep( 1000 )
	end
	local nIndex , nParam	
	while true do
		nIndex , nParam = getInfoMission4()
		if nIndex ~= -1 then
			break
		end
	end
	local szPosString = ""

	szPosString = DataPool:GetPlayerMission_StrList( nIndex , nParam )
	local nStartChar = string.find( szPosString , "#{" ) + 2 
	local nEndChar = string.find( szPosString , "}" , nStartChar ) - 1 

	local szLinkItem = string.sub( szPosString , nStartChar , nEndChar )

	local szItemName = GetDictionaryString( szLinkItem )
--	DbgPrintf( "szItems %s " , szItemName )
	if DataPool:UserBag_CountItemByName( szItemName ) > 0  then
		missionComplete2()		
	else

	local nMoney = Player:GetData( "MONEY" )

		if nMoney <= 0 then
			ShowMessage( "金钱不够！" )
			System:Sleep( 500 )
			bStop = true
			return
		end
		local nResult = buyItemFromShopLest( szItemName , -1 , g_szNameMiss[ 4 ] , nMoney , 10 ) 
--		DbgPrintf( "nResult 去买物品 %d " , nResult )

		if nResult == -1 then
			ShowMessage( "商店没有物品 %s " , szItemName )
			System:Sleep( 1000 )
			bStop = true
			return
		elseif nResult == -2 then
			ShowMessage( "您的金钱不够!")
			System:Sleep( 1000 )
			bStop = true
			return
		elseif nResult == 1 then
			ShowMessage( "已购买可您背包里没有此物品!")
			System:Sleep( 1000 )
			bStop = true
			return			
		elseif nResult == 2 then
--			ShowMessage( "购买成功!" )
			missionComplete2()		
		end
	end
end 

function execMission9()

--	if DataPool:UserBag_CountItemByIDTable( 0x2626BD8 ) > 0 then
--		UseItemById( 0x2626BD8 )
--	end

	ShowMessage( "请您回答谜题！" )
	bStop = true

end

function getIdScriptMission( szNameMission )

	local nIndex , idMission , idScript
	for i = 1 , 3 do
		nIndex , idMission , idScript = DataPool:GetPlayerMission( szNameMission )
		if nIndex ~= -1 then
			break
		else
			System:Sleep( 1500 )
		end

	end

	if nIndex == -1 then
		ShowMessage( "Task %s invalid => the end tesk" , szNameMission )
		System:Sleep( 500 )
		return -1
	end

--	DbgPrintf( "ID script %d " , idScript )
	return idScript

end

function isStop()
		
	System:Sleep( 1000 )
	local bFlag = false
	local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)	
	if strType == "text" then				
		if string.find(szText, "你今天已经完成27环天机任务了") then
			ShowMessage("你今天已经完成27环天机任务了，不能接受更多的天机任务，请明天再来！")
			System:Sleep( 1000 )
			bFlag = true
		else
			bFlag = false
		end
	end
	return bFlag
end


function _SMain( ... )

	local bItem = false
	local nItemMiss = 0x1d77f79

	UI:WindowHide()

--[[	
buyItemFromShop( "九宫盒" , 0x1d77f79 , Player:GetData( "MONEY" ) )
if 1== 1 then
return
end
]]--


while true do

	if bStop then
		ShowMessage( "停止脚本！" )
 		return
	end
	System:Sleep( 1000 )
	
	local idCurItem , nIndexMiss = getCurrentMiss()

	if DataPool:UserBag_CountItemByIDTable( nItemMiss ) <= 0 and idCurItem == -1 then
		local nMoney = Player:GetData( "MONEY" )
		if nMoney <= 0  then
			ShowMesage( "金钱不够!" )
			System:Sleep( 1000 )
			return
		end

		MoveToNPC( 330 , 299 ,  0x00 , "乔复盛" ) 

		while true do
			if isFullBag( 0 ) == false then
				break
			end
			System:Sleep( 1000 )
		end

		for i = 1 , 3 do
			local szItemName = "九宫盒"

			local nResult = buyItemFromShop( szItemName , 0x1d77f79 , nMoney , 5 ) 
--			DbgPrintf( "nResult 去买物品 %d " , nResult )

			if nResult == 2 then
				bItem = true
				break				
			else
				bStop = true
				break
			end
			System:Sleep( 1000 )
		end			

	else
		bItem = true
	end

	if bItem == false then
		ShowMessage( "没有您需要的物品或者金钱不够，请再检查一下！" )
		return
	end
--	DbgPrintf( "idCurrent1 %x , idex %d " , idCurItem , nIndexMiss )
	local idItemRubik = 0x2626bd0
	for i = 0 , 8 do
		if DataPool:UserBag_CountItemByIDTable( idItemRubik + i ) > 0 then
			if i == 0 then
				if isMissionComplete( g_szNameMiss[ 1 ] ) ~= -1 then
					nIndexMiss = 1
					idCurItem = idItemRubik + i
					break
				end
			elseif i == 1 then
				if isMissionComplete( g_szNameMiss[ 2 ] ) ~= -1 then
					nIndexMiss = 2
					idCurItem = idItemRubik + i
					break
				end
			elseif i == 2 then
				if isMissionComplete( g_szNameMiss[ 3 ] ) ~= -1 or isMissionComplete( g_szNameMiss[ 7 ] ) ~= -1 then
					nIndexMiss = 3
					idCurItem = idItemRubik + i
					break
				end
			elseif i == 3 then
				if isMissionComplete( g_szNameMiss[ 4 ] ) ~= -1 then
					nIndexMiss = 4
					idCurItem = idItemRubik + i
					break
				end
			elseif i == 4 then
				if isMissionComplete( g_szNameMiss[ 1 ] ) ~= -1 then
					nIndexMiss = 5
					idCurItem = idItemRubik + i
					break
				end
			elseif i == 5 then

				if isMissionComplete( g_szNameMiss[ 2 ] ) ~= -1 then
					nIndexMiss = 6
					idCurItem = idItemRubik + i
					break
				end
			elseif i == 6 then
				if isMissionComplete( g_szNameMiss[ 3 ] ) ~= -1 or isMissionComplete( g_szNameMiss[ 7 ] ) ~= -1 then
					nIndexMiss = 7
					idCurItem = idItemRubik + i
					break
				end
			elseif i == 7 then
				if isMissionComplete( g_szNameMiss[ 4 ] ) ~= -1 then
					nIndexMiss = 8
					idCurItem = idItemRubik + i
					break
				end
			elseif i == 8 then
				if isMissionComplete( g_szNameMiss[ 9 ] ) ~= -1 then
					nIndexMiss = 9
					idCurItem = idItemRubik + i
					break
				end		
			end
		end	
	end

	if idCurItem == -1 then
		while true do
			System:Sleep( 1000 )
			UseItemById( nItemMiss )
			System:Sleep( 1000 )
			QuestFrameOptionClicked( "第一宫：神秘拼图" , CMP_PART_MATCH )	
			if isStop() == true then
				bStop = true
				break
			end
			QuestFrameMissionClose()
			idCurItem , nIndexMiss = getCurrentMiss()

			if idCurItem == 0x2626bd0 then
				break
			end
		end

	end

--	DbgPrintf( "idCurrent %x , idex %d " , idCurItem , nIndexMiss )
	
	if nIndexMiss == 1 then

		local MissionFlag = isMissionComplete( g_szNameMiss[ 1 ] ) 
		local idMapMiss1
		if MissionFlag == -1 then				
			while true do
				System:Sleep( 1000 )
				UseItemById( idCurItem )
				System:Sleep( 1000 )
				QuestFrameOptionClicked( "第一宫：神秘拼图" , CMP_PART_MATCH )

				System:Sleep( 1000 )
				if isStop() == true then
					bStop = true
					break
				end

				QuestFrameMissionClose()
				idMapMiss1 = isItemMapMission1()
				if idMapMiss1 ~= -1 then
					break
				end
			end

			execMission1( idMapMiss1 )

		elseif MissionFlag == 0 then
			System:Sleep( 1000 )
			local idMapMiss1 = isItemMapMission1()
			if idMapMiss1 ~= -1 then
				execMission1( idMapMiss1 )
			else
--				DbgPrintf( "Stop : %d " ,  idCurItem )
				return
			end
		elseif MissionFlag == 1 then
			missionComplete2()
		elseif MissionFlag == 2 then
			

			local idScript = getIdScriptMission( g_szNameMiss[ 1 ] )				
			if idScript ~= -1 then
				MissionBlockProcess( idScript )
			else
--				DbgPrintf( "idScript = -1" )
				return
			end
		else
--			DbgPrintf( "Stop2 : %d " ,  idCurItem )
			return
		end

	elseif nIndexMiss == 2 then

		local MissionFlag = isMissionComplete( g_szNameMiss[ 2 ] ) 
		if MissionFlag == -1 then
			while true do
				System:Sleep( 1000 )
				UseItemById( idCurItem )
				System:Sleep( 1000 )
				QuestFrameOptionClicked( "第二宫：亦幻亦真" , CMP_PART_MATCH )

				System:Sleep( 1000 )
				if isStop() == true then
					bStop = true
					break
				end

				QuestFrameMissionClose()

				if DataPool:UserBag_CountItemByIDTable( 0x2626bb9 ) > 0 then
					break
				end
			end
			System:Sleep( 1000 )	
			execMission2()
		elseif MissionFlag == 0 then

			if execMission2() == false then
				local idScript = getIdScriptMission( g_szNameMiss[ 2 ] )				
				if idScript ~= -1 then
					MissionBlockProcess( idScript )
				else
--					DbgPrintf( "idScript = -1" )
					return
				end
			end

		elseif MissionFlag == 1 then
			missionComplete2()
		elseif MissionFlag == 2 then

			local idScript = getIdScriptMission( g_szNameMiss[ 2 ] )				
			if idScript ~= -1 then
				MissionBlockProcess( idScript )
			else
--				DbgPrintf( "idScript = -1" )
				return
			end

		else
			return
		end

	elseif nIndexMiss == 3 then

		local MissionFlag = isMissionComplete( g_szNameMiss[ 3 ] ) 
		
		if MissionFlag == -1 then
			MissionFlag = isMissionComplete( g_szNameMiss[ 7 ] ) 
		end

		if MissionFlag == -1 then
			while true do

				System:Sleep( 1000 )
				UseItemById( idCurItem )
				System:Sleep( 1000 )
				QuestFrameOptionClicked( "第三宫：众里寻" , CMP_PART_MATCH ) --她

				System:Sleep( 1000 )
				if isStop() == true then
					bStop = true
					break
				end

				QuestFrameMissionClose()

				if DataPool:UserBag_CountItemByIDTable( 0x2626bcf ) > 0 then
					break
				end

			end
			execMission3()
		elseif MissionFlag == 0 then
			System:Sleep( 1000 )
			execMission3()

		elseif MissionFlag == 1 then
			missionComplete2()
		elseif MissionFlag == 2 then
			local idScript = getIdScriptMission( g_szNameMiss[ 3 ] )
			if idScript == -1 then
				idScript = getIdScriptMission( g_szNameMiss[ 7 ] )
			end

			if idScript ~= -1 then
				MissionBlockProcess( idScript )
			else
--				DbgPrintf( "idScript = -1" )
				return
			end
		else
--			DbgPrintf( "Stop2 : %d " ,  idCurItem )
			return
		end


	elseif nIndexMiss == 4 then

		local MissionFlag = isMissionComplete( g_szNameMiss[ 4 ] ) 
		if MissionFlag == -1 then
			while true do
				System:Sleep( 1000 )
				UseItemById( idCurItem )
				System:Sleep( 1000 )
				QuestFrameOptionClicked( "第四宫：寻寻觅觅" , CMP_PART_MATCH )

				System:Sleep( 1000 )
				if isStop() == true then
					bStop = true
					break
				end

				QuestFrameMissionClose()

				if isMissionComplete( g_szNameMiss[ 4 ] ) == 0 then
					break
				end
			end
			execMission4()
		elseif MissionFlag == 0 then
			execMission4()
		elseif MissionFlag == 1 then
			missionComplete2()
		elseif MissionFlag == 2 then
			
			local idScript = getIdScriptMission( g_szNameMiss[ 4 ] )				
			if idScript ~= -1 then
				MissionBlockProcess( idScript )
			else
--				DbgPrintf( "idScript = -1" )
				return
			end
		else
--			DbgPrintf( "Stop2 : %d " ,  idCurItem )
			return
		end

	elseif nIndexMiss == 5 then

		local MissionFlag = isMissionComplete( g_szNameMiss[ 1 ] ) 
		local idMapMiss1
		if MissionFlag == -1 then				
			while true do
				System:Sleep( 1000 )
				UseItemById( idCurItem )
				System:Sleep( 1000 )
				QuestFrameOptionClicked( "第五宫：神秘拼图" , CMP_PART_MATCH )

				System:Sleep( 1000 )
				if isStop() == true then
					bStop = true
					break
				end

				QuestFrameMissionClose()

				idMapMiss1 = isItemMapMission1()
				if idMapMiss1 ~= -1 then
					break
				end


			end

			execMission1( idMapMiss1 )

		elseif MissionFlag == 0 then

			local idMapMiss1
			while true do
				idMapMiss1 = isItemMapMission1()
				if idMapMiss1~= -1 then
					break
				end
				System:Sleep( 1000 )
			end
			execMission1( idMapMiss1 )


		elseif MissionFlag == 1 then
			missionComplete2()
		elseif MissionFlag == 2 then

			local idScript = getIdScriptMission( g_szNameMiss[ 1 ] )				
			if idScript ~= -1 then
				MissionBlockProcess( idScript )
			else
--				DbgPrintf( "idScript = -1" )
				return
			end

		else
--			DbgPrintf( "Stop2 : %d " ,  idCurItem )
			return
		end


	elseif nIndexMiss == 6 then
		local MissionFlag = isMissionComplete( g_szNameMiss[ 2 ] ) 
		if MissionFlag == -1 then
			while true do
				System:Sleep( 1000 )
				UseItemById( idCurItem )
				System:Sleep( 1000 )
				QuestFrameOptionClicked( "第六宫：亦幻亦真" , CMP_PART_MATCH )

				System:Sleep( 1000 )
				if isStop() == true then
					bStop = true
					break
				end

				QuestFrameMissionClose()

				if DataPool:UserBag_CountItemByIDTable( 0x2626bb9 ) > 0 then
					break
				end
			end
			execMission2()
		elseif MissionFlag == 0 then

			if execMission2() == false then
				local idScript = getIdScriptMission( g_szNameMiss[ 2 ] )				
				if idScript ~= -1 then
					MissionBlockProcess( idScript )
				else
--					DbgPrintf( "idScript = -1" )
					return
				end
			end

		elseif MissionFlag == 1 then
			missionComplete2()
		elseif MissionFlag == 2 then

			local idScript = getIdScriptMission( g_szNameMiss[ 2 ] )				
			if idScript ~= -1 then
				MissionBlockProcess( idScript )
			else
--				DbgPrintf( "idScript = -1" )
				return
			end

		else
			return
		end

	elseif nIndexMiss == 7 then
	
		local MissionFlag = isMissionComplete( g_szNameMiss[ 3 ] ) 
		
		if MissionFlag == -1 then
			MissionFlag = isMissionComplete( g_szNameMiss[ 7 ] ) 
		end

		if MissionFlag == -1 then
			while true do
				System:Sleep( 1000 )
				UseItemById( idCurItem )
				System:Sleep( 1000 )
				QuestFrameOptionClicked( "第七宫：众里寻" , CMP_PART_MATCH ) --她

				System:Sleep( 1000 )
				if isStop() == true then
					bStop = true
					break
				end

				QuestFrameMissionClose()
				if DataPool:UserBag_CountItemByIDTable( 0x2626bcf ) > 0 then
					break
				end
			end
			execMission3()
		elseif MissionFlag == 0 then
			System:Sleep( 1000 )
			execMission3()

		elseif MissionFlag == 1 then
			missionComplete2()
		elseif MissionFlag == 2 then
			local idScript = getIdScriptMission( g_szNameMiss[ 3 ] )
			if idScript == -1 then
				idScript = getIdScriptMission( g_szNameMiss[ 7 ] )
			end

			if idScript ~= -1 then
				MissionBlockProcess( idScript )
			else
--				DbgPrintf( "idScript = -1" )
				return
			end
		else
--			DbgPrintf( "Stop2 : %d " ,  idCurItem )
			return
		end

	elseif nIndexMiss == 8 then
	
		local MissionFlag = isMissionComplete( g_szNameMiss[ 4 ] ) 
		if MissionFlag == -1 then
			while true do
				System:Sleep( 1000 )
				UseItemById( idCurItem )
				System:Sleep( 1000 )
				QuestFrameOptionClicked( "第八宫：寻寻觅觅" , CMP_PART_MATCH )

				System:Sleep( 1000 )
				if isStop() == true then
					bStop = true
					break
				end

				QuestFrameMissionClose()
				if isMissionComplete( g_szNameMiss[ 4 ] ) == 0 then
					break
				end
			end
			execMission4()
		elseif MissionFlag == 0 then
			execMission4()
		elseif MissionFlag == 1 then
			missionComplete2()
		elseif MissionFlag == 2 then
			

			local idScript = getIdScriptMission( g_szNameMiss[ 4 ] )				
			if idScript ~= -1 then
				MissionBlockProcess( idScript )
			else
--				DbgPrintf( "idScript = -1" )
				return
			end

		else
--			DbgPrintf( "Stop2 : %d " ,  idCurItem )
			return
		end

	elseif nIndexMiss == 9 then 

		System:Sleep( 1000 )
	
		local MissionFlag = isMissionComplete( g_szNameMiss[ 9 ] ) 
		if MissionFlag == -1 then
			while true do
				UseItemById( idCurItem )
				System:Sleep( 1000 )
				QuestFrameOptionClicked( "第九宫：九宫谜题" , CMP_PART_MATCH )

				System:Sleep( 1000 )
				if isStop() == true then
					bStop = true
					break
				end

				QuestFrameMissionClose()
				if isMissionComplete( g_szNameMiss[ 9 ] ) == 0 or DataPool:UserBag_CountItemByIDTable( 0x2626bba ) > 0 then
					break
				end
				System:Sleep( 1000 )
			end
			System:Sleep( 1000 )
			execMission9()
		elseif MissionFlag == 0 then
			System:Sleep( 1000 )
			execMission9()
		elseif MissionFlag == 1 then
			missionComplete2()
		elseif MissionFlag == 2 then

			local idScript = getIdScriptMission( g_szNameMiss[ 9 ] )				
			if idScript ~= -1 then
				MissionBlockProcess( idScript )
			else
--				DbgPrintf( "idScript = -1" )
				return
			end
		else
--			DbgPrintf( "Stop2 : %d " ,  idCurItem )
			return
		end
	end	

end

end
_SMain()
