Last active
August 16, 2016 22:46
-
-
Save matrixfox/f63bf545497825111af3cb23cfa22cba to your computer and use it in GitHub Desktop.
Malmo Minecraft Ai Simple Detect Lava - Avoiding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------------------------------------------ | |
# 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. | |
# ------------------------------------------------------------------------------------------------ | |
# Tutorial sample #2: Run simple mission using raw XML | |
import MalmoPython | |
import os | |
import sys | |
import time | |
import json | |
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately | |
# More interesting generator string: "3;7,44*49,73,35:1,159:4,95:13,35:13,159:11,95:10,159:14,159:6,35:6,95:6;12;" | |
missionXML='''<?xml version="1.0" encoding="UTF-8" standalone="no" ?> | |
<Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<About> | |
<Summary>Hello world!</Summary> | |
</About> | |
<ServerSection> | |
<ServerInitialConditions> | |
<Time> | |
<StartTime>1000</StartTime> | |
<AllowPassageOfTime>false</AllowPassageOfTime> | |
</Time> | |
<Weather>clear</Weather> | |
</ServerInitialConditions> | |
<ServerHandlers> | |
<FlatWorldGenerator generatorString="3;7,61*1,5*3,2;3;,biome_1"/> | |
<DrawingDecorator> | |
<DrawCuboid x1="-16" y1="67" z1="-1" x2="-16" y2="39" z2="1" type="lava"/> | |
<DrawCuboid x1="-25" y1="67" z1="-2" x2="-29" y2="39" z2="2" type="lava"/> | |
<DrawCuboid x1="-26" y1="67" z1="-1" x2="-28" y2="39" z2="1" type="obsidian"/> | |
<DrawBlock x="-27" y="67" z="0" type="diamond_block"/> | |
</DrawingDecorator> | |
<ServerQuitFromTimeUp timeLimitMs="20000"/> | |
<ServerQuitWhenAnyAgentFinishes/> | |
</ServerHandlers> | |
</ServerSection> | |
<AgentSection mode="Survival"> | |
<Name>MalmoTutorialBot</Name> | |
<AgentStart> | |
<Placement x="0.5" y="68.2" z="0.5" yaw="90"/> | |
<Inventory> | |
<InventoryItem slot="8" type="diamond_pickaxe"/> | |
</Inventory> | |
</AgentStart> | |
<AgentHandlers> | |
<ObservationFromFullStats/> | |
<ObservationFromGrid> | |
<Grid name="floor3x3"> | |
<min x="-1" y="-1" z="-1"/> | |
<max x="1" y="-1" z="1"/> | |
</Grid> | |
</ObservationFromGrid> | |
<ContinuousMovementCommands turnSpeedDegs="180"/> | |
<InventoryCommands/> | |
<AgentQuitFromTouchingBlockType> | |
<Block type="diamond_block" /> | |
</AgentQuitFromTouchingBlockType> | |
</AgentHandlers> | |
</AgentSection> | |
</Mission>''' | |
# Create default Malmo objects: | |
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) | |
my_mission = MalmoPython.MissionSpec(missionXML, True) | |
my_mission_record = MalmoPython.MissionRecordSpec() | |
# Attempt to start a mission: | |
max_retries = 3 | |
for retry in range(max_retries): | |
try: | |
agent_host.startMission( my_mission, my_mission_record ) | |
break | |
except RuntimeError as e: | |
if retry == max_retries - 1: | |
print "Error starting mission:",e | |
exit(1) | |
else: | |
time.sleep(2) | |
# Loop until mission starts: | |
print "Waiting for the mission to start ", | |
world_state = agent_host.getWorldState() | |
while not world_state.is_mission_running: | |
sys.stdout.write(".") | |
time.sleep(0.1) | |
world_state = agent_host.getWorldState() | |
for error in world_state.errors: | |
print "Error:",error.text | |
print "Mission running ", | |
agent_host.sendCommand("move 1") | |
jumping = False | |
# Loop until mission ends: | |
while world_state.is_mission_running: | |
if world_state.number_of_observations_since_last_state > 0: | |
msg = world_state.observations[-1].text | |
observations = json.loads(msg) | |
grid = observations.get(u'floor3x3', 0) | |
if jumping and grid[4]!=u'lava': | |
agent_host.sendCommand("jump 0") | |
jumping = False | |
if grid[3]==u'lava': | |
agent_host.sendCommand("jump 1") | |
jumping = True | |
print "Mission ended" | |
# Mission has ended. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment