Remove Location reference from Unit

This commit is contained in:
Tim Van Baak 2024-08-14 08:20:04 -07:00
parent e1772ce60b
commit 885628900b
10 changed files with 23 additions and 27 deletions

View File

@ -92,7 +92,7 @@ public class MovementDecisions
.ToList();
(Province province, Season season) Point(Unit unit)
=> (world.Map.GetLocation(unit.LocationId).Province, unit.Season);
=> (world.Map.GetLocation(unit.Location).Province, unit.Season);
// Create a hold strength decision with an associated order for every province with a unit.
foreach (UnitOrder order in relevantOrders)

View File

@ -77,7 +77,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// Trivial check: a unit cannot move to where it already is.
AdjudicatorHelpers.InvalidateIfNotMatching(
order => !(order.Location.Designation == order.Unit.LocationId && order.Season == order.Unit.Season),
order => !(order.Location.Designation == order.Unit.Location && order.Season == order.Unit.Season),
ValidationReason.DestinationMatchesOrigin,
ref moveOrders,
ref validationResults);
@ -138,7 +138,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// Trivial check: cannot convoy a unit to its own location
AdjudicatorHelpers.InvalidateIfNotMatching(
order => !(
order.Location.Designation == order.Target.LocationId
order.Location.Designation == order.Target.Location
&& order.Season == order.Target.Season),
ValidationReason.DestinationMatchesOrigin,
ref convoyOrders,
@ -337,7 +337,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
Season moveSeason = doesMove.Order.Season;
if (doesMove.Outcome == true && createdFutures.ContainsKey(moveSeason))
{
Unit next = doesMove.Order.Unit.Next(doesMove.Order.Location, createdFutures[moveSeason]);
Unit next = doesMove.Order.Unit.Next(doesMove.Order.Location.Designation, createdFutures[moveSeason]);
logger.Log(3, "Advancing unit to {0}", next);
createdUnits.Add(next);
}
@ -366,7 +366,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
if (isDislodged.Outcome == false)
{
// Non-dislodged units continue into the future.
Unit next = order.Unit.Next(world.Map.GetLocation(order.Unit), future);
Unit next = order.Unit.Next(world.Map.GetLocation(order.Unit).Designation, future);
logger.Log(3, "Advancing unit to {0}", next);
createdUnits.Add(next);
}

View File

@ -32,7 +32,7 @@ public static class PathFinder
// belong to another power or were not given convoy orders; it will simply fail.
IDictionary<(string location, Season season), Unit> fleets = world.Units
.Where(unit => unit.Type == UnitType.Fleet)
.ToDictionary(unit => (unit.LocationId, unit.Season));
.ToDictionary(unit => (unit.Location, unit.Season));
// Verify that the origin is a coastal province.
if (world.Map.GetLocation(movingUnit).Type != LocationType.Land) return false;

View File

@ -67,7 +67,7 @@ public class Map
=> LocationLookup[designation];
public Location GetLocation(Unit unit)
=> GetLocation(unit.LocationId);
=> GetLocation(unit.Location);
/// <summary>
/// Get the sole land location of a province.

View File

@ -1,5 +1,3 @@
using System.Text.Json.Serialization;
namespace MultiversalDiplomacy.Model;
/// <summary>
@ -15,9 +13,7 @@ public class Unit
/// <summary>
/// The location on the map where the unit is.
/// </summary>
public Location Location { get; }
public string LocationId => Location.Designation;
public string Location { get; }
/// <summary>
/// The season in time when the unit is.
@ -34,7 +30,7 @@ public class Unit
/// </summary>
public UnitType Type { get; }
private Unit(Unit? past, Location location, Season season, Power power, UnitType type)
private Unit(Unit? past, string location, Season season, Power power, UnitType type)
{
this.Past = past;
this.Location = location;
@ -44,18 +40,18 @@ public class Unit
}
public override string ToString()
=> $"{Power.Name[0]} {Type.ToShort()} {Season.Timeline}-{LocationId}@{Season.Turn}";
=> $"{Power.Name[0]} {Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}";
/// <summary>
/// Create a new unit. No validation is performed; the adjudicator should only call this
/// method after accepting a build order.
/// </summary>
public static Unit Build(Location location, Season season, Power power, UnitType type)
public static Unit Build(string location, Season season, Power power, UnitType type)
=> new(past: null, location, season, power, type);
/// <summary>
/// Advance this unit's timeline to a new location and season.
/// </summary>
public Unit Next(Location location, Season season)
public Unit Next(string location, Season season)
=> new(past: this, location, season, this.Power, this.Type);
}

View File

@ -204,7 +204,7 @@ public class World
: splits.Length == 3
? Map.GetWater(splits[2])
: Map.GetWater(splits[2], splits[3]);
Unit unit = Unit.Build(location, this.RootSeason, power, type);
Unit unit = Unit.Build(location.Designation, this.RootSeason, power, type);
return unit;
});
return this.Update(units: units);

View File

@ -179,7 +179,7 @@ public class MovementAdjudicatorTest
// Confirm the unit was created
Assert.That(updated.Units.Count, Is.EqualTo(2));
Unit second = updated.Units.Single(u => u.Past != null);
Assert.That(second.LocationId, Is.EqualTo(mun.Order.Unit.LocationId));
Assert.That(second.Location, Is.EqualTo(mun.Order.Unit.Location));
}
[Test]

View File

@ -270,7 +270,7 @@ public class TestCaseBuilder
}
// Not found
Unit newUnit = Unit.Build(location, season, power, type);
Unit newUnit = Unit.Build(location.Designation, season, power, type);
this.World = this.World.Update(units: this.World.Units.Append(newUnit));
return newUnit;
}

View File

@ -39,7 +39,7 @@ class TestCaseBuilderTest
Assert.That(fleetSTP.Power.Name, Is.EqualTo("Russia"), "Unit created with wrong power");
Assert.That(fleetSTP.Type, Is.EqualTo(UnitType.Fleet), "Unit created with wrong type");
Assert.That(
fleetSTP.LocationId,
fleetSTP.Location,
Is.EqualTo(setup.World.Map.GetWater("STP", "wc").Designation),
"Unit created on wrong coast");
}
@ -127,7 +127,7 @@ class TestCaseBuilderTest
Is.EqualTo(setup.World.Map.GetPower("Germany")),
"Wrong power");
Assert.That(
orderMun.Order.Unit.LocationId,
orderMun.Order.Unit.Location,
Is.EqualTo(setup.World.Map.GetLand("Mun").Designation),
"Wrong unit");

View File

@ -15,13 +15,13 @@ public class UnitTests
Tyr = world.Map.GetLand("Tyr");
Power pw1 = world.Map.GetPower("Austria");
Season a0 = world.RootSeason;
Unit u1 = Unit.Build(Mun, a0, pw1, UnitType.Army);
Unit u1 = Unit.Build(Mun.Designation, a0, pw1, UnitType.Army);
world = world.ContinueOrFork(a0, out Season a1);
Unit u2 = u1.Next(Boh, a1);
Unit u2 = u1.Next(Boh.Designation, a1);
_ = world.ContinueOrFork(a1, out Season a2);
Unit u3 = u2.Next(Tyr, a2);
Unit u3 = u2.Next(Tyr.Designation, a2);
Assert.That(u3.Past, Is.EqualTo(u2), "Missing unit past");
Assert.That(u2.Past, Is.EqualTo(u1), "Missing unit past");
@ -31,8 +31,8 @@ public class UnitTests
Assert.That(u2.Season, Is.EqualTo(a1), "Unexpected unit season");
Assert.That(u3.Season, Is.EqualTo(a2), "Unexpected unit season");
Assert.That(u1.LocationId, Is.EqualTo(Mun.Designation), "Unexpected unit location");
Assert.That(u2.LocationId, Is.EqualTo(Boh.Designation), "Unexpected unit location");
Assert.That(u3.LocationId, Is.EqualTo(Tyr.Designation), "Unexpected unit location");
Assert.That(u1.Location, Is.EqualTo(Mun.Designation), "Unexpected unit location");
Assert.That(u2.Location, Is.EqualTo(Boh.Designation), "Unexpected unit location");
Assert.That(u3.Location, Is.EqualTo(Tyr.Designation), "Unexpected unit location");
}
}