Skip to content

Commit

Permalink
refactor(autoforager): Rework location data
Browse files Browse the repository at this point in the history
Using Location data for forage spawns instead of artifact spot chances
  • Loading branch information
jag3dagster committed May 20, 2024
1 parent 143f555 commit 77ebd88
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/AutoForager/AutoForager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Name>Auto Forager</Name>
<Authors>Jag3Dagster</Authors>
<Description>Automatically forage items simply by moving near them.</Description>
<Version>3.5.2</Version>
<Version>3.6.0</Version>
<UpdateKeys>Nexus:7736</UpdateKeys>

<MinimumApiVersion>auto</MinimumApiVersion>
Expand Down
46 changes: 15 additions & 31 deletions src/AutoForager/Classes/ForageableItem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using HedgeTech.Common.Extensions;
using StardewModdingAPI;
using StardewValley;
using StardewValley.GameData.FruitTrees;
Expand All @@ -11,6 +10,8 @@
using StardewValley.ItemTypeDefinitions;
using StardewValley.TokenizableStrings;
using AutoForager.Extensions;
using HedgeTech.Common.Extensions;
using HedgeTech.Common.Utilities;

using Constants = AutoForager.Helpers.Constants;

Expand Down Expand Up @@ -216,49 +217,32 @@ public static (IEnumerable<ForageableItem>, IEnumerable<ForageableItem>) ParseOb
return (forageItems, bushItems);
}

public static IEnumerable<ForageableItem> ParseLocationData(IDictionary<string, ObjectData> oData, IDictionary<string, LocationData> lData, IDictionary<string, bool>? configValues = null)
public static IEnumerable<ForageableItem> ParseLocationData(IDictionary<string, LocationData> data, IDictionary<string, bool>? configValues = null, IMonitor? monitor = null)
{
var forageItems = new List<ForageableItem>();

foreach (var kvp in lData)
foreach (var location in data.Values)
{
var artifactSpots = kvp.Value.ArtifactSpots;
if (artifactSpots is null || artifactSpots.Count <= 0) continue;
var forage = location.Forage;

foreach (var artifact in artifactSpots)
{
List<string> itemIds;
if (forage.IsNullOrEmpty()) continue;

if (artifact.RandomItemId is not null)
{
itemIds = artifact.RandomItemId;
}
else if (artifact.ItemId is not null)
{
itemIds = new() { artifact.ItemId };
}
else
{
continue;
}
foreach (var forageObj in forage)
{
var qualifiedItemId = ItemUtilities.GetItemIdFromName(forageObj.ItemId);
var itemData = ItemRegistry.GetData(qualifiedItemId);
var internalName = itemData?.InternalName;

foreach (var itemId in itemIds)
if (itemData is not null && internalName is not null)
{
var artifactId = itemId.Substring(itemId.IndexOf(')') + 1);
if (!oData.ContainsKey(artifactId)) continue;

var objData = oData[artifactId];
if (objData is null || objData.CustomFields is null || !objData.CustomFields.ContainsKey(Constants.CustomFieldForageableKey)) continue;

var enabled = true;
var itemData = ItemRegistry.GetData(itemId);

if (configValues is not null && configValues.TryGetValue(itemData.InternalName, out var configEnabled))
if (configValues is not null && configValues.TryGetValue(internalName, out var configEnabled))
{
enabled = configEnabled;
}

forageItems.AddDistinct(new ForageableItem(itemData, objData.CustomFields, enabled));
forageItems.AddDistinct(new ForageableItem(itemData, new() { { Constants.CustomFieldCategoryKey, "Locations" } }, enabled));
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/AutoForager/Classes/ForageableItemTracker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;

namespace AutoForager.Classes
Expand All @@ -12,9 +12,6 @@ internal sealed class ForageableItemTracker

// ---------- Trackers ---------- //

private readonly List<ForageableItem> _artifactForageables;
public List<ForageableItem> ArtifactForageables => _artifactForageables;

private readonly List<ForageableItem> _bushForageables;
public List<ForageableItem> BushForageables => _bushForageables;

Expand All @@ -34,7 +31,6 @@ internal sealed class ForageableItemTracker

private ForageableItemTracker()
{
_artifactForageables = new();
_bushForageables = new();
_caveForageables = new();
_fruitTreeForageables = new();
Expand Down
23 changes: 22 additions & 1 deletion src/AutoForager/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using HedgeTech.Common.Extensions;
using StardewModdingAPI;
Expand All @@ -17,6 +17,27 @@ public static void AddDistinct(this List<ForageableItem> items, ForageableItem n
items.Add(newItem);
}

public static void AddOrMergeCustomFieldsRange(this List<ForageableItem> items, IEnumerable<ForageableItem> newItems)
{
foreach (var newItem in newItems)
{
if (items.TryGetItem(newItem.QualifiedItemId, out var oldItem))
{
if (newItem.CustomFields.Count > 0)
{
foreach (var kvp in newItem.CustomFields)
{
oldItem?.CustomFields.TryAdd(kvp.Key, kvp.Value);
}
}
}
else
{
items.AddDistinct(newItem);
}
}
}

public static IOrderedEnumerable<IGrouping<string, ForageableItem>> GroupByCategory(this List<ForageableItem> list, IModHelper helper, string? categoryKey = null, IComparer<string>? comparer = null)
{
categoryKey ??= Constants.CustomFieldCategoryKey;
Expand Down
4 changes: 0 additions & 4 deletions src/AutoForager/ModConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,6 @@ public void RegisterModConfigMenu(IModHelper helper, IManifest manifest)
{
item.IsEnabled = val;
ForageToggles[Constants.ForagingToggleKey].AddOrUpdate(item.InternalName, val);
if (_forageableTracker.ArtifactForageables.TryGetItem(item.QualifiedItemId, out var artifact) && artifact is not null)
{
artifact.IsEnabled = val;
}
UpdateEnabled();
});
}
Expand Down
27 changes: 16 additions & 11 deletions src/AutoForager/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,32 @@ private void ParseAssets<T>(Dictionary<string, T> data)
{
if (data is Dictionary<string, FruitTreeData> fruitTreeData)
{
Monitor.Log("Parsing Fruit Tree Data", LogLevel.Debug);

_forageableTracker.FruitTreeForageables.Clear();
_forageableTracker.FruitTreeForageables.AddRange(ForageableItem.ParseFruitTreeData(fruitTreeData, _config?.ForageToggles[Constants.FruitTreeToggleKey], Monitor));
_forageableTracker.FruitTreeForageables.SortByDisplayName();
Monitor.Log("Parsing Fruit Tree Data", LogLevel.Debug);
}
else if (data is Dictionary<string, LocationData> locationData)
{
if (ObjectCache is null || ObjectCache.Count == 0)
{
ObjectCache = Game1.content.Load<Dictionary<string, ObjectData>>(Constants.ObjectsAssetName);
Monitor.Log("Sub-Location: Grabbing Object Data", LogLevel.Debug);

ObjectCache = Game1.content.Load<Dictionary<string, ObjectData>>(Constants.ObjectsAssetName);
}

_forageableTracker.ArtifactForageables.Clear();
_forageableTracker.ArtifactForageables.AddRange(ForageableItem.ParseLocationData(ObjectCache, locationData, _config?.ForageToggles[Constants.ForagingToggleKey]));
_forageableTracker.ArtifactForageables.SortByDisplayName();
Monitor.Log("Parsing Location Data", LogLevel.Debug);

_forageableTracker.ObjectForageables.AddOrMergeCustomFieldsRange(ForageableItem.ParseLocationData(locationData, _config?.ForageToggles[Constants.ForagingToggleKey], Monitor));
_forageableTracker.ObjectForageables.SortByDisplayName();
}
else if (data is Dictionary<string, ObjectData> objectData)
{
var parsedObjectForageableItems = ForageableItem.ParseObjectData(objectData, _config, Monitor);

Monitor.Log("Parsing Object Data", LogLevel.Debug);

_forageableTracker.ObjectForageables.Clear();
_forageableTracker.ObjectForageables.AddRange(parsedObjectForageableItems.Item1);
_forageableTracker.ObjectForageables.SortByDisplayName();
Expand All @@ -139,27 +143,26 @@ private void ParseAssets<T>(Dictionary<string, T> data)
_forageableTracker.BushForageables.AddRange(parsedObjectForageableItems.Item2);
_forageableTracker.BushForageables.SortByDisplayName();

Monitor.Log("Parsing Object Data", LogLevel.Debug);

if (LocationCache is not null && LocationCache.Count > 0)
{
_forageableTracker.ArtifactForageables.Clear();
_forageableTracker.ArtifactForageables.AddRange(ForageableItem.ParseLocationData(objectData, LocationCache, _config?.ForageToggles[Constants.ForagingToggleKey]));
_forageableTracker.ArtifactForageables.SortByDisplayName();
Monitor.Log("Sub-Object: Parsing Location Data", LogLevel.Debug);
_forageableTracker.ObjectForageables.AddOrMergeCustomFieldsRange(ForageableItem.ParseLocationData(LocationCache, _config?.ForageToggles[Constants.ForagingToggleKey], Monitor));
_forageableTracker.ObjectForageables.SortByDisplayName();
}
}
else if (data is Dictionary<string, WildTreeData> wildTreeData)
{
Monitor.Log("Parsing Wild Tree Data", LogLevel.Debug);

_forageableTracker.WildTreeForageables.Clear();
_forageableTracker.WildTreeForageables.AddRange(ForageableItem.ParseWildTreeData(wildTreeData, _config?.ForageToggles[Constants.WildTreeToggleKey], Monitor));
_forageableTracker.WildTreeForageables.SortByDisplayName();
Monitor.Log("Parsing Wild Tree Data", LogLevel.Debug);
}

if (_config is not null && _gameStarted)
{
Monitor.Log("Reregistering Generic Mod Config Menu", LogLevel.Debug);

_config.RegisterModConfigMenu(Helper, ModManifest);
}
}
Expand Down Expand Up @@ -633,6 +636,8 @@ private void OnUpdateTicked(object? sender, UpdateTickedEventArgs e)
// Forageable Item
if (_forageableTracker.ObjectForageables.TryGetItem(obj.QualifiedItemId, out var objItem) && (objItem?.IsEnabled ?? false))
{
if (!obj.IsSpawnedObject) continue;

ForageItem(obj, vec, Utility.CreateDaySaveRandom(vec.X, vec.Y * 777f), 7, true);

Game1.player.currentLocation.removeObject(vec, false);
Expand Down

0 comments on commit 77ebd88

Please sign in to comment.