Skip to content

Commit

Permalink
Don't switch off the lights of area main buildings (industry and campus)
Browse files Browse the repository at this point in the history
Apparently fixes #196
  • Loading branch information
dymanoid committed Jun 1, 2019
1 parent 4ccacf3 commit e1e1b17
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
1 change: 0 additions & 1 deletion src/RealTime/Core/RealTimeCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ private static List<IPatch> GetMethodPatches(Compatibility compatibility)
BuildingAIPatch.GetColor,
BuildingAIPatch.CalculateUnspawnPosition,
BuildingAIPatch.ProduceGoods,
BuildingAIPatch.HandleCrime,
ResidentAIPatch.Location,
ResidentAIPatch.ArriveAtTarget,
ResidentAIPatch.StartMoving,
Expand Down
11 changes: 11 additions & 0 deletions src/RealTime/CustomAI/RealTimeBuildingAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,17 @@ private bool ShouldSwitchBuildingLightsOff(ushort buildingId, ItemClass.Service
case ItemClass.Service.Museums:
return false;

case ItemClass.Service.PlayerEducation:
case ItemClass.Service.PlayerIndustry:
if (buildingManager.IsAreaMainBuilding(buildingId))
{
return false;
}
else
{
goto default;
}

case ItemClass.Service.Beautification when subService == ItemClass.SubService.BeautificationParks:
byte parkId = buildingManager.GetParkId(buildingId);
if (parkId == 0 || (buildingManager.GetParkPolicies(parkId) & DistrictPolicies.Park.NightTours) == 0)
Expand Down
20 changes: 20 additions & 0 deletions src/RealTime/GameConnection/BuildingManagerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,26 @@ public bool IsRealUniqueBuilding(ushort buildingId)
&& (monumentAI.m_supportEvents & (EventManager.EventType.Football | EventManager.EventType.Concert)) == 0;
}

/// <summary>
/// Determines whether the building with specified ID is the main building of an Industrial or a Campus area.
/// </summary>
/// <param name="buildingId">The building ID to check.</param>
/// <returns>
/// <c>true</c> if the building with the specified ID is the main building of an Industrial or a Campus area;
/// otherwise, <c>false</c>.
/// </returns>
public bool IsAreaMainBuilding(ushort buildingId)
{
if (buildingId == 0)
{
return false;
}

var buildingInfo = BuildingManager.instance.m_buildings.m_buffer[buildingId].Info;
var buildinAI = buildingInfo?.m_buildingAI;
return buildinAI is MainCampusBuildingAI || buildinAI is MainIndustryBuildingAI;
}

private static bool BuildingCanBeVisited(ushort buildingId)
{
uint currentUnitId = BuildingManager.instance.m_buildings.m_buffer[buildingId].m_citizenUnits;
Expand Down
10 changes: 10 additions & 0 deletions src/RealTime/GameConnection/IBuildingManagerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,15 @@ ushort FindActiveBuilding(
/// <c>true</c> if the building with the specified ID is a real unique building; otherwise, <c>false</c>.
/// </returns>
bool IsRealUniqueBuilding(ushort buildingId);

/// <summary>
/// Determines whether the building with specified ID is the main building of an Industrial or a Campus area.
/// </summary>
/// <param name="buildingId">The building ID to check.</param>
/// <returns>
/// <c>true</c> if the building with the specified ID is the main building of an Industrial or a Campus area;
/// otherwise, <c>false</c>.
/// </returns>
bool IsAreaMainBuilding(ushort buildingId);
}
}
36 changes: 0 additions & 36 deletions src/RealTime/GameConnection/Patches/BuildingAIPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ internal static class BuildingAIPatch
/// <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 @@ -321,38 +318,5 @@ 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(ref 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 e1e1b17

Please sign in to comment.