using System; using System.Collections.Generic; using System.Linq; #nullable enable namespace GameCult.Aetheria.State.Verse { public static class AetheriaRuntimeEquipmentRotation { public static int QuarterTurns(string? value) { if (string.Equals(value, "CounterClockwise", StringComparison.OrdinalIgnoreCase)) return 1; if (string.Equals(value, "Half", StringComparison.OrdinalIgnoreCase) || string.Equals(value, "Reversed", StringComparison.OrdinalIgnoreCase)) return 2; if (string.Equals(value, "Clockwise", StringComparison.OrdinalIgnoreCase)) return 3; return 0; } public static (double X, double Y) RotateQuarter(double x, double y, int quarterTurns) => quarterTurns switch { 1 => (-y, x), 2 => (-x, -y), 3 => (y, -x), _ => (x, y) }; } public sealed class AetheriaRuntimeEquippedBehavior { private IReadOnlyList? modifierBehaviors; public AetheriaRuntimeEquippedBehavior( AetheriaRuntimeEntitySnapshotCommit entity, AetheriaRuntimeCatalogSnapshot catalog, int equipmentIndex, int behaviorIndex, AetheriaRuntimeLoadoutItemSlotCommit slot, AetheriaRuntimeLoadoutItemCommit item, AetheriaRuntimeCatalogItem catalogItem, AetheriaRuntimeBehaviorPayload payload, AetheriaRuntimeBehaviorStateCommit state) { Entity = entity; Catalog = catalog; EquipmentIndex = equipmentIndex; BehaviorIndex = behaviorIndex; Slot = slot; Item = item; CatalogItem = catalogItem; Payload = payload; State = state; } public AetheriaRuntimeEntitySnapshotCommit Entity { get; } public AetheriaRuntimeCatalogSnapshot Catalog { get; } public int EquipmentIndex { get; } public int BehaviorIndex { get; } public AetheriaRuntimeLoadoutItemSlotCommit Slot { get; } public AetheriaRuntimeLoadoutItemCommit Item { get; } public AetheriaRuntimeCatalogItem CatalogItem { get; } public AetheriaRuntimeBehaviorPayload Payload { get; } public AetheriaRuntimeBehaviorStateCommit State { get; } internal void BindModifierBehaviors(IReadOnlyList value) => modifierBehaviors = value ?? Array.Empty(); public double EvaluateStat(int fieldKey, double thermalPerformance = 1.0) { var field = (Payload.Fields ?? Array.Empty()) .FirstOrDefault(candidate => candidate != null && candidate.Key == fieldKey); var baseline = AetheriaRuntimeDaemonItemStatQueries.EvaluatePerformanceStat( field?.Value, Item, Math.Max(0, Math.Min(1, thermalPerformance))); return AetheriaRuntimeBehaviorSimulation.Apply( Entity, Catalog, EquipmentIndex, Payload, fieldKey, baseline, modifierBehaviors); } } public static class AetheriaRuntimeEquippedBehaviorQueries { public static IReadOnlyList Find( AetheriaRuntimeEntitySnapshotCommit? entity, AetheriaRuntimeCatalogSnapshot? catalog, string behaviorKind) { if (string.IsNullOrWhiteSpace(behaviorKind)) return Array.Empty(); return FindAll(entity, catalog) .Where(value => AetheriaRuntimeBehaviorMetadataCatalog.IsKindOrDescendant( value.Payload.Kind, behaviorKind)) .ToArray(); } public static IReadOnlyList FindAll( AetheriaRuntimeEntitySnapshotCommit? entity, AetheriaRuntimeCatalogSnapshot? catalog) { if (entity == null || catalog == null) return Array.Empty(); AetheriaRuntimeBehaviorStateProjector.EnsureEquipmentBehaviorStates(entity, catalog); var states = (entity.BehaviorStates ?? Array.Empty()) .Where(state => state != null && string.Equals(state.OwnerKind, AetheriaRuntimeBehaviorStateProjector.EquipmentOwnerKind, StringComparison.Ordinal)) .ToDictionary(state => (state.OwnerIndex, state.BehaviorIndex)); var found = new List(); var equipment = entity.Equipment ?? Array.Empty(); for (var equipmentIndex = 0; equipmentIndex < equipment.Count; equipmentIndex++) { var item = equipment[equipmentIndex]?.Item; var catalogItem = catalog.FindItem(item?.ItemKey ?? ""); if (item == null || catalogItem == null) continue; var payloads = catalogItem.BehaviorPayloads ?? Array.Empty(); for (var behaviorIndex = 0; behaviorIndex < payloads.Count; behaviorIndex++) { var payload = payloads[behaviorIndex]; if (payload == null || !states.TryGetValue((equipmentIndex, behaviorIndex), out var state)) { continue; } found.Add(new AetheriaRuntimeEquippedBehavior( entity, catalog, equipmentIndex, behaviorIndex, equipment[equipmentIndex], item, catalogItem, payload, state)); } } var equipmentStates = (entity.EquipmentStates ?? Array.Empty()) .Where(state => state != null) .ToDictionary(state => state.EquipmentIndex); var modifierBehaviors = found .Where(value => AetheriaRuntimeBehaviorMetadataCatalog.IsKindOrDescendant( value.Payload.Kind, "StatModifier")) .Where(value => value.Item.Enabled && value.Item.Durability > 0.01 && (!equipmentStates.TryGetValue(value.EquipmentIndex, out var state) || state.Online)) .ToArray(); foreach (var behavior in found) behavior.BindModifierBehaviors(modifierBehaviors); return found; } public static IReadOnlyList FindOperational( AetheriaRuntimeEntitySnapshotCommit? entity, AetheriaRuntimeCatalogSnapshot? catalog, string behaviorKind) { if (entity == null) return Array.Empty(); var equipmentStates = (entity.EquipmentStates ?? Array.Empty()) .Where(state => state != null) .ToDictionary(state => state.EquipmentIndex); return Find(entity, catalog, behaviorKind) .Where(value => value.Item.Enabled && value.Item.Durability > 0.01 && (!equipmentStates.TryGetValue(value.EquipmentIndex, out var state) || state.Online)) .ToArray(); } public static IReadOnlyList FindAllOperational( AetheriaRuntimeEntitySnapshotCommit? entity, AetheriaRuntimeCatalogSnapshot? catalog) { if (entity == null) return Array.Empty(); var equipmentStates = (entity.EquipmentStates ?? Array.Empty()) .Where(state => state != null) .ToDictionary(state => state.EquipmentIndex); return FindAll(entity, catalog) .Where(value => value.Item.Enabled && value.Item.Durability > 0.01 && (!equipmentStates.TryGetValue(value.EquipmentIndex, out var state) || state.Online)) .ToArray(); } public static IReadOnlyList FindExecuting( AetheriaRuntimeEntitySnapshotCommit? entity, AetheriaRuntimeCatalogSnapshot? catalog, string behaviorKind) => FindOperational(entity, catalog, behaviorKind) .Where(value => value.State.ChainReached) .ToArray(); } }