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 mcdata from mosfet.info import mobs class PlantTreeStates: 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 idle(self): return None def init(self): self.failed_openings = set() if self.g.chopped_tree: self.state = self.check_feet else: print('Aborting planting, did not plant') self.state = self.cleanup def check_feet(self): p = utils.pint(self.g.pos) # check for air at feet if self.g.chunks.get_block_at(*p) in [0]: self.state = self.select_sapling else: print('Aborting planting, feet not air') self.state = self.cleanup def select_sapling(self): p = utils.pint(self.g.pos) sapling_item = items.get_sapling_id(self.g.chopped_tree) if sapling_item is None: print('Aborting planting, no sapling for', self.g.chopped_tree) self.state = self.cleanup return sapling_type = items.get_name(sapling_item) if self.g.game.select_item([sapling_item]): self.g.look_at = utils.padd(p, path.BLOCK_BELOW) self.state = self.wait_select self.wait_time = 1 else: print('Aborting planting, no', sapling_type) self.state = self.cleanup def wait_select(self): # wait a bit to look down if self.wait_time > 0: self.wait_time -= utils.TICK else: self.state = self.place_sapling def place_sapling(self): p = utils.pint(self.g.pos) self.g.game.place_block(p, BlockFace.TOP) print('Placed sapling') self.state = self.wait_place self.wait_time = 1 def wait_place(self): # wait a bit for chunk data to update if self.wait_time > 0: self.wait_time -= utils.TICK else: self.state = self.find_open_spot def find_open_spot(self): w = self.g.world p = utils.pint(self.g.pos) for opening in w.find_tree_openings(p)[::-1]: if (opening in self.failed_openings or not w.work_position_allowed(opening)): continue print('trying sapling opening', opening) navpath = w.path_to_place(p, opening) if navpath: self.g.path = navpath self.g.look_at = None self.area = opening self._begin_navigation() self.state = self.going_to_area return else: # for print('cant escape sapling') self.state = self.cleanup def going_to_area(self): if utils.pint(self.g.pos) == self.area: self._clear_navigation() self.state = self.cleanup elif self._navigation_stalled(): print('No path progress leaving planted sapling at {}; trying ' 'another opening'.format(self.area)) self.failed_openings.add(self.area) self.g.path = [] self.g.look_at = None self._clear_navigation() self.state = self.find_open_spot def cleanup(self): self.g.look_at = None self.g.path = [] 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.wait_time = 0 self.area = None self.failed_openings = set() self.navigation_path_head = None self.navigation_best_distance = None self.navigation_last_progress_at = None def run(self): self.state()