Program the bot to crawl, fix wood farming bugs

This commit is contained in:
Tanner Collin 2021-03-12 06:04:04 +00:00
parent e642e426b9
commit 2fa3044acb
3 changed files with 57 additions and 33 deletions

3
bot.py
View File

@ -142,9 +142,11 @@ def tick(global_state):
g.y_v += g.y_a * utils.TICK 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_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_void = p.y < 0
in_air = block_below in blocks.NON_SOLID_IDS or in_void in_air = block_below in blocks.NON_SOLID_IDS or in_void
in_water = block_below in blocks.WATER_IDS in_water = block_below in blocks.WATER_IDS
g.crawling = block_above not in blocks.NON_SOLID_IDS
if in_air: if in_air:
g.y_a = -36.0 g.y_a = -36.0
@ -204,6 +206,7 @@ def init(global_state):
g.y_a = 0 g.y_a = 0
g.yaw = 360 g.yaw = 360
g.pitch = 0 g.pitch = 0
g.crawling = False
g.breaking = None g.breaking = None
g.break_time = 0 g.break_time = 0

16
game.py
View File

@ -117,9 +117,7 @@ class MCWorld:
log_count += 1 log_count += 1
for offset in path.CHECK_DIRECTIONS: for offset in path.CHECK_DIRECTIONS:
if self.block_at(*utils.padd(log, offset)) not in blocks.LEAF_IDS: if self.block_at(*utils.padd(log, offset)) in blocks.LEAF_IDS:
break
else: # for:
good_leaves = True good_leaves = True
# make sure it's a good tree # make sure it's a good tree
@ -132,7 +130,7 @@ class MCWorld:
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
maze_solver = path.Pathfinder(self.g.chunks) maze_solver = path.Pathfinder(self.g)
result = [] result = []
# TODO: make sure only non-solid and leaves between # TODO: make sure only non-solid and leaves between
@ -146,7 +144,7 @@ class MCWorld:
return result return result
def path_to_place(self, start, place): def path_to_place(self, start, place):
maze_solver = path.Pathfinder(self.g.chunks) maze_solver = path.Pathfinder(self.g)
try: try:
s = maze_solver.astar(start, place) s = maze_solver.astar(start, place)
@ -155,8 +153,8 @@ class MCWorld:
return None return None
def find_bed_areas(self, center, distance): def find_bed_areas(self, center, distance):
bed_clearance = 25 # 5x5 area bed_clearance = 9 # 5x5 area
clear_distance = 3 clear_distance = 2
for a in self.find_blocks_3d(center, [0], distance, 50): for a in self.find_blocks_3d(center, [0], distance, 50):
# check for air around the area # check for air around the area
@ -324,7 +322,7 @@ class MCWorld:
def find_villager_openings(self, villager): def find_villager_openings(self, villager):
# returns coords in a cardinal direction where we can stand by a 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 = [] result = []
for distance in range(3): for distance in range(3):
@ -398,7 +396,7 @@ class Game:
print('new waypoint:', self.g.goal) print('new waypoint:', self.g.goal)
start = time.time() 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: if solution:
solution = list(solution) solution = list(solution)
self.g.path = solution self.g.path = solution

27
path.py
View File

@ -121,8 +121,9 @@ HALF_PARKOUR = {
BLOCK_CACHE_SIZE = 2**14 BLOCK_CACHE_SIZE = 2**14
class Pathfinder(AStar): class Pathfinder(AStar):
def __init__(self, chunks): def __init__(self, g):
self.chunks = chunks self.g = g
self.chunks = g.chunks
self.start_time = time.time() self.start_time = time.time()
@functools.lru_cache(maxsize=BLOCK_CACHE_SIZE) @functools.lru_cache(maxsize=BLOCK_CACHE_SIZE)
@ -133,6 +134,23 @@ class Pathfinder(AStar):
def bavoid(self, p): def bavoid(self, p):
return self.chunks.get_block_at(*p) in blocks.AVOID_IDS or p[1] < 0 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): def check_traverse(self, node, offset):
dest = utils.padd(node, offset) dest = utils.padd(node, offset)
@ -254,6 +272,11 @@ class Pathfinder(AStar):
def neighbors(self, node): def neighbors(self, node):
results = [] results = []
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: for offset in TRAVERSE:
if self.check_traverse(node, offset): if self.check_traverse(node, offset):
results.append(utils.padd(node, offset)) results.append(utils.padd(node, offset))