|
| 1 | +import com.microsoft.msr.malmo.*; |
| 2 | + |
| 3 | +import static java.lang.System.loadLibrary; |
| 4 | + |
| 5 | +/** |
| 6 | + * A Java translation of the Python "tutorial_1" example for the Malmo platform made by Microsoft |
| 7 | + */ |
| 8 | +public class Tutorial1 { |
| 9 | + |
| 10 | + static { |
| 11 | + loadLibrary("MalmoJava"); // Load the Malmo JNI |
| 12 | + } |
| 13 | + |
| 14 | + public static void main(String[] argv) { |
| 15 | + AgentHost agentHost = new AgentHost(); |
| 16 | + try { |
| 17 | + StringVector args = new StringVector(); |
| 18 | + args.add("Tutorial1"); |
| 19 | + for(String arg: argv) |
| 20 | + args.add(arg); |
| 21 | + } catch(Exception e) { |
| 22 | + System.out.format("ERROR: %s", e.getMessage()); |
| 23 | + System.out.println(agentHost.getUsage()); |
| 24 | + } |
| 25 | + if(agentHost.receivedArgument("help")) { |
| 26 | + System.out.println(agentHost.getUsage()); |
| 27 | + System.exit(0); |
| 28 | + } |
| 29 | + |
| 30 | + MissionSpec missionSpec = new MissionSpec(); // Initialize our mission |
| 31 | + MissionRecordSpec missionRecordSpec = new MissionRecordSpec("./saved_data.tgz"); // Initialize where we want to store our mission logs |
| 32 | + |
| 33 | + // Try to start the mission |
| 34 | + int maxRetries = 3; // How many times will we try to start our mission |
| 35 | + for(int i=0; i<maxRetries; i++) { |
| 36 | + try { |
| 37 | + // Start the mission - hope it doesn't throw an exception |
| 38 | + agentHost.startMission(missionSpec, missionRecordSpec); |
| 39 | + break; |
| 40 | + } catch(Exception e) { |
| 41 | + // Failed to start the mission since an exception was thrown. |
| 42 | + if(i == maxRetries - 1) { // Failed to start the mission as many times as we defined in "maxRetries" |
| 43 | + System.out.format("ERROR: %s", e.getMessage()); |
| 44 | + System.out.format("Couldn't start the mission after trying %d times. Exiting mission.", maxRetries); |
| 45 | + System.exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + System.out.format("Failed to start the mission after %d/%d attempts. Retrying in 2 seconds.", i+1, maxRetries); |
| 49 | + try { |
| 50 | + Thread.sleep(2000); |
| 51 | + } catch(InterruptedException e2) { |
| 52 | + System.out.format("ERROR: %s", e2.getMessage()); |
| 53 | + System.out.println("Couldn't sleep the thread for two seconds"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Wait for the mission to start |
| 59 | + WorldState worldState = agentHost.getWorldState(); |
| 60 | + while(!worldState.getHasMissionBegun()) { // Wait for the mission to begin |
| 61 | + System.out.print("."); |
| 62 | + try { |
| 63 | + Thread.sleep(100); |
| 64 | + } catch(InterruptedException e) { |
| 65 | + System.out.format("ERROR: %s", e.getMessage()); |
| 66 | + System.out.println("Couldn't sleep the thread for 0.1 second while waiting for the mission to begin"); |
| 67 | + } |
| 68 | + |
| 69 | + worldState = agentHost.getWorldState(); |
| 70 | + for(int i=0; i<worldState.getErrors().size(); i++) { |
| 71 | + System.out.format("ERROR: "+worldState.getErrors().get(i)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Log that the mission started |
| 76 | + System.out.println("\nMission started"); |
| 77 | + |
| 78 | + // Do something. In our case, we do nothing for 10 seconds. |
| 79 | + while(worldState.getIsMissionRunning()) { |
| 80 | + System.out.print("."); |
| 81 | + |
| 82 | + try { |
| 83 | + Thread.sleep(100); |
| 84 | + } catch(InterruptedException e) { |
| 85 | + System.out.format("ERROR: %s", e.getMessage()); |
| 86 | + System.out.println("Couldn't sleep the thread for 0.1 second while the mission's executing"); |
| 87 | + } |
| 88 | + |
| 89 | + worldState = agentHost.getWorldState(); |
| 90 | + for(int i=0; i<worldState.getErrors().size(); i++) { |
| 91 | + System.out.format("ERROR: "+worldState.getErrors().get(i)); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + System.out.println("\n\nMission has ended."); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments