Skip to content

Commit

Permalink
fixed schedules starting at default locations (#10)
Browse files Browse the repository at this point in the history
fixed npc start at default locations after reload
code cleanup
bump version to 3.2.0
fixed update keys
  • Loading branch information
RealSweetPanda authored Apr 29, 2023
1 parent 73b7a33 commit 5bcef15
Show file tree
Hide file tree
Showing 14 changed files with 1,012 additions and 680 deletions.
29 changes: 19 additions & 10 deletions Framework/IGenericModConfigMenuApi.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
using StardewModdingAPI.Utilities;
using StardewValley;

namespace GenericModConfigMenu
{
Expand All @@ -21,7 +17,10 @@ public interface IGenericModConfigMenuApi
/// <param name="reset">Reset the mod's config to its default values.</param>
/// <param name="save">Save the mod's current config to the <c>config.json</c> file.</param>
/// <param name="titleScreenOnly">Whether the options can only be edited from the title screen.</param>
/// <remarks>Each mod can only be registered once, unless it's deleted via <see cref="Unregister"/> before calling this again.</remarks>
/// <remarks>
/// Each mod can only be registered once, unless it's deleted via <see cref="Unregister" /> before calling this
/// again.
/// </remarks>
void Register(IManifest mod, Action reset, Action save, bool titleScreenOnly = false);


Expand All @@ -31,7 +30,10 @@ public interface IGenericModConfigMenuApi
/// <summary>Add a section title at the current position in the form.</summary>
/// <param name="mod">The mod's manifest.</param>
/// <param name="text">The title text shown in the form.</param>
/// <param name="tooltip">The tooltip text shown when the cursor hovers on the title, or <c>null</c> to disable the tooltip.</param>
/// <param name="tooltip">
/// The tooltip text shown when the cursor hovers on the title, or <c>null</c> to disable the
/// tooltip.
/// </param>
void AddSectionTitle(IManifest mod, Func<string> text, Func<string> tooltip = null);

/// <summary>Add a paragraph of text at the current position in the form.</summary>
Expand Down Expand Up @@ -97,9 +99,16 @@ public interface IGenericModConfigMenuApi
/// <param name="getValue">Get the current value from the mod config.</param>
/// <param name="setValue">Set a new value in the mod config.</param>
/// <param name="name">The label text to show in the form.</param>
/// <param name="tooltip">The tooltip text shown when the cursor hovers on the field, or <c>null</c> to disable the tooltip.</param>
/// <param name="fieldId">The unique field ID for use with <see cref="OnFieldChanged"/>, or <c>null</c> to auto-generate a randomized ID.</param>
void AddKeybind(IManifest mod, Func<SButton> getValue, Action<SButton> setValue, Func<string> name, Func<string> tooltip = null, string fieldId = null);
/// <param name="tooltip">
/// The tooltip text shown when the cursor hovers on the field, or <c>null</c> to disable the
/// tooltip.
/// </param>
/// <param name="fieldId">
/// The unique field ID for use with <see cref="OnFieldChanged" />, or <c>null</c> to auto-generate a
/// randomized ID.
/// </param>
void AddKeybind(IManifest mod, Func<SButton> getValue, Action<SButton> setValue, Func<string> name,
Func<string> tooltip = null, string fieldId = null);

/// <summary>Add a key binding list at the current position in the form.</summary>
/// <param name="mod">The mod's manifest.</param>
Expand Down Expand Up @@ -174,4 +183,4 @@ public interface IGenericModConfigMenuApi
/// <param name="mod">The mod's manifest.</param>
//void Unregister(IManifest mod);
}
}
}
8 changes: 5 additions & 3 deletions Framework/ModConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using StardewModdingAPI;

namespace SaveAnywhere.Framework {
internal class ModConfig {
public SButton SaveKey { get; set; } = (SButton) 75;
namespace SaveAnywhere.Framework
{
internal class ModConfig
{
public SButton SaveKey { get; set; } = (SButton)75;
}
}
3 changes: 2 additions & 1 deletion Framework/Model/BuffData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace SaveAnywhere.Framework.Model {
namespace SaveAnywhere.Framework.Model
{
public sealed record BuffData(
string DisplaySource,
string Source,
Expand Down
18 changes: 10 additions & 8 deletions Framework/Model/PlayerData.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
namespace SaveAnywhere.Framework.Model {
public class PlayerData {
namespace SaveAnywhere.Framework.Model
{
public class PlayerData
{
public int Time { get; init; }
public BuffData[] otherBuffs { get; init; }
public BuffData drinkBuff { get; init; }
public BuffData foodBuff { get; init; }
public PositionData Position { get; init; }
public BuffData[] OtherBuffs { get; init; }

public BuffData DrinkBuff { get; init; }

public BuffData FoodBuff { get; init; }
public PositionData[] Position { get; init; }

public bool IsCharacterSwimming { get; init; }
}
Expand Down
3 changes: 2 additions & 1 deletion Framework/Model/PositionData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace SaveAnywhere.Framework.Model {
namespace SaveAnywhere.Framework.Model
{
public sealed record PositionData(
string Name,
string Map,
Expand Down
97 changes: 97 additions & 0 deletions Framework/NPCExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;
using Netcode;
using StardewValley;

namespace SaveAnywhere.Framework
{
public static class NpcExtensions
{

public static SchedulePathDescription pathfindToNextScheduleLocation(this NPC npc,
string startingLocation,
int startingX,
int startingY,
string endingLocation,
int endingX,
int endingY,
int finalFacingDirection,
string endBehavior,
string endMessage)
{
var pointStack = new Stack<Point>();
var startPoint = new Point(startingX, startingY);
var stringList = !startingLocation.Equals(endingLocation, StringComparison.Ordinal)
? npc.getLocationRoute(startingLocation, endingLocation)
: null;
if (stringList != null)
{
for (var index = 0; index < stringList.Count; ++index)
{
var locationFromName = Game1.getLocationFromName(stringList[index]);
if (locationFromName.Name.Equals("Trailer") &&
Game1.MasterPlayer.mailReceived.Contains("pamHouseUpgrade"))
locationFromName = Game1.getLocationFromName("Trailer_Big");
if (index < stringList.Count - 1)
{
var warpPointTo = locationFromName.getWarpPointTo(stringList[index + 1]);
if (warpPointTo.Equals(Point.Zero) || startPoint.Equals(Point.Zero))
throw new Exception("schedule pathing tried to find a warp point that doesn't exist.");
pointStack = addToStackForSchedule(pointStack,
PathFindController.findPathForNPCSchedules(startPoint, warpPointTo, locationFromName,
30000));
startPoint = locationFromName.getWarpPointTarget(warpPointTo, npc);
}
else
{
pointStack = addToStackForSchedule(pointStack,
PathFindController.findPathForNPCSchedules(startPoint, new Point(endingX, endingY),
locationFromName, 30000));
}
}
}
else if (startingLocation.Equals(endingLocation, StringComparison.Ordinal))
{
var locationFromName = Game1.getLocationFromName(startingLocation);
if (locationFromName.Name.Equals("Trailer") &&
Game1.MasterPlayer.mailReceived.Contains("pamHouseUpgrade"))
locationFromName = Game1.getLocationFromName("Trailer_Big");
pointStack = PathFindController.findPathForNPCSchedules(startPoint, new Point(endingX, endingY),
locationFromName, 30000);
}

return new SchedulePathDescription(pointStack, finalFacingDirection, endBehavior, endMessage);
}

private static List<string> getLocationRoute(this NPC npc, string startingLocation, string endingLocation)
{
var field = typeof(NPC)
.GetField("routesFromLocationToLocation",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Default | BindingFlags.Instance);
//get routesFromLocationToLocation field from fields and get its value
var routes = (List<List<string>>)field.GetValue(null);
foreach (var source in routes)
if (source.First().Equals(startingLocation, StringComparison.Ordinal) &&
source.Last().Equals(endingLocation, StringComparison.Ordinal) &&
((int)(NetFieldBase<int, NetInt>)npc.gender == 0 ||
!source.Contains("BathHouse_MensLocker", StringComparer.Ordinal)) &&
((int)(NetFieldBase<int, NetInt>)npc.gender != 0 ||
!source.Contains("BathHouse_WomensLocker", StringComparer.Ordinal)))
return source;
return null;
}

private static Stack<Point> addToStackForSchedule(Stack<Point> original, Stack<Point> toAdd)
{
if (toAdd == null)
return original;
original = new Stack<Point>(original);
while (original.Count > 0)
toAdd.Push(original.Pop());
return toAdd;
}
}
}
Loading

0 comments on commit 5bcef15

Please sign in to comment.