Skip to content

Commit

Permalink
Perform re-scheduling at home to eliminate invalid/outdated schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
dymanoid committed Jul 24, 2018
1 parent e4312b0 commit ae0a2ea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/RealTime/CustomAI/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal static class Constants
public const float PrepareToWorkHours = 1f;

/// <summary>An assumed maximum travel time to a target building.</summary>
public const float MaxTravelTime = 4f;
public const float MaxTravelTime = 2.5f;

/// <summary>An assumed minimum travel to a target building.</summary>
public const float MinTravelTime = 0.25f;
Expand All @@ -54,6 +54,7 @@ internal static class Constants
/// An assumed average speed of a citizen when moving to target (this is not just a walking speed, but also takes
/// into account moving by car or public transport).
/// </summary>
// TODO: calculate this dynamically depending on time speed
public const float OnTheWayDistancePerHour = 500f;

/// <summary>A chance in percent that a virtual citizen will not be realized in 'few virtual citizens' mode.</summary>
Expand Down
5 changes: 4 additions & 1 deletion src/RealTime/CustomAI/RealTimeResidentAI.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ private void ExecuteCitizenSchedule(ref CitizenSchedule schedule, TAI instance,
{
if (ProcessCurrentState(ref schedule, ref citizen) && schedule.ScheduledState == ResidentState.Unknown)
{
Log.Debug(TimeInfo.Now, $"{GetCitizenDesc(citizenId, ref citizen)} will be rescheduled now");
Log.Debug(TimeInfo.Now, $"{GetCitizenDesc(citizenId, ref citizen)} will be re-scheduled now");

// If the state processing changed the schedule, we need to update it
UpdateCitizenSchedule(ref schedule, citizenId, ref citizen);
Expand Down Expand Up @@ -371,6 +371,9 @@ private bool ProcessCurrentState(ref CitizenSchedule schedule, ref TCitizen citi
{
switch (schedule.CurrentState)
{
case ResidentState.AtHome:
return RescheduleAtHome(ref schedule, ref citizen);

case ResidentState.Shopping:
return ProcessCitizenShopping(ref schedule, ref citizen);

Expand Down
30 changes: 30 additions & 0 deletions src/RealTime/CustomAI/RealTimeResidentAI.Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,35 @@ private void DoScheduledHome(ref CitizenSchedule schedule, TAI instance, uint ci
schedule.Schedule(ResidentState.Unknown, default);
Log.Debug(TimeInfo.Now, $"{GetCitizenDesc(citizenId, ref citizen)} is going from {currentBuilding} back home");
}

private bool RescheduleAtHome(ref CitizenSchedule schedule, ref TCitizen citizen)
{
if (schedule.CurrentState != ResidentState.AtHome || TimeInfo.Now < schedule.ScheduledStateTime)
{
return false;
}

if (schedule.ScheduledState != ResidentState.Relaxing && schedule.ScheduledState != ResidentState.Shopping)
{
return false;
}

if (IsBadWeather())
{
Log.Debug(TimeInfo.Now, $"{GetCitizenDesc(0, ref citizen)} re-schedules an activity because of bad weather (see next line for citizen ID)");
schedule.Schedule(ResidentState.Unknown, default);
return true;
}

uint goOutChance = spareTimeBehavior.GetGoOutChance(CitizenProxy.GetAge(ref citizen));
if (Random.ShouldOccur(goOutChance))
{
return false;
}

Log.Debug(TimeInfo.Now, $"{GetCitizenDesc(0, ref citizen)} re-schedules an activity because of time (see next line for citizen ID)");
schedule.Schedule(ResidentState.Unknown, default);
return true;
}
}
}
12 changes: 8 additions & 4 deletions src/RealTime/CustomAI/RealTimeResidentAI.Visit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,15 @@ private bool IsBuildingNoiseRestricted(ushort targetBuilding, ushort currentBuil

private bool RescheduleVisit(ref CitizenSchedule schedule, ref TCitizen citizen, ushort currentBuilding)
{
if (schedule.ScheduledState != ResidentState.Relaxing
&& schedule.ScheduledState != ResidentState.Shopping
&& schedule.ScheduledState != ResidentState.Visiting)
switch (schedule.ScheduledState)
{
return false;
case ResidentState.Shopping:
case ResidentState.Relaxing:
case ResidentState.Visiting:
break;

default:
return false;
}

if (IsBadWeather())
Expand Down

0 comments on commit ae0a2ea

Please sign in to comment.