From 2fa3044acb50f0e9da1cdef7549d6954bba86cee Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Fri, 12 Mar 2021 06:04:04 +0000 Subject: [PATCH] Program the bot to crawl, fix wood farming bugs --- bot.py | 3 +++ game.py | 18 +++++++-------- path.py | 69 ++++++++++++++++++++++++++++++++++++++------------------- 3 files changed, 57 insertions(+), 33 deletions(-) diff --git a/bot.py b/bot.py index c26cf33..06c4a83 100644 --- a/bot.py +++ b/bot.py @@ -142,9 +142,11 @@ def tick(global_state): g.y_v += g.y_a * utils.TICK block_below = g.chunks.get_block_at(floor(p.x), ceil(p.y-1), floor(p.z)) + block_above = g.chunks.get_block_at(floor(p.x), ceil(p.y+1), floor(p.z)) in_void = p.y < 0 in_air = block_below in blocks.NON_SOLID_IDS or in_void in_water = block_below in blocks.WATER_IDS + g.crawling = block_above not in blocks.NON_SOLID_IDS if in_air: g.y_a = -36.0 @@ -204,6 +206,7 @@ def init(global_state): g.y_a = 0 g.yaw = 360 g.pitch = 0 + g.crawling = False g.breaking = None g.break_time = 0 diff --git a/game.py b/game.py index d1d2a22..7aecfc4 100644 --- a/game.py +++ b/game.py @@ -117,10 +117,8 @@ class MCWorld: log_count += 1 for offset in path.CHECK_DIRECTIONS: - if self.block_at(*utils.padd(log, offset)) not in blocks.LEAF_IDS: - break - else: # for: - good_leaves = True + if self.block_at(*utils.padd(log, offset)) in blocks.LEAF_IDS: + good_leaves = True # make sure it's a good tree if not good_leaves or log_count < 3: @@ -132,7 +130,7 @@ class MCWorld: def find_tree_openings(self, tree): # returns coords in a cardinal direction where we can stand by tree - maze_solver = path.Pathfinder(self.g.chunks) + maze_solver = path.Pathfinder(self.g) result = [] # TODO: make sure only non-solid and leaves between @@ -146,7 +144,7 @@ class MCWorld: return result def path_to_place(self, start, place): - maze_solver = path.Pathfinder(self.g.chunks) + maze_solver = path.Pathfinder(self.g) try: s = maze_solver.astar(start, place) @@ -155,8 +153,8 @@ class MCWorld: return None def find_bed_areas(self, center, distance): - bed_clearance = 25 # 5x5 area - clear_distance = 3 + bed_clearance = 9 # 5x5 area + clear_distance = 2 for a in self.find_blocks_3d(center, [0], distance, 50): # check for air around the area @@ -324,7 +322,7 @@ class MCWorld: def find_villager_openings(self, villager): # returns coords in a cardinal direction where we can stand by a villager - maze_solver = path.Pathfinder(self.g.chunks) + maze_solver = path.Pathfinder(self.g) result = [] for distance in range(3): @@ -398,7 +396,7 @@ class Game: print('new waypoint:', self.g.goal) start = time.time() - solution = path.Pathfinder(self.g.chunks).astar(utils.pint(self.g.pos), utils.pint(self.g.goal)) + solution = path.Pathfinder(self.g).astar(utils.pint(self.g.pos), utils.pint(self.g.goal)) if solution: solution = list(solution) self.g.path = solution diff --git a/path.py b/path.py index 33e8021..9513db5 100644 --- a/path.py +++ b/path.py @@ -121,8 +121,9 @@ HALF_PARKOUR = { BLOCK_CACHE_SIZE = 2**14 class Pathfinder(AStar): - def __init__(self, chunks): - self.chunks = chunks + def __init__(self, g): + self.g = g + self.chunks = g.chunks self.start_time = time.time() @functools.lru_cache(maxsize=BLOCK_CACHE_SIZE) @@ -133,6 +134,23 @@ class Pathfinder(AStar): def bavoid(self, p): return self.chunks.get_block_at(*p) in blocks.AVOID_IDS or p[1] < 0 + def check_traverse_crawling(self, node, offset): + dest = utils.padd(node, offset) + + if not self.bair(dest): + return False + + if self.bair(utils.padd(dest, BLOCK_BELOW)): + return False + + if self.bavoid(dest): + return False + + if self.bavoid(utils.padd(dest, BLOCK_BELOW)): + return False + + return True + def check_traverse(self, node, offset): dest = utils.padd(node, offset) @@ -254,27 +272,32 @@ class Pathfinder(AStar): def neighbors(self, node): results = [] - for offset in TRAVERSE: - if self.check_traverse(node, offset): - results.append(utils.padd(node, offset)) - for offset in DIAGONAL: - if self.check_diagonal(node, offset): - results.append(utils.padd(node, offset)) - for offset in ASCEND: - if self.check_ascend(node, offset): - results.append(utils.padd(node, offset)) - for offset in DESCEND: - if self.check_descend(node, offset): - results.append(utils.padd(node, offset)) - for offset in DESCEND2: - if self.check_descend2(node, offset): - results.append(utils.padd(node, offset)) - for offset in DESCEND3: - if self.check_descend3(node, offset): - results.append(utils.padd(node, offset)) - for offset in PARKOUR: - if self.check_parkour(node, offset): - results.append(utils.padd(node, offset)) + if self.g.crawling: + for offset in TRAVERSE: + if self.check_traverse_crawling(node, offset): + results.append(utils.padd(node, offset)) + else: + for offset in TRAVERSE: + if self.check_traverse(node, offset): + results.append(utils.padd(node, offset)) + for offset in DIAGONAL: + if self.check_diagonal(node, offset): + results.append(utils.padd(node, offset)) + for offset in ASCEND: + if self.check_ascend(node, offset): + results.append(utils.padd(node, offset)) + for offset in DESCEND: + if self.check_descend(node, offset): + results.append(utils.padd(node, offset)) + for offset in DESCEND2: + if self.check_descend2(node, offset): + results.append(utils.padd(node, offset)) + for offset in DESCEND3: + if self.check_descend3(node, offset): + results.append(utils.padd(node, offset)) + for offset in PARKOUR: + if self.check_parkour(node, offset): + results.append(utils.padd(node, offset)) if not results: if time.time() - self.start_time > 2.0: