# Variables
spot = (31899, 32060, 11)
npc = (31896, 32068, 8)
shop = (31958, 31998, 7)

# Paths
to_minotaurs = 0
to_npc = 1
hunting = 2
to_shop = 3
to_hunt = 4

# Keep this as it is. (Must be 0)
ignore_menu = 0
last_time_shop = 0

def onTick():
	global last_time_shop

	if not script.IsInShop():
		last_time_shop = 0
		return

	if last_time_shop > 0:
		cur_time = script.GetCurrentTime()
		if (cur_time - last_time_shop) > 60:
			last_time_shop = 0
			script.StatusMessage('Was stuck')
			script.LeaveShopEx()
			
def onScriptActivation():
	global last_time_shop
	last_time_shop = 0
	script.EnableTickEvent(10000)

def onReceiveEnteredMenu(shopPacket, menuId, title):
	global ignore_menu, last_time_shop
	
	if title == 'Scrapyard':
		ignore_menu = 0
		last_time_shop = script.GetCurrentTime()
		script.StatusMessage('Shop: ' + title)
		if shopPacket == 1:
			script.ChooseShopOption(menuId, 1) # Sell
	elif title == 'Sell':
		ignore_menu = 0
		last_time_shop = script.GetCurrentTime()
		if script.GetWay() != to_hunt:
			script.SetWay(to_hunt, 2)
		script.StatusMessage('Gonna sell loot.')
	else:
		ignore_menu = 1

def onReceiveMenuText(menuId, title, text):
	if 'No entry in the list' in text:
		script.StatusMessage('Done.')
		script.LeaveShop() # didn't find items to sell.

def onReceiveMenuRows(rows):
	global last_time_shop

	if ignore_menu == 1:
		return

	found = 0
	for row in rows:
		list = row.split()
		menuId = int(list[0])
		rowId = int(list[1])
		title = list[2]
		looktype = int(list[-1])

		if script.IsInLoot(looktype):
			script.ChooseMenuOption(menuId, 1, rowId) # Sell item
			last_time_shop = script.GetCurrentTime()
			found = 1
			break # break to avoid flood
	if found == 0:
		script.StatusMessage('Done.')
		script.LeaveShop() # didn't find items to sell.

def onChangeLocation(x, y, z):
	if x == spot[0] and y == spot[1] and z == spot[2]:
		if script.IsStorageFull() == 1:
			if script.GetWay() != to_shop:
				script.StatusMessage('Going to Scrapyard to sell stuff.')
				script.SetWay(to_shop, 2)
		else:
			if script.GetVar('quest') == 1:
				if script.GetWay() != to_npc:
					script.StatusMessage('Going to Rugnarak to retrieve reward!')
					script.SetWay(to_npc, 2)
			elif script.GetWay() != hunting:
				script.StatusMessage('Hunt started!')
				script.SetWay(hunting, 2)
	elif x == npc[0] and y == npc[1] and z == npc[2]:
		if script.GetVar('quest') == 0:
			if script.GetWay() != to_minotaurs:
				script.SetWay(to_minotaurs, 2)
		elif script.GoToNpcEx('Rugnarak'):
			script.StatusMessage('Going to Rugnarak.')
		else:
			script.StatusMessage('Can\'t find Rugnarak.')
			script.Alarm('Can\'t find Rugnarak.')
	elif x == shop[0] and y == shop[1] and z == shop[2]:
		if script.GetWay() == to_shop:
			if script.EnterShop():
				script.StatusMessage('Going to shop.')
			else:
				script.Alarm('Can\'t find shop.')

def onReachedNpc(uid, name):
	if not script.TalkToNpc(uid):
		script.StatusMessage('Can\'t talk to ' + name + '!')

def onTalkedWithNpc(uid, result):
	if not result:
		if not script.TalkToNpc(uid):
			script.StatusMessage('Can\'t talk to ' + name + '!')

def onReceiveNpcText(name, text, page):
	if name == 'Rugnarak':
		if 'Have you killed' in text:
			script.StatusMessage('Saying Yes.')
			script.ChooseNpcOption(1) # Yes
		elif 'Erm. I still' in text:
			script.SetVar('quest', 0)
			script.SetWay(to_minotaurs, 2)
			script.ForgetNpc()
		elif 'Anyway, here is your reward.' in text:
			script.SetWay(to_minotaurs, 2)
			script.ForgetNpc()

def onReceiveDirectionMarker(x, y, z, text):
	if text == 'Aurea: Rugnarak':
		script.SetVar('quest', 1)
	elif text == 'Aurea: Minotaurs':
		script.SetVar('quest', 0)
		
def onReceiveAddItemToBackpack(slot, itemId):
	# Warning! Don't use it when retrieving items from depot and etc!
	if not script.IsInLoot(itemId): # Drop items not in autoloot list.
		script.DropItem(slot, 1)

def onKilledEnemy(name, templateId):
	varname = 'killed_' + name
	mobs = script.GetVar(varname) + 1
	script.SetVar(varname, mobs)
	txt = ''
	if script.GetVar('quest') == 1:
		txt = str(mobs) + '\n' + 'Will go to Rugnarak...'
	else:
		txt = str(mobs)
	script.StatusMessage('Killed: ' + name + ' \nTotal: ~' + txt)
