Add sign-labeled chest storage

This commit is contained in:
Tanner
2026-11-07 16:35:57 +00:00
parent ab8f59a89b
commit 43b37b76f6
7 changed files with 401 additions and 66 deletions
+95 -8
View File
@@ -22,6 +22,8 @@ class CacheItemsStates:
def init(self):
self.skip_slots = []
self.skip_items = []
self.is_labeled_container = False
self.container_item_ids = None
num_stacks = self.g.game.count_inventory_slots()
minimum = getattr(
@@ -36,25 +38,54 @@ class CacheItemsStates:
self.state = self.cleanup
def find_trapped_chests(self):
print('Finding trapped chests...')
print('Finding labeled and trapped chests...')
w = self.g.world
p = utils.pint(self.g.pos)
self.labeled_chests = w.find_labeled_chests(p, 80)
self.checked_labeled_chests = set()
self.trapped_chests = w.find_blocks_indexed(p, blocks.TRAPPED_CHEST_IDS)
print('Found:', self.trapped_chests)
print('Found labeled chests:', len(self.labeled_chests),
'trapped chests:', self.trapped_chests)
self.state = self.choose_trapped_chest
def choose_trapped_chest(self):
print('Choosing a trapped chest...')
print('Choosing a labeled or trapped chest...')
w = self.g.world
p = utils.pint(self.g.pos)
c = self.g.chunks
for container in self.labeled_chests:
key = container['positions']
if key in self.checked_labeled_chests:
continue
self.checked_labeled_chests.add(key)
if not any(item.present and item.item_id in container['item_ids']
for item in self.g.inv.values()):
continue
self.area = container['position']
self.container_item_ids = container['item_ids']
self.is_labeled_container = True
for opening in w.find_cache_openings(self.area):
navpath = w.path_to_place(p, opening)
if navpath:
print('Going to labeled chest', container['labels'],
self.area)
self.opening = opening
self.g.path = navpath
self.state = self.going_to_trapped_chest
return
print('No path to labeled chest', self.area)
if not len(self.trapped_chests):
print('No trapped chests')
self.is_labeled_container = False
self.container_item_ids = None
print('No labeled or trapped chests')
self.state = self.select_chest
return
self.is_labeled_container = False
self.container_item_ids = None
chest = self.trapped_chests[0]
self.area = chest
@@ -83,6 +114,8 @@ class CacheItemsStates:
self.state = self.open_chest
def select_chest(self):
self.is_labeled_container = False
self.container_item_ids = None
if self.g.game.select_item([items.CHEST_ID]):
self.state = self.find_cache_spot
else:
@@ -149,6 +182,41 @@ class CacheItemsStates:
else:
self.state = self.move_items
def _needed_inventory_slots_to_keep(self):
needed = set(items.NEEDED_ITEMS)
groups = (
items.BED_IDS, {items.CHEST_ID}, items.FOOD_IDS,
items.AXE_IDS, items.SHOVEL_IDS,
)
keep = set()
covered = set()
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 5 <= 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 5 <= 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 _window_player_slot(self, window_slot):
offset = window_slot - self.g.window.container_slots
if 0 <= offset < 27:
return 9 + offset
if 27 <= offset < 36:
return 36 + offset - 27
return None
def move_items(self):
if self.g.item_lock: return
w = self.g.window
@@ -177,10 +245,13 @@ class CacheItemsStates:
print('Container is too full, aborting')
self.g.game.close_window()
self.g.look_at = None
self.state = self.cleanup
self.state = (self.close_chest if self.is_labeled_container
else self.cleanup)
return
slot_list = []
needed_slots = (self._needed_inventory_slots_to_keep()
if self.is_labeled_container else set())
for slot_num in w_inventory_slots:
if slot_num not in w.contents:
@@ -191,7 +262,14 @@ class CacheItemsStates:
if not slot.present:
continue
if slot.item_id in items.NEEDED_ITEMS:
if self.is_labeled_container:
if slot.item_id not in self.container_item_ids:
continue
inventory_slot = self._window_player_slot(slot_num)
if (slot.item_id in items.NEEDED_ITEMS and
inventory_slot in needed_slots):
continue
elif slot.item_id in items.NEEDED_ITEMS:
continue
if slot_num in self.skip_slots:
@@ -222,7 +300,12 @@ class CacheItemsStates:
self.g.game.close_window()
if not self.silent:
self.g.chat.send('cache at ' + str(self.area)[1:-1])
self.state = self.cleanup
if self.is_labeled_container:
self.is_labeled_container = False
self.container_item_ids = None
self.state = self.choose_trapped_chest
else:
self.state = self.cleanup
def cleanup(self):
self.g.look_at = None
@@ -244,6 +327,10 @@ class CacheItemsStates:
self.skip_slots = []
self.skip_items = []
self.labeled_chests = []
self.checked_labeled_chests = set()
self.is_labeled_container = False
self.container_item_ids = None
self.area = None
self.opening = None
self.trapped_chests = []