Find trees using new 3D search

This commit is contained in:
Tanner Collin 2020-10-19 15:49:14 -06:00
parent 90388cf110
commit ee2a1958f9
3 changed files with 48 additions and 24 deletions

35
game.py
View File

@ -62,13 +62,8 @@ class MCWorld:
return result return result
def find_trees(self, center, distance): def find_trees(self, center, distance):
logs = [] found_trees = []
for i in range(5): for log in self.find_blocks_3d(center, blocks.LOG_IDS, distance, 15):
check = utils.padd(center, utils.alternate(i, 3))
logs.extend(self.find_blocks(check, distance, blocks.LOG_IDS, 50))
trees = []
for log in logs:
# crawl to the bottom log # crawl to the bottom log
while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS: while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS:
log = utils.padd(log, path.BLOCK_BELOW) log = utils.padd(log, path.BLOCK_BELOW)
@ -84,14 +79,18 @@ class MCWorld:
log_count += 1 log_count += 1
# make sure it's a good tree # 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: 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 # crawl back to the bottom log
while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS: while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS:
log = utils.padd(log, path.BLOCK_BELOW) log = utils.padd(log, path.BLOCK_BELOW)
trees.append(log)
trees.sort(key=lambda x: utils.phyp(center, x)) if log in found_trees:
return trees continue
found_trees.append(log)
yield log
def find_tree_openings(self, tree): def find_tree_openings(self, tree):
# returns coords in a cardinal direction where we can stand by 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 confirm_packet.teleport_id = packet.teleport_id
self.g.connection.write_packet(confirm_packet) 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): def handle_chat(self, message):
source, text = message source, text = message
@ -679,3 +689,6 @@ class Game:
else: else:
self.g.dumping = None self.g.dumping = None
if not len(self.g.path):
self.g.correction_count = 0

30
jobs.py
View File

@ -140,18 +140,20 @@ class GatherWoodStates:
w = self.g.world w = self.g.world
p = utils.pint(self.g.pos) p = utils.pint(self.g.pos)
trees = w.find_trees(p, 100) for tree in w.find_trees(p, 100):
print('Found trees:', trees) print('Found tree:', tree)
if tree not in self.bad_trees:
try: break
while trees[0] in self.bad_trees: else: # for
trees.pop(0) print('No good trees left, aborting.')
self.tree = trees[0]
except IndexError:
print('No good tress left, aborting.')
self.state = self.cleanup self.state = self.cleanup
return 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.openings = w.find_tree_openings(self.tree)
self.state = self.choose_opening self.state = self.choose_opening
@ -251,7 +253,15 @@ class GatherWoodStates:
else: else:
self.g.chopped_tree = True self.g.chopped_tree = True
self.good_trees.append(self.tree) 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 self.state = self.cleanup
else:
self.state = self.find_openings
def cleanup(self): def cleanup(self):
self.g.look_at = None self.g.look_at = None
@ -820,7 +830,7 @@ class GrabSaplingStates:
saplings = w.find_objects(items.SAPLING_IDS) saplings = w.find_objects(items.SAPLING_IDS)
if not saplings: if not saplings:
print('No saplings objects found, aborting') print('No sapling objects found, aborting')
self.state = self.cleanup self.state = self.cleanup
return return

View File

@ -22,6 +22,7 @@ g.inv = {}
g.objects = {} g.objects = {}
g.window = None g.window = None
g.job = None g.job = None
g.correction_count = 0
@app.route('/') @app.route('/')
def hello_world(): def hello_world():