public int CalculateValue(ResourceCount resources) { // Give a negative value to null. It's decidedly less valuable than any existing state. if (resources == null) { return(-1); } // We are assuming that dead states (0 energy) won't show up. If we wanted to support that, we'd have to check specifically for it and give it a negative value too. // (but greater than the value for null) int value = 0; foreach (ConsumableResourceEnum currentResource in Enum.GetValues(typeof(ConsumableResourceEnum))) { value += resources.GetAmount(currentResource) * FixedResourceValues[currentResource]; } return(value); }
public ExecutionResult Execute(SuperMetroidModel model, InGameState inGameState, int times = 1, bool usePreviousRoom = false) { var requirementsResult = FarmCycle.RequirementExecution.Execute(model, inGameState, times: times, usePreviousRoom: usePreviousRoom); // Can't even execute one cycle, so return a failure if (requirementsResult == null) { return(null); } // Build a dictionary of resources that are spent while doing the execution of a cycle ResourceCount resourceVariation = requirementsResult.ResultingState.GetResourceVariationWith(inGameState); // Start with all consumable resources IDictionary <ConsumableResourceEnum, int> costingResources = Enum.GetValues(typeof(ConsumableResourceEnum)) .Cast <ConsumableResourceEnum>() // Invert the resource variation to convert it to a resource cost .Select(resource => (resource: resource, cost: resourceVariation.GetAmount(resource) * -1)) // Keep only pairs where some of the resource has been spent .Where(resourceCost => resourceCost.cost > 0) // Finally, build a dictionary from the pairs .ToDictionary(resourceCost => resourceCost.resource, resourceCost => resourceCost.cost); // Identify all resources that can be refilled by this farm cycle IEnumerable <ConsumableResourceEnum> farmableResources = ComputeFarmableResources(model, costingResources); // Calculate the initial effective drop rates, taking into account currently full resources. // However, resources that are full but not farmable here should not be taken into account. IEnumerable <ConsumableResourceEnum> fullResources = inGameState.GetFullConsumableResources().Intersect(farmableResources).ToArray(); EnemyDrops initialEffectiveDropRates = FarmCycle.RoomEnemy.Enemy.GetEffectiveDropRates(model, fullResources); // Build a dictionary containing the variation per cycle for each consmable resource IDictionary <ConsumableResourceEnum, decimal> resourceVariationPerCycle = Enum.GetValues(typeof(ConsumableResourceEnum)) .Cast <ConsumableResourceEnum>() .ToDictionary(resource => resource, resource => CalculateResourceVariationPerCycle(model, resource, initialEffectiveDropRates, costingResources)); // Identify resources that are creeping down as we farm IEnumerable <ConsumableResourceEnum> initiallyUnstableResources = costingResources .Where(pair => resourceVariationPerCycle[pair.Key] < 0) .Select(pair => pair.Key) .ToArray(); // If there's no resources we can farm, just return now if (!farmableResources.Any()) { // If any of the resources initially lose out per farm cycle, we're not even able to farm. Return a failure. if (initiallyUnstableResources.Any()) { return(null); } // Otherwise, we're able to farm but it doesn't do anything according to logical options return(new ExecutionResult(inGameState.Clone())); } // If there's no resource that initially loses out, we're not concerned about losing any resources. // We can refill all farmable resources and report a success if (!initiallyUnstableResources.Any()) { return(ExecuteRefill(model, inGameState, farmableResources)); } // If we have resources that initially lose out, they must eventually turn farmable. // Otherwise, we consider this a failure. if (initiallyUnstableResources.Except(farmableResources).Any()) { return(null); } // Now we know we have at least one resource that currently loses out per cycle, but can eventually recharge. // Execute some farming to see if we can stabilize those resources before we run out. IEnumerable <ConsumableResourceEnum> notFullFarmableResources = farmableResources.Except(fullResources).ToArray(); IDictionary <ConsumableResourceEnum, decimal> resourceCounts = Enum.GetValues(typeof(ConsumableResourceEnum)) .Cast <ConsumableResourceEnum>() .ToDictionary(resource => resource, resource => (decimal)inGameState.GetCurrentAmount(resource)); EnemyDrops effectiveDropRates = initialEffectiveDropRates; // Execute farm cycles until a resource runs out or all costing resources have stabilized while (costingResources .Where(pair => resourceVariationPerCycle[pair.Key] < 0) .Any()) { // Figure out how many cycles we need to execute in order to refill something farmable and stable int cyclesToRefillSomething = notFullFarmableResources.Select(resource => decimal.ToInt32(decimal.Ceiling((inGameState.GetMaxAmount(resource) - resourceCounts[resource]) / resourceVariationPerCycle[resource]))) .Where(cycleCount => cycleCount > 0) .Min(); // Apply to each farmable resource the resource variation from executing that many cycles. // We don't care if it goes over maximum since we won't apply these to the in-game state foreach (ConsumableResourceEnum resource in notFullFarmableResources) { resourceCounts[resource] += resourceVariationPerCycle[resource] * cyclesToRefillSomething; } // If an unstable resource has dipped below the cost per cycle, we can't go on. Return a failure. if (costingResources.Where(costingResource => resourceCounts[costingResource.Key] < costingResource.Value).Any()) { return(null); } // If we haven't run out of anything, prepare the next loop // Update full resources fullResources = resourceCounts .Where(pair => pair.Value >= inGameState.GetMaxAmount(pair.Key)) .Select(pair => pair.Key) .Intersect(farmableResources) .ToArray(); // Update farmable resources by excluding newly-full resources notFullFarmableResources = notFullFarmableResources.Except(fullResources).ToArray(); // Calculate a new effective drop rate using the new list of full resources // If that new effective drop rate stabilizes all unstable resources, we'll make it out of the loop effectiveDropRates = model.Rules.CalculateEffectiveDropRates(FarmCycle.RoomEnemy.Enemy.Drops, model.Rules.GetUnneededDrops(fullResources)); // Use the new effective drop rate to calculate the new resourceVariationPerCycle for resources we still care about resourceVariationPerCycle = notFullFarmableResources .ToDictionary(resource => resource, resource => CalculateResourceVariationPerCycle(model, resource, effectiveDropRates, costingResources)); } // All resources are now stable. We already checked beforehand that all costing resources eventually become farmable, // so we can just apply a refill for all farmable resources and return a success. return(ExecuteRefill(model, inGameState, farmableResources)); }