Files
minecraft-bot/mosfet/jobs/grab_supplies.py
T

611 lines
23 KiB
Python

import re
import time
import importlib
import random
from itertools import count
from math import 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
from mosfet.jobs.work_boundary import WorkBoundaryReturnMixin
class GrabSuppliesStates(WorkBoundaryReturnMixin):
DEFAULT_SUPPLIES = {
tuple(items.BED_IDS): (1, 1),
(items.CHEST_ID,): (1, 1),
tuple(items.FOOD_IDS): (1, 1),
}
def default_supplies(self):
return dict(self.DEFAULT_SUPPLIES)
def idle(self):
return None
def init(self):
self._capture_work_boundary()
print('Started grab supplies states')
self.checked_supply_chests = set()
used_slots = self.g.game.count_inventory_slots()
print('used:', used_slots, 'total:', self.g.maximum_supply_slots)
self.checked_ground_items = set()
self.checked_ground_groups = set()
self.checked_supply_chests = set()
self.protected_ground_items_logged = set()
self.ground_stack_counts = {}
self.failed_drop_slots = set()
self.pending_drop_slot = None
self.ground_entity_id = None
self.ground_position = None
if self.supplies:
self.state = self.check_supplies
else:
print('Aborting getting supplies, none specified')
self.state = self.cleanup
def _ground_bed_reserved_by_nearby_bot(self, obj):
if obj.item_id not in items.BED_IDS:
return False
position = (obj.x, obj.y, obj.z)
nearby_bots = utils.find_nearby_bots(self.g, position, 4)
if nearby_bots:
logged = getattr(self, 'protected_ground_items_logged', set())
if obj.entity_id not in logged:
print('Leaving bed drop for nearby bot {} at {}'.format(
nearby_bots[0]['name'], utils.pint(position)))
logged.add(obj.entity_id)
self.protected_ground_items_logged = logged
return True
return False
def _has_nearby_ground_items(self, item_ids, maximum_stacks):
if (maximum_stacks and
self.ground_stack_counts.get(item_ids, 0) >= maximum_stacks):
return False
p = utils.pint(self.g.pos)
return any(
obj.entity_id not in self.checked_ground_items and
not self._ground_bed_reserved_by_nearby_bot(obj) and
utils.phyp(p, (obj.x, obj.y, obj.z)) <= 16
for obj in self.g.world.find_objects(item_ids))
def has_nearby_ground_supply(self):
return any(
self._has_nearby_ground_items(item_ids, limits[1])
for item_ids, limits in self.supplies.items())
def _protected_inventory_slots(self):
needed = set(items.NEEDED_ITEMS)
keep = set()
covered = set()
groups = (
items.BED_IDS, {items.CHEST_ID}, items.FOOD_IDS,
items.AXE_IDS, items.SHOVEL_IDS,
)
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 9 <= 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 9 <= 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])
for item_id in items.WANTED_ITEMS:
stacks = [(slot, item) for slot, item in self.g.inv.items()
if 9 <= 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 _inventory_replacement_rank(self, slot):
item = self.g.inv[slot]
important = set(items.NEEDED_ITEMS) | set(items.WANTED_ITEMS)
return (item.item_id in important, -(item.item_count or 0), slot)
def _least_important_inventory_slot(self, hotbar_only=False,
excluded_item_ids=(),
inventory_range=None):
if hotbar_only:
first_slot, last_slot = 36, 44
elif inventory_range is not None:
first_slot, last_slot = inventory_range
else:
first_slot, last_slot = 9, 45
if hotbar_only:
for slot in range(first_slot, last_slot + 1):
item = self.g.inv.get(slot)
if not item or not item.present:
return slot
protected = self._protected_inventory_slots()
excluded = set(excluded_item_ids)
candidates = [
(slot, item) for slot, item in self.g.inv.items()
if first_slot <= slot <= last_slot and item.present and
slot not in protected and item.item_id not in excluded and
(not self.g.get('drop_waiting') or
slot != self.g.drop_waiting.get('slot')) and
(hotbar_only or slot not in self.failed_drop_slots)
]
if not candidates:
return None
important = set(items.NEEDED_ITEMS) | set(items.WANTED_ITEMS)
candidates.sort(key=lambda pair: (
pair[1].item_id in important,
-(pair[1].item_count or 0), pair[0]))
return candidates[0][0]
def _drop_stack_for_ground_pickup(self):
slot = self._least_important_inventory_slot(
excluded_item_ids=self.target_items)
if slot is None:
return False
item = self.g.inv[slot]
print('Dropping lesser-priority stack to make pickup room:',
item.item_id, item.item_count, 'from slot', slot)
if not self.g.game.drop_inventory_slot(slot, whole_stack=True):
return False
self.pending_drop_slot = slot
self.wait_time = 0.0
self.state = self.wait_for_inventory_space
return True
def _record_ground_pickup(self):
self.checked_ground_items.add(self.ground_entity_id)
self.ground_stack_counts[self.target_items] = (
self.ground_stack_counts.get(self.target_items, 0) + 1)
if (self.target_maximum_stacks and
self.ground_stack_counts[self.target_items] >=
self.target_maximum_stacks):
self.checked_ground_groups.add(self.target_items)
self.g.path = []
self.ground_entity_id = None
self.ground_position = None
def check_supplies(self):
# Search every configured item group on the ground before looking in
# labeled chests; a missing bed must not hide a nearby chest or food drop.
missing = []
for item_ids, limits in self.supplies.items():
minimum, maximum = limits
print('Checking items:', item_ids)
num_items = self.g.game.count_items(item_ids)
print('Have:', num_items)
if (item_ids not in self.checked_ground_groups and
self._has_nearby_ground_items(item_ids, maximum)):
print('Found matching supplies on the ground')
self.target_items = item_ids
self.target_minimum = minimum
self.target_maximum_stacks = maximum
self.state = self.find_ground_items
return
if num_items >= minimum:
print('Have enough, skipping')
continue
missing.append((item_ids, minimum, maximum))
if item_ids not in self.checked_ground_groups:
self.target_items = item_ids
self.target_minimum = minimum
self.target_maximum_stacks = maximum
self.state = self.find_ground_items
return
if missing:
self.target_items, self.target_minimum, self.target_maximum_stacks = missing[0]
print('No more ground drops; checking supply-labeled chests for',
self.target_items)
self.state = self.find_supply_chests
return
print('Aborting, dont need any supplies')
self.state = self.cleanup
def find_ground_items(self):
stack_count = self.ground_stack_counts.get(self.target_items, 0)
if self.target_maximum_stacks and stack_count >= self.target_maximum_stacks:
self.checked_ground_groups.add(self.target_items)
if (self.g.game.count_items(self.target_items) <
self.target_minimum):
self.state = self.find_supply_chests
else:
self.state = self.check_supplies
return
print('Looking for supplies on the ground...')
p = utils.pint(self.g.pos)
ground_items = self.g.world.find_objects(self.target_items)
ground_items.sort(key=lambda obj: utils.phyp(
p, (obj.x, obj.y, obj.z)))
for obj in ground_items:
if (obj.entity_id in self.checked_ground_items or
self._ground_bed_reserved_by_nearby_bot(obj)):
continue
current_count = self.g.game.count_items(self.target_items)
if utils.phyp(p, (obj.x, obj.y, obj.z)) > 16:
continue
target = utils.pint((obj.x, obj.y, obj.z))
self.ground_entity_id = obj.entity_id
self.ground_position = target
self.ground_required_count = max(
self.target_minimum, current_count + 1)
if utils.phyp(p, (obj.x, obj.y, obj.z)) <= 1.25:
self.g.path = []
self.wait_time = 1.0
self.state = self.wait_for_ground_item
return
navpath = self.g.world.path_to_place(p, target)
if not navpath:
self.checked_ground_items.add(obj.entity_id)
continue
print('Going to ground item', obj.item_id, target)
self.g.path = navpath
self.state = self.going_to_ground_item
return
self.checked_ground_groups.add(self.target_items)
self.state = self.check_supplies
def going_to_ground_item(self):
obj = self.g.objects.get(self.ground_entity_id)
have_enough = (self.g.game.count_items(self.target_items) >=
self.ground_required_count)
if have_enough:
print('Picked up required ground supplies')
self._record_ground_pickup()
self.state = self.check_supplies
elif (obj is None or
utils.pint(self.g.pos) == self.ground_position or
utils.phyp(self.g.pos, (obj.x, obj.y, obj.z)) <= 1.25):
if (self.g.game.count_inventory_slots() >=
self.g.maximum_supply_slots):
if not self._drop_stack_for_ground_pickup():
print('No lesser-priority stack available for ground pickup')
self.checked_ground_items.add(self.ground_entity_id)
self.g.path = []
self.ground_entity_id = None
self.ground_position = None
self.state = self.find_ground_items
else:
self.wait_time = 1.0
self.state = self.wait_for_ground_item
def wait_for_inventory_space(self):
if (self.g.game.count_items(self.target_items) >=
self.ground_required_count):
print('Picked up required ground supplies after freeing inventory space')
self._record_ground_pickup()
self.pending_drop_slot = None
self.state = self.check_supplies
return
if self.g.get('drop_waiting'):
return
if (self.g.game.count_inventory_slots() <
self.g.maximum_supply_slots):
self.pending_drop_slot = None
self.wait_time = 1.0
self.state = self.wait_for_ground_item
return
if self.pending_drop_slot is not None:
self.failed_drop_slots.add(self.pending_drop_slot)
self.pending_drop_slot = None
if self.wait_time > 0:
self.wait_time -= utils.TICK
return
print('Drop did not free inventory space; trying another stack')
if not self._drop_stack_for_ground_pickup():
print('No more lesser-priority stacks available')
self.checked_ground_items.add(self.ground_entity_id)
self.g.path = []
self.ground_entity_id = None
self.ground_position = None
self.state = self.find_ground_items
def wait_for_ground_item(self):
if (self.g.game.count_items(self.target_items) >=
self.ground_required_count):
print('Picked up required ground supplies')
self._record_ground_pickup()
self.state = self.check_supplies
return
if self.wait_time > 0:
self.wait_time -= utils.TICK
return
if self.ground_entity_id not in self.g.objects:
# Entity removal and inventory updates can arrive in either order.
self.state = self.check_supplies
return
if (self.g.game.count_inventory_slots() >=
self.g.maximum_supply_slots):
self.state = self.going_to_ground_item
return
print('Could not pick up ground item; trying another')
self.checked_ground_items.add(self.ground_entity_id)
self.g.path = []
self.ground_entity_id = None
self.ground_position = None
self.state = self.find_ground_items
@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.remove(key)
self.checked_supply_chests.discard(key)
print('Supply chest was opened by another player; checking it again:',
position)
self._processed_chest_open_events.intersection_update(current_events)
def find_supply_chests(self):
print('Finding chests labeled for this bot\'s supplies...')
p = utils.pint(self.g.pos)
self.supply_chests = self.g.world.find_supply_chests(
p, self.g.name, 80)
print('Found:', len(self.supply_chests), 'supply-labeled chests')
self.state = self.choose_supply_chest
def choose_supply_chest(self):
print('Choosing a supply-labeled chest...')
for container in self.supply_chests:
key = tuple(container['positions'])
if key in self.useless_chests or key in self.checked_supply_chests:
continue
self.checked_supply_chests.add(key)
self.current_supply_chest_key = key
self.chest = container['position']
self.area = self.chest
print('Chose supply chest:', self.chest)
self.state = self.path_to_supply_chest
return
print('No more supply-labeled chests')
self.state = self.cleanup
def path_to_supply_chest(self):
print('Finding path to supply chest')
p = utils.pint(self.g.pos)
navpath = self.g.world.path_to_place_faked(p, self.chest)
print('navpath:', navpath)
if navpath:
self.g.path = navpath[:-1]
self.opening = self.g.path[-1]
self.state = self.going_to_supply_chest
self.checked_supplies = []
else:
print('Supply chest is unreachable; trying another labeled chest')
self.state = self.choose_supply_chest
def going_to_supply_chest(self):
if utils.pint(self.g.pos) == self.opening:
self.g.look_at = self.area
self.state = self.open_supply_chest
def open_supply_chest(self):
print('Opening supply chest')
self.g.game.open_container(self.area)
self.wait_time = 1
self.state = self.wait
def wait(self):
# wait for server to send us inventory contents
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.choose_items
def choose_items(self):
print('Selecting next item')
for items, limits in self.supplies.items():
minimum_items, maximum_stacks = limits
print('Checking items:', items)
num_items = self.g.game.count_items(items)
print('Have:', num_items)
if num_items >= minimum_items:
print('Have enough, skipping')
continue
if items in self.checked_supplies:
print('Already checked, skipping')
continue
print('Need some')
self.checked_supplies.append(items)
self.target_items = items
self.maximum_stacks = maximum_stacks
self.count = 0
self.state = self.grab_items
return
print('No matching items in this supply chest')
key = getattr(self, 'current_supply_chest_key', None)
if key is not None and key not in self.useless_chests:
self.useless_chests.append(key)
self.state = self.close_supply_chest
def grab_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 w.get('container_slots', 0) not in (27, 54):
print('Got wrong container size, aborting')
self.state = self.cleanup
return
used_slots = self.g.game.count_inventory_slots()
print('used:', used_slots, 'total:', self.g.maximum_supply_slots)
for slot_num in range(w.container_slots):
if slot_num not in w.contents:
continue
slot = w.contents[slot_num]
if not slot.present:
continue
if slot.item_id not in self.target_items:
continue
if self.maximum_stacks and self.count >= self.maximum_stacks:
break
if used_slots >= self.g.maximum_supply_slots:
inventory_slot = self._least_important_inventory_slot(
hotbar_only=True, excluded_item_ids=self.target_items)
main_slot = self._least_important_inventory_slot(
excluded_item_ids=self.target_items,
inventory_range=(9, 35))
hotbar_item = (self.g.inv.get(inventory_slot)
if inventory_slot is not None else None)
use_main_stack = (
main_slot is not None and
(inventory_slot is None or
(hotbar_item and hotbar_item.present and
self._inventory_replacement_rank(main_slot) <
self._inventory_replacement_rank(inventory_slot))))
if use_main_stack:
transit_slot = next((hotbar_slot for hotbar_slot in
range(36, 45)
if (self.g.inv.get(hotbar_slot) and
self.g.inv[hotbar_slot].present and
self.g.inv[hotbar_slot].item_id not in
self.target_items)), None)
if transit_slot is None:
print('No hotbar slot available to stage inventory stack')
self.g.game.close_window()
self.g.look_at = None
self.state = self.cleanup
return
window_slot = w.container_slots + main_slot - 9
print('Moving lesser-priority stack from inventory slot',
main_slot, 'through hotbar slot', transit_slot)
self.g.item_lock = True
if self.g.game.swap_container_slot_with_hotbar(
window_slot, transit_slot):
return
self.g.item_lock = False
return
if inventory_slot is None:
print('No replaceable inventory stack; cannot take supply')
self.g.game.close_window()
self.g.look_at = None
self.state = self.cleanup
return
print('Swapping', slot, 'for hotbar slot', inventory_slot)
self.g.item_lock = True
if self.g.game.swap_container_slot_with_hotbar(
slot_num, inventory_slot):
self.count += 1
return
self.g.item_lock = False
return
self.count += 1
print('Moving', slot)
self.g.item_lock = True
self.g.game.click_window(slot_num, 0, 1, slot)
return
print('None left to move')
self.wait_time = 0.25
self.state = self.wait
def close_supply_chest(self):
print('Closing supply chest')
self.g.game.close_window()
self.g.look_at = None
self.state = self.choose_supply_chest
def cleanup(self):
if self._maybe_return_to_work_boundary(self.cleanup):
return
self.state = self.done
def done(self):
# never gets ran, placeholder
return None
def __init__(self, global_state):
self.g = global_state
self.state = self.idle
self.supplies = self.default_supplies()
self.supply_chests = []
self.checked_supply_chests = set()
self.useless_chests = []
self._processed_chest_open_events = {
self._chest_event_key(event)
for event in self.g.get('chest_open_events', ())
}
self.current_supply_chest_key = None
self.supply_chest = None
self.checked_supplies = []
self.target_items = None
self.maximum_stacks = 0
self.count = 0
self.area = None
self.opening = None
self.wait_time = 0
self.failed_drop_slots = set()
self.pending_drop_slot = None
self.protected_ground_items_logged = set()
self.work_policies = []
def run(self):
self._track_work_boundary_position()
self._refresh_useless_chests()
self.state()