<?xml version="1.0" encoding="utf-8"?>
<waypoints>
	<!--
		v0.2 Cattle.xml
		
		Do the Cowboy - Fairyfestival event
		
		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 
			_cowboy = RoMScript("TEXT(\"Sys425687_name\")")
			
			-- Count number of times
			completed = 0
			
			-- 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
		]]>
	</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>
	
	<!-- Hunt Cows -->
	<!-- #  2 --><waypoint x="2408" z="1760" y="30" tag="hunt">
		<![CDATA[
			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 not player:PlayerInRange(75, cattle._X, cattle._Z)
					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)	or -- We lost cow
								player:PlayerInRange(75, cattle._X, cattle._Z) then					   -- Some1 else uses cow
								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
		]]>
	</waypoint>
	
	<!-- #  3 --><waypoint x="2222" z="1916" y="30">
		<![CDATA[	
			quest = questlog:getQuest(_cowboy)
			if (not quest) or quest:isComplete() then
				__WPL:setWaypointIndex(__WPL:findWaypointTag("npc"));
			else			
				__WPL:setWaypointIndex(__WPL:findWaypointTag("hunt"));
			end
		]]>
	</waypoint>
</waypoints>