Exemplo n.º 1
0
 public Boolean Near(Location p)
 {
     return ((Math.Abs(this.x - p.x)<15) && (Math.Abs(this.y - p.y)<15));
 }
Exemplo n.º 2
0
        public void StartListening()
        {

            while (!network.IsConnected())
            {
                Thread.Sleep(100);
            }
            subscribeToEvents();
            isActive = true;
            while (network.IsConnected())
            {// as long as there's a network we remain open to input events
                while (isActive && network.IsConnected())
                {

                    events = network.GetEvents();

                    foreach (SimulationEvent e in events)
                    {
                        switch (e.eventType)
                        {

                            case "RevealObject":
                                if (ScudLauncher.AgentControls(((StringValue)e["ObjectID"]).value))
                                {
                                    AttributeCollectionValue attributes = (AttributeCollectionValue)e["Attributes"];
                                    LocationValue locus = (LocationValue)attributes["Location"];
                                    ScudLauncher.AddUnit(((StringValue)e["ObjectID"]).value, locus.X, locus.Y);
                                }
                                else if (Target.IsTarget((((StringValue)e["ObjectID"]).value)))
                                {
                                    Target.AddUnit(((StringValue)e["ObjectID"]).value);
                                }

                                break;
                            case "StateChange":
                                if (ScudLauncher.AgentControls(((StringValue)e["ObjectID"]).value))
                                {
                                    if ("Dead" == ((StringValue)e["NewState"]).value)
                                    {
                                        if (ScudLauncher.AgentControls(((StringValue)e["ObjectID"]).value))
                                            if (ScudLauncher.AgentControls(((StringValue)e["ObjectID"]).value))

                                                ScudLauncher.DropUnit(((StringValue)e["ObjectID"]).value);
                                            else if (Target.IsTarget((((StringValue)e["ObjectID"]).value)))
                                                Target.DropUnit(((StringValue)e["ObjectID"]).value);
                                    }
                                }
                                break;
                            /*        case "MoveDone":
                                        if (ScudLauncher.AgentControls(((StringValue)e["ObjectID"]).value))
                                            ScudLauncher.GetScudLauncher((((StringValue)e["ObjectID"]).value)).InMotion = false;
 
                                        break;*/
                            case "SimulationTimeEvent":
                                if (0 == ((IntegerValue)e["Time"]).value % 1000)// only bother with full seconds
                                {
                                    ScudLauncher.Tick(((IntegerValue)e["Time"]).value);
                                }
                                break;
                            case "ViewProMotionUpdate":
                                if (ScudLauncher.AgentControls(((StringValue)e["ObjectID"]).value))
                                {
                                    string objectID=((StringValue)e["ObjectID"]).value;
                                    LocationValue locVal=(LocationValue)e["Location"];
                                    Location current=new Location(locVal.X,locVal.Y);
                                     locVal=(LocationValue)e["DestinationLocation"];
                                     Location destination=new Location(locVal.X,locVal.Y);

                                     Boolean moving = (current.X != destination.X) || (current.Y != destination.Y);
                                     if (moving != ScudLauncher.IsMoving(objectID))
                                         if(!moving)
                                         {
                                             Console.WriteLine("Updating position of "+objectID+" to ("+destination.X.ToString()+","+destination.Y.ToString()+")");
                                             ScudLauncher.SetLocation(objectID, current);

                                         }
                      ScudLauncher.SetMovement(objectID, moving);
                      
                                }
                                break;


                        }
                    }

                    Thread.Sleep(100);
                }
                if (!network.IsConnected())
                    isActive = false;
            }
            Console.WriteLine("Lost connection.");
            network = null;
        }
Exemplo n.º 3
0
 public Location(Location p)
 {
     this.x = p.x;
     this.y = p.y;
 }
Exemplo n.º 4
0
        /// <summary>
        /// newLocation generates a location along the box that a unit can move to
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private Location newLocation()
        {

            Location returnValue;

            Location p = this.position;
            string corner = nearCorner(p);
            if ("" == corner)
            {// along a side
                string side = onSide(p);
                switch (side)
                {
                    case "S":
                    case "N":
                        returnValue = new Location(randInt(corners["SW"].X, 1 + Corners["SE"].X), p.Y);
                        break;
                    default: //"E" or "W" side
                        returnValue = new Location(p.X, randInt(Corners["SW"].Y, 1 + Corners["NW"].Y));
                        break;
                }
            }
            else
            {
                //Disallow move from corner to corner
                int moveX = randInt(1 + Corners["SW"].X, Corners["SE"].X);
                int moveY = randInt(1 + Corners["SW"].Y, Corners["NW"].Y);
                int choiceOfMove = randInt(2);
                switch (choiceOfMove)
                {
                    case 0:
                        returnValue = new Location(p.X, moveY);
                        break;
                    default:// without a default case compiler thinks returnValue might not be assigned!
                        returnValue = new Location(moveX, p.Y);
                        break;
                }

            }
            return returnValue;
        }
Exemplo n.º 5
0
 /// <summary>
 /// onSide assumes point is not at a corner
 /// </summary>
 /// <param name="p">current locatiob\n</param>
 /// <returns>"S","E","W",or "N"</returns>
 private static string onSide(Location p)
 {
     // since not a corner, only one of the values can match a value at a corner
     string returnValue;
     if (Math.Abs( p.X - corners["NW"].X)<15)
         returnValue = "W";
     else if (Math.Abs(p.X - corners["NE"].X)<15)
         returnValue = "E";
     else if (Math.Abs(p.Y - corners["NE"].Y)<15)
         returnValue = "N";
     else returnValue = "S";
     return returnValue;
 }
Exemplo n.º 6
0
 private static string nearCorner(Location p)
 {
     string returnValue = "";
     if (p.Near(corners["SW"]))
         returnValue = "SW";
     else if (p.Near(corners["NW"]))
         returnValue = "NW";
     else if (p.Near(corners["SE"]))
         returnValue = "SE";
     else if (p.Near(corners["NE"]))
         returnValue = "NE";
     return returnValue;
 }
Exemplo n.º 7
0
 public static void SetLocation(string unitId, Location p)
 {
     allUnits[unitId].position.X = p.X;
     allUnits[unitId].position.Y = p.Y;
 }