using SpaceGame.Api.Simulation.Engine; using SpaceGame.Api.Simulation.Model; namespace SpaceGame.Api.Simulation.AI; internal sealed class ShipBehaviorStateMachine { private readonly IReadOnlyDictionary states; private readonly IShipBehaviorState fallbackState; private ShipBehaviorStateMachine(IReadOnlyDictionary states, IShipBehaviorState fallbackState) { this.states = states; this.fallbackState = fallbackState; } public static ShipBehaviorStateMachine CreateDefault() { var idleState = new IdleShipBehaviorState(); var knownStates = new IShipBehaviorState[] { idleState, new PatrolShipBehaviorState(), new ResourceHarvestShipBehaviorState("auto-mine", "ore", "mining"), new ConstructStationShipBehaviorState(), }; return new ShipBehaviorStateMachine( knownStates.ToDictionary(state => state.Kind, StringComparer.Ordinal), idleState); } public void Plan(SimulationEngine engine, ShipRuntime ship, SimulationWorld world) => Resolve(ship.DefaultBehavior.Kind).Plan(engine, ship, world); public void ApplyEvent(SimulationEngine engine, ShipRuntime ship, SimulationWorld world, string controllerEvent) => Resolve(ship.DefaultBehavior.Kind).ApplyEvent(engine, ship, world, controllerEvent); private IShipBehaviorState Resolve(string kind) => states.TryGetValue(kind, out var state) ? state : fallbackState; }