816 lines
31 KiB
Python
816 lines
31 KiB
Python
import re
|
|
import time
|
|
import importlib
|
|
import random
|
|
from itertools import count
|
|
from math import ceil, floor
|
|
|
|
from minecraft.networking.types import BlockFace
|
|
|
|
from mosfet.protocol.managers import ChunkNotLoadedException
|
|
|
|
from mosfet import utils
|
|
from mosfet import path
|
|
from mosfet.info import blocks
|
|
from mosfet.info import items
|
|
from mosfet.info import mobs
|
|
|
|
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:
|
|
return False
|
|
keep_slots = self._needed_inventory_slots_to_keep()
|
|
for chest in world.find_labeled_chests(bed, distance):
|
|
positions = chest.get('positions') or (chest['position'],)
|
|
if min(utils.phyp(bed, position) for position in positions) > distance:
|
|
continue
|
|
item_ids = chest['item_ids']
|
|
for slot, item in self.g.inv.items():
|
|
if (not 9 <= slot <= 44 or not item.present or
|
|
item.item_id not in item_ids or
|
|
item.item_id in items.WANTED_ITEMS):
|
|
continue
|
|
if (item.item_id in items.NEEDED_ITEMS and
|
|
slot in keep_slots):
|
|
continue
|
|
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', ''), 105)
|
|
|
|
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'))
|
|
|
|
MAX_WORK_BOUNDARY_OVERSHOOT = 40
|
|
|
|
def _capture_work_policies(self):
|
|
world = self.g.get('world')
|
|
finder = getattr(world, 'sign_policies', None)
|
|
self.work_policies = (finder(self.g.get('name', ''))
|
|
if callable(finder) else [])
|
|
position = self.g.get('pos')
|
|
self.cache_start_position = (
|
|
utils.pint(position) if position is not None and
|
|
position is not False else None)
|
|
self.cache_return_point = (
|
|
self.cache_start_position
|
|
if self.cache_start_position is not None and
|
|
self._inside_work_policies(self.cache_start_position) else None)
|
|
self.cache_return_required = False
|
|
self.cache_trip_max_outside = (
|
|
self._work_boundary_overshoot([self.cache_start_position])
|
|
if self.work_policies and self.cache_start_position is not None
|
|
else 0.0)
|
|
self.return_retry_at = 0.0
|
|
|
|
def _inside_work_policies(self, position):
|
|
world = self.g.get('world')
|
|
return bool(self.work_policies) and all(
|
|
world._inside_sign_policy(position, policy)
|
|
for policy in self.work_policies)
|
|
|
|
def _work_boundary_overshoot(self, positions):
|
|
world = self.g.world
|
|
return max((
|
|
max(0.0,
|
|
world._sign_policy_distance(position, policy) -
|
|
policy['distance'])
|
|
for position in positions for policy in self.work_policies
|
|
), default=0.0)
|
|
|
|
def _return_candidates(self, world, from_position):
|
|
candidates = []
|
|
for position in (self.cache_return_point,
|
|
self.cache_start_position):
|
|
if (position is not None and
|
|
self._inside_work_policies(position) and
|
|
position not in candidates):
|
|
candidates.append(position)
|
|
|
|
standable = getattr(world, '_cache_opening_standable', None)
|
|
if callable(standable):
|
|
offsets = ((0, 0), (1, 0), (-1, 0), (0, 1), (0, -1),
|
|
(1, 1), (1, -1), (-1, 1), (-1, -1))
|
|
for policy in self.work_policies:
|
|
anchor = tuple(policy['position'])
|
|
radius = max(0.0, policy['distance'] - 1.0)
|
|
dx = from_position[0] - anchor[0]
|
|
dz = from_position[2] - anchor[2]
|
|
if policy['shape'] == 'square':
|
|
base_x = anchor[0] + max(-radius, min(radius, dx))
|
|
base_z = anchor[2] + max(-radius, min(radius, dz))
|
|
else:
|
|
distance = utils.hypot(dx, dz)
|
|
scale = min(1.0, radius / distance) if distance else 0.0
|
|
base_x = anchor[0] + dx * scale
|
|
base_z = anchor[2] + dz * scale
|
|
base = (int(round(base_x)), anchor[1], int(round(base_z)))
|
|
for dy in (0, -1, 1):
|
|
for ox, oz in offsets:
|
|
point = (base[0] + ox, base[1] + dy,
|
|
base[2] + oz)
|
|
if (point not in candidates and
|
|
self._inside_work_policies(point) and
|
|
standable(point)):
|
|
candidates.append(point)
|
|
|
|
return sorted(candidates, key=lambda point: (
|
|
utils.phyp(from_position, point), point))
|
|
|
|
def _find_valid_return_plan(self, world, from_position):
|
|
for point in self._return_candidates(world, from_position):
|
|
navpath = world.path_to_place(from_position, point)
|
|
if navpath is None:
|
|
continue
|
|
path_points = [tuple(from_position)] + [tuple(pos)
|
|
for pos in navpath]
|
|
if all(world._path_allowed_by_sign(path_points, policy)
|
|
for policy in self.work_policies):
|
|
return point, navpath
|
|
return None, None
|
|
|
|
def _accept_cache_trip(self, start, opening, navpath):
|
|
if not self.work_policies:
|
|
return True
|
|
points = [tuple(start)] + [tuple(position) for position in navpath]
|
|
trip_max = max(self.cache_trip_max_outside,
|
|
self._work_boundary_overshoot(points))
|
|
if (self.cache_return_required or
|
|
trip_max > self.MAX_WORK_BOUNDARY_OVERSHOOT):
|
|
return_point, _return_path = self._find_valid_return_plan(
|
|
self.g.world, tuple(opening))
|
|
if return_point is None:
|
|
print('Skipping cache opening outside work area; no valid return path:',
|
|
opening)
|
|
return False
|
|
self.cache_return_required = True
|
|
self.cache_return_point = return_point
|
|
self.cache_trip_max_outside = trip_max
|
|
return True
|
|
|
|
def return_to_work_area(self):
|
|
if not self.cache_return_required:
|
|
self.state = self.cleanup
|
|
return
|
|
now = time.monotonic()
|
|
if now < self.return_retry_at:
|
|
return
|
|
|
|
current = utils.pint(self.g.pos)
|
|
if (self.cache_return_point is not None and
|
|
current == self.cache_return_point):
|
|
self.g.path = []
|
|
self.g.look_at = None
|
|
self.cache_return_required = False
|
|
self.state = self.cleanup
|
|
return
|
|
|
|
return_point, navpath = self._find_valid_return_plan(
|
|
self.g.world, current)
|
|
if navpath is None:
|
|
if self.return_retry_at == 0.0:
|
|
print('Could not replan a valid path back inside the work area; '
|
|
'will retry')
|
|
self.return_retry_at = now + 2.0
|
|
return
|
|
|
|
self.cache_return_point = return_point
|
|
self.return_retry_at = 0.0
|
|
self.g.path = navpath
|
|
self.g.look_at = None
|
|
print('Returning inside work boundary to', return_point)
|
|
self.state = self.going_to_return_point
|
|
|
|
def going_to_return_point(self):
|
|
if utils.pint(self.g.pos) == self.cache_return_point:
|
|
print('Back inside work boundary at', self.cache_return_point)
|
|
self.g.path = []
|
|
self.g.look_at = None
|
|
self.cache_return_required = False
|
|
self.state = self.cleanup
|
|
|
|
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 or not self._accept_cache_trip(
|
|
center, opening_position, 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 = []
|
|
self.failed_transfer_slots = set()
|
|
self.pending_transfer = None
|
|
self.transferred_to_labeled_chest = False
|
|
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
|
|
self.throw_signs = []
|
|
self.checked_throw_signs = set()
|
|
self.throw_sign = None
|
|
self.throw_failed_slots = set()
|
|
self.pending_throw = None
|
|
self._capture_work_policies()
|
|
|
|
num_stacks = self.g.game.count_inventory_slots()
|
|
has_override = 'minimum' in self.__dict__
|
|
minimum = self.__dict__.pop(
|
|
'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)):
|
|
minimum = ceil(minimum / 2)
|
|
print('Lowering cache threshold after sleep near a compatible chest:',
|
|
minimum)
|
|
print('Inventory amount:', num_stacks)
|
|
if num_stacks == 0:
|
|
self.state = self.cleanup
|
|
elif num_stacks >= minimum:
|
|
self.state = self.find_trapped_chests
|
|
else:
|
|
print('Aborting caching, not full')
|
|
self.state = self.cleanup
|
|
|
|
def find_trapped_chests(self):
|
|
print('Finding labeled and trapped chests...')
|
|
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, 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.state = self.choose_throw_sign
|
|
|
|
def _opening_in_bad_region(self, world, opening):
|
|
opening = tuple(opening)
|
|
if opening in self.bad_openings:
|
|
return True
|
|
if not self.bad_openings:
|
|
return False
|
|
nearest = min(self.bad_openings,
|
|
key=lambda bad: utils.phyp(opening, bad))
|
|
if (world.path_to_place(opening, nearest) and
|
|
world.path_to_place(nearest, opening)):
|
|
self.bad_openings.add(opening)
|
|
return True
|
|
return False
|
|
|
|
def choose_trapped_chest(self):
|
|
print('Choosing a labeled or trapped chest...')
|
|
self.bad_openings = set()
|
|
w = self.g.world
|
|
p = utils.pint(self.g.pos)
|
|
|
|
for container in self.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):
|
|
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 labeled chest', container['labels'],
|
|
self.area)
|
|
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)
|
|
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
|
|
|
|
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):
|
|
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)
|
|
|
|
def going_to_trapped_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
|
|
else:
|
|
self.g.look_at = None
|
|
|
|
def turn_to_trapped_chest(self):
|
|
self.g.look_at = self.open_interaction['look_at']
|
|
self.wait_time -= utils.TICK
|
|
if self.wait_time <= 0:
|
|
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:
|
|
print('No chest, aborting')
|
|
self.state = self.cleanup
|
|
|
|
def find_cache_spot(self):
|
|
print('Finding a chest spot...')
|
|
w = self.g.world
|
|
p = utils.pint(self.g.pos)
|
|
|
|
for area in w.find_cache_areas(p, 16):
|
|
print('Found area:', area)
|
|
if area not in self.bad_areas:
|
|
break
|
|
else: # for
|
|
print('Unable to find area')
|
|
self.state = self.cleanup
|
|
return
|
|
|
|
self.area = area
|
|
openings = w.find_cache_openings(self.area, p)
|
|
|
|
self.bad_openings = set()
|
|
for opening in openings:
|
|
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)):
|
|
self.opening = opening_position
|
|
self.open_interaction = opening
|
|
break
|
|
self.bad_openings.add(opening_position)
|
|
else: # for
|
|
print('Unable to get to cache area', self.area)
|
|
self.bad_areas.append(self.area)
|
|
self.state = self.cleanup
|
|
return
|
|
|
|
self.g.path = navpath
|
|
self.g.look_at = None
|
|
self.state = self.going_to_area
|
|
|
|
def going_to_area(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_cache_spot
|
|
else:
|
|
self.g.look_at = None
|
|
|
|
def turn_to_cache_spot(self):
|
|
self.g.look_at = self.open_interaction['look_at']
|
|
self.wait_time -= utils.TICK
|
|
if self.wait_time <= 0:
|
|
self.state = self.place_chest
|
|
|
|
def place_chest(self):
|
|
support = utils.padd(self.area, path.BLOCK_BELOW)
|
|
self.g.game.place_block(support, BlockFace.TOP, sneaking=True)
|
|
self.state = self.open_chest
|
|
|
|
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(
|
|
self.open_interaction['target'],
|
|
face=self.open_interaction['face'],
|
|
cursor=self.open_interaction['cursor'])
|
|
self.wait_time = 1
|
|
self.state = self.wait
|
|
|
|
def wait(self):
|
|
# wait for server to send us chest contents
|
|
if self.wait_time > 0:
|
|
self.wait_time -= utils.TICK
|
|
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 _has_transferable_item(self, item_ids):
|
|
item_ids = set(item_ids)
|
|
needed_slots = self._needed_inventory_slots_to_keep()
|
|
planned_skip_items = set(self.skip_items)
|
|
for inventory_slot, item in self.g.inv.items():
|
|
if (not item.present or item.item_id not in item_ids or
|
|
inventory_slot in self.skip_inventory_slots):
|
|
continue
|
|
if (item.item_id in items.NEEDED_ITEMS and
|
|
inventory_slot in needed_slots):
|
|
continue
|
|
if (item.item_id in items.WANTED_ITEMS and
|
|
item.item_id not in planned_skip_items):
|
|
planned_skip_items.add(item.item_id)
|
|
continue
|
|
return True
|
|
return False
|
|
|
|
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
|
|
|
|
if not w:
|
|
print('Didnt get a window, aborting')
|
|
self.state = self.cleanup
|
|
return
|
|
if not w.container_slots:
|
|
print('Chest contents have not arrived, aborting')
|
|
self.g.game.close_window()
|
|
self.state = self.cleanup
|
|
return
|
|
if w.container_slots not in (27, 54):
|
|
print('Got non-chest window, aborting')
|
|
self.g.game.close_window()
|
|
self.state = self.cleanup
|
|
return
|
|
|
|
if self.pending_transfer:
|
|
pending = self.pending_transfer
|
|
if time.monotonic() < pending['confirm_after']:
|
|
return
|
|
source = w.contents.get(pending['window_slot'])
|
|
moved = (source is None or not source.present or
|
|
source.item_id != pending['item_id'] or
|
|
source.item_count < pending['item_count'])
|
|
if moved:
|
|
if pending['is_labeled']:
|
|
self.transferred_to_labeled_chest = True
|
|
else:
|
|
self.failed_transfer_slots.add(pending['window_slot'])
|
|
self.pending_transfer = None
|
|
|
|
w_container_slots = range(w.container_slots)
|
|
w_inventory_slots = range(w.container_slots, len(w.contents))
|
|
|
|
used_slots = self.g.game.count_window_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
|
|
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:
|
|
continue
|
|
|
|
slot = w.contents[slot_num]
|
|
|
|
if not slot.present:
|
|
continue
|
|
|
|
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
|
|
|
|
inventory_slot = self._window_player_slot(slot_num)
|
|
if (inventory_slot in self.skip_inventory_slots or
|
|
slot_num in self.failed_transfer_slots):
|
|
continue
|
|
|
|
slot_list.append((slot_num, slot))
|
|
|
|
slot_list.sort(key=lambda x: x[1].item_count, reverse=True)
|
|
|
|
for slot_num, slot in slot_list:
|
|
if slot.item_id in items.WANTED_ITEMS and slot.item_id not in self.skip_items:
|
|
print('skipping wanted item', slot)
|
|
inventory_slot = self._window_player_slot(slot_num)
|
|
if inventory_slot is not None:
|
|
self.skip_inventory_slots.add(inventory_slot)
|
|
self.skip_items.append(slot.item_id)
|
|
continue
|
|
|
|
print('moving', slot)
|
|
|
|
self.pending_transfer = {
|
|
'window_slot': slot_num,
|
|
'item_id': slot.item_id,
|
|
'item_count': slot.item_count,
|
|
'is_labeled': self.is_labeled_container,
|
|
'confirm_after': time.monotonic() + 0.15,
|
|
}
|
|
self.g.item_lock = True
|
|
self.g.game.click_window(slot_num, 0, 1, slot)
|
|
return
|
|
|
|
print('nothing left to move')
|
|
self.state = self.close_chest
|
|
|
|
def close_chest(self):
|
|
print('closing chest')
|
|
self.g.game.close_window()
|
|
if not self.silent:
|
|
self.g.chat.send('cache at ' + str(self.area)[1:-1])
|
|
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):
|
|
if self.cache_return_required:
|
|
self.state = self.return_to_work_area
|
|
return
|
|
self.g.look_at = None
|
|
self.state = self.done
|
|
if (self.g.get('job') and
|
|
getattr(self.g.job.state, '__name__', None) == 'cache_items'):
|
|
self.g.job.state = self.g.job.idle
|
|
|
|
def done(self):
|
|
# never gets ran, placeholder
|
|
return None
|
|
|
|
def __init__(self, global_state):
|
|
self.g = global_state
|
|
self.state = self.idle
|
|
|
|
self.silent = False
|
|
|
|
self.skip_inventory_slots = set()
|
|
self.failed_transfer_slots = set()
|
|
self.pending_transfer = None
|
|
self.transferred_to_labeled_chest = False
|
|
self.skip_items = []
|
|
|
|
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
|
|
self.opening = None
|
|
self.open_interaction = None
|
|
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
|
|
self.work_policies = []
|
|
self.cache_start_position = None
|
|
self.cache_return_point = None
|
|
self.cache_return_required = False
|
|
self.cache_trip_max_outside = 0.0
|
|
self.return_retry_at = 0.0
|
|
|
|
def run(self):
|
|
self._refresh_useless_chests()
|
|
self.state()
|