using Omegasis.TimeFreeze.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Locations;
namespace Omegasis.TimeFreeze
{
/// The mod entry point.
public class TimeFreeze : Mod
{
/*********
** Fields
*********/
/// The mod configuration.
private ModConfig Config;
/*********
** 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.Config = helper.ReadConfig();
helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
helper.Events.GameLoop.SaveLoaded += this.GameLoop_SaveLoaded;
}
private void GameLoop_SaveLoaded(object sender, SaveLoadedEventArgs e)
{
foreach (GameLocation loc in Game1.locations)
{
if (this.Config.freezeTimeInThisLocation.ContainsKey(loc.Name) == false)
{
if (loc.IsOutdoors)
{
this.Config.freezeTimeInThisLocation.Add(loc.Name, false);
}
else
{
this.Config.freezeTimeInThisLocation.Add(loc.Name, true);
}
}
}
//Patch in the underground mine shaft.
if (this.Config.freezeTimeInThisLocation.ContainsKey("UndergroundMine") == false)
{
this.Config.freezeTimeInThisLocation.Add("UndergroundMine", true);
}
this.Helper.WriteConfig(this.Config);
}
/*********
** Private methods
*********/
/// Raised after the game state is updated (≈60 times per second).
/// The event sender.
/// The event arguments.
private void OnUpdateTicked(object sender, UpdateTickedEventArgs e)
{
if (!Context.IsWorldReady)
return;
/*
if (Game1.gameTimeInterval != 0)
{
oldInterval = Game1.gameTimeInterval;
if (oldInterval < 3)
{
oldInterval = 7;
}
}
*/
if (Game1.IsMultiplayer)
{
if (this.Config.freezeIfEvenOnePlayerMeetsTimeFreezeConditions)
{
//bool isAnyFarmerSuitable = false;
foreach (Farmer farmer in Game1.getOnlineFarmers())
{
if (this.ShouldFreezeTime(farmer, farmer.currentLocation))
{
Game1.gameTimeInterval = 0;
//isAnyFarmerSuitable = true;
}
}
//if (!isAnyFarmerSuitable)
//{
// Game1.gameTimeInterval += Game1.currentGameTime.ElapsedGameTime.Milliseconds;
//}
}
else if (this.Config.freezeIfMajorityPlayersMeetsTimeFreezeConditions)
{
int freezeCount = 0;
int playerCount = 0;
foreach (Farmer farmer in Game1.getOnlineFarmers())
{
playerCount++;
if (this.ShouldFreezeTime(farmer, farmer.currentLocation))
{
//Game1.gameTimeInterval = 0;
freezeCount++;
}
}
if (freezeCount >= (playerCount / 2))
Game1.gameTimeInterval = 0;
//else
// Game1.gameTimeInterval += Game1.currentGameTime.ElapsedGameTime.Milliseconds;
}
else if (this.Config.freezeIfAllPlayersMeetTimeFreezeConditions)
{
int freezeCount = 0;
int playerCount = 0;
foreach (Farmer farmer in Game1.getOnlineFarmers())
{
playerCount++;
if (this.ShouldFreezeTime(farmer, farmer.currentLocation))
{
//Game1.gameTimeInterval = 0;
freezeCount++;
}
}
if (freezeCount >= playerCount)
Game1.gameTimeInterval = 0;
//else
// Game1.gameTimeInterval = this.oldInterval;
}
}
else
{
Farmer player = Game1.player;
if (this.ShouldFreezeTime(player, player.currentLocation))
{
Game1.gameTimeInterval = 0;
}
//else
// Game1.gameTimeInterval = this.oldInterval;
}
}
/// Get whether time should be frozen for the player at the given location.
/// The player to check.
/// The location to check.
private bool ShouldFreezeTime(Farmer player, GameLocation location)
{
if (Game1.showingEndOfNightStuff) return false;
if (this.Config.freezeTimeInThisLocation.ContainsKey(location.Name))
{
if (location.Name.Equals("SkullCave") || location.Name.StartsWith("SkullCave"))
{
return this.Config.freezeTimeInThisLocation["SkullCave"];
}
if (player.swimming.Value)
{
if (this.Config.PassTimeWhileSwimmingInBathhouse && location is BathHousePool)
return false;
}
return this.Config.freezeTimeInThisLocation[location.Name];
}
if (location.NameOrUniqueName.StartsWith("UndergroundMine"))
{
return this.Config.freezeTimeInThisLocation["UndergroundMine"];
}
//Skull cave check.
if (location.Name.Equals("SkullCave") || location.Name.StartsWith("SkullCave"))
{
return this.Config.freezeTimeInThisLocation["SkullCave"];
}
//this.Monitor.Log(Game1.player.currentLocation.NameOrUniqueName, LogLevel.Info);
//If for some reason the location wasn't accounted for then just freeze time there by default.
if (location.IsOutdoors)
return false;
else
{
return true;
}
}
}
}