2024-08-13 15:17:34 +00:00
|
|
|
using System.Text.Json.Serialization;
|
2022-03-30 03:40:19 +00:00
|
|
|
|
2022-03-13 04:29:00 +00:00
|
|
|
namespace MultiversalDiplomacy.Model;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The global game state.
|
|
|
|
/// </summary>
|
|
|
|
public class World
|
|
|
|
{
|
2024-08-12 04:01:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The map variant of the game.
|
|
|
|
/// </summary>
|
2024-08-13 15:17:34 +00:00
|
|
|
[JsonIgnore]
|
2024-08-13 04:58:24 +00:00
|
|
|
public Map Map { get; }
|
2024-08-12 04:01:05 +00:00
|
|
|
|
2024-08-13 15:17:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The map variant of the game.
|
|
|
|
/// </summary>
|
2024-08-14 05:14:55 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// While this is serialized to JSON, deserialization uses it to populate <see cref="Map"/>
|
|
|
|
/// </remarks>
|
2024-08-13 15:17:34 +00:00
|
|
|
public MapType MapType => this.Map.Type;
|
|
|
|
|
2022-03-13 04:29:00 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The game map.
|
|
|
|
/// </summary>
|
2024-08-13 15:17:34 +00:00
|
|
|
[JsonIgnore]
|
2024-08-14 05:14:55 +00:00
|
|
|
public IReadOnlyCollection<Province> Provinces => this.Map.Provinces;
|
2022-03-13 04:29:00 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The game powers.
|
|
|
|
/// </summary>
|
2024-08-13 15:17:34 +00:00
|
|
|
[JsonIgnore]
|
2024-08-14 05:14:55 +00:00
|
|
|
public IReadOnlyCollection<Power> Powers => this.Map.Powers;
|
2022-03-13 04:29:00 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The state of the multiverse.
|
|
|
|
/// </summary>
|
2024-08-14 05:14:55 +00:00
|
|
|
public List<Season> Seasons { get; }
|
2022-03-13 04:29:00 +00:00
|
|
|
|
2024-08-12 20:56:06 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Lookup for seasons by designation.
|
|
|
|
/// </summary>
|
2024-08-13 15:17:34 +00:00
|
|
|
[JsonIgnore]
|
2024-08-14 05:14:55 +00:00
|
|
|
public Dictionary<string, Season> SeasonLookup { get; }
|
2024-08-12 20:56:06 +00:00
|
|
|
|
2022-03-29 05:34:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The first season of the game.
|
|
|
|
/// </summary>
|
2024-08-13 15:17:34 +00:00
|
|
|
[JsonIgnore]
|
2024-08-12 21:57:16 +00:00
|
|
|
public Season RootSeason => GetSeason("a0");
|
2022-03-29 05:34:57 +00:00
|
|
|
|
2022-03-13 04:29:00 +00:00
|
|
|
/// <summary>
|
|
|
|
/// All units in the multiverse.
|
|
|
|
/// </summary>
|
2024-08-14 05:14:55 +00:00
|
|
|
public List<Unit> Units { get; }
|
2022-03-13 04:29:00 +00:00
|
|
|
|
2022-03-24 14:25:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// All retreating units in the multiverse.
|
|
|
|
/// </summary>
|
2024-08-14 05:14:55 +00:00
|
|
|
public List<RetreatingUnit> RetreatingUnits { get; }
|
2022-03-24 14:25:51 +00:00
|
|
|
|
2022-03-30 03:40:19 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Orders given to units in each season.
|
|
|
|
/// </summary>
|
2024-08-14 05:14:55 +00:00
|
|
|
public Dictionary<string, OrderHistory> OrderHistory { get; }
|
2022-03-30 03:40:19 +00:00
|
|
|
|
2024-08-13 22:24:40 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The shared timeline number generator.
|
|
|
|
/// </summary>
|
|
|
|
public TimelineFactory Timelines { get; }
|
|
|
|
|
2022-03-13 07:15:26 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Immutable game options.
|
|
|
|
/// </summary>
|
|
|
|
public Options Options { get; }
|
|
|
|
|
2024-08-14 05:14:55 +00:00
|
|
|
[JsonConstructor]
|
|
|
|
public World(
|
|
|
|
MapType mapType,
|
|
|
|
List<Season> seasons,
|
|
|
|
List<Unit> units,
|
|
|
|
List<RetreatingUnit> retreatingUnits,
|
|
|
|
Dictionary<string, OrderHistory> orderHistory,
|
|
|
|
TimelineFactory timelines,
|
|
|
|
Options options)
|
|
|
|
{
|
|
|
|
this.Map = Map.FromType(mapType);
|
|
|
|
this.Seasons = seasons;
|
|
|
|
this.Units = units;
|
|
|
|
this.RetreatingUnits = retreatingUnits;
|
|
|
|
this.OrderHistory = orderHistory;
|
|
|
|
this.Timelines = timelines;
|
|
|
|
this.Options = options;
|
|
|
|
|
|
|
|
this.SeasonLookup = new(Seasons.ToDictionary(season => $"{season.Timeline}{season.Turn}"));
|
|
|
|
}
|
|
|
|
|
2022-03-29 05:34:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Create a new World, providing all state data.
|
|
|
|
/// </summary>
|
2022-03-15 22:43:06 +00:00
|
|
|
private World(
|
2024-08-12 04:01:05 +00:00
|
|
|
Map map,
|
2024-08-14 05:14:55 +00:00
|
|
|
List<Season> seasons,
|
|
|
|
List<Unit> units,
|
|
|
|
List<RetreatingUnit> retreatingUnits,
|
|
|
|
Dictionary<string, OrderHistory> orderHistory,
|
2024-08-13 22:24:40 +00:00
|
|
|
TimelineFactory timelines,
|
2022-03-13 07:15:26 +00:00
|
|
|
Options options)
|
2022-03-13 04:29:00 +00:00
|
|
|
{
|
2024-08-12 04:01:05 +00:00
|
|
|
this.Map = map;
|
2022-03-13 04:29:00 +00:00
|
|
|
this.Seasons = seasons;
|
|
|
|
this.Units = units;
|
2022-03-24 14:25:51 +00:00
|
|
|
this.RetreatingUnits = retreatingUnits;
|
2022-11-09 00:25:47 +00:00
|
|
|
this.OrderHistory = orderHistory;
|
2024-08-13 22:24:40 +00:00
|
|
|
this.Timelines = timelines;
|
2022-03-13 07:15:26 +00:00
|
|
|
this.Options = options;
|
2024-08-12 20:56:06 +00:00
|
|
|
|
|
|
|
this.SeasonLookup = new(Seasons.ToDictionary(season => $"{season.Timeline}{season.Turn}"));
|
2022-03-13 07:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-03-29 05:34:57 +00:00
|
|
|
/// Create a new World from a previous one, replacing some state data.
|
|
|
|
/// </summary>
|
|
|
|
private World(
|
|
|
|
World previous,
|
2024-08-14 05:14:55 +00:00
|
|
|
List<Season>? seasons = null,
|
|
|
|
List<Unit>? units = null,
|
|
|
|
List<RetreatingUnit>? retreatingUnits = null,
|
|
|
|
Dictionary<string, OrderHistory>? orderHistory = null,
|
2022-03-29 05:34:57 +00:00
|
|
|
Options? options = null)
|
|
|
|
: this(
|
2024-08-12 04:01:05 +00:00
|
|
|
previous.Map,
|
2022-03-29 05:34:57 +00:00
|
|
|
seasons ?? previous.Seasons,
|
|
|
|
units ?? previous.Units,
|
|
|
|
retreatingUnits ?? previous.RetreatingUnits,
|
2022-11-09 00:25:47 +00:00
|
|
|
orderHistory ?? previous.OrderHistory,
|
2024-08-13 22:24:40 +00:00
|
|
|
previous.Timelines,
|
2022-03-29 05:34:57 +00:00
|
|
|
options ?? previous.Options)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new world with specified provinces and powers and an initial season.
|
2022-03-13 07:15:26 +00:00
|
|
|
/// </summary>
|
2024-08-12 04:01:05 +00:00
|
|
|
public static World WithMap(Map map)
|
2022-03-29 05:34:57 +00:00
|
|
|
{
|
2024-08-13 22:24:40 +00:00
|
|
|
TimelineFactory timelines = new();
|
2022-03-29 05:34:57 +00:00
|
|
|
return new World(
|
2024-08-12 04:01:05 +00:00
|
|
|
map,
|
2024-08-13 22:24:40 +00:00
|
|
|
new([new(past: null, Season.FIRST_TURN, timelines.NextTimeline())]),
|
2024-08-12 17:59:48 +00:00
|
|
|
new([]),
|
|
|
|
new([]),
|
2024-08-13 04:58:24 +00:00
|
|
|
new(new Dictionary<string, OrderHistory>()),
|
2024-08-13 22:24:40 +00:00
|
|
|
timelines,
|
2022-03-15 22:43:06 +00:00
|
|
|
new Options());
|
2022-03-29 05:34:57 +00:00
|
|
|
}
|
2022-03-15 22:43:06 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new world with the standard Diplomacy provinces and powers.
|
|
|
|
/// </summary>
|
|
|
|
public static World WithStandardMap()
|
2024-08-12 04:01:05 +00:00
|
|
|
=> WithMap(Map.Classical);
|
2022-03-15 22:43:06 +00:00
|
|
|
|
2022-03-29 05:34:57 +00:00
|
|
|
public World Update(
|
|
|
|
IEnumerable<Season>? seasons = null,
|
|
|
|
IEnumerable<Unit>? units = null,
|
2022-03-30 03:40:19 +00:00
|
|
|
IEnumerable<RetreatingUnit>? retreats = null,
|
2024-08-13 04:58:24 +00:00
|
|
|
IEnumerable<KeyValuePair<string, OrderHistory>>? orders = null)
|
2024-08-12 17:59:48 +00:00
|
|
|
=> new(
|
2022-03-29 05:34:57 +00:00
|
|
|
previous: this,
|
2022-03-30 03:40:19 +00:00
|
|
|
seasons: seasons == null
|
|
|
|
? this.Seasons
|
|
|
|
: new(seasons.ToList()),
|
|
|
|
units: units == null
|
|
|
|
? this.Units
|
|
|
|
: new(units.ToList()),
|
|
|
|
retreatingUnits: retreats == null
|
|
|
|
? this.RetreatingUnits
|
|
|
|
: new(retreats.ToList()),
|
2022-11-09 00:25:47 +00:00
|
|
|
orderHistory: orders == null
|
|
|
|
? this.OrderHistory
|
2022-03-30 03:40:19 +00:00
|
|
|
: new(orders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)));
|
2022-03-15 22:43:06 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new world with new units created from unit specs. Units specs are in the format
|
|
|
|
/// "<power> <A/F> <province> [<coast>]". If the province or coast name has a space in it, the
|
2022-03-29 05:34:57 +00:00
|
|
|
/// abbreviation should be used. Unit specs always describe units in the root season.
|
2022-03-15 22:43:06 +00:00
|
|
|
/// </summary>
|
2022-03-29 05:34:57 +00:00
|
|
|
public World AddUnits(params string[] unitSpecs)
|
2022-03-15 22:43:06 +00:00
|
|
|
{
|
|
|
|
IEnumerable<Unit> units = unitSpecs.Select(spec =>
|
|
|
|
{
|
|
|
|
string[] splits = spec.Split(' ', 4);
|
2024-08-12 04:01:05 +00:00
|
|
|
Power power = Map.GetPower(splits[0]);
|
2022-03-15 22:43:06 +00:00
|
|
|
UnitType type = splits[1] switch
|
|
|
|
{
|
|
|
|
"A" => UnitType.Army,
|
|
|
|
"F" => UnitType.Fleet,
|
2022-03-23 04:27:06 +00:00
|
|
|
_ => throw new ApplicationException($"Unknown unit type {splits[1]}")
|
2022-03-15 22:43:06 +00:00
|
|
|
};
|
|
|
|
Location location = type == UnitType.Army
|
2024-08-12 04:01:05 +00:00
|
|
|
? Map.GetLand(splits[2])
|
2022-03-15 22:43:06 +00:00
|
|
|
: splits.Length == 3
|
2024-08-12 04:01:05 +00:00
|
|
|
? Map.GetWater(splits[2])
|
|
|
|
: Map.GetWater(splits[2], splits[3]);
|
2022-03-29 05:34:57 +00:00
|
|
|
Unit unit = Unit.Build(location, this.RootSeason, power, type);
|
2022-03-15 22:43:06 +00:00
|
|
|
return unit;
|
|
|
|
});
|
2022-03-29 05:34:57 +00:00
|
|
|
return this.Update(units: units);
|
2022-03-15 22:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create a new world with standard Diplomacy initial unit placements.
|
|
|
|
/// </summary>
|
2022-03-29 05:34:57 +00:00
|
|
|
public World AddStandardUnits()
|
2022-03-15 22:43:06 +00:00
|
|
|
{
|
2022-03-29 05:34:57 +00:00
|
|
|
return this.AddUnits(
|
2022-03-15 22:43:06 +00:00
|
|
|
"Austria A Bud",
|
|
|
|
"Austria A Vir",
|
|
|
|
"Austria F Tri",
|
|
|
|
"England A Lvp",
|
|
|
|
"England F Edi",
|
|
|
|
"England F Lon",
|
|
|
|
"France A Mar",
|
|
|
|
"France A Par",
|
|
|
|
"France F Bre",
|
|
|
|
"Germany A Ber",
|
|
|
|
"Germany A Mun",
|
|
|
|
"Germany F Kie",
|
|
|
|
"Italy A Rom",
|
|
|
|
"Italy A Ven",
|
|
|
|
"Italy F Nap",
|
|
|
|
"Russia A Mos",
|
|
|
|
"Russia A War",
|
|
|
|
"Russia F Sev",
|
|
|
|
"Russia F Stp wc",
|
|
|
|
"Turkey A Con",
|
|
|
|
"Turkey A Smy",
|
|
|
|
"Turkey F Ank"
|
|
|
|
);
|
|
|
|
}
|
2022-03-13 07:15:26 +00:00
|
|
|
|
2024-08-12 20:56:06 +00:00
|
|
|
/// <summary>
|
2024-08-14 05:14:55 +00:00
|
|
|
/// Create a continuation of this season if it has no futures, otherwise create a fork.
|
2024-08-12 20:56:06 +00:00
|
|
|
/// </summary>
|
2024-08-13 22:24:40 +00:00
|
|
|
public Season ContinueOrFork(Season season)
|
|
|
|
=> GetFutures(season).Any()
|
|
|
|
? new(season.Designation, season.Turn + 1, Timelines.NextTimeline())
|
|
|
|
: new(season.Designation, season.Turn + 1, season.Timeline);
|
2024-08-12 20:56:06 +00:00
|
|
|
|
2022-03-13 07:15:26 +00:00
|
|
|
/// <summary>
|
2022-03-15 22:43:06 +00:00
|
|
|
/// A standard Diplomacy game setup.
|
2022-03-13 07:15:26 +00:00
|
|
|
/// </summary>
|
2024-08-13 15:17:34 +00:00
|
|
|
public static World Standard => WithStandardMap().AddStandardUnits();
|
2022-03-13 07:15:26 +00:00
|
|
|
|
2022-03-30 00:16:00 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get a season by coordinate. Throws if the season is not found.
|
|
|
|
/// </summary>
|
2024-08-12 16:28:56 +00:00
|
|
|
public Season GetSeason(string timeline, int turn)
|
2024-08-12 21:57:16 +00:00
|
|
|
=> GetSeason($"{timeline}{turn}");
|
2022-03-30 00:16:00 +00:00
|
|
|
|
2024-08-14 05:14:55 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get a season by designation.
|
|
|
|
/// </summary>
|
2024-08-12 20:56:06 +00:00
|
|
|
public Season GetSeason(string designation)
|
|
|
|
=> SeasonLookup[designation];
|
|
|
|
|
2024-08-12 22:25:23 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get all seasons that are immediate futures of a season.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="present">A season designation.</param>
|
|
|
|
/// <returns>The immediate futures of the designated season.</returns>
|
|
|
|
public IEnumerable<Season> GetFutures(string present)
|
|
|
|
=> Seasons.Where(future => future.Past == present);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get all seasons that are immediate futures of a season.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="present">A season.</param>
|
|
|
|
/// <returns>The immediate futures of the season.</returns>
|
|
|
|
public IEnumerable<Season> GetFutures(Season present) => GetFutures(present.Designation);
|
|
|
|
|
2024-08-12 21:39:35 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the first season in this season's timeline. The first season is the
|
|
|
|
/// root of the first timeline. The earliest season in each alternate timeline is
|
|
|
|
/// the root of that timeline.
|
|
|
|
/// </summary>
|
|
|
|
public Season GetTimelineRoot(Season season)
|
|
|
|
{
|
2024-08-12 21:52:50 +00:00
|
|
|
if (season.Past is null) {
|
2024-08-12 21:51:07 +00:00
|
|
|
return season;
|
|
|
|
}
|
2024-08-12 21:52:50 +00:00
|
|
|
Season past = SeasonLookup[season.Past];
|
2024-08-12 21:51:07 +00:00
|
|
|
return season.Timeline == past.Timeline
|
|
|
|
? GetTimelineRoot(past)
|
2024-08-12 21:39:35 +00:00
|
|
|
: season;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns whether this season is in an adjacent timeline to another season.
|
|
|
|
/// Seasons are considered to be in adjacent timelines if they are in the same timeline,
|
|
|
|
/// one is in a timeline that branched from the other's timeline, or both are in timelines
|
|
|
|
/// that branched from the same point.
|
|
|
|
/// </summary>
|
|
|
|
public bool InAdjacentTimeline(Season one, Season two)
|
|
|
|
{
|
|
|
|
// Timelines are adjacent to themselves. Early out in that case.
|
|
|
|
if (one == two) return true;
|
|
|
|
|
|
|
|
// If the timelines aren't identical, one of them isn't the initial trunk.
|
|
|
|
// They can still be adjacent if one of them branched off of the other, or
|
|
|
|
// if they both branched off of the same point.
|
|
|
|
Season rootOne = GetTimelineRoot(one);
|
|
|
|
Season rootTwo = GetTimelineRoot(two);
|
2024-08-12 21:52:50 +00:00
|
|
|
bool oneForked = rootOne.Past != null && GetSeason(rootOne.Past).Timeline == two.Timeline;
|
|
|
|
bool twoForked = rootTwo.Past != null && GetSeason(rootTwo.Past).Timeline == one.Timeline;
|
|
|
|
bool bothForked = rootOne.Past == rootTwo.Past;
|
2024-08-12 21:39:35 +00:00
|
|
|
return oneForked || twoForked || bothForked;
|
|
|
|
}
|
|
|
|
|
2022-03-13 07:15:26 +00:00
|
|
|
/// <summary>
|
2022-03-30 00:16:00 +00:00
|
|
|
/// Returns a unit in a province. Throws if there are duplicate units.
|
2022-03-13 07:15:26 +00:00
|
|
|
/// </summary>
|
2024-08-13 23:24:43 +00:00
|
|
|
public Unit GetUnitAt(string provinceName, Season? season = null)
|
2022-03-13 07:15:26 +00:00
|
|
|
{
|
2024-08-12 04:01:05 +00:00
|
|
|
Province province = Map.GetProvince(provinceName);
|
2024-08-13 23:24:43 +00:00
|
|
|
season ??= RootSeason;
|
2022-03-15 22:43:06 +00:00
|
|
|
Unit? foundUnit = this.Units.SingleOrDefault(
|
2024-08-14 14:39:49 +00:00
|
|
|
u => Map.GetLocation(u!).Province == province && u!.Season == season,
|
2024-08-12 16:28:56 +00:00
|
|
|
null)
|
|
|
|
?? throw new KeyNotFoundException($"Unit at {province} at {season} not found");
|
2022-03-15 22:43:06 +00:00
|
|
|
return foundUnit;
|
2022-03-13 07:15:26 +00:00
|
|
|
}
|
2024-08-12 16:28:56 +00:00
|
|
|
}
|