Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Malmo/samples/Python_examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set( SOURCES
mission_quit_command_example.py
MultiMaze.py
mob_fun.py
moving_target_test.py
overclock_test.py
patchwork_quilt.py
quit_from_reaching_position_test.py
Expand Down
171 changes: 171 additions & 0 deletions Malmo/samples/Python_examples/moving_target_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# ------------------------------------------------------------------------------------------------
# Copyright (c) 2016 Microsoft Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------------------------

# Sample to demonstrate use of MovingTargetDecorator.
# Creates two moving targets - one which moves as fast as possible, and one which is turn-based, and
# will wait for the agent to take its turn.

import MalmoPython
import os
import random
import sys
import time
import json
import random
import errno

def GetMissionXML(summary):
return '''<?xml version="1.0" encoding="UTF-8" ?>
<Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<About>
<Summary>''' + summary + '''</Summary>
</About>

<ServerSection>
<ServerHandlers>
<FlatWorldGenerator generatorString="3;7,220*1,5*3,2;3;,biome_1" />
<DrawingDecorator>
<DrawCuboid x1="-50" y1="226" z1="-50" x2="50" y2="227" z2="50" type="lapis_block"/>
</DrawingDecorator>
<MovingTargetDecorator>
<ArenaBounds>
<min x="-50" y="226" z="-50"/>
<max x="50" y="226" z="50"/>
</ArenaBounds>
<StartPos x="3" y="226" z="0"/>
<Seed>random</Seed>
<UpdateSpeed>turnbased</UpdateSpeed>
<PermeableBlocks type="air obsidian"/>
<BlockType type="glowstone"/>
</MovingTargetDecorator>
<MovingTargetDecorator>
<ArenaBounds>
<min x="-50" y="226" z="-50"/>
<max x="50" y="226" z="50"/>
</ArenaBounds>
<StartPos x="-3" y="226" z="0"/>
<Seed>random</Seed>
<UpdateSpeed>1</UpdateSpeed>
<PermeableBlocks type="air obsidian"/>
<BlockType type="beacon"/>
</MovingTargetDecorator>
<MazeDecorator>
<SizeAndPosition length="20" width="20" yOrigin="226" zOrigin="-10" xOrigin="-10" height="20"/>
<GapProbability>0.1</GapProbability>
<Seed>random</Seed>
<MaterialSeed>random</MaterialSeed>
<AllowDiagonalMovement>false</AllowDiagonalMovement>
<StartBlock fixedToEdge="false" type="emerald_block" height="0"/>
<EndBlock fixedToEdge="false" type="redstone_block" height="0"/>
<PathBlock type="obsidian" height="0"/>
<FloorBlock type="obsidian"/>
<GapBlock type="stained_hardened_clay" height="1"/>
</MazeDecorator>
<ServerQuitFromTimeUp timeLimitMs="150000"/>
<ServerQuitWhenAnyAgentFinishes />
</ServerHandlers>
</ServerSection>

<AgentSection mode="Creative">
<Name>Chevy</Name>
<AgentStart>
<Placement x="0.5" y="227.0" z="0.5"/>
</AgentStart>
<AgentHandlers>
<TurnBasedCommands requestedPosition="1">
<DiscreteMovementCommands/>
</TurnBasedCommands>
<AgentQuitFromTouchingBlockType>
<Block type="glowstone"/>
</AgentQuitFromTouchingBlockType>
</AgentHandlers>
</AgentSection>

</Mission>'''

recordingsDirectory="MovingTargetRecordings"
try:
os.makedirs(recordingsDirectory)
except OSError as exception:
if exception.errno != errno.EEXIST: # ignore error if already existed
raise

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately

validate = True
my_client_pool = MalmoPython.ClientPool()
my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10000))

agent_host = MalmoPython.AgentHost()
try:
agent_host.parse( sys.argv )
except RuntimeError as e:
print 'ERROR:',e
print agent_host.getUsage()
exit(1)
if agent_host.receivedArgument("help"):
print agent_host.getUsage()
exit(0)

if agent_host.receivedArgument("test"):
num_reps = 1
else:
num_reps = 30000

for iRepeat in range(num_reps):
my_mission = MalmoPython.MissionSpec(GetMissionXML("Moving target #" + str(iRepeat)),validate)
my_mission_record = MalmoPython.MissionRecordSpec()
max_retries = 3
for retry in range(max_retries):
try:
# Attempt to start the mission:
agent_host.startMission( my_mission, my_client_pool, my_mission_record, 0, "movingTargetTestExperiment" )
break
except RuntimeError as e:
if retry == max_retries - 1:
print "Error starting mission",e
print "Is the game running?"
exit(1)
else:
time.sleep(2)

world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
time.sleep(0.1)
world_state = agent_host.getWorldState()

# main loop:
turn_key = ""
while world_state.is_mission_running:
world_state = agent_host.getWorldState()
if world_state.number_of_observations_since_last_state > 0:
msg = world_state.observations[-1].text
ob = json.loads(msg)
new_turn_key = ob.get(u'turn_key', "")
turn_index = ob.get(u'turn_number',0)
if len(new_turn_key) > 0 and new_turn_key != turn_key:
if agent_host.receivedArgument("test"):
nb = random.choice(["movenorth","movesouth","moveeast","movewest"])
else:
nb = raw_input('Enter command: ')
agent_host.sendCommand(nb, str(new_turn_key))
turn_key = new_turn_key

# mission has ended.
time.sleep(0.5) # Give the mod a little time to prepare for the next mission.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

package com.microsoft.Malmo.MissionHandlerInterfaces;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.world.World;

import com.microsoft.Malmo.Schemas.MissionInit;

/** Interface for objects which can determine the world structure for the Minecraft mission.
Expand Down Expand Up @@ -59,4 +62,17 @@ public DecoratorException(String message)
/** Called once after the mission ends - use for any necessary mission cleanup.
*/
public void cleanup();

/** Used by the turn scheduler - if decorator matches this string, it must acknowledge and take its turn.
* @param nextAgentName - string to match against
* @return true if matching
*/
public boolean targetedUpdate(String nextAgentName);

/** Used by the turn scheduler - if the decorator wants to be part of the turn schedule, it must add a name
* and a requested slot (can be null) to these arrays.
* @param participants
* @param participantSlots
*/
public void getTurnParticipants(ArrayList<String> participants, ArrayList<Integer> participantSlots);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.microsoft.Malmo.MissionHandlers;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

Expand Down Expand Up @@ -182,4 +183,16 @@ public void prepare(MissionInit missionInit)
public void cleanup()
{
}

@Override
public boolean targetedUpdate(String nextAgentName)
{
return false; // Does nothing.
}

@Override
public void getTurnParticipants(ArrayList<String> participants, ArrayList<Integer> participantSlots)
{
// Does nothing.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,17 @@ public void cleanup()
MinecraftForge.EVENT_BUS.unregister(this);
FMLCommonHandler.instance().bus().unregister(this);
}

@Override
public boolean targetedUpdate(String nextAgentName)
{
return false; // Does nothing.
}

@Override
public void getTurnParticipants(ArrayList<String> participants, ArrayList<Integer> participantSlots)
{
// Does nothing.
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1529,4 +1529,16 @@ public void prepare(MissionInit missionInit)
public void cleanup()
{
}

@Override
public boolean targetedUpdate(String nextAgentName)
{
return false; // Does nothing.
}

@Override
public void getTurnParticipants(ArrayList<String> participants, ArrayList<Integer> participantSlots)
{
// Does nothing.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.microsoft.Malmo.MissionHandlers;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -82,4 +83,16 @@ public void prepare(MissionInit missionInit)
public void cleanup()
{
}

@Override
public boolean targetedUpdate(String nextAgentName)
{
return false; // Does nothing.
}

@Override
public void getTurnParticipants(ArrayList<String> participants, ArrayList<Integer> participantSlots)
{
// Does nothing.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import net.minecraft.world.World;

import com.microsoft.Malmo.MissionHandlerInterfaces.IWorldDecorator;
import com.microsoft.Malmo.Schemas.AgentHandlers;
import com.microsoft.Malmo.Schemas.AgentQuitFromReachingPosition;
import com.microsoft.Malmo.Schemas.AgentSection;
import com.microsoft.Malmo.Schemas.BlockOrItemSpec;
Expand Down Expand Up @@ -795,4 +794,16 @@ public void prepare(MissionInit missionInit)
public void cleanup()
{
}

@Override
public boolean targetedUpdate(String nextAgentName)
{
return false; // Does nothing.
}

@Override
public void getTurnParticipants(ArrayList<String> participants, ArrayList<Integer> participantSlots)
{
// Does nothing.
}
}
Loading