public static void Pickup(Beaver picker, IEnumerable <Tile> targets, string resource) { if (!picker.CanAct() || picker.OpenCarryCapacity() == 0) { return; } var targettables = targets.Where(t => t.GetCount(resource) > 0 && picker.Tile._HasNeighbor(t)); if (targettables.Any()) { var target = targettables.MaxByValue(t => t.GetCount(resource)); picker.Pickup(target, resource, Math.Min(target.GetCount(resource), picker.OpenCarryCapacity())); } }
/// <summary>Picks up resource from owned lodges</summary> private bool Pickup(Beaver b, string resource) { if (b.Branches + b.Food >= b.Job.CarryLimit) { return(false); } bool keepGoing = false; // Go to nearest lodge with the given resource Func <Tile, bool> isTarget = t => t.LodgeOwner == this.Player && resource == "branches" ? t.Branches > 0 : t.Food > 0; // 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); } if (this.IsNextTo(t, isTarget)) { return(0); } return(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 pick up resources if next to the target lodge Tile target = this.GetNeighborWhere(b.Tile, isTarget); if (target != null && b.Actions > 0) { int amount = resource == "food" ? target.Food : target.Branches; if (target.LodgeOwner == this.Player) { amount--; } amount = Math.Min(amount, b.Job.CarryLimit - b.Branches - b.Food); if (amount > 0) { if (AI.Verbose) { Console.WriteLine("Picking up resource"); } b.Pickup(target, resource, amount); keepGoing = true; } } return(keepGoing); }