<?xml version="1.0" encoding="utf-8"?>
<waypoints>
	<!--
		Do the Cowboy - Fairyfestival event	 v.02

		$Revision: 92 $
		$Date: 2012-08-31 18:00:05 +0200 (fr, 31 aug 2012) $

		Prereq:
			- addon_precence
			- userfunction_QuestByName
			- userfunction_questlog
	-->
	<onLoad>
		<![CDATA[
			-- Use guild transport runes to repeat
			dorepeat = true

			-- Involved id's
			_cattle = 121267
			_bell   = 241639
			_jack   = 121266
			_gstone = 203784

			-- Where to go with cattle
			_wp = {{X = 2222, Z = 1916}, {X = 2138, Z = 1866}}

			-- Get NLS questname (replace with a RoMScript call)
			_cowboy = RoMScript("TEXT(\"Sys425687_name\")")

			-- Count number of times
			completed = 0

			-- Options
			changeProfileOption("QUICK_TURN", true)

			--
			-- Ye, this is basically catchcavy
			--
			function CalcNewPos(_wp, _cow)
				local HerdDist = 15 -- distance to herd cow
				local Dist = distance(_wp.X, _wp.Z, _cow.X, _cow.Z)
				local Ratio = (Dist + HerdDist) / Dist
				local x = (_cow.X - _wp.X) * Ratio + _wp.X
				local z = (_cow.Z - _wp.Z) * Ratio + _wp.Z
				local NewPos = CWaypoint(x, z)
				NewPos.Type = 4 -- Travel
				return NewPos
			end

			-- Cow is in use if a player is within a distance of it
			function isCowFree(cow)
				return not player:PlayerInRange(75, cow._X, cow._Z)
			end

			-- Go get a cow and bring it back
			function GetCow()
				quest = questlog:getQuest(_cowboy)
				if not quest then
					__WPL:setWaypointIndex(__WPL:findWaypointTag("npc"));
				else
					ncows = quest:getKillCount(1)

					while ncows == quest:getKillCount(1) do
						NewPos = nil
						repeat
							cattle = player:findNearestNameOrId(_cattle)
							yrest(200)
						until isCowFree(cattle)
						player:target(cattle.Address)

						if cattle then
							cprintf(cli.yellow,"\a\aAttempting to catch a cow...\n")
							player:update()

							wpi = 1
							while true do
								cattle:update()
								quest:update()

								if (NewPos and distance(cattle.X, cattle.Z, player.X, player.Z ) > 50) then
									cprintf(cli.yellow, "Dropped the cow, finding a new one\n")
									break
								end

								-- Completed a cow or quest or reached a wp
								if quest:getKillCount(1) > ncows or distance(cattle.X, cattle.Z, _wp[wpi].X, _wp[wpi].Z ) < 20 then
									wpi = wpi + 1
									if wpi > #_wp then
										cprintf(cli.yellow, "Cow delivered\n")
										break
									end
								end

								NewPos = CalcNewPos(_wp[wpi], cattle)
								success, failreason = player:moveTo(NewPos, true)
								inventory:useItem(_bell)
								yrest(200)
								player:update()
							end
						end
					end
				end
			end

			--
			-- Some extended or modified functions, originally not
			-- written by me
			--
			function CPlayer:nMobsInRange(_id,_range,_X,_Z)
				_range = _range or 300
				_X = _X or self.X
				_Z = _Z or self.Z
				local dist = 0
				local found = 0

				local objectList = CObjectList()
				objectList:update()
				for i = 0,objectList:size() do
					obj = objectList:getObject(i)
					if( obj.Id == _id ) then
						dist = isInDist(_X, _Z, obj.X, obj.Z, _range)
						if( dist > -1 ) then
							found = found + 1
						end
					end
				end
				return found
			end

			function CWaypointList:findWaypointRegexp(tag)
				res = {}
				tag = string.lower(tag);
				for i,v in pairs(self.Waypoints) do
					if( v.Tag and string.match(v.Tag, tag) ) then
						table.insert(res, i)
					end
				end
				return res;
			end
		]]>
	</onLoad>

	<!-- Jack Jones -->
	<!-- #  1 --><waypoint x="2279" z="1858" y="59" tag="npc">
		<![CDATA[
			quest = questlog:getQuest(_cowboy)
			if quest and quest:isComplete() then
				player:target_NPC(_jack); yrest(1000)
				CompleteQuestByName(_cowboy)
				completed = completed + 1
			end

			if completed > 0 and not dorepeat then
				error("Completed")
			else
				quest = questlog:getQuest(_cowboy)
				while not quest do
					player:target_NPC(_jack); yrest(1000)
					AcceptQuestByName(_cowboy)
					quest = questlog:getQuest(_cowboy)
					if not quest then
						if dorepeat and inventory:itemTotalCount(_gstone) > 0 then
							player:target_NPC(_jack); yrest(1000)
							ChoiceOptionByName("I would like to help you once again."); yrest(1000)
							ChoiceOptionByName("Confirm"); yrest(1000)
						else
							error("Quest finished")
							break
						end
					end
				end
			end
		]]>
	</waypoint>

	<!-- Cow hunting grounds -->
	<!-- #  2 --><waypoint x="2408" z="1760" y="30" tag="hunt1">
		<![CDATA[
			GetCow()
			__WPL:setWaypointIndex(__WPL:findWaypointTag("return"));
		]]>
	</waypoint>

	<!-- #  3 --><waypoint x="2444" z="1941" y="31" tag="hunt2">
		<![CDATA[
			GetCow()
			__WPL:setWaypointIndex(__WPL:findWaypointTag("return"));
		]]>
	</waypoint>

	<!-- #  4 --><waypoint x="2254" z="1978" y="64" tag="hunt3">
		<![CDATA[
			GetCow()
			__WPL:setWaypointIndex(__WPL:findWaypointTag("return"));
		]]>
	</waypoint>

	<!-- Return from delivering a cow -->
	<!-- #  5 --><waypoint x="2222" z="1916" y="30" tag="return">
		<![CDATA[
			quest = questlog:getQuest(_cowboy)
			if (not quest) or quest:isComplete() then
				__WPL:setWaypointIndex(__WPL:findWaypointTag("npc"));
			else
				-- Count number of cows at the different hunting grounds
				-- Goto the area with most cows
				local max, n = 0, 0
				local hunt = __WPL:findWaypointRegexp("hunt[1-9]")
				for k,i in pairs(hunt) do
					local ncows = player:nMobsInRange(_cattle, 200, __WPL.Waypoints[i].X, __WPL.Waypoints[i].Z)
					if ncows > max then
						n = i
						max = ncows
					end
				end
				if max > 0 and n > 0 then
					__WPL:setWaypointIndex(n);
				else
					error("No cows found!")
				end
			end
		]]>
	</waypoint>
</waypoints>
