minecraft-bot/mosfet/jobs/gather_wood.py

192 lines
5.5 KiB
Python
Raw Normal View History

2021-04-19 01:34:54 +00:00
import re
import time
import importlib
import random
from itertools import count
from math import floor
2021-04-19 01:34:54 +00:00
from minecraft.networking.types import BlockFace
2021-04-22 00:46:54 +00:00
from mosfet.protocol.managers import ChunkNotLoadedException
2021-04-19 01:34:54 +00:00
2021-04-22 00:46:54 +00:00
from mosfet import utils
from mosfet import path
from mosfet.info import blocks
from mosfet.info import items
from mosfet.info import mcdata
from mosfet.info import mobs
2021-04-19 01:34:54 +00:00
class GatherWoodStates:
def bair(self, p):
return self.g.chunks.get_block_at(*p) in blocks.NON_SOLID_IDS
def blog(self, p):
return self.g.chunks.get_block_at(*p) in blocks.LOG_IDS
def idle(self):
return None
def init(self):
self.g.chopped_tree = False
self.state = self.select_axe
def select_axe(self):
self.g.game.select_item(items.AXE_IDS)
self.state = self.find_new_tree
def find_new_tree(self):
print('Finding new tree...')
w = self.g.world
p = utils.pint(self.g.pos)
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.type = blocks.BLOCKS[w.block_at(*tree)].replace('_log', '')
print('Type:', self.type)
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
def choose_opening(self):
w = self.g.world
p = utils.pint(self.g.pos)
print('openings:', self.openings)
if not len(self.openings):
print('Unable to get to tree', self.tree)
if self.tree not in self.good_trees:
self.bad_trees.append(self.tree)
print('Added to bad trees list')
self.state = self.cleanup
return
navpath = w.path_to_place(p, self.openings[0])
if navpath:
self.g.path = navpath
self.state = self.going_to_tree
else:
self.openings.pop(0)
time.sleep(0.1)
2021-04-19 01:34:54 +00:00
def going_to_tree(self):
if utils.pint(self.g.pos) == self.openings[0]:
self.g.look_at = self.tree
self.state = self.clear_leaves
def clear_leaves(self):
if not self.g.breaking:
p = utils.pint(self.g.pos)
diff = utils.psub(self.tree, p)
for x in utils.diffrange(diff[0]):
for z in utils.diffrange(diff[2]):
for y in range(2):
check = utils.padd(p, (x, y, z))
if check == self.tree:
break
if not self.bair(check):
print('Breaking leaf')
self.g.game.break_block(check)
return
self.state = self.clear_trunk_base
def clear_trunk_base(self):
if not self.g.breaking:
base = self.tree
above = utils.padd(self.tree, path.BLOCK_ABOVE)
if self.blog(base):
self.g.game.break_block(base)
print('breaking base')
elif self.blog(above):
self.g.game.break_block(above)
print('breaking above')
else:
w = self.g.world
p = utils.pint(self.g.pos)
navpath = w.path_to_place(p, self.tree)
if navpath:
self.g.path = navpath
self.state = self.going_to_trunk_base
else:
self.openings.pop(0)
self.state = self.choose_opening
time.sleep(0.1)
2021-04-19 01:34:54 +00:00
def going_to_trunk_base(self):
if utils.pint(self.g.pos) == self.tree:
self.g.look_at = utils.padd(self.tree, path.BLOCK_ABOVE2)
self.state = self.clear_trunk
def clear_trunk(self):
if not self.g.breaking:
check = self.tree
count = 0
while self.bair(check) and count < 6:
check = utils.padd(check, path.BLOCK_ABOVE)
count += 1
if self.blog(check):
print('breaking log', check)
self.g.game.break_block(check)
else:
print('Finished clearing tree')
self.wait_time = 0.5
self.state = self.wait
def wait(self):
# wait for the last log to fall
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.g.chopped_tree = self.type
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
self.state = self.done
def done(self):
# never gets ran, placeholder
return None
def __init__(self, global_state):
self.g = global_state
self.state = self.idle
self.tree = None
self.type = None
self.openings = []
self.bad_trees = []
self.good_trees = []
self.wait_time = 0
def run(self):
self.state()