Implement coastal accessibility checks for move orders

This commit is contained in:
Tim Van Baak 2024-09-06 15:09:30 +00:00
parent 864a933ba0
commit aaf2e78730
1 changed files with 29 additions and 18 deletions

View File

@ -307,34 +307,45 @@ public class OrderParser(World world)
: subject.Season.Turn; : subject.Season.Turn;
var destProvince = world.Map.Provinces.Single(province => province.Is(move.destProvince)); var destProvince = world.Map.Provinces.Single(province => province.Is(move.destProvince));
string? destLocationKey = null;
// DATC 4.B.6 requires that "irrelevant" locations like army to Spain nc be ignored. // DATC 4.B specifies how to interpret orders with missing or incorrect locations. These issues arise because
// To satisfy this, any location of the wrong type is categorically ignored, so for an army the // of provinces with multiple locations of the same type, i.e. two-coast provinces in Classical. In general,
// "north coast" location effectively doesn't exist here. // DATC's only concern is to disambiguate the order, failing the order only when it is ineluctably ambiguous
// (4.B.1) or explicitly incorrect (4.B.3). Irrelevant or nonexistent locations can be ignored.
// If there is only one possible location for the moving unit, that location is used. The idea of land and
// water locations is an implementation detail of 5dplomacy and not part of the Diplomacy rules, so they will
// usually be omitted, and so moving an army to any land province or a fleet to a non-multi-coast province is
// naturally unambiguous even without the location.
var unitLocations = destProvince.Locations.Where(loc => loc.Type switch { var unitLocations = destProvince.Locations.Where(loc => loc.Type switch {
LocationType.Land => subject.Type == UnitType.Army, LocationType.Land => subject.Type == UnitType.Army,
LocationType.Water => subject.Type == UnitType.Fleet, LocationType.Water => subject.Type == UnitType.Fleet,
_ => false, _ => false,
}); });
// DATC 4.6.B also requires that unknown coasts be ignored. To satisfy this, an additional filter by name. if (!unitLocations.Any()) return false; // If *no* locations match, the move is illegal
// Doing both of these filters means "A - Spain/nc" is as meaningful as "F - Spain/wc". if (unitLocations.Count() == 1) destLocationKey ??= unitLocations.Single().Key;
var matchingLocations = unitLocations.Where(loc => loc.Is(move.destLocation));
// If one location matched, use that location. If the coast is inaccessible to the subject, the order will
// be invalidated by a path check later to satisfy DATC 4.B.3.
string? destLocationKey = matchingLocations.FirstOrDefault(defaultValue: null)?.Key;
// If more than one location is possible for the unit, the order must be disambiguated by the dest location
// or the physical realities of which coast is accessible. DATC 4.B.3 makes an order illegal if the location
// is specified but it isn't an accessible coast, so successfully specifying a location takes precedence over
// there being one accessible coast.
if (destLocationKey is null) { if (destLocationKey is null) {
// If no location matched, location was omitted, nonexistent, or the wrong type. var matchingLocations = unitLocations.Where(loc => loc.Is(move.destLocation));
// If one location is accessible, DATC 4.B.2 requires that it be used. if (matchingLocations.Any()) destLocationKey ??= matchingLocations.Single().Key;
// If more than one location is accessible, DATC 4.B.1 requires the order fail.
// TODO check which locations are accessible per the above
destLocationKey = unitLocations.First().Key;
// return false;
} }
// If the order location didn't disambiguate the coasts, either because it's missing or it's nonsense, the
// order can be disambiguated by there being one accessible coast from the order source.
if (destLocationKey is null) {
Location source = world.Map.GetLocation(subject.Location);
var accessibleLocations = destProvince.Locations.Where(loc => loc.Adjacents.Contains(source));
if (accessibleLocations.Count() == 1) destLocationKey ??= accessibleLocations.Single().Key;
}
// If the order is still ambiguous, fail per DATC 4.B.1.
if (destLocationKey is null) return false;
order = new MoveOrder(power, subject, new(destTimeline, destTurn), destLocationKey); order = new MoveOrder(power, subject, new(destTimeline, destTurn), destLocationKey);
return true; return true;
} }