Skip to content

Commit

Permalink
Fixed cabin upgrade showing invalid cabins
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoked-Fish committed Jun 13, 2024
1 parent 9bed395 commit 6e92982
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Framework/Helpers/RenovationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static void ShowRenovationMenuAtLocation(string cabin)

#nullable disable
#pragma warning disable AvoidImplicitNetFieldCast, RCS1163
private static List<ISalable> GetAvailableRenovationsForFarmer(Farmer owner)
public static List<ISalable> GetAvailableRenovationsForFarmer(Farmer owner)
{
FarmHouse farmhouse = Game1.RequireLocation<FarmHouse>(owner.homeLocation.Value);
List<ISalable> available_renovations = [];
Expand Down
73 changes: 48 additions & 25 deletions Framework/Helpers/UpgradeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class UpgradeHelper
public static void UpgradeCabinsResponses()
{
var cabinPageNames = CabinUtility.GetCabinsToUpgrade();
Game1.currentLocation.ShowPagedResponses("Upgrade Cabin?", cabinPageNames, OfferCabinUpgrade, true);
Game1.currentLocation.ShowPagedResponses("Upgrade Cabin?", cabinPageNames, OfferCabinUpgrade);
}

private static void OfferCabinUpgrade(string cabin)
Expand All @@ -24,35 +24,26 @@ private static void OfferCabinUpgrade(string cabin)
case 0:
msg = Game1.content.LoadString("Strings\\Locations:ScienceHouse_Carpenter_UpgradeHouse1");
msg = ModEntry.Config.EnableFreeHouseUpgrade ? msg.Replace("10,000", "0").Replace("10.000", "0").Replace("10 000", "0").Replace("450", "0") : msg;
Game1.currentLocation.createQuestionDialogue(Game1.parseText(msg), Game1.currentLocation.createYesNoResponses(), (_, answer) =>
{
if (answer == "Yes")
AcceptCabinUpgrade(cabin);
}
);
break;
case 1:
msg = Game1.content.LoadString("Strings\\Locations:ScienceHouse_Carpenter_UpgradeHouse2", ModEntry.Config.EnableFreeHouseUpgrade ? "0" : "65,000", ModEntry.Config.EnableFreeHouseUpgrade ? "0" : "100");
Game1.currentLocation.createQuestionDialogue(Game1.parseText(msg), Game1.currentLocation.createYesNoResponses(), (_, answer) =>
{
if (answer == "Yes")
AcceptCabinUpgrade(cabin);
}
);
break;
case 2:
msg = Game1.content.LoadString("Strings\\Locations:ScienceHouse_Carpenter_UpgradeHouse3");
msg = ModEntry.Config.EnableFreeHouseUpgrade ? msg.Replace("10,000", "0").Replace("10.000", "0").Replace("100,000", "0").Replace("100.000", "0").Replace("100 000", "0") : msg;
Game1.currentLocation.createQuestionDialogue(Game1.parseText(msg), Game1.currentLocation.createYesNoResponses(), (_, answer) =>
{
if (answer == "Yes")
AcceptCabinUpgrade(cabin);
}
);
break;
default:
return; // or handle the default case
}

Game1.currentLocation.createQuestionDialogue(Game1.parseText(msg), Game1.currentLocation.createYesNoResponses(), (_, answer) =>
{
if (answer != "Yes") return;
AcceptCabinUpgrade(cabin);
});
}


private static void AcceptCabinUpgrade(string cabin)
{
var cabinBuilding = Game1.getLocationFromName(cabin).GetContainingBuilding();
Expand All @@ -66,7 +57,7 @@ private static void AcceptCabinUpgrade(string cabin)

if (ModEntry.Config.EnableFreeHouseUpgrade)
{
cabinInstance.owner.daysUntilHouseUpgrade.Value = ModEntry.Config.InstantHomeUpgrade ? 0 : 3;
cabinInstance.owner.daysUntilHouseUpgrade.Value = 3;
Game1.RequireCharacter("Robin").setNewDialogue("Data\\ExtraDialogue:Robin_HouseUpgrade_Accepted");
Game1.drawDialogue(Game1.getCharacterFromName("Robin"));
ModEntry.Multiplayer?.globalChatInfoMessage("HouseUpgrade", Game1.player.Name, Lexicon.getTokenizedPossessivePronoun(Game1.player.IsMale));
Expand All @@ -79,7 +70,7 @@ private static void AcceptCabinUpgrade(string cabin)
case 0:
if (Game1.player.Money >= 10000 && Game1.player.Items.ContainsId("(O)388", 450))
{
cabinInstance.owner.daysUntilHouseUpgrade.Value = ModEntry.Config.InstantHomeUpgrade ? 0 : 3;
cabinInstance.owner.daysUntilHouseUpgrade.Value = 3;
Game1.player.Money -= 10000;
Game1.player.Items.ReduceId("(O)388", 450);
Game1.RequireCharacter("Robin").setNewDialogue("Data\\ExtraDialogue:Robin_HouseUpgrade_Accepted");
Expand All @@ -100,7 +91,7 @@ private static void AcceptCabinUpgrade(string cabin)
case 1:
if (Game1.player.Money >= 65000 && Game1.player.Items.ContainsId("(O)709", 100))
{
cabinBuilding.daysUntilUpgrade.Value = ModEntry.Config.InstantHomeUpgrade ? 0 : 3;
cabinBuilding.daysUntilUpgrade.Value = 3;
Game1.player.Money -= 65000;
Game1.player.Items.ReduceId("(O)709", 100);
Game1.RequireCharacter("Robin").setNewDialogue("Data\\ExtraDialogue:Robin_HouseUpgrade_Accepted");
Expand All @@ -121,7 +112,7 @@ private static void AcceptCabinUpgrade(string cabin)
case 2:
if (Game1.player.Money >= 100000)
{
cabinBuilding.daysUntilUpgrade.Value = ModEntry.Config.InstantHomeUpgrade ? 0 : 3;
cabinBuilding.daysUntilUpgrade.Value = 3;
Game1.player.Money -= 100000;
Game1.RequireCharacter("Robin").setNewDialogue("Data\\ExtraDialogue:Robin_HouseUpgrade_Accepted");
Game1.drawDialogue(Game1.getCharacterFromName("Robin"));
Expand All @@ -137,11 +128,17 @@ private static void AcceptCabinUpgrade(string cabin)
}
}

public static void CompleteHouseUpgrade(Farmer owner)
public static void CompleteHouseUpgrade(Farmer owner, bool debug = false)
{
if (!ModEntry.Config.InstantHomeUpgrade)
if (!ModEntry.Config.InstantHomeUpgrade && !debug)
return;

if (debug)
{
var msg = $"Upgrading cabin at {Utility.getHomeOfFarmer(owner).NameOrUniqueName} from level {owner.HouseUpgradeLevel} to {owner.HouseUpgradeLevel + 1}";
ModEntry.ModMonitor.Log(msg, LogLevel.Debug);
}

var homeOfFarmer = Utility.getHomeOfFarmer(owner);
homeOfFarmer.moveObjectsForHouseUpgrade(owner.HouseUpgradeLevel + 1);
owner.HouseUpgradeLevel++;
Expand All @@ -150,4 +147,30 @@ public static void CompleteHouseUpgrade(Farmer owner)
owner.stats.checkForBuildingUpgradeAchievements();
owner.autoGenerateActiveDialogueEvent("houseUpgrade_" + owner.HouseUpgradeLevel);
}

public static void UpgradeAllCabins()
{
int count = 0;
foreach (var cabin in CabinUtility.GetCabins())
{
if (cabin.owner.isActive() || cabin.owner.HouseUpgradeLevel >= 3)
continue;

var msg = $"Upgrading cabin at {cabin.NameOrUniqueName} from level {cabin.owner.HouseUpgradeLevel} to 3";
ModEntry.ModMonitor.Log(msg, LogLevel.Debug);

var homeOfFarmer = Utility.getHomeOfFarmer(cabin.owner);
homeOfFarmer.moveObjectsForHouseUpgrade(3);
cabin.owner.HouseUpgradeLevel = 3;
cabin.owner.daysUntilHouseUpgrade.Value = -1;
homeOfFarmer.setMapForUpgradeLevel(cabin.owner.HouseUpgradeLevel);
cabin.owner.stats.checkForBuildingUpgradeAchievements();
cabin.owner.autoGenerateActiveDialogueEvent("houseUpgrade_" + cabin.owner.HouseUpgradeLevel);

count++;
}

ModEntry.ModMonitor.Log($"Upgraded {count} cabins", LogLevel.Debug);

}
}
4 changes: 2 additions & 2 deletions Framework/Patches/BuildingPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Apply()
Patch<GameLocation>(PatchType.Postfix, nameof(GameLocation.isBuildable), nameof(IsBuildablePostfix), [typeof(Vector2), typeof(bool)]);
Patch<CarpenterMenu>(PatchType.Prefix, nameof(CarpenterMenu.receiveLeftClick), nameof(ReceiveLeftClickPrefix), [typeof(int), typeof(int), typeof(bool)]);
Patch<CarpenterMenu>(PatchType.Prefix, nameof(CarpenterMenu.GetInitialBuildingPlacementViewport), nameof(GetInitialBuildingPlacementViewportPrefix), [typeof(GameLocation)]);
Patch<CarpenterMenu>(PatchType.Transpiler, nameof(CarpenterMenu.draw), nameof(DrawTranspiler), [typeof(SpriteBatch)]);
Patch<CarpenterMenu>(PatchType.Transpiler, nameof(CarpenterMenu.draw), nameof(CarpenterMenuDrawTranspiler), [typeof(SpriteBatch)]);
}

// Sets tiles buildable for construction (just visual)
Expand Down Expand Up @@ -275,7 +275,7 @@ static Location CenterOnTile(int x, int y)
}

// Don't display gold if build cost is less than 1 instead of 0
private static IEnumerable<CodeInstruction> DrawTranspiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator, MethodBase original)
private static IEnumerable<CodeInstruction> CarpenterMenuDrawTranspiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator, MethodBase original)
{
var codeInstructions = instructions.ToList();
try
Expand Down
24 changes: 23 additions & 1 deletion Framework/Patches/FarmingPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public void Apply()
Patch<FruitTree>(PatchType.Postfix, nameof(FruitTree.IsTooCloseToAnotherTree), nameof(IsTooCloseToAnotherTreePostfix), [typeof(Vector2), typeof(GameLocation), typeof(bool)]);
Patch<Tree>(PatchType.Postfix, nameof(Tree.IsGrowthBlockedByNearbyTree), nameof(IsGrowthBlockedByNearbyTreePostfix));

Patch<FruitTree>(PatchType.Postfix, nameof(FruitTree.IgnoresSeasonsHere), nameof(IgnoreSeasonsHerePostfix));
Patch<FruitTree>(PatchType.Postfix, nameof(FruitTree.IsInSeasonHere), nameof(IsInSeasonHerePostfix));

Patch<JunimoHut>(PatchType.Postfix, nameof(JunimoHut.dayUpdate), nameof(DayUpdatePostfix), [typeof(int)]);
}

Expand Down Expand Up @@ -110,12 +113,31 @@ private static void IsTooCloseToAnotherTreePostfix(FruitTree __instance, Vector2
__result = false;
}

public static void IsGrowthBlockedByNearbyTreePostfix(Tree __instance, ref bool __result)
private static void IsGrowthBlockedByNearbyTreePostfix(Tree __instance, ref bool __result)
{
if (ModEntry.Config.EnableWildTreeTweaks)
__result = false;
}

// Fix to use the normal season index but still produce fruit
private static void IgnoreSeasonsHerePostfix(FruitTree __instance, ref bool __result)
{
if (!ModEntry.Config.ForceCorrectTreeSprite || __instance.Location.IsGreenhouse)
return;

__result = false;
}

// Fix to use the normal season index but still produce fruit
private static void IsInSeasonHerePostfix(FruitTree __instance, ref bool __result)
{
if (!ModEntry.Config.ForceCorrectTreeSprite || __instance.Location.IsGreenhouse)
return;

if (ModEntry.Config.DisableSeasonRestrictions)
__result = true;
}

// Send juminos out even if the farmer isn't there.
private static void DayUpdatePostfix(JunimoHut __instance)
{
Expand Down
6 changes: 4 additions & 2 deletions Framework/Utilities/CabinUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ internal static class CabinUtility
foreach (var cabin in cabins)
{
bool shouldAddCabin = toRenovate ? cabin.owner.HouseUpgradeLevel >= 2 : cabin.owner.HouseUpgradeLevel < 3;
if (!shouldAddCabin) continue;
bool isUpgrading = cabin.owner.daysUntilHouseUpgrade.Value > 0;

if (!shouldAddCabin || isUpgrading || cabin.owner.isActive()) continue;

string msg = Game1.content.LoadString("Strings\\Buildings:Cabin_Name");
msg = string.IsNullOrEmpty(cabin.owner.Name) ? $"Empty {msg}" : $"{cabin.owner.displayName}'s {msg}";
Expand All @@ -33,7 +35,7 @@ public static bool HasCabinsToUpgrade(bool toRenovate = false)
return cabinsToUpgrade is { Count: > 0 };
}

private static List<Cabin> GetCabins()
public static List<Cabin> GetCabins()
{
List<Cabin> cabins = [];

Expand Down

0 comments on commit 6e92982

Please sign in to comment.