Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BeaverBuddies/Colonies/ColonyConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static void Configure(IContainerDefinition containerDefinition)
// A hosted shared save made separate colonies at Start, from its waiting room (1.4.0-rc4).
containerDefinition.Bind<SaveConversion>().AsSingleton();
containerDefinition.Bind<ColonyViewService>().AsSingleton();
// The batch control window's whole-map history graphs, drawn for this player's colony (1.4.0-rc31).
containerDefinition.Bind<ColonyHistoryView>().AsSingleton();
containerDefinition.Bind<ColonyJournal>().AsSingleton();
containerDefinition.Bind<ColonyCuttingViewRefresher>().AsSingleton();
containerDefinition.Bind<ColonyWellbeingRecords>().AsSingleton();
Expand Down
263 changes: 263 additions & 0 deletions BeaverBuddies/Colonies/ColonyHistoryView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
using HarmonyLib;
using System;
using System.Collections.Generic;
using Timberborn.Common;
using Timberborn.GameDistricts;
using Timberborn.GoodsSampling;
using Timberborn.GoodStatisticsBatchControl;
using Timberborn.GoodStatisticsUI;
using Timberborn.PopulationStatisticsBatchControl;
using Timberborn.PopulationStatisticsSampling;
using Timberborn.SingletonSystem;

namespace BeaverBuddies.Colonies
{
/// <summary>
/// The batch control window's history graphs with no district chosen (goods statistics, F9; population statistics,
/// F10): the game draws the whole map's history there, both colonies added together. In a separate-colonies co-op
/// game (<see cref="ColonyViewService.Active"/>) they draw this player's colony instead: their own districts' saved
/// histories added together day by day, on the whole map's days (a district counts from the day it was first
/// sampled; the days before it, it simply isn't there).
///
/// Display only. The game's registries (whole map and districts) are only read, never changed: the colony's
/// histories are this computer's own lists, handed to the graphs in place of the whole map's, never saved, and rebuilt
/// only when the graphs ask for them after the game has taken a new day's sample (or this player's districts change).
/// Nothing simulated reads them.
/// </summary>
public class ColonyHistoryView : RegisteredSingleton, ILoadableSingleton
{
private readonly GlobalPopulationSamplesRegistry _globalPopulationSamplesRegistry;
private readonly GlobalGoodSamplingRegistry _globalGoodSamplingRegistry;
private readonly ColonyViewService _colonyViewService;

public ColonyHistoryView(GlobalPopulationSamplesRegistry globalPopulationSamplesRegistry,
GlobalGoodSamplingRegistry globalGoodSamplingRegistry, ColonyViewService colonyViewService)
{
_globalPopulationSamplesRegistry = globalPopulationSamplesRegistry;
_globalGoodSamplingRegistry = globalGoodSamplingRegistry;
_colonyViewService = colonyViewService;
}

public void Load() { }

public static ColonyHistoryView Instance => SingletonManager.GetSingleton<ColonyHistoryView>();

/// <summary>What a history was last built from: rebuilt only when one of these changes.</summary>
private class Built
{
private int samples = -1;
private bool active;
private readonly List<DistrictCenter> districts = new List<DistrictCenter>();

public bool Changed(int samples, bool active, List<DistrictCenter> own)
{
bool same = samples == this.samples && active == this.active && (!active || Same(own));
if (same) return false;
this.samples = samples;
this.active = active;
districts.Clear();
if (active) districts.AddRange(own);
return true;
}

private bool Same(List<DistrictCenter> own)
{
if (own.Count != districts.Count) return false;
for (int i = 0; i < own.Count; i++)
if (own[i] != districts[i]) return false;
return true;
}
}

// ---- population (F10) ----

private readonly List<PopulationSample> populationSamples = new List<PopulationSample>();
private PopulationSampleHistory populationHistory;
private readonly Built populationBuilt = new Built();
private readonly List<PopulationSampleHistory> ownPopulation = new List<PopulationSampleHistory>();

/// <summary>The whole map's history is swapped for the colony's; any other (a district's) is left as it is.</summary>
public void SwapPopulation(ref PopulationSampleHistory history)
{
PopulationSampleHistory global = _globalPopulationSamplesRegistry.PopulationSampleHistory;
if (global == null || !ReferenceEquals(history, global)) return;
if (populationHistory == null) populationHistory = PopulationSampleHistory.CreateFromSave(populationSamples);
RefreshPopulation();
history = populationHistory;
}

/// <summary>Before a graph reads its history: the colony's is brought up to date (other histories are the game's).</summary>
public void BeforePopulationGraphReads(PopulationSampleHistory history)
{
if (populationHistory != null && ReferenceEquals(history, populationHistory)) RefreshPopulation();
}

private void RefreshPopulation()
{
ReadOnlyList<PopulationSample> days = _globalPopulationSamplesRegistry.PopulationSampleHistory.PopulationSamples;
bool active = ColonyViewService.Active;
List<DistrictCenter> own = active ? _colonyViewService.OwnDistricts() : null;
if (!populationBuilt.Changed(days.Count, active, own)) return;
populationSamples.Clear();
if (!active)
{
// No longer showing one colony (the session ended with the window open): the whole map's, as the game.
for (int i = 0; i < days.Count; i++) populationSamples.Add(days[i]);
return;
}
ownPopulation.Clear();
foreach (DistrictCenter districtCenter in own)
{
PopulationSampleHistory history = districtCenter.GetComponent<DistrictPopulationSamplesRegistry>()?.PopulationSampleHistory;
if (history != null) ownPopulation.Add(history);
}
for (int i = 0; i < days.Count; i++)
{
// A district is sampled with the whole map on every day it stands, so its latest sample is the map's
// latest day: its days line up from the end.
int back = days.Count - 1 - i;
PopulationSample sum = new PopulationSample(days[i].Day, days[i].Cycle, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
long wellbeing = 0;
int beavers = 0;
foreach (PopulationSampleHistory history in ownPopulation)
{
ReadOnlyList<PopulationSample> samples = history.PopulationSamples;
if (back >= samples.Count) continue;
PopulationSample sample = samples[samples.Count - 1 - back];
sum += sample;
// Each district's average weighted by its beavers, as the top bar's (ColonyViewService.ColonyWellbeing).
int count = sample.Adults + sample.Children;
wellbeing += (long)sample.Wellbeing * count;
beavers += count;
}
sum.SetWellbeing(beavers == 0 ? 0 : (int)Math.Round((double)wellbeing / beavers));
populationSamples.Add(sum);
}
}

// ---- goods (F9) ----

private GoodSamplingRegistry goodsRegistry;
private readonly Dictionary<string, List<GoodSample>> goodsSamples = new Dictionary<string, List<GoodSample>>();
private readonly HashSet<GoodSampleHistory> goodsHistories = new HashSet<GoodSampleHistory>();
private readonly Built goodsBuilt = new Built();
private readonly List<GoodSamplingRegistry> ownGoods = new List<GoodSamplingRegistry>();

/// <summary>The whole map's goods registry is swapped for the colony's; any other (a district's) is left as it is.</summary>
public void SwapGoods(ref GoodSamplingRegistry registry)
{
GoodSamplingRegistry global = _globalGoodSamplingRegistry.GoodSamplingRegistry;
if (global == null || !ReferenceEquals(registry, global)) return;
if (goodsRegistry == null)
{
// One history per good the whole map has, each on a list of this view's own.
var histories = new List<GoodSampleHistory>();
foreach (GoodSampleHistory all in global.GoodSampleHistories)
{
var samples = new List<GoodSample>();
goodsSamples[all.GoodId] = samples;
GoodSampleHistory history = GoodSampleHistory.CreateFromSave(all.GoodId, samples);
goodsHistories.Add(history);
histories.Add(history);
}
goodsRegistry = GoodSamplingRegistry.CreateFromSave(histories, new List<string>());
}
RefreshGoods();
registry = goodsRegistry;
}

/// <summary>Before a chart reads its history: the colony's are brought up to date (other histories are the game's).</summary>
public void BeforeGoodChartReads(GoodSampleHistory history)
{
if (goodsRegistry != null && history != null && goodsHistories.Contains(history)) RefreshGoods();
}

private void RefreshGoods()
{
ReadOnlyList<GoodSampleHistory> all = _globalGoodSamplingRegistry.GoodSamplingRegistry.GoodSampleHistories;
// Every good gains a sample each day: the total changes with each new day.
int samples = 0;
foreach (GoodSampleHistory history in all) samples += history.GoodSamples.Count;
bool active = ColonyViewService.Active;
List<DistrictCenter> own = active ? _colonyViewService.OwnDistricts() : null;
if (!goodsBuilt.Changed(samples, active, own)) return;
ownGoods.Clear();
if (active)
{
foreach (DistrictCenter districtCenter in own)
{
GoodSamplingRegistry registry = districtCenter.GetComponent<DistrictGoodSamplingRegistry>()?.GoodSamplingRegistry;
if (registry != null) ownGoods.Add(registry);
}
}
foreach (GoodSampleHistory global in all)
{
if (!goodsSamples.TryGetValue(global.GoodId, out List<GoodSample> summed)) continue;
summed.Clear();
ReadOnlyList<GoodSample> days = global.GoodSamples;
if (!active)
{
for (int i = 0; i < days.Count; i++) summed.Add(days[i]);
continue;
}
for (int i = 0; i < days.Count; i++)
{
// Lined up from the latest day, as the population's (and the top bar's goods chart).
int back = days.Count - 1 - i;
GoodSample sum = new GoodSample(days[i].Cycle, days[i].Day, 0, 0, 0, 0);
foreach (GoodSamplingRegistry registry in ownGoods)
{
if (!registry._goodSampleHistoryMap.TryGetValue(global.GoodId, out GoodSampleHistory history)) continue;
ReadOnlyList<GoodSample> district = history.GoodSamples;
if (back < district.Count) sum += district[district.Count - 1 - back];
}
summed.Add(sum);
}
}
}
}

// The population statistics tab (F10) builds its graphs for the whole map from the game's saved global history.
// Hand them the colony's instead; a district's graphs are untouched.
[HarmonyPatch(typeof(PopulationStatisticsGraphFactory), nameof(PopulationStatisticsGraphFactory.Create), new[] { typeof(PopulationSampleHistory) })]
static class ColonyHistoryPopulationGraphsPatcher
{
static void Prefix(ref PopulationSampleHistory populationSampleHistory)
{
if (!ColonyViewService.Active) return;
ColonyHistoryView.Instance?.SwapPopulation(ref populationSampleHistory);
}
}

// A graph redraws after each day's sample: the colony's history catches up first.
[HarmonyPatch(typeof(PopulationStatisticsGraph), nameof(PopulationStatisticsGraph.UpdateItem))]
static class ColonyHistoryPopulationRefreshPatcher
{
static void Prefix(PopulationStatisticsGraph __instance)
{
ColonyHistoryView.Instance?.BeforePopulationGraphReads(__instance._populationSampleHistory);
}
}

// The goods statistics tab (F9) builds its charts for the whole map from the game's saved global registry. Hand them
// the colony's instead; a district's charts are untouched.
[HarmonyPatch(typeof(GoodStatisticsGroupFactory), nameof(GoodStatisticsGroupFactory.Create))]
static class ColonyHistoryGoodsGroupsPatcher
{
static void Prefix(ref GoodSamplingRegistry goodSamplingRegistry)
{
if (!ColonyViewService.Active) return;
ColonyHistoryView.Instance?.SwapGoods(ref goodSamplingRegistry);
}
}

// A chart redraws after each day's sample: the colony's histories catch up first (only a chart of theirs asks).
[HarmonyPatch(typeof(GoodSampleHistoryElement), nameof(GoodSampleHistoryElement.Update))]
static class ColonyHistoryGoodsRefreshPatcher
{
static void Prefix(GoodSampleHistoryElement __instance)
{
ColonyHistoryView.Instance?.BeforeGoodChartReads(__instance._goodSampleHistory);
}
}
}
8 changes: 4 additions & 4 deletions BeaverBuddies/Colonies/ColonyView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace BeaverBuddies.Colonies
/// <summary>
/// What each player sees: their own colony, never both added together. Timberborn's interface shows the whole
/// settlement whenever no district is selected (the top bar's goods and their stock history, population, wellbeing,
/// the wellbeing window, the batch control window's lists and district choices, the alert panel, the notification
/// journal); in a separate-colonies session "the whole
/// the wellbeing window, the batch control window's lists, history graphs (ColonyHistoryView) and district choices,
/// the alert panel, the notification journal); in a separate-colonies session "the whole
/// settlement" becomes "your colony". Display only: everything here reads the game and changes nothing that is
/// simulated, so each computer may show something different without any risk to the shared game.
/// </summary>
Expand Down Expand Up @@ -447,8 +447,8 @@ static bool Prefix(BatchControlRowGroup __instance, DistrictCenter selectedDistr
}
}

// The window opens on the district the interface shows; with none, it would open on the whole settlement, whose
// graphs (goods, population) add both colonies together. Open on this player's biggest district instead.
// The window opens on the district the interface shows; with none, it would open on the whole settlement (whose
// lists and graphs show this player's colony, ColonyHistoryView). Open on this player's biggest district instead.
[HarmonyPatch(typeof(BatchControlBoxDistrictController), nameof(BatchControlBoxDistrictController.Show))]
static class ColonyViewBatchControlShowPatcher
{
Expand Down
35 changes: 34 additions & 1 deletion BeaverBuddies/Colonies/ExchangeTerms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,38 @@ public static class ExchangeTerms
public const int MaxRounds = 99;
/// <summary>The most a side can say it keeps back (see <see cref="CanSpare"/>).</summary>
public const int MaxKeep = 9999;
/// <summary>The most one side gives over a whole exchange: a full round every round.</summary>
public const int MaxWhole = MaxAmount * MaxRounds;

/// <summary>
/// The rounds a whole exchange takes: one for each <see cref="MaxAmount"/> of its larger side (300 for 300 is 3,
/// 301 for 300 is 4). The players never choose them; they follow from what a Trading Post carries.
/// </summary>
public static int RoundsFor(int wholeGive, int wholeGet) =>
Math.Max(1, (Math.Max(Math.Max(0, wholeGive), Math.Max(0, wholeGet)) + MaxAmount - 1) / MaxAmount);

/// <summary>
/// What a side gives in round <paramref name="index"/> (from 0) of <paramref name="rounds"/>: the whole spread as
/// evenly as it goes, the larger rounds first (101 over 2 rounds is 51, then 50; 1 over 5 is 1, then 0). The
/// rounds add up to exactly the whole, and none carries more than <see cref="MaxAmount"/> when
/// <paramref name="rounds"/> is <see cref="RoundsFor"/>.
/// </summary>
public static int ShareOf(int whole, int rounds, int index)
{
if (whole <= 0 || rounds <= 0 || index < 0 || index >= rounds) return 0;
return whole / rounds + (index < whole % rounds ? 1 : 0);
}

/// <summary>
/// Whole exchange terms: amounts from 0 to <see cref="MaxWhole"/> (a repeating exchange's are each round's, up to
/// <see cref="MaxAmount"/>), not both 0, each with its item, two different items.
/// </summary>
public static bool AreValidTerms(string giveGood, int giveAmount, string getGood, int getAmount, bool repeat)
{
int most = repeat ? MaxAmount : MaxWhole;
if (giveAmount > most || getAmount > most) return false;
return AreValid(giveGood, Math.Min(giveAmount, MaxAmount), getGood, Math.Min(getAmount, MaxAmount));
}

/// <summary>Science points as an exchange item: moved from pool to pool, with separate science.</summary>
public const string Science = "BeaverBuddies.Science";
Expand Down Expand Up @@ -207,7 +239,8 @@ public static bool TryDecodeTerms(string text, out string giveGood, out int give
giveGood = GoodOf(parts[0], giveAmount);
getGood = GoodOf(parts[2], getAmount);
repeat = parts[5] == "1";
return AreValid(giveGood, giveAmount, getGood, getAmount) && (repeat || AreValidRounds(rounds)) && IsValidKeep(keep);
return AreValidTerms(giveGood, giveAmount, getGood, getAmount, repeat)
&& (repeat || rounds == RoundsFor(giveAmount, getAmount)) && IsValidKeep(keep);
}

/// <summary>The good a side gives, or null for a side that gives nothing.</summary>
Expand Down
Loading
Loading