Port sand gathering to 26.3
This commit is contained in:
1 parent
f28269f71c
commit
3e8b95c895
9 files changed
+424
-158
No files matched your search
+209
-81
@@ -1,132 +1,260 @@
|
||||
import re
|
||||
"""Gather naturally generated sand using the protocol-777 world APIs."""
|
||||
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 mcdata
|
||||
from mosfet.info import mobs
|
||||
|
||||
|
||||
class GatherSandStates:
|
||||
def bair(self, p):
|
||||
return self.g.chunks.get_block_at(*p) in blocks.NON_SOLID_IDS
|
||||
SEARCH_DISTANCE = 200
|
||||
BREAK_CONFIRM_TIMEOUT = 15.0
|
||||
BREAK_REACH = 4.5
|
||||
NAVIGATION_STALL_TIMEOUT = 20.0
|
||||
NAVIGATION_PROGRESS_EPSILON = 0.15
|
||||
|
||||
def bsand(self, p):
|
||||
return self.g.chunks.get_block_at(*p) == blocks.SAND
|
||||
def _path_head_distance(self):
|
||||
if not self.g.path:
|
||||
return None
|
||||
waypoint = self.g.path[0]
|
||||
return utils.hypot(
|
||||
self.g.pos.x - (waypoint[0] + 0.5),
|
||||
self.g.pos.z - (waypoint[2] + 0.5))
|
||||
|
||||
def _begin_navigation(self):
|
||||
self.navigation_path_head = (
|
||||
tuple(self.g.path[0]) if self.g.path else None)
|
||||
self.navigation_best_distance = self._path_head_distance()
|
||||
self.navigation_last_progress_at = time.monotonic()
|
||||
|
||||
def _clear_navigation(self):
|
||||
self.navigation_path_head = None
|
||||
self.navigation_best_distance = None
|
||||
self.navigation_last_progress_at = None
|
||||
|
||||
def _navigation_stalled(self):
|
||||
now = time.monotonic()
|
||||
path_head = tuple(self.g.path[0]) if self.g.path else None
|
||||
distance = self._path_head_distance()
|
||||
if path_head != self.navigation_path_head:
|
||||
self.navigation_path_head = path_head
|
||||
self.navigation_best_distance = distance
|
||||
self.navigation_last_progress_at = now
|
||||
elif (distance is not None and
|
||||
(self.navigation_best_distance is None or
|
||||
distance <= self.navigation_best_distance -
|
||||
self.NAVIGATION_PROGRESS_EPSILON)):
|
||||
self.navigation_best_distance = distance
|
||||
self.navigation_last_progress_at = now
|
||||
|
||||
return (self.navigation_last_progress_at is not None and
|
||||
now - self.navigation_last_progress_at >=
|
||||
self.NAVIGATION_STALL_TIMEOUT)
|
||||
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
def init(self):
|
||||
if not self.g.sand_origin or not self.g.chunks.check_loaded(self.g.sand_origin):
|
||||
if (not self.g.get('sand_origin_from_sign') and
|
||||
(not self.g.sand_origin or
|
||||
self.g.chunks.get_block_at(*self.g.sand_origin) is None)):
|
||||
self.g.sand_origin = utils.pint(self.g.pos)
|
||||
|
||||
self.state = self.select_shovel
|
||||
|
||||
def select_shovel(self):
|
||||
self.g.game.select_item(items.SHOVEL_IDS)
|
||||
if not self.g.game.select_item(items.SHOVEL_IDS):
|
||||
print('No shovel found; gathering sand without one')
|
||||
self.state = self.find_new_slice
|
||||
|
||||
def _sand_is_safe(self, position):
|
||||
world = self.g.world
|
||||
if world.block_at(*position) != blocks.SAND:
|
||||
return False
|
||||
|
||||
below = world.block_at(*utils.padd(position, (0, -1, 0)))
|
||||
below_two = world.block_at(*utils.padd(position, (0, -2, 0)))
|
||||
above = world.block_at(*utils.padd(position, (0, 1, 0)))
|
||||
if (below is None or below_two is None or above is None or
|
||||
below in blocks.NON_SOLID_IDS or
|
||||
below_two in blocks.NON_SOLID_IDS or
|
||||
above not in blocks.NON_SOLID_IDS):
|
||||
return False
|
||||
return world.sand_adjacent_safe(position)
|
||||
|
||||
def find_new_slice(self):
|
||||
print('Finding new slice...')
|
||||
w = self.g.world
|
||||
origin = self.g.sand_origin
|
||||
|
||||
print('using origin', self.g.sand_origin)
|
||||
start = time.time()
|
||||
self.prev_layer, s = w.find_sand_slice(self.g.sand_origin, 200, 10, self.bad_slices, self.prev_layer)
|
||||
print('Found slice:', s, 'in', time.time() - start, 'seconds')
|
||||
|
||||
if s:
|
||||
self.slice = s
|
||||
self.bad_slices.append(s)
|
||||
self.state = self.find_new_sand
|
||||
else:
|
||||
print('No slices remaining.')
|
||||
print('Finding a new 3x3x1 sand slice...')
|
||||
started = time.monotonic()
|
||||
self.prev_layer, sand_slice = self.g.world.find_sand_slice(
|
||||
self.g.sand_origin, self.SEARCH_DISTANCE, 10,
|
||||
self.bad_slices, self.prev_layer)
|
||||
if sand_slice is None:
|
||||
print('No safe sand slices remain')
|
||||
self.state = self.cleanup
|
||||
|
||||
def find_new_sand(self):
|
||||
print('Finding new sand...')
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
head = utils.padd(p, path.BLOCK_ABOVE)
|
||||
|
||||
for sand in w.find_sand(self.slice, 2, p):
|
||||
if (sand not in self.bad_sand and
|
||||
w.work_position_allowed(sand)):
|
||||
print('Found sand:', sand)
|
||||
break
|
||||
else: # for
|
||||
print('No in-bounds sand left in this slice; trying another.')
|
||||
self.state = self.find_new_slice
|
||||
return
|
||||
|
||||
self.sand = sand
|
||||
self.sand_slice = tuple(sand_slice)
|
||||
self.bad_slices.append(self.sand_slice)
|
||||
self.bad_sand.clear()
|
||||
print('Selected sand slice {} (search {:.3f}s)'.format(
|
||||
self.sand_slice, time.monotonic() - started))
|
||||
self.state = self.find_new_sand
|
||||
|
||||
if utils.phyp(head, self.sand) < blocks.BREAK_DISTANCE:
|
||||
self.state = self.dig_sand
|
||||
else:
|
||||
self.state = self.nav_to_sand
|
||||
def find_new_sand(self):
|
||||
world = self.g.world
|
||||
center = utils.pint(self.g.pos)
|
||||
for sand in world.find_sand(self.sand_slice, 2, center):
|
||||
if (sand not in self.bad_sand and
|
||||
world.work_position_allowed(sand)):
|
||||
self.sand = tuple(sand)
|
||||
self.tried_openings = set()
|
||||
self.state = self.nav_to_sand
|
||||
return
|
||||
|
||||
print('No more eligible sand in slice', self.sand_slice)
|
||||
self.state = self.find_new_slice
|
||||
|
||||
def _sand_openings(self):
|
||||
world = self.g.world
|
||||
openings = []
|
||||
for direction in ((0, 0, -1), (0, 0, 1),
|
||||
(-1, 0, 0), (1, 0, 0)):
|
||||
for height in (1, 0):
|
||||
opening = (self.sand[0] + direction[0],
|
||||
self.sand[1] + height,
|
||||
self.sand[2] + direction[2])
|
||||
if opening in self.tried_openings:
|
||||
continue
|
||||
if not world.work_position_allowed(opening):
|
||||
continue
|
||||
feet = world.block_at(*opening)
|
||||
head = world.block_at(*utils.padd(opening, (0, 1, 0)))
|
||||
floor = world.block_at(*utils.padd(opening, (0, -1, 0)))
|
||||
if (feet not in blocks.NON_SOLID_IDS or
|
||||
head not in blocks.NON_SOLID_IDS or floor is None or
|
||||
floor in blocks.NON_SOLID_IDS):
|
||||
continue
|
||||
openings.append(opening)
|
||||
return openings
|
||||
|
||||
def nav_to_sand(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
c = self.g.chunks
|
||||
world = self.g.world
|
||||
if not self._sand_is_safe(self.sand):
|
||||
self.state = self.find_new_sand
|
||||
return
|
||||
|
||||
navpath = w.path_to_place_faked(p, self.sand)
|
||||
print('navpath:', navpath)
|
||||
|
||||
if navpath:
|
||||
self.g.path = navpath[:-1]
|
||||
position = utils.pint(self.g.pos)
|
||||
routes = []
|
||||
for opening in self._sand_openings():
|
||||
navpath = ([opening] if position == opening else
|
||||
world.path_to_place(position, opening))
|
||||
if navpath:
|
||||
routes.append((len(navpath), opening, navpath))
|
||||
if routes:
|
||||
_, self.opening, navpath = min(routes)
|
||||
self.g.path = navpath
|
||||
self.g.look_at = self.sand
|
||||
self._begin_navigation()
|
||||
print('Going to sand', self.sand, 'via', self.opening)
|
||||
self.state = self.going_to_sand
|
||||
else:
|
||||
print('Cant get to that sand')
|
||||
time.sleep(0.1)
|
||||
self.bad_sand.append(self.sand)
|
||||
print('Cannot path to sand; skipping', self.sand)
|
||||
self.bad_sand.add(self.sand)
|
||||
self.state = self.find_new_sand
|
||||
|
||||
def going_to_sand(self):
|
||||
if not len(self.g.path):
|
||||
self.g.look_at = self.sand
|
||||
if self.g.world.block_at(*self.sand) != blocks.SAND:
|
||||
self.g.path = []
|
||||
self._clear_navigation()
|
||||
self.state = self.find_new_sand
|
||||
elif utils.pint(self.g.pos) == self.opening:
|
||||
self.g.path = []
|
||||
self._clear_navigation()
|
||||
self.state = self.dig_sand
|
||||
elif self._navigation_stalled():
|
||||
print('Navigation stalled to sand {}; trying another opening'.format(
|
||||
self.sand))
|
||||
self.g.path = []
|
||||
self.g.look_at = None
|
||||
self._clear_navigation()
|
||||
self.tried_openings.add(self.opening)
|
||||
self.state = self.nav_to_sand
|
||||
|
||||
def _set_break_deadline(self):
|
||||
game_deadline = getattr(self.g.game, 'break_deadline', None)
|
||||
self.break_deadline = (
|
||||
game_deadline + 5.0 if game_deadline is not None else
|
||||
time.monotonic() + self.BREAK_CONFIRM_TIMEOUT)
|
||||
|
||||
def dig_sand(self):
|
||||
if not self.g.breaking:
|
||||
if self.bsand(self.sand):
|
||||
self.g.look_at = self.sand
|
||||
self.g.game.break_block(self.sand)
|
||||
print('digging sand')
|
||||
else:
|
||||
self.state = self.find_new_sand
|
||||
if self.g.world.block_at(*self.sand) != blocks.SAND:
|
||||
self.g.look_at = None
|
||||
self.state = self.find_new_sand
|
||||
return
|
||||
|
||||
position = self.g.pos
|
||||
eye = (position.x, position.y + 1.62, position.z)
|
||||
hit_point = tuple(value + 0.5 for value in self.sand)
|
||||
if (utils.phyp(eye, hit_point) > self.BREAK_REACH or
|
||||
not self.g.world._has_cache_line_of_sight(
|
||||
eye, hit_point, self.sand)):
|
||||
self.tried_openings.add(self.opening)
|
||||
self.state = self.nav_to_sand
|
||||
return
|
||||
|
||||
breaking = self.g.get('breaking')
|
||||
if breaking:
|
||||
if tuple(breaking) == self.sand:
|
||||
self._set_break_deadline()
|
||||
self.state = self.wait_break
|
||||
return
|
||||
|
||||
self.g.look_at = self.sand
|
||||
if self.g.game.break_block(self.sand):
|
||||
self._set_break_deadline()
|
||||
self.state = self.wait_break
|
||||
else:
|
||||
print('Break request rejected; skipping', self.sand)
|
||||
self.bad_sand.add(self.sand)
|
||||
self.state = self.find_new_sand
|
||||
|
||||
def wait_break(self):
|
||||
if self.break_deadline is None:
|
||||
self._set_break_deadline()
|
||||
if self.g.world.block_at(*self.sand) != blocks.SAND:
|
||||
print('Sand break confirmed at', self.sand)
|
||||
self.g.look_at = None
|
||||
self.state = self.find_new_sand
|
||||
elif time.monotonic() > self.break_deadline:
|
||||
print('Sand did not break; skipping', self.sand)
|
||||
if self.g.get('breaking') == self.sand:
|
||||
self.g.game.cancel_break()
|
||||
self.g.look_at = None
|
||||
self.bad_sand.add(self.sand)
|
||||
self.state = self.find_new_sand
|
||||
|
||||
def cleanup(self):
|
||||
self.g.path = []
|
||||
self.g.look_at = None
|
||||
self._clear_navigation()
|
||||
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.slice = None
|
||||
self.sand = None
|
||||
self.sand_slice = None
|
||||
self.bad_slices = []
|
||||
self.prev_layer = 0
|
||||
self.sand = None
|
||||
self.bad_sand = []
|
||||
self.wait_time = 0
|
||||
self.bad_sand = set()
|
||||
self.tried_openings = set()
|
||||
self.opening = None
|
||||
self.break_deadline = None
|
||||
self.navigation_path_head = None
|
||||
self.navigation_best_distance = None
|
||||
self.navigation_last_progress_at = None
|
||||
|
||||
def run(self):
|
||||
self.state()
|
||||
+84
-52
@@ -1,89 +1,121 @@
|
||||
import re
|
||||
"""Pick up dropped sand using 26.3 entity and navigation state."""
|
||||
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 mcdata
|
||||
from mosfet.info import mobs
|
||||
|
||||
|
||||
class GrabSandStates:
|
||||
PICKUP_DISTANCE = 1.5
|
||||
PICKUP_TIMEOUT = 5.0
|
||||
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
def init(self):
|
||||
self.eid_blacklist.clear()
|
||||
self.state = self.find_sand
|
||||
print('Looking for dropped sand')
|
||||
|
||||
def _object(self, entity_id):
|
||||
obj = self.g.get('objects', {}).get(entity_id)
|
||||
if obj is None or obj.get('item_id') != items.SAND_ID:
|
||||
return None
|
||||
return obj
|
||||
|
||||
def _skip_target(self):
|
||||
if self.entity_id is not None:
|
||||
self.eid_blacklist.add(self.entity_id)
|
||||
self.g.path = []
|
||||
self.g.look_at = None
|
||||
self.state = self.find_sand
|
||||
print('Trying to grab sand')
|
||||
|
||||
def find_sand(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
world = self.g.world
|
||||
position = utils.pint(self.g.pos)
|
||||
candidates = [
|
||||
(entity_id, obj) for entity_id, obj in
|
||||
self.g.get('objects', {}).items()
|
||||
if obj.get('item_id') == items.SAND_ID and
|
||||
entity_id not in self.eid_blacklist
|
||||
]
|
||||
candidates.sort(key=lambda pair: utils.phyp(
|
||||
position, (pair[1].x, pair[1].y, pair[1].z)))
|
||||
|
||||
sand = w.find_objects([items.SAND_ID])
|
||||
for entity_id, obj in candidates:
|
||||
item_position = (obj.x, obj.y, obj.z)
|
||||
if utils.phyp(position, item_position) > 6:
|
||||
continue
|
||||
item_block = utils.pint(item_position)
|
||||
support = world.block_at(*utils.padd(item_block, path.BLOCK_BELOW))
|
||||
if support is None or support in blocks.NON_SOLID_IDS:
|
||||
continue
|
||||
|
||||
if not sand:
|
||||
print('No sand objects found, aborting')
|
||||
self.state = self.cleanup
|
||||
self.entity_id = entity_id
|
||||
self.sand = item_block
|
||||
if utils.phyp(tuple(self.g.pos), item_position) <= self.PICKUP_DISTANCE:
|
||||
self.pickup_deadline = time.monotonic() + self.PICKUP_TIMEOUT
|
||||
self.state = self.wait_for_pickup
|
||||
return
|
||||
|
||||
navpath = world.path_to_place(position, item_block)
|
||||
if not navpath:
|
||||
self.eid_blacklist.add(entity_id)
|
||||
continue
|
||||
|
||||
self.entity_id = entity_id
|
||||
self.sand = item_block
|
||||
self.g.path = navpath
|
||||
self.g.look_at = item_block
|
||||
print('Going to dropped sand', self.sand)
|
||||
self.state = self.going_to_sand
|
||||
return
|
||||
|
||||
sand.sort(key=lambda s: utils.phyp(p, (s.x, s.y, s.z)))
|
||||
|
||||
for s in sand:
|
||||
s_pos = utils.pint((s.x, s.y, s.z))
|
||||
check = utils.padd(s_pos, path.BLOCK_BELOW)
|
||||
|
||||
if utils.phyp(p, s_pos) > 6:
|
||||
continue
|
||||
# skip if the sand is floating
|
||||
if self.g.chunks.get_block_at(*check) in {0}:
|
||||
continue
|
||||
if s.entity_id in self.eid_blacklist:
|
||||
continue
|
||||
|
||||
self.eid_blacklist.append(s.entity_id)
|
||||
|
||||
navpath = w.path_to_place(p, s_pos)
|
||||
|
||||
if navpath:
|
||||
self.g.path = navpath
|
||||
self.state = self.going_to_sand
|
||||
self.sand = s_pos
|
||||
print('Going to sand', self.sand)
|
||||
return
|
||||
else:
|
||||
print('Cant get to sand', self.sand)
|
||||
time.sleep(0.1)
|
||||
|
||||
print('Cant get to any more sand, aborting')
|
||||
print('No reachable dropped sand nearby')
|
||||
self.state = self.cleanup
|
||||
|
||||
def going_to_sand(self):
|
||||
if utils.pint(self.g.pos) == self.sand:
|
||||
self.state = self.cleanup
|
||||
obj = self._object(self.entity_id)
|
||||
if obj is None:
|
||||
self.g.path = []
|
||||
self.state = self.find_sand
|
||||
return
|
||||
|
||||
item_position = (obj.x, obj.y, obj.z)
|
||||
distance = utils.phyp(tuple(self.g.pos), item_position)
|
||||
if distance <= self.PICKUP_DISTANCE:
|
||||
self.g.path = []
|
||||
self.pickup_deadline = time.monotonic() + self.PICKUP_TIMEOUT
|
||||
self.state = self.wait_for_pickup
|
||||
elif not self.g.path:
|
||||
self._skip_target()
|
||||
|
||||
def wait_for_pickup(self):
|
||||
if self._object(self.entity_id) is None:
|
||||
print('Picked up dropped sand')
|
||||
self.g.look_at = None
|
||||
self.state = self.find_sand
|
||||
elif time.monotonic() > self.pickup_deadline:
|
||||
print('Could not pick up dropped sand; skipping', self.sand)
|
||||
self._skip_target()
|
||||
|
||||
def cleanup(self):
|
||||
self.g.path = []
|
||||
self.g.look_at = None
|
||||
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.sand = None
|
||||
self.eid_blacklist = []
|
||||
self.entity_id = None
|
||||
self.eid_blacklist = set()
|
||||
self.pickup_deadline = None
|
||||
|
||||
def run(self):
|
||||
self.state()
|
||||
Reference in new issue
Block a user