Skip to content

Commit

Permalink
Implement workaround for crime growth in inactive industry buildings
Browse files Browse the repository at this point in the history
This is actually caused by a bug in the CO's code
  • Loading branch information
dymanoid committed Oct 28, 2018
1 parent 8024aa9 commit 5d621b4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/RealTime/Core/RealTimeCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ private static List<IPatch> GetMethodPatches(Compatibility compatibility)
BuildingAIPatches.GetColor,
BuildingAIPatches.CalculateUnspawnPosition,
BuildingAIPatches.ProduceGoods,
BuildingAIPatches.HandleCrime,
ResidentAIPatch.Location,
ResidentAIPatch.ArriveAtTarget,
ResidentAIPatch.StartMoving,
Expand Down
36 changes: 36 additions & 0 deletions src/RealTime/GameConnection/Patches/BuildingAIPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ internal static class BuildingAIPatches
/// <summary>Gets the patch for the building AI method 'ProduceGoods'.</summary>
public static IPatch ProduceGoods { get; } = new PlayerBuildingAI_ProduceGoods();

/// <summary>Gets the patch for the industry DLC building AI method 'HandleCrime'.</summary>
public static IPatch HandleCrime { get; } = new IndustryBuildingAI_HandleCrime();

private sealed class CommercialBuildingA_SimulationStepActive : PatchBase
{
protected override MethodInfo GetMethod()
Expand Down Expand Up @@ -328,5 +331,38 @@ private static void Postfix(ushort buildingID, InfoManager.InfoMode infoMode, re
}
}
}

private sealed class IndustryBuildingAI_HandleCrime : PatchBase
{
protected override MethodInfo GetMethod()
{
return typeof(IndustryBuildingAI).GetMethod(
"HandleCrime",
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new[] { typeof(ushort), typeof(Building).MakeByRefType(), typeof(int), typeof(int) },
new ParameterModifier[0]);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Redundancy", "RCS1213", Justification = "Harmony patch")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming Rules", "SA1313", Justification = "Harmony patch")]
private static bool Prefix(ushort __state, ref Building data)
{
__state = data.m_crimeBuffer;
return true;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Redundancy", "RCS1213", Justification = "Harmony patch")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming Rules", "SA1313", Justification = "Harmony patch")]
private static void Postfix(ushort __state, ref Building data)
{
// CO's code has a bug that leads to unbounded crime buffer growth for inactive industrial buildings.
// Since Real Time switches buildings to inactive at night, we have to stop the crime growth for those buildings.
if ((data.m_flags & Building.Flags.Active) == 0)
{
data.m_crimeBuffer = __state;
}
}
}
}
}

0 comments on commit 5d621b4

Please sign in to comment.