def test_model_render_groups(): m = Model(io.StringIO(""" usemtl brick_block g group1 v 0 0 0 v 4 0 0 v 4 0 4 v 0 0 4 f -1 -2 -3 -4 g group2 v 0 1 0 v 4 1 0 v 4 1 4 v 0 1 4 f -1 -2 -3 -4 """)) b = Block('brick_block') assert m.render(groups='group1') == { v: b for v in vector_range(O, 4*X + 4*Z + 1) } assert m.render(groups=b'group1') == { v: b for v in vector_range(O, 4*X + 4*Z + 1) } assert m.render(groups={'group1'}) == { v: b for v in vector_range(O, 4*X + 4*Z + 1) }
def test_vector_range_init(): v = vector_range(Vector() + 2) assert v.start == Vector() assert v.stop == Vector(2, 2, 2) assert v.step == Vector(1, 1, 1) with pytest.raises(TypeError): vector_range(Vector(0.0, 0.0, 0.0), step=Vector(0.5, 0.5, 1.0))
def test_model_render_groups(): m = Model( io.StringIO(""" usemtl brick_block g group1 v 0 0 0 v 4 0 0 v 4 0 4 v 0 0 4 f -1 -2 -3 -4 g group2 v 0 1 0 v 4 1 0 v 4 1 4 v 0 1 4 f -1 -2 -3 -4 """)) b = Block('brick_block') assert m.render(groups='group1') == { v: b for v in vector_range(O, 4 * X + 4 * Z + 1) } assert m.render(groups=b'group1') == { v: b for v in vector_range(O, 4 * X + 4 * Z + 1) } assert m.render(groups={'group1'}) == { v: b for v in vector_range(O, 4 * X + 4 * Z + 1) }
def test_vector_range_step(): assert vector_range(Vector() + 1, step=Vector() + 2).step == Vector(2, 2, 2) assert vector_range(Vector(), Vector() + 1, Vector() + 1).step == Vector(1, 1, 1) with pytest.raises(ValueError): vector_range(Vector() + 1, step=Vector(1, 1, 0))
def test_vector_getslice(): v = vector_range(Vector() + 2, order='xyz') assert v[Vector(1, 0, 0):] == vector_range(Vector(x=1), Vector() + 2, order='xyz') assert v[:Vector(1, None, None)] == vector_range(Vector(1, 2, 2), order='xyz') with pytest.raises(ValueError): v[1:] with pytest.raises(ValueError): v[::Vector(1, 1, 0)]
def test_blocks_set_vrange_different(): conn = mock.MagicMock() v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) blocks = [Block(1, 1) for v in vector_range(v_from, v_to)] picraft.block.Blocks(conn)[v_from:v_to] = blocks for v in vector_range(v_from, v_to): conn.send.assert_any_call('world.setBlock(%d,%d,%d,1,1)' % (v.x, v.y, v.z))
def test_blocks_get_vrange(): v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) conn = mock.MagicMock() conn.transact.return_value = '1,1' assert picraft.block.Blocks(conn)[v_from:v_to] == [ Block(1, 1) for v in vector_range(v_from, v_to) ] for v in vector_range(v_from, v_to): conn.transact.assert_any_call('world.getBlockWithData(%d,%d,%d)' % (v.x, v.y, v.z))
def test_blocks_set_many_different(): conn = mock.MagicMock() v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) blocks = [Block(1, 1) for v in vector_range(v_from, v_to)] picraft.block.Blocks(conn)[v_from:v_to] = blocks for v in vector_range(v_from, v_to): conn.send.assert_any_call( 'world.setBlock(%d,%d,%d,1,1)' % (v.x, v.y, v.z)) with pytest.raises(ValueError): picraft.block.Blocks(conn)[v_from:v_to] = blocks[1:] with pytest.raises(ValueError): picraft.block.Blocks(conn)[v_from:v_to] = blocks * 2
def test_vector_range_index(): assert vector_range(Vector() + 2).index(Vector()) == 0 assert vector_range(Vector() + 2)[:Vector() - 1].index(Vector()) == 0 assert vector_range(Vector() + 2).index(Vector(1, 1, 1)) == 7 assert vector_range(Vector() + 2)[Vector(z=1):].index(Vector(1, 1, 1)) == 3 assert vector_range(Vector() + 2, order='xyz').index(Vector(1, 0, 0)) == 1 assert vector_range(Vector() + 2, order='zxy').index(Vector(1, 0, 0)) == 2 assert vector_range(Vector() + 2, order='zyx').index(Vector(1, 0, 0)) == 4 with pytest.raises(ValueError): vector_range(Vector() + 2).index(Vector(2, 2, 2))
def animation_frames(count): cube_range = vector_range(Vector() - 2, Vector() + 2 + 1) for frame in range(count): yield { v.rotate(15 * frame, about=X).round() + (5 * Y): Block('stone') for v in cube_range }
def animation_frames(count): cube_range = vector_range(Vector() - 2, Vector() + 2 + 1) for frame in range(count): state = {} for v in cube_range: state[v.rotate(15 * frame, about=X).round() + (5 * Y)] = Block('stone') yield state
def test_blocks_get_many(): conn = mock.MagicMock() conn.transact.return_value = '1,1' v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) picraft.block.Blocks(conn)[v_from:v_to] for v in vector_range(v_from, v_to): conn.transact.assert_any_call( 'world.getBlockWithData(%d,%d,%d)' % (v.x, v.y, v.z))
def test_blocks_set_vrange_same_stepped(): conn = mock.MagicMock() v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) v_step = Vector(2, 2, 2) picraft.block.Blocks(conn)[v_from:v_to:v_step] = Block(0, 0) for v in vector_range(v_from, v_to, v_step): conn.send.assert_any_call('world.setBlock(%d,%d,%d,0,0)' % (v.x, v.y, v.z))
def test_world_height_get(): with mock.patch('picraft.world.Connection') as c: World().height[Vector(1, 2, 3)] c().transact.assert_called_once_with('world.getHeight(1,3)') c().transact.reset_mock() v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) World().height[v_from:v_to] for v in vector_range(v_from, v_to): c().transact.assert_any_call('world.getHeight(%d,%d)' % (v.x, v.z))
def test_blocks_get_vrange_fast(): v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) conn = mock.MagicMock() conn.server_version = 'raspberry-juice' conn.transact.return_value = '1,1' picraft.block.Blocks(conn)[v_from:v_to] == [ Block(1, 0) for v in vector_range(v_from, v_to) ] conn.transact.assert_called_once_with('world.getBlocks(%s,%s)' % (v_from, v_to - 1))
def test_model_render_defaults(): m = Model( io.StringIO(""" usemtl brick_block v 0 0 0 v 4 0 0 v 4 0 4 v 0 0 4 f -1 -2 -3 -4""")) b = Block('brick_block') assert m.render() == {v: b for v in vector_range(O, 4 * X + 4 * Z + 1)}
def test_model_render_defaults(): m = Model(io.StringIO(""" usemtl brick_block v 0 0 0 v 4 0 0 v 4 0 4 v 0 0 4 f -1 -2 -3 -4""")) b = Block('brick_block') assert m.render() == { v: b for v in vector_range(O, 4*X + 4*Z + 1) }
def test_model_render_materials_dict(): m = Model(io.StringIO(""" usemtl brick v 0 0 0 v 4 0 0 v 4 0 4 v 0 0 4 f -1 -2 -3 -4""")) b = Block('brick_block') assert m.render(materials={'brick': b}) == { v: b for v in vector_range(O, 4*X + 4*Z + 1) }
def test_model_render_materials_dict(): m = Model( io.StringIO(""" usemtl brick v 0 0 0 v 4 0 0 v 4 0 4 v 0 0 4 f -1 -2 -3 -4""")) b = Block('brick_block') assert m.render(materials={'brick': b}) == { v: b for v in vector_range(O, 4 * X + 4 * Z + 1) }
def test_vector_range_getitem(): v = vector_range(Vector() + 2, order='xyz') assert v[0] == Vector() assert v[1] == Vector(1, 0, 0) assert v[7] == Vector(1, 1, 1) assert v[7] == v[-1] assert v[6] == v[-2] assert v[0] == v[-8] assert v[Vector()] == Vector() assert v[Vector(1, 0, 0)] == Vector(1, 0, 0) assert v[Vector() - 1] == Vector(1, 1, 1) with pytest.raises(IndexError): v[8] with pytest.raises(IndexError): v[-9] with pytest.raises(IndexError): v[Vector(2, 1, 1)]
def test_vector_range_reversed(): assert list(reversed(vector_range(Vector() + 2))) == list( reversed(list(vector_range(Vector() + 2)))) assert list(reversed(vector_range( Vector() + 2, order='xyz'))) == vector_range(Vector() + 1, Vector() - 1, Vector() - 1, order='xyz') assert list(reversed(vector_range(Vector() + 2, order='xyz'))) == vector_range( Vector() + 2, order='xyz')[::Vector() - 1]
def test_parse_model_face_complex(): m = Model(io.StringIO(""" usemtl brick g group1 v 0 0 0 v 1 0 0 v 1 0 1 v 0 0 1 vn 0 1 0 vt 0 f -1/1/1 -2/1/1 -3 -4""")) assert len(m.faces) == 1 assert m.faces[0].material == "brick" assert m.faces[0].groups == {"group1"} assert m.faces[0].vectors == ((0.0, 0.0, 1.0), (1.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 0.0, 0.0)) assert m.materials == {"brick"} assert list(m.groups.items()) == [("group1", [m.faces[0]])] assert m.bounds == vector_range(O, X + Z + 1)
def test_parse_model_face_complex(): m = Model( io.StringIO(""" usemtl brick g group1 v 0 0 0 v 1 0 0 v 1 0 1 v 0 0 1 vn 0 1 0 vt 0 f -1/1/1 -2/1/1 -3 -4""")) assert len(m.faces) == 1 assert m.faces[0].material == "brick" assert m.faces[0].groups == {"group1"} assert m.faces[0].vectors == ((0.0, 0.0, 1.0), (1.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 0.0, 0.0)) assert m.materials == {"brick"} assert list(m.groups.items()) == [("group1", [m.faces[0]])] assert m.bounds == vector_range(O, X + Z + 1)
def test_vector_range_stop(): assert vector_range(Vector() + 1).stop == Vector(1, 1, 1) assert vector_range(Vector(1, 1, 0)).stop == Vector(1, 1, 0) assert vector_range(Vector(1, 0, 1), Vector() + 1).stop == Vector(1, 1, 1)
def test_vector_range_order(): assert vector_range(Vector() + 1).order == 'zxy' assert vector_range(Vector() + 1, order='xyz').order == 'xyz' with pytest.raises(ValueError): vector_range(Vector() + 2, order='abc')
def test_vector_range_ordering(): assert vector_range(Vector() + 2, order='xyz') == vector_range(Vector(2, 2, 2), order='xyz') assert vector_range(Vector() + 2, order='xyz') == list( vector_range(Vector(2, 2, 2), order='xyz')) assert vector_range(Vector() + 2, order='zxy') <= vector_range( Vector() + 2, order='xyz') assert vector_range(Vector() + 2, order='zxy') <= vector_range( Vector() + 2, order='zxy') assert vector_range(Vector() + 2, order='zxy') < vector_range(Vector() + 2, order='xyz') assert not (vector_range(Vector() + 2, order='zxy') < vector_range( Vector() + 2, order='zxy')) assert vector_range(Vector() + 2, order='xyz') > vector_range(Vector() + 2, order='zxy') assert vector_range(Vector() + 2, order='xyz') >= vector_range( Vector() + 2, order='zxy') assert vector_range(Vector() + 2, order='xyz') != vector_range( Vector() + 2, order='zxy') assert vector_range(Vector() + 2, order='xyz') == [ Vector(0, 0, 0), Vector(1, 0, 0), Vector(0, 1, 0), Vector(1, 1, 0), Vector(0, 0, 1), Vector(1, 0, 1), Vector(0, 1, 1), Vector(1, 1, 1), ] assert vector_range(Vector() + 2, order='zxy') == [ Vector(0, 0, 0), Vector(0, 0, 1), Vector(1, 0, 0), Vector(1, 0, 1), Vector(0, 1, 0), Vector(0, 1, 1), Vector(1, 1, 0), Vector(1, 1, 1), ] assert vector_range(Vector() + 2, order='zyx') == [ Vector(0, 0, 0), Vector(0, 0, 1), Vector(0, 1, 0), Vector(0, 1, 1), Vector(1, 0, 0), Vector(1, 0, 1), Vector(1, 1, 0), Vector(1, 1, 1), ] assert iter(vector_range(Vector() + 2, order='zxy')) < vector_range( Vector() + 2, order='xyz') assert iter(vector_range(Vector() + 2, order='zxy')) <= vector_range( Vector() + 2, order='xyz') assert iter(vector_range(Vector() + 2, order='xyz')) > vector_range( Vector() + 2, order='zxy') assert iter(vector_range(Vector() + 2, order='xyz')) >= vector_range( Vector() + 2, order='zxy') assert iter(vector_range(Vector() + 2, order='xyz')) != vector_range( Vector() + 2, order='zxy')
def test_vector_range_start(): assert vector_range(Vector() + 1).start == Vector() assert vector_range(Vector(), Vector() + 1).start == Vector() assert vector_range(Vector(1, 0, 1), Vector() + 1).start == Vector(1, 0, 1)
def test_vector_range_reversed(): assert list(reversed(vector_range(Vector() + 2))) == list(reversed(list(vector_range(Vector() + 2)))) assert list(reversed(vector_range(Vector() + 2, order='xyz'))) == vector_range(Vector() + 1, Vector() - 1, Vector() - 1, order='xyz') assert list(reversed(vector_range(Vector() + 2, order='xyz'))) == vector_range(Vector() + 2, order='xyz')[::Vector() - 1]
def test_vector_range_len(): assert len(vector_range(Vector())) == 0 assert len(vector_range(Vector() + 1)) == 1 assert len(vector_range(Vector() + 2)) == 8 assert len(vector_range(Vector() + 3)) == 27
def animation_frames(count): cube_range = vector_range(Vector() - 2, Vector() + 2 + 1) for frame in range(count): yield { v.rotate(15 * frame, about=X).round() + (5 * Y): Block('stone') for v in cube_range}
Alternate design: If thread_count > 1 is desired, this could first get the query_blocks dict, then iterate vrange again. """ return [Vector(pos[0], data, pos[1]) for (pos, data) in query_blocks(world.connection, ((v[0], v[2]) for v in vrange), "world.getHeight(%d,%d)", int, thread_count = 0)] if __name__ == "__main__": world = picraft.World() world.say("Hello mcpi_fast_query!") cuboid = picraft.vector_range(Vector(-2, 0, -2), Vector(2, 0, 2) + 1) my_blocks = {} for pos, blk in query_blocks( world.connection, cuboid, "world.getBlockWithData(%d,%d,%d)", lambda ans: tuple(map(int, ans.split(","))), thread_count=0): my_blocks[pos] = blk #print(my_blocks) x = alt_picraft_getblock_vrange(world, cuboid) print(x) y = world.blocks[cuboid] #print(y) print(x==y)
def test_vector_range_contains(): assert Vector() in vector_range(Vector() + 2) assert Vector(1, 1, 1) in vector_range(Vector() + 2) assert Vector(x=1) in vector_range(Vector() + 2, order='xyz') assert Vector() + 2 not in vector_range(Vector() + 2)
def test_vector_range_count(): assert vector_range(Vector() + 2).count(Vector()) == 1 assert vector_range(Vector() + 2).count(Vector(1, 1, 1)) == 1 assert vector_range(Vector() + 2, order='xyz').count(Vector(1, 0, 0)) == 1 assert vector_range(Vector() + 2).count(Vector(2, 2, 2)) == 0
from __future__ import division from time import sleep from picraft import World, Vector, X, Y, Z, vector_range, Block world = World() world.checkpoint.save() try: cube_range = vector_range(Vector() - 2, Vector() + 2 + 1) # Draw frame 1 state = {} for v in cube_range: state[v + (5 * Y)] = Block('stone') with world.connection.batch_start(): for v, b in state.items(): world.blocks[v] = b sleep(0.2) # Wipe frame 1 with world.connection.batch_start(): for v in state: world.blocks[v] = Block('air') # Draw frame 2 state = {} for v in cube_range: state[v.rotate(15, about=X).round() + (5 * Y)] = Block('stone') with world.connection.batch_start(): for v, b in state.items(): world.blocks[v] = b sleep(0.2) # and so on... finally:
from picraft import World, Block, Vector, X, Y, Z, vector_range, line world = World() world.checkpoint.save() world.events.track_players = world.players p = world.player.tile_pos GAME_AREA = vector_range(p - Vector(30, 2, 25), p + Vector(25, 20, 25) + 1) DRAWING_AREA = vector_range(p - Vector(25, 1, 25), p + Vector(25, -1, 25) + 1) WALKING_AREA = vector_range(DRAWING_AREA.start + Y, DRAWING_AREA.stop + Y) CLEAR_BUTTON = p - Vector(30, 0, -23) QUIT_BUTTON = p - Vector(30, 0, -25) with world.connection.batch_start(): world.blocks[GAME_AREA[:Vector(None, 2, None)]] = Block('dirt') world.blocks[GAME_AREA[Vector(None, 2, None):]] = Block('air') world.blocks[DRAWING_AREA] = Block('sand') world.blocks[QUIT_BUTTON] = Block('#ff0000') world.blocks[CLEAR_BUTTON] = Block('#ffff00') def track_changes(old_state, new_state, default=Block('sand')): changes = {v: b for v, b in new_state.items() if old_state.get(v) != b} changes.update({v: default for v in old_state if v not in new_state}) return changes def draw_lines(old_state, lines): new_state = { v: Block('brick_block') for line_start, line_finish in lines for v in line(line_start, line_finish) }
def test_vector_range_ordering(): assert vector_range(Vector() + 2, order='xyz') == vector_range(Vector(2, 2, 2), order='xyz') assert vector_range(Vector() + 2, order='xyz') == list(vector_range(Vector(2, 2, 2), order='xyz')) assert vector_range(Vector() + 2, order='zxy') <= vector_range(Vector() + 2, order='xyz') assert vector_range(Vector() + 2, order='zxy') <= vector_range(Vector() + 2, order='zxy') assert vector_range(Vector() + 2, order='zxy') < vector_range(Vector() + 2, order='xyz') assert not (vector_range(Vector() + 2, order='zxy') < vector_range(Vector() + 2, order='zxy')) assert vector_range(Vector() + 2, order='xyz') > vector_range(Vector() + 2, order='zxy') assert vector_range(Vector() + 2, order='xyz') >= vector_range(Vector() + 2, order='zxy') assert vector_range(Vector() + 2, order='xyz') != vector_range(Vector() + 2, order='zxy') assert vector_range(Vector() + 2, order='xyz') == [ Vector(0, 0, 0), Vector(1, 0, 0), Vector(0, 1, 0), Vector(1, 1, 0), Vector(0, 0, 1), Vector(1, 0, 1), Vector(0, 1, 1), Vector(1, 1, 1), ] assert vector_range(Vector() + 2, order='zxy') == [ Vector(0, 0, 0), Vector(0, 0, 1), Vector(1, 0, 0), Vector(1, 0, 1), Vector(0, 1, 0), Vector(0, 1, 1), Vector(1, 1, 0), Vector(1, 1, 1), ] assert vector_range(Vector() + 2, order='zyx') == [ Vector(0, 0, 0), Vector(0, 0, 1), Vector(0, 1, 0), Vector(0, 1, 1), Vector(1, 0, 0), Vector(1, 0, 1), Vector(1, 1, 0), Vector(1, 1, 1), ] assert iter(vector_range(Vector() + 2, order='zxy')) < vector_range(Vector() + 2, order='xyz') assert iter(vector_range(Vector() + 2, order='zxy')) <= vector_range(Vector() + 2, order='xyz') assert iter(vector_range(Vector() + 2, order='xyz')) > vector_range(Vector() + 2, order='zxy') assert iter(vector_range(Vector() + 2, order='xyz')) >= vector_range(Vector() + 2, order='zxy') assert iter(vector_range(Vector() + 2, order='xyz')) != vector_range(Vector() + 2, order='zxy')
def test_vector_range_bool(): assert not vector_range(Vector()) assert vector_range(Vector() + 1) assert not vector_range(Vector() + 1)[Vector() + 1:]