From 2b21b60247cacf3441feeea3ebd2f43e4b115448 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 16 Dec 2020 06:14:04 +0000 Subject: [PATCH] Add a job for farming crops --- blocks.py | 24 ++++++++++ game.py | 3 ++ items.py | 7 ++- jobs.py | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 166 insertions(+), 2 deletions(-) diff --git a/blocks.py b/blocks.py index 52c631c..39183bc 100644 --- a/blocks.py +++ b/blocks.py @@ -284,6 +284,26 @@ for block_name in ['nether_wart']: for state in JSON_BLOCKS['minecraft:' + block_name]['states']: NETHERWART_IDS.add(state['id']) +WHEAT_IDS = set() +for block_name in ['wheat']: + for state in JSON_BLOCKS['minecraft:' + block_name]['states']: + WHEAT_IDS.add(state['id']) + +POTATO_IDS = set() +for block_name in ['potatoes']: + for state in JSON_BLOCKS['minecraft:' + block_name]['states']: + POTATO_IDS.add(state['id']) + +CARROT_IDS = set() +for block_name in ['carrots']: + for state in JSON_BLOCKS['minecraft:' + block_name]['states']: + CARROT_IDS.add(state['id']) + +BEETROOT_IDS = set() +for block_name in ['beetroots']: + for state in JSON_BLOCKS['minecraft:' + block_name]['states']: + BEETROOT_IDS.add(state['id']) + INDEXED_IDS = set() for block_name in INDEXED: for state in JSON_BLOCKS['minecraft:' + block_name]['states']: @@ -294,6 +314,10 @@ for block_name in SAPLINGS: for state in JSON_BLOCKS['minecraft:' + block_name]['states']: SAPLING_IDS.add(state['id']) +MATURE_WHEAT_ID = max(WHEAT_IDS) +MATURE_POTATO_ID = max(POTATO_IDS) +MATURE_CARROT_ID = max(CARROT_IDS) +MATURE_BEETROOT_ID = max(BEETROOT_IDS) def get(bid): name = BLOCKS[bid] diff --git a/game.py b/game.py index 90cdad4..e6ddcdc 100644 --- a/game.py +++ b/game.py @@ -644,6 +644,9 @@ class Game: elif data == 'wart': self.g.job.state = self.g.job.farm_wart reply = 'ok' + elif data.startswith('crop'): + self.g.job.state = self.g.job.farm_crop + reply = 'ok' if reply: for i in self.g.inv.values(): diff --git a/items.py b/items.py index d0e4a42..89f58e0 100644 --- a/items.py +++ b/items.py @@ -64,5 +64,10 @@ GAPPLE_ID = get_id('enchanted_golden_apple') SAND_ID = get_id('sand') NETHERWART_ID = get_id('nether_wart') +CARROT_ID = get_id('carrot') +POTATO_ID = get_id('potato') +WHEAT_SEEDS_ID = get_id('wheat_seeds') +BEETROOT_SEEDS_ID = get_id('beetroot_seeds') + NEEDED_ITEMS = BED_IDS | set([CHEST_ID]) -WANTED_ITEMS = SAPLING_IDS | set([NETHERWART_ID]) +WANTED_ITEMS = SAPLING_IDS | set([NETHERWART_ID, CARROT_ID, POTATO_ID, WHEAT_SEEDS_ID, BEETROOT_SEEDS_ID]) diff --git a/jobs.py b/jobs.py index b83a519..f1fb3b4 100644 --- a/jobs.py +++ b/jobs.py @@ -124,6 +124,128 @@ class FindGappleStates: self.state() +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, + ] + + for crop in w.find_blocks_3d(p, mature_crops, 50, 20): + print('Found crop:', crop) + if crop not in self.bad_crops: + break + else: # for + print('No good crops left, aborting.') + self.state = self.cleanup + return + + 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: + self.g.path = navpath + self.g.look_at = utils.padd(self.crop, path.BLOCK_BELOW) + self.state = self.going_to_crop + else: + self.bad_crops.append(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) + self.wait_time = 0.5 + self.state = self.wait + + def wait(self): + # wait for the item + if self.wait_time > 0: + self.wait_time -= utils.TICK + else: + self.state = self.select_seed + + 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, + } + + if self.g.game.select_item([crop_seeds[self.type_id]]): + self.state = self.wait_select + self.wait_time = 0.5 + else: + print('Aborting planting, no crop') + self.state = self.cleanup + + def wait_select(self): + # wait a bit to select + if self.wait_time > 0: + self.wait_time -= utils.TICK + 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 + self.wait_time = 0.5 + + 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.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 + self.bad_crops = [] + self.wait_time = 0 + + def run(self): + self.state() + + class GatherWartStates: def idle(self): return None @@ -1444,6 +1566,7 @@ class JobStates: self.fill_blocks_states = FillBlocksStates(self.g) self.check_threats_states = CheckThreatsStates(self.g) self.gather_wart_states = GatherWartStates(self.g) + self.gather_crop_states = GatherCropStates(self.g) def run_machines(self, machines): for m in machines: @@ -1456,7 +1579,6 @@ class JobStates: for m in machines: m.state = m.init - def gather_sand(self): machines = [ self.gather_sand_states, @@ -1522,6 +1644,16 @@ class JobStates: self.cache_items_states.silent = True return machines + def farm_crop(self): + machines = [ + self.gather_crop_states, + self.sleep_with_bed_states, + self.cache_items_states, + ] + self.sleep_with_bed_states.silent = True + self.cache_items_states.silent = True + return machines + def fill_blocks(self): machines = [ self.fill_blocks_states,