Complete daemon-owned Terminus service loop
This commit is contained in:
parent
1fbf238adb
commit
6c2bcd6afb
18 changed files with 559 additions and 53 deletions
|
|
@ -36,9 +36,31 @@ namespace GameCult.Aetheria.State.Verse
|
|||
Math.Max(0, Capacity(entity, catalog) - Occupied(entity, catalog));
|
||||
|
||||
public static int UnitsThatFit(AetheriaRuntimeEntitySnapshotCommit? entity, AetheriaRuntimeCatalogSnapshot? catalog, string itemKey)
|
||||
{
|
||||
return UnitsThatFit(entity, catalog, itemKey, 0);
|
||||
}
|
||||
|
||||
public static int UnitsThatFit(
|
||||
AetheriaRuntimeEntitySnapshotCommit? entity,
|
||||
AetheriaRuntimeCatalogSnapshot? catalog,
|
||||
string itemKey,
|
||||
int cargoIndex)
|
||||
{
|
||||
var volume = catalog?.FindItem(itemKey ?? "")?.Volume ?? 0;
|
||||
return volume <= 0 ? int.MaxValue : Math.Max(0, (int)Math.Floor(Available(entity, catalog) / volume));
|
||||
if (volume <= 0)
|
||||
return int.MaxValue;
|
||||
var cargoBays = (entity?.Equipment ?? Array.Empty<AetheriaRuntimeLoadoutItemSlotCommit>())
|
||||
.Select(slot => catalog?.FindItem(slot?.Item?.ItemKey ?? ""))
|
||||
.Where(item => item != null && item.InteriorOccupiedCells > 0)
|
||||
.ToArray();
|
||||
if (cargoIndex < 0 || cargoIndex >= cargoBays.Length)
|
||||
return 0;
|
||||
var capacity = cargoBays[cargoIndex]!.InteriorOccupiedCells;
|
||||
var bay = (entity?.CargoContents ?? Array.Empty<AetheriaRuntimeCargoBayLoadoutCommit>())
|
||||
.ElementAtOrDefault(cargoIndex);
|
||||
var occupied = (bay?.Items ?? Array.Empty<AetheriaRuntimeLoadoutItemSlotCommit>())
|
||||
.Sum(slot => Volume(slot?.Item, catalog!));
|
||||
return Math.Max(0, (int)Math.Floor((capacity - occupied) / volume));
|
||||
}
|
||||
|
||||
private static double Volume(AetheriaRuntimeLoadoutItemCommit? item, AetheriaRuntimeCatalogSnapshot catalog) =>
|
||||
|
|
|
|||
|
|
@ -195,7 +195,8 @@ namespace GameCult.Aetheria.State.Verse
|
|||
ToggleShieldEnabled,
|
||||
IssueAgentTask,
|
||||
CancelAgentTask,
|
||||
SetSimulationRate
|
||||
SetSimulationRate,
|
||||
AdvanceSimulationStep
|
||||
}
|
||||
|
||||
[CultDocument("gamecult.aetheria.daemon_provider_advertisement", "gamecult.aetheria.daemon_provider_advertisement.v1")]
|
||||
|
|
|
|||
|
|
@ -165,6 +165,12 @@ namespace GameCult.Aetheria.State.Verse
|
|||
return Send(command);
|
||||
}
|
||||
|
||||
internal AetheriaRuntimeDaemonCommandEnvelope AdvanceSimulationStep(
|
||||
AetheriaRuntimeDaemonFrameDocument? frame)
|
||||
{
|
||||
return Send(Create(AetheriaRuntimeDaemonCommandKinds.AdvanceSimulationStep, frame));
|
||||
}
|
||||
|
||||
internal AetheriaRuntimeDaemonCommandEnvelope SetLookDirection(
|
||||
AetheriaRuntimeDaemonFrameDocument? frame,
|
||||
double directionX,
|
||||
|
|
|
|||
|
|
@ -143,7 +143,7 @@ namespace GameCult.Aetheria.State.Verse
|
|||
case AetheriaRuntimeDaemonCommandKinds.ToggleHullConductivity:
|
||||
return ApplyToggleHullConductivity(run, command);
|
||||
case AetheriaRuntimeDaemonCommandKinds.TradePurchase:
|
||||
return ApplyTradePurchase(run, command);
|
||||
return ApplyTradePurchase(run, command, context.Catalog);
|
||||
case AetheriaRuntimeDaemonCommandKinds.RestoreLoadout:
|
||||
return ApplyRestoreLoadout(run, command, context);
|
||||
case AetheriaRuntimeDaemonCommandKinds.SetMoveVector:
|
||||
|
|
@ -181,6 +181,8 @@ namespace GameCult.Aetheria.State.Verse
|
|||
return ApplyCancelAgentTask(run, command);
|
||||
case AetheriaRuntimeDaemonCommandKinds.SetSimulationRate:
|
||||
return IsSupportedSimulationRate(command.ScalarValue);
|
||||
case AetheriaRuntimeDaemonCommandKinds.AdvanceSimulationStep:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
|
@ -191,6 +193,27 @@ namespace GameCult.Aetheria.State.Verse
|
|||
public static bool IsSupportedSimulationRate(double value) =>
|
||||
SupportedSimulationRates.Any(rate => Math.Abs(value - rate) < 0.000001);
|
||||
|
||||
public static bool RequiresSimulationStep(AetheriaRuntimeDaemonCommandKinds kind)
|
||||
{
|
||||
switch (kind)
|
||||
{
|
||||
case AetheriaRuntimeDaemonCommandKinds.SetMoveVector:
|
||||
case AetheriaRuntimeDaemonCommandKinds.FireWeaponGroup:
|
||||
case AetheriaRuntimeDaemonCommandKinds.SetWeaponGroupActive:
|
||||
case AetheriaRuntimeDaemonCommandKinds.SetBehaviorActive:
|
||||
case AetheriaRuntimeDaemonCommandKinds.ActivateConsumable:
|
||||
case AetheriaRuntimeDaemonCommandKinds.SensorPing:
|
||||
case AetheriaRuntimeDaemonCommandKinds.Dock:
|
||||
case AetheriaRuntimeDaemonCommandKinds.DockNearest:
|
||||
case AetheriaRuntimeDaemonCommandKinds.Undock:
|
||||
case AetheriaRuntimeDaemonCommandKinds.Interact:
|
||||
case AetheriaRuntimeDaemonCommandKinds.EnterWormhole:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ApplyIssueAgentTask(
|
||||
AetheriaRuntimeRunCheckpointCommit run,
|
||||
AetheriaRuntimeDaemonCommandDocument command)
|
||||
|
|
@ -748,7 +771,7 @@ namespace GameCult.Aetheria.State.Verse
|
|||
var quantity = transfer.Quantity > 0
|
||||
? transfer.Quantity
|
||||
: (int)Math.Round(command.ScalarValue);
|
||||
if (quantity <= 0 || quantity > AetheriaRuntimeCargoCapacityQueries.UnitsThatFit(destinationEntity, catalog, command.TextValue))
|
||||
if (quantity <= 0 || quantity > AetheriaRuntimeCargoCapacityQueries.UnitsThatFit(destinationEntity, catalog, command.TextValue, destinationCargoIndex))
|
||||
return false;
|
||||
if (!TryRemoveCargoItemQuantity(
|
||||
originEntity,
|
||||
|
|
@ -917,18 +940,58 @@ namespace GameCult.Aetheria.State.Verse
|
|||
|
||||
private static bool ApplyTradePurchase(
|
||||
AetheriaRuntimeRunCheckpointCommit run,
|
||||
AetheriaRuntimeDaemonCommandDocument command)
|
||||
AetheriaRuntimeDaemonCommandDocument command,
|
||||
AetheriaRuntimeCatalogSnapshot? catalog)
|
||||
{
|
||||
var purchase = command.TradePurchase ?? new AetheriaRuntimeTradePurchaseCommand();
|
||||
var totalPrice = purchase.TotalPrice;
|
||||
if (totalPrice < 0 || run.Credits < totalPrice)
|
||||
var itemKey = purchase.ItemKey ?? "";
|
||||
var typedItem = catalog?.FindItem(itemKey);
|
||||
var quantity = Math.Max(1, purchase.Quantity);
|
||||
if (typedItem == null ||
|
||||
!TryResolveDockParent(run, run.CurrentEntityKey, out var dockParentKey, out var dockParent))
|
||||
return false;
|
||||
|
||||
if (purchase.CreatesDockedShip)
|
||||
if (!TryFindStationStock(
|
||||
dockParent,
|
||||
itemKey,
|
||||
purchase.StationCargoIndex,
|
||||
purchase.SourceX,
|
||||
purchase.SourceY,
|
||||
out var stationCargoIndex,
|
||||
out var stockSlot))
|
||||
{
|
||||
if (!ApplyCreateDockedShipPurchase(run, purchase, out var purchasedShipKey))
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
var stationEntity = dockParent;
|
||||
|
||||
var unitPrice = AetheriaRuntimeDaemonTradeItemQueries.TradeItemValue(
|
||||
typedItem,
|
||||
stockSlot.Item,
|
||||
catalog.TradeValueSettings).Price;
|
||||
var totalPrice = checked(unitPrice * quantity);
|
||||
if (unitPrice < 0 || run.Credits < totalPrice)
|
||||
return false;
|
||||
|
||||
var createsDockedShip = !string.IsNullOrWhiteSpace(typedItem.HullType);
|
||||
if (createsDockedShip)
|
||||
{
|
||||
if (quantity != 1 || !HasAvailableDockingBay(dockParent))
|
||||
return false;
|
||||
purchase.TargetEntityKey = dockParentKey;
|
||||
if (!TryRemoveCargoItemQuantity(
|
||||
stationEntity,
|
||||
stationCargoIndex,
|
||||
itemKey,
|
||||
purchase.SourceX,
|
||||
purchase.SourceY,
|
||||
1,
|
||||
out var purchasedHull))
|
||||
return false;
|
||||
if (!ApplyCreateDockedShipPurchase(run, purchase, out var purchasedShipKey))
|
||||
{
|
||||
AddCargoItem(stationEntity, stationCargoIndex, purchasedHull);
|
||||
return false;
|
||||
}
|
||||
run.CurrentEntityKey = purchasedShipKey;
|
||||
run.Credits -= totalPrice;
|
||||
return true;
|
||||
|
|
@ -936,24 +999,17 @@ namespace GameCult.Aetheria.State.Verse
|
|||
|
||||
if (!TryResolveCargoBay(
|
||||
run,
|
||||
purchase.StationEntityKey,
|
||||
purchase.StationCargoIndex,
|
||||
out var stationEntity,
|
||||
out var stationCargoIndex,
|
||||
out _) ||
|
||||
!TryResolveCargoBay(
|
||||
run,
|
||||
purchase.TargetEntityKey,
|
||||
purchase.TargetCargoIndex,
|
||||
run.CurrentEntityKey,
|
||||
Math.Max(0, purchase.TargetCargoIndex),
|
||||
out var targetEntity,
|
||||
out var targetCargoIndex,
|
||||
out _))
|
||||
out _) ||
|
||||
!IsEntityDockedAt(run, run.CurrentEntityKey, dockParent) ||
|
||||
quantity > AetheriaRuntimeCargoCapacityQueries.UnitsThatFit(targetEntity, catalog, itemKey, targetCargoIndex))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var itemKey = purchase.ItemKey ?? "";
|
||||
var quantity = Math.Max(1, purchase.Quantity);
|
||||
if (!TryRemoveCargoItemQuantity(
|
||||
stationEntity,
|
||||
stationCargoIndex,
|
||||
|
|
@ -973,6 +1029,79 @@ namespace GameCult.Aetheria.State.Verse
|
|||
return true;
|
||||
}
|
||||
|
||||
private static bool TryResolveDockParent(
|
||||
AetheriaRuntimeRunCheckpointCommit run,
|
||||
string childEntityKey,
|
||||
out string parentEntityKey,
|
||||
out AetheriaRuntimeEntitySnapshotCommit parent)
|
||||
{
|
||||
parentEntityKey = "";
|
||||
parent = null!;
|
||||
if (!TryResolveEntity(run, childEntityKey, out var zoneIndex, out var childIndex, out _))
|
||||
return false;
|
||||
var zone = (run.Zones ?? Array.Empty<AetheriaRuntimeZoneSnapshotCommit>())
|
||||
.FirstOrDefault(candidate => candidate != null && candidate.ZoneIndex == zoneIndex);
|
||||
parent = (zone?.Entities ?? Array.Empty<AetheriaRuntimeEntitySnapshotCommit>())
|
||||
.FirstOrDefault(entity => entity != null &&
|
||||
(entity.DockingBayAssignments ?? Array.Empty<int>()).Contains(childIndex))!;
|
||||
if (parent == null)
|
||||
return false;
|
||||
parentEntityKey = AetheriaRuntimeRunCheckpointCommit.EntityRecordKey(run.RunId, zoneIndex, parent.EntityIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsEntityDockedAt(
|
||||
AetheriaRuntimeRunCheckpointCommit run,
|
||||
string entityKey,
|
||||
AetheriaRuntimeEntitySnapshotCommit parent)
|
||||
{
|
||||
return TryResolveEntity(run, entityKey, out _, out var entityIndex, out _) &&
|
||||
(parent.DockingBayAssignments ?? Array.Empty<int>()).Contains(entityIndex);
|
||||
}
|
||||
|
||||
private static bool HasAvailableDockingBay(AetheriaRuntimeEntitySnapshotCommit parent) =>
|
||||
(parent.DockingBayAssignments ?? Array.Empty<int>()).Any(index => index < 0);
|
||||
|
||||
private static bool TryFindCargoItem(
|
||||
AetheriaRuntimeCargoBayLoadoutCommit cargo,
|
||||
string itemKey,
|
||||
int x,
|
||||
int y,
|
||||
out AetheriaRuntimeLoadoutItemSlotCommit slot)
|
||||
{
|
||||
slot = (cargo?.Items ?? Array.Empty<AetheriaRuntimeLoadoutItemSlotCommit>())
|
||||
.FirstOrDefault(candidate => candidate?.Item != null &&
|
||||
string.Equals(candidate.Item.ItemKey ?? "", itemKey, StringComparison.Ordinal) &&
|
||||
candidate.X == x && candidate.Y == y)!;
|
||||
return slot != null;
|
||||
}
|
||||
|
||||
private static bool TryFindStationStock(
|
||||
AetheriaRuntimeEntitySnapshotCommit station,
|
||||
string itemKey,
|
||||
int requestedCargoIndex,
|
||||
int requestedX,
|
||||
int requestedY,
|
||||
out int cargoIndex,
|
||||
out AetheriaRuntimeLoadoutItemSlotCommit slot)
|
||||
{
|
||||
cargoIndex = -1;
|
||||
slot = null!;
|
||||
var cargo = station.CargoContents ?? Array.Empty<AetheriaRuntimeCargoBayLoadoutCommit>();
|
||||
IEnumerable<int> indices = requestedCargoIndex >= 0 && requestedCargoIndex < cargo.Count
|
||||
? new[] { requestedCargoIndex }
|
||||
: Enumerable.Range(0, cargo.Count);
|
||||
foreach (var index in indices)
|
||||
{
|
||||
if (TryFindCargoItem(cargo[index], itemKey, requestedX, requestedY, out slot))
|
||||
{
|
||||
cargoIndex = index;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool ApplyCreateDockedShipPurchase(
|
||||
AetheriaRuntimeRunCheckpointCommit run,
|
||||
AetheriaRuntimeTradePurchaseCommand purchase,
|
||||
|
|
|
|||
|
|
@ -482,6 +482,7 @@ public sealed class AetheriaRuntimeDaemonOperationsClient
|
|||
AetheriaRuntimeDaemonCommandKinds.SetSimulationRate => client.SetSimulationRate(
|
||||
frame,
|
||||
ReadPayloadDouble(request, "scalarValue", 0.0)),
|
||||
AetheriaRuntimeDaemonCommandKinds.AdvanceSimulationStep => client.AdvanceSimulationStep(frame),
|
||||
AetheriaRuntimeDaemonCommandKinds.SetLookDirection => client.SetLookDirection(
|
||||
frame,
|
||||
ReadPayloadDouble(request, "directionX", 0.0),
|
||||
|
|
@ -528,6 +529,20 @@ public sealed class AetheriaRuntimeDaemonOperationsClient
|
|||
AetheriaRuntimeDaemonCommandKinds.CancelAgentTask => client.CancelAgentTask(
|
||||
frame,
|
||||
ReadPayloadString(request, "taskId", "")),
|
||||
AetheriaRuntimeDaemonCommandKinds.TradePurchase => client.TradePurchase(
|
||||
frame,
|
||||
"",
|
||||
ReadPayloadString(request, "itemKey", ""),
|
||||
(int)ReadPayloadDouble(request, "quantity", 1),
|
||||
0,
|
||||
0,
|
||||
ReadPayloadString(request, "stationEntityKey", ""),
|
||||
(int)ReadPayloadDouble(request, "stationCargoIndex", -1),
|
||||
ReadPayloadString(request, "targetEntityKey", ""),
|
||||
(int)ReadPayloadDouble(request, "targetCargoIndex", -1),
|
||||
(int)ReadPayloadDouble(request, "sourceX", int.MinValue),
|
||||
(int)ReadPayloadDouble(request, "sourceY", int.MinValue),
|
||||
false),
|
||||
_ => AetheriaRuntimeDaemonSurfaceCommandCatalog.TrySubmitArgumentless(
|
||||
client,
|
||||
frame,
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ namespace GameCult.Aetheria.State.Verse
|
|||
frameId, simulationTimeSeconds);
|
||||
AetheriaRuntimeMiningSimulation.Step(run, zone, entities, intents, catalog, frameId, simulationTimeSeconds, deltaSeconds);
|
||||
AetheriaRuntimeSurveySimulation.Step(run, zone, entities, intents, catalog, frameId, simulationTimeSeconds, deltaSeconds);
|
||||
RefreshContacts(entities, settings);
|
||||
RefreshContacts(entities, settings, catalog);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1724,10 +1724,12 @@ namespace GameCult.Aetheria.State.Verse
|
|||
|
||||
private static void RefreshContacts(
|
||||
IReadOnlyList<AetheriaRuntimeEntitySnapshotCommit> entities,
|
||||
AetheriaRuntimeDaemonSimulationSettings settings)
|
||||
AetheriaRuntimeDaemonSimulationSettings settings,
|
||||
AetheriaRuntimeCatalogSnapshot? catalog)
|
||||
{
|
||||
foreach (var observer in entities)
|
||||
{
|
||||
var sensorRange = ResolveSensorRange(observer, settings, catalog);
|
||||
var contacts = new List<AetheriaRuntimeEntityContactCommit>();
|
||||
foreach (var target in entities)
|
||||
{
|
||||
|
|
@ -1735,7 +1737,7 @@ namespace GameCult.Aetheria.State.Verse
|
|||
continue;
|
||||
|
||||
var distance = Math.Sqrt(DistanceSq(observer, target));
|
||||
var visible = distance <= ResolveSensorRange(observer, settings);
|
||||
var visible = distance <= sensorRange;
|
||||
if (!visible && !Hostile(observer, target))
|
||||
continue;
|
||||
|
||||
|
|
@ -1749,8 +1751,6 @@ namespace GameCult.Aetheria.State.Verse
|
|||
}
|
||||
|
||||
observer.Contacts = contacts;
|
||||
observer.Visibility = observer.IsActive ? ResolveSensorRange(observer, settings) : 0;
|
||||
observer.VisibilitySourceCount = observer.IsActive ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2066,10 +2066,27 @@ namespace GameCult.Aetheria.State.Verse
|
|||
|
||||
private static double ResolveSensorRange(
|
||||
AetheriaRuntimeEntitySnapshotCommit entity,
|
||||
AetheriaRuntimeDaemonSimulationSettings settings)
|
||||
AetheriaRuntimeDaemonSimulationSettings settings,
|
||||
AetheriaRuntimeCatalogSnapshot? catalog)
|
||||
{
|
||||
return string.Equals(entity.Kind, "station", StringComparison.OrdinalIgnoreCase)
|
||||
? settings.StationSensorRange
|
||||
if (!entity.IsActive)
|
||||
return 0;
|
||||
|
||||
var equipmentStates = (entity.EquipmentStates ?? Array.Empty<AetheriaRuntimeEquipmentStateCommit>())
|
||||
.Where(state => state != null)
|
||||
.ToDictionary(state => state.EquipmentIndex);
|
||||
var sensitivitySquared = AetheriaRuntimeEquippedBehaviorQueries.Find(entity, catalog, "Sensor")
|
||||
.Where(sensor => sensor.Item.Enabled && sensor.Item.Durability > 0 &&
|
||||
(!equipmentStates.TryGetValue(sensor.EquipmentIndex, out var state) || state.Online))
|
||||
.Select(sensor => Math.Max(0, sensor.EvaluateStat(
|
||||
3,
|
||||
equipmentStates.TryGetValue(sensor.EquipmentIndex, out var state)
|
||||
? state.ThermalPerformance
|
||||
: 1)))
|
||||
.Select(sensitivity => sensitivity * sensitivity)
|
||||
.Sum();
|
||||
return sensitivitySquared > 0
|
||||
? settings.EntitySensorRange * Math.Sqrt(sensitivitySquared)
|
||||
: settings.EntitySensorRange;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -141,7 +141,9 @@ namespace GameCult.Aetheria.State.Verse
|
|||
public double ProjectileSpawnOffset { get; }
|
||||
public double ProjectileHeatScale { get; }
|
||||
public double HeatDissipationPerSecond { get; }
|
||||
[System.Obsolete("Sensor reach derives from installed equipment. Retained only for snapshot compatibility.")]
|
||||
public double StationSensorRange { get; }
|
||||
public double FallbackSensorRange => EntitySensorRange;
|
||||
public double EntitySensorRange { get; }
|
||||
public double PlayerStationHull { get; }
|
||||
public double HostileStationHull { get; }
|
||||
|
|
|
|||
|
|
@ -59,7 +59,8 @@ namespace GameCult.Aetheria.State.Verse
|
|||
|
||||
public static AetheriaRuntimeInputCapabilityDocument FromFrame(
|
||||
AetheriaRuntimeDaemonFrameDocument frame,
|
||||
bool includeSimulationClock = false)
|
||||
bool includeSimulationClock = false,
|
||||
AetheriaRuntimeCatalogSnapshot? catalog = null)
|
||||
{
|
||||
var run = frame?.Run ?? new AetheriaRuntimeRunCheckpointCommit();
|
||||
var entity = run.Zones.SelectMany(zone => zone.Entities).FirstOrDefault(candidate =>
|
||||
|
|
@ -68,6 +69,7 @@ namespace GameCult.Aetheria.State.Verse
|
|||
if (includeSimulationClock)
|
||||
{
|
||||
actions.Add(Action("simulation.pause", "Pause", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "0")));
|
||||
actions.Add(Action("simulation.step", "Advance One Step", "AdvanceSimulationStep", "simulation", "terminus-clock"));
|
||||
actions.Add(Action("simulation.rate.quarter", "Quarter Speed", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "0.25")));
|
||||
actions.Add(Action("simulation.rate.half", "Half Speed", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "0.5")));
|
||||
actions.Add(Action("simulation.rate.realtime", "Real Time", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "1")));
|
||||
|
|
@ -84,10 +86,72 @@ namespace GameCult.Aetheria.State.Verse
|
|||
actions.AddRange((entity.WeaponGroups ?? Array.Empty<IReadOnlyList<int>>()).Select((_, index) => Action($"weapon-group.{index}.fire", $"Fire Weapon Group {index + 1}", "FireWeaponGroup", "weapon-group", $"{run.CurrentEntityKey}#weapon-group/{index}", ("weaponGroup", index.ToString()))));
|
||||
actions.AddRange((entity.Equipment ?? Array.Empty<AetheriaRuntimeLoadoutItemSlotCommit>()).Where(slot => slot?.Item != null).Select((slot, index) => Action($"equipment.{index}.activate", $"Activate {slot.Item.ItemKey}", "SetBehaviorActive", "equipment", $"{run.CurrentEntityKey}#equipment/{index}", ("equipmentIndex", index.ToString()), ("behaviorIndex", "0"), ("active", "true"))));
|
||||
actions.AddRange((entity.CargoContents ?? Array.Empty<AetheriaRuntimeCargoBayLoadoutCommit>()).SelectMany(bay => bay.Items).Where(slot => slot?.Item != null).Select((slot, index) => Action($"cargo.{slot.Item.ItemKey}.{index}.use", $"Use {slot.Item.ItemKey}", "ActivateConsumable", "consumable", $"{run.CurrentEntityKey}#cargo/{index}", ("itemKey", slot.Item.ItemKey))));
|
||||
actions.AddRange(TradeActions(run, entity, catalog));
|
||||
}
|
||||
return new AetheriaRuntimeInputCapabilityDocument { Version = frame?.FrameId ?? 0, Actions = actions.ToArray(), DefaultProfiles = BuildDefaultProfiles(actions) };
|
||||
}
|
||||
|
||||
private static IEnumerable<AetheriaRuntimeInputActionDocument> TradeActions(
|
||||
AetheriaRuntimeRunCheckpointCommit run,
|
||||
AetheriaRuntimeEntitySnapshotCommit entity,
|
||||
AetheriaRuntimeCatalogSnapshot? catalog)
|
||||
{
|
||||
if (catalog == null)
|
||||
yield break;
|
||||
var zone = (run.Zones ?? Array.Empty<AetheriaRuntimeZoneSnapshotCommit>())
|
||||
.FirstOrDefault(candidate => candidate != null && candidate.ZoneIndex == run.CurrentZoneIndex);
|
||||
var parent = (zone?.Entities ?? Array.Empty<AetheriaRuntimeEntitySnapshotCommit>())
|
||||
.FirstOrDefault(candidate => candidate != null &&
|
||||
(candidate.DockingBayAssignments ?? Array.Empty<int>()).Contains(entity.EntityIndex));
|
||||
if (parent == null)
|
||||
yield break;
|
||||
|
||||
var stationKey = AetheriaRuntimeRunCheckpointCommit.EntityRecordKey(
|
||||
run.RunId,
|
||||
run.CurrentZoneIndex,
|
||||
parent.EntityIndex);
|
||||
var cargo = parent.CargoContents ?? Array.Empty<AetheriaRuntimeCargoBayLoadoutCommit>();
|
||||
for (var bayIndex = 0; bayIndex < cargo.Count; bayIndex++)
|
||||
{
|
||||
foreach (var slot in cargo[bayIndex]?.Items ?? Array.Empty<AetheriaRuntimeLoadoutItemSlotCommit>())
|
||||
{
|
||||
var itemKey = slot?.Item?.ItemKey ?? "";
|
||||
var typedItem = catalog.FindItem(itemKey);
|
||||
if (typedItem == null || slot?.Item == null || slot.Item.Quantity <= 0)
|
||||
continue;
|
||||
var value = AetheriaRuntimeDaemonTradeItemQueries.TradeItemValue(
|
||||
typedItem,
|
||||
slot.Item,
|
||||
catalog.TradeValueSettings).Price;
|
||||
var createsShip = !string.IsNullOrWhiteSpace(typedItem.HullType);
|
||||
var canReceive = createsShip
|
||||
? (parent.DockingBayAssignments ?? Array.Empty<int>()).Any(index => index < 0)
|
||||
: AetheriaRuntimeCargoCapacityQueries.UnitsThatFit(entity, catalog, itemKey, 0) > 0;
|
||||
var action = Action(
|
||||
$"trade.buy.{StableToken(itemKey)}.{bayIndex}.{slot.X}.{slot.Y}",
|
||||
$"Buy {typedItem.Name} ({value})",
|
||||
"TradePurchase",
|
||||
"trade",
|
||||
$"{stationKey}#cargo/{bayIndex}/{slot.X}/{slot.Y}",
|
||||
("itemKey", itemKey),
|
||||
("quantity", "1"),
|
||||
("stationEntityKey", stationKey),
|
||||
("stationCargoIndex", bayIndex.ToString()),
|
||||
("targetEntityKey", run.CurrentEntityKey),
|
||||
("targetCargoIndex", "0"),
|
||||
("sourceX", slot.X.ToString()),
|
||||
("sourceY", slot.Y.ToString()));
|
||||
action.Availability = value >= 0 && run.Credits >= value && canReceive
|
||||
? "available"
|
||||
: "unavailable";
|
||||
yield return action;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string StableToken(string value) =>
|
||||
new string((value ?? "").Select(character => char.IsLetterOrDigit(character) ? char.ToLowerInvariant(character) : '-').ToArray()).Trim('-');
|
||||
|
||||
private static IEnumerable<AetheriaRuntimeInputActionDocument> CoreActions()
|
||||
{
|
||||
yield return Action("pilot.scoop", "Scoop", "SetTractorPower", "ship", "pilot", ("scalarValue", "1"));
|
||||
|
|
|
|||
Loading…
Reference in a new issue