# Ignore monsters when on route to shop.
IgnoreMonstersTo = True
# Ignore monsters when on route back to cave.
IgnoreMonstersBack = True
# Ignore monsters when on route to the switch spot.
IgnoreMonstersSwitch = True
# Ignore monsters when at hunting cave (Waypoints set: hunt).
# Useful when you just want to loot items and sell.
IgnoreMonstersHunt = False

# Try moving directly to the spot when storages are full. (If map is open in that area)
MoveToSpotDirectly = True

# Drop unwatned items.
DropUnwantedItems = False

# Names of items which must be dropped:
DropUnwantedItemsList = (
	'Hell crystal',
	'Chili stick',
	'Your mom',
	'Bolkar\'s dildo'
)

# Sell types: 1 - sell items one by one, 2 - sell all at once, 3 - sell one by one from backpack and all at once from depotmail, inventory.
SellType = 1

# Variables
spot = Location(31881, 32078, 12) # Spot where path changes to hunt or shop. 'Shop' path starts from it and 'Back' ends in it.
shop = Location(31958, 31998, 7) # Spot near shop, player enters shop from it
ShopName = 'Scrapyard' # Change to your shop name which you will be using
# For Scrapyard set to 1. For other shops set to 2.
SellOptionId = 1

# Paths
# Make hunting path in set 0.
# Make to shop path in set 1.
# Make back to hunt place path in set 2.
hunt = 0
to_shop = 1
back = 2

# Don't touch these or I will cut your fingers!
ignore_menu = False
last_time_shop = 0

queue = {}

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
	CheckIgnoring(script.GetWay())
	last_time_shop = 0
	script.EnableTickEvent(10000)
	script.PZChecksForDrop(False) # allow dropping items in protection zone
	script.PauseStorage(False)
	
def onScriptDeactivation():
	script.PauseStorage(False)
	if script.IsInShop():
		script.LeaveShop()

def onReceiveEnteredMenu(shopPacket, menuId, title):
	global ignore_menu, last_time_shop
	
	if title == ShopName:
		ignore_menu = False
		script.SetVar('checkedBackpack', False)
		script.SetVar('checkedInv', False)
		script.SetVar('checkedDep', False)

		last_time_shop = script.GetCurrentTime()
		script.StatusMessage('Shop: ' + title)
		if shopPacket:
			script.ChooseShopOption(menuId, SellOptionId) # Sell
	elif title == 'Sell':
		ignore_menu = False
		script.SetVar('lastMenuId', menuId)
		last_time_shop = script.GetCurrentTime()
		if script.GetWay() != back:
			ChangeWay(back)
		script.StatusMessage('Selling items...')
	elif title == 'Review Sell':
		ignore_menu = True
		last_time_shop = script.GetCurrentTime()
		script.ChooseShopOption(menuId, 1)
	else:
		ignore_menu = True

def onReceiveMenuRowsEx(rows):
	global last_time_shop

	if ignore_menu:
		return

	if SellType == 2:
		last_time_shop = script.GetCurrentTime()

		menuId = script.GetVar('lastMenuId')

		if not script.GetVar('checkedBackpack'):
			script.ChooseMenuOption(menuId, 1, 0)
			script.SetVar('checkedBackpack', True)
			return
		if not script.GetVar('checkedInv'):
			script.ChooseMenuOption(menuId, 1, 25600)
			script.SetVar('checkedInv', True)
			return
		if not script.GetVar('checkedDep'):
			script.ChooseMenuOption(menuId, 1, 25856)
			script.SetVar('checkedDep', True)
			return

		script.StatusMessage('Done.')
		script.LeaveShop()
	else:
		found = False

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

			if SellType == 3 and isAct == 1 and ('Inventory' in title or 'Depotmail' in title):
				script.ChooseMenuOption(menuId, 1, rowId) # Sell item
				last_time_shop = script.GetCurrentTime()
				found = True
				break
			else:
				if script.IsInLoot(looktype):
					script.ChooseMenuOption(menuId, 1, rowId) # Sell item
					last_time_shop = script.GetCurrentTime()
					found = True
					break # break to avoid flood
		if not found:
			script.StatusMessage('Done.')
			script.LeaveShop() # didn't find items to sell.

def onReceiveMenuText(menuId, title, text):
	if title != 'Review Sell' and 'No entry in the list' in text:
		script.StatusMessage('Done.')
		script.LeaveShop()

def initResume():
	if len(queue) == 0:
		script.RunEvent('resumeStorage', 2000)
		
def resumeStorage(): # executed by RunEvent
	script.PauseStorage(False)

def onReceiveAddItemToBackpack(slot, itemId):
	if not DropUnwantedItems:
		return

	script.PauseStorage(True)

def onDroppedItem(slot):
	initResume()
	
def onReceiveItemDescriptionEx(itemId, name, slot):
	if DropUnwantedItems and name in DropUnwantedItemsList:
		if script.DropItem(slot, 1) == 'no_room':
			script.StatusMessage('No room! Adding to queue.')
			queue[slot] = itemId # add item to queue and drop it later
		else:
			script.StatusMessage('Dropping: ' + name)
			script.IgnoreNextItem(itemId)
	else: # item not in drop list, trying to resume storage
		if MoveToSpotDirectly and script.IsStorageFull():
			if script.GoToLocationEx(spot.x, spot.y, spot.z):
				script.IgnoreMonsters(IgnoreMonstersSwitch)
				script.StatusMessage('Storages are full.\nGoing to the spot.')
			else:
				script.StatusMessage('Storages are full.\nWarning! Will move by the waypoints at this time.')
		initResume()

def onChangeLocation(x, y, z):
	checkDropQueue(x, y, z)
	initResume()

	xyz = Location(x, y, z)
	if xyz == spot:
		if script.IsStorageFull():
			if script.GetWay() != to_shop:
				script.StatusMessage('Going to shop to sell items...')
				ChangeWay(to_shop)
		else:
			if script.GetWay() != hunt:
				script.StatusMessage('Hunt started!')
				ChangeWay(hunt)
	elif xyz == shop:
		if script.GetWay() == to_shop:
			if script.EnterShop():
				script.StatusMessage('Entering shop.')
			else:
				script.Alarm('Can\'t find the shop.')

def checkDropQueue(x, y, z):
	if not DropUnwantedItems:
		return

	if script.IsLocationFree(x, y, z):
		if len(queue) > 0: # there are items in queue, try to drop them
			for slot, itemId in queue.items():
				if script.GetItemInSlot(slot) == itemId:
					if script.DropItem(slot, 0) == 'ok':
						script.IgnoreNextItem(itemId)
						script.StatusMessage('Dropping item from queue.')
						queue.pop(slot) # Deletes item from queue
						break
				else:
					script.StatusMessage('Deleted item from queue.')
					queue.pop(slot) # Deletes item from queue, because it's not in slot.

def CheckIgnoring(way):
	if way == to_shop:
		script.IgnoreMonsters(IgnoreMonstersTo)
	elif way == back:
		script.IgnoreMonsters(IgnoreMonstersBack)
	else:
		script.IgnoreMonsters(IgnoreMonstersHunt)

def ChangeWay(way):
	script.SetWay(way, 2)
	CheckIgnoring(way)