Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active July 13, 2016 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jaykul/496334e970818cbbe8e94ef75faa7d7f to your computer and use it in GitHub Desktop.
Save Jaykul/496334e970818cbbe8e94ef75faa7d7f to your computer and use it in GitHub Desktop.
Driving Minecraft from PowerShell
#requires -version 5.0
using assembly MalmoNet.dll
using namespace Microsoft.Research.Malmo
[Environment]::CurrentDirectory = $PSScriptRoot
$ErrorActionPreference = "Stop"
$agentHost = [AgentHost]::new()
$mission =[MissionSpec]::new()
$missionRecord = [MissionRecordSpec]::new("$PSScriptRoot\saved_data.tgz")
$mission.timeLimitInSeconds(10);
$mission.requestVideo( 320, 240 );
$mission.rewardForReachingPosition(19.5,0.0,19.5,100.0,1.1);
$missionRecord.recordCommands();
$missionRecord.recordMP4(20, 400000);
$missionRecord.recordRewards();
$missionRecord.recordObservations();
# May throw ...
$agentHost.startMission($mission, $missionRecord)
Write-Host "Waiting for the mission to start"
do
{
Write-Host ". " -NoNewline
sleep -milli 100
$worldState = $agentHost.getWorldState()
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while (!$worldState.is_mission_running);
$rand = [Random]::new()
do
{
$agentHost.sendCommand("move 1");
$agentHost.sendCommand("turn {0}" -f $rand.NextDouble());
sleep -milli 500
$worldState = $agentHost.getWorldState();
$worldState | Format-Table number* | Out-Host
foreach ($reward in $worldState.rewards) {
Write-Warning ("Summed reward: {0}" -f $reward.getValue())
}
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while ($worldState.is_mission_running);
Write-Host "Mission has stopped."
#requires -version 5.0
using assembly MalmoNet.dll
using namespace Microsoft.Research.Malmo
[Environment]::CurrentDirectory = $PSScriptRoot
$ErrorActionPreference = "Stop"
$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>12500</StartTime>
<AllowPassageOfTime>false</AllowPassageOfTime>
</Time>
<Weather>rain</Weather>
</ServerInitialConditions>
<ServerHandlers>
<FlatWorldGenerator generatorString="3;7,59*1,3*3,2;140;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake"/>
<DrawingDecorator>
<DrawSphere x="-31" y="64" z="0" radius="30" type="air"/>
<!--
<DrawCuboid x1, y1, z1, x2, y2, z2, type/>
<DrawLine x1, y1, z1, x2, y2, z2, type/>
<DrawBlock x, y, z, type/>
<DrawSphere x, y, z, radius, type/>
<DrawItem x, y, z, type/>
-->
</DrawingDecorator>
<ServerQuitFromTimeUp timeLimitMs="30000"/>
<ServerQuitWhenAnyAgentFinishes/>
</ServerHandlers>
</ServerSection>
<AgentSection mode="Survival">
<Name>MalmoTutorialBot</Name>
<AgentStart>
<Placement x="0" y="64" z="0" yaw="90"/>
<Inventory>
<InventoryItem slot="0" type="diamond_pickaxe"/>
</Inventory>
</AgentStart>
<AgentHandlers>
<ObservationFromFullStats/>
<ContinuousMovementCommands turnSpeedDegs="180"/>
</AgentHandlers>
</AgentSection>
</Mission>
'@
$agentHost = [AgentHost]::new()
$mission =[MissionSpec]::new($missionXML, $true)
$mission.timeLimitInSeconds(10);
$mission.requestVideo( 640, 480 );
$mission.rewardForReachingPosition(19.5,0.0,19.5,100.0,1.1);
$missionRecord = [MissionRecordSpec]::new("$PSScriptRoot/missionRecord.tgz")
$missionRecord.recordCommands();
$missionRecord.recordMP4(20, 400000);
$missionRecord.recordRewards();
$missionRecord.recordObservations();
# May throw ...
$agentHost.startMission($mission, $missionRecord)
Write-Host "Waiting for the mission to start"
do
{
Write-Host ". " -NoNewline
sleep -milli 100
$worldState = $agentHost.getWorldState()
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while (!$worldState.is_mission_running);
$rand = [Random]::new()
do
{
sleep -milli 500
$worldState = $agentHost.getWorldState();
$worldState | Select number*
foreach ($reward in $worldState.rewards) {
Write-Warning ("Summed reward: {0}" -f $reward.getValue())
}
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while ($worldState.is_mission_running);
Write-Host "Mission has stopped."
#requires -version 5.0
using assembly MalmoNet.dll
using namespace Microsoft.Research.Malmo
[Environment]::CurrentDirectory = $PSScriptRoot
$ErrorActionPreference = "Stop"
$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>12500</StartTime>
<AllowPassageOfTime>false</AllowPassageOfTime>
</Time>
<Weather>rain</Weather>
</ServerInitialConditions>
<ServerHandlers>
<FlatWorldGenerator generatorString="3;7,59*1,3*3,2;140;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake"/>
<DrawingDecorator>
<DrawSphere x="-31" y="64" z="0" radius="30" type="air"/>
<!--
<DrawCuboid x1, y1, z1, x2, y2, z2, type/>
<DrawLine x1, y1, z1, x2, y2, z2, type/>
<DrawBlock x, y, z, type/>
<DrawSphere x, y, z, radius, type/>
<DrawItem x, y, z, type/>
-->
</DrawingDecorator>
<ServerQuitFromTimeUp timeLimitMs="30000"/>
<ServerQuitWhenAnyAgentFinishes/>
</ServerHandlers>
</ServerSection>
<AgentSection mode="Survival">
<Name>MalmoTutorialBot</Name>
<AgentStart>
<Placement x="0" y="64" z="0" yaw="90"/>
<Inventory>
<InventoryItem slot="0" type="diamond_pickaxe"/>
</Inventory>
</AgentStart>
<AgentHandlers>
<ObservationFromFullStats/>
<ContinuousMovementCommands turnSpeedDegs="180"/>
</AgentHandlers>
</AgentSection>
</Mission>
'@
$agentHost = [AgentHost]::new()
$mission =[MissionSpec]::new($missionXML, $true)
$mission.timeLimitInSeconds(10);
$mission.requestVideo( 640, 480 );
$mission.rewardForReachingPosition(19.5,0.0,19.5,100.0,1.1);
$missionRecord = [MissionRecordSpec]::new("$PSScriptRoot/missionRecord.tgz")
$missionRecord.recordCommands();
$missionRecord.recordMP4(20, 400000);
$missionRecord.recordRewards();
$missionRecord.recordObservations();
# May throw ...
$agentHost.startMission($mission, $missionRecord)
Write-Host "Waiting for the mission to start"
do
{
Write-Host ". " -NoNewline
sleep -milli 100
$worldState = $agentHost.getWorldState()
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while (!$worldState.is_mission_running);
$rand = [Random]::new()
do
{
sleep -milli 100
$worldState = $agentHost.getWorldState();
$worldState | Select number*
foreach ($reward in $worldState.rewards) {
Write-Warning ("Summed reward: {0}" -f $reward.getValue())
}
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while ($worldState.is_mission_running);
Write-Host "Mission has stopped."
$missionRecord.Dispose()
$agentHost.Dispose()
$mission.Dispose()
#requires -version 5.0
using assembly MalmoNet.dll
using namespace Microsoft.Research.Malmo
[Environment]::CurrentDirectory = $PSScriptRoot
$ErrorActionPreference = "Stop"
# Tutorial sample #3: Drawing
function New-Menger {
param([int]$xorg, [int]$yorg, [int]$zorg, [int]$size, $blocktype, $holetype)
#draw solid chunk
New-Cuboid $xorg $yorg $zorg ($xorg+$size-1) ($yorg+$size-1) ($zorg+$size-1) $blocktype
#now remove holes
[int]$unit = $size
while ($unit -ge 3) {
[int]$w=$unit/3
for($i=0;$i -lt $size; $i +=$unit) {
for($j=0;$j -lt $size; $j += $unit) {
$x=$xorg+$i
$y=$yorg+$j
New-Cuboid ($x+$w) ($y+$w) $zorg (($x+2*$w)-1) (($y+2*$w)-1) ($zorg+$size-1) $holetype
$y=$yorg+$i
$z=$zorg+$j
New-Cuboid $xorg ($y+$w) ($z+$w) ($xorg+$size-1) (($y+2*$w)-1) (($z+2*$w)-1) $holetype
New-Cuboid ($x+$w) $yorg ($z+$w) (($x+2*$w)-1) ($yorg+$size-1) (($z+2*$w)-1) $holetype
}
}
$unit/=3
}
}
function New-Cuboid {
param ([int]$x1, [int]$y1, [int]$z1, [int]$x2, [int]$y2, [int]$z2, [string]$blocktype)
"<DrawCuboid x1='$x1' y1='$y1' z1='$z1' x2='$x2' y2='$y2' z2='$z2' type='$blocktype'/>"
}
function New-Letter {
param([int]$x, [int]$y, [int]$z, [char]$char, $blocktype)
$x2 = $x + 2
$y = $y + 10
switch($char) {
'e' {
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-1) ($z-1) $blocktype
New-Cuboid $x ($y-5) $z $x2 ($y-6) ($z-1) $blocktype
New-Cuboid $x ($y-9) $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-1) ($z-1) $blocktype
New-Cuboid $x ($y-9) $z $x2 ($y-10) ($z-1) $blocktype
}
'h' {
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x ($y-5) $z $x2 ($y-6) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
}
'l' {
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x ($y-9) $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x ($y-9) $z $x2 ($y-10) ($z-1) $blocktype
}
'o' {
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-1) ($z-1) $blocktype
New-Cuboid $x ($y-9) $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
}
'p' {
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-1) ($z-1) $blocktype
New-Cuboid $x ($y-5) $z $x2 ($y-6) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-6) ($z-1) $blocktype
}
'r' {
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-1) ($z-1) $blocktype
New-Cuboid $x ($y-5) $z $x2 ($y-7) ($z-1) $blocktype
New-Cuboid $x ($y-8) ($z-1) $x2 ($y-8) ($z-2) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-6) ($z-1) $blocktype
New-Cuboid $x ($y-9) $z $x2 ($y-10) ($z-1) $blocktype
}
's' {
New-Cuboid $x $y $z $x2 ($y-6) ($z-1) $blocktype
New-Cuboid $x ($y-9) $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-1) ($z-1) $blocktype
New-Cuboid $x ($y-5) $z $x2 ($y-6) ($z-1) $blocktype
New-Cuboid $x ($y-9) $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-1) ($z-1) $blocktype
New-Cuboid $x ($y-5) $z $x2 ($y-10) ($z-1) $blocktype
}
'w' {
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x ($y-5) $z $x2 ($y-10) ($z-1) $blocktype
$z = $z - 2
New-Cuboid $x $y $z $x2 ($y-10) ($z-1) $blocktype
}
}
}
function New-Text {
param([int]$x, [int]$y, [int]$z, [string]$text, $blocktype)
foreach($char in [char[]]$text) {
New-Letter $x $y $z $char $blocktype
$z -= 8
}
}
$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 PowerShell!</Summary>
</About>
<ServerSection>
<ServerInitialConditions>
<Time>
<StartTime>12000</StartTime>
<AllowPassageOfTime>false</AllowPassageOfTime>
</Time>
<Weather>clear</Weather>
</ServerInitialConditions>
<ServerHandlers>
<FlatWorldGenerator generatorString="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;"/>
<DrawingDecorator>
<DrawSphere x="-27" y="70" z="0" radius="30" type="air"/>
$((New-Text -27 56 33 "POWERSHELL" "stone") -join "`n")
</DrawingDecorator>
<ServerQuitFromTimeUp timeLimitMs="30000"/>
<ServerQuitWhenAnyAgentFinishes/>
</ServerHandlers>
</ServerSection>
<AgentSection mode="Survival">
<Name>MalmoTutorialBot</Name>
<AgentStart>
<Placement x="0.5" y="56.0" z="0.5" yaw="90"/>
</AgentStart>
<AgentHandlers>
<ObservationFromFullStats/>
<ContinuousMovementCommands turnSpeedDegs="180"/>
</AgentHandlers>
</AgentSection>
</Mission>
"@
Write-Debug $missionXML
# make sure we dispose of things
# It's not automatic when we go out of scope
# and these objects are extremely sensitive to the cwd
# so we can't have them cleaning up later ...
function Clean-Session {
if($missionRecord) {
$missionRecord.Dispose();
$script:missionRecord = $null
remove-variable missionRecord -scope Script -EA Continue
} else {
Write-Warning "No MissionRecordSpec?"
}
if($agentHost) {
$agentHost.Dispose();
$script:agentHost = $null
remove-variable agentHost -scope Script -EA Continue
} else {
Write-Warning "No AgentHost?"
}
if($mission) {
$mission.Dispose();
$script:mission = $null
remove-variable mission -scope Script -EA Continue
} else {
Write-Warning "No Mission?"
}
}
trap {
Clean-Session
throw $_
}
# Create default Malmo objects:
$agentHost = [AgentHost]::new()
try {
$mission =[MissionSpec]::new($missionXML, $true)
} catch [Exception] {
Write-Warning $missionXML
Write-Error $_
}
$mission.timeLimitInSeconds(10);
$mission.requestVideo( 640, 480 );
$mission.rewardForReachingPosition(19.5,0.0,19.5,100.0,1.1);
$missionRecord = [MissionRecordSpec]::new()
# May throw ...
$agentHost.startMission($mission, $missionRecord)
Write-Verbose "Waiting for the mission to start"
$Status = ""
do
{
Write-Progress "Waiting for the mission to start" -Status ($Status += ". ")
sleep -milli 100
$worldState = $agentHost.getWorldState()
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while (!$worldState.is_mission_running);
$rand = [Random]::new()
do
{
sleep -milli 100
$worldState = $agentHost.getWorldState();
$worldState | Select number*
foreach ($reward in $worldState.rewards) {
Write-Warning ("Summed reward: {0}" -f $reward.getValue())
}
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while ($worldState.is_mission_running);
Write-Verbose "Mission has stopped."
. Clean-Session
#requires -version 5.0
using assembly MalmoNet.dll
using namespace Microsoft.Research.Malmo
[Environment]::CurrentDirectory = $PSScriptRoot
$ErrorActionPreference = "Stop"
# Tutorial sample #3: Drawing
function New-Menger {
param([int]$xorg, [int]$yorg, [int]$zorg, [int]$size, $blocktype, $holetype)
#draw solid chunk
New-Cuboid $xorg $yorg $zorg ($xorg+$size-1) ($yorg+$size-1) ($zorg+$size-1) $blocktype
#now remove holes
[int]$unit = $size
while ($unit -ge 3) {
[int]$w=$unit/3
for($i=0;$i -lt $size; $i +=$unit) {
for($j=0;$j -lt $size; $j += $unit) {
$x=$xorg+$i
$y=$yorg+$j
New-Cuboid ($x+$w) ($y+$w) $zorg (($x+2*$w)-1) (($y+2*$w)-1) ($zorg+$size-1) $holetype
$y=$yorg+$i
$z=$zorg+$j
New-Cuboid $xorg ($y+$w) ($z+$w) ($xorg+$size-1) (($y+2*$w)-1) (($z+2*$w)-1) $holetype
New-Cuboid ($x+$w) $yorg ($z+$w) (($x+2*$w)-1) ($yorg+$size-1) (($z+2*$w)-1) $holetype
}
}
$unit/=3
}
}
function New-Cuboid {
param ([int]$x1, [int]$y1, [int]$z1, [int]$x2, [int]$y2, [int]$z2, [string]$blocktype)
"<DrawCuboid x1='$x1' y1='$y1' z1='$z1' x2='$x2' y2='$y2' z2='$z2' type='$blocktype'/>"
}
$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>12000</StartTime>
<AllowPassageOfTime>false</AllowPassageOfTime>
</Time>
<Weather>clear</Weather>
</ServerInitialConditions>
<ServerHandlers>
<FlatWorldGenerator generatorString="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;"/>
<DrawingDecorator>
<DrawSphere x="-27" y="70" z="0" radius="30" type="air"/>
$((New-Menger -40 40 -13 27 "wool" "air") -join "`n")
</DrawingDecorator>
<ServerQuitFromTimeUp timeLimitMs="30000"/>
<ServerQuitWhenAnyAgentFinishes/>
</ServerHandlers>
</ServerSection>
<AgentSection mode="Survival">
<Name>MalmoTutorialBot</Name>
<AgentStart>
<Placement x="0.5" y="56.0" z="0.5" yaw="90"/>
</AgentStart>
<AgentHandlers>
<ObservationFromFullStats/>
<ContinuousMovementCommands turnSpeedDegs="180"/>
</AgentHandlers>
</AgentSection>
</Mission>
"@
# make sure we dispose of things
# It's not automatic when we go out of scope
# and these objects are extremely sensitive to the cwd
# so we can't have them cleaning up later ...
trap {
Write-Warning "Disposing of things"
if($missionRecord) {
$missionRecord.Dispose();
remove-variable missionRecord
}
if($agentHost) {
$agentHost.Dispose();
remove-variable agentHost
}
if($mission) {
$mission.Dispose();
remove-variable mission
}
throw $_
}
# Create default Malmo objects:
$agentHost = [AgentHost]::new()
try {
$mission =[MissionSpec]::new($missionXML, $true)
} catch [Exception] {
Write-Warning $missionXML
Write-Error $_
}
$mission.timeLimitInSeconds(10);
$mission.requestVideo( 640, 480 );
$mission.rewardForReachingPosition(19.5,0.0,19.5,100.0,1.1);
$missionRecord = [MissionRecordSpec]::new()
# May throw ...
$agentHost.startMission($mission, $missionRecord)
Write-Host "Waiting for the mission to start"
do
{
Write-Host ". " -NoNewline
sleep -milli 100
$worldState = $agentHost.getWorldState()
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while (!$worldState.is_mission_running);
$rand = [Random]::new()
do
{
sleep -milli 100
$worldState = $agentHost.getWorldState();
$worldState | Select number*
foreach ($reward in $worldState.rewards) {
Write-Warning ("Summed reward: {0}" -f $reward.getValue())
}
foreach ($err in $worldState.errors) {
Write-Error $err.text
}
}
while ($worldState.is_mission_running);
throw "Mission has stopped." # to make sure we clean up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment