Recover inventory space when gathering supplies
This commit is contained in:
1 parent
1b4dc21ea4
commit
1a7bdb2be4
4 files changed
+435
-13
No files matched your search
@@ -1373,6 +1373,21 @@ class Game(LegacyGame):
|
||||
self.g.connection.write_packet(ClientTickEnd(), force=True)
|
||||
return True
|
||||
|
||||
def swap_container_slot_with_hotbar(self, container_slot, inventory_slot):
|
||||
window = self.g.get('window')
|
||||
if not window or not 36 <= inventory_slot <= 44:
|
||||
return False
|
||||
packet = ContainerClickSwapPacket(
|
||||
container_id=window.window_id,
|
||||
state_id=self.g.inventory_state_ids.get(
|
||||
window.window_id, window.state_id),
|
||||
slot=container_slot,
|
||||
button=inventory_slot - 36,
|
||||
)
|
||||
self.g.connection.write_packet(packet, force=True)
|
||||
self.g.connection.write_packet(ClientTickEnd(), force=True)
|
||||
return True
|
||||
|
||||
def close_window(self):
|
||||
window = self.g.get('window')
|
||||
if not window:
|
||||
|
||||
+196
-12
@@ -34,15 +34,13 @@ class GrabSuppliesStates:
|
||||
|
||||
used_slots = self.g.game.count_inventory_slots()
|
||||
print('used:', used_slots, 'total:', self.g.maximum_supply_slots)
|
||||
if used_slots >= self.g.maximum_supply_slots:
|
||||
print('Inventory is too full, aborting')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
self.checked_ground_items = set()
|
||||
self.checked_ground_groups = set()
|
||||
self.checked_supply_chests = set()
|
||||
self.ground_stack_counts = {}
|
||||
self.failed_drop_slots = set()
|
||||
self.pending_drop_slot = None
|
||||
self.ground_entity_id = None
|
||||
self.ground_position = None
|
||||
if self.supplies:
|
||||
@@ -66,6 +64,94 @@ class GrabSuppliesStates:
|
||||
self._has_nearby_ground_items(item_ids, limits[1])
|
||||
for item_ids, limits in self.supplies.items())
|
||||
|
||||
def _protected_inventory_slots(self):
|
||||
needed = set(items.NEEDED_ITEMS)
|
||||
keep = set()
|
||||
covered = set()
|
||||
groups = (
|
||||
items.BED_IDS, {items.CHEST_ID}, items.FOOD_IDS,
|
||||
items.AXE_IDS, items.SHOVEL_IDS,
|
||||
)
|
||||
for group in groups:
|
||||
group = needed.intersection(group)
|
||||
if not group:
|
||||
continue
|
||||
covered.update(group)
|
||||
stacks = [(slot, item) for slot, item in self.g.inv.items()
|
||||
if 9 <= slot <= 45 and item.present and
|
||||
item.item_id in group]
|
||||
if stacks:
|
||||
keep.add(max(stacks, key=lambda pair: pair[1].item_count)[0])
|
||||
|
||||
for item_id in needed - covered:
|
||||
stacks = [(slot, item) for slot, item in self.g.inv.items()
|
||||
if 9 <= slot <= 45 and item.present and
|
||||
item.item_id == item_id]
|
||||
if stacks:
|
||||
keep.add(max(stacks, key=lambda pair: pair[1].item_count)[0])
|
||||
|
||||
for item_id in items.WANTED_ITEMS:
|
||||
stacks = [(slot, item) for slot, item in self.g.inv.items()
|
||||
if 9 <= slot <= 45 and item.present and
|
||||
item.item_id == item_id]
|
||||
if stacks:
|
||||
keep.add(max(stacks, key=lambda pair: pair[1].item_count)[0])
|
||||
return keep
|
||||
|
||||
def _inventory_replacement_rank(self, slot):
|
||||
item = self.g.inv[slot]
|
||||
important = set(items.NEEDED_ITEMS) | set(items.WANTED_ITEMS)
|
||||
return (item.item_id in important, -(item.item_count or 0), slot)
|
||||
|
||||
def _least_important_inventory_slot(self, hotbar_only=False,
|
||||
excluded_item_ids=(),
|
||||
inventory_range=None):
|
||||
if hotbar_only:
|
||||
first_slot, last_slot = 36, 44
|
||||
elif inventory_range is not None:
|
||||
first_slot, last_slot = inventory_range
|
||||
else:
|
||||
first_slot, last_slot = 9, 45
|
||||
if hotbar_only:
|
||||
for slot in range(first_slot, last_slot + 1):
|
||||
item = self.g.inv.get(slot)
|
||||
if not item or not item.present:
|
||||
return slot
|
||||
|
||||
protected = self._protected_inventory_slots()
|
||||
excluded = set(excluded_item_ids)
|
||||
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
|
||||
(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)
|
||||
]
|
||||
if not candidates:
|
||||
return None
|
||||
|
||||
important = set(items.NEEDED_ITEMS) | set(items.WANTED_ITEMS)
|
||||
candidates.sort(key=lambda pair: (
|
||||
pair[1].item_id in important,
|
||||
-(pair[1].item_count or 0), pair[0]))
|
||||
return candidates[0][0]
|
||||
|
||||
def _drop_stack_for_ground_pickup(self):
|
||||
slot = self._least_important_inventory_slot(
|
||||
excluded_item_ids=self.target_items)
|
||||
if slot is None:
|
||||
return False
|
||||
item = self.g.inv[slot]
|
||||
print('Dropping lesser-priority stack to make pickup room:',
|
||||
item.item_id, item.item_count, 'from slot', slot)
|
||||
if not self.g.game.drop_inventory_slot(slot, whole_stack=True):
|
||||
return False
|
||||
self.pending_drop_slot = slot
|
||||
self.wait_time = 0.0
|
||||
self.state = self.wait_for_inventory_space
|
||||
return True
|
||||
|
||||
def _record_ground_pickup(self):
|
||||
self.checked_ground_items.add(self.ground_entity_id)
|
||||
self.ground_stack_counts[self.target_items] = (
|
||||
@@ -177,8 +263,54 @@ class GrabSuppliesStates:
|
||||
elif (obj is None or
|
||||
utils.pint(self.g.pos) == self.ground_position or
|
||||
utils.phyp(self.g.pos, (obj.x, obj.y, obj.z)) <= 1.25):
|
||||
if (self.g.game.count_inventory_slots() >=
|
||||
self.g.maximum_supply_slots):
|
||||
if not self._drop_stack_for_ground_pickup():
|
||||
print('No lesser-priority stack available for ground pickup')
|
||||
self.checked_ground_items.add(self.ground_entity_id)
|
||||
self.g.path = []
|
||||
self.ground_entity_id = None
|
||||
self.ground_position = None
|
||||
self.state = self.find_ground_items
|
||||
else:
|
||||
self.wait_time = 1.0
|
||||
self.state = self.wait_for_ground_item
|
||||
|
||||
def wait_for_inventory_space(self):
|
||||
if (self.g.game.count_items(self.target_items) >=
|
||||
self.ground_required_count):
|
||||
print('Picked up required ground supplies after freeing inventory space')
|
||||
self._record_ground_pickup()
|
||||
self.pending_drop_slot = None
|
||||
self.state = self.check_supplies
|
||||
return
|
||||
|
||||
if self.g.get('drop_waiting'):
|
||||
return
|
||||
|
||||
if (self.g.game.count_inventory_slots() <
|
||||
self.g.maximum_supply_slots):
|
||||
self.pending_drop_slot = None
|
||||
self.wait_time = 1.0
|
||||
self.state = self.wait_for_ground_item
|
||||
return
|
||||
|
||||
if self.pending_drop_slot is not None:
|
||||
self.failed_drop_slots.add(self.pending_drop_slot)
|
||||
self.pending_drop_slot = None
|
||||
|
||||
if self.wait_time > 0:
|
||||
self.wait_time -= utils.TICK
|
||||
return
|
||||
|
||||
print('Drop did not free inventory space; trying another stack')
|
||||
if not self._drop_stack_for_ground_pickup():
|
||||
print('No more lesser-priority stacks available')
|
||||
self.checked_ground_items.add(self.ground_entity_id)
|
||||
self.g.path = []
|
||||
self.ground_entity_id = None
|
||||
self.ground_position = None
|
||||
self.state = self.find_ground_items
|
||||
|
||||
def wait_for_ground_item(self):
|
||||
if (self.g.game.count_items(self.target_items) >=
|
||||
@@ -197,6 +329,11 @@ class GrabSuppliesStates:
|
||||
self.state = self.check_supplies
|
||||
return
|
||||
|
||||
if (self.g.game.count_inventory_slots() >=
|
||||
self.g.maximum_supply_slots):
|
||||
self.state = self.going_to_ground_item
|
||||
return
|
||||
|
||||
print('Could not pick up ground item; trying another')
|
||||
self.checked_ground_items.add(self.ground_entity_id)
|
||||
self.g.path = []
|
||||
@@ -328,12 +465,6 @@ class GrabSuppliesStates:
|
||||
return
|
||||
used_slots = self.g.game.count_inventory_slots()
|
||||
print('used:', used_slots, 'total:', self.g.maximum_supply_slots)
|
||||
if used_slots >= self.g.maximum_supply_slots:
|
||||
print('Inventory is too full, aborting')
|
||||
self.g.game.close_window()
|
||||
self.g.look_at = None
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
for slot_num in range(w.container_slots):
|
||||
if slot_num not in w.contents:
|
||||
@@ -349,10 +480,61 @@ class GrabSuppliesStates:
|
||||
|
||||
if self.maximum_stacks and self.count >= self.maximum_stacks:
|
||||
break
|
||||
if used_slots >= self.g.maximum_supply_slots:
|
||||
inventory_slot = self._least_important_inventory_slot(
|
||||
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))
|
||||
hotbar_item = (self.g.inv.get(inventory_slot)
|
||||
if inventory_slot is not None else None)
|
||||
use_main_stack = (
|
||||
main_slot is not None and
|
||||
(inventory_slot is None or
|
||||
(hotbar_item and hotbar_item.present and
|
||||
self._inventory_replacement_rank(main_slot) <
|
||||
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 from inventory slot',
|
||||
main_slot, 'through hotbar slot', transit_slot)
|
||||
self.g.item_lock = True
|
||||
if self.g.game.swap_container_slot_with_hotbar(
|
||||
window_slot, transit_slot):
|
||||
return
|
||||
self.g.item_lock = False
|
||||
return
|
||||
|
||||
if inventory_slot is None:
|
||||
print('No replaceable inventory stack; cannot take supply')
|
||||
self.g.game.close_window()
|
||||
self.g.look_at = None
|
||||
self.state = self.cleanup
|
||||
return
|
||||
print('Swapping', slot, 'for hotbar slot', inventory_slot)
|
||||
self.g.item_lock = True
|
||||
if self.g.game.swap_container_slot_with_hotbar(
|
||||
slot_num, inventory_slot):
|
||||
self.count += 1
|
||||
return
|
||||
self.g.item_lock = False
|
||||
return
|
||||
|
||||
self.count += 1
|
||||
|
||||
print('Moving', slot)
|
||||
|
||||
self.g.item_lock = True
|
||||
self.g.game.click_window(slot_num, 0, 1, slot)
|
||||
return
|
||||
@@ -395,6 +577,8 @@ class GrabSuppliesStates:
|
||||
self.area = None
|
||||
self.opening = None
|
||||
self.wait_time = 0
|
||||
self.failed_drop_slots = set()
|
||||
self.pending_drop_slot = None
|
||||
|
||||
def run(self):
|
||||
self._refresh_useless_chests()
|
||||
|
||||
Reference in new issue
Block a user