diff --git a/mosfet/jobs/gather_crop.py b/mosfet/jobs/gather_crop.py index 8813540..b07cd14 100644 --- a/mosfet/jobs/gather_crop.py +++ b/mosfet/jobs/gather_crop.py @@ -108,12 +108,12 @@ class GatherCropStates: w = self.g.world p = utils.pint(self.g.pos) - mature_crops = [ + mature_crops = { blocks.MATURE_WHEAT_ID, blocks.MATURE_POTATO_ID, blocks.MATURE_CARROT_ID, blocks.MATURE_BEETROOT_ID, - ] + } candidates = w.filter_work_positions( w.find_blocks_indexed(p, mature_crops, 50)) @@ -137,11 +137,17 @@ class GatherCropStates: mature_count, total_count = self.active_section_maturity maturity_percent = (100 * mature_count / total_count if total_count else 0) + crop_state = w.block_at(*crop) + if crop_state not in mature_crops: + print('Crop changed while selecting; refreshing the crop list') + self.state = self.find_new_crop + return + print('Found crop:', crop, '(same-species section: {}/{} mature, {:.0f}%)'.format( mature_count, total_count, maturity_percent)) self.crop = crop - self.type_id = w.block_at(*crop) + self.type_id = crop_state self.state = self.nav_to_crop def nav_to_crop(self): @@ -167,8 +173,20 @@ class GatherCropStates: self.state = self.break_crop def break_crop(self): + current_state = self.g.world.block_at(*self.crop) + mature_crops = { + blocks.MATURE_WHEAT_ID, + blocks.MATURE_POTATO_ID, + blocks.MATURE_CARROT_ID, + blocks.MATURE_BEETROOT_ID, + } + if current_state != self.type_id or current_state not in mature_crops: + print('Crop changed before break; selecting another crop:', + self.crop, current_state) + self.state = self.find_new_crop + return print('Requesting crop break at {}: block={}, mode={}, held={}'.format( - self.crop, self.g.world.block_at(*self.crop), + self.crop, current_state, self.g.get('game_mode'), self.g.get('holding'))) if self.g.game.break_block(self.crop): print('Started breaking crop {} (state {})'.format( @@ -197,15 +215,20 @@ class GatherCropStates: self.state = self.find_new_crop 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, + 'wheat': items.WHEAT_SEEDS_ID, + 'potatoes': items.POTATO_ID, + 'carrots': items.CARROT_ID, + 'beetroots': items.BEETROOT_SEEDS_ID, } - self.target_seed = crop_seeds[self.type_id] + crop_name = blocks.BLOCKS.get(self.type_id) + self.target_seed = crop_seeds.get(crop_name) + if self.target_seed is None: + print('Cannot select a seed for crop state', self.type_id, + crop_name) + self.bad_crops.append(self.crop) + self.state = self.find_new_crop + return if self.g.game.select_item([self.target_seed]): self.seed_deadline = None diff --git a/tests/test_crop_batch.py b/tests/test_crop_batch.py index 9a688a0..b7afb34 100644 --- a/tests/test_crop_batch.py +++ b/tests/test_crop_batch.py @@ -1,7 +1,7 @@ import unittest from munch import Munch -from mosfet.info import blocks +from mosfet.info import blocks, items from mosfet.jobs.gather_crop import GatherCropStates @@ -23,6 +23,78 @@ class CropBatchTests(unittest.TestCase): self.assertEqual(g.look_at, crop) self.assertEqual(g.path[-1], crop) + def test_selection_rechecks_maturity_after_candidate_search(self): + position = (1, 64, 1) + unripe = blocks.MCD_BLOCKS['wheat']['minStateId'] + + class FakeWorld: + @staticmethod + def find_blocks_indexed(_center, block_ids, _distance): + return [position] + + @staticmethod + def filter_work_positions(positions): + return list(positions) + + @staticmethod + def block_at(*_position): + return unripe + + state = GatherCropStates(Munch( + world=FakeWorld(), pos=(0, 64, 0))) + state.find_new_crop() + + self.assertIs(state.state.__func__, state.find_new_crop.__func__) + self.assertIsNone(state.crop) + + def test_break_skips_crop_that_became_unripe_on_the_way(self): + position = (1, 64, 1) + unripe = blocks.MCD_BLOCKS['wheat']['minStateId'] + mature = blocks.MATURE_WHEAT_ID + + class FakeWorld: + @staticmethod + def block_at(*_position): + return unripe + + class FakeGame: + def __init__(self): + self.break_calls = [] + + def break_block(self, target): + self.break_calls.append(target) + return True + + game = FakeGame() + state = GatherCropStates(Munch(world=FakeWorld(), game=game)) + state.crop = position + state.type_id = mature + + state.break_crop() + + self.assertEqual(game.break_calls, []) + self.assertIs(state.state.__func__, state.find_new_crop.__func__) + + def test_seed_selection_uses_crop_species_for_any_age_state(self): + unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId'] + + class FakeGame: + def __init__(self): + self.selected = None + + def select_item(self, item_ids): + self.selected = set(item_ids) + return True + + game = FakeGame() + state = GatherCropStates(Munch(game=game)) + state.type_id = unripe_wheat + + state.select_seed() + + self.assertEqual(state.target_seed, items.WHEAT_SEEDS_ID) + self.assertEqual(game.selected, {items.WHEAT_SEEDS_ID}) + def test_gather_crop_finishes_after_sixteen_successful_replants(self): unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId'] mature_wheat = blocks.MATURE_WHEAT_ID