2018-12-30 18:00:05 +08:00
|
|
|
using System;
|
2016-10-11 15:28:18 +08:00
|
|
|
using System.IO;
|
2017-07-31 11:07:07 +08:00
|
|
|
using Omegasis.BuildEndurance.Framework;
|
2016-10-11 15:28:18 +08:00
|
|
|
using StardewModdingAPI;
|
2017-07-30 02:05:24 +08:00
|
|
|
using StardewModdingAPI.Events;
|
|
|
|
using StardewValley;
|
2017-05-04 05:00:17 +08:00
|
|
|
|
2017-07-28 08:28:39 +08:00
|
|
|
namespace Omegasis.BuildEndurance
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
/// <summary>The mod entry point.</summary>
|
2016-10-11 15:28:18 +08:00
|
|
|
public class BuildEndurance : Mod
|
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
/*********
|
|
|
|
** Properties
|
|
|
|
*********/
|
2017-08-06 03:51:44 +08:00
|
|
|
/// <summary>The relative path for the current player's data file.</summary>
|
|
|
|
private string DataFilePath => Path.Combine("data", $"{Constants.SaveFolderName}.json");
|
|
|
|
|
|
|
|
/// <summary>The mod settings.</summary>
|
2017-07-31 11:07:07 +08:00
|
|
|
private ModConfig Config;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
/// <summary>The data for the current player.</summary>
|
|
|
|
private PlayerData PlayerData;
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
/// <summary>Whether the player has been exhausted today.</summary>
|
|
|
|
private bool WasExhausted;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
/// <summary>Whether the player has collapsed today.</summary>
|
|
|
|
private bool WasCollapsed;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
/// <summary>Whether the player recently gained XP for tool use.</summary>
|
|
|
|
private bool HasRecentToolExp;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
/// <summary>Whether the player was eating last time we checked.</summary>
|
|
|
|
private bool WasEating;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
2018-05-01 09:21:31 +08:00
|
|
|
public IModHelper ModHelper;
|
|
|
|
public IMonitor ModMonitor;
|
|
|
|
|
2018-12-31 08:45:04 +08:00
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
/*********
|
|
|
|
** Public methods
|
|
|
|
*********/
|
|
|
|
/// <summary>The mod entry point, called after the mod is first loaded.</summary>
|
|
|
|
/// <param name="helper">Provides simplified APIs for writing mods.</param>
|
|
|
|
public override void Entry(IModHelper helper)
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
this.Config = helper.ReadConfig<ModConfig>();
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
GameEvents.UpdateTick += this.GameEvents_UpdateTick;
|
|
|
|
GameEvents.OneSecondTick += this.GameEvents_OneSecondTick;
|
|
|
|
SaveEvents.AfterLoad += this.SaveEvents_AfterLoad;
|
2017-08-06 03:51:44 +08:00
|
|
|
SaveEvents.BeforeSave += this.SaveEvents_BeforeSave;
|
2018-05-01 09:21:31 +08:00
|
|
|
|
|
|
|
this.ModHelper = this.Helper;
|
|
|
|
this.ModMonitor = this.Monitor;
|
2016-10-11 15:28:18 +08:00
|
|
|
}
|
2017-05-04 05:00:17 +08:00
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
|
|
|
/*********
|
|
|
|
** Private methods
|
|
|
|
*********/
|
2017-07-30 02:05:24 +08:00
|
|
|
/// <summary>The method invoked once per second during a game update.</summary>
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
private void GameEvents_OneSecondTick(object sender, EventArgs e)
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
// nerf how quickly tool xp is gained (I hope)
|
|
|
|
if (this.HasRecentToolExp)
|
|
|
|
this.HasRecentToolExp = false;
|
2016-10-11 15:28:18 +08:00
|
|
|
}
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
/// <summary>The method invoked when the game updates (roughly 60 times per second).</summary>
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
private void GameEvents_UpdateTick(object sender, EventArgs e)
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
if (!Context.IsWorldReady)
|
|
|
|
return;
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
// give XP when player finishes eating
|
2018-05-01 09:21:31 +08:00
|
|
|
if (Game1.player.isEating)
|
2017-07-30 02:05:24 +08:00
|
|
|
this.WasEating = true;
|
|
|
|
else if (this.WasEating)
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForEating;
|
2017-07-30 02:05:24 +08:00
|
|
|
this.WasEating = false;
|
2016-10-11 15:28:18 +08:00
|
|
|
}
|
2017-07-30 02:05:24 +08:00
|
|
|
|
|
|
|
// give XP when player uses tool
|
2018-05-01 09:21:31 +08:00
|
|
|
if (!this.HasRecentToolExp && Game1.player.UsingTool)
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForToolUse;
|
2017-07-30 02:05:24 +08:00
|
|
|
this.HasRecentToolExp = true;
|
2016-10-11 15:28:18 +08:00
|
|
|
}
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
// give XP when exhausted
|
2018-06-26 12:13:40 +08:00
|
|
|
if (!this.WasExhausted && Game1.player.exhausted.Value)
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForExhaustion;
|
2017-07-30 02:05:24 +08:00
|
|
|
this.WasExhausted = true;
|
2018-05-01 09:21:31 +08:00
|
|
|
//this.Monitor.Log("The player is exhausted");
|
2016-10-11 15:28:18 +08:00
|
|
|
}
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
// give XP when player stays up too late or collapses
|
2018-12-30 18:00:05 +08:00
|
|
|
if (!this.WasCollapsed && this.shouldFarmerPassout())
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForCollapsing;
|
2017-07-30 02:05:24 +08:00
|
|
|
this.WasCollapsed = true;
|
2018-05-01 09:21:31 +08:00
|
|
|
//this.Monitor.Log("The player has collapsed!");
|
2016-10-11 15:28:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
/// <summary>The method invoked after the player loads a save.</summary>
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
private void SaveEvents_AfterLoad(object sender, EventArgs e)
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2017-08-06 11:00:37 +08:00
|
|
|
// reset state
|
|
|
|
this.WasExhausted = false;
|
|
|
|
this.WasCollapsed = false;
|
|
|
|
this.HasRecentToolExp = false;
|
|
|
|
this.WasEating = false;
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
// load player data
|
|
|
|
this.PlayerData = this.Helper.ReadJsonFile<PlayerData>(this.DataFilePath) ?? new PlayerData();
|
|
|
|
if (this.PlayerData.OriginalMaxStamina == 0)
|
|
|
|
this.PlayerData.OriginalMaxStamina = Game1.player.MaxStamina;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
|
|
|
// reset if needed
|
2017-08-06 03:51:44 +08:00
|
|
|
if (this.PlayerData.ClearModEffects)
|
|
|
|
{
|
|
|
|
Game1.player.MaxStamina = this.PlayerData.OriginalMaxStamina;
|
|
|
|
this.PlayerData.ExpToNextLevel = this.Config.ExpToNextLevel;
|
|
|
|
this.PlayerData.CurrentExp = this.Config.CurrentExp;
|
|
|
|
this.PlayerData.CurrentLevelStaminaBonus = 0;
|
|
|
|
this.PlayerData.OriginalMaxStamina = Game1.player.MaxStamina;
|
|
|
|
this.PlayerData.BaseStaminaBonus = 0;
|
|
|
|
this.PlayerData.CurrentLevel = 0;
|
|
|
|
this.PlayerData.ClearModEffects = false;
|
|
|
|
}
|
2017-07-30 02:05:24 +08:00
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
// else apply stamina
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Game1.player.MaxStamina = this.PlayerData.NightlyStamina <= 0
|
|
|
|
? this.PlayerData.BaseStaminaBonus + this.PlayerData.CurrentLevelStaminaBonus + this.PlayerData.OriginalMaxStamina
|
|
|
|
: this.PlayerData.NightlyStamina;
|
|
|
|
}
|
2016-10-11 15:28:18 +08:00
|
|
|
}
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
/// <summary>The method invoked just before the game is saved.</summary>
|
2017-07-30 02:05:24 +08:00
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
private void SaveEvents_BeforeSave(object sender, EventArgs e)
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
// reset data
|
|
|
|
this.WasExhausted = false;
|
|
|
|
this.WasCollapsed = false;
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
// update player data
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForSleeping;
|
|
|
|
if (this.PlayerData.OriginalMaxStamina == 0)
|
|
|
|
this.PlayerData.OriginalMaxStamina = Game1.player.MaxStamina; //grab the initial stamina value
|
2016-10-11 15:28:18 +08:00
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
if (this.PlayerData.CurrentLevel < this.Config.MaxLevel)
|
2017-07-30 02:05:24 +08:00
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
while (this.PlayerData.CurrentExp >= this.PlayerData.ExpToNextLevel)
|
2016-10-11 15:28:18 +08:00
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
this.PlayerData.CurrentLevel += 1;
|
|
|
|
this.PlayerData.CurrentExp = this.PlayerData.CurrentExp - this.PlayerData.ExpToNextLevel;
|
|
|
|
this.PlayerData.ExpToNextLevel = (this.Config.ExpCurve * this.PlayerData.ExpToNextLevel);
|
|
|
|
Game1.player.MaxStamina += this.Config.StaminaIncreasePerLevel;
|
|
|
|
this.PlayerData.CurrentLevelStaminaBonus += this.Config.StaminaIncreasePerLevel;
|
2016-10-11 15:28:18 +08:00
|
|
|
}
|
|
|
|
}
|
2017-08-06 03:51:44 +08:00
|
|
|
this.PlayerData.ClearModEffects = false;
|
2018-06-26 12:13:40 +08:00
|
|
|
this.PlayerData.NightlyStamina = Game1.player.MaxStamina;
|
2016-10-11 15:28:18 +08:00
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
// save data
|
|
|
|
this.Helper.WriteJsonFile(this.DataFilePath, this.PlayerData);
|
2016-10-11 15:28:18 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Try and emulate the old Game1.shouldFarmerPassout logic.</summary>
|
2018-06-26 12:13:40 +08:00
|
|
|
public bool shouldFarmerPassout()
|
|
|
|
{
|
|
|
|
if (Game1.player.stamina <= 0 || Game1.player.health <= 0 || Game1.timeOfDay >= 2600) return true;
|
|
|
|
else return false;
|
|
|
|
}
|
2017-07-30 02:05:24 +08:00
|
|
|
}
|
|
|
|
}
|