<?xml version="1.0" encoding="utf-8"?><waypoints type="TRAVEL">
	<!-- The Andor Training Range event v1.5 -->
	<onLoad>
		When_Finished = "charlist"
		-- "end" to end script, "relog" to log next character, "waypointfilename" to load that waypointfile

		Training = "timed"
		-- Set to "timed" or "arena" for select based on what reward we already have?

		stop_at = 3300
		-- Stop around this score
 
		Exit_After_Finished = false		-- Set to false or true
		-- Exit, Get rewards and go to mailbox

		MailReceiver = "Zandorya"		-- Set mail receiver name
		-- Mail Receiver

		-- Set Char List
		SetCharList({ [1] = {
						{account=31,chars={}}, {account=32,chars={}}, {account=33,chars={}},
						},
					[2] = {
						{account=36,chars={}}, {account=37,chars={}}, {account=38,chars={1,2}}, {account=39,chars={1}},
						},
					});
		PrintCharList();
		
		Guild_Items = {202916, 203450, 206590, 206591, 206592, 207326, 207330, 207930};

		-------------------------------------------------------------------------------

		-- Table of mobs in KILL order: newt, tortoise, armored, rabbit, ammo
		obj_table = {{106882, 106881}, {106883, 106884, 106885}}

		-- Multilang text
		ChoiceConfirm = RoMScript("TEXT('SKWERL_MOBIUS_23')")
		ChoiceTransport = RoMScript("TEXT('SKWERL_MOBIUS_24')")
		ChoiceParticipate = RoMScript("TEXT('SKWERL_MOBIUS_25')")
		ChoiceExchangeCertificates = RoMScript("TEXT('SKWERL_MOBIUS_47')")
		ChoiceTimed = RoMScript("TEXT('SKWERL_MOBIUS_14')")
		ChoiceArena = RoMScript("TEXT('SKWERL_MOBIUS_15')")
		ChoiceSelect = (Training == "timed" and ChoiceTimed) or (Training == "arena" and ChoiceArena)
		ChoiceGoBackToVaranas = RoMScript("string.sub(TEXT('SKWERL_MOBIUS_39'), 1, string.find(TEXT('SKWERL_MOBIUS_39'), '[', 1, true)-1);")

		-- get mobs
		function getMobs(idTable)
			local objectList = CObjectList()
			local mobsTable = {}

			objectList:update()
			for i = 0,objectList:size() do
				local obj = objectList:getObject(i)
				if( obj ~= nil ) then
					for __, objtbl in pairs(idTable) do
						for __, objid in pairs(objtbl) do
							if( obj.Address ~= ignore and obj.Id == objid) then
								table.insert(mobsTable, obj)
							end
						end
					end
				end
			end
			return mobsTable
		end;

		function checkclicked(address)
			local tmp = memoryReadRepeat("int", getProc(), address + addresses.pawnAttackable_offset) or 0;
			if bitAnd(tmp,0x8) then
				return true
			else
				return false
			end
		end;

		function killIt(obj)
			if obj.Id ~= 0 then
				obj:update()
				if obj.Id ~= 0 then		
					player:target(obj)					
					RoMScript("UseExtraAction(1)")
					yrest(50)
				end
			end
		end;
			
		function getScore()
			player:updateBuffs()
			local buff = player:getBuff(622402)	-- score
			if buff then
				return buff.Level
			end
			return 0
		end;

		function getSlack()
			player:updateBuffs()
			local buff = player:getBuff(622576)		-- slack
			if buff then
				return buff.Level
			end
			return 0
		end;
		
		function getAndorTraining()
			cprintf(cli.white, "getAndorTraining.\n")
			-- Repeat until we fail
			local first = true
			repeat
				player:update()
				local killTable
	
				-- Wait for mobs to spawn, i.e. 45 of them on field	
				cprintf(cli.lightblue, "Scanning for mobs, ")
				local starttimer = os.clock()
				yrest(5000)
				repeat
					killTable = getMobs(obj_table)
					yrest(200)
				until #killTable >= 45 or not (first or player:getBuff(622402)) or os.clock() - starttimer > 10
				first = false

				-- We finished?
				if( #killTable >= 45 or #killTable > 0 and os.clock() - starttimer > 10 ) then
					cprintf(cli.lightblue, "found all %d mobs.\n", #killTable)
					cprintf(cli.lightblue, "Wait for mobs to spawn %d seconds.\n", os.clock() - starttimer)
				else
					cprintf(cli.lightblue, "finished\n")
					break					
				end

				-- We reached the limit?
				score = getScore()
				if stop_at > 0 and  score > stop_at then
					cprintf(cli.red, "We reached the break limit (%d > %d)!\n", score, stop_at)						
					break
				end

				-- Wait for adds to move
				killTable[1]:update()
				local Px = killTable[1].X
				local Py = killTable[1].Y
				local starttimer = os.clock() yrest(3000)
				repeat
					yrest(200)
					killTable[1]:update()
				until Px ~= killTable[1].X or Py ~= killTable[1].Y or os.clock() - starttimer > 4
				cprintf(cli.lightblue, "They'r on the move %d seconds.\n", os.clock() - starttimer)

				-- Training, kill mobs in order, newts tortoises, rest
				for __,objtbl in pairs(obj_table) do
					repeat
						local killed = 0
						for __,objid in pairs(objtbl) do
							for k,obj in pairs(killTable) do
								if obj.Id ~= 0 and checkclicked(obj.Address) then
									obj:update()
									if( obj.Id ==  objid ) then
										killIt(obj)
										killed = killed + 1
									end
								end
							end
						end
					until killed == 0

					score = getScore()
					slack = getSlack()
	
					-- We reached the limit?
					if stop_at > 0 and  score > stop_at then						
						break
					end
				end

				-- Print status
				if stop_at > 0 and  score > stop_at then
					cprintf(cli.red, "We reached the break limit (%d > %d)!\n", score, stop_at)
				else
					cprintf(cli.lightblue, "Score is %d, we have %d slack left\n", score, slack)
				end

			until stop_at > 0 and  score > stop_at

			-- Leave again
			repeat	
				yrest(500)
				player:updateBuffs()
			until not player:getBuff(622402)	-- score
			yrest(2000)
			RoMScript("CloseWindows()")
			yrest(1000)
			RoMScript("CloseWindows()")
			AndorTraining = 1
		end;

		-- When Finished
		function WhenFinished()
			if When_Finished == "relog" then
				sendMacro("}LoginNextToon=true;a={")
				sendMacro("Logout();");
				waitForLoadingScreen();
				loadPaths(__WPL.FileName);
			elseif When_Finished == "charlist" then
				--SetCharList(CharList)
				local currentChannel = RoMScript("GetCurrentParallelID()")
				printf("Current channel %d\n", currentChannel)
				SetChannelForLogin(currentChannel)
				if( getZoneId() ~= 360 ) then
					sendMacro("Logout();"); yrest(8000);
				end
				LoginNextChar()
				loadPaths(__WPL.FileName);
			elseif When_Finished == "end" then
				error("Ending script",2)
			elseif When_Finished == "Use_TP_Skill" then
				Cur_Zone = getZoneId()
				if Cur_Zone == 360 or Cur_Zone == 2 and getZoneId() ~= 13 then	-- in Andor Training or Varanas
					print("Use teleport skill.")
					sendMacro("CastSpellByName(TEXT('Sys540193_name'));");
					waitForLoadingScreen(30);
				end
				sendMacro("}LoginNextToon=true;a={")
				sendMacro("Logout();");
				waitForLoadingScreen();
				loadPaths(__WPL.FileName);
			else
				if not string.find(When_Finished,".", 1, true) then
					When_Finished = When_Finished .. ".xml"
				end
				local filename = getExecutionPath() .. "/waypoints/" .. When_Finished
				local file, err = io.open(filename, "r");
				if file then
					file:close();
					loadPaths(When_Finished)
				else
					error("'When_Finished' option is invalid",0)
				end
			end
		end;

		-------------------------Start-The-Andor-Training-Range-event-----------------------
		-- player info
		cprintf(cli.white, player.Name.."-"..RoMScript("GetAccountName();")..": Started The Andor Training Range event.\n")

		-- Move to Malatina
		if inventory:itemTotalCount(205792) >= 1 and getZoneId() == 2 then
			player:target_NPC(111813) yrest(1000)
			ChoiceOptionByName("Обмен предметов"); yrest(1000)
			while inventory:getItemCount(205792) > 0 do
				ChoiceOptionByName("Обмен предметов"); yrest(1000)
				ChoiceOptionByName("обменять Карта привидения"); yrest(1000)
				ChoiceOptionByName("обменять на Рубин гильдии"); yrest(1000)
				ChoiceOptionByName("обменять 100");
				ChoiceOptionByName("обменять 50");
				ChoiceOptionByName("обменять 10");
				ChoiceOptionByName("обменять 5");
				ChoiceOptionByName("обменять 1");
				ChoiceOptionByName("Назад");
				inventory:update()
			end
		end;

		-- Move to Kate Wesker and enter
		if inventory:itemTotalCount(203038) >= 30 and getZoneId() == 2 then
			player:target_NPC(121035) yrest(1000)		-- Kate Wesker
			ChoiceOptionByName(ChoiceTransport); yrest(1000)			-- Transport to Andor Training Range
			waitForLoadingScreen(30) yrest(2000)
			player:update()
		end;

		-- Move to Magelly Basac and get Andor Training
		AndorTraining = 0
		if ( getZoneId() == 360 and 50 > distance(player.X,player.Z,-37,324) ) then
			AndorTraining = 360
			cprintf(cli.white, "Move to Magelly Basac.\n")
			-- Move to Magelly Basac
			player:target_NPC(120687); yrest(1000)		-- Magelly Basac
			-- Check if we already completed
			cprintf(cli.white, "ChoiceOptionByName: %s.\n", ChoiceSelect)
			if ChoiceOptionByName(ChoiceSelect) then					-- Select based on what reward we already have?
				yrest(1000)
				ChoiceOptionByName(ChoiceParticipate); yrest(1000)		-- Participate in training
				ChoiceOptionByName(ChoiceConfirm); yrest(1000)			-- Confirm
				getAndorTraining()
			end
		elseif ( getZoneId() == 360 and distance(player.X,player.Z,-37,324) > 50 ) then
			AndorTraining = 360
			getAndorTraining()
		end;

		-- Check inventory and Log result
		local filename = getExecutionPath() .. "/logs/AndorTraining.log";
		local file, err = io.open(filename, "a+");
		ShellItemFounded = 0
		if file then
			inventory:update()
			for slot = 61, 240 do
				item = inventory.BagSlot[slot]
				if item.RequiredLvl >= 70 and item.Quality >= 3 and (item.ObjType == 0 or item.ObjType == 1) then
					ShellItemFounded = ShellItemFounded + 1
				end
			end
			file:write("Account: "..RoMScript("GetAccountName()").." \tName: " ..string.format("%-10s",player.Name ).." \tDate: "..os.date()..
			" \tShells total: "..inventory:getItemCount(240181).." \tPhirius Coins: "..inventory:getItemCount(203038)..
			" \tPurified Fusion Stone: "..inventory:getItemCount(202881).." \tEmptie bag space: "..inventory:itemTotalCount(0)..
			" \tCharges: "..RoMScript("GetMagicBoxEnergy()").." \tShell Item founded: "..ShellItemFounded.." \tAndor Training: "..AndorTraining.." \n\n")
			file:close();
		end;

		-- Move to Magelly Basac and Go back to Varanas
		if( Exit_After_Finished and getZoneId() == 360 ) then
			player:target_NPC(120687); yrest(1000)		-- Magelly Basac
			ChoiceOptionByName(ChoiceGoBackToVaranas); yrest(1000)		-- Go back to Varanas
			sendMacro('StaticPopup_OnClick(StaticPopup1, 1);'); yrest(1000)
			waitForLoadingScreen()
		-- Do we have enough Phirius coins
		elseif( 30 > inventory:itemTotalCount(203038) and getZoneId() ~= 13 ) or ( ShellItemFounded > 0 and 450 > inventory:getItemCount(203038) ) then
			cprintf(cli.yellow, "Not enough phirius token coins or Item founded.\n")
			print("Use teleport skill.")
			sendMacro("CastSpellByName(TEXT('Sys540193_name'));");		-- Use teleport skill to "Heffner Camp"
			waitForLoadingScreen(30) yrest(2000)
			player:update()
			__WPL:setWaypointIndex(__WPL:findWaypointTag("ToHugope"));
			return
		elseif( getZoneId() == 13 ) then
			if 50 > distance(player.X,player.Z,-7095,-4126) then
				__WPL:setWaypointIndex(__WPL:getNearestWaypoint(player.X, player.Z));
				return
			else
				WhenFinished()
				return
			end
		end;

		-- Get rewards
		if( getZoneId() == 2 ) then
			-- check item count of lock and key
			while inventory:getItemCount(241647) > 0 and inventory:getItemCount(241648) > 0 do
				player:target_NPC(121035); yrest(1000)	-- Kate Wesker
				ChoiceOptionByName(ChoiceExchangeCertificates); yrest(1000)		-- exchange certificates
			end
		end;

		-- Completed
		if Exit_After_Finished and getZoneId() ~= 13 and (inventory:getItemCount(206590) > 0 or inventory:getItemCount(206591) > 0 or inventory:getItemCount(206592) > 0
			or inventory:getItemCount(207326) > 0 or inventory:getItemCount(207330) > 0 or inventory:getItemCount(207930) > 0 ) then
			__WPL:setWaypointIndex(__WPL:findWaypointTag("ToMailbox"));
		else
			WhenFinished()
		end;
	</onLoad>
	
<!-- To mailbox -->
	<waypoint x="4965" z="-1948" y="109" tag="ToMailbox">	</waypoint>
	<waypoint x="5025" z="-2013" y="109">	</waypoint>
	<waypoint x="5088" z="-2066" y="103">	</waypoint>
	<waypoint x="5149" z="-2117" y="103">	</waypoint>
	<waypoint x="5215" z="-2170" y="103" tag="Mailbox">
		player:target_Object(110538,1500);
		RoMScript("ChoiceOption(1);"); yrest(1000);
		UMM_SendByNameOrId(MailReceiver, Guild_Items);		-- Send Guild_Items
		RoMScript("CloseWindows()")

		-- check for error
		if( inventory:getItemCount(206590) > 0 or inventory:getItemCount(206591) > 0 or inventory:getItemCount(206592) > 0
			or inventory:getItemCount(207326) > 0 or inventory:getItemCount(207330) > 0 or inventory:getItemCount(207930) > 0 ) then
			player.free_counter1 = 0
			repeat
				player.free_counter1 = player.free_counter1 + 1
				printf("\a\a\a\Error or final!\n");
				yrest(10000)
			until player.free_counter1 == 20
		end;

		-- Do we have enough Phirius coins
		if( 30 > inventory:itemTotalCount(203038) and getZoneId() ~= 13 ) or ( ShellItemFounded > 0 and 450 > inventory:getItemCount(203038) ) then
			cprintf(cli.yellow, "Not enough phirius token coins or Item founded.\n")
			print("Use teleport skill.")
			sendMacro("CastSpellByName(TEXT('Sys540193_name'));");	-- Use teleport skill to "Heffner Camp"
			waitForLoadingScreen(30) yrest(2000)
			player:update()
			__WPL:setWaypointIndex(__WPL:findWaypointTag("ToHugope"));
		end;
	</waypoint>
	<waypoint x="5177" z="-2134" y="103">	</waypoint>
	<waypoint x="5083" z="-2055" y="103">	</waypoint>
	<waypoint x="4978" z="-1996" y="113">
		-- Completed
		WhenFinished()
	</waypoint>

<!-- To Hugope -->
	<!-- #  1 --><waypoint x="-6359" z="-3883" y="164" tag="ToHugope">	</waypoint>
	<!-- #  2 --><waypoint x="-6418" z="-3854" y="166">	</waypoint>
	<!-- #  3 --><waypoint x="-6511" z="-3824" y="166">	</waypoint>
	<!-- #  4 --><waypoint x="-6620" z="-3804" y="168">	</waypoint>
	<!-- #  5 --><waypoint x="-6713" z="-3813" y="169">	</waypoint>
	<!-- #  6 --><waypoint x="-6801" z="-3865" y="168">	</waypoint>
	<!-- #  7 --><waypoint x="-6873" z="-3947" y="169">	</waypoint>
	<!-- #  8 --><waypoint x="-6922" z="-4000" y="169">	</waypoint>
	<!-- #  9 --><waypoint x="-6994" z="-4057" y="171">	</waypoint>
	<!-- # 10 --><waypoint x="-7041" z="-4094" y="170">	</waypoint>
	<!-- # 11 --><waypoint x="-7095" z="-4126" y="172">
		-- Completed
		WhenFinished()
	</waypoint>
</waypoints>