diff --git a/mosfet/jobs/grab_supplies.py b/mosfet/jobs/grab_supplies.py index 05a6333..b5c52d6 100644 --- a/mosfet/jobs/grab_supplies.py +++ b/mosfet/jobs/grab_supplies.py @@ -64,6 +64,8 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin): self.ground_stack_counts = {} self.failed_drop_slots = set() self.pending_drop_slot = None + self.failed_supply_stash_slots = set() + self.pending_supply_stash = None self.ground_entity_id = None self.ground_position = None if self.supplies: @@ -202,7 +204,8 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin): def _least_important_inventory_slot(self, hotbar_only=False, excluded_item_ids=(), - inventory_range=None): + inventory_range=None, + excluded_slots=()): if hotbar_only: first_slot, last_slot = 36, 44 elif inventory_range is not None: @@ -217,10 +220,12 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin): protected = self._protected_inventory_slots() excluded = set(excluded_item_ids) + excluded_slots = set(excluded_slots) candidates = [ (slot, item) for slot, item in self.g.inv.items() if first_slot <= slot <= last_slot and item.present and - slot not in protected and item.item_id not in excluded and + slot not in protected and slot not in excluded_slots and + item.item_id not in excluded and (not self.g.get('drop_waiting') or slot != self.g.drop_waiting.get('slot')) and (hotbar_only or slot not in self.failed_drop_slots) @@ -537,6 +542,8 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin): continue self.checked_supply_chests.add(key) self.current_supply_chest_key = key + self.failed_supply_stash_slots = set() + self.pending_supply_stash = None self.chest = container['position'] self.area = self.chest print('Chose supply chest:', self.chest) @@ -613,6 +620,24 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin): self.state = self.close_supply_chest def grab_items(self): + pending = self.pending_supply_stash + if pending is not None: + stack = self.g.inv.get(pending['slot']) + moved = (not stack or not stack.present or + stack.item_id != pending['item_id'] or + stack.item_count < pending['item_count']) + if moved: + self.pending_supply_stash = None + elif time.monotonic() >= pending['deadline']: + slot = pending['slot'] + print('Could not stash inventory slot', slot, + 'in the supply chest; skipping it', flush=True) + self.failed_supply_stash_slots.add(slot) + self.pending_supply_stash = None + self.g.item_lock = False + else: + return + if self.g.item_lock: return w = self.g.window @@ -646,7 +671,8 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin): hotbar_only=True, excluded_item_ids=self.target_items) main_slot = self._least_important_inventory_slot( excluded_item_ids=self.target_items, - inventory_range=(9, 35)) + inventory_range=(9, 35), + excluded_slots=self.failed_supply_stash_slots) hotbar_item = (self.g.inv.get(inventory_slot) if inventory_slot is not None else None) use_main_stack = ( @@ -657,28 +683,24 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin): self._inventory_replacement_rank(inventory_slot)))) if use_main_stack: - transit_slot = next((hotbar_slot for hotbar_slot in - range(36, 45) - if (self.g.inv.get(hotbar_slot) and - self.g.inv[hotbar_slot].present and - self.g.inv[hotbar_slot].item_id not in - self.target_items)), None) - if transit_slot is None: - print('No hotbar slot available to stage inventory stack') - self.g.game.close_window() - self.g.look_at = None - self.state = self.cleanup - return window_slot = w.container_slots + main_slot - 9 - print('Moving lesser-priority stack', - self._stack_label(self.g.inv[main_slot]), + stack_to_stash = self.g.inv[main_slot] + print('Stashing lesser-priority stack', + self._stack_label(stack_to_stash), 'from inventory slot', main_slot, - 'through hotbar slot', transit_slot) + 'in the supply chest') + self.pending_supply_stash = { + 'slot': main_slot, + 'item_id': stack_to_stash.item_id, + 'item_count': stack_to_stash.item_count, + 'deadline': time.monotonic() + 2.0, + } self.g.item_lock = True - if self.g.game.swap_container_slot_with_hotbar( - window_slot, transit_slot): + if self.g.game.click_window(window_slot): return self.g.item_lock = False + self.failed_supply_stash_slots.add(main_slot) + self.pending_supply_stash = None return if inventory_slot is None: