<?xml version="1.0" encoding="utf-8"?><waypoints>

<!--        Rock5's Millers Ranch Chickens run        -->
<!--           By Rock5     Version 3.0  beta 2       -->
<!-- www.solarstrike.net/phpBB3/viewtopic.php?p=12139 -->

<onLoad>
	--== User Option ==--

	GetReward = true -- Doesn't complete the quest if 'false'.
	Timeout = 300    -- How long hens and eggs stay in the ignore list before being given another chance.

	--=================--

	ignoredHens = {}
	ignoredEggs = {}

	changeProfileOption("HARVEST_DISTANCE", 150)

	-- Get name strings
	questname=RoMScript("TEXT('Sys422386_name')"); yrest(500) -- ie. "An Easy Lay?"
	npcname=RoMScript("TEXT('Sys114930_name')"); yrest(500) -- ie. "Jenna Miller"
	merchant=RoMScript("TEXT('Sys114935_name')"); yrest(500) -- ie. "Pykesile"

	-- Don't need assist. Disable it.
	if settings.profile.options.EGGPET_ENABLE_ASSIST == true then
		settings.profile.options.EGGPET_ENABLE_ASSIST = false
		local assistPet = CEggPet(settings.profile.options.EGGPET_ASSIST_SLOT)
		assistPet:Return()
	end

	-- Don't need buffs, disable those skills
	for i, skill in pairs(settings.profile.skills) do
		if skill.Type == STYPE_BUFF then
			settings.profile.skills[i].AutoUse = false
		end
	end

	zoneid = RoMScript("GetZoneID()")
	if zoneid ~= 304 then -- not at Miller's Ranch
		-- Try to find Sorrun and teleport in
		if player:target_NPC("Sorrun") then
			sendMacro("ChoiceOption(3)") yrest(1000)
			sendMacro("ChoiceOption(1)")
			waitForLoadingScreen()
			yrest(1000)
			__WPL:setWaypointIndex(__WPL:getNearestWaypoint(player.X,player.Z))
		else
			error("You are too far from Miller's Ranch")
		end
	end

	lastHen = 0
	lastEgg = 0
	Timeout = 300 -- How long hens and eggs stay in the ignore list before being given another chance.

	function feedHenEvalFunc(address)
		-- Don't target hens in the ignore list
		for k,v in pairs(ignoredHens) do
			if v.address == address then
				if  (os.time() - v.time) > Timeout then
					table.remove(ignoredHens,k)
				else
					return false
				end
			end
		end

		local hen = CObject(address)

		-- Don't target hens in coop
		if hen.Z > 3240 then return false end -- Hen in coop

		-- Don't target moving hens
		local oldX, oldZ = hen.X, hen.Z
		yrest(100)
		hen:update()
		if distance(hen.X,hen.Z,oldX,oldZ) > 2 then return false end

		lastHen = address

		return true
	end

	function coopHenEvalFunc(address)
		-- Don't target hens in the ignore list
		for k,v in pairs(ignoredHens) do
			if v.address == address then
				if  (os.time() - v.time) > Timeout then
					table.remove(ignoredHens,k)
				else
					return false
				end
			end
		end

		-- Only target coop Hens
		if 3240 > CObject(address).Z then return false end

		return true
	end

	function eggEvalFunc(address)
		-- Don't target eggs in the ignore list
		for k,v in pairs(ignoredEggs) do
			if v == address then
				if  (os.time() - v.time) > Timeout then
					table.remove(ignoredEggs,k)
				else
					return false
				end
			end
		end

		lastEgg = address

		return true
	end

</onLoad>
	<!-- #  1 --><waypoint x="4033" z="3450">	</waypoint>
	<!-- #  2 --><waypoint x="4026" z="3400">
		if settings.profile.options.EGGPET_ENABLE_CRAFT == true then -- if crafting is enabled
			player:merchant(merchant);
		end
		__WPL:setDirection(WPT_FORWARD)
	</waypoint>
	<!-- #  3 --><waypoint x="4029" z="3327">	</waypoint>
	<!-- #  4 --><waypoint x="4016" z="3172" tag="merchant">	</waypoint>
	<!-- #  5 --><waypoint x="3948" z="3107" tag="Main">

		if settings.profile.options.EGGPET_ENABLE_CRAFT == true then
			local craftpet = CEggPet(settings.profile.options.EGGPET_CRAFT_SLOT)
			local toolChoice = craftpet:getToolChoices()
			if inventory:itemTotalCount(toolChoice[1].Id) == 0 then
				-- go buy some.
				__WPL:setDirection(WPT_BACKWARD)
				__WPL:setWaypointIndex(__WPL:findWaypointTag("merchant"));
				return
			end
		end

		queststate = getQuestStatus(questname)
		if queststate == "complete" and GetReward == true then
			-- Complete quest
			player:target_NPC(npcname);
			sendMacro("CompleteQuest()"); yrest(2000);
			-- Accept quest
			player:target_NPC(npcname);
			sendMacro("AcceptQuest()"); yrest(2000);
			__WPL:setWaypointIndex(__WPL:findWaypointTag("Main"))

		elseif queststate == "not accepted" then
			-- Accept quest
			player:target_NPC(npcname);
			sendMacro("AcceptQuest()"); yrest(2000);
			__WPL:setWaypointIndex(__WPL:findWaypointTag("Main"))

		else
			local feedNumber = inventory:itemTotalCount(204789)
			local rememberLast = lastHen
			local strikes = 0
			while feedNumber > 0 and -- While you have feed
				player:target_Object(112955,nil,nil,true,feedHenEvalFunc) do -- Feed Hens
				if( inventory:itemTotalCount(204789) == feedNumber ) and rememberLast == lastHen then -- Feed didn't work
					strikes = strikes + 1
					if strikes >= 3 then -- Add to ignore list
						table.insert(ignoredHens,{address=lastHen, time=os.time()})
						strikes = 0
					end
				else
					strikes = 0
				end

				rememberLast = lastHen
				feedNumber = inventory:itemTotalCount(204789)
			end
			-- Out of feed or hens
			if 1 > inventory:itemTotalCount(204789) then -- Get more feed
				__WPL:setWaypointIndex(__WPL:findWaypointTag("Get Feed"));
			else
				__WPL:setWaypointIndex(__WPL:findWaypointTag("Get Eggs"))
			end
		end

		if #ignoredHens > 0 then
			printf("Ignored hens:\n")
			for k,v in pairs(ignoredHens) do
				if type(v) == "number" then
					printf("%x\t",v)
				else
					printf(v) --print("")
				end
			end
			printf("\n")
		end

		if #ignoredEggs > 0 then
			printf("Ignored eggs:\n")
			for k,v in pairs(ignoredEggs) do
				if type(v) == "number" then
					printf("%x\t",v)
				else
					printf(v) --print("")
				end
			end
			printf("\n")
		end

		if #ignoredHens == 3 then
			printf("All hens are stuck.\n")
			player:sleep()
		end
	</waypoint>
	<!-- #  6 --><waypoint x="3940" z="3055" tag="Get Feed">	</waypoint>
	<!-- #  7 --><waypoint x="3937" z="2978"> player:target_Object(112956,nil,true)	</waypoint>
	<!-- #  8 --><waypoint x="3935" z="3051">__WPL:setWaypointIndex(__WPL:findWaypointTag("Main"));	</waypoint>
	<!-- #  9 --><waypoint x="3928" z="3185" tag="Get Eggs">	</waypoint>
	<!-- # 10 --><waypoint x="3929" z="3261">
		while player:target_Object(112955,nil,nil,true,coopHenEvalFunc) do -- Check for hens
			yrest(500)
			local rememberLast = lastEgg
			local strikes = 0
			while player:target_Object({112958,114931},nil,nil,false,eggEvalFunc) do -- Get Fresh/Golden Eggs
				if rememberLast == lastEgg then -- Failed to collect
					strikes = strikes + 1
					if strikes >= 5 then
						table.insert(ignoredEggs,{address=lastEgg, time=os.time()})
						strikes = 0
					end
				else
					strikes = 0
				end
				rememberLast = lastEgg
			end
		end
	</waypoint>
	<!-- # 11 --><waypoint x="3926" z="3198">__WPL:setWaypointIndex(__WPL:findWaypointTag("Main"));	</waypoint>
</waypoints>
