Codice:
import chat, player, item, math, net, time
SLOTS = player.GetExtendInvenMax() if hasattr(player, 'GetExtendInvenMax') and player.GetExtendInvenMax() > 0 else player.INVENTORY_PAGE_SIZE * player.INVENTORY_PAGE_COUNT
SLOTS_PER_PAGE = player.INVENTORY_PAGE_SIZE
class Item:
def __init__(self, id, slot, size, name):
self.id = id
self.slot = slot
self.size = size
self.name = name
def move(self, slot):
net.SendItemMovePacket(self.slot, slot, 0)
self.slot = slot
class ItemHandler:
def __init__(self):
self.list = []
self.placed = []
def retrieveItems(self):
for slot in range(SLOTS):
item_id = player.GetItemIndex(slot)
if item_id != 0:
item.SelectItem(item_id)
self.list.append(Item(item_id, slot, int(item.GetItemSize()[1]), item.GetItemName()))
def sortByID(self):
self.list.sort(key=lambda item: item.id, reverse=False)
def getNextSlot(self, size):
for page in range(int(math.ceil(SLOTS / SLOTS_PER_PAGE))):
LAST_SLOT_THIS_PAGE = min((page + 1) * SLOTS_PER_PAGE, SLOTS)
for slot in range(page * SLOTS_PER_PAGE, LAST_SLOT_THIS_PAGE):
if slot + 5 * (size - 1) < LAST_SLOT_THIS_PAGE:
good = True
for i in range(size):
if (slot + 5 * i) in self.placed:
good = False
if good:
return slot
return -1
def itemsInSlot(self, slot, size):
items = []
for i in range(size):
cur_slot = slot + i * 5
it = [x for x in self.list if x.slot == cur_slot or (x.size == 2 and x.slot + 5 == cur_slot or False) or (x.size == 3 and (x.slot + 10 == cur_slot or x.slot + 5 == cur_slot) or False) ]
if len(it) > 0 and it[0] not in items:
items.append(it[0])
return items
def getNextFreeSlot(self, size, skip = []):
for page in range(int(math.ceil(SLOTS / SLOTS_PER_PAGE))):
LAST_SLOT_THIS_PAGE = min((page + 1) * SLOTS_PER_PAGE, SLOTS)
for slot in range(page * SLOTS_PER_PAGE, LAST_SLOT_THIS_PAGE):
if slot not in skip and len(self.itemsInSlot(slot, size)) == 0 and slot + 5 * (size - 1) < LAST_SLOT_THIS_PAGE:
return slot
return -1
def setItemAsPlaced(self, item):
for i in range(item.size):
self.placed.append(item.slot + 5 * i)
def sort(self):
self.retrieveItems()
self.sortByID()
for it in self.list:
next_slot = self.getNextSlot(it.size)
if next_slot == -1:
continue;
if it.slot == next_slot:
self.setItemAsPlaced(it)
continue
occupied_slots = []
for i in range(it.slot):
occupied_slots.append(next_slot + 5 * i)
for in_way_item in self.itemsInSlot(next_slot, it.size):
next_free_slot = self.getNextFreeSlot(in_way_item.size, occupied_slots)
if next_free_slot == -1:
continue
in_way_item.move(next_free_slot)
it.move(next_slot)
self.setItemAsPlaced(it)
handler = ItemHandler()
handler.sort()
Codice non mio. Lo si può integrare con M2Bob o con qualsiasi py loader
Esempio: (Qui attivato tramite comando)
Spero che possa tornare utile.