to rename
This commit is contained in:
@@ -1,25 +1,26 @@
|
||||
using System.Threading.Channels;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SpaceGame.Api.Universe.Bootstrap;
|
||||
|
||||
namespace SpaceGame.Api.Universe.Simulation;
|
||||
|
||||
public sealed class WorldService(
|
||||
IWebHostEnvironment environment,
|
||||
IOptions<WorldGenerationOptions> worldGenerationOptions,
|
||||
WorldBootstrapper worldBootstrapper,
|
||||
IOptions<BalanceOptions> balance,
|
||||
IOptions<OrbitalSimulationOptions> orbitalSimulationOptions)
|
||||
{
|
||||
private const int DeltaHistoryLimit = 256;
|
||||
|
||||
private readonly Lock _sync = new();
|
||||
private readonly OrbitalSimulationSnapshot _orbitalSimulation = new(orbitalSimulationOptions.Value.SimulatedSecondsPerRealSecond);
|
||||
private readonly ScenarioLoader _loader = new(environment.ContentRootPath, worldGenerationOptions.Value);
|
||||
private readonly WorldBootstrapper _bootstrapper = worldBootstrapper;
|
||||
private readonly SimulationEngine _engine = new(orbitalSimulationOptions.Value);
|
||||
private readonly PlayerFactionService _playerFaction = new();
|
||||
private readonly Dictionary<Guid, SubscriptionState> _subscribers = [];
|
||||
private readonly Queue<WorldDelta> _history = [];
|
||||
private SimulationWorld _world = new ScenarioLoader(environment.ContentRootPath, worldGenerationOptions.Value).Load();
|
||||
private SimulationWorld _world = worldBootstrapper.Bootstrap();
|
||||
private long _sequence;
|
||||
private BalanceDefinition? _balanceOverride;
|
||||
private BalanceOptions? _balanceOverride;
|
||||
|
||||
public WorldSnapshot GetSnapshot()
|
||||
{
|
||||
@@ -45,32 +46,31 @@ public sealed class WorldService(
|
||||
}
|
||||
}
|
||||
|
||||
public BalanceDefinition GetBalance()
|
||||
public BalanceOptions GetBalance()
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
var b = _world.Balance;
|
||||
return new BalanceDefinition
|
||||
return new BalanceOptions
|
||||
{
|
||||
SimulationSpeedMultiplier = b.SimulationSpeedMultiplier,
|
||||
YPlane = b.YPlane,
|
||||
ArrivalThreshold = b.ArrivalThreshold,
|
||||
MiningRate = b.MiningRate,
|
||||
MiningCycleSeconds = b.MiningCycleSeconds,
|
||||
TransferRate = b.TransferRate,
|
||||
DockingDuration = b.DockingDuration,
|
||||
UndockingDuration = b.UndockingDuration,
|
||||
UndockDistance = b.UndockDistance,
|
||||
SimulationSpeedMultiplier = balance.Value.SimulationSpeedMultiplier,
|
||||
YPlane = balance.Value.YPlane,
|
||||
ArrivalThreshold = balance.Value.ArrivalThreshold,
|
||||
MiningRate = balance.Value.MiningRate,
|
||||
MiningCycleSeconds = balance.Value.MiningCycleSeconds,
|
||||
TransferRate = balance.Value.TransferRate,
|
||||
DockingDuration = balance.Value.DockingDuration,
|
||||
UndockingDuration = balance.Value.UndockingDuration,
|
||||
UndockDistance = balance.Value.UndockDistance,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public BalanceDefinition UpdateBalance(BalanceDefinition balance)
|
||||
public BalanceOptions UpdateBalance(BalanceOptions balance)
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
_balanceOverride = SanitizeBalance(balance);
|
||||
ApplyBalance(_world, _balanceOverride);
|
||||
ApplyBalance(_balanceOverride);
|
||||
return GetBalance();
|
||||
}
|
||||
}
|
||||
@@ -285,10 +285,10 @@ public sealed class WorldService(
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
_world = _loader.Load();
|
||||
_world = _bootstrapper.Bootstrap();
|
||||
if (_balanceOverride is not null)
|
||||
{
|
||||
ApplyBalance(_world, _balanceOverride);
|
||||
ApplyBalance(_balanceOverride);
|
||||
}
|
||||
_sequence += 1;
|
||||
_history.Clear();
|
||||
@@ -323,26 +323,25 @@ public sealed class WorldService(
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyBalance(SimulationWorld world, BalanceDefinition balance) =>
|
||||
world.Balance = new BalanceDefinition
|
||||
{
|
||||
SimulationSpeedMultiplier = balance.SimulationSpeedMultiplier,
|
||||
YPlane = balance.YPlane,
|
||||
ArrivalThreshold = balance.ArrivalThreshold,
|
||||
MiningRate = balance.MiningRate,
|
||||
MiningCycleSeconds = balance.MiningCycleSeconds,
|
||||
TransferRate = balance.TransferRate,
|
||||
DockingDuration = balance.DockingDuration,
|
||||
UndockingDuration = balance.UndockingDuration,
|
||||
UndockDistance = balance.UndockDistance,
|
||||
};
|
||||
private void ApplyBalance(BalanceOptions value)
|
||||
{
|
||||
balance.Value.SimulationSpeedMultiplier = value.SimulationSpeedMultiplier;
|
||||
balance.Value.YPlane = value.YPlane;
|
||||
balance.Value.ArrivalThreshold = value.ArrivalThreshold;
|
||||
balance.Value.MiningRate = value.MiningRate;
|
||||
balance.Value.MiningCycleSeconds = value.MiningCycleSeconds;
|
||||
balance.Value.TransferRate = value.TransferRate;
|
||||
balance.Value.DockingDuration = value.DockingDuration;
|
||||
balance.Value.UndockingDuration = value.UndockingDuration;
|
||||
balance.Value.UndockDistance = value.UndockDistance;
|
||||
}
|
||||
|
||||
private static BalanceDefinition SanitizeBalance(BalanceDefinition candidate)
|
||||
private static BalanceOptions SanitizeBalance(BalanceOptions candidate)
|
||||
{
|
||||
static float finiteOr(float value, float fallback) =>
|
||||
float.IsFinite(value) ? value : fallback;
|
||||
|
||||
return new BalanceDefinition
|
||||
return new BalanceOptions
|
||||
{
|
||||
SimulationSpeedMultiplier = MathF.Max(0.01f, finiteOr(candidate.SimulationSpeedMultiplier, 1f)),
|
||||
YPlane = MathF.Max(0f, finiteOr(candidate.YPlane, 0f)),
|
||||
|
||||
Reference in New Issue
Block a user