Skip to content

Commit

Permalink
Refactor event tile logic and consolidate tile data
Browse files Browse the repository at this point in the history
- **Unify tile data loading**:
  - Refactored event tile logic to load from the unified `tiles.json`, aligning it with how other static tiles are managed.

- **Remove obsolete configuration**:
  - Deleted `event-tiles.json` and removed related code from `DynamicTiles.cs`, eliminating redundancy and simplifying the codebase.

- **Integrate event-specific data**:
  - Moved event tile data into `tiles.json` under respective festival names, ensuring all tile definitions are centralized.
  - Relocated festival Fluent tokens to `static_tiles.en.ftl` to streamline localization processes.

- **Update naming references**:
  - Modified references from location names to event names where appropriate, enhancing clarity and consistency in tile identification.

Partially addresses khanshoaib3#420, khanshoaib3#407, khanshoaib3#311,  and khanshoaib3#225
  • Loading branch information
ParadoxiKat committed Jan 7, 2025
1 parent 5e85e98 commit 8c1b4e2
Show file tree
Hide file tree
Showing 10 changed files with 554 additions and 152 deletions.
12 changes: 6 additions & 6 deletions stardew-access/Features/ObjectTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ private void MoveToCurrentlySelectedObject()

public void SaveToFavorites(int hotkey)
{
string location = Game1.currentLocation.NameOrUniqueName;
string location = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName;
string currentSaveFileName = MainClass.GetCurrentSaveFileName();
if (!favorites.ContainsKey(currentSaveFileName))
{
Expand All @@ -566,7 +566,7 @@ public void SaveToFavorites(int hotkey)

public (string?, string?) GetFromFavorites(int hotkey)
{
string location = Game1.currentLocation.NameOrUniqueName;
string location = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName;
if (!favorites.TryGetValue(MainClass.GetCurrentSaveFileName(), out var _saveFileFavorites) || _saveFileFavorites != null)
{
LoadDefaultFavorites();
Expand Down Expand Up @@ -596,7 +596,7 @@ public void SetFromFavorites(int hotkey)

public void DeleteFavorite(int favoriteNumber)
{
string currentLocation = Game1.currentLocation.NameOrUniqueName;
string currentLocation = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName;

// Try to get the sub-dictionary for the current location
if (favorites.TryGetValue(MainClass.GetCurrentSaveFileName(), out var saveFileFavorites) && saveFileFavorites != null)
Expand Down Expand Up @@ -728,7 +728,7 @@ private void HandleFavorite(int favKeyNum)
new
{
coordinates = Vector2ToString(CurrentPlayer.FacingTile),
location_name = Game1.currentLocation!.NameOrUniqueName,
location_name = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName,
favorite_number
}
);
Expand All @@ -738,7 +738,7 @@ private void HandleFavorite(int favKeyNum)
{
selected_object = SelectedObject,
selected_category = SelectedCategory,
location_name = Game1.currentLocation!.NameOrUniqueName,
location_name = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName,
favorite_number
}
);
Expand All @@ -758,7 +758,7 @@ private void HandleFavorite(int favKeyNum)
MainClass.ScreenReader.TranslateAndSay("feature-object_tracker-favorite_cleared", true,
new
{
location_name = Game1.currentLocation!.NameOrUniqueName,
location_name = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName,
favorite_number
}
);
Expand Down
4 changes: 2 additions & 2 deletions stardew-access/Features/TileViewer/TileDataEntryMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void PopulateOptions()
}

AddLabel(Translator.Instance.Translate("menu-tile_data_entry-heading_label",
new { tile_x = _tileX, tile_y = _tileY, location_name = Game1.currentLocation.NameOrUniqueName },
new { tile_x = _tileX, tile_y = _tileY, location_name = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName },
TranslationCategory.Menu),
(int)OptionsIdentifiers.HeadingLabel);

Expand Down Expand Up @@ -269,7 +269,7 @@ public override void receiveLeftClick(int x, int y, bool playSound = true)

GetEnteredTileInformation(out var tileInfo);
if (tileInfo == null) return;
if (_defaultData != null) UserTilesUtils.RemoveTileDataAt(_tileX, _tileY, Game1.currentLocation.NameOrUniqueName);
if (_defaultData != null) UserTilesUtils.RemoveTileDataAt(_tileX, _tileY, Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName);
UserTilesUtils.AddTileData(tileInfo);
MainClass.TileManager.Initialize();
exitThisMenu();
Expand Down
6 changes: 3 additions & 3 deletions stardew-access/Tiles/AccessibleLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public AccessibleLocation(
Tiles = new OverlayedDictionary<Vector2, AccessibleTile>(DefaultLayerName);
}
#if DEBUG
Log.Verbose($"AccessibleLocation: initialized \"{Location.NameOrUniqueName}\"");
Log.Verbose($"AccessibleLocation: initialized \"{(Location.currentEvent is not null ? Location.currentEvent.FestivalName : Location.NameOrUniqueName)}\"");
#endif
}

Expand Down Expand Up @@ -147,12 +147,12 @@ public void AddTile(AccessibleTile tile, string? layerName = null)
{
result = Tiles.TryAdd(coordinate, tile, layerName);
#if DEBUG
Log.Verbose($"Adding tile {tile} to layer {layerName} of location {Location.NameOrUniqueName} at ({coordinate.X}, {coordinate.Y})");
Log.Verbose($"Adding tile {tile} to layer {layerName} of location {(Location.currentEvent is not null ? Location.currentEvent.FestivalName : Location.NameOrUniqueName)} at ({coordinate.X}, {coordinate.Y})");
#endif
} else {
result = Tiles.TryAdd(coordinate, tile);
#if DEBUG
Log.Verbose($"Adding tile {tile} to location {Location.NameOrUniqueName} at ({coordinate.X}, {coordinate.Y})");
Log.Verbose($"Adding tile {tile} to location {(Location.currentEvent is not null ? Location.currentEvent.FestivalName : Location.NameOrUniqueName)} at ({coordinate.X}, {coordinate.Y})");
#endif
}
if (result) AddTileToCategoryMap(tile, layerName ?? "");
Expand Down
28 changes: 17 additions & 11 deletions stardew-access/Tiles/AccessibleTileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,18 @@ internal static bool ConvertOldCustomTilesFormat()
public AccessibleLocation CreateLocation(GameLocation gameLocation)
{
string locationName = gameLocation.NameOrUniqueName;
Log.Trace($"AccessibleTileManager.CreateLocation: Creating new AccessibleLocation {locationName}");
bool isEvent = gameLocation.currentEvent is not null;
string eventName = gameLocation.currentEvent?.FestivalName ?? "";
if (isEvent)
Log.Trace($"AccessibleTileManager.CreateLocation: Creating new event AccessibleLocation {eventName}");
else
Log.Trace($"AccessibleTileManager.CreateLocation: Creating new AccessibleLocation {locationName}");

JArray? jsonData = null;
JArray? userJsonData = null;

// Check if location data exists in the JSON objects
if (tilesJson != null && tilesJson.TryGetValue(locationName, out JToken? locationJsonToken) && locationJsonToken is JArray array)
if (tilesJson != null && tilesJson.TryGetValue(isEvent ? eventName : locationName, out JToken? locationJsonToken) && locationJsonToken is JArray array)
{
jsonData = array;
}
Expand All @@ -170,7 +175,7 @@ public AccessibleLocation CreateLocation(GameLocation gameLocation)
AddFromTileProperties(location, gameLocation);

// Add the location to the Locations dictionary
Locations.Add(locationName, location);
Locations.Add(isEvent ? eventName : locationName, location);

return location;
}
Expand All @@ -180,7 +185,7 @@ public AccessibleLocation CreateLocation(GameLocation gameLocation)
{
if (string.IsNullOrEmpty(locationName))
{
locationName = Game1.currentLocation.NameOrUniqueName;
locationName = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName;
}

EnsureLocationLoaded(Game1.currentLocation);
Expand All @@ -197,14 +202,15 @@ public AccessibleLocation CreateLocation(GameLocation gameLocation)
public AccessibleLocation? GetLocation(GameLocation? location = null)
{
location ??= Game1.currentLocation;
return GetLocation(location!.NameOrUniqueName);
string locationName = location.currentEvent is not null ? location.currentEvent.FestivalName : location.NameOrUniqueName;
return GetLocation(locationName);
}

public void EnsureLocationLoaded(GameLocation gameLocation)
{
if (gameLocation == null) return;

string locationName = gameLocation.NameOrUniqueName;
string locationName = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName;

// Use negated TryGetValue to create an AccessibleLocation instance if it doesn't exist
if (!Locations.TryGetValue(locationName, out _))
Expand All @@ -216,14 +222,14 @@ public void EnsureLocationLoaded(GameLocation gameLocation)

public (string nameOrTranslationKey, CATEGORY category)? GetNameAndCategoryAt(Vector2 coordinates, string? layerName = null, string? locationName = null)
{
locationName ??= Game1.currentLocation.NameOrUniqueName;
locationName ??= Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName;
return GetLocation(locationName)?.GetNameAndCategoryAt(coordinates, layerName);
}

public (string nameOrTranslationKey, CATEGORY category)? GetNameAndCategoryAt(Vector2 coordinates, string? layerName = null) => GetNameAndCategoryAt(coordinates, layerName, Game1.currentLocation.NameOrUniqueName);
public (string nameOrTranslationKey, CATEGORY category)? GetNameAndCategoryAt(Vector2 coordinates, string? layerName = null, GameLocation? location = null) => GetNameAndCategoryAt(coordinates, layerName, location?.NameOrUniqueName);
public (string nameOrTranslationKey, CATEGORY category)? GetNameAndCategoryAt(Vector2 coordinates, string? layerName = null) => GetNameAndCategoryAt(coordinates, layerName, Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName);
public (string nameOrTranslationKey, CATEGORY category)? GetNameAndCategoryAt(Vector2 coordinates, string? layerName = null, GameLocation? location = null) => GetNameAndCategoryAt(coordinates, layerName, Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName);
public (string nameOrTranslationKey, CATEGORY category)? GetNameAndCategoryAt((int x, int y) coordinates, string? layerName = null, string? locationName = null) => GetNameAndCategoryAt(new Vector2(coordinates.x, coordinates.y), layerName, locationName);
public (string nameOrTranslationKey, CATEGORY category)? GetNameAndCategoryAt((int x, int y) coordinates, string? layerName = null, GameLocation? location = null) => GetNameAndCategoryAt(new Vector2(coordinates.x, coordinates.y), layerName, location?.NameOrUniqueName);
public (string nameOrTranslationKey, CATEGORY category)? GetNameAndCategoryAt((int x, int y) coordinates, string? layerName = null, GameLocation? location = null) => GetNameAndCategoryAt(new Vector2(coordinates.x, coordinates.y), layerName, Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName);

public HashSet<AccessibleTile> GetTilesByCategory(CATEGORY category, string? layerName = null) => GetLocation()?.GetTilesByCategory(category, layerName) ?? [];
public HashSet<AccessibleTile> GetTilesByCategory(CATEGORY category, string? layerName = null, string? locationName = null) => GetLocation(locationName)?.GetTilesByCategory(category, layerName) ?? [];
Expand Down Expand Up @@ -251,7 +257,7 @@ private void AddFromTileProperties(AccessibleLocation location, GameLocation gam
? Translator.Instance.Translate(valArray[2])
: valArray[1];
#if DEBUG
Log.Debug($"[AccessibleTileManager::AddFromTileProperties {{{gameLocation.NameOrUniqueName}}}] Adding a tile: {name}, {x}x {y}y, category={category.Value}, layer={layer.Id}");
Log.Debug($"[AccessibleTileManager::AddFromTileProperties {{{(Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName)}}}] Adding a tile: {name}, {x}x {y}y, category={category.Value}, layer={layer.Id}");
#endif
location.AddTile(new(staticNameOrTranslationKey: name, staticCoordinates: [new(x, y)], category: category));
}
Expand Down
69 changes: 0 additions & 69 deletions stardew-access/Utils/DynamicTiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,65 +150,11 @@ public static DynamicTiles Instance
{ "Deluxe Coop", (6, 17, 3) }
};

// Dictionary to hold event info
private static readonly Dictionary<string, Dictionary<(int X, int Y), string>> EventInteractables;

/// <summary>
/// Initializes a new instance of the <see cref="DynamicTiles"/> class.
/// Loads the event file.
/// </summary>
static DynamicTiles()
{
EventInteractables = LoadEventTiles();
}

/// <summary>
/// Loads event tiles from the "event-tiles.json" file and returns a dictionary representation of the data.
/// </summary>
/// <returns>
/// A dictionary with event names as keys and nested dictionaries as values, where nested dictionaries have
/// coordinate tuples (x, y) as keys and tile names as values.
/// </returns>
private static Dictionary<string, Dictionary<(int x, int y), string>> LoadEventTiles()
{
const string EventTilesFileName = "event-tiles.json";
bool loaded = JsonLoader.TryLoadJsonFile(EventTilesFileName, out JToken? json, subdir: "assets/TileData");

if (!loaded || json == null || json.Type != JTokenType.Object)
{
// If the JSON couldn't be loaded, parsed, or is not a JSON object, return an empty dictionary
return [];
}

var eventTiles = new Dictionary<string, Dictionary<(int x, int y), string>>();

// Iterate over the JSON properties to create a dictionary representation of the data
foreach (var eventProperty in ((JObject)json).Properties())
{
string eventName = eventProperty.Name;
var coordinates = new Dictionary<(int x, int y), string>();

// Iterate over the coordinate properties to create a nested dictionary with coordinate tuples as keys
if (eventProperty.Value is JObject coordinatesObject)
{
foreach (var coordinateProperty in coordinatesObject.Properties())
{
string[] xy = coordinateProperty.Name.Split(',');
if (xy.Length == 2 && int.TryParse(xy[0], out int x) && int.TryParse(xy[1], out int y))
{
coordinates.Add((x, y), value: coordinateProperty.Value.ToString() ?? string.Empty);
}
else
{
Log.Warn($"Invalid coordinate format '{coordinateProperty.Name}' in {EventTilesFileName}.");
}
}
}

eventTiles.Add(eventName, coordinates);
}

return eventTiles;
}

/// <summary>
Expand Down Expand Up @@ -1083,21 +1029,6 @@ public static (string? translationKeyOrName, CATEGORY? category) GetDynamicTileW
{
return ("tile_name-panning_spot", CATEGORY.Interactables);
}
// Check if the current location has an event
else if (currentLocation.currentEvent is not null)
{
string eventName = currentLocation.currentEvent.FestivalName;
// Attempt to retrieve the nested dictionary for the event name from the EventInteractables dictionary
if (EventInteractables.TryGetValue(eventName, out var coordinateDictionary))
{
// Attempt to retrieve the interactable value from the nested dictionary using the coordinates (x, y) as the key
if (coordinateDictionary.TryGetValue((x, y), value: out var interactable))
{
// If the interactable value is found, return the corresponding category and interactable name
return (interactable, CATEGORY.Interactables);
}
}
}

// Retrieve dynamic tile information based on the current location type
return currentLocation switch
Expand Down
9 changes: 5 additions & 4 deletions stardew-access/Utils/UserTilesUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ public static void AddTileData(AccessibleTile.JsonSerializerFormat jsonDataForTi
root = [];
}

if (!root.TryGetValue(Game1.currentLocation.NameOrUniqueName, out JToken? locationToken))
string locationName = Game1.currentLocation.currentEvent is not null ? Game1.currentLocation.currentEvent.FestivalName : Game1.currentLocation.NameOrUniqueName;
if (!root.TryGetValue(locationName, out JToken? locationToken))
{
// Creates the location property if not exists
Log.Trace($"Entry for location {Game1.currentLocation.NameOrUniqueName} not found, adding one...");
locationToken = new JProperty(Game1.currentLocation.NameOrUniqueName, new JArray());
Log.Trace($"Entry for location {locationName} not found, adding one...");
locationToken = new JProperty(locationName, new JArray());
root.Add(locationToken);
}

Expand Down Expand Up @@ -74,7 +75,7 @@ public static void RemoveTileDataAt(int x, int y, string locationName)
return;
}

if (!root.TryGetValue(Game1.currentLocation.NameOrUniqueName, out JToken? locationToken))
if (!root.TryGetValue(locationName, out JToken? locationToken))
{
// Creates the location property if not exists
Log.Trace($"Cannot find location data with name: {locationName}");
Expand Down
32 changes: 0 additions & 32 deletions stardew-access/assets/TileData/event-tiles.json

This file was deleted.

Loading

0 comments on commit 8c1b4e2

Please sign in to comment.