diff --git a/Eu07.BlackBox.Views/CabPanel.cs b/Eu07.BlackBox.Views/CabPanel.cs index e6cdfb1..741bace 100644 --- a/Eu07.BlackBox.Views/CabPanel.cs +++ b/Eu07.BlackBox.Views/CabPanel.cs @@ -226,7 +226,7 @@ public sealed class CabPanel : Control // The doors under them, in two columns: open, permitted, the departure signal, the steps and the lock. top = Math.Max(bottom, top + 16 + 9 * LampRow) + 6; context.DrawText(MapView.Text(Strings["CabGroupDoors"], Palette.DimText, 10, FontWeight.SemiBold), new Point(Pad, top)); - var doors = Doors(s); + var doors = Doors(s.AsTheCabSees); double doorColumn = width / DoorColumns; for (int i = 0; i < doors.Length; i++) { diff --git a/Eu07.BlackBox.Views/EventList.cs b/Eu07.BlackBox.Views/EventList.cs index be3b07d..a47f486 100644 --- a/Eu07.BlackBox.Views/EventList.cs +++ b/Eu07.BlackBox.Views/EventList.cs @@ -282,6 +282,8 @@ public sealed class EventList : ListBox (CabDevices.DoorStep, "DevDoorStep"), (CabDevices.DoorLock, "DevDoorLock"), (CabDevices.SpeedControl, "DevSpeedControl") ]; + private const CabDevices DoorSides = CabDevices.DoorsLeft | CabDevices.DoorsRight | CabDevices.DoorPermitLeft | CabDevices.DoorPermitRight; + /// /// What the driver did in the cab, from one look to the next in the same vehicle: the switches on and off, the /// doors opened and closed, the departure signal, the hand brake, the brakes' setting, the radio's channel, the @@ -291,7 +293,8 @@ public sealed class EventList : ListBox { CabSample? last = null; bool movingOpen = false; - foreach (var s in recording.Cab) + // the doors' sides as the driver has them, from the cab the driver is in (CabSample.AsTheCabSees) + foreach (var s in recording.Cab.Select(sample => sample.AsTheCabSees)) { if (last is not { } b || b.Vehicle != s.Vehicle) { @@ -299,8 +302,10 @@ public sealed class EventList : ListBox movingOpen = false; continue; } - bool Came(CabDevices device) => !b.Has(device) && s.Has(device); - bool Went(CabDevices device) => b.Has(device) && !s.Has(device); + // another cab swaps the sides, and tells nothing of the doors + bool sameCab = b.CabEnd == s.CabEnd; + bool Came(CabDevices device) => !b.Has(device) && s.Has(device) && (sameCab || (device & DoorSides) == 0); + bool Went(CabDevices device) => b.Has(device) && !s.Has(device) && (sameCab || (device & DoorSides) == 0); foreach (var (device, key) in Switched) { if (Came(device)) diff --git a/Eu07.BlackBox/Records.cs b/Eu07.BlackBox/Records.cs index 91df8d0..bf100d7 100644 --- a/Eu07.BlackBox/Records.cs +++ b/Eu07.BlackBox/Records.cs @@ -375,6 +375,26 @@ public readonly record struct CabSample(double Time, int Vehicle, Position Posit public bool Has(CabDevices device) => (Devices & device) == device; public bool Has(SecurityFlags flag) => (Security & flag) == flag; + private const CabDevices LeftDoors = CabDevices.DoorsLeft | CabDevices.DoorPermitLeft; + private const CabDevices RightDoors = CabDevices.DoorsRight | CabDevices.DoorPermitRight; + + /// + /// The doors as the driver has them: the recording holds the left and right of the vehicle, and from its rear + /// cab the driver's left is the vehicle's right, as the simulator works the doors from it. Out of a cab, the + /// vehicle's. + /// + public CabSample AsTheCabSees => + CabEnd >= 0 + ? this + : this with + { + Devices = (Devices & ~(LeftDoors | RightDoors)) + | ((Devices & CabDevices.DoorsLeft) != 0 ? CabDevices.DoorsRight : 0) + | ((Devices & CabDevices.DoorsRight) != 0 ? CabDevices.DoorsLeft : 0) + | ((Devices & CabDevices.DoorPermitLeft) != 0 ? CabDevices.DoorPermitRight : 0) + | ((Devices & CabDevices.DoorPermitRight) != 0 ? CabDevices.DoorPermitLeft : 0) + }; + public bool AiValid => (Flags & CabFlags.AiValid) != 0; public bool InCab => (Flags & CabFlags.InCab) != 0; public float AbsSpeedKmh => Math.Abs(SpeedKmh); diff --git a/StarterNG.Tests/BlackBox/CabDoorsTests.cs b/StarterNG.Tests/BlackBox/CabDoorsTests.cs new file mode 100644 index 0000000..7907e73 --- /dev/null +++ b/StarterNG.Tests/BlackBox/CabDoorsTests.cs @@ -0,0 +1,31 @@ +using Eu07.BlackBox; + +namespace StarterNG.Tests.BlackBox; + +public class CabDoorsTests +{ + private static CabSample Sample(sbyte cab, CabDevices devices) => + default(CabSample) with { CabEnd = cab, Devices = devices | CabDevices.Radio }; + + [Fact] + public void From_the_rear_cab_the_vehicles_right_doors_are_the_drivers_left() + { + var seen = Sample(-1, CabDevices.DoorsRight | CabDevices.DoorPermitRight).AsTheCabSees; + + Assert.True(seen.Has(CabDevices.DoorsLeft)); + Assert.True(seen.Has(CabDevices.DoorPermitLeft)); + Assert.False(seen.Has(CabDevices.DoorsRight)); + Assert.False(seen.Has(CabDevices.DoorPermitRight)); + Assert.True(seen.Has(CabDevices.Radio)); + } + + [Theory] + [InlineData(1)] + [InlineData(0)] + public void From_the_front_cab_or_out_of_one_the_sides_are_the_vehicles(sbyte cab) + { + var sample = Sample(cab, CabDevices.DoorsLeft | CabDevices.DoorPermitRight); + + Assert.Equal(sample, sample.AsTheCabSees); + } +}