Turn multi-car units as a whole when orientation is randomised or a unit swapped

Randomising flipped only the card's flag, so a multi-car unit was exported
with its cars in reverse order but each still facing forward - coupled back
to front. A swap copied the flag without turning the new cars. Both now turn
flag, cars and order together, as Flip does, and a locked unit as one.
This commit is contained in:
maj00r
2026-10-04 13:54:51 +02:00
parent c1c47f746e
commit d510a55740
5 changed files with 133 additions and 19 deletions

View File

@@ -256,9 +256,9 @@ public sealed class Consist : IReadOnlyList<ConsistItem>
}
/// <summary>
/// Puts item in place of the vehicle at index. Its crew moves over where the
/// replacement can take it - a driver only into something powered - and the
/// rest is staffed as a newly added vehicle would be.
/// Puts item in place of the vehicle at index, facing the same way. Its crew
/// moves over where the replacement can take it - a driver only into something
/// powered - and the rest is staffed as a newly added vehicle would be.
/// </summary>
public void Replace(int index, ConsistItem item)
{
@@ -266,6 +266,10 @@ public sealed class Consist : IReadOnlyList<ConsistItem>
_items[index] = item;
Staff(item, index);
// Facing the way the replaced vehicle did - cars and all, as Flip would.
if (item.Flipped != replaced.Flipped)
Turn(index, index);
var crew = replaced.Driver;
if (crew == eDriverType.Passenger ||
(crew is eDriverType.Headdriver or eDriverType.Reardriver && IsPowered(item)))
@@ -339,6 +343,16 @@ public sealed class Consist : IReadOnlyList<ConsistItem>
int index = _items.IndexOf(item);
if (index < 0) return;
var (first, last) = UnitAround(index);
Turn(first, last);
AutoConnectAll();
Raise();
}
/// <summary>The cards locked together with the one at index, as the range they span.</summary>
private (int First, int Last) UnitAround(int index)
{
int first = index;
while (first > 0 && HoldsTail(_items[first - 1]))
first--;
@@ -347,6 +361,17 @@ public sealed class Consist : IReadOnlyList<ConsistItem>
while (last + 1 < _items.Count && HoldsTail(_items[last]))
last++;
return (first, last);
}
/// <summary>
/// Turns the cards first..last end for end - the flag on each card, the cars it
/// holds and the order of the cards. All three or nothing: the export writes the
/// cars in card order and each car with its own orientation, so a flag turned
/// alone couples a multi-car unit back to front.
/// </summary>
private void Turn(int first, int last)
{
for (int k = first; k <= last; k++)
{
var card = _items[k];
@@ -357,9 +382,6 @@ public sealed class Consist : IReadOnlyList<ConsistItem>
if (last > first)
_items.Reverse(first, last - first + 1);
AutoConnectAll();
Raise();
}
public void CycleDriver(ConsistItem item)
@@ -492,9 +514,15 @@ public sealed class Consist : IReadOnlyList<ConsistItem>
if (start < 0) start = 0;
if (start >= _items.Count) return;
for (int i = start; i < _items.Count; i++)
// Unit by unit, as Flip turns them: half a locked unit turned on its own
// would be coupled back to front.
for (int i = start; i < _items.Count;)
{
var (first, last) = UnitAround(i);
if (rng.Next(2) == 0)
_items[i].Flipped = !_items[i].Flipped;
Turn(first, last);
i = last + 1;
}
AutoConnectAll();
Raise();

View File

@@ -1,14 +1,19 @@
using StarterNG.Classes;
using StarterNG.Domain.Vehicles;
using StarterNG.Application;
using StarterNG.Application.Abstractions;
namespace StarterNG.Domain;
public sealed class VehicleInfo
{
private readonly VehicleCatalog _db;
private readonly IPhysicsRepository _physics;
public VehicleInfo(VehicleCatalog db) => _db = db;
public VehicleInfo(VehicleCatalog db, IPhysicsRepository physics)
{
_db = db;
_physics = physics;
}
public static string ClassOf(VehicleTexture t) => t.ResolvedClass;
@@ -23,14 +28,14 @@ public sealed class VehicleInfo
TextureFor(car) is { } t ? CategoryOf(t) : null;
public VehiclePhysics? PhysicsFor(VehicleTexture texture) =>
AppServices.Current.Physics.For(texture.Directory, texture.Model)
?? AppServices.Current.Physics.For(texture.Directory, texture.Skinfile);
_physics.For(texture.Directory, texture.Model)
?? _physics.For(texture.Directory, texture.Skinfile);
public VehiclePhysics? PhysicsFor(Dynamic car)
{
string? dbModel = TextureFor(car)?.Model;
return AppServices.Current.Physics.For(car.DataFolder, dbModel)
?? AppServices.Current.Physics.For(car.DataFolder, car.MmdFile)
?? AppServices.Current.Physics.For(car.DataFolder, car.SkinFile);
return _physics.For(car.DataFolder, dbModel)
?? _physics.For(car.DataFolder, car.MmdFile)
?? _physics.For(car.DataFolder, car.SkinFile);
}
}

View File

@@ -56,7 +56,7 @@ public partial class Depot : UserControl
public Depot()
{
_info = new VehicleInfo(_db);
_info = new VehicleInfo(_db, AppServices.Current.Physics);
_consist = new Consist(_db, _info);
_cargo = new Cargo(_info, _rng);
_consist.Changed += RebuildConsist;
@@ -572,8 +572,7 @@ public partial class Depot : UserControl
var item = new ConsistItem
{
Cars = unit.Select(NewVehicle).ToList(),
Grouped = unit.Count > 1,
Flipped = _consist.Selected.Flipped
Grouped = unit.Count > 1
};
_consist.Replace(i, item);
_consist.Selected = item;