diff --git a/Assets/Scripts/ServerShared/Agents/States/Haul.cs b/Assets/Scripts/ServerShared/Agents/States/Haul.cs index 30ff78b6d..692d76b6d 100644 --- a/Assets/Scripts/ServerShared/Agents/States/Haul.cs +++ b/Assets/Scripts/ServerShared/Agents/States/Haul.cs @@ -1,12 +1,18 @@ -// A hauler's pilot: it flies to the orbit of HaulRun.Leg and, within docking distance of a station where the call would move a unit, -// commands the call (HaulRun.Call docks, moves the goods and undocks it, which gives it a fresh pilot on the same task). No field -// but its child: the leg is derived from the hold on every update, so a hand-emptied hold changes it at once. +using GameCult.Caching; + +// A hauler's pilot: it flies to HaulRun.Waypoint, derived from the hold and the order on every update. With no hop it flies to the leg's +// orbit and, within docking distance of a station where the call would move a unit, commands the call (HaulRun.Call docks, moves the +// goods and undocks it, which gives it a fresh pilot on the same task). With a hop the leg lies in another zone: it flies to this zone's +// wormhole toward it and, within the wormhole's reach, commands the jump (HaulRun.Jump). No field but its children: a hand-emptied hold +// changes the waypoint at once. public class HaulState : BaseState { public HaulState(Agent agent) : base(agent) { var move = new MoveToOrbitState(agent); + var leave = new MoveToPointState(agent); HaulOrderData Order() => _agent.Task is HaulingTask task ? HaulRun.OrderOf(_agent.ItemManager, task.Order) : null; + (CultRecordKey Orbit, GalaxyZone Hop)? Way() => Order() is HaulOrderData order ? HaulRun.Waypoint(_agent.Ship, order) : null; OrbitalEntity Station() => HaulRun.Station(_agent.Ship.Zone, move.Orbit); bool Callable() { @@ -14,10 +20,14 @@ public class HaulState : BaseState var station = order == null ? null : Station(); return station != null && move.Distance < _agent.Settings.DockingDistance && HaulRun.CanCall(_agent.Ship, station, order); } + bool Leaving() => leave.Distance < _agent.Settings.WormholeExitRadius && !_agent.Ship.WormholeAnimationInProgress; - Transitions.Add(new StateTransition(move, () => Order() != null, () => move.Orbit = HaulRun.Leg(_agent.Ship, Order()))); - // A changed leg re-enters this state, which points the child at it; a callable station commands the call. - move.Transitions.Add(new StateTransition(this, () => Order() is HaulOrderData order && !HaulRun.Leg(_agent.Ship, order).Equals(move.Orbit), null)); + Transitions.Add(new StateTransition(move, () => Way() is { Hop: null }, () => move.Orbit = Way().Value.Orbit)); + Transitions.Add(new StateTransition(leave, () => Way() is { Hop: not null }, () => leave.Point = _agent.Ship.Zone.WormholeTo(Way().Value.Hop))); + // A changed waypoint re-enters this state, which points the child at it; a callable station commands the call, a reached wormhole the jump. + move.Transitions.Add(new StateTransition(this, () => !(Way() is { Hop: null } way && way.Orbit.Equals(move.Orbit)), null)); move.Transitions.Add(new StateTransition(this, Callable, () => _agent.Ship.Command.Call(Station()))); + leave.Transitions.Add(new StateTransition(this, () => !(Way() is { Hop: not null } way && _agent.Ship.Zone.WormholeTo(way.Hop).Equals(leave.Point)), null)); + leave.Transitions.Add(new StateTransition(this, Leaving, () => _agent.Ship.Command.Jump(Way().Value.Hop))); } } diff --git a/Assets/Scripts/ServerShared/Agents/States/MoveTo.cs b/Assets/Scripts/ServerShared/Agents/States/MoveTo.cs index aad04db0d..f40746716 100644 --- a/Assets/Scripts/ServerShared/Agents/States/MoveTo.cs +++ b/Assets/Scripts/ServerShared/Agents/States/MoveTo.cs @@ -36,3 +36,11 @@ public class MoveToOrbitState : MoveToState protected override float2 TargetPosition => _agent.Ship.Zone.GetOrbitPosition(Orbit); } + +public class MoveToPointState : MoveToState +{ + public float2 Point { get; set; } + public MoveToPointState(Agent agent) : base(agent) { } + + protected override float2 TargetPosition => Point; +} diff --git a/Assets/Scripts/ServerShared/HaulRun.cs b/Assets/Scripts/ServerShared/HaulRun.cs index 8db9e6466..e624df256 100644 --- a/Assets/Scripts/ServerShared/HaulRun.cs +++ b/Assets/Scripts/ServerShared/HaulRun.cs @@ -1,12 +1,18 @@ using System.Collections.Generic; using System.Linq; using GameCult.Caching; +using CultMath; +using static CultMath.math; +using float2 = CultMath.float2; +using float3 = CultMath.float3; -// The interim haul (aetheria-living-world:ruling:transit-depots-buy-for-onward-haul, demo-zenith-ore-depot): which ship carries for which -// standing order (Entity.Haul), where it flies (the leg, derived from its hold every update), and the one dock call that moves goods: -// the hauler docks (ruling docking-is-enclosed-bays), the From station's silo loads it or the To station takes its cargo through -// StationServices.TryStorePart, and it undocks piloted. No payment moves: both ends are in one comms cell and no station holds an account -// (ruling comms-cells-are-zones). The ship's task, orders and cargo are the ship's own; nothing here is stored. +// The haul (aetheria-living-world:ruling:transit-depots-buy-for-onward-haul, demo-zenith-ore-depot, demo-chain-spans-the-region): which ship carries for which +// standing order (Entity.Haul), where it flies (the waypoint, derived from its hold every update), and the one dock call that moves goods: +// the hauler docks (ruling docking-is-enclosed-bays), the From station's output bays load it or the To station takes its cargo through +// StationServices.TryStorePart, and it undocks piloted. Legs join stations of one faction (seeded by RegionLegs) or the staged demo depot and +// refinery in one comms cell, and no station holds an account, so no payment moves (ruling comms-cells-are-zones). A hauler is a member of +// exactly one zone: flown by the full sim and its pilot in the player's zone, and by Fly off screen, which jumps it between zones by wormhole. +// The ship's task, orders and cargo are the ship's own; nothing here is stored. public static class HaulRun { public static HaulOrderData OrderOf(ItemManager items, CultRecordKey order) => @@ -54,6 +60,94 @@ public static class HaulRun depot.TryUndock(ship, piloted: true); } + + // The GalaxyZone whose live contents hold the orbit; null when none does. + public static GalaxyZone ZoneOf(Galaxy galaxy, CultRecordKey orbit) => + galaxy?.Zones.FirstOrDefault(zone => zone.Contents != null && zone.Contents.Orbits.ContainsKey(orbit)); + + // Where a hauler is bound: the orbit of its leg and the hop, the next zone on the way, when that orbit is not in the ship's zone (null + // hop: it is). Null when no zone holds the orbit or no path reaches it. Derived on every call, stored nowhere. + public static (CultRecordKey Orbit, GalaxyZone Hop)? Waypoint(Ship ship, HaulOrderData order) + { + var orbit = Leg(ship, order); + var zone = ship.Zone; + if (zone.Galaxy == null || zone.GalaxyZone == null) return zone.Orbits.ContainsKey(orbit) ? (orbit, null) : ((CultRecordKey, GalaxyZone)?) null; + var target = ZoneOf(zone.Galaxy, orbit); + if (target == null) return null; + if (target == zone.GalaxyZone) return (orbit, null); + var path = zone.Galaxy.FindPath(zone.GalaxyZone, target); + return path != null && path.Length > 1 ? (orbit, path[1]) : ((CultRecordKey, GalaxyZone)?) null; + } + + // A hauler through its wormhole to `hop`, on screen: it dives, and its zone changes when the dive ends. Off screen Fly transfers + // at once. The pilot's Jump verb is the only caller (PilotApplier). + public static void Jump(Ship ship, GalaxyZone hop) + { + if (ship.WormholeAnimationInProgress || hop.Contents == null) return; + ship.EnterWormhole(ship.Zone.WormholeTo(hop)); + ship.OnEnteredWormhole += () => Transfer(ship, hop); + } + + // The one move between zones: the ship leaves its zone's membership (its agent and flight go with it), is placed at the new zone's + // wormhole back to the old one and admitted piloted there, on the task its order gives it. + private static void Transfer(Ship ship, GalaxyZone hop) + { + var from = ship.Zone; + var into = hop.Contents; + from.Remove(ship); + ship.Deactivate(); + ship.Zone = into; + var at = into.WormholeTo(from.GalaxyZone); + ship.Position = float3(at.x, 0, at.y); + into.Admit(ship, piloted: true); + } + + // An off-screen hauler flown `seconds` of sim time: straight at its waypoint at its hull's top speed (Agent.TopSpeedOf), the same + // speed its pilot flies on screen, then the one cargo move at a station (Call, when CanCall; otherwise it waits there) or the + // jump at a wormhole. Position and velocity are its only writes besides Call and Jump. It stops when it waits, when a Call leaves it + // docked, or when it has jumped into a zone that is not being advanced (`advanced`: the player's zone, `current`, where it plays the + // exit animation and the full sim takes it over, or any zone outside the pass), whose next span flies it from there. + public static void Fly(Ship ship, double seconds, Zone current, ICollection advanced) + { + var order = OrderOf(ship.ItemManager, ship.Haul.Key); + var speed = Agent.TopSpeedOf(ship); + for (var steps = 0; steps < 256 && order != null && seconds > 0 && ship.Zone != null && ship.Parent == null; steps++) + { + var zone = ship.Zone; + var way = Waypoint(ship, order); + if (way == null) return; + var (orbit, hop) = way.Value; + var target = hop == null ? zone.GetOrbitPosition(orbit) : zone.WormholeTo(hop); + var offset = target - ship.Position.xz; + var distance = length(offset); + if (distance > speed * seconds) + { + var direction = normalize(offset); + ship.Position.xz += direction * (float) (speed * seconds); + ship.Velocity = direction * speed; + return; + } + seconds -= distance / speed; + ship.Position.xz = target; + ship.Velocity = float2.zero; + if (hop == null) + { + var station = Station(zone, orbit); + if (station == null || !CanCall(ship, station, order)) return; + Call(ship, station); + continue; + } + var origin = zone.GalaxyZone; + if (hop.Contents == null) return; + Transfer(ship, hop); + if (hop.Contents == current) + { + var gate = hop.Contents.WormholeTo(origin); + ship.ExitWormhole(gate, normalize(gate) * ship.ItemManager.GameplaySettings.WormholeExitVelocity); + } + if (!advanced.Contains(hop.Contents)) return; + } + } private static IReadOnlyCollection Goods(HaulOrderData order) => order.Goods.Select(good => good.Key).ToArray(); private static IEnumerable Carried(Ship ship, HaulOrderData order) diff --git a/Assets/Scripts/ServerShared/Pilot.cs b/Assets/Scripts/ServerShared/Pilot.cs index 6600d95fd..2e5249719 100644 --- a/Assets/Scripts/ServerShared/Pilot.cs +++ b/Assets/Scripts/ServerShared/Pilot.cs @@ -60,6 +60,8 @@ public sealed class PilotCommand internal (Entity Receiver, ItemInstance Item, int Quantity)? GiveVerb; // The step's call on a station (HaulRun.Call): a one-shot a hauler's pilot commands. internal Entity CallVerb; + // The step's jump through the wormhole to this zone (HaulRun.Jump): a one-shot a hauler's pilot commands at its wormhole. + internal GalaxyZone JumpVerb; internal int AimCycles; internal int ShutdownOverrideToggles, HeatsinkToggles, ShieldToggles, Pings; @@ -67,6 +69,7 @@ public sealed class PilotCommand public void SelectItem(EquippedItem item) => Selections.Add(item); public void Hail(string message) => Hails.Add(message); public void Call(Entity station) => CallVerb = station; + public void Jump(GalaxyZone hop) => JumpVerb = hop; public void Use(ConsumableItemData consumable) => Uses.Add(consumable); public void Pull(ActivatedBehavior behavior) => Triggers.Add((behavior, true)); public void Release(ActivatedBehavior behavior) => Triggers.Add((behavior, false)); @@ -124,6 +127,7 @@ public sealed class PilotCommand TargetVerb = TargetVerb.None; GiveVerb = null; CallVerb = null; + JumpVerb = null; AimCycles = 0; ShutdownOverrideToggles = HeatsinkToggles = ShieldToggles = Pings = 0; } @@ -166,6 +170,7 @@ public static class PilotApplier else if (!ReferenceEquals(give.Receiver, entity)) CargoTransfer.Begin(entity, new GiveOrder(give.Receiver, give.Item, give.Quantity)); } if (command.CallVerb is Entity station && entity is Ship caller) HaulRun.Call(caller, station); + if (command.JumpVerb is GalaxyZone hop && entity is Ship jumper) HaulRun.Jump(jumper, hop); if (command.Aim is float3 aim) entity.Aim = aim; foreach (var (behavior, pull) in command.Triggers) if (pull) behavior.Activate(); diff --git a/Assets/Scripts/ServerShared/RegionClock.cs b/Assets/Scripts/ServerShared/RegionClock.cs index b0489c228..36a5bb78c 100644 --- a/Assets/Scripts/ServerShared/RegionClock.cs +++ b/Assets/Scripts/ServerShared/RegionClock.cs @@ -3,13 +3,15 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ using System; +using System.Collections.Generic; +using System.Linq; // What simulation time does to a run's region (ruling demo-chain-spans-the-region). The player's zone steps every game // step; every other zone's facilities are advanced to the player's zone's time at each Span boundary, by the span they // missed, and entering a zone settles it and records what the player saw of the zone left. It is a run's one driver: // nothing else steps a zone in play, advances an off-screen zone, writes a visit or writes a sighting. Time has no stored -// state of its own: the player's zone's ExactTime is the region's time, an off-screen zone's span is that time minus its -// own, and both persist in ZonePack.Time. Off screen only facilities run (the tick conjures nothing, ruling +// own, and both persist in ZonePack.Time. Off screen only facilities run and haulers fly (the tick conjures nothing, ruling +// economy-is-never-faked): thermal, combat and every other ship's flight are on-screen detail. // economy-is-never-faked): thermal, flight and combat are on-screen detail. public static class RegionClock { @@ -24,8 +26,7 @@ public static class RegionClock var boundary = Math.Floor(current.ExactTime / Span); current.Update(delta); if (current.Galaxy == null || Math.Floor(current.ExactTime / Span) <= boundary) return; - foreach (var zone in current.Galaxy.Zones) - if (zone.Contents != null && zone.Contents != current) zone.Contents.AdvanceTo(current.ExactTime); + Advance(current.Galaxy, current, current.ExactTime); } // The player goes to `entering`. A zone of this galaxy being left is recorded as the player saw it (LastSeen, a copy @@ -38,12 +39,33 @@ public static class RegionClock if (leaving != null && leaving.Galaxy == galaxy && leaving.GalaxyZone != null && leaving != contents) { leaving.GalaxyZone.LastSeen = leaving.Snapshot(); - contents.AdvanceTo(leaving.ExactTime); + Advance(new[] { contents }, null, leaving.ExactTime); } galaxy.VisitedZones.Add(entering); return contents; } + + // Every zone of the galaxy that exists but the player's (`current`, null in a harness where none is) advances to `time`, then the + // haulers fly. Haulers fly off screen and nothing else does (thermal, flight and combat are on-screen detail). + public static void Advance(Galaxy galaxy, Zone current, double time) => + Advance(galaxy.Zones.Where(zone => zone.Contents != null && zone.Contents != current).Select(zone => zone.Contents).ToArray(), current, time); + + // The zones advance in the order given, each by the span it missed (time less its own ExactTime, read before it advances); then every + // hauler (a ship whose Haul is set) of those zones, snapshotted before any flies, in zone and entity order, is flown once by HaulRun.Fly + // for its starting zone's span. Known skew: a ship that crosses into or out of the player's zone, or out of a zone being entered, is + // flown by its new zone's next span from that zone's own time, up to one Span (60 s) more than it spent; no per-ship time is kept. + private static void Advance(IReadOnlyList zones, Zone current, double time) + { + var spans = zones.Select(zone => time - zone.ExactTime).ToArray(); + foreach (var zone in zones) zone.AdvanceTo(time); + var advanced = new HashSet(zones); + var flights = new List<(Ship Hauler, double Seconds)>(); + for (var i = 0; i < zones.Count; i++) + if (spans[i] > 0) + flights.AddRange(zones[i].Entities.OfType().Where(ship => ship.Haul.Key.IsSet()).Select(ship => (ship, spans[i]))); + foreach (var (hauler, seconds) in flights) HaulRun.Fly(hauler, seconds, current, advanced); + } // What the map may say of a zone: the live zone as it stands, a zone left as the player saw it leaving, and null for a // zone never seen. public static ZonePack Seen(GalaxyZone zone, Zone current) =>