Track useless supply chests per item group

This commit is contained in:
Tanner committed 2026-11-18 20:54:17 +00:00
1 parent 37f72d674f
commit 86cf8672f6
1 file changed
+63 -15
+63 -15
View File
@@ -365,6 +365,33 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
def _chest_event_key(event): def _chest_event_key(event):
return (event.get('timestamp'), tuple(event.get('position', ()))) return (event.get('timestamp'), tuple(event.get('position', ())))
@staticmethod
def _supply_group_key(item_ids):
return tuple(sorted(item_ids))
def _useless_chest_item_key(self, chest_key, item_ids):
return (tuple(chest_key), self._supply_group_key(item_ids))
def _remember_empty_supply_groups(self):
key = getattr(self, 'current_supply_chest_key', None)
window = self.g.get('window')
if key is None or not window:
return
container_slots = window.get('container_slots', 0)
if container_slots not in (27, 54):
return
available_item_ids = {
slot.item_id for slot_num in range(container_slots)
if (slot := window.contents.get(slot_num)) and slot.present
}
for item_ids in self.supplies:
useless_key = self._useless_chest_item_key(key, item_ids)
if available_item_ids.intersection(item_ids):
self.useless_chests.discard(useless_key)
else:
self.useless_chests.add(useless_key)
def _refresh_useless_chests(self): def _refresh_useless_chests(self):
events = self.g.get('chest_open_events', ()) events = self.g.get('chest_open_events', ())
current_events = {self._chest_event_key(event) for event in events} current_events = {self._chest_event_key(event) for event in events}
@@ -375,10 +402,13 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
self._processed_chest_open_events.add(event_key) self._processed_chest_open_events.add(event_key)
position = tuple(event.get('position', ())) position = tuple(event.get('position', ()))
reopened = [key for key in self.useless_chests reopened = [key for key in self.useless_chests
if position in key] if position in key[0]]
for key in reopened: for key in reopened:
self.useless_chests.remove(key) self.useless_chests.discard(key)
self.checked_supply_chests.discard(key) for chest_key in tuple(self.checked_supply_chests):
if position in chest_key:
self.checked_supply_chests.discard(chest_key)
if reopened:
print('Supply chest was opened by another player; checking it again:', print('Supply chest was opened by another player; checking it again:',
position) position)
self._processed_chest_open_events.intersection_update(current_events) self._processed_chest_open_events.intersection_update(current_events)
@@ -393,9 +423,24 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
def choose_supply_chest(self): def choose_supply_chest(self):
print('Choosing a supply-labeled chest...') print('Choosing a supply-labeled chest...')
missing_groups = [
self._supply_group_key(item_ids)
for item_ids, (minimum, _maximum) in self.supplies.items()
if self.g.game.count_items(item_ids) < minimum
]
if not missing_groups:
self.state = self.cleanup
return
for container in self.supply_chests: for container in self.supply_chests:
key = tuple(container['positions']) key = tuple(container['positions'])
if key in self.useless_chests or key in self.checked_supply_chests: useless_groups = {
item_ids for chest_key, item_ids in self.useless_chests
if chest_key == key
}
if (key in self.checked_supply_chests or
all(item_ids in useless_groups
for item_ids in missing_groups)):
continue continue
self.checked_supply_chests.add(key) self.checked_supply_chests.add(key)
self.current_supply_chest_key = key self.current_supply_chest_key = key
@@ -443,32 +488,35 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
def choose_items(self): def choose_items(self):
print('Selecting next item') print('Selecting next item')
for items, limits in self.supplies.items(): self._remember_empty_supply_groups()
for item_ids, limits in self.supplies.items():
minimum_items, maximum_stacks = limits minimum_items, maximum_stacks = limits
print('Checking items:', items) print('Checking items:', item_ids)
num_items = self.g.game.count_items(items) num_items = self.g.game.count_items(item_ids)
print('Have:', num_items) print('Have:', num_items)
if num_items >= minimum_items: if num_items >= minimum_items:
print('Have enough, skipping') print('Have enough, skipping')
continue continue
if items in self.checked_supplies: if self._useless_chest_item_key(
self.current_supply_chest_key, item_ids) in self.useless_chests:
print('No matching items in this supply chest, skipping')
continue
if item_ids in self.checked_supplies:
print('Already checked, skipping') print('Already checked, skipping')
continue continue
print('Need some') print('Need some')
self.checked_supplies.append(items) self.checked_supplies.append(item_ids)
self.target_items = items self.target_items = item_ids
self.maximum_stacks = maximum_stacks self.maximum_stacks = maximum_stacks
self.count = 0 self.count = 0
self.state = self.grab_items self.state = self.grab_items
return return
print('No matching items in this supply chest') print('No more needed items in this supply chest')
key = getattr(self, 'current_supply_chest_key', None)
if key is not None and key not in self.useless_chests:
self.useless_chests.append(key)
self.state = self.close_supply_chest self.state = self.close_supply_chest
def grab_items(self): def grab_items(self):
@@ -585,7 +633,7 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
self.supplies = self.default_supplies() self.supplies = self.default_supplies()
self.supply_chests = [] self.supply_chests = []
self.checked_supply_chests = set() self.checked_supply_chests = set()
self.useless_chests = [] self.useless_chests = set()
self._processed_chest_open_events = { self._processed_chest_open_events = {
self._chest_event_key(event) self._chest_event_key(event)
for event in self.g.get('chest_open_events', ()) for event in self.g.get('chest_open_events', ())