# Cleansing fluid  [v1.0.3]
###############################################

# Waypoints:
Set_Hunt = 0
Set_To_Fountain = 1
Set_To_Hunt = 2

# Spots:
Spot_Hunt = (x, y, z)
Spot_Fountain = (x, y, z)

# Bottle states:
full = 2587
empty = 5230
fluid = [6493, 6494, 2587]
fill = [5230, 6493, 6494]


###############################################


def onScriptActivation():
	script.DisableStorageInfo(True)
	script.EnableTickEvent(5000)


def onEraseMonsterHealth():
	backpack = list(script.GetBackpackItems())

	for itemId in backpack:
		if itemId in fluid:
			script.UseItemId(itemId)
			break


def onReceiveUserData(icon, looktypeId):
	way = script.GetWay()
	inventory = list(script.GetInventoryItems())
	slot = script.GetItemSlot(empty)

	if way in [Set_Hunt, Set_To_Hunt]:
		for itemId in inventory:
			if (itemId in fluid) and (slot > -1):
				script.RetrieveItemTo(itemId, slot)
				break


def onChangeLocation(x, y, z):
	way = script.GetWay()
	backpack = list(script.GetBackpackItems())
	inventory = list(script.GetInventoryItems())

	if way in [Set_Hunt, Set_To_Hunt]:
		if script.IsOnScreen(Spot_Hunt[0], Spot_Hunt[1], Spot_Hunt[2], 2):
			if (full in backpack) or (full in inventory):
				script.SetWay(Set_Hunt, 2)
			else:
				script.SetWay(Set_To_Fountain, 2)


def onTick():
	way = script.GetWay()
	xyz = (script.GetX(), script.GetY(), script.GetZ())
	backpack = list(script.GetBackpackItems())
	inventory = list(script.GetInventoryItems())
	slot = script.GetItemSlot(full)

	if (way == Set_To_Fountain) and (xyz == Spot_Fountain):
		for itemId in backpack:
			if itemId in fill:
				script.UseItemId(itemId)
				break
		else:
			for itemId in inventory:
				if itemId in fill:
					script.RetrieveItemTo(itemId, slot)
					break
			else:
				script.SetWay(Set_To_Hunt, 2)