/// <summary>
        /// Open a mission from a file
        /// </summary>
        /// <param name="p"></param>
        private void OpenMissionFromFile(string p)
        {
            // create file
            FileStream fs = new FileStream(p, FileMode.Open);

            // serializer
            BinaryFormatter bf = new BinaryFormatter();

            // serialize
            ArbiterMissionDescription amd = (ArbiterMissionDescription)bf.Deserialize(fs);

            // set default mission
            this.defaultMissionDescription = amd;

            // release holds
            fs.Dispose();
        }
        /// <summary>
        /// Restore from a saved editor save
        /// </summary>
        /// <param name="es"></param>
        private void Restore(SimulatorSave es)
        {
            // restore the editor
            this.gridSizeToolStripComboBox.SelectedIndex = es.GridSizeIndex;
            this.arbiterRoads = es.ArbiterRoads;
            this.simEngine = es.SimEngine;
            this.simEngine.simulationMain = this;
            this.simEngine.SetPropertyGrid(this.SimulatorPropertyGrid);
            this.defaultMissionDescription = es.Mission;

            // restore the display
            this.roadDisplay1.LoadSave(es.displaySave);

            // attempt to rehash the clients
            this.clientHandler.ReBindAll(this.simEngine.Vehicles);
        }
        private void InitializeClient_Click(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Forms.ListView.SelectedListViewItemCollection slvic = this.listView1.SelectedItems;

                if (slvic != null)
                {
                    foreach (ListViewItem lvi in slvic)
                    {
                        System.Windows.Forms.ListViewItem.ListViewSubItemCollection lvsic = lvi.SubItems;

                        if (this.clientHandler.AvailableClients.ContainsKey(lvsic[2].Text))
                        {
                            string clientName = lvsic[2].Text;
                            SimulatorClientFacade scf = this.clientHandler.AvailableClients[lvsic[2].Text];

                            if (this.clientHandler.ClientToVehicleMap.ContainsKey(clientName))
                            {
                                SimVehicle sv = this.simEngine.Vehicles[this.clientHandler.ClientToVehicleMap[clientName]];
                                KeyValuePair<SimVehicleId, SimVehicle> vhcs = new KeyValuePair<SimVehicleId, SimVehicle>(sv.VehicleId, sv);
                                ArbiterRoadNetwork roadNetwork = this.arbiterRoads;
                                ArbiterMissionDescription mission = this.defaultMissionDescription;

                                // check if we need to randomize mission
                                if (vhcs.Value.RandomMission)
                                {
                                    // create random mission
                                    Queue<ArbiterCheckpoint> checks = new Queue<ArbiterCheckpoint>(60);
                                    int num = mission.MissionCheckpoints.Count - 1;
                                    ArbiterCheckpoint[] checkPointArray = mission.MissionCheckpoints.ToArray();
                                    Random r = new Random();
                                    for (int i = 0; i < 60; i++)
                                    {
                                        checks.Enqueue(checkPointArray[r.Next(num)]);
                                    }
                                    ArbiterMissionDescription amd = new ArbiterMissionDescription(checks, mission.SpeedLimits);

                                    // set road , mission
                                    scf.SetRoadNetworkAndMission(roadNetwork, amd);
                                }
                                // otherwise no random mission
                                else
                                {
                                    // set road , mission
                                    scf.SetRoadNetworkAndMission(roadNetwork, mission);
                                }

                                // startup ai
                                bool b = scf.StartupVehicle();

                                // check for false
                                if (!b)
                                {
                                    SimulatorOutput.WriteLine("Error starting simulation for vehicle id: " + vhcs.Key.ToString());
                                    return;
                                }
                            }
                            else
                            {
                                SimulatorOutput.WriteLine("Client needs to be connected to vehicle");
                            }
                        }
                    }

                    this.OnClientsChanged();
                }
            }
            catch (Exception ex)
            {
                SimulatorOutput.WriteLine("Client Initialization Failed: \n " + ex.ToString());
            }
        }
        /// <summary>
        /// Starts the vehicle
        /// </summary>
        /// <param name="arbiterRoadNetwork"></param>
        /// <param name="arbiterMissionDescription"></param>
        /// <returns></returns>
        public bool StartupVehicle(ArbiterRoadNetwork arbiterRoadNetwork, ArbiterMissionDescription arbiterMissionDescription)
        {
            try
            {
                // get arbiter
                this.client.ClientArbiter = (ArbiterAdvancedRemote)this.objectDirectory.Resolve("ArbiterAdvancedRemote_" + SimulatorClient.MachineName);

                // set road and mission and spool up ai to wait for initial data stream
                this.client.ClientArbiter.JumpstartArbiter(arbiterRoadNetwork, arbiterMissionDescription);

                // success!
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error spooling up ai: \n" + e.ToString());
                return false;
            }
        }
 /// <summary>
 /// Sets the road network and mission for the car
 /// </summary>
 /// <param name="roadNetwork"></param>
 /// <param name="mission"></param>
 public abstract void SetRoadNetworkAndMission(ArbiterRoadNetwork roadNetwork, ArbiterMissionDescription mission);
 public abstract void JumpstartArbiter(ArbiterRoadNetwork roadNetwork, ArbiterMissionDescription mission);
 /// <summary>
 /// Udpates teh ai's mission
 /// </summary>
 /// <param name="mission"></param>
 /// <returns></returns>
 /// <remarks>Vehicle needs to be in pause</remarks>
 public abstract bool UpdateMission(ArbiterMissionDescription mission);
        /// <summary>
        /// Begin simulation
        /// </summary>
        public void BeginSimulation(ArbiterRoadNetwork roadNetwork, ArbiterMissionDescription mission)
        {
            try
            {
                // startup clients
                foreach (KeyValuePair<SimVehicleId, SimVehicle> vhcs in this.Vehicles)
                {
                    // set road and mission
                    SimulatorClientFacade scf = this.simulationMain.clientHandler.AvailableClients[this.simulationMain.clientHandler.VehicleToClientMap[vhcs.Key]];

                    // check if we need to randomize mission
                    if (vhcs.Value.RandomMission)
                    {
                        // create random mission
                        Queue<ArbiterCheckpoint> checks = new Queue<ArbiterCheckpoint>(60);
                        int num = mission.MissionCheckpoints.Count - 1;
                        ArbiterCheckpoint[] checkPointArray = mission.MissionCheckpoints.ToArray();
                        Random r = new Random();
                        for (int i = 0; i < 60; i++)
                        {
                            checks.Enqueue(checkPointArray[r.Next(num)]);
                        }
                        ArbiterMissionDescription amd = new ArbiterMissionDescription(checks, mission.SpeedLimits);

                        // set road , mission
                        scf.SetRoadNetworkAndMission(roadNetwork, amd);
                    }
                    // otherwise no random mission
                    else
                    {
                        // set road , mission
                        scf.SetRoadNetworkAndMission(roadNetwork, mission);
                    }

                    // startup ai
                    bool b = scf.StartupVehicle();

                    // check for false
                    if (!b)
                    {
                        Console.WriteLine("Error starting simulation for vehicle id: " + vhcs.Key.ToString());
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error starting simulation: \n" + e.ToString());
                return;
            }

            // run sim
            this.RunSimulation();
        }
        /// <summary>
        /// Updates the ai with a new mission if can
        /// </summary>
        /// <param name="mission"></param>
        /// <returns></returns>
        public override bool UpdateMission(ArbiterMissionDescription mission)
        {
            try
            {
                ArbiterOutput.Output("Setting new mission");
                CarMode carMode = CoreCommon.Communications.GetCarMode();

                if (carMode == CarMode.Pause || carMode == CarMode.Human)
                {
                    if (mission != null)
                    {
                        // create roads and mission
                        CoreCommon.RoadNetwork.SetSpeedLimits(mission.SpeedLimits);
                        this.arbiterMissionDescription = mission;
                        CoreCommon.Mission = mission;
                        return true;
                    }
                    else
                    {
                        ArbiterOutput.Output("Mission must have value");
                    }
                }
                else
                {
                    ArbiterOutput.Output("Cannot set mission when car is in CarMode: " + carMode.ToString());
                }
            }
            catch (Exception ex)
            {
                ArbiterOutput.Output("UpdateMission(ArbiterMissionDescription mission) Failed", ex);
            }

            return false;
        }
        /// <summary>
        /// Jumpstarts the ai with a new road network and mission if we can
        /// </summary>
        /// <param name="roadNetwork"></param>
        /// <param name="mission"></param>
        public override void JumpstartArbiter(ArbiterRoadNetwork roadNetwork, ArbiterMissionDescription mission)
        {
            try
            {
                ArbiterOutput.Output("Jumpstarting Arbiter");

                // warn if carmode not correct
                CarMode carMode = CoreCommon.Communications.GetCarMode();
                if (carMode != CarMode.Pause && carMode != CarMode.Human)
                    ArbiterOutput.Output("Warning: Vehicle is in CarMode: " + carMode.ToString());

                if (roadNetwork != null && mission != null)
                {
                    // destroy intelligence if exists
                    this.intelligenceCore.DestroyIntelligence();

                    // create roads and mission
                    roadNetwork.SetSpeedLimits(mission.SpeedLimits);
                    roadNetwork.GenerateVehicleAreas();
                    this.arbiterRoadNetwork = roadNetwork;
                    this.arbiterMissionDescription = mission;
                    CoreCommon.RoadNetwork = roadNetwork;
                    CoreCommon.Mission = mission;

                    // startup ai
                    this.intelligenceCore.Jumpstart();
                }
                else
                {
                    ArbiterOutput.Output("RoadNetwork and Mission must both have value");
                }
            }
            catch (Exception ex)
            {
                ArbiterOutput.Output("JumpstartArbiter(ArbiterRoadNetwork roadNetwork, ArbiterMissionDescription mission) Failed", ex);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Sets mission
        /// </summary>
        /// <param name="mission"></param>
        private void SetMission(ArbiterMissionDescription mission)
        {
            // set mission
            RemoraCommon.Mission = mission;

            // set speeds if can
            if (RemoraCommon.RoadNetwork != null)
            {
                try
                {
                    RemoraCommon.RoadNetwork.SetSpeedLimits(mission.SpeedLimits);
                }
                catch (Exception e)
                {
                    RemoraOutput.WriteLine("Error setting road network speed limits from inputted mission: " + e.ToString(), OutputType.Remora);
                }

                RemoraOutput.WriteLine("Successfully loaded mission, passed speed limit set check", OutputType.Remora);

                int numAreas = RemoraCommon.RoadNetwork.ArbiterSegments.Count + RemoraCommon.RoadNetwork.ArbiterZones.Count;
                int numSpeeds = RemoraCommon.Mission.SpeedLimits.Count;

                if (numAreas != numSpeeds)
                {
                    RemoraOutput.WriteLine("Warning: Number of Speed Limits: " + numSpeeds.ToString() + " not equal to number of network Areas: " + numAreas.ToString(), OutputType.Remora);
                }
                else
                {
                    RemoraOutput.WriteLine("Number of Speed Limits: " + numSpeeds.ToString() + " equal to number of network Areas: " + numAreas.ToString(), OutputType.Remora);
                }
            }
            else
            {
                RemoraOutput.WriteLine("Cannot load Mission before Road Network", OutputType.Remora);
            }
        }
 /// <summary>
 /// Sets the road network and mission for the car
 /// </summary>
 /// <param name="roadNetwork"></param>
 /// <param name="mission"></param>
 public override void SetRoadNetworkAndMission(ArbiterRoadNetwork roadNetwork, ArbiterMissionDescription mission)
 {
     this.roadNetwork = roadNetwork;
     this.mission = mission;
     this.roadNetwork.SetSpeedLimits(mission.SpeedLimits);
     this.world = new WorldService(this.roadNetwork);
 }