Cache active crop section and streamline harvesting

This commit is contained in:
Tanner committed 2026-11-09 17:37:32 +00:00
1 parent 1b4f32060d
commit 281e90a59f
6 files changed
+356 -22

No files matched your search

+68 -1
View File
@@ -1,4 +1,8 @@
import io
import unittest
from contextlib import redirect_stdout
from unittest.mock import patch
from munch import Munch
from mosfet.info import blocks, items
@@ -23,13 +27,50 @@ class CropBatchTests(unittest.TestCase):
self.assertEqual(g.look_at, crop)
self.assertEqual(g.path[-1], crop)
def test_found_crop_log_includes_search_duration(self):
crop = (1, 64, 1)
mature = blocks.MATURE_WHEAT_ID
class FakeWorld:
@staticmethod
def find_blocks_indexed(_center, _block_ids, _distance, sort=True):
return [crop]
@staticmethod
def filter_work_positions(positions):
return list(positions)
@staticmethod
def block_at(*_position):
return mature
state = GatherCropStates(Munch(
world=FakeWorld(), pos=(0, 64, 0)))
output = io.StringIO()
clock = [10.0]
def monotonic():
clock[0] += 0.1
return clock[0]
with patch('mosfet.jobs.gather_crop.time.monotonic',
side_effect=monotonic), redirect_stdout(output):
state.find_new_crop()
self.assertIn('Found crop after 0.90s:', output.getvalue())
self.assertIn('Crop search timings:', output.getvalue())
self.assertIn('ripe-index 0.100s (1/1 allowed)', output.getvalue())
self.assertIn('all-crops+types 0.100s (1/1 allowed)', output.getvalue())
self.assertIn('section-select 0.100s (1 ripe, 1 crops, selected section 1)',
output.getvalue())
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):
def find_blocks_indexed(_center, block_ids, _distance, sort=True):
return [position]
@staticmethod
@@ -75,6 +116,32 @@ class CropBatchTests(unittest.TestCase):
self.assertEqual(game.break_calls, [])
self.assertIs(state.state.__func__, state.find_new_crop.__func__)
def test_already_held_seed_skips_selection_and_places_from_held_hand(self):
position = (1, 64, 1)
seed = items.WHEAT_SEEDS_ID
class FakeGame:
def __init__(self):
self.placements = []
def select_item(self, _item_ids):
raise AssertionError('already-held seed should not be selected')
def place_block(self, location, face, hand=0):
self.placements.append((location, face, hand))
game = FakeGame()
state = GatherCropStates(Munch(
game=game, holding=seed, holding_hand=1))
state.crop = position
state.type_id = blocks.MCD_BLOCKS['wheat']['minStateId']
state.select_seed()
self.assertIs(state.state.__func__, state.place_crop.__func__)
state.place_crop()
self.assertEqual(game.placements, [((1, 63, 1), 1, 1)])
def test_seed_selection_uses_crop_species_for_any_age_state(self):
unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId']
+75
View File
@@ -0,0 +1,75 @@
import unittest
from munch import Munch
from mosfet.info import blocks
from mosfet.jobs.gather_crop import GatherCropStates
class CropSectionCacheTests(unittest.TestCase):
def make_world(self, crops):
class FakeWorld:
def __init__(self):
self.crops = dict(crops)
self.index_calls = 0
def find_blocks_indexed(self, _center, block_ids, _distance,
sort=True):
self.index_calls += 1
return [position for position, state in self.crops.items()
if state in block_ids]
@staticmethod
def filter_work_positions(positions):
return list(positions)
def block_at(self, *position):
return self.crops.get(tuple(position), blocks.AIR)
return FakeWorld()
def test_reuses_active_section_without_rebuilding_world_indexes(self):
first = (1, 64, 0)
second = (2, 64, 0)
unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId']
world = self.make_world({
first: blocks.MATURE_WHEAT_ID,
second: unripe_wheat,
})
state = GatherCropStates(Munch(world=world, pos=(0, 64, 0)))
state.find_new_crop()
self.assertEqual(state.crop, first)
self.assertEqual(state.active_section_positions, {first, second})
self.assertEqual(world.index_calls, 2)
world.crops[first] = unripe_wheat
world.crops[second] = blocks.MATURE_WHEAT_ID
state.find_new_crop()
self.assertEqual(state.crop, second)
self.assertEqual(world.index_calls, 2)
self.assertEqual(state.active_section_maturity, (1, 2))
def test_rebuilds_cache_when_a_cached_crop_disappears(self):
first = (1, 64, 0)
second = (2, 64, 0)
unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId']
world = self.make_world({
first: blocks.MATURE_WHEAT_ID,
second: unripe_wheat,
})
state = GatherCropStates(Munch(world=world, pos=(0, 64, 0)))
state.find_new_crop()
self.assertEqual(world.index_calls, 2)
world.crops.pop(first)
world.crops[second] = blocks.MATURE_WHEAT_ID
state.find_new_crop()
self.assertEqual(state.crop, second)
self.assertEqual(world.index_calls, 4)
self.assertEqual(state.active_section_positions, {second})
if __name__ == '__main__':
unittest.main()
+40
View File
@@ -0,0 +1,40 @@
import unittest
from munch import Munch
from mosfet.info import blocks
from mosfet.world import World
class IndexedSearchTests(unittest.TestCase):
def test_index_deduplication_is_linear_and_sort_is_optional(self):
far = (10, 64, 0)
near = (1, 64, 0)
class FakeChunks:
def __init__(self):
self.reads = {}
@staticmethod
def indexed_positions(_block_ids):
return [far, near, far]
def get_block_at(self, *position):
position = tuple(position)
self.reads[position] = self.reads.get(position, 0) + 1
return blocks.STONE
chunks = FakeChunks()
world = World(Munch(chunks=chunks))
self.assertEqual(world.find_blocks_indexed(
(0, 64, 0), {blocks.STONE}, 50), [near, far])
self.assertEqual(chunks.reads, {far: 1, near: 1})
chunks.reads.clear()
self.assertEqual(world.find_blocks_indexed(
(0, 64, 0), {blocks.STONE}, 50, sort=False), [far, near])
self.assertEqual(chunks.reads, {far: 1, near: 1})
if __name__ == '__main__':
unittest.main()
+2 -1
View File
@@ -13,7 +13,8 @@ class SignWorkTargetTests(unittest.TestCase):
crop_state = blocks.MCD_BLOCKS['wheat']['minStateId']
class FakeWorld:
def find_blocks_indexed(self, _center, _block_ids, _distance):
def find_blocks_indexed(self, _center, _block_ids, _distance,
sort=True):
return [outside]
@staticmethod