using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using StardewValley;
using StardewValley.Buildings;
using StardewValley.TerrainFeatures;
namespace StardewModdingAPI.Framework.StateTracking.Snapshots
{
/// A frozen snapshot of a tracked game location.
internal class LocationSnapshot
{
/*********
** Accessors
*********/
/// The tracked location.
public GameLocation Location { get; }
/// Tracks added or removed buildings.
public SnapshotListDiff Buildings { get; } = new SnapshotListDiff();
/// Tracks added or removed debris.
public SnapshotListDiff Debris { get; } = new SnapshotListDiff();
/// Tracks added or removed large terrain features.
public SnapshotListDiff LargeTerrainFeatures { get; } = new SnapshotListDiff();
/// Tracks added or removed NPCs.
public SnapshotListDiff Npcs { get; } = new SnapshotListDiff();
/// Tracks added or removed objects.
public SnapshotListDiff> Objects { get; } = new SnapshotListDiff>();
/// Tracks added or removed terrain features.
public SnapshotListDiff> TerrainFeatures { get; } = new SnapshotListDiff>();
/// Tracks changed chest inventories.
public IDictionary> ChestItems { get; } = new Dictionary>();
/*********
** Public methods
*********/
/// Construct an instance.
/// The tracked location.
public LocationSnapshot(GameLocation location)
{
this.Location = location;
}
/// Update the tracked values.
/// The watcher to snapshot.
public void Update(LocationTracker watcher)
{
// main lists
this.Buildings.Update(watcher.BuildingsWatcher);
this.Debris.Update(watcher.DebrisWatcher);
this.LargeTerrainFeatures.Update(watcher.LargeTerrainFeaturesWatcher);
this.Npcs.Update(watcher.NpcsWatcher);
this.Objects.Update(watcher.ObjectsWatcher);
this.TerrainFeatures.Update(watcher.TerrainFeaturesWatcher);
// chest inventories
foreach (Vector2 key in this.ChestItems.Keys.ToArray())
{
if (!watcher.ChestWatchers.ContainsKey(key))
this.ChestItems.Remove(key);
}
foreach (var pair in watcher.ChestWatchers)
{
if (!this.ChestItems.TryGetValue(pair.Key, out var diff))
this.ChestItems[pair.Key] = diff = new SnapshotListDiff- ();
diff.Update(pair.Value);
}
}
}
}