# 0 = walk to town, 1 = use portal
use_portal = 1
# Display kill count.
# 0 = disabled, 1 = enabled.
display_kill_count = 1
# 0 = don't drop anything, 1 = drop picked up/added to backpack items not in autoloot list.
# Warning! If enabled, bot will drop stuff when retrieving from depot, buying from shop.
# Just use it when hunting.
drop_items = 0

# Variables
spot = (32719, 32200, 8)
shop = (32716, 32176, 7)

# Paths
assassin = 0
to_shop_portal = 1
back_portal = 2
to_shop = 3
back = 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 getToShopPath():
	if use_portal:
		return to_shop_portal
	return to_shop
	
def getToHuntPath():
	if use_portal:
		return back_portal
	return back

def onReceiveEnteredMenu(shopPacket, menuId, title):
	global ignore_menu, last_time_shop
	
	if title == 'Provisionary Things':
		ignore_menu = 0
		last_time_shop = script.GetCurrentTime()
		script.StatusMessage('Menu: ' + title)
		if shopPacket == 1:
			script.ChooseShopOption(menuId, 2) # Sell
	elif title == 'Sell':
		ignore_menu = 0
		last_time_shop = script.GetCurrentTime()
		if script.GetWay() != getToHuntPath():
			script.SetWay(getToHuntPath(), 2)
		script.StatusMessage('Selling items...')
	else:
		ignore_menu = 1

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() != getToShopPath():
				script.StatusMessage('Going to town to sell items...')
				script.SetWay(getToShopPath(), 2)
		else:
			if script.GetWay() != assassin:
				script.StatusMessage('Hunt started!')
				script.SetWay(assassin, 2)
	elif x == shop[0] and y == shop[1] and z == shop[2]:
		if script.GetWay() == getToShopPath():
			if script.EnterShop():
				script.StatusMessage('Going inside shop.')
			else:
				script.Alarm('Can\'t find the shop.')

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 onReceiveAddItemToBackpack(slot, itemId):
	if drop_items == 1:
		if not script.IsInLoot(itemId):
			script.DropItem(slot, 1)

def onKilledEnemy(name, templateId):
	if display_kill_count == 0:
		return
	varname = 'killed_' + name
	mobs = script.GetVar(varname) + 1
	script.SetVar(varname, mobs)
	txt = str(mobs)
	script.StatusMessage('Killed: ' + name + ' \nTotal: ~' + txt)
