255 lines
8.1 KiB
Python
255 lines
8.1 KiB
Python
"""Gather naturally generated trees using the active 26.3 world/game APIs."""
|
|
import time
|
|
|
|
from mosfet import utils
|
|
from mosfet import path
|
|
from mosfet.info import blocks
|
|
from mosfet.info import items
|
|
|
|
|
|
class GatherWoodStates:
|
|
NAVIGATION_STALL_TIMEOUT = 8.0
|
|
NAVIGATION_PROGRESS_EPSILON = 0.15
|
|
|
|
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 _try_next_opening(self, reason):
|
|
target = self.openings[0] if self.openings else self.tree
|
|
print('{} {}; trying another tree opening'.format(reason, target))
|
|
self.g.path = []
|
|
self.g.look_at = None
|
|
self._clear_navigation()
|
|
if self.openings:
|
|
self.openings.pop(0)
|
|
self.state = self.choose_opening
|
|
|
|
def bair(self, p):
|
|
return self.g.chunks.get_block_at(*p) in blocks.NON_SOLID_IDS
|
|
|
|
def blog(self, p):
|
|
return self.g.chunks.get_block_at(*p) in blocks.LOG_IDS
|
|
|
|
def idle(self):
|
|
return None
|
|
|
|
def init(self):
|
|
self.g.chopped_tree = False
|
|
self.state = self.select_axe
|
|
|
|
def select_axe(self):
|
|
self.g.game.select_item(items.AXE_IDS)
|
|
self.state = self.find_new_tree
|
|
|
|
def find_new_tree(self):
|
|
print('Finding new tree...')
|
|
w = self.g.world
|
|
p = utils.pint(self.g.pos)
|
|
|
|
trees = w.filter_work_positions(w.find_trees(p, 100))
|
|
for tree in trees:
|
|
if tree in self.bad_trees:
|
|
continue
|
|
print('Found tree:', tree)
|
|
tree_type = blocks.BLOCKS[w.block_at(*tree)].replace('_log', '')
|
|
if items.get_sapling_id(tree_type) is None:
|
|
print('No plantable sapling for', tree_type, '- skipping')
|
|
self.bad_trees.append(tree)
|
|
continue
|
|
break
|
|
else: # for
|
|
print('No good trees left, aborting.')
|
|
self.state = self.cleanup
|
|
return
|
|
|
|
self.tree = tree
|
|
self.type = tree_type
|
|
print('Type:', self.type)
|
|
|
|
self.state = self.find_openings
|
|
|
|
def find_openings(self):
|
|
w = self.g.world
|
|
self.openings = [
|
|
opening for opening in w.find_tree_openings(self.tree)
|
|
if w.work_position_allowed(opening)
|
|
]
|
|
self.state = self.choose_opening
|
|
|
|
def choose_opening(self):
|
|
w = self.g.world
|
|
p = utils.pint(self.g.pos)
|
|
|
|
print('openings:', self.openings)
|
|
|
|
if not len(self.openings):
|
|
print('Unable to get to tree', self.tree)
|
|
if self.tree not in self.good_trees:
|
|
self.bad_trees.append(self.tree)
|
|
print('Added to bad trees list')
|
|
self.state = self.cleanup
|
|
return
|
|
|
|
navpath = w.path_to_place(p, self.openings[0])
|
|
|
|
if navpath:
|
|
self.g.path = navpath
|
|
self._begin_navigation()
|
|
self.state = self.going_to_tree
|
|
else:
|
|
self.openings.pop(0)
|
|
time.sleep(0.1)
|
|
|
|
def going_to_tree(self):
|
|
if utils.pint(self.g.pos) == self.openings[0]:
|
|
self._clear_navigation()
|
|
self.g.look_at = self.tree
|
|
self.state = self.clear_leaves
|
|
elif self._navigation_stalled():
|
|
self._try_next_opening('No path progress to tree opening')
|
|
|
|
def clear_leaves(self):
|
|
if not self.g.breaking:
|
|
p = utils.pint(self.g.pos)
|
|
diff = utils.psub(self.tree, p)
|
|
|
|
for x in utils.diffrange(diff[0]):
|
|
for z in utils.diffrange(diff[2]):
|
|
for y in range(2):
|
|
check = utils.padd(p, (x, y, z))
|
|
if check == self.tree:
|
|
break
|
|
if not self.bair(check):
|
|
print('Breaking leaf')
|
|
self.g.game.break_block(check)
|
|
return
|
|
|
|
self.state = self.clear_trunk_base
|
|
|
|
def clear_trunk_base(self):
|
|
if not self.g.breaking:
|
|
base = self.tree
|
|
above = utils.padd(self.tree, path.BLOCK_ABOVE)
|
|
|
|
if self.blog(base):
|
|
self.g.game.break_block(base)
|
|
print('breaking base')
|
|
elif self.blog(above):
|
|
self.g.game.break_block(above)
|
|
print('breaking above')
|
|
else:
|
|
w = self.g.world
|
|
p = utils.pint(self.g.pos)
|
|
navpath = w.path_to_place(p, self.tree)
|
|
|
|
if navpath:
|
|
self.g.path = navpath
|
|
self._begin_navigation()
|
|
self.state = self.going_to_trunk_base
|
|
else:
|
|
self.openings.pop(0)
|
|
self.state = self.choose_opening
|
|
time.sleep(0.1)
|
|
|
|
def going_to_trunk_base(self):
|
|
if utils.pint(self.g.pos) == self.tree:
|
|
self._clear_navigation()
|
|
self.g.look_at = utils.padd(self.tree, path.BLOCK_ABOVE2)
|
|
self.state = self.clear_trunk
|
|
elif self._navigation_stalled():
|
|
self._try_next_opening('No path progress to trunk base')
|
|
|
|
def clear_trunk(self):
|
|
if not self.g.breaking:
|
|
check = self.tree
|
|
|
|
count = 0
|
|
while self.bair(check) and count < 6:
|
|
check = utils.padd(check, path.BLOCK_ABOVE)
|
|
count += 1
|
|
|
|
if self.blog(check):
|
|
print('breaking log', check)
|
|
self.g.game.break_block(check)
|
|
else:
|
|
print('Finished clearing tree')
|
|
self.wait_time = 0.5
|
|
self.state = self.wait
|
|
|
|
def wait(self):
|
|
# wait for the last log to fall
|
|
if self.wait_time > 0:
|
|
self.wait_time -= utils.TICK
|
|
else:
|
|
self.g.chopped_tree = self.type
|
|
self.good_trees.append(self.tree)
|
|
self.state = self.check_pos
|
|
|
|
def check_pos(self):
|
|
# make sure we are at base of trunk
|
|
# doesn't always happen, for some reason
|
|
if utils.pint(self.g.pos) == self.tree:
|
|
self.state = self.cleanup
|
|
else:
|
|
self.state = self.find_openings
|
|
|
|
def cleanup(self):
|
|
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.tree = None
|
|
self.type = None
|
|
self.openings = []
|
|
self.bad_trees = []
|
|
self.good_trees = []
|
|
self.wait_time = 0
|
|
self.navigation_path_head = None
|
|
self.navigation_best_distance = None
|
|
self.navigation_last_progress_at = None
|
|
|
|
def run(self):
|
|
self.state()
|