Compare commits

...

2 Commits

Author SHA1 Message Date
bd8f8a0d55 Move beside planted sapling 2020-10-23 17:45:35 -06:00
ee2a1958f9 Find trees using new 3D search 2020-10-19 15:49:14 -06:00
3 changed files with 80 additions and 65 deletions

41
game.py
View File

@ -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
trees.sort(key=lambda x: utils.phyp(center, x))
return trees
# 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)
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

103
jobs.py
View File

@ -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
@ -423,7 +433,7 @@ class SleepWithBedStates:
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
self.state = self.place_bed
else:
self.g.chat.send('I need a bed')
print('No bed, aborting.')
self.state = self.cleanup
def place_bed(self):
@ -539,7 +549,7 @@ class CacheItemsStates:
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
self.state = self.place_chest
else:
self.g.chat.send('I need a chest')
print('No chest, aborting')
self.state = self.cleanup
def place_chest(self):
@ -606,7 +616,8 @@ class CacheItemsStates:
def close_chest(self):
print('closing chest')
self.g.game.close_window()
self.g.chat.send('cache at ' + str(self.area)[1:-1])
if not self.silent:
self.g.chat.send('cache at ' + str(self.area)[1:-1])
self.state = self.cleanup
def cleanup(self):
@ -621,6 +632,8 @@ class CacheItemsStates:
self.g = global_state
self.state = self.idle
self.silent = False
# keep all needed items
self.needed_items = items.NEEDED_ITEMS
# keep one stack of wanted items
@ -681,47 +694,35 @@ class PlantTreeStates:
p = utils.pint(self.g.pos)
self.g.game.place_block(p, BlockFace.TOP)
print('Placed sapling')
self.state = self.cleanup
# self.state = self.wait_place
# self.wait_time = 1
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 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):
# print('Finding an open spot to stand...')
# w = self.g.world
# p = utils.pint(self.g.pos)
def find_open_spot(self):
w = self.g.world
p = utils.pint(self.g.pos)
# for area in w.find_cache_areas(p, 20):
# print('Found area:', area)
# if area not in self.bad_areas:
# break
# else: # for
# print('Unable to find area')
# self.state = self.cleanup
# return
for opening in w.find_tree_openings(p):
print('trying sapling opening', opening)
navpath = w.path_to_place(p, opening)
if navpath:
self.g.path = navpath
self.area = opening
self.state = self.going_to_area
return
else: # for
print('cant escape sapling')
self.state = self.cleanup
# self.area = area
# navpath = w.path_to_place(p, self.area)
# if not navpath:
# print('Unable to get to open area', self.area)
# self.bad_areas.append(self.area)
# self.state = self.cleanup
# return
# self.g.path = navpath
# self.state = self.going_to_area
# print('Going to area', self.area)
#def going_to_area(self):
# if utils.pint(self.g.pos) == self.area:
# self.state = self.cleanup
def going_to_area(self):
if utils.pint(self.g.pos) == self.area:
self.state = self.cleanup
def cleanup(self):
self.g.look_at = None
@ -737,7 +738,6 @@ class PlantTreeStates:
self.wait_time = 0
self.area = None
self.bad_areas = []
def run(self):
self.state()
@ -820,7 +820,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
@ -950,6 +950,7 @@ class JobStates:
def farm_wood(self):
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
s1 = self.gather_wood_states
s2 = self.plant_tree_states

View File

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