Add province shortcuts to decrease verbosity

This commit is contained in:
Jaculabilis 2022-03-30 12:52:57 -07:00
parent b679558d9c
commit 9f5ecaa16a
11 changed files with 37 additions and 17 deletions

View File

@ -84,7 +84,7 @@ public class MovementDecisions
// Create a dislodge decision for this unit.
List<MoveOrder> incoming = orders
.OfType<MoveOrder>()
.Where(move => move.Location.Province == order.Unit.Location.Province)
.Where(move => move.Province == order.Unit.Province)
.ToList();
this.IsDislodged[order.Unit] = new(order, incoming);
@ -110,7 +110,7 @@ public class MovementDecisions
.OfType<MoveOrder>()
.Where(other
=> other != move
&& other.Location.Province == move.Location.Province)
&& other.Province == move.Province)
.ToList();
// Create the move-related decisions.

View File

@ -124,7 +124,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// Trivial check: cannot convoy to non-coastal province.
AdjudicatorHelpers.InvalidateIfNotMatching(
order => order.Location.Type == LocationType.Land
&& order.Location.Province.Locations.Any(loc => loc.Type == LocationType.Water),
&& order.Province.Locations.Any(loc => loc.Type == LocationType.Water),
ValidationReason.IllegalDestinationType,
ref convoyOrders,
ref validationResults);
@ -170,7 +170,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
order =>
// Map adjacency with respect to province
order.Unit.Location.Adjacents.Any(
adjLocation => adjLocation.Province == order.Target.Location.Province)
adjLocation => adjLocation.Province == order.Target.Province)
// Turn adjacency
&& Math.Abs(order.Unit.Season.Turn - order.Target.Season.Turn) <= 1
// Timeline adjacency
@ -189,7 +189,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
// Support-move orders are invalid if the unit supports a move to any location in its own
// province.
AdjudicatorHelpers.InvalidateIfNotMatching(
order => order.Unit.Location.Province != order.Location.Province,
order => order.Unit.Province != order.Province,
ValidationReason.NoSupportMoveAgainstSelf,
ref supportMoveOrders,
ref validationResults);
@ -202,7 +202,7 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator
order =>
// Map adjacency with respect to province
order.Unit.Location.Adjacents.Any(
adjLocation => adjLocation.Province == order.Location.Province)
adjLocation => adjLocation.Province == order.Province)
// Turn adjacency
&& Math.Abs(order.Unit.Season.Turn - order.Season.Turn) <= 1
// Timeline adjacency

View File

@ -36,7 +36,7 @@ public static class PathFinder
// Verify that the origin is a coastal province.
if (movingUnit.Location.Type != LocationType.Land) return false;
IEnumerable<Location> originCoasts = movingUnit.Location.Province.Locations
IEnumerable<Location> originCoasts = movingUnit.Province.Locations
.Where(location => location.Type == LocationType.Water);
if (!originCoasts.Any()) return false;

View File

@ -15,6 +15,11 @@ public class Unit
/// </summary>
public Location Location { get; }
/// <summary>
/// The province where the unit is.
/// </summary>
public Province Province => this.Location.Province;
/// <summary>
/// The season in time when the unit is.
/// </summary>
@ -33,7 +38,7 @@ public class Unit
/// <summary>
/// The unit's spatiotemporal location as a province-season tuple.
/// </summary>
public (Province province, Season season) Point => (this.Location.Province, this.Season);
public (Province province, Season season) Point => (this.Province, this.Season);
private Unit(Unit? past, Location location, Season season, Power power, UnitType type)
{
@ -46,7 +51,7 @@ public class Unit
public override string ToString()
{
return $"{this.Power} {this.Type} {this.Location.Province} {this.Season}";
return $"{this.Power} {this.Type} {this.Province} {this.Season}";
}
/// <summary>

View File

@ -291,7 +291,7 @@ public class World
seasonCoord ??= (this.RootSeason.Turn, this.RootSeason.Timeline);
Season season = GetSeason(seasonCoord.Value.turn, seasonCoord.Value.timeline);
Unit? foundUnit = this.Units.SingleOrDefault(
u => u != null && u.Location.Province == province && u.Season == season,
u => u != null && u.Province == province && u.Season == season,
null);
if (foundUnit == null) throw new KeyNotFoundException(
$"Unit at {province} at {season} not found");

View File

@ -22,6 +22,11 @@ public class ConvoyOrder : UnitOrder
/// </summary>
public Location Location { get; }
/// <summary>
/// The destination province to which the target is moving.
/// </summary>
public Province Province => this.Location.Province;
public ConvoyOrder(Power power, Unit unit, Unit target, Season season, Location location)
: base (power, unit)
{

View File

@ -17,10 +17,15 @@ public class MoveOrder : UnitOrder
/// </summary>
public Location Location { get; }
/// <summary>
/// The destination province to which the unit should move.
/// </summary>
public Province Province => this.Location.Province;
/// <summary>
/// The destination's spatiotemporal location as a province-season tuple.
/// </summary>
public (Province province, Season season) Point => (this.Location.Province, this.Season);
public (Province province, Season season) Point => (this.Province, this.Season);
public MoveOrder(Power power, Unit unit, Season season, Location location)
: base (power, unit)
@ -32,6 +37,6 @@ public class MoveOrder : UnitOrder
public bool IsOpposing(MoveOrder other)
=> this.Season == other.Unit.Season
&& other.Season == this.Unit.Season
&& this.Location.Province == other.Unit.Location.Province
&& other.Location.Province == this.Unit.Location.Province;
&& this.Province == other.Unit.Province
&& other.Province == this.Unit.Province;
}

View File

@ -17,10 +17,15 @@ public class SupportMoveOrder : SupportOrder
/// </summary>
public Location Location { get; }
/// <summary>
/// The destination province to which the target is moving.
/// </summary>
public Province Province => this.Location.Province;
/// <summary>
/// The target's destination's spatiotemporal location as a province-season tuple.
/// </summary>
public (Province province, Season season) Point => (this.Location.Province, this.Season);
public (Province province, Season season) Point => (this.Province, this.Season);
public SupportMoveOrder(Power power, Unit unit, Unit target, Season season, Location location)
: base(power, unit, target)

View File

@ -108,7 +108,7 @@ public abstract class OrderReference
DefendStrength defend => defend.Order == this.Order,
PreventStrength prevent => prevent.Order == this.Order,
HoldStrength hold => this.Order is UnitOrder unitOrder
? hold.Province == unitOrder.Unit.Location.Province
? hold.Province == unitOrder.Unit.Province
: false,
_ => false,
}).ToList();

View File

@ -249,7 +249,7 @@ public class TestCaseBuilder
foreach (Unit unit in this.World.Units)
{
if (unit.Power == power
&& unit.Location.Province == location.Province
&& unit.Province == location.Province
&& unit.Season == season)
{
return unit;

View File

@ -68,7 +68,7 @@ class TestCaseBuilderTest
List<UnitOrder> orders = setup.Orders.OfType<UnitOrder>().ToList();
Func<UnitOrder, bool> OrderForProvince(string name)
=> order => order.Unit.Location.Province.Name == name;
=> order => order.Unit.Province.Name == name;
UnitOrder orderBer = orders.Single(OrderForProvince("Berlin"));
Assert.That(orderBer, Is.InstanceOf<MoveOrder>(), "Unexpected order type");