Skip full cache chests until reopened
This commit is contained in:
1 parent
5197c1ece7
commit
1b4dc21ea4
2 files changed
+150
-4
No files matched your search
@@ -19,6 +19,36 @@ class CacheItemsStates:
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _chest_event_key(event):
|
||||
return (event.get('timestamp'), tuple(event.get('position', ())))
|
||||
|
||||
def _refresh_useless_chests(self):
|
||||
events = self.g.get('chest_open_events', ())
|
||||
current_events = {self._chest_event_key(event) for event in events}
|
||||
for event in events:
|
||||
event_key = self._chest_event_key(event)
|
||||
if event_key in self._processed_chest_open_events:
|
||||
continue
|
||||
self._processed_chest_open_events.add(event_key)
|
||||
position = tuple(event.get('position', ()))
|
||||
reopened = [key for key in self.useless_chests
|
||||
if position in key]
|
||||
for key in reopened:
|
||||
self.useless_chests.discard(key)
|
||||
self.checked_labeled_chests.discard(key)
|
||||
print('Cache chest was opened by another player; checking it again:',
|
||||
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):
|
||||
world = self.g.get('world')
|
||||
if world is None:
|
||||
@@ -49,6 +79,8 @@ class CacheItemsStates:
|
||||
self.bad_openings = set()
|
||||
self.is_labeled_container = False
|
||||
self.container_item_ids = None
|
||||
self.current_container_key = None
|
||||
self.container_was_full_on_open = None
|
||||
|
||||
num_stacks = self.g.game.count_inventory_slots()
|
||||
has_override = 'minimum' in self.__dict__
|
||||
@@ -103,14 +135,18 @@ class CacheItemsStates:
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
for container in self.labeled_chests:
|
||||
key = container['positions']
|
||||
if key in self.checked_labeled_chests:
|
||||
key = tuple(tuple(position) for position in
|
||||
(container.get('positions') or
|
||||
(container['position'],)))
|
||||
if (key in self.checked_labeled_chests or
|
||||
key in self.useless_chests):
|
||||
continue
|
||||
self.checked_labeled_chests.add(key)
|
||||
if not self._has_transferable_item(container['item_ids']):
|
||||
continue
|
||||
|
||||
self.area = container['position']
|
||||
self.current_container_key = key
|
||||
self.container_item_ids = container['item_ids']
|
||||
self.is_labeled_container = True
|
||||
for opening in w.find_cache_openings(self.area, p):
|
||||
@@ -130,6 +166,11 @@ class CacheItemsStates:
|
||||
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
|
||||
@@ -145,6 +186,7 @@ class CacheItemsStates:
|
||||
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'])
|
||||
@@ -246,6 +288,7 @@ class CacheItemsStates:
|
||||
|
||||
def open_chest(self):
|
||||
print('Opening chest')
|
||||
self.container_was_full_on_open = None
|
||||
self.failed_transfer_slots.clear()
|
||||
self.pending_transfer = None
|
||||
self.g.game.open_container(
|
||||
@@ -353,8 +396,16 @@ class CacheItemsStates:
|
||||
w_inventory_slots = range(w.container_slots, len(w.contents))
|
||||
|
||||
used_slots = self.g.game.count_window_slots()
|
||||
print('used:', used_slots, 'total:', len(w_container_slots))
|
||||
if used_slots >= len(w_container_slots):
|
||||
capacity = len(w_container_slots)
|
||||
print('used:', used_slots, 'total:', capacity)
|
||||
if self.container_was_full_on_open is None:
|
||||
self.container_was_full_on_open = used_slots >= capacity
|
||||
if used_slots >= capacity:
|
||||
if (self.container_was_full_on_open and
|
||||
self.current_container_key is not None):
|
||||
self.useless_chests.add(self.current_container_key)
|
||||
print('Chest was full when first opened; skipping until another player opens it:',
|
||||
self.current_container_key)
|
||||
print('Container is too full, aborting')
|
||||
self.g.game.close_window()
|
||||
self.g.look_at = None
|
||||
@@ -456,6 +507,13 @@ class CacheItemsStates:
|
||||
|
||||
self.labeled_chests = []
|
||||
self.checked_labeled_chests = set()
|
||||
self.useless_chests = set()
|
||||
self._processed_chest_open_events = {
|
||||
self._chest_event_key(event)
|
||||
for event in self.g.get('chest_open_events', ())
|
||||
}
|
||||
self.current_container_key = None
|
||||
self.container_was_full_on_open = None
|
||||
self.is_labeled_container = False
|
||||
self.container_item_ids = None
|
||||
self.area = None
|
||||
@@ -466,4 +524,5 @@ class CacheItemsStates:
|
||||
self.wait_time = 0
|
||||
|
||||
def run(self):
|
||||
self._refresh_useless_chests()
|
||||
self.state()
|
||||
@@ -217,6 +217,93 @@ class CacheItemsTests(unittest.TestCase):
|
||||
state.choose_trapped_chest()
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_full_chest_is_blacklisted_when_first_opened(self):
|
||||
position = (5, 64, 0)
|
||||
key = (position,)
|
||||
window = Munch(container_slots=27, contents={})
|
||||
closed = []
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_window_slots():
|
||||
return 27
|
||||
|
||||
@staticmethod
|
||||
def close_window():
|
||||
closed.append(True)
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
game=FakeGame(), window=window, inv={}, item_lock=False,
|
||||
look_at=None))
|
||||
state.current_container_key = key
|
||||
state.is_labeled_container = True
|
||||
|
||||
state.move_items()
|
||||
|
||||
self.assertIn(key, state.useless_chests)
|
||||
self.assertTrue(state.container_was_full_on_open)
|
||||
self.assertEqual(closed, [True])
|
||||
|
||||
def test_chest_filled_by_this_cache_round_is_not_blacklisted(self):
|
||||
position = (5, 64, 0)
|
||||
key = (position,)
|
||||
item_id = items.get_id('dirt')
|
||||
stack = Munch(present=True, item_id=item_id, item_count=16)
|
||||
window = Munch(container_slots=27, contents={
|
||||
**dict.fromkeys(range(27)), 27: stack,
|
||||
})
|
||||
clicks = []
|
||||
|
||||
class FakeGame:
|
||||
used = 26
|
||||
|
||||
@classmethod
|
||||
def count_window_slots(cls):
|
||||
return cls.used
|
||||
|
||||
@classmethod
|
||||
def click_window(cls, *_args):
|
||||
clicks.append(True)
|
||||
cls.used = 27
|
||||
window.contents[27] = Munch(
|
||||
present=False, item_id=0, item_count=0)
|
||||
|
||||
@staticmethod
|
||||
def close_window():
|
||||
return None
|
||||
|
||||
state = CacheItemsStates(Munch(
|
||||
game=FakeGame(), window=window, inv={}, item_lock=False,
|
||||
look_at=None))
|
||||
state.current_container_key = key
|
||||
|
||||
state.move_items()
|
||||
self.assertFalse(state.container_was_full_on_open)
|
||||
self.assertEqual(clicks, [True])
|
||||
|
||||
state.pending_transfer['confirm_after'] = 0
|
||||
state.g.item_lock = False
|
||||
state.move_items()
|
||||
|
||||
self.assertNotIn(key, state.useless_chests)
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_other_player_open_removes_chest_from_full_blacklist(self):
|
||||
position = (5, 64, 0)
|
||||
key = (position, (6, 64, 0))
|
||||
g = Munch(chest_open_events=[])
|
||||
state = CacheItemsStates(g)
|
||||
state.useless_chests.add(key)
|
||||
state.checked_labeled_chests.add(key)
|
||||
|
||||
g.chest_open_events.append({
|
||||
'position': (6, 64, 0), 'timestamp': 1.0,
|
||||
})
|
||||
state.run()
|
||||
|
||||
self.assertNotIn(key, state.useless_chests)
|
||||
self.assertNotIn(key, state.checked_labeled_chests)
|
||||
|
||||
def test_mutually_reachable_opening_is_skipped_after_path_failure(self):
|
||||
start = (0, 64, 0)
|
||||
first = (4, 64, 0)
|
||||
|
||||
Reference in new issue
Block a user