Skip to content

Commit

Permalink
Ensure compatibility with 'Realistic Walking Speed', closes #218
Browse files Browse the repository at this point in the history
  • Loading branch information
dymanoid committed May 30, 2019
1 parent 6bf431c commit f277f49
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/RealTime/Core/ModIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ internal static class ModIds

/// <summary>The Workshop ID of the 'Force Level Up' mod.</summary>
public const ulong ForceLevelUp = 523818382ul;

/// <summary>The Workshop ID of the 'Realistic Walking Speed' mod.</summary>
public const ulong RealisticWalkingSpeed = 1412844620ul;
}
}
11 changes: 8 additions & 3 deletions src/RealTime/Core/RealTimeCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static RealTimeCore Run(
timeInfo,
Constants.MaxTravelTime);

if (!SetupCustomAI(timeInfo, configProvider.Configuration, gameConnections, eventManager))
if (!SetupCustomAI(timeInfo, configProvider.Configuration, gameConnections, eventManager, compatibility))
{
Log.Error("The 'Real Time' mod failed to setup the customized AI and will now be deactivated.");
patcher.Revert();
Expand Down Expand Up @@ -376,16 +376,21 @@ private static bool SetupCustomAI(
TimeInfo timeInfo,
RealTimeConfig config,
GameConnections<Citizen> gameConnections,
RealTimeEventManager eventManager)
RealTimeEventManager eventManager,
Compatibility compatibility)
{
ResidentAIConnection<ResidentAI, Citizen> residentAIConnection = ResidentAIPatch.GetResidentAIConnection();
if (residentAIConnection == null)
{
return false;
}

float travelDistancePerCycle = compatibility.IsAnyModActive(ModIds.RealisticWalkingSpeed)
? Constants.AverageTravelDistancePerCycle * 0.583f
: Constants.AverageTravelDistancePerCycle;

var spareTimeBehavior = new SpareTimeBehavior(config, timeInfo);
var travelBehavior = new TravelBehavior(gameConnections.BuildingManager);
var travelBehavior = new TravelBehavior(gameConnections.BuildingManager, travelDistancePerCycle);
var workBehavior = new WorkBehavior(config, gameConnections.Random, gameConnections.BuildingManager, timeInfo, travelBehavior);

ParkPatches.SpareTimeBehavior = spareTimeBehavior;
Expand Down
7 changes: 3 additions & 4 deletions src/RealTime/CustomAI/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal static class Constants
public const float PrepareToWorkHours = 1f;

/// <summary>An assumed maximum travel time to a target building (in hours).</summary>
public const float MaxTravelTime = 3.5f;
public const float MaxTravelTime = 4f;

/// <summary>An assumed minimum travel time to a target building (in hours).</summary>
public const float MinTravelTime = 0.5f;
Expand All @@ -74,10 +74,9 @@ internal static class Constants
/// <summary>The chance of a young female to get pregnant.</summary>
public const uint YoungFemalePregnancyChance = 50u;

/// <summary>The average distance a citizen can move for (walking, by car, by public transport) during a full simulation
/// cycle at maximum time speed (6).
/// <summary>The average distance a citizen can travel for (walking, by car, by public transport) during a single simulation cycle.
/// This value was determined empirically.</summary>
public const float AverageDistancePerSimulationCycle = 600f;
public const float AverageTravelDistancePerCycle = 450f;

/// <summary>The maximum number of buildings (of one zone type) that are in construction or upgrading process.</summary>
public const int MaximumBuildingsInConstruction = 50;
Expand Down
24 changes: 17 additions & 7 deletions src/RealTime/CustomAI/TravelBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace RealTime.CustomAI
{
using System;
using RealTime.GameConnection;
using SkyTools.Tools;
using static Constants;
Expand All @@ -14,17 +15,26 @@ namespace RealTime.CustomAI
internal sealed class TravelBehavior : ITravelBehavior
{
private readonly IBuildingManagerConnection buildingManager;
private float averageCitizenSpeed;
private readonly float travelDistancePerCycle;
private float averageTravelSpeedPerHour;

/// <summary>Initializes a new instance of the <see cref="TravelBehavior"/> class.</summary>
/// <param name="buildingManager">
/// A proxy object that provides a way to call the game-specific methods of the <see cref="BuildingManager"/> class.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown when the argument is null.</exception>
public TravelBehavior(IBuildingManagerConnection buildingManager)
/// <param name="travelDistancePerCycle">The average distance a citizen can travel during a single simulation cycle.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="buildingManager"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="travelDistancePerCycle"/> is negative or zero.</exception>
public TravelBehavior(IBuildingManagerConnection buildingManager, float travelDistancePerCycle)
{
this.buildingManager = buildingManager ?? throw new System.ArgumentNullException(nameof(buildingManager));
averageCitizenSpeed = AverageDistancePerSimulationCycle;
if (travelDistancePerCycle <= 0)
{
throw new ArgumentException("The travel distance per cycle cannot be negative or zero.");
}

this.buildingManager = buildingManager ?? throw new ArgumentNullException(nameof(buildingManager));
this.travelDistancePerCycle = travelDistancePerCycle;
averageTravelSpeedPerHour = travelDistancePerCycle;
}

/// <summary>Sets the duration (in hours) of a full simulation cycle for all citizens.
Expand All @@ -37,7 +47,7 @@ public void SetSimulationCyclePeriod(float cyclePeriod)
cyclePeriod = 1f;
}

averageCitizenSpeed = AverageDistancePerSimulationCycle / cyclePeriod;
averageTravelSpeedPerHour = travelDistancePerCycle / cyclePeriod;
}

/// <summary>Gets an estimated travel time (in hours) between two specified buildings.</summary>
Expand All @@ -57,7 +67,7 @@ public float GetEstimatedTravelTime(ushort building1, ushort building2)
return MinTravelTime;
}

return FastMath.Clamp(distance / averageCitizenSpeed, MinTravelTime, MaxTravelTime);
return FastMath.Clamp(distance / averageTravelSpeedPerHour, MinTravelTime, MaxTravelTime);
}
}
}

0 comments on commit f277f49

Please sign in to comment.