Skip to content

Commit

Permalink
Merge branch 'feature-simulation'
Browse files Browse the repository at this point in the history
  • Loading branch information
dymanoid committed Jul 22, 2018
2 parents 482bad5 + 60a7b76 commit 0e9c8c6
Show file tree
Hide file tree
Showing 54 changed files with 1,895 additions and 1,202 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ _ReSharper*/

#Nuget packages folder
packages/

#MacOS stuff
.DS_Store
4 changes: 2 additions & 2 deletions src/RealTime/Config/ConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static RealTimeConfig LoadConfiguration()
}

/// <summary>
/// Stores the provided <paramref name="config"/> object to the storage.
/// Stores the specified <paramref name="config"/> object to the storage.
/// </summary>
///
/// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
Expand Down Expand Up @@ -70,7 +70,7 @@ private static RealTimeConfig Deserialize()
var serializer = new XmlSerializer(typeof(RealTimeConfig));
using (var sr = new StreamReader(SettingsFileName))
{
return ((RealTimeConfig)serializer.Deserialize(sr)).Validate();
return ((RealTimeConfig)serializer.Deserialize(sr)).MigrateWhenNecessary().Validate();
}
}

Expand Down
96 changes: 48 additions & 48 deletions src/RealTime/Config/RealTimeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RealTime.Config
{
using System.Collections.Generic;
using RealTime.Tools;
using RealTime.UI;

/// <summary>
Expand All @@ -18,6 +18,9 @@ public RealTimeConfig()
ResetToDefaults();
}

/// <summary>Gets or sets the version number of this configuration.</summary>
public int Version { get; set; }

/// <summary>
/// Gets or sets the daytime hour when the city wakes up.
/// </summary>
Expand Down Expand Up @@ -96,31 +99,31 @@ public RealTimeConfig()
/// Valid values are 1..8.
/// </summary>
[ConfigItem("2Quotas", 0)]
[ConfigItemSlider(1, 8, DisplayMultiplier = 3.125f)]
[ConfigItemSlider(1, 25)]
public uint SecondShiftQuota { get; set; }

/// <summary>
/// Gets or sets a value that determines the percentage of the Cims that will work night shift.
/// Valid values are 1..8.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "NightShift", Justification = "Reviewed")]
[ConfigItem("2Quotas", 0)]
[ConfigItemSlider(1, 8, DisplayMultiplier = 3.125f)]
[ConfigItem("2Quotas", 1)]
[ConfigItemSlider(1, 25)]
public uint NightShiftQuota { get; set; }

/// <summary>
/// Gets or sets the percentage of the Cims that will go out for lunch.
/// Valid values are 0..100.
/// </summary>
[ConfigItem("2Quotas", 0)]
[ConfigItem("2Quotas", 2)]
[ConfigItemSlider(0, 100)]
public uint LunchQuota { get; set; }

/// <summary>
/// Gets or sets the percentage of the population that will search locally for buildings.
/// Valid values are 0..100.
/// </summary>
[ConfigItem("2Quotas", 1)]
[ConfigItem("2Quotas", 3)]
[ConfigItemSlider(0, 100)]
public uint LocalBuildingSearchQuota { get; set; }

Expand All @@ -129,7 +132,7 @@ public RealTimeConfig()
/// on time (no overtime!).
/// Valid values are 0..100.
/// </summary>
[ConfigItem("2Quotas", 2)]
[ConfigItem("2Quotas", 4)]
[ConfigItemSlider(0, 100)]
public uint OnTimeQuota { get; set; }

Expand Down Expand Up @@ -212,46 +215,60 @@ public RealTimeConfig()
[ConfigItemSlider(11, 16, 0.25f, SliderValueType.Time)]
public float SchoolEnd { get; set; }

/// <summary>Checks the version of the deserialized object and migrates it to the latest version when necessary.</summary>
/// <returns>This instance.</returns>
public RealTimeConfig MigrateWhenNecessary()
{
if (Version == 0)
{
SecondShiftQuota = (uint)(SecondShiftQuota * 3.125f);
NightShiftQuota = (uint)(NightShiftQuota * 3.125f);
}

Version = 1;
return this;
}

/// <summary>Validates this instance and corrects possible invalid property values.</summary>
/// <returns>This instance.</returns>
public RealTimeConfig Validate()
{
WakeupHour = Clamp(WakeupHour, 4f, 8f);
GoToSleepUpHour = Clamp(GoToSleepUpHour, 20f, 23.75f);
WakeupHour = RealTimeMath.Clamp(WakeupHour, 4f, 8f);
GoToSleepUpHour = RealTimeMath.Clamp(GoToSleepUpHour, 20f, 23.75f);

DayTimeSpeed = Clamp(DayTimeSpeed, 1u, 7u);
NightTimeSpeed = Clamp(NightTimeSpeed, 1u, 7u);
DayTimeSpeed = RealTimeMath.Clamp(DayTimeSpeed, 1u, 7u);
NightTimeSpeed = RealTimeMath.Clamp(NightTimeSpeed, 1u, 7u);

VirtualCitizens = (VirtualCitizensLevel)Clamp((int)VirtualCitizens, (int)VirtualCitizensLevel.None, (int)VirtualCitizensLevel.Many);
ConstructionSpeed = Clamp(ConstructionSpeed, 0u, 100u);
VirtualCitizens = (VirtualCitizensLevel)RealTimeMath.Clamp((int)VirtualCitizens, (int)VirtualCitizensLevel.None, (int)VirtualCitizensLevel.Many);
ConstructionSpeed = RealTimeMath.Clamp(ConstructionSpeed, 0u, 100u);

SecondShiftQuota = Clamp(SecondShiftQuota, 1u, 8u);
NightShiftQuota = Clamp(NightShiftQuota, 1u, 8u);
LunchQuota = Clamp(LunchQuota, 0u, 100u);
LocalBuildingSearchQuota = Clamp(LocalBuildingSearchQuota, 0u, 100u);
OnTimeQuota = Clamp(OnTimeQuota, 0u, 100u);
SecondShiftQuota = RealTimeMath.Clamp(SecondShiftQuota, 1u, 25u);
NightShiftQuota = RealTimeMath.Clamp(NightShiftQuota, 1u, 25u);
LunchQuota = RealTimeMath.Clamp(LunchQuota, 0u, 100u);
LocalBuildingSearchQuota = RealTimeMath.Clamp(LocalBuildingSearchQuota, 0u, 100u);
OnTimeQuota = RealTimeMath.Clamp(OnTimeQuota, 0u, 100u);

EarliestHourEventStartWeekday = Clamp(EarliestHourEventStartWeekday, 0f, 23.75f);
LatestHourEventStartWeekday = Clamp(LatestHourEventStartWeekday, 0f, 23.75f);
EarliestHourEventStartWeekday = RealTimeMath.Clamp(EarliestHourEventStartWeekday, 0f, 23.75f);
LatestHourEventStartWeekday = RealTimeMath.Clamp(LatestHourEventStartWeekday, 0f, 23.75f);
if (LatestHourEventStartWeekday < EarliestHourEventStartWeekday)
{
LatestHourEventStartWeekday = EarliestHourEventStartWeekday;
}

EarliestHourEventStartWeekend = Clamp(EarliestHourEventStartWeekend, 0f, 23.75f);
LatestHourEventStartWeekend = Clamp(LatestHourEventStartWeekend, 0f, 23.75f);
EarliestHourEventStartWeekend = RealTimeMath.Clamp(EarliestHourEventStartWeekend, 0f, 23.75f);
LatestHourEventStartWeekend = RealTimeMath.Clamp(LatestHourEventStartWeekend, 0f, 23.75f);
if (LatestHourEventStartWeekend < EarliestHourEventStartWeekend)
{
LatestHourEventStartWeekend = EarliestHourEventStartWeekend;
}

WorkBegin = Clamp(WorkBegin, 4f, 11f);
WorkEnd = Clamp(WorkEnd, 12f, 20f);
LunchBegin = Clamp(LunchBegin, 11f, 13f);
LunchEnd = Clamp(LunchEnd, 13f, 15f);
SchoolBegin = Clamp(SchoolBegin, 4f, 10f);
SchoolEnd = Clamp(SchoolEnd, 11f, 16f);
MaxOvertime = Clamp(MaxOvertime, 0f, 4f);
WorkBegin = RealTimeMath.Clamp(WorkBegin, 4f, 11f);
WorkEnd = RealTimeMath.Clamp(WorkEnd, 12f, 20f);
LunchBegin = RealTimeMath.Clamp(LunchBegin, 11f, 13f);
LunchEnd = RealTimeMath.Clamp(LunchEnd, 13f, 15f);
SchoolBegin = RealTimeMath.Clamp(SchoolBegin, 4f, 10f);
SchoolEnd = RealTimeMath.Clamp(SchoolEnd, 11f, 16f);
MaxOvertime = RealTimeMath.Clamp(MaxOvertime, 0f, 4f);
return this;
}

Expand All @@ -273,8 +290,8 @@ public void ResetToDefaults()
StopConstructionAtNight = true;
ConstructionSpeed = 50;

SecondShiftQuota = 4;
NightShiftQuota = 2;
SecondShiftQuota = 13;
NightShiftQuota = 6;

LunchQuota = 80;
LocalBuildingSearchQuota = 60;
Expand All @@ -293,22 +310,5 @@ public void ResetToDefaults()
SchoolBegin = 8f;
SchoolEnd = 14f;
}

private static T Clamp<T>(T value, T min, T max)
where T : struct
{
Comparer<T> comparer = Comparer<T>.Default;
if (comparer.Compare(value, min) < 0)
{
return min;
}

if (comparer.Compare(value, max) > 0)
{
return max;
}

return value;
}
}
}
4 changes: 2 additions & 2 deletions src/RealTime/Core/IStorageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal interface IStorageData
string StorageDataId { get; }

/// <summary>
/// Reads the data set from the provided <see cref="Stream"/>.
/// Reads the data set from the specified <see cref="Stream"/>.
/// </summary>
///
/// <exception cref="System.ArgumentNullException">Thrown when the argument is null.</exception>
Expand All @@ -26,7 +26,7 @@ internal interface IStorageData
void ReadData(Stream source);

/// <summary>
/// Reads the data set to the provided <see cref="Stream"/>.
/// Stores the data set to the specified <see cref="Stream"/>.
/// </summary>
///
/// <exception cref="System.ArgumentNullException">Thrown when the argument is null.</exception>
Expand Down
15 changes: 12 additions & 3 deletions src/RealTime/Core/RealTimeCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static RealTimeCore Run(RealTimeConfig config, string rootPath, ILocaliza
BuildingAIPatches.PrivateHandleWorkers,
BuildingAIPatches.CommercialSimulation,
ResidentAIPatch.Location,
ResidentAIPatch.ArriveAtDestination,
TouristAIPatch.Location,
UIGraphPatches.MinDataPoints,
UIGraphPatches.VisibleEndTime,
Expand Down Expand Up @@ -125,6 +126,7 @@ public static RealTimeCore Run(RealTimeConfig config, string rootPath, ILocaliza

var timeAdjustment = new TimeAdjustment(config);
DateTime gameDate = timeAdjustment.Enable();
SimulationHandler.CitizenProcessor.SetFrameDuration(timeAdjustment.HoursPerFrame);

CityEventsLoader.Instance.ReloadEvents(rootPath);

Expand All @@ -146,6 +148,7 @@ public static RealTimeCore Run(RealTimeConfig config, string rootPath, ILocaliza

RealTimeStorage.CurrentLevelStorage.GameSaving += result.GameSaving;
result.storageData.Add(eventManager);
result.storageData.Add(ResidentAIPatch.RealTimeAI.GetStorageService());
result.LoadStorageData();

result.Translate(localizationProvider);
Expand Down Expand Up @@ -185,13 +188,14 @@ public void Stop()
SimulationHandler.TimeAdjustment = null;
SimulationHandler.WeatherInfo = null;
SimulationHandler.Buildings = null;
SimulationHandler.CitizenProcessor = null;

isEnabled = false;
}

/// <summary>
/// Translates all the mod's component to a different language obtained from
/// the provided <paramref name="localizationProvider"/>.
/// the specified <paramref name="localizationProvider"/>.
/// </summary>
///
/// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
Expand Down Expand Up @@ -220,13 +224,17 @@ private static bool SetupCustomAI(
return false;
}

var spareTimeBehavior = new SpareTimeBehavior(config, timeInfo);

var realTimeResidentAI = new RealTimeResidentAI<ResidentAI, Citizen>(
config,
gameConnections,
residentAIConnection,
eventManager);
eventManager,
spareTimeBehavior);

ResidentAIPatch.RealTimeAI = realTimeResidentAI;
SimulationHandler.CitizenProcessor = new CitizenProcessor(realTimeResidentAI, spareTimeBehavior);

TouristAIConnection<TouristAI, Citizen> touristAIConnection = TouristAIPatch.GetTouristAIConnection();
if (touristAIConnection == null)
Expand All @@ -238,7 +246,8 @@ private static bool SetupCustomAI(
config,
gameConnections,
touristAIConnection,
eventManager);
eventManager,
spareTimeBehavior);

TouristAIPatch.RealTimeAI = realTimeTouristAI;

Expand Down
4 changes: 2 additions & 2 deletions src/RealTime/Core/RealTimeStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override void OnReleased()
}

/// <summary>
/// Serializes the data described by the provided <paramref name="data"/> to this level's storage.
/// Serializes the data described by the specified <paramref name="data"/> to this level's storage.
/// </summary>
///
/// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
Expand Down Expand Up @@ -86,7 +86,7 @@ internal void Serialize(IStorageData data)
}

/// <summary>
/// Deserializes the data described by the provided <paramref name="data"/> from this level's storage.
/// Deserializes the data described by the specified <paramref name="data"/> from this level's storage.
/// </summary>
///
/// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
Expand Down
Loading

0 comments on commit 0e9c8c6

Please sign in to comment.