<?xml version="1.0" encoding="utf-8"?>
<waypoints>
<onload>
	local When_Finished = "nothing"
	
	
	
	
	
	
--
  local tablesize = 0
  local furniture = {}
  local objectList = nil
  local objSize = 0;
	

local function gotolyliya()
	 local lyliya;
	repeat
			lyliya = player:findNearestNameOrId(120993)
	until lyliya ~= nil
	lylx = lyliya.X
	lyly = lyliya.Y
	lylz = lyliya.Z
	teleport(lylx,lylz,lyly+15)
end
local function faceit()

		player:updateTargetPtr()
		local target = CPawn.new(player.TargetPtr)
		target:updateXYZ()
		local angle = math.atan2(target.Z - player.Z, target.X - player.X);
		local yangle = math.atan2(target.Y - player.Y, ((target.X - player.X)^2 + (target.Z - player.Z)^2)^.5 );
		player:faceDirection(angle, yangle);
		camera:setRotation(angle);
end
local proc = getProc()

local function checkclicked(address)
	local tmp = memoryReadRepeat("int", proc, address + addresses.pawnAttackable_offset) or 0;
	if bitAnd(tmp,0x8) then
		return true
	else
		return false
	end
end

local function newOne(ID)
	local objectListK = CObjectList();
	objectListK:update();
	local objSize = objectListK:size()
	for i = 0,objSize do 
		local obj = objectListK:getObject(i)
		if(obj ~= nil)then
			obj:update()
			if (obj.Id == ID and checkclicked(obj.Address)) then 
				player:target(obj)
				 faceit()
				RoMScript("UseExtraAction(1)")
				yrest(1000)		
			end
			obj:update()
			
		end
	end
end
local function castit(what)
	print("Go to ".. what)
	local todo	
	
	todo = player:findNearestNameOrId(what)
	
	if todo then
		todo:update();
		local x1 = todo.X - player.X
		local z1 = todo.Z - player.Z
		local y1 = todo.Y - player.Y
		
		local length = math.sqrt( math.pow(x1,2) + math.pow(z1,2) + math.pow(y1,2))
		local fraction = (length-5)/length
		x1 = x1 * fraction
		z1 = z1 * fraction
		
		local x2 = x1 + player.X
		local z2 = z1 + player.Z
		teleport(x2,z2,todo.Y+10)
		
		if( distance(player.X, player.Z,player.Y, todo.X, todo.Z, todo.Y) > 45 ) then
			local success, reason = player:moveTo(todo, true);
			if(success == false)then
				teleport(x2,z2,todo.Y+10)
			end
		end
		repeat
			if( distance(player.X, player.Z,player.Y, todo.X, todo.Z, todo.Y) > 45 ) then
				teleport(x2,z2,todo.Y+10)
			end
			newOne(todo.Id) -- we cast	
		until not player:findNearestNameOrId(todo.Id)
		teleport(lylx,lylz,lyly+15)
   else
      print("not found!")
   end
end
local function memorize()

	repeat
		objectList = CObjectList();
		objectList:update();
		objSize = objectList:size()
	until objectList~=nil and objSize~=nil and objSize > 1
      for i = 0,objSize do
         local obj = objectList:getObject(i);
         obj:update()
         if obj.Name and obj.Name ~= "" and obj.Type ~= PT_PLAYER and obj.Name ~= "Lyliya" then
            print(obj.Name .. " " .. obj.X .. " " .. obj.Z .. " " .. obj.Y)
            table.insert(furniture, table.copy(obj))
            tablesize = tablesize + 1
         end
         player:update();
      end
end
local function takeaction()
	local tablesize2 = 0
	local furniture2 = {}
	 fly()
	repeat
		objectList:update();
		objSize = objectList:size()
	until objectList~=nil and objSize~=nil and objSize > 1
	
	for i = 0,objSize do
         local obj = objectList:getObject(i);
         obj:update()
         if obj.Name and obj.Name ~= "" and obj.Type ~= PT_PLAYER and obj.Name ~= "Lyliya" then
            print(obj.Name .. " " .. obj.X .. " " .. obj.Z .. " " .. obj.Y)
            table.insert(furniture2, table.copy(obj))
            tablesize2 = tablesize2 + 1
         end
         player:update();
	end
	print("Action")
	for ka,va in ipairs(furniture2) do
        local cc = 0
		local foundflag = false
         for kb,vb in ipairs(furniture) do
            cc=cc+1
            if va.Name == vb.Name then
               -- object was here and is here again
               print(va.Name)
               if 1 > math.abs(va.X - vb.X)  and 1 > math.abs (va.Z - vb.Z) then
                  -- it is on same position as before
				  foundflag = true
               else
					castit(va.Name)
				   foundflag = true
                  break
				 
               end
            end
			   
         end
		if(   foundflag == false)then
            castit(va.Name)
        end
		 
	end
	
	tablesize = 0
	tablesize2 = 0
	furniture = {}
	furniture2 = {}
	
end
local function mainroutine()
	while true do
		yrest(1500)
		print("Memorize room")
		memorize()
    
		repeat
			yrest(100)
		until RoMScript("TimeKeeperFrame:IsVisible()")
		yrest(1000)
		print("compare room")
		takeaction()
	
		repeat
			yrest(100)
		until not RoMScript("TimeKeeperFrame:IsVisible()")
		print("We go to lyliya")
		yrest(100)
		gotolyliya()
		repeat
			yrest(100)
		until player:target_NPC(120993);
		if( RoMScript("GetNumSpeakOption()")> 2)then
			yrest(500)
			sendMacro("ChoiceOption(2);");
		else
			break;
		end
   end
end
	if getZoneId() == 2 then
		repeat
			yrest(100)
		until player:target_NPC(120992);
		sendMacro("ChoiceOption(1);");
		waitForLoadingScreen(30);
	end
	if getZoneId() == 361  then
		 local lyliya;
		repeat
			lyliya = player:findNearestNameOrId(120993)
		until lyliya ~= nil
		lylx = lyliya.X
		lyly = lyliya.Y
		lylz = lyliya.Z
		teleport(lylx,lylz,lyly+15)
		yrest(500)
		print("Start game")
		repeat
			yrest(100)
		until player:target_NPC(120993);
		if(RoMScript("GetNumSpeakOption()")> 2)then
			sendMacro("ChoiceOption(2);");
			fly()
			mainroutine()
		else
			flyoff()
			sendMacro("ChoiceOption(1);");
			waitForLoadingScreen(30);
		end
	else
		flyoff()
		loadPaths(When_Finished)
	end
	
	
	flyoff()
	
	repeat
		yrest(100)
	until player:target_NPC(120993);
	sendMacro("ChoiceOption(1);");
	waitForLoadingScreen(30);
	repeat
		yrest(100)
	until player:target_NPC(120992);
	loadPaths(When_Finished)
</onload>
</waypoints>