public static void Attack(Beaver attacker, IEnumerable <Beaver> targets) { var targettables = targets.Where(t => t.CanBeAttacked()); var target = targettables.FirstOrDefault(t => attacker.Tile._HasNeighbor(t.Tile)); if (target != null) { while (attacker.CanAct() && target.CanBeAttacked()) { if (attacker.Owner == target.Owner) { Console.WriteLine("DIE SLACKER"); } attacker.Attack(target); } } }
/// <summary>Attacks enemy beavers</summary> private bool Attack(Beaver b) { if (b.Actions <= 0) { return(false); } bool retValue = false; // Find suitable building spot Func <Tile, bool> isTarget = t => t.Beaver != null && t.Beaver.Owner != this.Player && t.Beaver.Recruited; IEnumerable <Tile> pfResult; if (this.NextLodge == null) { // Look for a spot two spaces away from an unclaimed food spot pfResult = this.FindPathCustom(b.Tile, (t, parent) => { double cost = this.GetCost(t, parent, b); if (cost < 0) { return(cost); } return(this.IsNextTo(t, isTarget) ? 0 : cost); }) .Skip(1); // Look for a spot two spaces away from any unclaimed resource if (!pfResult.Any()) { pfResult = this.FindPathCustom(b.Tile, (t, parent) => { double cost = this.GetCost(t, parent, b); if (cost < 0) { return(cost); } if (isTarget(t)) { return(0); } return(cost); }) .Skip(1); } // Set this as the next lodge spot if (pfResult.Any()) { this.NextLodge = pfResult.Last(); } } else { pfResult = this.FindPathCustom(b.Tile, this.NextLodge).Skip(1); } // Traverse path LinkedList <Tile> path = new LinkedList <Tile>(pfResult); while (path.Any()) { // If can't move if (b.Moves < b.MoveCost(path.First()) || !b.Move(path.First())) { break; } path.RemoveFirst(); retValue = true; } // Try to attack a beaver Tile target = this.GetNeighborWhere(b.Tile, isTarget); if (target != null) { while (b.Actions > 0 && b.Health > target.Beaver.Job.Damage) { b.Attack(target.Beaver); } } return(retValue); }