diff --git a/tests/vereya/common.py b/tests/vereya/common.py index b3f1f38..b5137c3 100644 --- a/tests/vereya/common.py +++ b/tests/vereya/common.py @@ -9,7 +9,7 @@ from base_test import BaseTest -def init_mission(mc, start_x, start_y, seed, forceReset="false", forceReuse="false"): +def init_mission(mc, start_x, start_z, seed, forceReset="false", forceReuse="false", start_y=78): want_depth = False video_producer = mb.VideoProducer(width=320 * 4, height=240 * 4, want_depth=want_depth) @@ -27,7 +27,7 @@ def init_mission(mc, start_x, start_y, seed, forceReset="false", forceReuse="fal agentSections=[mb.AgentSection(name='Cristina', agenthandlers=agent_handlers, # depth - agentstart=mb.AgentStart([start_x, 78.0, start_y, 1]))]) + agentstart=mb.AgentStart([start_x, start_y, start_z, 1]))]) flat_json = {"biome":"minecraft:plains", "layers":[{"block":"minecraft:diamond_block","height":1}], "structures":{"structures": {"village":{}}}} @@ -54,3 +54,10 @@ def init_mission(mc, start_x, start_y, seed, forceReset="false", forceReuse="fal else: mc.setMissionXML(miss) return mc, obs + +def count_items(inv, name): + result = 0 + for elem in inv: + if elem['type'] == name: + result += elem['quantity'] + return result diff --git a/tests/vereya/examples b/tests/vereya/examples new file mode 120000 index 0000000..da7b196 --- /dev/null +++ b/tests/vereya/examples @@ -0,0 +1 @@ +../../examples/ \ No newline at end of file diff --git a/tests/vereya/run_tests.py b/tests/vereya/run_tests.py index 10ac0e0..db97666 100644 --- a/tests/vereya/run_tests.py +++ b/tests/vereya/run_tests.py @@ -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_consistency', 'test_motion_mob', 'test_mob', 'test_agent'] res = run_tests(test_files) if not res.wasSuccessful(): sys.exit(1) diff --git a/tests/vereya/test_agent.py b/tests/vereya/test_agent.py new file mode 100644 index 0000000..88a2317 --- /dev/null +++ b/tests/vereya/test_agent.py @@ -0,0 +1,88 @@ +import unittest +import logging +import json +import time +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 init_mission, count_items +from mcdemoaux.agenttools.agent import TAgent +from examples.minelogy import Minelogy +from examples.knowledge_lists import * +import examples.skills + + +item_to_obtain = "stone_pickaxe" + +class Tester(TAgent): + + def __init__(self, mc, visualizer=None, goal=None): + super().__init__(mc, visualizer) + self.set_goal(goal) + + def set_goal(self, goal=None): + self.goal = examples.skills.Obtain(self, [item_to_obtain]) + + def run(self): + running = True + while running: + acts, running = self.goal.cycle() + for act in acts: + self.rob.sendCommand(act) + time.sleep(0.05) + self.blockMem.updateBlocks(self.rob) + acts = self.goal.stop() + for act in acts: + self.rob.sendCommand(act) + +class TestAgent(BaseTest): + mc = None + + @classmethod + def setUpClass(self, *args, **kwargs): + start = (4.0, 69.0, 68) + mc, obs = init_mission(None, start_x=start[0], start_y=start[1], start_z=start[2], forceReset='true', seed='2') + self.mc = mc + self.rob = obs + assert mc.safeStart() + time.sleep(4) + + @classmethod + def tearDownClass(self, *args, **kwargs): + self.mc.stop() + + def setUp(self): + super().setUp() + self.mc.sendCommand("chat /clear") + time.sleep(4) + + def test_agent(self): + mc = self.mc + agent = Tester(mc) + + # initialize_minelogy + item_list, recipes = agent.rob.getItemsAndRecipesLists() + self.assertNotEqual(item_list, None, "check item_list not None") + self.assertNotEqual(recipes, None, "check recipes not None") + blockdrops = agent.rob.getBlocksDropsList() + self.assertNotEqual(blockdrops, None, "check blockdrops not None") + agent.rob.updatePassableBlocks() + try: + mlogy = Minelogy(item_list, items_to_craft, recipes, items_to_mine, blockdrops, ore_depths) + except Exception as e: + print(f'Exception occured: {e}') + return + agent.set_mlogy(mlogy) + agent.run() + mc.observeProc() + inv = mc.getInventory() + self.assertEqual(count_items(inv, item_to_obtain), 1, msg=f"check if {item_to_obtain} was crafted") + +def main(): + VereyaPython.setupLogger() + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/vereya/test_consistency.py b/tests/vereya/test_consistency.py index fbb1a77..148bd0d 100644 --- a/tests/vereya/test_consistency.py +++ b/tests/vereya/test_consistency.py @@ -6,28 +6,19 @@ import tagilmo.utils.mission_builder as mb from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver from base_test import BaseTest -from common import init_mission +from common import init_mission, count_items def test_basic_motion(): pass - -def count_items(inv, name): - result = 0 - for elem in inv: - if elem['type'] == name: - result += elem['quantity'] - return result - - class TestConsistency(BaseTest): mc = None @classmethod def setUpClass(cls, *args, **kwargs): - start = (-151.0, -213.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1], forceReset='true', seed='0145371047') + start = (-151.0, -213) + mc, obs = init_mission(None, start_x=start[0], start_z=start[1], forceReset='true', seed='0145371047') cls.mc = mc cls.rob = obs assert mc.safeStart() diff --git a/tests/vereya/test_craft.py b/tests/vereya/test_craft.py index e820a90..98703c4 100644 --- a/tests/vereya/test_craft.py +++ b/tests/vereya/test_craft.py @@ -6,70 +6,18 @@ import tagilmo.utils.mission_builder as mb from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver from base_test import BaseTest - - -def init_mission(mc, start_x=None, start_y=None): - want_depth = False - video_producer = mb.VideoProducer(width=320 * 4, - height=240 * 4, want_depth=want_depth) - - obs = mb.Observations() - obs.gridNear = [[-1, 1], [-2, 1], [-1, 1]] - - - agent_handlers = mb.AgentHandlers(observations=obs, video_producer=video_producer) - - print('starting at ({0}, {1})'.format(start_x, start_y)) - - #miss = mb.MissionXML(namespace="ProjectMalmo.microsoft.com", - miss = mb.MissionXML( - agentSections=[mb.AgentSection(name='Cristina', - agenthandlers=agent_handlers, - # depth - agentstart=mb.AgentStart([start_x, 78.0, start_y, 1]))]) - flat_json = {"biome":"minecraft:plains", - "layers":[{"block":"minecraft:diamond_block","height":1}], - "structures":{"structures": {"village":{}}}} - - flat_param = "3;7,25*1,3*3,2;1;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake" - flat_json = json.dumps(flat_json).replace('"', "%ESC") - world = mb.defaultworld( - seed='4', - forceReset="false", - forceReuse="false") - miss.setWorld(world) - miss.serverSection.initial_conditions.allowedmobs = "Pig Sheep Cow Chicken Ozelot Rabbit Villager" - # uncomment to disable passage of time: - miss.serverSection.initial_conditions.time_pass = 'false' - miss.serverSection.initial_conditions.time_start = "1000" - - if mc is None: - mc = MCConnector(miss) - obs = RobustObserver(mc) - else: - mc.setMissionXML(miss) - return mc, obs - +from common import count_items, init_mission def test_basic_motion(): pass - -def count_items(inv, name): - result = 0 - for elem in inv: - if elem['type'] == name: - result += elem['quantity'] - return result - - class TestCraft(BaseTest): mc = None @classmethod def setUpClass(cls, *args, **kwargs): start = (-125.0, 71.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1]) + mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed='4', forceReset="true") cls.mc = mc assert mc.safeStart() time.sleep(4) diff --git a/tests/vereya/test_human.py b/tests/vereya/test_human.py index fdc9aef..a7445cd 100644 --- a/tests/vereya/test_human.py +++ b/tests/vereya/test_human.py @@ -5,69 +5,15 @@ import time import tagilmo.utils.mission_builder as mb from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver - - -def init_mission(mc, start_x=None, start_y=None): - want_depth = False - # quit by reaching target or when zero health - mission_ending = """ - - - - - """ - - video_producer = mb.VideoProducer(width=320 * 4, - height=240 * 4, want_depth=want_depth) - - obs = mb.Observations(bHuman=False) - obs = mb.Observations(bHuman=True) - obs.gridNear = [[-1, 1], [-2, 1], [-1, 1]] - - - agent_handlers = mb.AgentHandlers(observations=obs, - all_str=mission_ending, video_producer=video_producer) - - print('starting at ({0}, {1})'.format(start_x, start_y)) - - #miss = mb.MissionXML(namespace="ProjectMalmo.microsoft.com", - miss = mb.MissionXML( - agentSections=[mb.AgentSection(name='Cristina', - agenthandlers=agent_handlers, - # depth - agentstart=mb.AgentStart([start_x, 74.0, start_y, 1]))]) - flat_json = {"biome":"minecraft:plains", - "layers":[{"block":"minecraft:diamond_block","height":1}], - "structures":{"structures": {"village":{}}}} - - flat_param = "3;7,25*1,3*3,2;1;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake" - flat_json = json.dumps(flat_json).replace('"', "%ESC") - world = mb.defaultworld( - seed='5', - forceReset="false", - forceReuse="false") - miss.setWorld(world) - miss.serverSection.initial_conditions.allowedmobs = "Pig Sheep Cow Chicken Ozelot Rabbit Villager" - # uncomment to disable passage of time: - miss.serverSection.initial_conditions.time_pass = 'false' - miss.serverSection.initial_conditions.time_start = "1000" - - if mc is None: - mc = MCConnector(miss) - obs = RobustObserver(mc) - else: - mc.setMissionXML(miss) - return mc, obs - +from common import init_mission class TestQuit(unittest.TestCase): mc: MCConnector = None @classmethod def setUpClass(cls, *args, **kwargs): - start = 316.5, 5375.5 start = (-108.0, -187.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1]) + mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed='5', forceReset="true") cls.mc = mc assert mc.safeStart() time.sleep(3) diff --git a/tests/vereya/test_image.py b/tests/vereya/test_image.py index 623fa6b..f1a1563 100644 --- a/tests/vereya/test_image.py +++ b/tests/vereya/test_image.py @@ -20,7 +20,7 @@ class TestData(BaseTest): @classmethod def setUpClass(cls, *args, **kwargs): start = (-126, 73.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1], seed='4', forceReset='true') + mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed='4', forceReset='true') cls.mc = mc cls.rob = obs mc.safeStart() diff --git a/tests/vereya/test_inventory.py b/tests/vereya/test_inventory.py index ab8f4c2..f992962 100644 --- a/tests/vereya/test_inventory.py +++ b/tests/vereya/test_inventory.py @@ -6,49 +6,7 @@ import tagilmo.utils.mission_builder as mb from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver from base_test import BaseTest - - -def init_mission(mc, start_x=None, start_y=None): - want_depth = False - video_producer = mb.VideoProducer(width=320 * 4, - height=240 * 4, want_depth=want_depth) - - obs = mb.Observations() - obs.gridNear = [[-1, 1], [-2, 1], [-1, 1]] - - - agent_handlers = mb.AgentHandlers(observations=obs, video_producer=video_producer) - - print('starting at ({0}, {1})'.format(start_x, start_y)) - - #miss = mb.MissionXML(namespace="ProjectMalmo.microsoft.com", - miss = mb.MissionXML( - agentSections=[mb.AgentSection(name='Cristina', - agenthandlers=agent_handlers, - # depth - agentstart=mb.AgentStart([start_x, 74.0, start_y, 1]))]) - flat_json = {"biome":"minecraft:plains", - "layers":[{"block":"minecraft:diamond_block","height":1}], - "structures":{"structures": {"village":{}}}} - - flat_param = "3;7,25*1,3*3,2;1;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake" - flat_json = json.dumps(flat_json).replace('"', "%ESC") - world = mb.defaultworld( - seed='5', - forceReset="false", - forceReuse="false") - miss.setWorld(world) - miss.serverSection.initial_conditions.allowedmobs = "Pig Sheep Cow Chicken Ozelot Rabbit Villager" - # uncomment to disable passage of time: - miss.serverSection.initial_conditions.time_pass = 'false' - miss.serverSection.initial_conditions.time_start = "1000" - - if mc is None: - mc = MCConnector(miss) - obs = RobustObserver(mc) - else: - mc.setMissionXML(miss) - return mc, obs +from common import init_mission def getInvSafe(obs, item_type): @@ -61,9 +19,8 @@ class TestCraft(unittest.TestCase): @classmethod def setUpClass(cls, *args, **kwargs): - start = 316.5, 5375.5 start = (-108.0, -187.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1]) + mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed='5', forceReset='true') cls.mc = mc cls.obs = obs assert mc.safeStart() diff --git a/tests/vereya/test_mob.py b/tests/vereya/test_mob.py index 3833c81..dc3f94f 100644 --- a/tests/vereya/test_mob.py +++ b/tests/vereya/test_mob.py @@ -21,7 +21,7 @@ class TestData(BaseTest): @classmethod def setUpClass(cls, *args, **kwargs): start = (-151.0, -213.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1], forceReset='true', seed='43') + mc, obs = init_mission(None, start_x=start[0], start_z=start[1], forceReset='true', seed='43') cls.mc = mc diff --git a/tests/vereya/test_motion_mob.py b/tests/vereya/test_motion_mob.py index d6fa4c7..05b8fe2 100644 --- a/tests/vereya/test_motion_mob.py +++ b/tests/vereya/test_motion_mob.py @@ -20,7 +20,7 @@ class TestMotionMob(TestMotion): @classmethod def setUpClass(cls, *args, **kwargs): start = (-151.0, -213.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1], forceReset='true', seed='43') + mc, obs = init_mission(None, start_x=start[0], start_z=start[1], forceReset='true', seed='43') cls.mc = mc cls.obs = obs assert mc.safeStart() diff --git a/tests/vereya/test_motion_vereya.py b/tests/vereya/test_motion_vereya.py index a43ca5d..336688a 100644 --- a/tests/vereya/test_motion_vereya.py +++ b/tests/vereya/test_motion_vereya.py @@ -19,7 +19,7 @@ class TestMotion(BaseTest): @classmethod def setUpClass(cls, *args, **kwargs): start = (-151.0, -213.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1], forceReset='true', seed='43') + mc, obs = init_mission(None, start_x=start[0],start_z=start[1], forceReset='true', seed='43') cls.mc = mc cls.obs = obs assert mc.safeStart() diff --git a/tests/vereya/test_observation.py b/tests/vereya/test_observation.py index 698b08d..0b71883 100644 --- a/tests/vereya/test_observation.py +++ b/tests/vereya/test_observation.py @@ -21,7 +21,7 @@ class TestData(BaseTest): @classmethod def setUpClass(cls, *args, **kwargs): start = (-151.0, -213.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1], forceReset='true', seed='43') + mc, obs = init_mission(None, start_x=start[0], start_z=start[1], forceReset='true', seed='43') cls.mc = mc cls.rob = obs mc.safeStart() diff --git a/tests/vereya/test_placement.py b/tests/vereya/test_placement.py index 2ae3ab5..ee7e424 100644 --- a/tests/vereya/test_placement.py +++ b/tests/vereya/test_placement.py @@ -10,7 +10,7 @@ class TestPlacement(BaseTest): @classmethod def setUpClass(cls, *args, **kwargs): start = (-125.0, 73) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1], seed=4, + mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed=4, forceReset='true') cls.mc = mc cls.rob = obs diff --git a/tests/vereya/test_quit.py b/tests/vereya/test_quit.py index 4060124..f2fd79b 100644 --- a/tests/vereya/test_quit.py +++ b/tests/vereya/test_quit.py @@ -36,12 +36,6 @@ def init_mission(mc, start_x=None, start_y=None): agenthandlers=agent_handlers, # depth agentstart=mb.AgentStart([start_x, 74.0, start_y, 1]))]) - flat_json = {"biome":"minecraft:plains", - "layers":[{"block":"minecraft:diamond_block","height":1}], - "structures":{"structures": {"village":{}}}} - - flat_param = "3;7,25*1,3*3,2;1;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake" - flat_json = json.dumps(flat_json).replace('"', "%ESC") world = mb.defaultworld( seed='5', forceReset="false", @@ -65,9 +59,8 @@ class TestQuit(BaseTest): @classmethod def setUpClass(cls, *args, **kwargs): - start = 316.5, 5375.5 start = (-108.0, -187.0) - mc, obs = init_mission(None, start_x=start[0], start_y=start[1]) + mc, obs = init_mission(None, start_x=start[0], start_y=start[1]) cls.mc = mc assert mc.safeStart() time.sleep(3) diff --git a/tests/vereya/test_vereya.py b/tests/vereya/test_vereya.py index 4025cef..8c16599 100644 --- a/tests/vereya/test_vereya.py +++ b/tests/vereya/test_vereya.py @@ -17,12 +17,10 @@ def start_mission(): - miss = mb.MissionXML() colourmap_producer = mb.ColourMapProducer(width=WIDTH, height=HEIGHT) video_producer = mb.VideoProducer(width=WIDTH, height=HEIGHT, want_depth=False) obs = mb.Observations(bNearby=True, bRecipes=True) - agent_handlers = mb.AgentHandlers(observations=obs) agent_handlers = mb.AgentHandlers(observations=obs, colourmap_producer=colourmap_producer, @@ -40,9 +38,6 @@ def start_mission(): forceReset="false", forceReuse="true") - world1 = mb.flatworld("3;7,25*1,3*3,2;1;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake", - seed='43', - forceReset="false") miss.setWorld(world) miss.serverSection.initial_conditions.allowedmobs = "Pig Sheep Cow Chicken Ozelot Rabbit Villager" # uncomment to disable passage of time: