Skip to content

Commit

Permalink
Merge pull request #177 from EHAT32/main
Browse files Browse the repository at this point in the history
Added test for DrawingDecorator
  • Loading branch information
CICS-Oleg authored Nov 15, 2024
2 parents 29ae1f7 + 4628491 commit 0ee3f15
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
5 changes: 3 additions & 2 deletions tests/vereya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver
from base_test import BaseTest

def init_mission(mc, start_x, start_z, seed, forceReset="false", forceReuse="false", start_y=78, worldType = "default"):
def init_mission(mc, start_x, start_z, seed, forceReset="false", forceReuse="false", start_y=78, worldType = "default", drawing_decorator = None):
want_depth = False
video_producer = mb.VideoProducer(width=320 * 4,
height=240 * 4, want_depth=want_depth)
Expand All @@ -27,7 +27,8 @@ def init_mission(mc, start_x, start_z, seed, forceReset="false", forceReuse="fal
agentSections=[mb.AgentSection(name='Cristina',
agenthandlers=agent_handlers,
# depth
agentstart=mb.AgentStart([start_x, start_y, start_z, 1]))])
agentstart=mb.AgentStart([start_x, start_y, start_z, 1]))],
serverSection=mb.ServerSection(handlers=mb.ServerHandlers(drawingdecorator=drawing_decorator)))
flat_json = {"biome":"minecraft:plains",
"layers":[{"block":"minecraft:diamond_block","height":1}],
"structures":{"structures": {"village":{}}}}
Expand Down
2 changes: 1 addition & 1 deletion tests/vereya/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def main():
test_files = ['test_motion_vereya', 'test_craft',
'test_inventory', 'test_quit',
'test_observation', 'test_placement', 'test_image',
'test_consistency', 'test_motion_mob', 'test_mob', 'test_agent', 'test_flat_world']
'test_consistency', 'test_motion_mob', 'test_mob', 'test_agent', 'test_flat_world', 'test_draw']
res = run_tests(test_files)
if not res.wasSuccessful():
sys.exit(1)
Expand Down
116 changes: 116 additions & 0 deletions tests/vereya/test_draw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import unittest
import logging
import json
import time
from math import floor
from tagilmo import VereyaPython
import tagilmo.utils.mission_builder as mb
from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver
from base_test import BaseTest
from common import count_items, init_mission

def test_basic_motion():
pass

class TestCraft(BaseTest):
mc = None

@classmethod
def setUpClass(cls, *args, **kwargs):
start = (0, 0)
cls.block = mb.DrawBlock((start[0] + 2), -60, start[1] + 2, "cobblestone")
cls.cuboid = mb.DrawCuboid(start[0]-2, -61, start[1]-1, start[0]-2, -58, start[1]-2, "diamond_block")
cls.line = mb.DrawLine(start[0] - 2, -62, start[1]-2, start[1] + 2, -62, start[1]+ 2, "redstone_block")
cls.item = mb.DrawItem(start[0] + 1, -60, start[1], "diamond")
draw = mb.DrawingDecorator([cls.cuboid, cls.line, cls.block, cls.item])
mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed='4', forceReset="true", start_y=-60, worldType="flat", drawing_decorator=draw)
cls.mc = mc
cls.obs = obs
assert mc.safeStart()
time.sleep(4)

@classmethod
def tearDownClass(cls, *args, **kwargs):
cls.mc.stop()

def setUp(self):
super().setUp()
self.mc.sendCommand("chat /clear")
time.sleep(4)

def test_draw_block(self):
mc = self.mc
print('send ochat')
grid = mc.getGridBox()
grid_size = [grid[k][1] - grid[k][0] + 1 for k in range(3)]
blocks = mc.getNearGrid()
coords = mc.getAgentPos()
lowest_point = [coords[k] + grid[k][0] for k in range(3)]
block = self.block
idx = int(block.x - lowest_point[0] + (block.z - lowest_point[2]) * grid_size[0] + (block.y - lowest_point[1]) * grid_size[0] * grid_size[2])
real_block_type = blocks[idx]
self.assertEqual(block.blockType, real_block_type)
time.sleep(1)

def test_draw_cuboid(self):
mc = self.mc
print('send ochat')
grid = mc.getGridBox()
grid_size = [grid[k][1] - grid[k][0] + 1 for k in range(3)]
blocks = mc.getNearGrid()
coords = mc.getAgentPos()
lowest_point = [coords[k] + grid[k][0] for k in range(3)]
cuboid = self.cuboid
real_blocks = []
x1, y1, z1 = cuboid.x1, cuboid.y1, cuboid.z1
for y_ in range(cuboid.y2 - cuboid.y1 + 1):
for z_ in range(cuboid.z2 - cuboid.z1 + 1):
for x_ in range(cuboid.x2 - cuboid.x1 + 1):
idx = int(x1 + x_ - lowest_point[0] + (z1 + z_ - lowest_point[2]) * grid_size[0] + (y1 + y_ - lowest_point[1]) * grid_size[0] * grid_size[2])
real_blocks.append(blocks[idx])
needed_blocks = [cuboid.blockType] * (cuboid.x2 - cuboid.x1 + 1) * (cuboid.y2 - cuboid.y1 + 1) * (cuboid.z2 - cuboid.z1 + 1)
self.assertEqual(needed_blocks, real_blocks)
time.sleep(1)

def test_draw_line(self):
mc = self.mc
print('send ochat')
grid = mc.getGridBox()
grid_size = [grid[k][1] - grid[k][0] + 1 for k in range(3)]
blocks = mc.getNearGrid()
coords = mc.getAgentPos()
lowest_point = [coords[k] + grid[k][0] for k in range(3)]
line = self.line
x1, y1, z1 = line.x1, line.y1, line.z1
x2, y2, z2 = line.x2, line.y2, line.z2
dx, dy, dz = x2 - x1, y2 - y1, z2 - z1
steps = max(abs(dx), abs(dy), abs(dz))
x_inc, y_inc, z_inc = dx / steps, dy / steps, dz / steps
real_blocks = []
x_, y_, z_ = 0, 0, 0
for i in range(steps + 1):
x_, y_, z_ = int(i * x_inc), int(i * y_inc), int(i * z_inc)
idx = int(x1 + x_ - lowest_point[0] + (z1 + z_ - lowest_point[2]) * grid_size[0] + (y1 + y_ - lowest_point[1]) * grid_size[0] * grid_size[2])
real_blocks.append(blocks[idx])
needed_blocks = [line.blockType] * (steps + 1)
self.assertEqual(needed_blocks, real_blocks)
time.sleep(1)

def test_draw_item(self):
mc = self.mc
print('send ochat')
real_entity = mc.getNearEntities()[0] #there is only one entity in this test (agent does not count)
entity = self.item
real_entity_type = real_entity['name']
#entities spawn at center of the block surface when summoned via /summon command
self.assertEqual([entity.x, entity.y, entity.z], [floor(real_entity['x']), floor(real_entity['y']), floor(real_entity['z'])])
self.assertEqual(entity.itemType, real_entity_type)
time.sleep(1)

def main():
VereyaPython.setupLogger()
unittest.main()


if __name__ == '__main__':
main()

0 comments on commit 0ee3f15

Please sign in to comment.