79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using Craftimizer.Simulator;
|
|
using Craftimizer.Simulator.Actions;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace Craftimizer.Solver.Crafty;
|
|
|
|
[StructLayout(LayoutKind.Auto)]
|
|
public struct SimulationNode
|
|
{
|
|
public readonly SimulationState State;
|
|
public readonly ActionType? Action;
|
|
public readonly CompletionState SimulationCompletionState;
|
|
|
|
public ActionSet AvailableActions;
|
|
|
|
public readonly CompletionState CompletionState => GetCompletionState(SimulationCompletionState, AvailableActions);
|
|
|
|
public readonly bool IsComplete => CompletionState != CompletionState.Incomplete;
|
|
|
|
public SimulationNode(SimulationState state, ActionType? action, CompletionState completionState, ActionSet actions)
|
|
{
|
|
State = state;
|
|
Action = action;
|
|
SimulationCompletionState = completionState;
|
|
AvailableActions = actions;
|
|
}
|
|
|
|
public static CompletionState GetCompletionState(CompletionState simCompletionState, ActionSet actions) =>
|
|
actions.IsEmpty && simCompletionState == CompletionState.Incomplete ?
|
|
CompletionState.NoMoreActions :
|
|
simCompletionState;
|
|
|
|
public readonly float? CalculateScore(int maxStepCount) => CalculateScoreForState(State, SimulationCompletionState, maxStepCount);
|
|
|
|
public static float? CalculateScoreForState(SimulationState state, CompletionState completionState, int maxStepCount)
|
|
{
|
|
if (completionState != CompletionState.ProgressComplete)
|
|
return null;
|
|
|
|
static float Apply(float bonus, float value, float target) =>
|
|
bonus * Math.Min(1f, value / target);
|
|
|
|
var progressBonus = 0.20f;
|
|
var qualityBonus = 0.65f;
|
|
var durabilityBonus = 0.05f;
|
|
var cpBonus = 0.05f;
|
|
var fewerStepsBonus = 0.05f;
|
|
|
|
var progressScore = Apply(
|
|
progressBonus,
|
|
state.Progress,
|
|
state.Input.Recipe.MaxProgress
|
|
);
|
|
|
|
var qualityScore = Apply(
|
|
qualityBonus,
|
|
state.Quality,
|
|
state.Input.Recipe.MaxQuality
|
|
);
|
|
|
|
var durabilityScore = Apply(
|
|
durabilityBonus,
|
|
state.Durability,
|
|
state.Input.Recipe.MaxDurability
|
|
);
|
|
|
|
var cpScore = Apply(
|
|
cpBonus,
|
|
state.CP,
|
|
state.Input.Stats.CP
|
|
);
|
|
|
|
var fewerStepsScore =
|
|
fewerStepsBonus * (1f - ((float)(state.ActionCount + 1) / maxStepCount));
|
|
|
|
return progressScore + qualityScore + durabilityScore + cpScore + fewerStepsScore;
|
|
}
|
|
}
|