Ejemplo n.º 1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="coordinates"></param>
 public SurroundingView(Coordinates coordinates, MapTile[,] view)
 {
     // Set the center coordinate
     _centerOfView = coordinates;
     _view = new Map(Convert.ToInt16(view.GetUpperBound(0)), Convert.ToInt16(view.GetUpperBound(1)));
     _view.InitializeGrid(view);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Cell constructor
 /// </summary>
 /// <param name="x">The x position where the cell is spawned</param>
 /// <param name="y">The y position where the cell is spawned</param>
 /// <param name="initialLife">life the cell is going to spawn with</param>
 /// <param name="thisWorld">A reference to the world the cell lives in</param>
 /// <param name="teamColor">The color of the team this cell belongs to</param>
 public Cell(Int16 x, Int16 y, Int16 initialLife, World thisWorld, Color teamColor)
 {
     Position = new Coordinates(x, y);
     _brain = new SwarmBrain();
     _life = initialLife;
     _world = thisWorld;
     _team = teamColor;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Registers the movement of the cell
        /// </summary>
        /// <param name="oldCoordinates">Coordinates where the cell was before it moved</param>
        /// <param name="newCoordinates">Coordinates where the cell is after it moved</param>
        /// <param name="team">Team Color of the cell</param>
        internal void RegisterCellMovement(Coordinates oldCoordinates, Coordinates newCoordinates, Color team)
        {
            // Add the cell movements to the logs
            if (!_updatedElements.ContainsKey(oldCoordinates))        
                _updatedElements.Add(oldCoordinates, Color.Black);

            if (_updatedElements.ContainsKey(newCoordinates) && _updatedElements[newCoordinates] != Color.Black)
            {
                throw new InvalidOperationException("Trying to move a cell to a position where a cell already resides");
            }
            else
                _updatedElements.Add(newCoordinates, team);

            // Update the map
            this._masterMap.MoveCell(oldCoordinates, newCoordinates);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Function extracting a rectangular section of the map
 /// </summary>
 /// <param name="centerPoint">The point on which the section shall be centered</param>
 /// <param name="subWidth">The odd numbered width of the section</param>
 /// <param name="subHeight">The odd numbered height of the section</param>
 /// <returns>A 2D list MapTiles</returns>
 public MapTile[,] GetSubset(Coordinates centerPoint, short subWidth, short subHeight)
 {
     // Check input parameters
     if (null == centerPoint
         || 0 == subWidth % 2 || 0 == subHeight % 2
         || subWidth < MinimumMapSize || subHeight < MinimumMapSize
         || subHeight > Height || subWidth > Width)
         return null;
     
     // Get the number of rows / columns that there are on the side of the cell on the extract
     var numberOfSideColumns = (short)Math.Truncate((float)(subWidth / 2));
     var numberOfSideRows = (short) Math.Truncate((float) (subHeight/2));
     // Get the top left coordinates of the extract
     var smallGridMinX = centerPoint.X - numberOfSideColumns > 0 ? Convert.ToInt16(centerPoint.X - numberOfSideColumns) : (Int16)0;
     var smallGridMinY = centerPoint.Y - numberOfSideRows > 0 ? Convert.ToInt16(centerPoint.Y - numberOfSideRows) : (Int16)0;
     // Get the bottom right coordinates of the extract (-1 since we are working with a 0 based array)
     var smallGridMaxX = smallGridMinX + subWidth >= Grid.GetLength(0) ? Convert.ToInt16(Grid.GetLength(0) - 1) : Convert.ToInt16(smallGridMinX + subWidth -1);
     var smallGridMaxY = smallGridMinY + subHeight >= Grid.GetLength(1) ? Convert.ToInt16(Grid.GetLength(1) - 1) : Convert.ToInt16(smallGridMinY + subHeight -1);
     
     return GetSubArray(smallGridMinX, smallGridMaxX, smallGridMinY, smallGridMaxY);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Notify the world that a cell moved
 /// </summary>
 /// <param name="oldCoordinates">The old coordinates where the cell was</param>
 /// <param name="newCoordinates">The new coordinates where the cell is</param>
 /// <param name="team">The color the team is on</param>
 private void NotifyMovement(Coordinates oldCoordinates, Coordinates newCoordinates, Color team)
 {
     _world.RegisterCellMovement(oldCoordinates, newCoordinates, team);
     return;
 }
Ejemplo n.º 6
0
 internal void IncreaseRessources(Coordinates position, short ressources)
 {
     Grid[position.X, position.Y].RessourceLevel += ressources;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Function moving a cell on the map
 /// </summary>
 /// <param name="oldCoordinates">The coordinates where the cell is moving from</param>
 /// <param name="newCoordinates">The coordinates where the cell is moving to</param>
 internal void MoveCell(Coordinates oldCoordinates, Coordinates newCoordinates)
 {
     if (CoordinatesAreValid(oldCoordinates) && CoordinatesAreValid(newCoordinates))
     {
         Grid[newCoordinates.X, newCoordinates.Y].CellReference = Grid[oldCoordinates.X, oldCoordinates.Y].CellReference;
         Grid[oldCoordinates.X, oldCoordinates.Y].CellReference = null;
     }
 }
Ejemplo n.º 8
0
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //internal ICoordinates GetClosestRessourcePool()
        //{
        //    IList<ICoordinates> ressources = this._view.GetRessourcesList();
        //    ICoordinates closestRessource = null;
        //    Int16? minDistance = null;
        //    foreach(ICoordinates spot in ressources)
        //    {
        //        if (closestRessource == null)
        //        {
        //            closestRessource = spot;
        //            minDistance = this._centerOfView.DistanceTo(spot);
        //        }
        //        if (spot.DistanceTo(closestRessource) < minDistance)
        //        {
        //            closestRessource = spot;
        //            minDistance = this._centerOfView.DistanceTo(spot);
        //        }
        //    }
        //    return null;
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        internal IOffsetVector GetClosestRessourcePool(ICoordinates coordinatesFrom, ICell cell)
        {
            ICoordinates coordinatesTo = null;
            Int16? minDistanceSofar = (Int16)this.View.Grid.GetLength(0);

            for (int i = 0; i < this.View.Grid.GetLength(0); i++)
            {
                for (int j = 0; j < this.View.Grid.GetLength(1); j++)
                {
                    if (this.View.Grid[i, j].RessourceLevel > 0)
                    {
                        var coord = new Coordinates();
                        coord.SetCoordinates((Int16)i, (Int16)j);
                        Int16? distTo = this.CellPositionInView.DistanceTo(coord);
                        if (distTo < minDistanceSofar)
                        {
                            minDistanceSofar = distTo;
                            coordinatesTo = coord;
                        }
                    }
                }
            }

            return coordinatesTo == null ? null : new OffsetVector(coordinatesFrom, coordinatesTo);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Sets the position of the current element
 /// </summary>
 /// <param name="x">x coordinate of the element</param>
 /// <param name="y">y coordinate of the element</param>
 /// <returns></returns>
 internal void SetPositions(short x, short y)
 {
     _position = new Coordinates(x, y);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Increase the amount of ressources of the given amount at the given position
 /// </summary>
 /// <param name="position">The position where to perform the drop</param>
 /// <param name="life">The amount of ressources to drop</param>
 internal void DropRessources(Coordinates position, Int16 life)
 {
     _masterMap.IncreaseRessources(position, life);
 }
Ejemplo n.º 11
0
        private bool CoordinatesAreValid(Coordinates newCoordinates)
        {
            if (newCoordinates.X < 0
                || newCoordinates.Y < 0
                || newCoordinates.X >= this.Grid.GetLength(0)
                || newCoordinates.Y >= this.Grid.GetLength(1))
                return false;

            return true;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Tries to attack a cell in its direct proximity
        /// </summary>
        /// <param name="offsetToTarget"></param>
        private void Attack(IOffsetVector offsetToTarget)
        {
            if (offsetToTarget == null)
                return;

            ICoordinates coordinates = new Coordinates(this.Position.X, this.Position.Y);
            coordinates.X += offsetToTarget.X;
            coordinates.Y += offsetToTarget.Y;

            IInternalCell target = this.world.GetMap().GetCellAt(coordinates);

            if (target != null && target.GetLife() > 0)
            {
                target.DecreaseLife(Settings.Default.DamageOnOpponent);
            }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// 
 /// </summary>
 /// <returns></returns>
 private ICoordinates GetRandomCoordinates()
 {
     ICoordinates coord = new Coordinates();
     coord.X = RandomGenerator.GetRandomInt16((Int16)(Settings.Default.WorldWidth - 1));
     coord.Y = RandomGenerator.GetRandomInt16((Int16)(Settings.Default.WorldHeight - 1));
     Debug.WriteLine(coord.X.ToString() + " " + coord.Y.ToString());
     return coord;
 }
Ejemplo n.º 14
0
 internal Int16 GetLandscapeHeight(Coordinates position)
 {
     return Grid[position.X, position.Y].Height;
 }
Ejemplo n.º 15
0
 internal void LowerLandscape(Coordinates position)
 {
     Grid[position.X, position.Y].Height--;
 }
Ejemplo n.º 16
0
 internal void RaiseLandscape(Coordinates position)
 {
     Grid[position.X, position.Y].Height++;
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Function implanting ressources on the map
 /// </summary>
 /// <param name="coordinates">The coordinates where to implant the ressources</param>
 /// <param name="ressourceLevel">The amount of ressources to implant</param>
 /// <param name="growthRate">The growth rate of the ressources (per tick, 0 per default)</param>
 internal void ImplantRessources(Coordinates coordinates, Int16 ressourceLevel, Int16 growthRate = 0)
 {
     Grid[coordinates.X, coordinates.Y].GrowthRate = growthRate;
     Grid[coordinates.X, coordinates.Y].RessourceLevel = ressourceLevel;
 }
Ejemplo n.º 18
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="spawnLife"></param>
        /// <param name="cell"></param>
        public void CreateSpawns(short spawnLife, Cell cell)
        {
            // We don't create spawns at the same place
            ICoordinates positionSpawn = new Coordinates();
            positionSpawn.X = cell.Position.X;
            positionSpawn.Y = cell.Position.Y;

            if (Helper.CoordinatesAreValid((Int16)(positionSpawn.X + 1), positionSpawn.Y))
                positionSpawn.X += 1;
            else
                positionSpawn.X += -1;

            this.CreateCell(cell.GetAttachedBrainType(), cell.GetTeamQualifier(), spawnLife, cell.Position);
            this.CreateCell(cell.GetAttachedBrainType(), cell.GetTeamQualifier(), spawnLife, positionSpawn);
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Increases the landscape height at the given position
 /// </summary>
 /// <param name="position">The coordinates where to raise the landscape</param>
 /// <remarks>The function throws an InvalidOperationException in case the operation cannot be performed</remarks>
 internal void RaiseLandscape(Coordinates position)
 {
     if (_masterMap.GetLandscapeHeight(position) >= _maxLandscapeHeight)
         _masterMap.RaiseLandscape(position);
     else
         throw new InvalidOperationException("Landscape is already at its maximum at this location");
 }
Ejemplo n.º 20
0
        /// <summary>
        /// Extracts a sub array from the array passed as a parameter
        /// </summary>
        /// <param name="xMin">The "top left" x bound of the array to extract</param>
        /// <param name="xMax">The "top left" y bound of the array to extract</param>
        /// <param name="yMin">The "bottom right" x bound of the array to extract</param>
        /// <param name="yMax">The "bottom right" y bound of the array to extract</param>
        /// <returns>The sub 2D array</returns>
        private MapTile[,] GetSubArray(Int16 xMin, Int16 xMax, Int16 yMin, Int16 yMax)
        {
            ICoordinates cMin = new Coordinates();
            cMin.SetCoordinates(xMin, yMin);
            ICoordinates cMax = new Coordinates();
            cMax.SetCoordinates(xMax, yMax);

            if (CoordinatesAreValid(cMin) && CoordinatesAreValid(cMax))
            {
                // Create a new map (add +1 to the dimention since it is a 0 based array)
                var newMap = new MapTile[(Int16)(xMax - xMin +1), (Int16)(yMax - yMin + 1)];
                for (int i = xMin; i <= xMax; i++)
                    for (int j = yMin; j <= yMax; j++)
                        newMap[i - xMin, j - yMin] = Grid[i, j];
                return newMap;
            }
            else
                throw new Exception();
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Lowers the landscape height at the given position
 /// </summary>
 /// <param name="position">The coordinates where to lower the landscape</param>
 /// <remarks>The function throws an InvalidOperationException in case the operation cannot be performed</remarks>
 internal void LowerLandscape(Coordinates position)
 {
     if (_masterMap.GetLandscapeHeight(position) <= _minLandscapeHeight)
         _masterMap.LowerLandscape(position);
     else
         throw new InvalidOperationException("Landscape is already at its minimum at this location");
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Tries to eat ressources present at the given coordinates
        /// </summary>
        /// <param name="offsetToTarget"></param>
        private void Eat(IOffsetVector offsetToTarget)
        {
            if (offsetToTarget == null)
                return;

            ICoordinates coordinates = new Coordinates(this.Position.X, this.Position.Y);
            coordinates.X += offsetToTarget.X;
            coordinates.Y += offsetToTarget.Y;

            Int16 ressourcesLeft = this.world.GetAmountOfRessourcesLeft(coordinates);
            Int16 lifeBonus = ressourcesLeft < Settings.Default.MaxEatingPerRoundQuantity ? ressourcesLeft : Settings.Default.MaxEatingPerRoundQuantity;

            if (lifeBonus > 0)
            {
                this.world.ReduceRessources(coordinates, lifeBonus);
                this.IncreaseLife(lifeBonus);
            }
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Paint a single pixel of color at the given coordinates
 /// </summary>
 /// <param name="coordvector">The coordinates where to paint</param>
 /// <param name="color">The color in which to paint</param>
 private void PaintSinglePixel(Coordinates coordvector, Color color)
 {
     _canvas.FillRectangle(
         new SolidBrush(color),
         new Rectangle(coordvector.X * _pixelSize, coordvector.Y * _pixelSize, _pixelSize, _pixelSize));
 }