Files
minecraft-bot/tests/test_indexed_search.py
T

93 lines
3.0 KiB
Python

import unittest
from munch import Munch
from mosfet.info import blocks
from mosfet.world import World
class IndexedSearchTests(unittest.TestCase):
def test_sand_safety_reports_support_headroom_and_adjacent_hazards(self):
sand = (0, 64, 0)
world_blocks = {
sand: blocks.SAND,
(0, 63, 0): blocks.STONE,
(0, 62, 0): blocks.STONE,
}
chunks = Munch(get_block_at=lambda *position:
world_blocks.get(tuple(position), blocks.AIR))
world = World(Munch(chunks=chunks))
self.assertEqual(world.sand_safety(sand)[0], True)
world_blocks[(0, 63, 0)] = blocks.AIR
safe, reason = world.sand_safety(sand)
self.assertFalse(safe)
self.assertIn('support block', reason)
world_blocks[(0, 63, 0)] = blocks.STONE
world_blocks[(0, 65, 0)] = blocks.STONE
safe, reason = world.sand_safety(sand)
self.assertFalse(safe)
self.assertIn('headroom', reason)
world_blocks[(0, 65, 0)] = blocks.AIR
water = next(iter(blocks.WATER_IDS))
world_blocks[(1, 64, 0)] = water
safe, reason = world.sand_safety(sand)
self.assertFalse(safe)
self.assertIn('unsafe adjacent water', reason)
def test_sand_search_targets_safe_blocks_in_3x3_slice_only(self):
sand_position = (1, 64, 1)
class SandChunks:
def get_block_at(self, x, y, z):
position = (x, y, z)
if position == sand_position:
return blocks.SAND
if y < sand_position[1]:
return blocks.STONE
return blocks.AIR
world = World(Munch(chunks=SandChunks()))
self.assertTrue(world.check_sand_slice((0, 64, 0)))
self.assertEqual(
world.find_sand((0, 64, 0), 2, (0, 64, 0)),
[sand_position])
sand_position = (2, 64, 0)
self.assertFalse(world.check_sand_slice((0, 64, 0)))
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()