Add base for improved tile coordinate lookup functionality; typo fixes.

* Build tile dictionary at game launch to speed up tile lookups during gameplay.
* Add GameLaunched event to setup new dictionary after other mods loaded.
* Change `StaticTiles.getStaticTileInfoAtWithCategory` to use new dictionary for lookup.
* Various typo fixes and other code cleanup
master
Katie Durden 2023-03-08 14:09:13 -08:00
parent 4b2e31fadc
commit dd812851b4
5 changed files with 218 additions and 136 deletions

View File

@ -18,7 +18,7 @@ namespace stardew_access
return; return;
#region Read Tile #region Read Tile
helper.ConsoleCommands.Add("readtile", "Toggle read tile feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("readtile", "Toggle read tile feature.", (string command, string[] args) =>
{ {
MainClass.Config.ReadTile = !MainClass.Config.ReadTile; MainClass.Config.ReadTile = !MainClass.Config.ReadTile;
helper.WriteConfig(MainClass.Config); helper.WriteConfig(MainClass.Config);
@ -26,7 +26,7 @@ namespace stardew_access
MainClass.InfoLog("Read Tile is " + (MainClass.Config.ReadTile ? "on" : "off")); MainClass.InfoLog("Read Tile is " + (MainClass.Config.ReadTile ? "on" : "off"));
}); });
helper.ConsoleCommands.Add("flooring", "Toggle flooring in read tile.", (string commmand, string[] args) => helper.ConsoleCommands.Add("flooring", "Toggle flooring in read tile.", (string command, string[] args) =>
{ {
MainClass.Config.ReadFlooring = !MainClass.Config.ReadFlooring; MainClass.Config.ReadFlooring = !MainClass.Config.ReadFlooring;
helper.WriteConfig(MainClass.Config); helper.WriteConfig(MainClass.Config);
@ -34,7 +34,7 @@ namespace stardew_access
MainClass.InfoLog("Flooring is " + (MainClass.Config.ReadFlooring ? "on" : "off")); MainClass.InfoLog("Flooring is " + (MainClass.Config.ReadFlooring ? "on" : "off"));
}); });
helper.ConsoleCommands.Add("watered", "Toggle speaking watered or unwatered for crops.", (string commmand, string[] args) => helper.ConsoleCommands.Add("watered", "Toggle speaking watered or unwatered for crops.", (string command, string[] args) =>
{ {
MainClass.Config.WateredToggle = !MainClass.Config.WateredToggle; MainClass.Config.WateredToggle = !MainClass.Config.WateredToggle;
helper.WriteConfig(MainClass.Config); helper.WriteConfig(MainClass.Config);
@ -44,7 +44,7 @@ namespace stardew_access
#endregion #endregion
#region Radar Feature #region Radar Feature
helper.ConsoleCommands.Add("radar", "Toggle radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("radar", "Toggle radar feature.", (string command, string[] args) =>
{ {
MainClass.Config.Radar = !MainClass.Config.Radar; MainClass.Config.Radar = !MainClass.Config.Radar;
helper.WriteConfig(MainClass.Config); helper.WriteConfig(MainClass.Config);
@ -52,14 +52,14 @@ namespace stardew_access
MainClass.InfoLog("Radar " + (MainClass.Config.Radar ? "on" : "off")); MainClass.InfoLog("Radar " + (MainClass.Config.Radar ? "on" : "off"));
}); });
helper.ConsoleCommands.Add("rdebug", "Toggle debugging in radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rdebug", "Toggle debugging in radar feature.", (string command, string[] args) =>
{ {
MainClass.radarDebug = !MainClass.radarDebug; MainClass.radarDebug = !MainClass.radarDebug;
MainClass.InfoLog("Radar debugging " + (MainClass.radarDebug ? "on" : "off")); MainClass.InfoLog("Radar debugging " + (MainClass.radarDebug ? "on" : "off"));
}); });
helper.ConsoleCommands.Add("rstereo", "Toggle stereo sound in radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rstereo", "Toggle stereo sound in radar feature.", (string command, string[] args) =>
{ {
MainClass.Config.RadarStereoSound = !MainClass.Config.RadarStereoSound; MainClass.Config.RadarStereoSound = !MainClass.Config.RadarStereoSound;
helper.WriteConfig(MainClass.Config); helper.WriteConfig(MainClass.Config);
@ -67,14 +67,14 @@ namespace stardew_access
MainClass.InfoLog("Stereo sound is " + (MainClass.Config.RadarStereoSound ? "on" : "off")); MainClass.InfoLog("Stereo sound is " + (MainClass.Config.RadarStereoSound ? "on" : "off"));
}); });
helper.ConsoleCommands.Add("rfocus", "Toggle focus mode in radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rfocus", "Toggle focus mode in radar feature.", (string command, string[] args) =>
{ {
bool focus = MainClass.RadarFeature.ToggleFocus(); bool focus = MainClass.RadarFeature.ToggleFocus();
MainClass.InfoLog("Focus mode is " + (focus ? "on" : "off")); MainClass.InfoLog("Focus mode is " + (focus ? "on" : "off"));
}); });
helper.ConsoleCommands.Add("rdelay", "Set the delay of radar feature in milliseconds.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rdelay", "Set the delay of radar feature in milliseconds.", (string command, string[] args) =>
{ {
string? delayInString = null; string? delayInString = null;
@ -107,7 +107,7 @@ namespace stardew_access
}); });
helper.ConsoleCommands.Add("rrange", "Set the range of radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rrange", "Set the range of radar feature.", (string command, string[] args) =>
{ {
string? rangeInString = null; string? rangeInString = null;
@ -142,7 +142,7 @@ namespace stardew_access
#region Exclusions #region Exclusions
helper.ConsoleCommands.Add("readd", "Add an object key to the exclusions list of radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("readd", "Add an object key to the exclusions list of radar feature.", (string command, string[] args) =>
{ {
string? keyToAdd = null; string? keyToAdd = null;
@ -167,7 +167,7 @@ namespace stardew_access
} }
}); });
helper.ConsoleCommands.Add("reremove", "Remove an object key from the exclusions list of radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("reremove", "Remove an object key from the exclusions list of radar feature.", (string command, string[] args) =>
{ {
string? keyToAdd = null; string? keyToAdd = null;
@ -192,7 +192,7 @@ namespace stardew_access
} }
}); });
helper.ConsoleCommands.Add("relist", "List all the exclusions in the radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("relist", "List all the exclusions in the radar feature.", (string command, string[] args) =>
{ {
if (MainClass.RadarFeature.exclusions.Count > 0) if (MainClass.RadarFeature.exclusions.Count > 0)
{ {
@ -209,20 +209,20 @@ namespace stardew_access
} }
}); });
helper.ConsoleCommands.Add("reclear", "Clear the focus exclusions in the radar featrure.", (string commmand, string[] args) => helper.ConsoleCommands.Add("reclear", "Clear the focus exclusions in the radar featrure.", (string command, string[] args) =>
{ {
MainClass.RadarFeature.exclusions.Clear(); MainClass.RadarFeature.exclusions.Clear();
MainClass.InfoLog($"Cleared the focus list in the exclusions feature."); MainClass.InfoLog($"Cleared the focus list in the exclusions feature.");
}); });
helper.ConsoleCommands.Add("recount", "Number of exclusions in the radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("recount", "Number of exclusions in the radar feature.", (string command, string[] args) =>
{ {
MainClass.InfoLog($"There are {MainClass.RadarFeature.exclusions.Count} exclusiond in the radar feature."); MainClass.InfoLog($"There are {MainClass.RadarFeature.exclusions.Count} exclusiond in the radar feature.");
}); });
#endregion #endregion
#region Focus #region Focus
helper.ConsoleCommands.Add("rfadd", "Add an object key to the focus list of radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rfadd", "Add an object key to the focus list of radar feature.", (string command, string[] args) =>
{ {
string? keyToAdd = null; string? keyToAdd = null;
@ -247,7 +247,7 @@ namespace stardew_access
} }
}); });
helper.ConsoleCommands.Add("rfremove", "Remove an object key from the focus list of radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rfremove", "Remove an object key from the focus list of radar feature.", (string command, string[] args) =>
{ {
string? keyToAdd = null; string? keyToAdd = null;
@ -272,7 +272,7 @@ namespace stardew_access
} }
}); });
helper.ConsoleCommands.Add("rflist", "List all the exclusions in the radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rflist", "List all the exclusions in the radar feature.", (string command, string[] args) =>
{ {
if (MainClass.RadarFeature.focus.Count > 0) if (MainClass.RadarFeature.focus.Count > 0)
{ {
@ -289,13 +289,13 @@ namespace stardew_access
} }
}); });
helper.ConsoleCommands.Add("rfclear", "Clear the focus list in the radar featrure.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rfclear", "Clear the focus list in the radar featrure.", (string command, string[] args) =>
{ {
MainClass.RadarFeature.focus.Clear(); MainClass.RadarFeature.focus.Clear();
MainClass.InfoLog($"Cleared the focus list in the radar feature."); MainClass.InfoLog($"Cleared the focus list in the radar feature.");
}); });
helper.ConsoleCommands.Add("rfcount", "Number of list in the radar feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("rfcount", "Number of list in the radar feature.", (string command, string[] args) =>
{ {
MainClass.InfoLog($"There are {MainClass.RadarFeature.focus.Count} objects in the focus list in the radar feature."); MainClass.InfoLog($"There are {MainClass.RadarFeature.focus.Count} objects in the focus list in the radar feature.");
}); });
@ -304,7 +304,7 @@ namespace stardew_access
#endregion #endregion
#region Tile marking #region Tile marking
helper.ConsoleCommands.Add("mark", "Marks the player's position for use in building construction in Carpenter Menu.", (string commmand, string[] args) => helper.ConsoleCommands.Add("mark", "Marks the player's position for use in building construction in Carpenter Menu.", (string command, string[] args) =>
{ {
if (Game1.currentLocation is not Farm) if (Game1.currentLocation is not Farm)
{ {
@ -332,7 +332,7 @@ namespace stardew_access
MainClass.InfoLog($"Location {(int)Game1.player.getTileX()}x {(int)Game1.player.getTileY()}y added at {index} index."); MainClass.InfoLog($"Location {(int)Game1.player.getTileX()}x {(int)Game1.player.getTileY()}y added at {index} index.");
}); });
helper.ConsoleCommands.Add("marklist", "List all marked positions.", (string commmand, string[] args) => helper.ConsoleCommands.Add("marklist", "List all marked positions.", (string command, string[] args) =>
{ {
string toPrint = ""; string toPrint = "";
for (int i = 0; i < BuildingOperations.marked.Length; i++) for (int i = 0; i < BuildingOperations.marked.Length; i++)
@ -349,12 +349,12 @@ namespace stardew_access
MainClass.InfoLog($"Marked positions:{toPrint}\nOpen command menu and use pageup and pagedown to check the list"); MainClass.InfoLog($"Marked positions:{toPrint}\nOpen command menu and use pageup and pagedown to check the list");
}); });
helper.ConsoleCommands.Add("buildlist", "List all buildings for selection for upgrading/demolishing/painting", (string commmand, string[] args) => helper.ConsoleCommands.Add("buildlist", "List all buildings for selection for upgrading/demolishing/painting", (string command, string[] args) =>
{ {
onBuildListCalled(); onBuildListCalled();
}); });
helper.ConsoleCommands.Add("buildsel", "Select the building index which you want to upgrade/demolish/paint", (string commmand, string[] args) => helper.ConsoleCommands.Add("buildsel", "Select the building index which you want to upgrade/demolish/paint", (string command, string[] args) =>
{ {
if ((Game1.activeClickableMenu is not CarpenterMenu && Game1.activeClickableMenu is not PurchaseAnimalsMenu && Game1.activeClickableMenu is not AnimalQueryMenu) || !CarpenterMenuPatch.isOnFarm) if ((Game1.activeClickableMenu is not CarpenterMenu && Game1.activeClickableMenu is not PurchaseAnimalsMenu && Game1.activeClickableMenu is not AnimalQueryMenu) || !CarpenterMenuPatch.isOnFarm)
{ {
@ -451,28 +451,28 @@ namespace stardew_access
#endregion #endregion
#region Other #region Other
helper.ConsoleCommands.Add("refsr", "Refresh screen reader", (string commmand, string[] args) => helper.ConsoleCommands.Add("refsr", "Refresh screen reader", (string command, string[] args) =>
{ {
MainClass.ScreenReader.InitializeScreenReader(); MainClass.ScreenReader.InitializeScreenReader();
MainClass.InfoLog("Screen Reader refreshed!"); MainClass.InfoLog("Screen Reader refreshed!");
}); });
helper.ConsoleCommands.Add("refmc", "Refresh mod config", (string commmand, string[] args) => helper.ConsoleCommands.Add("refmc", "Refresh mod config", (string command, string[] args) =>
{ {
MainClass.Config = helper.ReadConfig<ModConfig>(); MainClass.Config = helper.ReadConfig<ModConfig>();
MainClass.InfoLog("Mod Config refreshed!"); MainClass.InfoLog("Mod Config refreshed!");
}); });
helper.ConsoleCommands.Add("refst", "Refresh static tiles", (string commmand, string[] args) => helper.ConsoleCommands.Add("refst", "Refresh static tiles", (string command, string[] args) =>
{ {
MainClass.STiles = new Features.StaticTiles(); MainClass.STiles = new Features.StaticTiles();
MainClass.InfoLog("Static tiles refreshed!"); MainClass.InfoLog("Static tiles refreshed!");
}); });
helper.ConsoleCommands.Add("hnspercent", "Toggle between speaking in percentage or full health and stamina.", (string commmand, string[] args) => helper.ConsoleCommands.Add("hnspercent", "Toggle between speaking in percentage or full health and stamina.", (string command, string[] args) =>
{ {
MainClass.Config.HealthNStaminaInPercentage = !MainClass.Config.HealthNStaminaInPercentage; MainClass.Config.HealthNStaminaInPercentage = !MainClass.Config.HealthNStaminaInPercentage;
helper.WriteConfig(MainClass.Config); helper.WriteConfig(MainClass.Config);
@ -480,7 +480,7 @@ namespace stardew_access
MainClass.InfoLog("Speaking in percentage is " + (MainClass.Config.HealthNStaminaInPercentage ? "on" : "off")); MainClass.InfoLog("Speaking in percentage is " + (MainClass.Config.HealthNStaminaInPercentage ? "on" : "off"));
}); });
helper.ConsoleCommands.Add("snapmouse", "Toggle snap mouse feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("snapmouse", "Toggle snap mouse feature.", (string command, string[] args) =>
{ {
MainClass.Config.SnapMouse = !MainClass.Config.SnapMouse; MainClass.Config.SnapMouse = !MainClass.Config.SnapMouse;
helper.WriteConfig(MainClass.Config); helper.WriteConfig(MainClass.Config);
@ -488,7 +488,7 @@ namespace stardew_access
MainClass.InfoLog("Snap Mouse is " + (MainClass.Config.SnapMouse ? "on" : "off")); MainClass.InfoLog("Snap Mouse is " + (MainClass.Config.SnapMouse ? "on" : "off"));
}); });
helper.ConsoleCommands.Add("warning", "Toggle warnings feature.", (string commmand, string[] args) => helper.ConsoleCommands.Add("warning", "Toggle warnings feature.", (string command, string[] args) =>
{ {
MainClass.Config.Warning = !MainClass.Config.Warning; MainClass.Config.Warning = !MainClass.Config.Warning;
helper.WriteConfig(MainClass.Config); helper.WriteConfig(MainClass.Config);
@ -496,7 +496,7 @@ namespace stardew_access
MainClass.InfoLog("Warnings is " + (MainClass.Config.Warning ? "on" : "off")); MainClass.InfoLog("Warnings is " + (MainClass.Config.Warning ? "on" : "off"));
}); });
helper.ConsoleCommands.Add("tts", "Toggles the screen reader/tts", (string commmand, string[] args) => helper.ConsoleCommands.Add("tts", "Toggles the screen reader/tts", (string command, string[] args) =>
{ {
MainClass.Config.TTS = !MainClass.Config.TTS; MainClass.Config.TTS = !MainClass.Config.TTS;
helper.WriteConfig(MainClass.Config); helper.WriteConfig(MainClass.Config);

View File

@ -1,14 +1,15 @@
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using StardewValley; using StardewValley;
using System.Linq;
namespace stardew_access.Features namespace stardew_access.Features
{ {
public class StaticTiles public class StaticTiles
{ {
private JObject? staticTilesData = null; private static JObject? staticTilesData = null;
private JObject? customTilesData = null; private static JObject? customTilesData = null;
HashSet<KeyValuePair<string, JToken?>>? staticTilesDataSet = null; private static Dictionary<string, Dictionary<(short x, short y), (string name, CATEGORY category)>?>? staticTilesDataDict = null;
HashSet<KeyValuePair<string, JToken?>>? customTilesDataSet = null; private static Dictionary<string, Dictionary<(short x, short y), (string name, CATEGORY category)>?>? customTilesDataDict = null;
public StaticTiles() public StaticTiles()
{ {
@ -17,14 +18,13 @@ namespace stardew_access.Features
try try
{ {
using (StreamReader file = new StreamReader(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "static-tiles.json"))) using (StreamReader file = new(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "static-tiles.json")))
{ {
string json = file.ReadToEnd(); string json = file.ReadToEnd();
staticTilesData = JObject.Parse(json); staticTilesData = JObject.Parse(json);
} }
if (staticTilesData is not null) if (staticTilesData is not null)
{ {
staticTilesDataSet = new HashSet<KeyValuePair<string, JToken>>(staticTilesData);
} }
MainClass.InfoLog($"Loaded static-tile.json"); MainClass.InfoLog($"Loaded static-tile.json");
@ -36,14 +36,13 @@ namespace stardew_access.Features
try try
{ {
using (StreamReader file = new StreamReader(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "custom-tiles.json"))) using (StreamReader file = new(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "custom-tiles.json")))
{ {
string json = file.ReadToEnd(); string json = file.ReadToEnd();
customTilesData = JObject.Parse(json); customTilesData = JObject.Parse(json);
} }
if (customTilesData is not null) if (customTilesData is not null)
{ {
customTilesDataSet = new HashSet<KeyValuePair<string, JToken>>(customTilesData);
} }
MainClass.InfoLog($"Loaded custom-tile.json"); MainClass.InfoLog($"Loaded custom-tile.json");
@ -52,11 +51,12 @@ namespace stardew_access.Features
{ {
MainClass.InfoLog($"custom-tiles.json file not found or an error occured while initializing custom-tiles.json\nThe path of the file should be:\n\t{Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "custom-tiles.json")}"); MainClass.InfoLog($"custom-tiles.json file not found or an error occured while initializing custom-tiles.json\nThe path of the file should be:\n\t{Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "custom-tiles.json")}");
} }
this.SetupTilesDicts();
} }
public bool isAvailable(string locationName) public static bool IsAvailable(string locationName)
{ {
List<JObject> allData = new List<JObject>(); List<JObject> allData = new();
if (customTilesData != null) allData.Add(customTilesData); if (customTilesData != null) allData.Add(customTilesData);
if (staticTilesData != null) allData.Add(staticTilesData); if (staticTilesData != null) allData.Add(staticTilesData);
@ -67,7 +67,7 @@ namespace stardew_access.Features
{ {
if (location.Key.Contains("||") && MainClass.ModHelper != null) if (location.Key.Contains("||") && MainClass.ModHelper != null)
{ {
string uniqueModID = location.Key.Substring(location.Key.LastIndexOf("||") + 2); string uniqueModID = location.Key[(location.Key.LastIndexOf("||") + 2)..];
string locationNameInJson = location.Key.Remove(location.Key.LastIndexOf("||")); string locationNameInJson = location.Key.Remove(location.Key.LastIndexOf("||"));
bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID); bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID);
@ -82,93 +82,92 @@ namespace stardew_access.Features
return false; return false;
} }
public (string? name, CATEGORY category) GetTileFromSet(int x, int y, GameLocation currentLocation, HashSet<KeyValuePair<string, JToken?>> data) public static (string? name, CATEGORY category) GetTileFromDict(int x, int y)
{ {
if (staticTilesDataDict is not null && staticTilesDataDict.TryGetValue(Game1.currentLocation.Name, out var locationDict))
{
if (locationDict is not null && locationDict.TryGetValue(((short)x, (short)y), out var tile))
{
//MainClass.DebugLog($"Tile ({x}, {y}) is in the dict as {tile.name}.");
return tile;
}
} /*else if (locationDict is null) {
//MainClass.DebugLog($"Skipping null entry for location {Game1.currentLocation.Name}.");
}
else {
MainClass.InfoLog($"Location {Game1.currentLocation.Name} not found in static tiles.");
}*/
return (null, CATEGORY.Others);
}
private static Dictionary<string, Dictionary<(short x, short y), (string name, CATEGORY category)>?>? BuildTilesDict(JObject? data)
{
if (data is null) return null;
//MainClass.DebugLog("Loading dict data");
var comparer = StringComparer.OrdinalIgnoreCase;
Dictionary<string, Dictionary<(short x, short y), (string name, CATEGORY category)>?> tilesDict = new(comparer);
foreach (KeyValuePair<string, JToken?> location in data) foreach (KeyValuePair<string, JToken?> location in data)
{ {
try
{
//MainClass.DebugLog($"Entering loop for location {location}.");
if (location.Value is null) continue;
string locationName = location.Key; string locationName = location.Key;
if (locationName.Contains("||") && MainClass.ModHelper is not null) if (locationName.Contains("||") && MainClass.ModHelper is not null)
{ {
// Mod Specific Tiles /* Mod Specific Tiles
// We can add tiles that only get detected when the specified mod is loaded. * We can add tiles that only get detected when the specified mod is loaded.
// Syntax: <location name>||<Mod's unique id, look into the mod's manifest.json for unique id> * Syntax: <location name>||<Mod's unique id, look into the mod's manifest.json for unique id>
// Example: THe following tile will only be detected if Stardew Valley Expanded mod is installed * Example: The following tile will only be detected if Stardew Valley Expanded mod is installed
// { * {
// . * .
// . * .
// . * .
// "Town||FlashShifter.StardewValleyExpandedCP":{ * "Town||FlashShifter.StardewValleyExpandedCP":{
// "<Tile Name>":{ * "<Tile Name>":{
// "x": [<x location(s)>], * "x": [<x location(s)>],
// "y": [<y location(s)>], * "y": [<y location(s)>],
// "type": "<Category name>" * "type": "<Category name>"
// } * }
// }, * },
// . * .
// . * .
// . * .
// } * }
string uniqueModID = locationName.Substring(locationName.LastIndexOf("||") + 2); */
string uniqueModID = locationName[(locationName.LastIndexOf("||") + 2)..];
locationName = locationName.Remove(locationName.LastIndexOf("||")); locationName = locationName.Remove(locationName.LastIndexOf("||"));
bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID); bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID);
if (!isLoaded) continue; // Skip if the specified mod is not loaded if (!isLoaded) continue; // Skip if the specified mod is not loaded
} }
//MainClass.DebugLog($"Loading tiles for {locationName}.");
if (locationName.StartsWith("farm_", StringComparison.OrdinalIgnoreCase)) if (location.Value.Type == JTokenType.Null)
{ {
string farmType = locationName.Substring(locationName.LastIndexOf("_") + 1); tilesDict.Add(location.Key, null);
int farmTypeIndex = getFarmTypeIndex(farmType); //MainClass.DebugLog($"Created null entry for location {location.Key}.");
locationName = locationName.Remove(locationName.LastIndexOf("_")); //MainClass.DebugLog("SPAM!!!");
continue;
if (farmTypeIndex != Game1.whichFarm) continue; // Skip if current farm type does not matches
// if (Game1.whichModFarm is not null) MainClass.DebugLog($"{farmType} {Game1.whichModFarm.MapName}");
if (farmTypeIndex != 7 || Game1.whichModFarm is null || !farmType.Equals(Game1.whichModFarm.MapName, StringComparison.OrdinalIgnoreCase)) continue; // Not tested but should work
} }
if (locationName.Equals("town_joja", StringComparison.OrdinalIgnoreCase) && Game1.MasterPlayer.mailReceived.Contains("JojaMember"))
Dictionary<(short x, short y), (string name, CATEGORY category)>? locationDict = new();
//MainClass.DebugLog($"Entering tiles loop for {locationName}.");
foreach (var tileInfo in ((JObject)location.Value))
{ {
locationName = "town"; if (tileInfo.Value == null) continue;
} string key = tileInfo.Key;
var tile = tileInfo.Value;
if (!currentLocation.Name.Equals(locationName, StringComparison.OrdinalIgnoreCase)) continue; if (tile.Type == JTokenType.Object )
if (location.Value is null) continue;
foreach (var tile in ((JObject)location.Value))
{ {
if (tile.Value is null) continue; JToken? tileXArray = tile["x"];
JToken? tileYArray = tile["y"];
JToken? tileXArray = tile.Value["x"]; JToken? tileType = tile["type"];
JToken? tileYArray = tile.Value["y"];
JToken? tileType = tile.Value["type"];
if (tileXArray is null || tileYArray is null || tileType is null) if (tileXArray is null || tileYArray is null || tileType is null)
continue; continue;
bool isXPresent = false; //MainClass.DebugLog($"Adding tile {key} to location {locationName}.");
bool isYPresent = false;
foreach (var item in tileXArray)
{
if (short.Parse(item.ToString()) != x)
continue;
isXPresent = true;
break;
}
foreach (var item in tileYArray)
{
if (short.Parse(item.ToString()) != y)
continue;
isYPresent = true;
break;
}
if (isXPresent && isYPresent)
{
string key = tile.Key;
if (key.Contains('[') && key.Contains(']')) if (key.Contains('[') && key.Contains(']'))
{ {
int i1 = key.IndexOf('['); int i1 = key.IndexOf('[');
@ -179,30 +178,105 @@ namespace stardew_access.Features
key = key.Remove(i1, ++i2 - i1); key = key.Remove(i1, ++i2 - i1);
} }
} }
(string key, CATEGORY category) tileData = (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower()));
return (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower())); foreach (var item_x in tileXArray)
{
short x = short.Parse(item_x.ToString());
foreach (var item_y in tileYArray)
{
short y = short.Parse(item_y.ToString());
(short x, short y) coords = (x, y);
try
{
locationDict.Add(coords, tileData);
}
catch (System.Exception e)
{
MainClass.ErrorLog($"Failed setting tile {key} for location {locationName}. Reason:\n\t{e}");
} }
} }
} }
}
}
//MainClass.DebugLog($"Location Dict has {locationDict.Count} members.");
if (locationDict.Count > 0)
{
//MainClass.DebugLog($"Adding locationDict for {locationName}");
tilesDict.Add(locationName, locationDict);
//MainClass.DebugLog($"Added locationDict for {locationName}");
}
} catch (System.Exception e) {
if (location.Value is null || location.Value.Type == JTokenType.Null)
{
tilesDict.Add(location.Key, null);
//MainClass.DebugLog($"Created null entry for location {location.Key}.");
} else {
MainClass.ErrorLog($"Unable to build tiles dict; failed on location {location.Key} with value ({location.Value.GetType()}){location.Value}. Reason:\n\t{e}");
throw;
}
}
}
if (tilesDict.Count > 0)
{
//MainClass.DebugLog("Dict loaded, returning.");
return tilesDict;
} else {
//MainClass.DebugLog("Dict not loaded, returning null");
return null;
}
}
public void SetupTilesDicts()
{
//MainClass.DebugLog("Attempting to set dicts");
try
{
staticTilesDataDict = BuildTilesDict(staticTilesData);
if (staticTilesDataDict is not null)
{
//MainClass.DebugLog($"staticTilesDataDict has {staticTilesDataDict.Count} entries.");
//MainClass.DebugLog($"Keys: {staticTilesDataDict.Keys}");
} else {
//MainClass.DebugLog("Static tiles not loaded.");
}
}
catch (System.Exception e)
{
MainClass.ErrorLog($"Failed to set static tiles dict. Reason: \n\t{e}");
}
try
{
customTilesDataDict = BuildTilesDict(customTilesData);
if (customTilesDataDict is not null)
{
//MainClass.DebugLog($"customTilesDataDict has {customTilesDataDict.Count} entries.");
} else {
//MainClass.DebugLog("Custom tiles not loaded.");
}
}
catch (System.Exception e)
{
MainClass.ErrorLog($"Faild to set custom tiles dict. Reason:\n\t{e}");
}
//MainClass.DebugLog("Successfully created tiles dicts.");
}
public static string? GetStaticTileInfoAt(int x, int y)
{
return GetStaticTileInfoAtWithCategory(x, y).name;
}
public static (string? name, CATEGORY category) GetStaticTileInfoAtWithCategory(int x, int y)
{
if (customTilesDataDict is not null) return GetTileFromDict(x, y);
if (staticTilesDataDict is not null) return GetTileFromDict(x, y);
return (null, CATEGORY.Others); return (null, CATEGORY.Others);
} }
public string? getStaticTileInfoAt(int x, int y) private static int GetFarmTypeIndex(string farmType)
{
return getStaticTileInfoAtWithCategory(x, y).name;
}
public (string? name, CATEGORY category) getStaticTileInfoAtWithCategory(int x, int y)
{
GameLocation currentLocation = Game1.currentLocation;
if (customTilesDataSet is not null) return GetTileFromSet(x, y, currentLocation, customTilesDataSet);
if (staticTilesDataSet is not null) return GetTileFromSet(x, y, currentLocation, staticTilesDataSet);
return (null, CATEGORY.Others);
}
private int getFarmTypeIndex(string farmType)
{ {
return farmType.ToLower() switch return farmType.ToLower() switch
{ {

View File

@ -46,7 +46,7 @@ namespace stardew_access.Features
string? resourceClump = getResourceClumpAtTile(x, y, lessInfo); string? resourceClump = getResourceClumpAtTile(x, y, lessInfo);
string? farmAnimal = getFarmAnimalAt(Game1.currentLocation, x, y); string? farmAnimal = getFarmAnimalAt(Game1.currentLocation, x, y);
string? parrot = getParrotPerchAtTile(x, y); string? parrot = getParrotPerchAtTile(x, y);
(string? name, CATEGORY category) staticTile = MainClass.STiles.getStaticTileInfoAtWithCategory(x, y); (string? name, CATEGORY category) staticTile = StaticTiles.GetStaticTileInfoAtWithCategory(x, y);
string? bush = getBushAtTile(x, y, lessInfo); string? bush = getBushAtTile(x, y, lessInfo);
if (Game1.currentLocation.isCharacterAtTile(tile) is NPC npc) if (Game1.currentLocation.isCharacterAtTile(tile) is NPC npc)

View File

@ -141,6 +141,7 @@ namespace stardew_access
helper.Events.Input.ButtonPressed += this.OnButtonPressed; helper.Events.Input.ButtonPressed += this.OnButtonPressed;
helper.Events.GameLoop.UpdateTicked += this.onUpdateTicked; helper.Events.GameLoop.UpdateTicked += this.onUpdateTicked;
helper.Events.GameLoop.GameLaunched += this.onGameLaunched;
AppDomain.CurrentDomain.DomainUnload += OnExit; AppDomain.CurrentDomain.DomainUnload += OnExit;
AppDomain.CurrentDomain.ProcessExit += OnExit; AppDomain.CurrentDomain.ProcessExit += OnExit;
} }
@ -159,6 +160,12 @@ namespace stardew_access
return new API(); return new API();
} }
private void onGameLaunched(object? sender, GameLaunchedEventArgs? e)
{
if (sTiles is not null)
sTiles.SetupTilesDicts();
}
private void onUpdateTicked(object? sender, UpdateTickedEventArgs? e) private void onUpdateTicked(object? sender, UpdateTickedEventArgs? e)
{ {
if (!Context.IsPlayerFree) if (!Context.IsPlayerFree)

View File

@ -674,6 +674,7 @@
"type": "decoration" "type": "decoration"
} }
}, },
"farmhouse": null,
"fishshop": { "fishshop": {
"Shop Counter": { "Shop Counter": {
"x": [4, 5, 6], "x": [4, 5, 6],