From ee2a1958f93fe0e72c942e08d6518b4861313d34 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Mon, 19 Oct 2020 15:49:14 -0600 Subject: [PATCH] Find trees using new 3D search --- game.py | 41 +++++++++++++++++++++++++++-------------- jobs.py | 30 ++++++++++++++++++++---------- main.py | 1 + 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/game.py b/game.py index b776beb..0cdad03 100644 --- a/game.py +++ b/game.py @@ -62,13 +62,8 @@ class MCWorld: return result def find_trees(self, center, distance): - logs = [] - for i in range(5): - check = utils.padd(center, utils.alternate(i, 3)) - logs.extend(self.find_blocks(check, distance, blocks.LOG_IDS, 50)) - - trees = [] - for log in logs: + found_trees = [] + for log in self.find_blocks_3d(center, blocks.LOG_IDS, distance, 15): # crawl to the bottom log while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS: log = utils.padd(log, path.BLOCK_BELOW) @@ -84,14 +79,18 @@ class MCWorld: log_count += 1 # make sure it's a good tree - if self.block_at(*utils.padd(log, path.BLOCK_ABOVE)) in blocks.LEAF_IDS and log_count > 2: - # crawl back to the bottom log - while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS: - log = utils.padd(log, path.BLOCK_BELOW) - trees.append(log) + if self.block_at(*utils.padd(log, path.BLOCK_ABOVE)) not in blocks.LEAF_IDS or log_count < 3: + continue + + # crawl back to the bottom log + while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS: + log = utils.padd(log, path.BLOCK_BELOW) - trees.sort(key=lambda x: utils.phyp(center, x)) - return trees + if log in found_trees: + continue + found_trees.append(log) + + yield log def find_tree_openings(self, tree): # returns coords in a cardinal direction where we can stand by tree @@ -261,6 +260,17 @@ class Game: confirm_packet.teleport_id = packet.teleport_id self.g.connection.write_packet(confirm_packet) + self.g.correction_count += 1 + + if self.g.get('path', None) and self.g.correction_count > 5: + self.g.correction_count = 0 + dest = self.g.path[-1] + w = self.g.world + p = utils.pint(self.g.pos) + new_path = w.path_to_place(p, dest) + + if new_path: + self.g.path = new_path def handle_chat(self, message): source, text = message @@ -679,3 +689,6 @@ class Game: else: self.g.dumping = None + if not len(self.g.path): + self.g.correction_count = 0 + diff --git a/jobs.py b/jobs.py index cd52043..e3c07e9 100644 --- a/jobs.py +++ b/jobs.py @@ -140,18 +140,20 @@ class GatherWoodStates: w = self.g.world p = utils.pint(self.g.pos) - trees = w.find_trees(p, 100) - print('Found trees:', trees) - - try: - while trees[0] in self.bad_trees: - trees.pop(0) - self.tree = trees[0] - except IndexError: - print('No good tress left, aborting.') + for tree in w.find_trees(p, 100): + print('Found tree:', tree) + if tree not in self.bad_trees: + break + else: # for + print('No good trees left, aborting.') self.state = self.cleanup return + self.tree = tree + self.state = self.find_openings + + def find_openings(self): + w = self.g.world self.openings = w.find_tree_openings(self.tree) self.state = self.choose_opening @@ -251,7 +253,15 @@ class GatherWoodStates: else: self.g.chopped_tree = True 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 @@ -820,7 +830,7 @@ class GrabSaplingStates: saplings = w.find_objects(items.SAPLING_IDS) if not saplings: - print('No saplings objects found, aborting') + print('No sapling objects found, aborting') self.state = self.cleanup return diff --git a/main.py b/main.py index af8ebd6..b07af94 100644 --- a/main.py +++ b/main.py @@ -22,6 +22,7 @@ g.inv = {} g.objects = {} g.window = None g.job = None +g.correction_count = 0 @app.route('/') def hello_world():