Skip to content

Commit

Permalink
Merge branch 'hotfix-aging'
Browse files Browse the repository at this point in the history
  • Loading branch information
dymanoid committed Aug 6, 2018
2 parents d60aa5d + e41adf7 commit 092bb5b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
22 changes: 14 additions & 8 deletions src/RealTime/CustomAI/RealTimeResidentAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,26 @@ public void RegisterCitizenArrival(uint citizenId)
schedule.DepartureToWorkTime = default;
}

/// <summary>Performs simulation for starting a new day. Enables the logic to perform the 'new day processing' for the citizens.</summary>
public void BeginNewDayProcessing()
/// <summary>Performs simulation for starting a day cycle beginning with specified hour.
/// Enables the logic to perform the 'new cycle processing' for the citizens.</summary>
/// <param name="hour">The hour of the cycle.</param>
public void BeginNewHourCycleProcessing(int hour)
{
Log.Debug(LogCategory.Generic, TimeInfo.Now, "Starting of the 'new day' processing for each citizen...");
workBehavior.BeginNewDay();
todayWakeUp = TimeInfo.Now.Date.AddHours(Config.WakeUpHour);
Log.Debug(LogCategory.Generic, TimeInfo.Now, "Starting of the 'new cycle' processing for each citizen...");
if (hour == 0)
{
workBehavior.BeginNewDay();
todayWakeUp = TimeInfo.Now.Date.AddHours(Config.WakeUpHour);
}

CanCitizensGrowUp = true;
}

/// <summary>Disables the 'new day processing' for the citizens.</summary>
public void EndNewDayProcessing()
/// <summary>Disables the 'new cycle processing' for the citizens.</summary>
public void EndHourCycleProcessing()
{
CanCitizensGrowUp = false;
Log.Debug(LogCategory.Generic, TimeInfo.Now, "The 'new day' processing for the citizens is now completed.");
Log.Debug(LogCategory.Generic, TimeInfo.Now, "The 'new cycle' processing for the citizens is now completed.");
}

/// <summary>Performs simulation for starting a new day for a citizen with specified ID.</summary>
Expand Down
30 changes: 18 additions & 12 deletions src/RealTime/Simulation/CitizenProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ internal sealed class CitizenProcessor<TAI, TCitizen>
private readonly RealTimeResidentAI<TAI, TCitizen> residentAI;
private readonly SpareTimeBehavior spareTimeBehavior;
private readonly ITimeInfo timeInfo;
private int dayStartFrame;
private int cycleStartFrame;
private int cycleHour;

/// <summary>Initializes a new instance of the <see cref="CitizenProcessor{TAI, TCitizen}"/> class.</summary>
/// <param name="residentAI">The custom resident AI implementation.</param>
Expand All @@ -34,13 +35,18 @@ public CitizenProcessor(RealTimeResidentAI<TAI, TCitizen> residentAI, SpareTimeB
this.residentAI = residentAI ?? throw new ArgumentNullException(nameof(residentAI));
this.spareTimeBehavior = spareTimeBehavior ?? throw new ArgumentNullException(nameof(spareTimeBehavior));
this.timeInfo = timeInfo ?? throw new ArgumentNullException(nameof(timeInfo));
dayStartFrame = int.MinValue;
cycleStartFrame = int.MinValue;
}

/// <summary>Notifies this simulation object that a new game day begins.</summary>
public void StartNewDay()
/// <summary>Notifies this simulation object that a particular day hour begins.</summary>
/// <param name="hour">The day time hour.</param>
public void TriggerHour(int hour)
{
dayStartFrame = int.MinValue;
if (hour % 8 == 0)
{
cycleHour = hour;
cycleStartFrame = int.MinValue;
}
}

/// <summary>Re-calculates the duration of a simulation frame.</summary>
Expand All @@ -61,21 +67,21 @@ public void ProcessTick()
/// <param name="frameIndex">The index of the simulation frame to process.</param>
public void ProcessFrame(uint frameIndex)
{
if (dayStartFrame == -1)
if (cycleStartFrame == -1)
{
return;
}

uint step = frameIndex & StepMask;
if (dayStartFrame == int.MinValue)
if (cycleStartFrame == int.MinValue)
{
residentAI.BeginNewDayProcessing();
dayStartFrame = (int)step;
residentAI.BeginNewHourCycleProcessing(cycleHour);
cycleStartFrame = (int)step;
}
else if (step == dayStartFrame)
else if (step == cycleStartFrame)
{
residentAI.EndNewDayProcessing();
dayStartFrame = -1;
residentAI.EndHourCycleProcessing();
cycleStartFrame = -1;
return;
}

Expand Down
19 changes: 13 additions & 6 deletions src/RealTime/Simulation/SimulationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,20 @@ public override void OnBeforeSimulationTick()
return;
}

DateTime currentDate = SimulationManager.instance.m_currentGameTime.Date;
if (currentDate != lastHandledDate)
DateTime currentDateTime = SimulationManager.instance.m_currentGameTime;
if (currentDateTime.Hour != lastHandledDate.Hour || lastHandledDate == default)
{
lastHandledDate = currentDate;
DayTimeSimulation.Process(currentDate);
CitizenProcessor.StartNewDay();
OnNewDay(this);
int triggerHour = lastHandledDate == default
? 0
: currentDateTime.Hour;

lastHandledDate = currentDateTime;
CitizenProcessor.TriggerHour(triggerHour);
if (triggerHour == 0)
{
DayTimeSimulation.Process(currentDateTime);
OnNewDay(this);
}
}
}

Expand Down

0 comments on commit 092bb5b

Please sign in to comment.