Add signed general cache destinations
This commit is contained in:
1 parent
11c1bad0c4
commit
86fbf911a5
7 files changed
+305
-114
No files matched your search
@@ -47,6 +47,17 @@ AXE_IDS = get_set(AXES)
|
||||
FOOD_IDS = get_set(FOOD)
|
||||
SAPLING_IDS = get_set(SAPLINGS)
|
||||
LOG_IDS = get_set(LOGS)
|
||||
_LOG_FAMILY_ITEM_IDS = {}
|
||||
for _name in LOGS:
|
||||
_unstripped_name = _name.removeprefix('stripped_')
|
||||
for _suffix in ('_log', '_wood', '_stem', '_hyphae'):
|
||||
if _unstripped_name.endswith(_suffix):
|
||||
_family_name = _unstripped_name[:-len(_suffix)] + ' log'
|
||||
_family_items = get_set({_name})
|
||||
if _family_items:
|
||||
_LOG_FAMILY_ITEM_IDS.setdefault(
|
||||
_family_name, set()).update(_family_items)
|
||||
break
|
||||
ITEM_NAMES = {item['protocol_id']: name.removeprefix('minecraft:')
|
||||
for name, item in ITEMS.items()}
|
||||
|
||||
@@ -92,6 +103,7 @@ _LABEL_ITEM_IDS = {
|
||||
'wheat': {WHEAT_ID},
|
||||
'log': LOG_IDS,
|
||||
'sapling': SAPLING_IDS,
|
||||
**_LOG_FAMILY_ITEM_IDS,
|
||||
}
|
||||
|
||||
|
||||
|
||||
+64
-63
@@ -12,7 +12,6 @@ from mosfet.protocol.managers import ChunkNotLoadedException
|
||||
from mosfet import utils
|
||||
from mosfet import path
|
||||
from mosfet.jobs.work_boundary import WorkBoundaryReturnMixin
|
||||
from mosfet.info import blocks
|
||||
from mosfet.info import items
|
||||
from mosfet.info import mobs
|
||||
|
||||
@@ -42,15 +41,7 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
position)
|
||||
self._processed_chest_open_events.intersection_update(current_events)
|
||||
|
||||
def _chest_positions(self, chest):
|
||||
finder = getattr(self.g.world, '_double_chest_positions', None)
|
||||
if finder:
|
||||
positions = finder(tuple(chest))
|
||||
if positions:
|
||||
return tuple(tuple(position) for position in positions)
|
||||
return (tuple(chest),)
|
||||
|
||||
def _has_compatible_labeled_chest_near(self, bed, distance=16):
|
||||
def _has_compatible_cache_chest_near(self, bed, distance=16):
|
||||
world = self.g.get('world')
|
||||
if world is None:
|
||||
return False
|
||||
@@ -69,6 +60,13 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
slot in keep_slots):
|
||||
continue
|
||||
return True
|
||||
for chest in world.find_cache_chests(
|
||||
bed, self.g.get('name', ''), distance):
|
||||
positions = chest.get('positions') or (chest['position'],)
|
||||
if min(utils.phyp(bed, position) for position in positions) > distance:
|
||||
continue
|
||||
if self._has_transferable_item(items.ITEM_NAMES):
|
||||
return True
|
||||
return False
|
||||
|
||||
def _find_throw_signs(self):
|
||||
@@ -152,7 +150,7 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
print('No reachable throw opening for sign', sign['position'])
|
||||
|
||||
self.throw_sign = None
|
||||
self.state = self.choose_trapped_chest
|
||||
self.state = self.choose_cache_chest
|
||||
|
||||
def going_to_throw_sign(self):
|
||||
if utils.pint(self.g.pos) == self.opening:
|
||||
@@ -234,7 +232,7 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
'minimum', self.g.get('minimum_cache_slots', 27))
|
||||
post_sleep_bed = self.g.pop('post_sleep_cache_bed', None)
|
||||
if (not has_override and post_sleep_bed is not None and
|
||||
self._has_compatible_labeled_chest_near(post_sleep_bed)):
|
||||
self._has_compatible_cache_chest_near(post_sleep_bed)):
|
||||
minimum = ceil(minimum / 2)
|
||||
print('Lowering cache threshold after sleep near a compatible chest:',
|
||||
minimum)
|
||||
@@ -242,13 +240,13 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
if num_stacks == 0:
|
||||
self.state = self.cleanup
|
||||
elif num_stacks >= minimum:
|
||||
self.state = self.find_trapped_chests
|
||||
self.state = self.find_cache_targets
|
||||
else:
|
||||
print('Aborting caching, not full')
|
||||
self.state = self.cleanup
|
||||
|
||||
def find_trapped_chests(self):
|
||||
print('Finding labeled and trapped chests...')
|
||||
def find_cache_targets(self):
|
||||
print('Finding item-specific and general cache chests...')
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
@@ -256,10 +254,11 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
self.checked_throw_signs = set()
|
||||
self.labeled_chests = w.find_labeled_chests(p, 105)
|
||||
self.checked_labeled_chests = set()
|
||||
self.trapped_chests = w.find_blocks_indexed(p, blocks.TRAPPED_CHEST_IDS)
|
||||
print('Found {} labeled chests; found {} trapped chests: {}'.format(
|
||||
len(self.labeled_chests), len(self.trapped_chests),
|
||||
self.trapped_chests))
|
||||
self.cache_chests = w.find_cache_chests(
|
||||
p, self.g.get('name', ''), 105)
|
||||
self.checked_cache_chests = set()
|
||||
print('Found {} item-specific chests and {} general cache chests'.format(
|
||||
len(self.labeled_chests), len(self.cache_chests)))
|
||||
self.state = self.choose_throw_sign
|
||||
|
||||
def _opening_in_bad_region(self, world, opening):
|
||||
@@ -276,8 +275,8 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
return True
|
||||
return False
|
||||
|
||||
def choose_trapped_chest(self):
|
||||
print('Choosing a labeled or trapped chest...')
|
||||
def choose_cache_chest(self):
|
||||
print('Choosing an item-specific or general cache chest...')
|
||||
self.bad_openings = set()
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
@@ -310,61 +309,62 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
self.open_interaction = opening
|
||||
self.g.path = navpath
|
||||
self.g.look_at = None
|
||||
self.state = self.going_to_trapped_chest
|
||||
self.state = self.going_to_cache_chest
|
||||
return
|
||||
self.bad_openings.add(opening_position)
|
||||
print('No path to labeled chest', self.area)
|
||||
|
||||
while (self.trapped_chests and
|
||||
self._chest_positions(self.trapped_chests[0]) in
|
||||
self.useless_chests):
|
||||
self.trapped_chests.pop(0)
|
||||
|
||||
if not len(self.trapped_chests):
|
||||
self.is_labeled_container = False
|
||||
self.container_item_ids = None
|
||||
if self.transferred_to_labeled_chest:
|
||||
print('Already cached items in labeled chests; skipping new chest')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
print('No labeled or trapped chests')
|
||||
self.state = self.select_chest
|
||||
return
|
||||
|
||||
# General cache chests are a fallback, after all matching item-specific
|
||||
# chests have been checked.
|
||||
self.is_labeled_container = False
|
||||
self.container_item_ids = None
|
||||
chest = self.trapped_chests[0]
|
||||
self.area = chest
|
||||
self.current_container_key = self._chest_positions(chest)
|
||||
|
||||
for opening in w.find_cache_openings(chest, p):
|
||||
opening_position = tuple(opening['position'])
|
||||
if self._opening_in_bad_region(w, opening_position):
|
||||
for container in self.cache_chests:
|
||||
key = tuple(tuple(position) for position in
|
||||
(container.get('positions') or
|
||||
(container['position'],)))
|
||||
if (key in self.checked_cache_chests or
|
||||
key in self.useless_chests):
|
||||
continue
|
||||
self.checked_cache_chests.add(key)
|
||||
if not self._has_transferable_item(items.ITEM_NAMES):
|
||||
continue
|
||||
navpath = w.path_to_place(p, opening_position)
|
||||
if (navpath and
|
||||
self._accept_cache_trip(p, opening_position, navpath)):
|
||||
print('navpath to trapped chest:', navpath)
|
||||
self.opening = opening_position
|
||||
self.open_interaction = opening
|
||||
self.g.path = navpath
|
||||
self.g.look_at = None
|
||||
self.state = self.going_to_trapped_chest
|
||||
return
|
||||
self.bad_openings.add(opening_position)
|
||||
|
||||
self.trapped_chests.pop(0)
|
||||
time.sleep(0.1)
|
||||
chest = container['position']
|
||||
self.area = chest
|
||||
self.current_container_key = key
|
||||
for opening in w.find_cache_openings(chest, p):
|
||||
opening_position = tuple(opening['position'])
|
||||
if self._opening_in_bad_region(w, opening_position):
|
||||
continue
|
||||
navpath = w.path_to_place(p, opening_position)
|
||||
if (navpath and
|
||||
self._accept_cache_trip(p, opening_position, navpath)):
|
||||
print('Going to general cache chest:', chest)
|
||||
self.opening = opening_position
|
||||
self.open_interaction = opening
|
||||
self.g.path = navpath
|
||||
self.g.look_at = None
|
||||
self.state = self.going_to_cache_chest
|
||||
return
|
||||
self.bad_openings.add(opening_position)
|
||||
print('No path to general cache chest:', chest)
|
||||
|
||||
def going_to_trapped_chest(self):
|
||||
if self.transferred_to_labeled_chest:
|
||||
print('Already cached items in item-specific chests; skipping new chest')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
print('No item-specific or general cache chests')
|
||||
self.state = self.select_chest
|
||||
|
||||
def going_to_cache_chest(self):
|
||||
if utils.pint(self.g.pos) == self.opening:
|
||||
self.g.look_at = self.open_interaction['look_at']
|
||||
self.wait_time = 0.5
|
||||
self.state = self.turn_to_trapped_chest
|
||||
self.state = self.turn_to_cache_chest
|
||||
else:
|
||||
self.g.look_at = None
|
||||
|
||||
def turn_to_trapped_chest(self):
|
||||
def turn_to_cache_chest(self):
|
||||
self.g.look_at = self.open_interaction['look_at']
|
||||
self.wait_time -= utils.TICK
|
||||
if self.wait_time <= 0:
|
||||
@@ -629,7 +629,7 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
if self.is_labeled_container:
|
||||
self.is_labeled_container = False
|
||||
self.container_item_ids = None
|
||||
self.state = self.choose_trapped_chest
|
||||
self.state = self.choose_cache_chest
|
||||
else:
|
||||
self.state = self.cleanup
|
||||
|
||||
@@ -660,6 +660,8 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
|
||||
self.labeled_chests = []
|
||||
self.checked_labeled_chests = set()
|
||||
self.cache_chests = []
|
||||
self.checked_cache_chests = set()
|
||||
self.useless_chests = set()
|
||||
self._processed_chest_open_events = {
|
||||
self._chest_event_key(event)
|
||||
@@ -672,7 +674,6 @@ class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
self.area = None
|
||||
self.opening = None
|
||||
self.open_interaction = None
|
||||
self.trapped_chests = []
|
||||
self.bad_areas = []
|
||||
self.wait_time = 0
|
||||
self.throw_signs = []
|
||||
|
||||
+64
-22
@@ -140,7 +140,8 @@ class World:
|
||||
tokens[1] != 'throw'):
|
||||
continue
|
||||
item_name = '_'.join(tokens[2:])
|
||||
item_ids = items.storage_item_ids_for_label(item_name)
|
||||
item_label = ' '.join(tokens[2:])
|
||||
item_ids = items.storage_item_ids_for_label(item_label)
|
||||
if not item_ids:
|
||||
item_ids = items.get_set({item_name})
|
||||
if not item_ids:
|
||||
@@ -272,13 +273,35 @@ class World:
|
||||
return tuple(sorted((tuple(chest), partner)))
|
||||
return (tuple(chest),)
|
||||
|
||||
def _chests_associated_with_sign(self, sign):
|
||||
sign_pos = tuple(sign['position'])
|
||||
side_offsets = (
|
||||
(1, 0, 0), (-1, 0, 0), (0, 0, 1), (0, 0, -1),
|
||||
)
|
||||
top_offsets = ((0, 0, 0), (0, -1, 0))
|
||||
adjacent = {
|
||||
utils.padd(sign_pos, offset)
|
||||
for offset in side_offsets
|
||||
if self.block_at(*utils.padd(sign_pos, offset)) in blocks.CHEST_IDS
|
||||
}
|
||||
if not adjacent:
|
||||
adjacent = {
|
||||
utils.padd(sign_pos, offset)
|
||||
for offset in top_offsets
|
||||
if self.block_at(*utils.padd(sign_pos, offset)) in blocks.CHEST_IDS
|
||||
}
|
||||
if not adjacent:
|
||||
nearby = self.find_blocks_indexed(
|
||||
sign_pos, blocks.CHEST_IDS, 2)
|
||||
if nearby:
|
||||
nearest = min(utils.phyp(sign_pos, chest) for chest in nearby)
|
||||
adjacent = {chest for chest in nearby
|
||||
if utils.phyp(sign_pos, chest) == nearest}
|
||||
return adjacent
|
||||
|
||||
def find_labeled_chests(self, center, distance=105):
|
||||
"""Find single/double chests assigned item categories by nearby signs."""
|
||||
grouped = {}
|
||||
sign_offsets = (
|
||||
(0, 0, 0), (0, -1, 0),
|
||||
(1, 0, 0), (-1, 0, 0), (0, 0, 1), (0, 0, -1),
|
||||
)
|
||||
for sign in self.find_signs(center, distance):
|
||||
categories = set()
|
||||
labels = set()
|
||||
@@ -290,22 +313,9 @@ class World:
|
||||
if not categories:
|
||||
continue
|
||||
|
||||
sign_pos = sign['position']
|
||||
adjacent = {
|
||||
utils.padd(sign_pos, offset)
|
||||
for offset in sign_offsets
|
||||
if self.block_at(*utils.padd(sign_pos, offset)) in blocks.CHEST_IDS
|
||||
}
|
||||
if not adjacent:
|
||||
nearby = self.find_blocks_indexed(
|
||||
sign_pos, blocks.CHEST_IDS, 2)
|
||||
if nearby:
|
||||
nearest = min(utils.phyp(sign_pos, chest)
|
||||
for chest in nearby)
|
||||
adjacent = {chest for chest in nearby
|
||||
if utils.phyp(sign_pos, chest) == nearest}
|
||||
|
||||
for chest in adjacent:
|
||||
# Side-mounted signs take priority over signs on top when chests
|
||||
# occupy both positions beside the sign.
|
||||
for chest in self._chests_associated_with_sign(sign):
|
||||
positions = self._double_chest_positions(chest)
|
||||
record = grouped.setdefault(positions, {
|
||||
'position': tuple(chest),
|
||||
@@ -316,7 +326,39 @@ class World:
|
||||
})
|
||||
record['item_ids'].update(categories)
|
||||
record['labels'].update(labels)
|
||||
record['sign_positions'].add(sign_pos)
|
||||
record['sign_positions'].add(tuple(sign['position']))
|
||||
|
||||
result = list(grouped.values())
|
||||
result.sort(key=lambda chest: utils.phyp(center, chest['position']))
|
||||
return result
|
||||
|
||||
def find_cache_chests(self, center, bot_name, distance=105):
|
||||
"""Find bot/group-addressed general cache chests from signs."""
|
||||
bot_name = str(bot_name).casefold()
|
||||
group_name = (bot_name[:-1] if len(bot_name) > 1 and
|
||||
bot_name[-1] in '123' else None)
|
||||
addresses = {bot_name}
|
||||
if group_name:
|
||||
addresses.add(group_name)
|
||||
|
||||
grouped = {}
|
||||
for sign in self.find_signs(center, distance):
|
||||
text = ' '.join(line for line in sign['lines'] if line.strip())
|
||||
tokens = re.findall(r'[a-z0-9_]+', text.casefold())
|
||||
if (len(tokens) < 2 or tokens[0] not in addresses or
|
||||
'cache' not in tokens[1:]):
|
||||
continue
|
||||
|
||||
for chest in self._chests_associated_with_sign(sign):
|
||||
positions = self._double_chest_positions(chest)
|
||||
record = grouped.setdefault(positions, {
|
||||
'position': tuple(chest),
|
||||
'positions': positions,
|
||||
'labels': set(),
|
||||
'sign_positions': set(),
|
||||
})
|
||||
record['labels'].add('cache')
|
||||
record['sign_positions'].add(tuple(sign['position']))
|
||||
|
||||
result = list(grouped.values())
|
||||
result.sort(key=lambda chest: utils.phyp(center, chest['position']))
|
||||
|
||||
Reference in new issue
Block a user