Esempio n. 1
0
 /// <summary>
 /// Returns a list of pirates that are within attack range of "loc", and their state is "state"
 /// </summary>
 public List <Pirate> GetPiratesInAttackRange(ILocateable loc, PirateState state)
 {
     if (!this.map.InMap(loc))
     {
         return(null);
     }
     return(new List <Pirate>(this.attackRangeMap[loc.GetLocation().Row, loc.GetLocation().Collumn, (int)state]));
 }
Esempio n. 2
0
 /// <summary>
 /// Tries to add a taken location to the ResourcesPack
 /// </summary>
 /// <returns>Was the addition successful</returns>
 public bool AddTakenLocation(ILocateable l)
 {
     if (l == null || l.GetLocation() == null)
     {
         return(false);
     }
     return(this.takenLocations.Add(l.GetLocation()));
 }
Esempio n. 3
0
 /// <summary>
 /// Tries to add a freed-up location to the ResourcesPack
 /// </summary>
 /// <returns>Was the addition successful</returns>
 public bool AddFreedLocation(ILocateable l)
 {
     if (l == null || l.GetLocation() == null)
     {
         return(false);
     }
     return(this.freedUpLocations.Add(l.GetLocation()));
 }
Esempio n. 4
0
        /// <summary>
        /// Returns a list of pirates that actually CAN attack, that are in attack range of loc
        /// </summary>
        public List <Pirate> GetDangerousPiratesInAttackRange(ILocateable loc)
        {
            if (!this.map.InMap(loc))
            {
                return(null);
            }

            return(this.attackRangeMap[loc.GetLocation().Row, loc.GetLocation().Collumn, (int)PirateState.Free].FindAll(p => p.CanAttack));
        }
Esempio n. 5
0
        /// <summary>
        /// Returns wether there are pirates whose state is "state", that are in attack range of "loc"
        /// </summary>
        public bool HasPiratesInAttackRange(ILocateable loc, PirateState state)
        {
            if (!this.map.InMap(loc))
            {
                return(false);
            }

            return(this.attackRangeMap[loc.GetLocation().Row, loc.GetLocation().Collumn, (int)state].Count > 0);
        }
Esempio n. 6
0
        /// <summary>
        /// Returns a list of neighbors in-map of the location given, that are away from the location as specified
        /// </summary>
        public List <Location> GetNeighbors(ILocateable l, int manhattanDistance)
        {
            if (l == null || l.GetLocation() == null ||
                manhattanDistance < 0 || manhattanDistance >= this.neighbors.GetLength(2) ||
                !this.InMap(l))
            {
                return(null);
            }

            return(new List <Location> (this.neighbors[l.GetLocation().Row, l.GetLocation().Collumn, manhattanDistance]));
        }
Esempio n. 7
0
 /// <summary>
 /// Returns the treasure whose initial location is on the location given, or null if one does not exist
 /// </summary>
 public Treasure GetTreasureSpawnOn(ILocateable loc)
 {
     if (loc == null)
     {
         return(null);
     }
     if (this.treasuresSpawnOnMap.ContainsKey(loc.GetLocation()))
     {
         return(this.treasuresSpawnOnMap[loc.GetLocation()]);
     }
     return(null);
 }
Esempio n. 8
0
 /// <summary>
 /// Returns the treasures which are on the location given
 /// </summary>
 public List <Treasure> GetTreasuresOn(ILocateable loc)
 {
     if (loc == null)
     {
         return(null);
     }
     if (this.treasuresOnMap.ContainsKey(loc.GetLocation()))
     {
         return(this.treasuresOnMap[loc.GetLocation()]);
     }
     return(new List <Treasure>());
 }
Esempio n. 9
0
 /// <summary>
 /// Returns the pirate whose initial location is on the location given, or null if one does not exist
 /// </summary>
 public Pirate GetPirateSpawnOn(ILocateable loc)
 {
     if (loc == null)
     {
         return(null);
     }
     if (this.piratesSpawnOnMap.ContainsKey(loc.GetLocation()))
     {
         return(this.piratesSpawnOnMap[loc.GetLocation()]);
     }
     return(null);
 }
Esempio n. 10
0
        /// <summary>
        /// Returns the closest point to the given point on the circle with radius r and given center
        /// </summary>
        private Location ClosestOnCircle(double r, ILocateable center, ILocateable point)
        {
            double x1       = center.GetLocation().Row;
            double y1       = center.GetLocation().Collumn;
            double x2       = point.GetLocation().Row;
            double y2       = point.GetLocation().Collumn;
            double distance = Game.EuclideanDistance(center, point);

            double x3 = (((r * x2) + (distance - r) * x1) / (distance));
            double y3 = (((r * y2) + (distance - r) * y1) / (distance));

            return(new Location(Utils.Round(x3), Utils.Round(y3)));
        }
Esempio n. 11
0
        /// <summary>
        /// Returns whether a, b and c are on the same straight line
        /// </summary>
        public static bool OnStraightLine(ILocateable a, ILocateable b, ILocateable c)
        {
            if (a == null || a.GetLocation() == null ||
                b == null || b.GetLocation() == null ||
                c == null || c.GetLocation() == null)
            {
                return(false);
            }

            return(Utils.OnStraightLine(a.GetLocation().Row, a.GetLocation().Collumn,
                                        b.GetLocation().Row, b.GetLocation().Collumn,
                                        c.GetLocation().Row, c.GetLocation().Collumn));
        }
Esempio n. 12
0
        /// <summary>
        /// Returns whether "c" is on a path between "a" and "b"
        /// </summary>
        public static bool OnTrack(ILocateable a, ILocateable b, ILocateable c)
        {
            if (a == null || a.GetLocation() == null ||
                b == null || b.GetLocation() == null ||
                c == null || c.GetLocation() == null)
            {
                return(false);
            }

            return(Utils.InSquare(a.GetLocation().Collumn, a.GetLocation().Row,
                                  b.GetLocation().Collumn, b.GetLocation().Row,
                                  c.GetLocation().Collumn, c.GetLocation().Row));
        }
Esempio n. 13
0
        /// <summary>
        /// Returns wether there are pirates that actually CAN attack, that are in attack range of loc
        /// </summary>
        public bool HasDangerousPiratesInAttackRange(ILocateable loc)
        {
            if (!this.map.InMap(loc))
            {
                return(false);
            }

            foreach (Pirate p in this.attackRangeMap[loc.GetLocation().Row, loc.GetLocation().Collumn, (int)PirateState.Free])
            {
                if (p.CanAttack)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 14
0
        //////////Methods//////////

        /// <summary>
        /// Creates a new sail command
        /// </summary>
        /// <param name="pirate">The pirate that will sail</param>
        /// <param name="immediateDestionation">The IMMEDIATE destination of the pirate</param>
        public SailCommand(Pirate pirate, ILocateable immediateDestionation)
            : base(pirate)
        {
            this.Pirate = pirate;
            this.ImmediateDestination = immediateDestionation.GetLocation();

            this.distance = Game.ManhattanDistance(this.Pirate, this.ImmediateDestination);
        }
Esempio n. 15
0
        /// <summary>
        /// Returns the naive sail options that the native game object returns
        /// </summary>
        public List <Location> GetNaiveSailOptions(Pirate pirate, ILocateable destination, int moves)
        {
            if (pirate == null || destination == null || destination.GetLocation() == null)
            {
                return(new List <Location>());
            }

            Pirates.Pirate   nativePirate      = pirate.Owner == Owner.Me ? this.game.GetMyPirate(pirate.Id) : this.game.GetEnemyPirate(pirate.Id);
            Pirates.Location nativeDestination = new Pirates.Location(destination.GetLocation().Row, destination.GetLocation().Collumn);
            List <Location>  res = new List <Location>();

            foreach (Pirates.Location loc in this.game.GetSailOptions(nativePirate, nativeDestination, moves))
            {
                res.Add(new Location(loc.Row, loc.Col));
            }
            return(res);
        }
Esempio n. 16
0
        /// <summary>
        /// Returns wether the location given is inside the map
        /// </summary>
        public bool InMap(ILocateable l)
        {
            Location loc = l.GetLocation();

            return(loc.Row >= 0 &&
                   loc.Row < this.Rows &&
                   loc.Collumn >= 0 &&
                   loc.Collumn < this.Collumns);
        }
Esempio n. 17
0
        /// <summary>
        /// Returns a list of all the pirates which have one of the "states" given, ordered by their manhattan distance to "location"
        /// </summary>
        public List <Pirate> GetClosestPirates(ILocateable location, params PirateState[] states)
        {
            if (location == null || location.GetLocation() == null)
            {
                return(new List <Pirate>());
            }

            return(this.GetPirates(states).OrderBy(pirate => Map.ManhattanDistance(pirate, location)).ToList());
        }
Esempio n. 18
0
        /// <summary>
        /// Returns if the given locations are in (euclidean) range of each other
        /// </summary>
        public static bool InEuclideanRange(ILocateable a, ILocateable b, double range)
        {
            if (a == null || a.GetLocation() == null ||
                b == null || b.GetLocation() == null)
            {
                return(false);
            }

            return(Map.EuclideanDistance(a, b) <= range);
        }
Esempio n. 19
0
        //////////Methods//////////

        /// <summary>
        /// Creates a new treasure with the parameters given.
        /// carryingPirate can be null.
        /// </summary>
        public Treasure(int id, ILocateable location, TreasureState state, Pirate carryingPirate, int value)
        {
            this.Id = id;
            this.InitialLocation = location.GetLocation();
            this.location        = this.InitialLocation;
            this.state           = state;
            this.carryingPirate  = carryingPirate;
            this.Value           = value;

            this.nativeObject = null;
        }
Esempio n. 20
0
        /// <summary>
        /// Returns the euclidean distance between two locations
        /// </summary>
        public static double EuclideanDistance(ILocateable a, ILocateable b)
        {
            if (a == null || b == null)
            {
                return(-1);
            }

            Location loc1 = a.GetLocation();
            Location loc2 = b.GetLocation();

            if (loc1 == null || loc2 == null)
            {
                return(-1);
            }

            return(Utils.EuclideanDistance(loc1.Row, loc1.Collumn, loc2.Row, loc2.Collumn));
        }
Esempio n. 21
0
        /// <summary>
        /// Returns a list of possible immediate destinations for the sail and the calculated distance to the destination,
        ///     that uses every number of moves satisfing minMoves &lt;= moves &lt;= maxMoves
        /// </summary>
        /// <param name="from">The start location</param>
        /// <param name="to">The destination</param>
        /// <param name="minToDistance">The minimum distance from the destination, which is OK to get to</param>
        /// <param name="maxToDistance">The maximum distance from the destination, which is OK to get to</param>
        /// <param name="maxDistancePerTurn">the maximum amount of distance a pirate can travel in a turn</param>
        /// <param name="dontStayInRange">should you avoid staying in the attack range of an enemy pirate</param>
        /// <param name="minMoves">The minimum number of moves to use</param>
        /// <param name="maxMoves">The maximum number of moves to use (or less, if the destination is in reach from the start location)</param>
        /// <param name="maxResultsForMovesNum">The maximum number of results to include, for each number of moves being checked</param>
        /// <param name="disallowedTerrain">the list of disallowed terrain to NOT walk on</param>
        /// <returns>A list of possible immediate destinations for the sail and their calculated distance to the destination</returns>
        public List <KeyValuePair <Location, int> > GetCompleteSailOptions(ILocateable from, ILocateable to, int minToDistance, int maxToDistance, int maxDistancePerTurn, bool dontStayInRange, int minMoves, int maxMoves, int maxResultsForMovesNum, params Terrain[] disallowedTerrain)
        {
            // sanity check
            if (from == null || from.GetLocation() == null || !this.map.InMap(from) ||
                to == null || to.GetLocation() == null || !this.map.InMap(to))
            {
                return(new List <KeyValuePair <Location, int> >());
            }

            // the attack map of the enemy
            AttackMap enemyAttackMap = this.map.EnemyAttackMap;

            // do the location stays in attack range
            System.Predicate <Location> staysInAttackRange =
                loc => enemyAttackMap.GetDangerousPiratesInAttackRange(loc).Intersect(
                    enemyAttackMap.GetDangerousPiratesInAttackRange(from)).Count() > 0;

            // use at most "distance" moves to reach destination
            int distance = Map.ManhattanDistance(from, to);

            if (distance < maxMoves)
            {
                maxMoves = distance;
            }

            // get all the possible results
            List <Location> possibleResults = new List <Location>();

            for (int moves = minMoves; moves <= maxMoves; moves++)
            {
                possibleResults.AddRange(this.map.GetNeighbors(from, moves));
            }
            // remove the results that stay in attack range if needed
            if (dontStayInRange)
            {
                possibleResults.RemoveAll(staysInAttackRange);
            }

            // get all the possible final destinations
            List <Location> finalDestinations = new List <Location>();

            for (int radius = minToDistance; radius <= maxToDistance; radius++)
            {
                finalDestinations.AddRange(this.map.GetNeighbors(to, radius));
            }

            // update A* algorithm
            this.Update_AStar(finalDestinations, possibleResults, maxDistancePerTurn, disallowedTerrain);

            // calculate the results
            List <KeyValuePair <Location, int> > results = new List <KeyValuePair <Location, int> >();

            // for each possible "moves" value
            for (int moves = minMoves; moves <= maxMoves; moves++)
            {
                // choose only best results
                possibleResults.Clear();
                possibleResults.AddRange(this.map.GetNeighbors(from, moves));
                // remove the results that stay in attack range if needed
                if (dontStayInRange)
                {
                    possibleResults.RemoveAll(staysInAttackRange);
                }
                possibleResults.Shuffle();                                                      // shuffle the neighbors, so that two neighbors with the same gScore will be randomly ordered
                possibleResults = possibleResults.OrderBy(loc => this.getGScore(loc)).ToList(); // sort by gScore
                for (int i = 0; i < possibleResults.Count && i < maxResultsForMovesNum; i++)
                {
                    if (this.getGScore(possibleResults[i]) >= this.aStar_gScore.Length) // if the value is "infinity"
                    {
                        break;
                    }
                    // add the result, and its distance to the destination
                    results.Add(new KeyValuePair <Location, int>(possibleResults[i], this.getGScore(possibleResults[i])));
                }
            }
            return(results);
        }