/// <summary> /// Shows an attacking overlay for armies. /// </summary> /// <param name="a">The army which needs an attacking overlay to show.</param> protected void SetArmyAttackingOverlay(Army a) { // Use a swordsman, as it has a range of 1. Soldier swordsman = SoldierRegistry.GetSoldier("unit.swordsman.1", a.Owner); swordsman.PositionInGrid = a.PositionInGrid; SetUnitAttackingOverlay(swordsman); }
// Creates a puppet unit to check if every tile is accessible from a certain unobstructed tile private bool CheckIfEveryTileIsAccessible(Configuration file, int gridWidth, int gridHeight) { // Puts a unit on the first available tile int startx = 0; int starty = 0; while ((this[startx, starty] as Tile).Terrain == Terrain.Mountain || (this[startx, starty] as Tile).Terrain == Terrain.Lake) { if (startx < gridWidth - 1) { startx++; } else { startx = 0; starty++; } } Unit u = SoldierRegistry.GetSoldier("unit.swordsman.1", new Player("", "Blue", Color.Blue, 1)); u.Parent = this; u.MovesLeft = u.MoveSpeed = int.MaxValue; (this[startx, starty] as Tile).SetUnit(u); Point[] reachableTiles = Pathfinding.ReachableTiles(u, gridWidth, gridHeight); // Tests if every other nonobstructed tile is accessible by this unit for (int gridX = 0; gridX < gridWidth; ++gridX) { for (int gridY = 0; gridY < gridHeight; ++gridY) { Tile t = this[gridX, gridY] as Tile; Terrain terrain = t.Terrain; if (u.Passable(terrain) && !(reachableTiles.Contains(t.PositionInGrid) || t.PositionInGrid.Equals(u.PositionInGrid))) { mountainArray = ResetMountains(file, gridWidth, gridHeight); lakeArray = ResetLakes(file, gridWidth, gridHeight); t.SetUnit(null); return(false); } else { t.SetUnit(null); } } } return(true); }
protected void TrySpawnUnit(string soldierType) { // Check if the player can afford this soldier int cost = SoldierRegistry.GetSoldierCost(soldierType); if (!owner.CanAfford(cost) || owner.Population <= 0) { return; } // Set this soldier in the world if possible Tile currentTile = ((GameWorld as Grid)[positionInGrid] as Tile); if (!currentTile.Occupied) { // Nothing here, just place it in this square Army army = new Army(positionInGrid.X, positionInGrid.Y, owner); army.AddSoldier(SoldierRegistry.GetSoldier(soldierType, owner)); currentTile.SetUnit(army); } else if (currentTile.Unit.Owner == owner && currentTile.Unit is Army) { // Our own army is here, just place it in there :) Army a = currentTile.Unit as Army; if (a.GetTotalUnitCount() >= 20) { return; } a.AddSoldier(SoldierRegistry.GetSoldier(soldierType, owner)); } else { // We can't place it, just stop this whole function return; } // Buy the soldier, as we placed it. owner.Buy(cost); owner.CalculatePopulation(); }
/// <summary> /// Populates the field with the armies of both attacker and defender. /// </summary> /// <param name="attacker">The attacking Army.</param> /// <param name="defender">The defending Army.</param> public void PopulateField(Army attacker, Army defender) { // Initialize which players are the attacker and the defender attackingPlayer = attacker.Owner; defendingPlayer = defender.Owner; // Initialize the defender's field int currentX = 0; int currentY = 0; // Iterate over every type of Soldier in the defending Army foreach (string s in defender.UnitsAndCounts.Keys) { // Get the amount of this kind of Soldier int soldierCount = defender.UnitsAndCounts[s]; // Place them in a position based on how many soldiers we have passed so far. for (; soldierCount > 0; --soldierCount) { //only places soldier if terrain is passable Soldier soldier = SoldierRegistry.GetSoldier(s, defender.Owner); while (!(this[currentX, currentY] as Tile).Passable(soldier)) { ++currentX; if (currentX >= Width) { currentX = 0; ++currentY; } } (this[currentX, currentY] as Tile).SetUnit(soldier); ++currentX; // Make sure we don't create a line of soldiers longer than the field. if (currentX >= Width) { currentX = 0; ++currentY; } } } // Do the same for the attacking Army, except start bottom right and go left->up for each Soldier. currentX = Width - 1; currentY = Height - 1; foreach (string s in attacker.UnitsAndCounts.Keys) { int soldierCount = attacker.UnitsAndCounts[s]; for (; soldierCount > 0; --soldierCount) { //only places soldier if terrain is passable Soldier soldier = SoldierRegistry.GetSoldier(s, attacker.Owner); while (!(this[currentX, currentY] as Tile).Passable(soldier)) { --currentX; if (currentX < 0) { currentX = Width - 1; --currentY; } } (this[currentX, currentY] as Tile).SetUnit(soldier); --currentX; if (currentX < 0) { currentX = Width - 1; --currentY; } } } // And clear all target positions after we populated the field. ClearAllTargetPositions(); CurrentPlayer = attacker.Owner; }