Esempio n. 1
0
        /// <summary>Performs one step of the simulation for one robot.</summary>
        /// <param name="r">The robot to perform the simulation step for.</param>
        void SimulateOneStep(Robot r)
        {
            // Set ptR to the location of the robot in room coordinates
            RoomPoint ptR = r.Location;
            double    vectorX = 0, vectorY = 0;

            foreach (Robot s in _robots)
            {
                if (r == s)
                {
                    continue;
                }
                RoomPoint ptS = s.Location;
                double    inverseSquareDistance = 1.0 / RoomPoint.Square(ptR.DistanceTo(ptS));
                double    angle = ptR.AngleTo(ptS);
                vectorX -= inverseSquareDistance * Math.Cos(angle);
                vectorY -= inverseSquareDistance * Math.Sin(angle);
            }

            double degrees = Math.Atan2(vectorY, vectorX) * 180 / Math.PI;

            degrees += 22.5;
            while (degrees < 0)
            {
                degrees += 360;
            }
            while (degrees >= 360)
            {
                degrees -= 360;
            }

            int direction = (int)(degrees * 8 / 360);

            if ((direction == 7) || (direction == 0) || (direction == 1))
            {
                ptR.X = Math.Min(ptR.X + 1, ROOM_SIZE - 1);
            }
            else if ((direction == 3) || (direction == 4) || (direction == 5))
            {
                ptR.X = Math.Max(ptR.X - 1, 0);
            }

            if ((direction == 1) || (direction == 2) || (direction == 3))
            {
                ptR.Y = Math.Min(ptR.Y + 1, ROOM_SIZE - 1);
            }
            else if ((direction == 5) || (direction == 6) || (direction == 7))
            {
                ptR.Y = Math.Max(ptR.Y - 1, 0);
            }

            if (((ptR.X != r.Location.X) || (ptR.Y != r.Location.Y)) && _roomCells[ptR.X, ptR.Y] == null)
            {
                _roomCells[r.Location.X, r.Location.Y] = null;
                _roomCells[ptR.X, ptR.Y] = r;
                r.Location = new RoomPoint(ptR.X, ptR.Y);
            }
        }