Example #1
0
        private async void addB_Click(object sender, RoutedEventArgs e)
        {
            var ofd = new Win32.OpenFileDialog();

            ofd.Filter = "Mission Files (*.xml)|*.xml";

            if (ofd.ShowDialog(this) == true)
            {
                using (StreamReader reader = new StreamReader(ofd.FileName))
                {
                    try
                    {
                        MissionSpec mission = new MissionSpec(await reader.ReadToEndAsync(), true);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error Parsing Mission", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }

                this.addItem(ofd.FileName);

                this.updateButtons();
            }
        }
Example #2
0
        private async void addB_Click(object sender, RoutedEventArgs e)
        {
            var ofd = new Win32.OpenFileDialog();
            ofd.Filter = "Mission Files (*.xml)|*.xml";

            if (ofd.ShowDialog(this) == true)
            {
                using (StreamReader reader = new StreamReader(ofd.FileName))
                {
                    try
                    {
                        MissionSpec mission = new MissionSpec(await reader.ReadToEndAsync(), true);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error Parsing Mission", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }

                this.addItem(ofd.FileName);

                this.updateButtons();
            }
        }
Example #3
0
        private async void openMI_Click(object sender, RoutedEventArgs e)
        {
            var ofd = new Win32.OpenFileDialog();

            ofd.Filter = "Mission Files (*.xml)|*.xml";

            if (ofd.ShowDialog(this) == true)
            {
                MissionSpec mission = await loadMission(ofd.FileName);

                if (mission != null)
                {
                    _score = 0;

                    openMI.IsEnabled = false;
                    startB.IsEnabled = false;
                    startB.Content   = "Ready";
                    await Task.Run(() => runMission(mission));

                    startB.IsEnabled = true;
                    startB.Content   = "Start";
                    openMI.IsEnabled = true;
                }
            }
        }
Example #4
0
        private void InitializeMission()
        {
            string missionXMLpath = System.IO.File.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + "myworld.xml");

            mission = new MissionSpec(missionXMLpath, false);
            AddBlocks(mission);
            mission.setModeToCreative();
        }
Example #5
0
        private static void InitMissionSpecs()
        {
            var missionXmlDefinition = File.ReadAllText("SampleMission.xml");

            _mission = new MissionSpec(missionXmlDefinition, false);
            _mission.requestVideo(640, 480);

            var recordedFileName = $"./mission_data{DateTime.Now:hh mm ss}.tgz";

            _missionRecord = new MissionRecordSpec(recordedFileName);
            _missionRecord.recordCommands();
            _missionRecord.recordMP4(20, 400000);
            _missionRecord.recordRewards();
            _missionRecord.recordObservations();
        }
Example #6
0
    public static void Main(string[] args)
    {
        MissionSpec my_mission = new MissionSpec();

        my_mission.timeLimitInSeconds(10);
        my_mission.drawBlock(19, 0, 19, "redstone_block");
        my_mission.createDefaultTerrain();
        my_mission.setTimeOfDay(6000, false);
        my_mission.drawCuboid(50, 0, 50, 100, 10, 100, "redstone_block");
        my_mission.drawItem(3, 0, 2, "diamond_pickaxe");
        my_mission.drawSphere(50, 10, 50, 10, "ice");
        my_mission.drawLine(50, 20, 50, 100, 20, 100, "redstone_block");
        my_mission.startAt(2.5f, 0.0f, 2.5f);
        my_mission.endAt(19.5f, 0.0f, 19.5f, 1.0f);
        my_mission.requestVideo(320, 240);
        my_mission.setModeToCreative();
        my_mission.rewardForReachingPosition(19.5f, 0.0f, 19.5f, 100.0f, 1.1f);
        my_mission.observeRecentCommands();
        my_mission.observeHotBar();
        my_mission.observeFullInventory();
        my_mission.observeGrid(-2, 0, -2, 2, 1, 2, "Cells");
        my_mission.observeDistance(19.5f, 0.0f, 19.5f, "Goal");
        my_mission.allowAllDiscreteMovementCommands();

        // check that the XML we produce validates
        bool   pretty_print = false;
        string xml          = my_mission.getAsXML(pretty_print);

        try
        {
            bool        validate    = true;
            MissionSpec my_mission2 = new MissionSpec(xml, validate);

            // check that we get the same XML if we go round again
            string xml2 = my_mission2.getAsXML(pretty_print);
            if (xml2 != xml)
            {
                Console.WriteLine("Mismatch between first generation XML and the second.");
                Environment.Exit(1);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error validating the XML we generated: {0}", e);
            Environment.Exit(1);
        }
    }
Example #7
0
        private async Task <MissionSpec> loadMission(string path)
        {
            using (StreamReader reader = new StreamReader(path))
            {
                try
                {
                    string xml = await reader.ReadToEndAsync();

                    xml = xml.Replace("__SEED__", _rand.Next().ToString());
                    MissionSpec mission = new MissionSpec(xml, true);

                    if (!mission.isVideoRequested(0))
                    {
                        mission.requestVideo(Settings.Default.VideoWidth, Settings.Default.VideoHeight);
                    }

                    mission.removeAllCommandHandlers();
                    mission.allowAllContinuousMovementCommands();
                    mission.allowAllDiscreteMovementCommands();

                    XDocument  xdoc       = XDocument.Parse(xml);
                    XNamespace malmo      = "http://ProjectMalmo.microsoft.com";
                    var        timeUpNode = xdoc.Descendants(malmo + "ServerQuitFromTimeUp").FirstOrDefault();
                    if (timeUpNode != null)
                    {
                        _missionDuration = TimeSpan.FromMilliseconds(double.Parse(timeUpNode.Attribute("timeLimitMs").Value));
                    }
                    else
                    {
                        _missionDuration = TimeSpan.Zero;
                    }

                    if (_recordWindow != null && _recordWindow.IsVisible)
                    {
                        _recordWindow.MissionPath = path;
                    }

                    return(mission);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error Parsing Mission", MessageBoxButton.OK, MessageBoxImage.Error);
                    return(null);
                }
            }
        }
Example #8
0
    public static void Main(string[] args)
    {
        MissionSpec my_mission = new MissionSpec();
        my_mission.timeLimitInSeconds( 10 );
        my_mission.drawBlock( 19, 0, 19, "redstone_block" );
        my_mission.createDefaultTerrain();
        my_mission.setTimeOfDay(6000,false);
        my_mission.drawCuboid(50,0,50,100,10,100,"redstone_block");
        my_mission.drawItem(3,0,2,"diamond_pickaxe");
        my_mission.drawSphere(50,10,50,10,"ice");
        my_mission.drawLine(50,20,50,100,20,100,"redstone_block");
        my_mission.startAt( 2.5f, 0.0f, 2.5f );
        my_mission.endAt( 19.5f, 0.0f, 19.5f, 1.0f );
        my_mission.requestVideo( 320, 240 );
        my_mission.setModeToCreative();
        my_mission.rewardForReachingPosition(19.5f,0.0f,19.5f,100.0f,1.1f);
        my_mission.observeRecentCommands();
        my_mission.observeHotBar();
        my_mission.observeFullInventory();
        my_mission.observeGrid(-2,0,-2,2,1,2,"Cells");
        my_mission.observeDistance(19.5f,0.0f,19.5f,"Goal");
        my_mission.allowAllDiscreteMovementCommands();

        // check that the XML we produce validates
        bool pretty_print = false;
        string xml = my_mission.getAsXML( pretty_print );
        try
        {
            bool validate = true;
            MissionSpec my_mission2 = new MissionSpec( xml, validate );

            // check that we get the same XML if we go round again
            string xml2 = my_mission2.getAsXML( pretty_print );
            if( xml2 != xml )
            {
                Console.WriteLine("Mismatch between first generation XML and the second.");
                Environment.Exit(1);
            }
        }
        catch( Exception e )
        {
            Console.WriteLine("Error validating the XML we generated: {0}", e);
            Environment.Exit(1);
        }
    }
Example #9
0
        private static void LoadMinecraft(AgentHost agentHost, string XMLFile, ListBox lstMessage)
        {
            String strXMLFileContents = "";

            using (StreamReader sr = new StreamReader(XMLFile))
            {
                // Read the stream to a string, and write the string to the console.
                strXMLFileContents = sr.ReadToEnd();
            }

            MissionSpec mission = new MissionSpec(strXMLFileContents, false);

            mission.requestVideo(640, 480);

            MissionRecordSpec missionRecord = new MissionRecordSpec("./saved_data.tgz");

            missionRecord.recordCommands();
            missionRecord.recordMP4(20, 400000);
            missionRecord.recordRewards();
            missionRecord.recordObservations();

            try
            {
                agentHost.startMission(mission, missionRecord);
            }
            catch (Exception ex)
            {
                lstMessage.Items.Insert(0, String.Format("Error starting mission: {0}", ex.Message));
                Environment.Exit(1);
            }

            WorldState worldState;

            lstMessage.Items.Insert(0, "Waiting for the mission to start");
            do
            {
                Thread.Sleep(100);
                worldState = agentHost.getWorldState();

                foreach (TimestampedString error in worldState.errors)
                {
                    lstMessage.Items.Insert(0, String.Format("Error: {0}", error.text));
                }
            }while (!worldState.has_mission_begun);
        }
Example #10
0
        private void AddBlocks(MissionSpec mission)
        {
            int x1 = -1;

            for (int z1 = -1; z1 < 20; z1++)
            {
                for (int y = 227; y <= 227 + 10; y++)
                {
                    mission.drawBlock(x1, y, z1, "cobblestone");
                }
            }

            int z2 = -1;

            for (int x2 = -1; x2 < 20; x2++)
            {
                for (int y = 227; y <= 227 + 10; y++)
                {
                    mission.drawBlock(x2, y, z2, "cobblestone");
                }
            }

            int x3 = 20;

            for (int z3 = 0; z3 < 20; z3++)
            {
                for (int y = 227; y < 227 + 10; y++)
                {
                    mission.drawBlock(x3, y, z3, "cobblestone");
                }
            }

            int z4 = 20;

            for (int x4 = 0; x4 < 20; x4++)
            {
                for (int y = 227; y < 227 + 10; y++)
                {
                    mission.drawBlock(x4, y, z4, "cobblestone");
                }
            }
        }
Example #11
0
        void InitializeMission()
        {
            string missionXMLpath = SharpNeat.PopulationReadWrite.GetEvolutionFolderPath();

            missionXMLpath += "minecraftWorldXML.txt";
            // We need to avoid trouble if the file gets simultaneus access demands
            string rawMissionXML = SaferRead(missionXMLpath);

            // This is a valid alternative, but changes in the mission xml will require compiling...
            //string rawMissionXML = Evolution.Malmo.RawXMLmissionFactory.xmlMission;
            try
            {
                // Hopefully this minimizes parallelization errors:
                // 'System.AccessViolationException' in MalmoNET.dll
                // Still, if it happens, this will require a server reset...
                RandomWait();
                mission = new MissionSpec(rawMissionXML, true);
            }
            catch (Exception ex)
            {
                string errorLine = "Fatal error when starting a mission in ProgramMalmo: " + ex.Message;
                SharpNeat.PopulationReadWrite.WriteErrorForDebug(errorLine);
                System.Diagnostics.Debug.WriteLine("\nFatal error when starting a mission in ProgramMalmo: " + ex.Message);
                Environment.Exit(1);
            }
            mission.timeLimitInSeconds(10);
            //mission.forceWorldReset();
            AddProceduralDecoration();
            string savePath = MakeSavePath();

            missionRecord = new MissionRecordSpec(savePath);
            //missionRecord = new MissionRecordSpec();
            missionRecord.recordCommands();
            missionRecord.recordMP4(30, 400000);
            missionRecord.recordRewards();
            missionRecord.recordObservations();
        }
Example #12
0
        private async void startB_Click(object sender, RoutedEventArgs e)
        {
            if (!startB.IsEnabled)
            {
                return;
            }

            startB.IsEnabled = false;

            _score = 0;
            int index = 0;

            for (int i = 0; i < _curriculumItemPaths.Length; i++)
            {
                startB.Content = "Ready";
                MissionSpec spec = await loadMission(_curriculumItemPaths[index]);

                if (spec != null)
                {
                    await Task.Run(() => runMission(spec));
                }

                if (_missionSuccess)
                {
                    index++;
                }
            }

            if (_highScoreWindow != null && _highScoreWindow.IsVisible)
            {
                _highScoreWindow.AddScore((int)_score);
            }

            startB.IsEnabled = true;
            startB.Content   = "Start";
        }
Example #13
0
    public static void Main()
    {
        AgentHost agentHost = new AgentHost();

        try
        {
            agentHost.parse(new StringVector(Environment.GetCommandLineArgs()));
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine("ERROR: {0}", ex.Message);
            Console.Error.WriteLine(agentHost.getUsage());
            Environment.Exit(1);
        }
        if (agentHost.receivedArgument("help"))
        {
            Console.Error.WriteLine(agentHost.getUsage());
            Environment.Exit(0);
        }

        MissionSpec mission = new MissionSpec();

        mission.timeLimitInSeconds(10);
        mission.requestVideo(320, 240);
        mission.rewardForReachingPosition(19.5f, 0.0f, 19.5f, 100.0f, 1.1f);

        MissionRecordSpec missionRecord = new MissionRecordSpec("./saved_data.tgz");

        missionRecord.recordCommands();
        missionRecord.recordMP4(20, 400000);
        missionRecord.recordRewards();
        missionRecord.recordObservations();

        try
        {
            agentHost.startMission(mission, missionRecord);
        }
        catch (MissionException ex)
        {
            // Using catch(Exception ex) would also work, but specifying MissionException allows
            // us to access the error code:
            Console.Error.WriteLine("Error starting mission: {0}", ex.Message);
            Console.Error.WriteLine("Error code: {0}", ex.getMissionErrorCode());
            // We can do more specific error handling using this code, eg:
            if (ex.getMissionErrorCode() == MissionException.MissionErrorCode.MISSION_INSUFFICIENT_CLIENTS_AVAILABLE)
            {
                Console.Error.WriteLine("Have you started a Minecraft client?");
            }
            Environment.Exit(1);
        }

        WorldState worldState;

        Console.WriteLine("Waiting for the mission to start");
        do
        {
            Console.Write(".");
            Thread.Sleep(100);
            worldState = agentHost.getWorldState();

            foreach (TimestampedString error in worldState.errors)
            {
                Console.Error.WriteLine("Error: {0}", error.text);
            }
        }while (!worldState.has_mission_begun);

        Console.WriteLine();

        Random rand = new Random();

        // main loop:
        do
        {
            agentHost.sendCommand("move 1");
            agentHost.sendCommand(string.Format("turn {0}", rand.NextDouble()));
            Thread.Sleep(500);
            worldState = agentHost.getWorldState();
            Console.WriteLine(
                "video,observations,rewards received: {0}, {1}, {2}",
                worldState.number_of_video_frames_since_last_state,
                worldState.number_of_observations_since_last_state,
                worldState.number_of_rewards_since_last_state);
            foreach (TimestampedReward reward in worldState.rewards)
            {
                Console.Error.WriteLine("Summed reward: {0}", reward.getValue());
            }
            foreach (TimestampedString error in worldState.errors)
            {
                Console.Error.WriteLine("Error: {0}", error.text);
            }
        }while (worldState.is_mission_running);

        Console.WriteLine("Mission has stopped.");
    }
Example #14
0
    public static void Main()
    {
        AgentHost agentHost = new AgentHost();

        try
        {
            agentHost.parse(new StringVector(Environment.GetCommandLineArgs()));
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine("ERROR: {0}", ex.Message);
            Console.Error.WriteLine(agentHost.getUsage());
            Environment.Exit(1);
        }
        if (agentHost.receivedArgument("help"))
        {
            Console.Error.WriteLine(agentHost.getUsage());
            Environment.Exit(0);
        }

        MissionSpec mission = new MissionSpec();

        mission.timeLimitInSeconds(10);
        mission.requestVideo(320, 240);
        mission.rewardForReachingPosition(19.5f, 0.0f, 19.5f, 100.0f, 1.1f);

        MissionRecordSpec missionRecord = new MissionRecordSpec("./saved_data.tgz");

        missionRecord.recordCommands();
        missionRecord.recordMP4(20, 400000);
        missionRecord.recordRewards();
        missionRecord.recordObservations();

        try
        {
            agentHost.startMission(mission, missionRecord);
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine("Error starting mission: {0}", ex.Message);
            Environment.Exit(1);
        }

        WorldState worldState;

        Console.WriteLine("Waiting for the mission to start");
        do
        {
            Console.Write(".");
            Thread.Sleep(100);
            worldState = agentHost.getWorldState();

            foreach (TimestampedString error in worldState.errors)
            {
                Console.Error.WriteLine("Error: {0}", error.text);
            }
        }while (!worldState.is_mission_running);

        Console.WriteLine();

        Random rand = new Random();

        // main loop:
        do
        {
            agentHost.sendCommand("move 1");
            agentHost.sendCommand(string.Format("turn {0}", rand.NextDouble()));
            Thread.Sleep(500);
            worldState = agentHost.getWorldState();
            Console.WriteLine(
                "video,observations,rewards received: {0}, {1}, {2}",
                worldState.number_of_video_frames_since_last_state,
                worldState.number_of_observations_since_last_state,
                worldState.number_of_rewards_since_last_state);
            foreach (TimestampedReward reward in worldState.rewards)
            {
                Console.Error.WriteLine("Summed reward: {0}", reward.getValue());
            }
            foreach (TimestampedString error in worldState.errors)
            {
                Console.Error.WriteLine("Error: {0}", error.text);
            }
        }while (worldState.is_mission_running);

        Console.WriteLine("Mission has stopped.");
    }
Example #15
0
        private void runMission(MissionSpec mission)
        {
            string            recordPath = "none";
            MissionRecordSpec missionRecord;

            if (string.IsNullOrEmpty(Settings.Default.OutputDir))
            {
                missionRecord = new MissionRecordSpec();
            }
            else
            {
                recordPath    = Path.Combine(Settings.Default.OutputDir, Guid.NewGuid() + ".tar.gz");
                missionRecord = new MissionRecordSpec(recordPath);
                if (Settings.Default.RecordObservations)
                {
                    missionRecord.recordObservations();
                }

                if (Settings.Default.RecordRewards)
                {
                    missionRecord.recordRewards();
                }

                if (Settings.Default.RecordCommands)
                {
                    missionRecord.recordCommands();
                }

                if (Settings.Default.RecordMP4)
                {
                    missionRecord.recordMP4(Settings.Default.MP4FPS, (long)(Settings.Default.BitRate * 1024));
                }
            }

            using (AgentHost agentHost = new AgentHost())
            {
                ClientPool clientPool = new ClientPool();
                clientPool.add(new ClientInfo(Settings.Default.ClientIP, Settings.Default.ClientPort));

                try
                {
                    agentHost.startMission(mission, clientPool, missionRecord, 0, "hac");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error Starting Mission", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                Dispatcher.Invoke(() =>
                {
                    Activate();
                    VideoImage.Focus();
                    startB.Content = "Set";
                    Title          = Settings.Default.Title + " : Waiting for mission start";
                    if (_recordWindow != null && _recordWindow.IsVisible)
                    {
                        _recordWindow.RecordPath = recordPath;
                    }
                });

                _isInSession = true;
                try
                {
                    WorldState worldState;
                    // wait for mission to start
                    do
                    {
                        Thread.Sleep(100);
                        worldState = agentHost.getWorldState();

                        if (worldState.errors.Any())
                        {
                            StringBuilder errors = new StringBuilder();
                            foreach (TimestampedString error in worldState.errors)
                            {
                                errors.AppendLine(error.text);
                            }

                            MessageBox.Show(errors.ToString(), "Error during mission initialization", MessageBoxButton.OK, MessageBoxImage.Error);
                            return;
                        }
                    }while (!worldState.is_mission_running && !_stopMission);

                    if (_stopMission)
                    {
                        return;
                    }

                    DateTime missionStartTime = DateTime.UtcNow;
                    _timeRemaining = _missionDuration;

                    Dispatcher.Invoke(() =>
                    {
                        startB.Content = "Go!";
                        Title          = Settings.Default.Title + " : Recording";
                    });

                    // run mission
                    TimeSpan loopTime = TimeSpan.FromSeconds(1.0 / 20);

                    _pendingCommandsMutex.Wait();
                    try
                    {
                        _pendingCommandQueue = new Queue <Tuple <string, float> >();
                    }
                    finally
                    {
                        _pendingCommandsMutex.Release();
                    }

                    _pendingMessagesMutex.Wait();
                    try
                    {
                        _pendingMessages = new Queue <string>();
                    }
                    finally
                    {
                        _pendingMessagesMutex.Release();
                    }

                    bool      failure   = false;
                    Stopwatch loopTimer = new Stopwatch();
                    do
                    {
                        loopTimer.Reset();
                        loopTimer.Start();

                        worldState = agentHost.getWorldState();

                        TimestampedVideoFrame frame = worldState.video_frames.FirstOrDefault();

                        if (frame != null)
                        {
                            if (_missionDuration != TimeSpan.Zero)
                            {
                                TimeSpan elapsed = frame.timestamp - missionStartTime;
                                _timeRemaining = _missionDuration - elapsed;
                            }

                            _pixelsMutex.Wait();
                            try
                            {
                                if (_pixels == null || _pixels.Length != frame.pixels.Count)
                                {
                                    _pixels = new byte[frame.pixels.Count];
                                    Dispatcher.Invoke(() =>
                                    {
                                        if (_bitmap.Width != frame.width || _bitmap.Height != frame.height)
                                        {
                                            _bitmap           = new WriteableBitmap(frame.width, frame.height, 72, 72, PixelFormats.Rgb24, null);
                                            VideoImage.Source = _bitmap;
                                            if (_recordWindow != null && _recordWindow.IsVisible)
                                            {
                                                _recordWindow.FrameSource = _bitmap;
                                            }
                                        }
                                    });
                                }

                                frame.pixels.CopyTo(_pixels);
                            }
                            finally
                            {
                                _pixelsMutex.Release();
                            }
                        }

                        _pendingMessagesMutex.Wait();
                        try
                        {
                            foreach (var reward in worldState.rewards)
                            {
                                _score += reward.getValue();
                                if (reward.getValue() < 0)
                                {
                                    failure = true;
                                }

                                _pendingMessages.Enqueue(string.Format("{0}> score {1}", reward.timestamp.ToString("hh:mm:ss.fff"), reward.getValue()));
                            }

                            _score = Math.Max(_score, 0);
                            _score = Math.Min(_score, 99999);

                            foreach (var observation in worldState.observations)
                            {
                                int posStart = observation.text.IndexOf("\"XPos\"");
                                if (posStart < 0)
                                {
                                    continue;
                                }

                                int posEnd = observation.text.IndexOf("\"ZPos\"");
                                posEnd = observation.text.IndexOf(',', posEnd);

                                string   posSegment = observation.text.Substring(posStart, posEnd - posStart);
                                string[] pos        = posSegment.Split(',');
                                float    x          = Convert.ToSingle(pos[0].Split(':')[1]);
                                float    y          = Convert.ToSingle(pos[1].Split(':')[1]);
                                float    z          = Convert.ToSingle(pos[2].Split(':')[1]);

                                _pendingMessages.Enqueue(string.Format("{0}> (x={1:0.00}, y={2:0.00}, z={3:0.00})", observation.timestamp.ToString("hh:mm:ss.fff"), x, y, z));
                            }
                        }
                        finally
                        {
                            _pendingMessagesMutex.Release();
                        }

                        CheckGamepad(agentHost);

                        _pendingCommandsMutex.Wait();
                        try
                        {
                            while (_pendingCommandQueue.Any())
                            {
                                var command = _pendingCommandQueue.Dequeue();
                                CheckAndSend(agentHost, command.Item1, command.Item2);
                            }
                        }
                        finally
                        {
                            _pendingCommandsMutex.Release();
                        }

                        loopTimer.Stop();
                        if (loopTimer.Elapsed < loopTime)
                        {
                            Thread.Sleep(loopTime - loopTimer.Elapsed);
                        }
                    } while (worldState.is_mission_running && !_stopMission);

                    if (_stopMission)
                    {
                        return;
                    }

                    if (!failure)
                    {
                        _score += _timeRemaining.TotalSeconds * 100;
                    }

                    _missionSuccess = !failure;

                    _pendingCommandsMutex.Wait();
                    try
                    {
                        _pendingCommandQueue = null;
                    }
                    finally
                    {
                        _pendingCommandsMutex.Release();
                    }

                    Dispatcher.Invoke(() =>
                    {
                        Title = Settings.Default.Title + " : Ready";
                        UpdateDisplayedMessages();
                        UpdateDisplayedReward();
                    });

                    _resetVerb = null;

                    var keys = _continuousCommandState.Keys.ToList();
                    foreach (var verb in keys)
                    {
                        _continuousCommandState[verb] = 0;
                    }

                    keys = _discreteCommandState.Keys.ToList();
                    foreach (var verb in keys)
                    {
                        _discreteCommandState[verb] = false;
                    }

                    if (worldState.errors.Any())
                    {
                        StringBuilder errors = new StringBuilder();
                        foreach (TimestampedString error in worldState.errors)
                        {
                            errors.AppendLine(error.text);
                        }

                        MessageBox.Show(errors.ToString(), "Error during mission initialization", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error during mission", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                finally
                {
                    _isInSession = false;
                }
            }
        }
Example #16
0
    public static void Main(string[] args)
    {
        MissionSpec my_mission = new MissionSpec();

        my_mission.setSummary("example mission");
        my_mission.timeLimitInSeconds(10);
        my_mission.drawBlock(19, 0, 19, "redstone_block");
        my_mission.createDefaultTerrain();
        my_mission.setTimeOfDay(6000, false);
        my_mission.drawCuboid(50, 0, 50, 100, 10, 100, "redstone_block");
        my_mission.drawItem(3, 0, 2, "diamond_pickaxe");
        my_mission.drawSphere(50, 10, 50, 10, "ice");
        my_mission.drawLine(50, 20, 50, 100, 20, 100, "redstone_block");
        my_mission.startAt(2.5f, 0.0f, 2.5f);
        my_mission.endAt(19.5f, 0.0f, 19.5f, 1.0f);
        my_mission.requestVideo(320, 240);
        my_mission.setModeToCreative();
        my_mission.rewardForReachingPosition(19.5f, 0.0f, 19.5f, 100.0f, 1.1f);
        my_mission.observeRecentCommands();
        my_mission.observeHotBar();
        my_mission.observeFullInventory();
        my_mission.observeGrid(-2, 0, -2, 2, 1, 2, "Cells");
        my_mission.observeDistance(19.5f, 0.0f, 19.5f, "Goal");
        my_mission.removeAllCommandHandlers();
        my_mission.allowContinuousMovementCommand("move");
        my_mission.allowContinuousMovementCommand("strafe");
        my_mission.allowDiscreteMovementCommand("movenorth");
        my_mission.allowInventoryCommand("swapInventoryItems");

        if (my_mission.getSummary() != "example mission")
        {
            Console.WriteLine("Unexpected summary");
            Environment.Exit(1);
        }

        string[] expected_command_handlers = { "ContinuousMovement", "DiscreteMovement", "Inventory" };
        string[] actual_command_handlers   = new List <string>(my_mission.getListOfCommandHandlers(0)).ToArray();
        if (actual_command_handlers.Length != expected_command_handlers.Length)
        {
            Console.WriteLine("Number of command handlers mismatch");
            Environment.Exit(1);
        }
        for (int i = 0; i < actual_command_handlers.Length; i++)
        {
            if (!actual_command_handlers[i].Equals(expected_command_handlers[i]))
            {
                Console.WriteLine("Unexpected command handler: {0}", actual_command_handlers[i]);
                Environment.Exit(1);
            }
        }

        string[] expected_continuous_commands = { "move", "strafe" };
        string[] actual_continuous_commands   = new List <string>(my_mission.getAllowedCommands(0, "ContinuousMovement")).ToArray();
        if (actual_continuous_commands.Length != expected_continuous_commands.Length)
        {
            Console.WriteLine("Number of continuous commands mismatch");
            Environment.Exit(1);
        }
        for (int i = 0; i < actual_continuous_commands.Length; i++)
        {
            if (!actual_continuous_commands[i].Equals(expected_continuous_commands[i]))
            {
                Console.WriteLine("Unexpected continuous command: {0}", actual_continuous_commands[i]);
                Environment.Exit(1);
            }
        }

        string[] expected_discrete_commands = { "movenorth" };
        string[] actual_discrete_commands   = new List <string>(my_mission.getAllowedCommands(0, "DiscreteMovement")).ToArray();
        if (actual_discrete_commands.Length != expected_discrete_commands.Length)
        {
            Console.WriteLine("Number of discrete commands mismatch");
            Environment.Exit(1);
        }
        for (int i = 0; i < actual_discrete_commands.Length; i++)
        {
            if (!actual_discrete_commands[i].Equals(expected_discrete_commands[i]))
            {
                Console.WriteLine("Unexpected discrete command: {0}", actual_discrete_commands[i]);
                Environment.Exit(1);
            }
        }

        string[] expected_inventory_commands = { "swapInventoryItems" };
        string[] actual_inventory_commands   = new List <string>(my_mission.getAllowedCommands(0, "Inventory")).ToArray();
        if (actual_inventory_commands.Length != expected_inventory_commands.Length)
        {
            Console.WriteLine("Number of commands mismatch");
            Environment.Exit(1);
        }
        for (int i = 0; i < actual_inventory_commands.Length; i++)
        {
            if (!actual_inventory_commands[i].Equals(expected_inventory_commands[i]))
            {
                Console.WriteLine("Unexpected command: {0}", actual_inventory_commands[i]);
                Environment.Exit(1);
            }
        }

        // check that the XML we produce validates
        bool   pretty_print = false;
        string xml          = my_mission.getAsXML(pretty_print);

        try
        {
            bool        validate    = true;
            MissionSpec my_mission2 = new MissionSpec(xml, validate);

            // check that we get the same XML if we go round again
            string xml2 = my_mission2.getAsXML(pretty_print);
            if (xml2 != xml)
            {
                Console.WriteLine("Mismatch between first generation XML and the second.");
                Environment.Exit(1);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error validating the XML we generated: {0}", e);
            Environment.Exit(1);
        }

        // check that known-good XML validates
        const string xml3 = @"<?xml version=""1.0"" encoding=""UTF-8"" ?><Mission xmlns=""http://ProjectMalmo.microsoft.com"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
            <About><Summary>Run the maze!</Summary></About>
            <ServerSection><ServerInitialConditions><AllowSpawning>true</AllowSpawning><Time><StartTime>1000</StartTime><AllowPassageOfTime>true</AllowPassageOfTime></Time><Weather>clear</Weather></ServerInitialConditions>
            <ServerHandlers>
            <FlatWorldGenerator generatorString=""3;7,220*1,5*3,2;3;,biome_1"" />
            <ServerQuitFromTimeUp timeLimitMs=""20000"" />
            <ServerQuitWhenAnyAgentFinishes />
            </ServerHandlers></ServerSection>
            <AgentSection><Name>Jason Bourne</Name><AgentStart><Placement x=""-204"" y=""81"" z=""217""/></AgentStart><AgentHandlers>
            <VideoProducer want_depth=""true""><Width>320</Width><Height>240</Height></VideoProducer>
            <RewardForReachingPosition><Marker reward=""100"" tolerance=""1.1"" x=""-104"" y=""81"" z=""217""/></RewardForReachingPosition>
            <ContinuousMovementCommands><ModifierList type=""deny-list""><command>attack</command><command>crouch</command></ModifierList></ContinuousMovementCommands>
            <AgentQuitFromReachingPosition><Marker x=""-104"" y=""81"" z=""217""/></AgentQuitFromReachingPosition>
            </AgentHandlers></AgentSection></Mission>";

        try
        {
            const bool  validate    = true;
            MissionSpec my_mission3 = new MissionSpec(xml3, validate);

            if (my_mission3.getSummary() != "Run the maze!")
            {
                Console.WriteLine("Unexpected summary");
                Environment.Exit(1);
            }

            string[] expected_command_handlers2 = { "ContinuousMovement" };
            string[] actual_command_handlers2   = new List <string>(my_mission3.getListOfCommandHandlers(0)).ToArray();
            if (actual_command_handlers2.Length != expected_command_handlers2.Length)
            {
                Console.WriteLine("Number of command handlers mismatch");
                Environment.Exit(1);
            }
            for (int i = 0; i < actual_command_handlers2.Length; i++)
            {
                if (!actual_command_handlers2[i].Equals(expected_command_handlers2[i]))
                {
                    Console.WriteLine("Unexpected command handler: {0}", actual_command_handlers2[i]);
                    Environment.Exit(1);
                }
            }

            string[] expected_continuous_commands2 = { "jump", "move", "pitch", "strafe", "turn", "use" };
            string[] actual_continuous_commands2   = new List <string>(my_mission3.getAllowedCommands(0, "ContinuousMovement")).ToArray();
            if (actual_continuous_commands2.Length != expected_continuous_commands2.Length)
            {
                Console.WriteLine("Number of continuous commands mismatch");
                Environment.Exit(1);
            }
            for (int i = 0; i < actual_continuous_commands2.Length; i++)
            {
                if (!actual_continuous_commands2[i].Equals(expected_continuous_commands2[i]))
                {
                    Console.WriteLine("Unexpected continuous command: {0}", actual_continuous_commands2[i]);
                    Environment.Exit(1);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error validating known-good XML: {0}", e);
            Environment.Exit(1);
        }
    }
Example #17
0
        public static void Main()
        {
            AgentHost agentHost = new AgentHost();

            try
            {
                agentHost.parse(new StringVector(Environment.GetCommandLineArgs()));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("ERROR: {0}", ex.Message);
                Console.Error.WriteLine(agentHost.getUsage());
                Environment.Exit(1);
            }
            if (agentHost.receivedArgument("help"))
            {
                Console.Error.WriteLine(agentHost.getUsage());
                Environment.Exit(0);
            }
            MissionSpec       my_mission        = new MissionSpec();
            MissionRecordSpec my_mission_record = new MissionRecordSpec("./saved_data.tgz");

            // Attempt to start a mission:
            int max_retries = 3;

            for (int retry = 0; retry < max_retries; retry++)
            {
                try
                {
                    agentHost.startMission(my_mission, my_mission_record);
                    break;
                }
                catch (MissionException ex)
                {
                    if (retry == max_retries - 1)
                    {
                        Console.WriteLine("Error starting mission:{0}", ex.Message);
                        Environment.Exit(1);
                    }
                    else
                    {
                        Thread.Sleep(2000);
                    }
                }
            }
            // Loop until mission starts:
            Console.WriteLine("Waiting for the mission to start ");
            WorldState world_state;

            do
            {
                Console.Write(".");
                Thread.Sleep(100);
                world_state = agentHost.getWorldState();
                foreach (TimestampedString error in world_state.errors)
                {
                    Console.Error.WriteLine("Error:{0}", error.text);
                }
            }while (!world_state.has_mission_begun);

            Console.WriteLine();
            Console.WriteLine("Mission running ");
            Console.WriteLine(my_mission.getAsXML(true));

            // Enter your commands here
            // agentHost.sendCommand("turn -0.5");
            // agentHost.sendCommand("move 1");
            // agentHost.sendCommand("jump 1");

            // Loop until mission ends:
            while (world_state.is_mission_running)
            {
                Console.Write(".");
                Thread.Sleep(100);
                world_state = agentHost.getWorldState();
                foreach (TimestampedString error in world_state.errors)
                {
                    Console.WriteLine("Error:{0}", error.text);
                }
            }
            Console.WriteLine();
            Console.WriteLine("Mission ended");
            // Mission has ended
        }
Example #18
0
    public static void Main()
    {
        for (int run = 0; run < 1; run++)
        {
            Console.WriteLine("Run #" + run);

            AgentHost agentHost = new AgentHost();
            try
            {
                agentHost.parse(new StringVector(Environment.GetCommandLineArgs()));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("ERROR: {0}", ex.Message);
                Console.Error.WriteLine(agentHost.getUsage());
                Environment.Exit(1);
            }
            if (agentHost.receivedArgument("help"))
            {
                Console.Error.WriteLine(agentHost.getUsage());
                Environment.Exit(0);
            }

            bool   pretty_print = false;
            string xml          = System.IO.File.ReadAllText(System.IO.Directory.GetCurrentDirectory() + "/mission.xml");

            MissionSpec mission  = null;
            bool        validate = true;
            mission = new MissionSpec(xml, validate);

            Random rand2 = new Random();

            for (int i = 0; i < rand2.Next(5, 15); i++)
            {
                mission.drawBlock(rand2.Next(1, 10), 46, rand2.Next(1, 10), "red_flower");
            }

            MissionRecordSpec missionRecord = new MissionRecordSpec("./saved_data.tgz");
            missionRecord.recordCommands();
            missionRecord.recordMP4(20, 400000);
            missionRecord.recordRewards();
            missionRecord.recordObservations();

            bool connected = false;
            int  attempts  = 0;
            while (!connected)
            {
                try
                {
                    attempts += 1;
                    agentHost.startMission(mission, missionRecord);
                    connected = true;
                }
                catch (MissionException ex)
                {
                    // Using catch(Exception ex) would also work, but specifying MissionException allows
                    // us to access the error code:
                    Console.Error.WriteLine("Error starting mission: {0}", ex.Message);
                    Console.Error.WriteLine("Error code: {0}", ex.getMissionErrorCode());
                    // We can do more specific error handling using this code, eg:
                    if (ex.getMissionErrorCode() == MissionException.MissionErrorCode.MISSION_INSUFFICIENT_CLIENTS_AVAILABLE)
                    {
                        Console.Error.WriteLine("Have you started a Minecraft client?");
                    }
                    if (attempts >= 3)   // Give up after three goes.
                    {
                        Environment.Exit(1);
                    }
                    Thread.Sleep(1000); // Wait a second and try again.
                }
            }
            WorldState worldState;

            Console.WriteLine("Waiting for the mission to start");
            do
            {
                Console.Write(".");
                Thread.Sleep(100);
                worldState = agentHost.getWorldState();

                foreach (TimestampedString error in worldState.errors)
                {
                    Console.Error.WriteLine("Error: {0}", error.text);
                }
            }while (!worldState.has_mission_begun);

            Console.WriteLine();

            Random rand = new Random();

            Queue <JToken> apples   = new Queue <JToken>();
            bool           observed = false;

            // main loop:
            do
            {
                //agentHost.sendCommand(string.Format("turn {0}", rand.NextDouble()));
                Thread.Sleep(500);
                worldState = agentHost.getWorldState();
                //agentHost.sendCommand("pitch 1");
                if (!observed)
                {
                    JObject obj = JObject.Parse(worldState.observations[0].text);

                    JToken entities;

                    if (obj.TryGetValue("close_entities", out entities))
                    {
                        JArray entitiesArr = (JArray)entities;

                        // The first element is always our agent ? - maybe
                        for (int i = 1; i < entitiesArr.Count; i++)
                        {
                            Console.WriteLine(entitiesArr[i]["name"]);
                            if ((string)entitiesArr[i]["name"] == "red_flower")
                            {
                                apples.Enqueue(entitiesArr[i]);
                            }
                        }

                        observed = true;
                    }
                }
                else
                {
                    // Start trying to get to the apples

                    if (apples.Count == 0)
                    {
                        Console.WriteLine("Mission acomplished.");
                        return;
                    }

                    bool popped = false;
                    while (!popped)
                    {
                        var obs = agentHost.getWorldState().observations;
                        if (obs.Count > 0)
                        {
                            // Get our position
                            JObject obj = JObject.Parse(obs[0].text);

                            JToken entities;

                            if (obj.TryGetValue("close_entities", out entities))
                            {
                                JArray entitiesArr = (JArray)entities;

                                // The first element is always our agent ? - maybe
                                Console.WriteLine(entitiesArr[0]);
                                int x = (int)entitiesArr[0]["x"];
                                int z = (int)entitiesArr[0]["z"];

                                int diffX = (int)apples.Peek()["x"] - x;
                                int diffZ = (int)apples.Peek()["z"] - z;

                                Console.WriteLine(diffX + "-" + diffZ);

                                if (diffZ > 0)
                                {
                                    agentHost.sendCommand("movesouth 1");
                                }
                                else if (diffZ < 0)
                                {
                                    agentHost.sendCommand("movenorth 1");
                                }
                                if (diffX > 0)
                                {
                                    agentHost.sendCommand("moveeast 1");
                                }
                                else if (diffX < 0)
                                {
                                    agentHost.sendCommand("movewest 1");
                                }

                                /* Console.WriteLine(entitiesArr[0]);
                                 * if (diffZ > 0)
                                 * {
                                 *   if((float)entitiesArr[0]["yaw"] > 180)
                                 *       agentHost.sendCommand("turn 180");
                                 *   agentHost.sendCommand("move 1");
                                 * }
                                 * else if (diffZ < 0)
                                 * {
                                 *   agentHost.sendCommand("turn 0");
                                 *   agentHost.sendCommand("move 1");
                                 * }
                                 * if (diffX > 0)
                                 * {
                                 *   agentHost.sendCommand("turn -90");
                                 *   agentHost.sendCommand("move 1");
                                 * }
                                 * else if (diffX < 0)
                                 * {
                                 *   agentHost.sendCommand("turn 90");
                                 *   agentHost.sendCommand("move 1");
                                 * }*/

                                if (diffZ == 0 && diffX == 0)
                                {
                                    Console.WriteLine("Dequeuing");
                                    agentHost.sendCommand("move 0");
                                    apples.Dequeue();

                                    // break block
                                    agentHost.sendCommand("pitch 1");
                                    agentHost.sendCommand("attack 1");
                                    agentHost.sendCommand("jump 1");
                                    Thread.Sleep(6000);
                                    break;
                                }
                            }
                        }
                        Thread.Sleep(500);
                        //  agentHost.sendCommand("movenorth 1");
                    }
                }


                Console.WriteLine(
                    "video,observations,rewards received: {0}, {1}, {2}",
                    worldState.number_of_video_frames_since_last_state,
                    worldState.number_of_observations_since_last_state,
                    worldState.number_of_rewards_since_last_state);
                foreach (TimestampedReward reward in worldState.rewards)
                {
                    Console.Error.WriteLine("Summed reward: {0}", reward.getValue());
                }

                foreach (TimestampedString error in worldState.errors)
                {
                    Console.Error.WriteLine("Error: {0}", error.text);
                }
            }while (worldState.is_mission_running);

            Console.WriteLine("Mission has stopped.");
        }
    }
        static void Main()
        {
            string 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,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""/>" + Menger(-40, 40, -13, 27, "stone", "smooth_granite", "air") + @"
                    <DrawBlock x=""-27"" y=""39"" z=""0"" type=""diamond_block""/>
                  </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""/>
                    <Inventory>
                        <InventoryItem slot=""8"" type=""diamond_pickaxe""/>
                    </Inventory>
                </AgentStart>
                <AgentHandlers>
                  <ObservationFromFullStats/>
                  <ContinuousMovementCommands turnSpeedDegs=""180""/>
                  <InventoryCommands/>
                  <AgentQuitFromReachingPosition>
                    <Marker x=""-26.5"" y=""40"" z=""0.5"" tolerance=""0.5"" description=""Goal_found""/>
                  </AgentQuitFromReachingPosition>
                </AgentHandlers>
              </AgentSection>
            </Mission>";

            // Create default Malmo objects:

            AgentHost agent_host = new AgentHost();

            try
            {
                agent_host.parse(new StringVector(Environment.GetCommandLineArgs()));
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:{0}", e.Message);
                Console.WriteLine(agent_host.getUsage());
                Environment.Exit(1);
            }
            if (agent_host.receivedArgument("help"))
            {
                Console.WriteLine(agent_host.getUsage());
                Environment.Exit(0);
            }

            MissionSpec       my_mission        = new MissionSpec(missionXML, true);
            MissionRecordSpec my_mission_record = new MissionRecordSpec();

            // Attempt to start a mission:
            int max_retries = 3;

            for (int retry = 0; retry < max_retries; retry++)
            {
                try
                {
                    agent_host.startMission(my_mission, my_mission_record);
                    break;
                }
                catch (Exception e)
                {
                    if (retry == max_retries)
                    {
                        Console.WriteLine("Error starting mission:{0}", e.Message);
                        Environment.Exit(1);
                    }
                    else
                    {
                        Thread.Sleep(2000);
                    }
                }
            }

            // Loop until mission starts:
            Console.WriteLine("Waiting for the mission to start ");
            WorldState world_state = agent_host.getWorldState();

            while (!world_state.has_mission_begun)
            {
                Console.Write(".");
                Thread.Sleep(100);
                world_state = agent_host.getWorldState();
                foreach (TimestampedString error in world_state.errors)
                {
                    Console.WriteLine("Error:{0}", error.text);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Mission running ");

            agent_host.sendCommand("hotbar.9 1");
            agent_host.sendCommand("hotbar.9 0");

            agent_host.sendCommand("pitch 0.2");
            Thread.Sleep(1000);
            agent_host.sendCommand("pitch 0");
            agent_host.sendCommand("move 1");
            agent_host.sendCommand("attack 1");

            // Loop until mission ends:
            while (world_state.is_mission_running)
            {
                Console.Write(".");
                Thread.Sleep(100);
                world_state = agent_host.getWorldState();
                foreach (TimestampedString error in world_state.errors)
                {
                    Console.WriteLine("Error:{0}", error.text);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Mission ended");
            // Mission has ended.
        }
Example #20
0
 public MazeMaker(MissionSpec givenMission)
 {
     mission = givenMission;
 }
Example #21
0
        private async Task<MissionSpec> loadMission(string path)
        {
            using (StreamReader reader = new StreamReader(path))
            {
                try
                {
                    string xml = await reader.ReadToEndAsync();
                    xml = xml.Replace("__SEED__", _rand.Next().ToString());
                    MissionSpec mission = new MissionSpec(xml, true);

                    if (!mission.isVideoRequested(0))
                    {
                        mission.requestVideo(Settings.Default.VideoWidth, Settings.Default.VideoHeight);
                    }

                    mission.removeAllCommandHandlers();
                    mission.allowAllContinuousMovementCommands();
                    mission.allowAllDiscreteMovementCommands();                    

                    XDocument xdoc = XDocument.Parse(xml);
                    XNamespace malmo = "http://ProjectMalmo.microsoft.com";
                    var timeUpNode = xdoc.Descendants(malmo + "ServerQuitFromTimeUp").FirstOrDefault();
                    if(timeUpNode != null)
                    {
                        _missionDuration = TimeSpan.FromMilliseconds(double.Parse(timeUpNode.Attribute("timeLimitMs").Value));
                    }
                    else
                    {
                        _missionDuration = TimeSpan.Zero;
                    }

                    if (_recordWindow != null && _recordWindow.IsVisible)
                    {
                        _recordWindow.MissionPath = path;
                    }

                    return mission;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error Parsing Mission", MessageBoxButton.OK, MessageBoxImage.Error);
                    return null;
                }
            }
        }
Example #22
0
 private MazeMaker()
 {
     mission = null;
 }
Example #23
0
        private void runMission(MissionSpec mission)
        {
            string recordPath = "none";
            MissionRecordSpec missionRecord;
            if (string.IsNullOrEmpty(Settings.Default.OutputDir))
            {
                missionRecord = new MissionRecordSpec();
            }
            else
            {
                recordPath = Path.Combine(Settings.Default.OutputDir, Guid.NewGuid() + ".tar.gz");
                missionRecord = new MissionRecordSpec(recordPath);
                if (Settings.Default.RecordObservations)
                {
                    missionRecord.recordObservations();
                }

                if (Settings.Default.RecordRewards)
                {
                    missionRecord.recordRewards();
                }

                if (Settings.Default.RecordCommands)
                {
                    missionRecord.recordCommands();
                }

                if (Settings.Default.RecordMP4)
                {
                    missionRecord.recordMP4(Settings.Default.MP4FPS, (long)(Settings.Default.BitRate * 1024));
                }
            }

            using (AgentHost agentHost = new AgentHost())
            {
                ClientPool clientPool = new ClientPool();
                clientPool.add(new ClientInfo(Settings.Default.ClientIP, Settings.Default.ClientPort));

                try
                {
                    agentHost.startMission(mission, clientPool, missionRecord, 0, "hac");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error Starting Mission", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                Dispatcher.Invoke(() =>
                {
                    Activate();
                    VideoImage.Focus();
                    startB.Content = "Set";
                    Title = Settings.Default.Title + " : Waiting for mission start";
                    if (_recordWindow != null && _recordWindow.IsVisible)
                    {
                        _recordWindow.RecordPath = recordPath;
                    }
                });

                _isInSession = true;
                try
                {
                    WorldState worldState;
                    // wait for mission to start
                    do
                    {
                        Thread.Sleep(100);
                        worldState = agentHost.getWorldState();

                        if (worldState.errors.Any())
                        {
                            StringBuilder errors = new StringBuilder();
                            foreach (TimestampedString error in worldState.errors)
                            {
                                errors.AppendLine(error.text);
                            }

                            MessageBox.Show(errors.ToString(), "Error during mission initialization", MessageBoxButton.OK, MessageBoxImage.Error);
                            return;
                        }
                    }
                    while (!worldState.is_mission_running && !_stopMission);

                    if (_stopMission)
                    {
                        return;
                    }

                    DateTime missionStartTime = DateTime.UtcNow;
                    _timeRemaining = _missionDuration;

                    Dispatcher.Invoke(() =>
                    {
                        startB.Content = "Go!";
                        Title = Settings.Default.Title + " : Recording";
                    });

                    // run mission
                    TimeSpan loopTime = TimeSpan.FromSeconds(1.0 / 20);

                    _pendingCommandsMutex.Wait();
                    try
                    {
                        _pendingCommandQueue = new Queue<Tuple<string, float>>();
                    }
                    finally
                    {
                        _pendingCommandsMutex.Release();
                    }

                    _pendingMessagesMutex.Wait();
                    try
                    {
                        _pendingMessages = new Queue<string>();
                    }
                    finally
                    {
                        _pendingMessagesMutex.Release();
                    }

                    bool failure = false;
                    Stopwatch loopTimer = new Stopwatch();
                    do
                    {
                        loopTimer.Reset();
                        loopTimer.Start();

                        worldState = agentHost.getWorldState();

                        TimestampedVideoFrame frame = worldState.video_frames.FirstOrDefault();

                        if (frame != null)
                        {
                            if (_missionDuration != TimeSpan.Zero)
                            {
                                TimeSpan elapsed = frame.timestamp - missionStartTime;
                                _timeRemaining = _missionDuration - elapsed;
                            }

                            _pixelsMutex.Wait();
                            try
                            {
                                if (_pixels == null || _pixels.Length != frame.pixels.Count)
                                {
                                    _pixels = new byte[frame.pixels.Count];
                                    Dispatcher.Invoke(() =>
                                    {
                                        if (_bitmap.Width != frame.width || _bitmap.Height != frame.height)
                                        {
                                            _bitmap = new WriteableBitmap(frame.width, frame.height, 72, 72, PixelFormats.Rgb24, null);
                                            VideoImage.Source = _bitmap;
                                            if (_recordWindow != null && _recordWindow.IsVisible)
                                            {
                                                _recordWindow.FrameSource = _bitmap;
                                            }
                                        }
                                    });
                                }

                                frame.pixels.CopyTo(_pixels);
                            }
                            finally
                            {
                                _pixelsMutex.Release();
                            }
                        }

                        _pendingMessagesMutex.Wait();
                        try
                        {
                            foreach (var reward in worldState.rewards)
                            {
                                _score += reward.getValue();
                                if (reward.getValue() < 0)
                                {
                                    failure = true;
                                }

                                _pendingMessages.Enqueue(string.Format("{0}> score {1}", reward.timestamp.ToString("hh:mm:ss.fff"), reward.getValue()));
                            }

                            _score = Math.Max(_score, 0);
                            _score = Math.Min(_score, 99999);

                            foreach (var observation in worldState.observations)
                            {
                                int posStart = observation.text.IndexOf("\"XPos\"");
                                if (posStart < 0)
                                {
                                    continue;
                                }

                                int posEnd = observation.text.IndexOf("\"ZPos\"");
                                posEnd = observation.text.IndexOf(',', posEnd);

                                string posSegment = observation.text.Substring(posStart, posEnd - posStart);
                                string[] pos = posSegment.Split(',');
                                float x = Convert.ToSingle(pos[0].Split(':')[1]);
                                float y = Convert.ToSingle(pos[1].Split(':')[1]);
                                float z = Convert.ToSingle(pos[2].Split(':')[1]);

                                _pendingMessages.Enqueue(string.Format("{0}> (x={1:0.00}, y={2:0.00}, z={3:0.00})", observation.timestamp.ToString("hh:mm:ss.fff"), x, y, z));
                            }
                        }
                        finally
                        {
                            _pendingMessagesMutex.Release();
                        }

                        CheckGamepad(agentHost);

                        _pendingCommandsMutex.Wait();
                        try
                        {
                            while (_pendingCommandQueue.Any())
                            {
                                var command = _pendingCommandQueue.Dequeue();
                                CheckAndSend(agentHost, command.Item1, command.Item2);
                            }
                        }
                        finally
                        {
                            _pendingCommandsMutex.Release();
                        }

                        loopTimer.Stop();
                        if (loopTimer.Elapsed < loopTime)
                        {
                            Thread.Sleep(loopTime - loopTimer.Elapsed);
                        }

                    } while (worldState.is_mission_running && !_stopMission);

                    if (_stopMission)
                    {
                        return;
                    }

                    if (!failure)
                    {
                        _score += _timeRemaining.TotalSeconds * 100;
                    }

                    _missionSuccess = !failure;

                    _pendingCommandsMutex.Wait();
                    try
                    {
                        _pendingCommandQueue = null;
                    }
                    finally
                    {
                        _pendingCommandsMutex.Release();
                    }

                    Dispatcher.Invoke(() =>
                    {
                        Title = Settings.Default.Title + " : Ready";
                        UpdateDisplayedMessages();
                        UpdateDisplayedReward();
                    });

                    _resetVerb = null;

                    var keys = _continuousCommandState.Keys.ToList();
                    foreach (var verb in keys)
                    {
                        _continuousCommandState[verb] = 0;
                    }

                    keys = _discreteCommandState.Keys.ToList();
                    foreach (var verb in keys)
                    {
                        _discreteCommandState[verb] = false;
                    }

                    if (worldState.errors.Any())
                    {
                        StringBuilder errors = new StringBuilder();
                        foreach (TimestampedString error in worldState.errors)
                        {
                            errors.AppendLine(error.text);
                        }

                        MessageBox.Show(errors.ToString(), "Error during mission initialization", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error during mission", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                finally
                {
                    _isInSession = false;
                }
            }
        }    
Example #24
0
    public static void Main()
    {
        AgentHost agentHost = new AgentHost();
        try
        {
            agentHost.parse( new StringVector( Environment.GetCommandLineArgs() ) );
        }
        catch( Exception ex )
        {
            Console.Error.WriteLine("ERROR: {0}", ex.Message);
            Console.Error.WriteLine(agentHost.getUsage());
            Environment.Exit(1);
        }
        if( agentHost.receivedArgument("help") )
        {
            Console.Error.WriteLine(agentHost.getUsage());
            Environment.Exit(0);
        }

        MissionSpec mission = new MissionSpec();
        mission.timeLimitInSeconds(10);
        mission.requestVideo( 320, 240 );
        mission.rewardForReachingPosition(19.5f,0.0f,19.5f,100.0f,1.1f);

        MissionRecordSpec missionRecord = new MissionRecordSpec("./saved_data.tgz");
        missionRecord.recordCommands();
        missionRecord.recordMP4(20, 400000);
        missionRecord.recordRewards();
        missionRecord.recordObservations();

        try
        {
            agentHost.startMission(mission, missionRecord);
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine("Error starting mission: {0}", ex.Message);
            Environment.Exit(1);
        }

        WorldState worldState;

        Console.WriteLine("Waiting for the mission to start");
        do
        {
            Console.Write(".");
            Thread.Sleep(100);
            worldState = agentHost.getWorldState();

            foreach (TimestampedString error in worldState.errors) Console.Error.WriteLine("Error: {0}", error.text);
        }
        while (!worldState.is_mission_running);

        Console.WriteLine();

        Random rand = new Random();
        // main loop:
        do
        {
            agentHost.sendCommand("move 1");
            agentHost.sendCommand(string.Format("turn {0}", rand.NextDouble()));
            Thread.Sleep(500);
            worldState = agentHost.getWorldState();
            Console.WriteLine(
                "video,observations,rewards received: {0}, {1}, {2}",
                worldState.number_of_video_frames_since_last_state,
                worldState.number_of_observations_since_last_state,
                worldState.number_of_rewards_since_last_state);
            foreach (TimestampedReward reward in worldState.rewards) Console.Error.WriteLine("Summed reward: {0}", reward.getValue());
            foreach (TimestampedString error in worldState.errors) Console.Error.WriteLine("Error: {0}", error.text);
        }
        while (worldState.is_mission_running);

        Console.WriteLine("Mission has stopped.");
    }
Example #25
0
        static void Main()
        {
            string 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,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""/>" + Menger(-40, 40, -13, 27, "stone", "smooth_granite", "air") + @"
                    <DrawCuboid x1=""-25"" y1=""39"" z1=""-2"" x2=""-29"" y2=""39"" z2=""2"" type=""lava""/>
                    <DrawCuboid x1=""-26"" y1=""39"" z1=""-1"" x2=""-28"" y2=""39"" z2=""1"" type=""obsidian""/>
                    <DrawBlock x=""-27"" y=""39"" z=""0"" type=""diamond_block""/>
                  </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""/>
                    <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:

            AgentHost agent_host = new AgentHost();

            try
            {
                agent_host.parse(new StringVector(Environment.GetCommandLineArgs()));
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:{0}", e.Message);
                Console.WriteLine(agent_host.getUsage());
                Environment.Exit(1);
            }
            if (agent_host.receivedArgument("help"))
            {
                Console.WriteLine(agent_host.getUsage());
                Environment.Exit(0);
            }

            MissionSpec       my_mission        = new MissionSpec(missionXML, true);
            MissionRecordSpec my_mission_record = new MissionRecordSpec();

            // Attempt to start a mission:
            int max_retries = 3;

            for (int retry = 0; retry < max_retries; retry++)
            {
                try
                {
                    agent_host.startMission(my_mission, my_mission_record);
                    break;
                }
                catch (Exception e)
                {
                    if (retry == max_retries - 1)
                    {
                        Console.WriteLine("Error starting mission:{0}", e.Message);
                        Environment.Exit(1);
                    }
                    else
                    {
                        Thread.Sleep(2000);
                    }
                }
            }

            // Loop until mission starts
            Console.WriteLine("Waiting for the mission to start ");
            WorldState world_state = agent_host.getWorldState();

            while (!world_state.has_mission_begun)
            {
                Console.Write(".");
                Thread.Sleep(100);
                world_state = agent_host.getWorldState();
                foreach (TimestampedString error in world_state.errors)
                {
                    Console.WriteLine("Error:{0}", error.text);
                }
            }
            Console.WriteLine();
            Console.WriteLine("Mission running ");

            agent_host.sendCommand("hotbar.9 1"); // Press the hotbar key
            agent_host.sendCommand("hotbar.9 0"); // Release hotbar key - agent should now be holding diamond_pickaxe
            agent_host.sendCommand("pitch 0.2");  // Start looking downward slowly
            Thread.Sleep(1000);                   // Wait a second until we are looking in roughly the right direction
            agent_host.sendCommand("pitch 0");    // Stop tilting the camera
            agent_host.sendCommand("move 1");     // And start running...
            agent_host.sendCommand("attack 1");   // Whilst flailing our pickaxe!
            bool jumping = false;

            // Loop until mission ends:
            while (world_state.is_mission_running)
            {
                Console.Write(".");
                Thread.Sleep(100);
                world_state = agent_host.getWorldState();
                foreach (TimestampedString error in world_state.errors)
                {
                    Console.WriteLine("Error:{0}", error.text);
                }
                if (world_state.number_of_observations_since_last_state > 0) // Have any observations come in?
                {
                    string  msg          = world_state.observations[world_state.observations.Count - 1].text;
                    JObject observations = JObject.Parse(msg);
                    JToken  grid         = observations.GetValue("floor3x3");
                    if (jumping is true)
                    {
                        if (grid[4].ToString() is "lava")
                        {
                            agent_host.sendCommand("jump 0");
                            jumping = false;
                        }
                    }
                    if (grid[3].ToString() is "lava")
                    {
                        agent_host.sendCommand("jump 1");
                        jumping = true;
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("Mission ended");
            // Mission has ended.
        }
Example #26
0
        static void Main()
        {
            string 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 >
  

                                   < Weather > rain </ Weather >
  

                               </ Time >
  

                           </ 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;' />
  

                              < ServerQuitFromTimeUp timeLimitMs = '30000' />
  

                               < ServerQuitWhenAnyAgentFinishes />
  

                             </ ServerHandlers >
  

                           </ ServerSection >
  


                           < AgentSection mode = 'Survival' >
  

                              < Name > MalmoTutorialBot </ Name >
  

                              < AgentStart >
  

                                  < Placement x = '0' y = '56' z = '0' yaw = '90' />
  

                                     </ AgentStart >
  

                                     < AgentHandlers >
  

                                       < ObservationFromFullStats />
  

                                       < ContinuousMovementCommands turnSpeedDegs = '180' />
  

                                      </ AgentHandlers >
  

                                    </ AgentSection >
  

                                  </ Mission >";
            // Create default Malmo objects:

            AgentHost agent_host = new AgentHost();

            try
            {
                agent_host.parse(new StringVector(Environment.GetCommandLineArgs()));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error:{0}", ex.Message);
                Console.Error.WriteLine(agent_host.getUsage());
                Environment.Exit(1);
            }
            if (agent_host.receivedArgument("help"))
            {
                Console.Error.WriteLine(agent_host.getUsage());
                Environment.Exit(0);
            }

            MissionSpec my_mission = new MissionSpec();

            my_mission.timeLimitInSeconds(30);
            MissionRecordSpec my_mission_record = new MissionRecordSpec("./saved_data.tgz");

            // Attempt to start a mission:
            int max_retries = 3;

            for (int retry = 0; retry < max_retries; retry++)
            {
                try
                {
                    agent_host.startMission(my_mission, my_mission_record);
                    break;
                }
                catch (Exception ex)
                {
                    if (retry == max_retries - 1)
                    {
                        Console.WriteLine("Error starting mission:{0}", ex.Message);
                        Environment.Exit(1);
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
            // Loop until mission starts:
            Console.WriteLine("Waiting for the mission to start ");
            WorldState world_state = agent_host.getWorldState();

            while (!world_state.has_mission_begun)
            {
                Console.Write(".");
                Thread.Sleep(100);
                world_state = agent_host.getWorldState();
                foreach (TimestampedString error in world_state.errors)
                {
                    Console.WriteLine("Error:{0}", error.text);
                }
            }
            Console.WriteLine();
            Console.WriteLine("Mission running ");

            // Commands go here //

            // Loop until mission ends:
            while (world_state.is_mission_running)
            {
                Console.Write(".");
                Thread.Sleep(100);
                world_state = agent_host.getWorldState();
                foreach (TimestampedString error in world_state.errors)
                {
                    Console.WriteLine("Error:{0}", error.text);
                }
            }
            Console.WriteLine();
            Console.WriteLine("Mission ended");
            // Mission has ended.
        }