235 lines
7.4 KiB
C#
235 lines
7.4 KiB
C#
using System.Collections.ObjectModel;
|
|
|
|
using MultiversalDiplomacy.Orders;
|
|
|
|
namespace MultiversalDiplomacy.Model;
|
|
|
|
/// <summary>
|
|
/// The global game state.
|
|
/// </summary>
|
|
public class World
|
|
{
|
|
/// <summary>
|
|
/// The map variant of the game.
|
|
/// </summary>
|
|
public readonly Map Map;
|
|
|
|
/// <summary>
|
|
/// The game map.
|
|
/// </summary>
|
|
public ReadOnlyCollection<Province> Provinces => this.Map.Provinces;
|
|
|
|
/// <summary>
|
|
/// The game powers.
|
|
/// </summary>
|
|
public ReadOnlyCollection<Power> Powers => this.Map.Powers;
|
|
|
|
/// <summary>
|
|
/// The state of the multiverse.
|
|
/// </summary>
|
|
public ReadOnlyCollection<Season> Seasons { get; }
|
|
|
|
/// <summary>
|
|
/// The first season of the game.
|
|
/// </summary>
|
|
public Season RootSeason { get; }
|
|
|
|
/// <summary>
|
|
/// All units in the multiverse.
|
|
/// </summary>
|
|
public ReadOnlyCollection<Unit> Units { get; }
|
|
|
|
/// <summary>
|
|
/// All retreating units in the multiverse.
|
|
/// </summary>
|
|
public ReadOnlyCollection<RetreatingUnit> RetreatingUnits { get; }
|
|
|
|
/// <summary>
|
|
/// Orders given to units in each season.
|
|
/// </summary>
|
|
public ReadOnlyDictionary<Season, OrderHistory> OrderHistory { get; }
|
|
|
|
/// <summary>
|
|
/// Immutable game options.
|
|
/// </summary>
|
|
public Options Options { get; }
|
|
|
|
/// <summary>
|
|
/// Create a new World, providing all state data.
|
|
/// </summary>
|
|
private World(
|
|
Map map,
|
|
ReadOnlyCollection<Season> seasons,
|
|
Season rootSeason,
|
|
ReadOnlyCollection<Unit> units,
|
|
ReadOnlyCollection<RetreatingUnit> retreatingUnits,
|
|
ReadOnlyDictionary<Season, OrderHistory> orderHistory,
|
|
Options options)
|
|
{
|
|
this.Map = map;
|
|
this.Seasons = seasons;
|
|
this.RootSeason = rootSeason;
|
|
this.Units = units;
|
|
this.RetreatingUnits = retreatingUnits;
|
|
this.OrderHistory = orderHistory;
|
|
this.Options = options;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new World from a previous one, replacing some state data.
|
|
/// </summary>
|
|
private World(
|
|
World previous,
|
|
ReadOnlyCollection<Season>? seasons = null,
|
|
ReadOnlyCollection<Unit>? units = null,
|
|
ReadOnlyCollection<RetreatingUnit>? retreatingUnits = null,
|
|
ReadOnlyDictionary<Season, OrderHistory>? orderHistory = null,
|
|
Options? options = null)
|
|
: this(
|
|
previous.Map,
|
|
seasons ?? previous.Seasons,
|
|
previous.RootSeason, // Can't change the root season
|
|
units ?? previous.Units,
|
|
retreatingUnits ?? previous.RetreatingUnits,
|
|
orderHistory ?? previous.OrderHistory,
|
|
options ?? previous.Options)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new world with specified provinces and powers and an initial season.
|
|
/// </summary>
|
|
public static World WithMap(Map map)
|
|
{
|
|
Season root = Season.MakeRoot();
|
|
return new World(
|
|
map,
|
|
new(new List<Season> { root }),
|
|
root,
|
|
new(new List<Unit>()),
|
|
new(new List<RetreatingUnit>()),
|
|
new(new Dictionary<Season, OrderHistory>()),
|
|
new Options());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new world with the standard Diplomacy provinces and powers.
|
|
/// </summary>
|
|
public static World WithStandardMap()
|
|
=> WithMap(Map.Classical);
|
|
|
|
public World Update(
|
|
IEnumerable<Season>? seasons = null,
|
|
IEnumerable<Unit>? units = null,
|
|
IEnumerable<RetreatingUnit>? retreats = null,
|
|
IEnumerable<KeyValuePair<Season, OrderHistory>>? orders = null)
|
|
=> new World(
|
|
previous: this,
|
|
seasons: seasons == null
|
|
? this.Seasons
|
|
: new(seasons.ToList()),
|
|
units: units == null
|
|
? this.Units
|
|
: new(units.ToList()),
|
|
retreatingUnits: retreats == null
|
|
? this.RetreatingUnits
|
|
: new(retreats.ToList()),
|
|
orderHistory: orders == null
|
|
? this.OrderHistory
|
|
: new(orders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)));
|
|
|
|
/// <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
|
|
/// abbreviation should be used. Unit specs always describe units in the root season.
|
|
/// </summary>
|
|
public World AddUnits(params string[] unitSpecs)
|
|
{
|
|
IEnumerable<Unit> units = unitSpecs.Select(spec =>
|
|
{
|
|
string[] splits = spec.Split(' ', 4);
|
|
Power power = Map.GetPower(splits[0]);
|
|
UnitType type = splits[1] switch
|
|
{
|
|
"A" => UnitType.Army,
|
|
"F" => UnitType.Fleet,
|
|
_ => throw new ApplicationException($"Unknown unit type {splits[1]}")
|
|
};
|
|
Location location = type == UnitType.Army
|
|
? Map.GetLand(splits[2])
|
|
: splits.Length == 3
|
|
? Map.GetWater(splits[2])
|
|
: Map.GetWater(splits[2], splits[3]);
|
|
Unit unit = Unit.Build(location, this.RootSeason, power, type);
|
|
return unit;
|
|
});
|
|
return this.Update(units: units);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new world with standard Diplomacy initial unit placements.
|
|
/// </summary>
|
|
public World AddStandardUnits()
|
|
{
|
|
return this.AddUnits(
|
|
"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"
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A standard Diplomacy game setup.
|
|
/// </summary>
|
|
public static World Standard => World
|
|
.WithStandardMap()
|
|
.AddStandardUnits();
|
|
|
|
/// <summary>
|
|
/// Get a season by coordinate. Throws if the season is not found.
|
|
/// </summary>
|
|
public Season GetSeason(int turn, int timeline)
|
|
{
|
|
Season? foundSeason = this.Seasons.SingleOrDefault(
|
|
s => s!.Turn == turn && s.Timeline == timeline,
|
|
null);
|
|
if (foundSeason == null) throw new KeyNotFoundException(
|
|
$"Season {turn}:{timeline} not found");
|
|
return foundSeason;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a unit in a province. Throws if there are duplicate units.
|
|
/// </summary>
|
|
public Unit GetUnitAt(string provinceName, (int turn, int timeline)? seasonCoord = null)
|
|
{
|
|
Province province = Map.GetProvince(provinceName);
|
|
seasonCoord ??= (this.RootSeason.Turn, this.RootSeason.Timeline);
|
|
Season season = GetSeason(seasonCoord.Value.turn, seasonCoord.Value.timeline);
|
|
Unit? foundUnit = this.Units.SingleOrDefault(
|
|
u => u!.Province == province && u.Season == season,
|
|
null);
|
|
if (foundUnit == null) throw new KeyNotFoundException(
|
|
$"Unit at {province} at {season} not found");
|
|
return foundUnit;
|
|
}
|
|
} |