minecraft-bot/mosfet/jobs/gather_crop.py

132 lines
3.4 KiB
Python
Raw Permalink 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 GatherCropStates:
def idle(self):
return None
def init(self):
self.state = self.find_new_crop
def find_new_crop(self):
print('Finding new crop...')
w = self.g.world
p = utils.pint(self.g.pos)
mature_crops = [
blocks.MATURE_WHEAT_ID,
blocks.MATURE_POTATO_ID,
blocks.MATURE_CARROT_ID,
blocks.MATURE_BEETROOT_ID,
]
2021-04-23 01:25:44 +00:00
for crop in w.find_blocks_3d(p, mature_crops, 50, 20, True):
2021-04-19 01:34:54 +00:00
if crop not in self.bad_crops:
break
else: # for
print('No good crops left, aborting.')
self.state = self.cleanup
return
2021-04-23 01:25:44 +00:00
print('Found crop:', crop)
2021-04-19 01:34:54 +00:00
self.crop = crop
self.type_id = w.block_at(*crop)
self.state = self.nav_to_crop
def nav_to_crop(self):
w = self.g.world
p = utils.pint(self.g.pos)
navpath = w.path_to_place(p, self.crop)
if navpath:
2021-04-23 01:25:44 +00:00
print('Going to crop', self.crop)
2021-04-19 01:34:54 +00:00
self.g.path = navpath
self.g.look_at = utils.padd(self.crop, path.BLOCK_BELOW)
self.state = self.going_to_crop
else:
2021-04-23 01:25:44 +00:00
print('Cant get to it, blacklisting')
time.sleep(0.1)
2021-04-19 01:34:54 +00:00
self.bad_crops.append(self.crop)
self.state = self.find_new_crop
def going_to_crop(self):
if utils.pint(self.g.pos) == self.crop:
print('At the crop')
self.state = self.break_crop
def break_crop(self):
self.g.game.break_block(self.crop)
2021-06-19 22:17:52 +00:00
self.state = self.select_seed
2021-04-19 01:34:54 +00:00
def select_seed(self):
p = utils.pint(self.g.pos)
crop_seeds = {
blocks.MATURE_WHEAT_ID: items.WHEAT_SEEDS_ID,
blocks.MATURE_POTATO_ID: items.POTATO_ID,
blocks.MATURE_CARROT_ID: items.CARROT_ID,
blocks.MATURE_BEETROOT_ID: items.BEETROOT_SEEDS_ID,
}
2021-06-19 21:30:20 +00:00
self.target_seed = crop_seeds[self.type_id]
2021-04-19 01:34:54 +00:00
2021-06-19 21:30:20 +00:00
if self.g.game.select_item([self.target_seed]):
2021-04-19 01:34:54 +00:00
self.state = self.wait_select
else:
2021-06-19 22:17:52 +00:00
print('Havent picked up seed yet')
return
2021-04-19 01:34:54 +00:00
def wait_select(self):
2021-06-19 21:30:20 +00:00
if self.target_seed != self.g.holding:
return
2021-04-19 01:34:54 +00:00
else:
self.state = self.place_crop
def place_crop(self):
p = utils.pint(self.g.pos)
self.g.game.place_block(p, BlockFace.TOP)
print('Placed crop')
self.state = self.wait_place
def wait_place(self):
2021-06-19 21:30:20 +00:00
w = self.g.world
if w.block_at(*self.crop) == blocks.AIR:
return
2021-04-19 01:34:54 +00:00
else:
self.state = self.cleanup
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.crop = None
self.type_id = None
2021-06-19 21:30:20 +00:00
self.target_seed = None
2021-04-19 01:34:54 +00:00
self.bad_crops = []
def run(self):
self.state()