/// <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); }
/// <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; }
/// <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); }
/// <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); }
/// <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; }
internal void IncreaseRessources(Coordinates position, short ressources) { Grid[position.X, position.Y].RessourceLevel += ressources; }
/// <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; } }
///// <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); }
/// <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); }
/// <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); }
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; }
/// <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); } }
/// <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; }
internal Int16 GetLandscapeHeight(Coordinates position) { return Grid[position.X, position.Y].Height; }
internal void LowerLandscape(Coordinates position) { Grid[position.X, position.Y].Height--; }
internal void RaiseLandscape(Coordinates position) { Grid[position.X, position.Y].Height++; }
/// <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; }
/// <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); }
/// <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"); }
/// <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(); }
/// <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"); }
/// <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); } }
/// <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)); }