Throw designated items at signs during caching
This commit is contained in:
1 parent
46b0657bcf
commit
ce3be28d0c
4 files changed
+469
-2
No files matched your search
+126
-1
@@ -70,6 +70,119 @@ class CacheItemsStates:
|
||||
return True
|
||||
return False
|
||||
|
||||
def _find_throw_signs(self):
|
||||
world = self.g.get('world')
|
||||
finder = getattr(world, 'find_throw_signs', None)
|
||||
position = self.g.get('pos')
|
||||
if not callable(finder) or position is None or position is False:
|
||||
return []
|
||||
return finder(utils.pint(position), self.g.get('name', ''), 80)
|
||||
|
||||
def _throwable_slots(self, item_ids, excluded=()):
|
||||
item_ids = set(item_ids)
|
||||
excluded = set(excluded)
|
||||
slots = [(slot, item) for slot, item in self.g.get('inv', {}).items()
|
||||
if 9 <= slot <= 45 and slot not in excluded and
|
||||
item.present and item.item_id in item_ids]
|
||||
slots.sort(key=lambda pair: (-pair[1].item_count, pair[0]))
|
||||
return slots
|
||||
|
||||
@staticmethod
|
||||
def _throw_sign_key(sign):
|
||||
return (tuple(sign['position']), sign.get('side'),
|
||||
sign.get('item_name'))
|
||||
|
||||
def choose_throw_sign(self):
|
||||
print('Checking item-throw signs...')
|
||||
world = self.g.get('world')
|
||||
center = utils.pint(self.g.pos)
|
||||
for sign in self.throw_signs:
|
||||
key = self._throw_sign_key(sign)
|
||||
if key in self.checked_throw_signs:
|
||||
continue
|
||||
self.checked_throw_signs.add(key)
|
||||
if not self._throwable_slots(sign['item_ids']):
|
||||
continue
|
||||
|
||||
finder = getattr(world, 'find_throw_openings', None)
|
||||
if not callable(finder):
|
||||
continue
|
||||
for opening in finder(sign, center, 3):
|
||||
opening_position = tuple(opening['position'])
|
||||
navpath = world.path_to_place(center, opening_position)
|
||||
if not navpath:
|
||||
continue
|
||||
self.throw_sign = sign
|
||||
self.throw_failed_slots = set()
|
||||
self.pending_throw = None
|
||||
self.opening = opening_position
|
||||
self.g.path = navpath
|
||||
self.g.look_at = None
|
||||
print('Going to throw', sign['item_name'], 'at sign',
|
||||
sign['position'], 'from', opening_position)
|
||||
self.state = self.going_to_throw_sign
|
||||
return
|
||||
print('No reachable throw opening for sign', sign['position'])
|
||||
|
||||
self.throw_sign = None
|
||||
self.state = self.choose_trapped_chest
|
||||
|
||||
def going_to_throw_sign(self):
|
||||
if utils.pint(self.g.pos) == self.opening:
|
||||
self.g.look_at = self.throw_sign['position']
|
||||
self.wait_time = 0.5
|
||||
self.state = self.turn_to_throw_sign
|
||||
else:
|
||||
self.g.look_at = None
|
||||
|
||||
def turn_to_throw_sign(self):
|
||||
self.g.look_at = self.throw_sign['position']
|
||||
self.wait_time -= utils.TICK
|
||||
if self.wait_time <= 0:
|
||||
self.state = self.throw_items
|
||||
|
||||
def throw_items(self):
|
||||
if self.g.item_lock or self.g.get('drop_waiting'):
|
||||
return
|
||||
slots = self._throwable_slots(
|
||||
self.throw_sign['item_ids'], self.throw_failed_slots)
|
||||
if not slots:
|
||||
print('Finished throwing', self.throw_sign['item_name'],
|
||||
'at sign', self.throw_sign['position'])
|
||||
self.g.look_at = None
|
||||
self.throw_sign = None
|
||||
self.state = self.choose_throw_sign
|
||||
return
|
||||
|
||||
slot_num, item = slots[0]
|
||||
if self.g.game.drop_inventory_slot(slot_num, whole_stack=True):
|
||||
self.pending_throw = {
|
||||
'slot': slot_num,
|
||||
'item_id': item.item_id,
|
||||
'item_count': item.item_count,
|
||||
'deadline': time.monotonic() + 4.0,
|
||||
}
|
||||
self.state = self.wait_for_throw
|
||||
|
||||
def wait_for_throw(self):
|
||||
pending = self.pending_throw
|
||||
if pending is None:
|
||||
self.state = self.throw_items
|
||||
return
|
||||
item = self.g.inv.get(pending['slot'])
|
||||
if (item is None or not item.present or
|
||||
item.item_id != pending['item_id'] or
|
||||
item.item_count < pending['item_count']):
|
||||
print('Throw confirmed from inventory slot', pending['slot'])
|
||||
self.pending_throw = None
|
||||
self.state = self.throw_items
|
||||
elif (self.g.get('drop_waiting') is None and
|
||||
time.monotonic() >= pending['deadline']):
|
||||
print('Throw was not confirmed from slot', pending['slot'])
|
||||
self.throw_failed_slots.add(pending['slot'])
|
||||
self.pending_throw = None
|
||||
self.state = self.throw_items
|
||||
|
||||
def init(self):
|
||||
self.skip_inventory_slots = set()
|
||||
self.skip_items = []
|
||||
@@ -81,6 +194,11 @@ class CacheItemsStates:
|
||||
self.container_item_ids = None
|
||||
self.current_container_key = None
|
||||
self.container_was_full_on_open = None
|
||||
self.throw_signs = []
|
||||
self.checked_throw_signs = set()
|
||||
self.throw_sign = None
|
||||
self.throw_failed_slots = set()
|
||||
self.pending_throw = None
|
||||
|
||||
num_stacks = self.g.game.count_inventory_slots()
|
||||
has_override = 'minimum' in self.__dict__
|
||||
@@ -106,13 +224,15 @@ class CacheItemsStates:
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
self.throw_signs = self._find_throw_signs()
|
||||
self.checked_throw_signs = set()
|
||||
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 {} labeled chests; found {} trapped chests: {}'.format(
|
||||
len(self.labeled_chests), len(self.trapped_chests),
|
||||
self.trapped_chests))
|
||||
self.state = self.choose_trapped_chest
|
||||
self.state = self.choose_throw_sign
|
||||
|
||||
def _opening_in_bad_region(self, world, opening):
|
||||
opening = tuple(opening)
|
||||
@@ -522,6 +642,11 @@ class CacheItemsStates:
|
||||
self.trapped_chests = []
|
||||
self.bad_areas = []
|
||||
self.wait_time = 0
|
||||
self.throw_signs = []
|
||||
self.checked_throw_signs = set()
|
||||
self.throw_sign = None
|
||||
self.throw_failed_slots = set()
|
||||
self.pending_throw = None
|
||||
|
||||
def run(self):
|
||||
self._refresh_useless_chests()
|
||||
|
||||
Reference in new issue
Block a user