/// <summary>Harvests resource</summary> private bool Harvest(Beaver b, string resource) { // Don't bother harvesting if it can't hold anymore stuff if (b.Branches + b.Food >= b.Job.CarryLimit) { return(false); } bool keepGoing = false; // Function for finding movement targets Func <Tile, bool> isTarget = t => t.Spawner != null && t.Spawner.Type == resource && !t.Spawner.HasBeenHarvested && t.Spawner.Health > 3; // Find nearest resource, skipping current node IEnumerable <Tile> basePath = 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); // Traverse path LinkedList <Tile> path = new LinkedList <Tile>(basePath); while (path.Any()) { // If can't move anymore if (b.Moves < b.MoveCost(path.First()) || !b.Move(path.First())) { break; } path.RemoveFirst(); keepGoing = true; } // Try to harvest if next to the target spawner Tile target = this.GetNeighborWhere(b.Tile, isTarget); if (target != null && b.Actions > 0) { if (AI.Verbose) { Console.WriteLine("Harvesting resource"); } b.Harvest(target.Spawner); keepGoing = true; } return(keepGoing); }
public static void Harvest(Beaver harvester, IEnumerable <Spawner> targets) { if (!harvester.CanAct() || harvester.OpenCarryCapacity() == 0) { return; } var targettables = targets.Where(t => t.Health > 0); var target = targettables.FirstOrDefault(t => harvester.Tile._HasNeighbor(t.Tile)); if (target != null) { while (harvester.CanAct() && target.Health > 0) { harvester.Harvest(target); } } }