using System; using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.Xna.Framework; using Netcode; using Omegasis.NightOwl.Framework; using StardewModdingAPI; using StardewModdingAPI.Events; using StardewValley; using StardewValley.Characters; using StardewValley.Locations; /*TODO: Issues: -Mail can't be wiped without destroying all mail. -Lighting transition does not work if it is raining. -set the weather to clear if you are stayig up late. -transition still doesnt work. However atleast it is dark now. -Known glitched */ namespace Omegasis.NightOwl { /// The mod entry point. public class NightOwl : Mod { /********* ** Properties *********/ /// The mod configuration. private ModConfig Config; /**** ** Context ****/ /// Whether the player stayed up all night. private bool IsUpLate; /// Whether the player should be reset to their pre-collapse details for the morning transition on the next update. private bool ShouldResetPlayerAfterCollapseNow; /// Whether the player just started a new day. private bool JustStartedNewDay; /// Whether the player just collapsed for the morning transition. private bool JustCollapsed; /**** ** Pre-collapse state ****/ /// The player's location name before they collapsed. private string PreCollapseMap; /// The player's tile position before they collapsed. private Point PreCollapseTile; /// The player's money before they collapsed. private int PreCollapseMoney; /// The player's stamina before they collapsed. private float PreCollapseStamina; /// The player's health before they collapsed. private int PreCollapseHealth; /// /// Checks if the player was bathing or not before passing out. /// private bool isBathing; /// /// Checks if the player was in their swimsuit before passing out. /// private bool isInSwimSuit; /// /// The horse the player was riding before they collapsed. /// private Horse horse; /// /// Determines whehther or not to rewarp the player's horse to them. /// private bool shouldWarpHorse; /// /// Event in the night taht simulates the earthquake event that should happen. /// StardewValley.Events.SoundInTheNightEvent eve; private List oldAnimalHappiness; /********* ** Public methods *********/ /// The mod entry point, called after the mod is first loaded. /// Provides simplified APIs for writing mods. public override void Entry(IModHelper helper) { this.oldAnimalHappiness = new List(); this.Config = helper.ReadConfig(); if (Config.UseInternalNightFishAssetEditor) { this.Helper.Content.AssetEditors.Add(new NightFishing()); } TimeEvents.TimeOfDayChanged += this.TimeEvents_TimeOfDayChanged; TimeEvents.AfterDayStarted += this.TimeEvents_AfterDayStarted; SaveEvents.AfterLoad += this.SaveEvents_AfterLoad; SaveEvents.BeforeSave += this.SaveEvents_BeforeSave; GameEvents.FourthUpdateTick += this.GameEvents_FourthUpdateTick; GameEvents.UpdateTick += GameEvents_UpdateTick; shouldWarpHorse = false; } /********* ** Private methods *********/ /// /// Updates the earthquake event. /// /// /// private void GameEvents_UpdateTick(object sender, EventArgs e) { if (eve == null) return; else { eve.tickUpdate(Game1.currentGameTime); } } /// The method invoked every fourth game update (roughly 15 times per second). /// The event sender. /// The event data. public void GameEvents_FourthUpdateTick(object sender, EventArgs e) { try { // reset position after collapse if (Context.IsWorldReady && this.JustStartedNewDay && this.Config.KeepPositionAfterCollapse) { if (this.PreCollapseMap != null) Game1.warpFarmer(this.PreCollapseMap, this.PreCollapseTile.X, this.PreCollapseTile.Y, false); this.PreCollapseMap = null; this.JustStartedNewDay = false; this.JustCollapsed = false; } } catch (Exception ex) { this.Monitor.Log(ex.ToString(), LogLevel.Error); this.WriteErrorLog(); } } /// The method invoked before the game saves. /// The event sender. /// The event data. public void SaveEvents_BeforeSave(object sender, EventArgs e) { int collapseFee = 0; string[] passOutFees = Game1.player.mailbox .Where(p => p.Contains("passedOut")) .ToArray(); for (int idx=0; idx< passOutFees.Length; idx++) { string[] msg = passOutFees[idx].Split(' '); collapseFee += Int32.Parse(msg[1]); } if (this.Config.KeepMoneyAfterCollapse) { Game1.player.money += collapseFee; } } /// The method invoked after the player loads a save. /// The event sender. /// The event data. public void SaveEvents_AfterLoad(object sender, EventArgs e) { this.IsUpLate = false; this.JustStartedNewDay = false; this.JustCollapsed = false; } /// The method invoked when a new day starts. /// The event sender. /// The event data. public void TimeEvents_AfterDayStarted(object sender, EventArgs e) { try { // reset data this.IsUpLate = false; // transition to the next day if (this.ShouldResetPlayerAfterCollapseNow) { this.ShouldResetPlayerAfterCollapseNow = false; if (this.Config.KeepStaminaAfterCollapse) Game1.player.stamina = this.PreCollapseStamina; if (this.Config.KeepHealthAfterCollapse) Game1.player.health = this.PreCollapseHealth; if (this.Config.KeepPositionAfterCollapse) if (Game1.weddingToday == false) { Game1.warpFarmer(this.PreCollapseMap, this.PreCollapseTile.X, this.PreCollapseTile.Y, false); } if (horse != null && shouldWarpHorse==true) { Game1.warpCharacter(horse, Game1.player.currentLocation, Game1.player.position); shouldWarpHorse = false; } if (isInSwimSuit) { Game1.player.changeIntoSwimsuit(); } if (isBathing) { Game1.player.swimming.Value = true; } //Reflction to ensure that the railroad becomes properly unblocked. if (Game1.dayOfMonth == 1 && Game1.currentSeason == "summer" && Game1.year == 1) { Mountain mountain = (Mountain)Game1.getLocationFromName("Mountain"); var reflect2 = Helper.Reflection.GetField(mountain, "railroadAreaBlocked", true); var netBool2 = reflect2.GetValue(); netBool2.Value = false; reflect2.SetValue(netBool2); var reflect3 = Helper.Reflection.GetField(mountain, "railroadBlockRect", true); var netBool3 = reflect3.GetValue(); netBool3 = new Rectangle(0, 0, 0, 0); reflect3.SetValue(netBool3); eve = new StardewValley.Events.SoundInTheNightEvent(4); eve.setUp(); eve.makeChangesToLocation(); } } if(Game1.currentSeason!="spring" && Game1.year >= 1) { clearRailRoadBlock(); } // delete annoying charge messages (if only I could do this with mail IRL) if (this.Config.SkipCollapseMail) { string[] validMail = Game1.mailbox .Where(p => !p.Contains("passedOut")) .ToArray(); Game1.mailbox.Clear(); foreach (string mail in validMail) Game1.mailbox.Add(mail); } this.JustStartedNewDay = true; } catch (Exception ex) { this.Monitor.Log(ex.ToString(), LogLevel.Error); this.WriteErrorLog(); } } /// /// If the user for this mod never gets the event that makes the railroad blok go away we will always force it to go away if they have met the conditions for it. I.E not being in spring of year 1. /// private void clearRailRoadBlock() { Mountain mountain = (Mountain)Game1.getLocationFromName("Mountain"); var reflect2 = Helper.Reflection.GetField(mountain, "railroadAreaBlocked", true); var netBool2 = reflect2.GetValue(); netBool2.Value = false; reflect2.SetValue(netBool2); var reflect3 = Helper.Reflection.GetField(mountain, "railroadBlockRect", true); var netBool3 = reflect3.GetValue(); netBool3 = new Rectangle(0, 0, 0, 0); reflect3.SetValue(netBool3); } /// The method invoked when changes. /// The event sender. /// The event data. private void TimeEvents_TimeOfDayChanged(object sender, EventArgsIntChanged e) { if (!Context.IsWorldReady) return; try { // transition morning light more realistically if (this.Config.MorningLightTransition && Game1.timeOfDay > 400 && Game1.timeOfDay < 600) { float colorMod = (1300 - Game1.timeOfDay) / 1000f; Game1.outdoorLight = Game1.ambientLight * colorMod; } // transition to next morning if (this.Config.StayUp && Game1.timeOfDay == 2550) { Game1.isRaining = false; // remove rain, otherwise lighting gets screwy Game1.updateWeatherIcon(); Game1.timeOfDay = 150; //change it from 1:50 am late, to 1:50 am early foreach(FarmAnimal animal in Game1.getFarm().getAllFarmAnimals()) { this.oldAnimalHappiness.Add(animal.happiness); } } // collapse player at 6am to save & reset if (Game1.timeOfDay == 550) this.IsUpLate = true; if (this.IsUpLate && Game1.timeOfDay == 600 && !this.JustCollapsed) { if (Game1.player.isRidingHorse()) { foreach (var character in Game1.player.currentLocation.characters) { try { if (character is Horse) { (character as Horse).dismount(); horse = (character as Horse); shouldWarpHorse = true; } } catch (Exception err) { } } } this.JustCollapsed = true; this.ShouldResetPlayerAfterCollapseNow = true; this.PreCollapseTile = new Point(Game1.player.getTileX(), Game1.player.getTileY()); this.PreCollapseMap = Game1.player.currentLocation.Name; this.PreCollapseStamina = Game1.player.stamina; this.PreCollapseHealth = Game1.player.health; this.PreCollapseMoney = Game1.player.money; this.isInSwimSuit = Game1.player.bathingClothes.Value; this.isBathing = Game1.player.swimming.Value; if (Game1.currentMinigame != null) Game1.currentMinigame = null; if (Game1.activeClickableMenu != null) Game1.activeClickableMenu.exitThisMenu(true); //Exit menus. Game1.timeOfDay += 2400; //Recalculate for the sake of technically being up a whole day. //Reset animal happiness since it drains over night. for(int i=0; i < oldAnimalHappiness.Count; i++) { Game1.getFarm().getAllFarmAnimals()[i].happiness.Value = oldAnimalHappiness[i].Value; } Game1.player.startToPassOut(); } } catch (Exception ex) { this.Monitor.Log(ex.ToString(), LogLevel.Error); this.WriteErrorLog(); } } /// Write the current mod state to the error log file. private void WriteErrorLog() { var state = new { this.Config, this.IsUpLate, this.ShouldResetPlayerAfterCollapseNow, this.JustStartedNewDay, this.JustCollapsed, this.PreCollapseMap, this.PreCollapseTile, this.PreCollapseMoney, this.PreCollapseStamina, this.PreCollapseHealth }; string path = Path.Combine(this.Helper.DirectoryPath, "Error_Logs", "Mod_State.json"); this.Helper.WriteJsonFile(path, state); } /// /// Try and emulate the old Game1.shouldFarmerPassout logic. /// /// public bool shouldFarmerPassout() { if (Game1.player.stamina <= 0 || Game1.player.health <= 0 || Game1.timeOfDay >= 2600) return true; else return false; } } }