<?xml version="1.0" encoding="utf-8"?>
<waypoints>
	<!--
		ElementEvent for Colorweave Festival v1.0
		By Dx
		
		Uses CountMobs by Rock5
		Loosly based on help from Lisa
		
		NPC: Ayna Rett ID: 120888
		
		Red bubble: 121055
		Blue bubble: 121057
		Yellow bubble: 121056
		
		Blue Element ID: 103015
		Yellow Element ID: 103014
		Red Element ID: 103013
		Purple Element ID: 106548
		
		Stone marker ID: 121045
		
		'Times Having Helped Out' ID: 623045 
		'Magic Thread' ID: 623044 
	-->
	<onLoad>
		<![CDATA[
			benice   = true		-- Be nice to other players
			maxdist  = 150    	-- Max dist to take an element on
			slowdown = 750		-- Pause after taking an element
			
			-- To go 'rape mode' set maxdist to 200 and slowdown to 0, 
			-- perhaps reduce yrest(200) in loop
			
			brel = {}
			brel[121055]={Id=103013,color="Red"}
			brel[121057]={Id=103015,color="Blue"}
			brel[121056]={Id=103014,color="Yellow"}
			
			-- Wait until NPC can be talked to "Let me help you out!"
			_npc = GetIdName(120888)
			cprintf(cli.lightblue, "Waiting for event registration with %s!\n", _npc)
			repeat 
				-- Target NPC
				repeat				
					if not player:target_NPC(_npc) then
						error("Can't find NPC ".._npc)
					end
					yrest(1000)
				until RoMScript("SpeakFrame:IsVisible()")
				yrest(500)

				-- Start event
				ChoiceOptionByName("Let me help you out!")
				yrest(500)
				if not player:hasBuff(623044) then
					yrest(60000)
				end
			until player:hasBuff(623044)
			cprintf(cli.lightblue, "Waiting for event to start!\n")
			repeat yrest(1000) until player:hasBuff(623041)
			cprintf(cli.lightblue, "Event started!\n")
			
			-- Setup event
			color = nil
			helped = 0
			hname = GetIdName(623045)
			npc = player:findNearestNameOrId({120888})
			
			-- Do event
			repeat
				bobj = player:findNearestNameOrId({121055, 121056, 121057})
				if bobj then
					if color ~= brel[bobj.Id].color then
						color = brel[bobj.Id].color
						cprintf(cli.lightblue, "Bubble changed to %s\n", color)
					end
					
					-- Wait until element is a little closer, looks bad killing at spawn
					obj=player:findNearestNameOrId({103013, 103014, 103015, 106548})					
					if obj and distance(npc.X, npc.Z, npc.Y, obj.X, obj.Z, obj.Y) < maxdist then
						player:target(obj)
						if obj.Id == brel[bobj.Id].Id then
							RoMScript("UseExtraAction(1)")
						else
							RoMScript("UseExtraAction(2)")
						end
						
						-- Keep track of number of times helped
						if player.Buffs[hname] then
							helped = player.Buffs[hname].Level
						end
						
						-- Do we have purple, relax for other players to complete
						if benice and helped > 29 then
							slowdown = 1500
							maxdist = 50
						end

						-- If other players in circle, slow down
						if CountPlayers(200) > 0 then
							yrest(slowdown) 
						end
					end
				end
				
				yrest(200)
				player:updateBuffs()
			until not player:hasBuff(623041)

			-- And were done
			cprintf(cli.lightblue, "You got %d points\n", helped)
   			error("Finished")
		]]>
	</onLoad>
</waypoints>
