So this condition is question called "WhenPCRests" is simply meant to track through currently existing events when and for how long the player has Rested. But the system I was using to track the hours is not working as I'd expect. That being the tracked value is only persisting and accurate AFTER the condition has been set, but before it is set everytime the incrementing happens the tracking values return to 0, and I have not much clue why.
I copied nearly the entire structure from the other quest condition "WhenPCEntersExits", which appears to work in a similar manner, but does not have a tracked number values.
Code: Select all
using DaggerfallWorkshop.Game.Entity;
using DaggerfallWorkshop.Game.Questing;
using System.Text.RegularExpressions;
using FullSerializer;
using DaggerfallWorkshop.Game.Serialization;
using DaggerfallWorkshop.Game.Utility;
using DaggerfallWorkshop.Game.UserInterfaceWindows;
namespace QuestMakersToolbox
{
/// <summary>
/// Triggers when the player rests (only counts proper sleeping hours, NOT loitering.)
/// If providing no specific hours, default behavior is to trigger condition as soon as one rest "tick" occurs, or any hour of proper rest basically.
/// When an hour value followed by 'consecutive hours' will only trigger condition when player has slept those number of hours consecutively in one rest cycle, capped at 99.
/// When an hour value followed by 'hours' will trigger condition when player has slept those TOTAL number of hours, can span multiple rest cycles.
/// </summary>
public class WhenPCRests : ActionTemplate
{
int totalRestHoursNeeded;
int hoursConsecutive;
int hourTracker;
int consecutiveTracker;
// Example match: "trigger if pc rests 8 consecutive hours"
// Example match: "trigger if pc rests 20 hours"
// Example match: "trigger if pc rests"
public override string Pattern
{
get
{
return @"trigger if pc rests (?<consHours>\d+) consecutive hours|" +
@"trigger if pc rests (?<restHours>\d+) hours|" +
@"trigger if pc rests";
}
}
public WhenPCRests(Quest parentQuest)
: base(parentQuest)
{
IsTriggerCondition = true;
}
public override IQuestAction CreateNew(string source, Quest parentQuest)
{
// Source must match pattern
Match match = Test(source);
if (!match.Success)
return null;
// Factory new action
WhenPCRests action = new WhenPCRests(parentQuest);
action.totalRestHoursNeeded = Parser.ParseInt(match.Groups["restHours"].Value);
action.hoursConsecutive = Parser.ParseInt(match.Groups["consHours"].Value);
action.hourTracker = 0;
action.consecutiveTracker = 0;
if (action.totalRestHoursNeeded < 0)
action.totalRestHoursNeeded = 0;
if (action.hoursConsecutive < 0)
action.hoursConsecutive = 0;
else if (action.hoursConsecutive > 99)
action.hoursConsecutive = 99;
// Register events when creating action
action.RegisterEvents();
return action;
}
public override bool CheckTrigger(Task caller)
{
// Check if current rested hours tracked value is within range to trigger condition
if (totalRestHoursNeeded <= 0 && hoursConsecutive <= 0 && hourTracker > 0)
return true;
if (hoursConsecutive > 0 && consecutiveTracker >= hoursConsecutive)
return true;
if (totalRestHoursNeeded > 0 && hourTracker >= totalRestHoursNeeded)
return true;
return false;
}
public override void RearmAction()
{
base.RearmAction();
hourTracker = 0;
consecutiveTracker = 0;
}
public override void Dispose()
{
base.Dispose();
// Unregister events when quest ends
UnregisterEvents();
}
#region Private Methods
private void RegisterEvents()
{
DaggerfallRestWindow.OnSleepTick += IncreaseHourTracker_OnSleepTick;
DaggerfallRestWindow.OnSleepEnd += ResetConsecutiveTracker_OnSleepEnd;
SaveLoadManager.OnStartLoad += SaveLoadManager_OnStartLoad;
StartGameBehaviour.OnNewGame += StartGameBehaviour_OnNewGame;
}
private void UnregisterEvents()
{
DaggerfallRestWindow.OnSleepTick -= IncreaseHourTracker_OnSleepTick;
DaggerfallRestWindow.OnSleepEnd -= ResetConsecutiveTracker_OnSleepEnd;
SaveLoadManager.OnStartLoad -= SaveLoadManager_OnStartLoad;
StartGameBehaviour.OnNewGame -= StartGameBehaviour_OnNewGame;
}
#endregion
#region Events Handlers
private void IncreaseHourTracker_OnSleepTick()
{
hourTracker++;
consecutiveTracker++;
}
private void ResetConsecutiveTracker_OnSleepEnd()
{
consecutiveTracker = 0; // Could add additional logic to this eventually, like to specify different amounts of completed "rest cycles" or something, but for now it's probably fine.
}
private void StartGameBehaviour_OnNewGame()
{
UnregisterEvents();
}
private void SaveLoadManager_OnStartLoad(DaggerfallWorkshop.Game.Serialization.SaveData_v1 saveData)
{
UnregisterEvents();
}
#endregion
#region Serialization
[fsObject("v1")]
public struct SaveData_v1
{
public int totalRestHoursNeeded;
public int hoursConsecutive;
public int hourTracker;
public int consecutiveTracker;
}
public override object GetSaveData()
{
SaveData_v1 data = new SaveData_v1();
data.totalRestHoursNeeded = totalRestHoursNeeded;
data.hoursConsecutive = hoursConsecutive;
data.hourTracker = hourTracker;
data.consecutiveTracker = consecutiveTracker;
return data;
}
public override void RestoreSaveData(object dataIn)
{
if (dataIn == null)
return;
SaveData_v1 data = (SaveData_v1)dataIn;
totalRestHoursNeeded = data.totalRestHoursNeeded;
hoursConsecutive = data.hoursConsecutive;
hourTracker = data.hourTracker;
consecutiveTracker = data.consecutiveTracker;
// Register events when restoring action
RegisterEvents();
}
#endregion
}
}