From e1e1b173bae9ee0dc5c7bc7d6533d4b4571f4099 Mon Sep 17 00:00:00 2001 From: dymanoid <9433345+dymanoid@users.noreply.github.com> Date: Sun, 2 Jun 2019 01:26:26 +0200 Subject: [PATCH] Don't switch off the lights of area main buildings (industry and campus) Apparently fixes #196 --- src/RealTime/Core/RealTimeCore.cs | 1 - src/RealTime/CustomAI/RealTimeBuildingAI.cs | 11 ++++++ .../BuildingManagerConnection.cs | 20 +++++++++++ .../IBuildingManagerConnection.cs | 10 ++++++ .../GameConnection/Patches/BuildingAIPatch.cs | 36 ------------------- 5 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/RealTime/Core/RealTimeCore.cs b/src/RealTime/Core/RealTimeCore.cs index 771c0edf..75440d28 100644 --- a/src/RealTime/Core/RealTimeCore.cs +++ b/src/RealTime/Core/RealTimeCore.cs @@ -309,7 +309,6 @@ private static List GetMethodPatches(Compatibility compatibility) BuildingAIPatch.GetColor, BuildingAIPatch.CalculateUnspawnPosition, BuildingAIPatch.ProduceGoods, - BuildingAIPatch.HandleCrime, ResidentAIPatch.Location, ResidentAIPatch.ArriveAtTarget, ResidentAIPatch.StartMoving, diff --git a/src/RealTime/CustomAI/RealTimeBuildingAI.cs b/src/RealTime/CustomAI/RealTimeBuildingAI.cs index 2a9b0b7e..039b6111 100644 --- a/src/RealTime/CustomAI/RealTimeBuildingAI.cs +++ b/src/RealTime/CustomAI/RealTimeBuildingAI.cs @@ -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) diff --git a/src/RealTime/GameConnection/BuildingManagerConnection.cs b/src/RealTime/GameConnection/BuildingManagerConnection.cs index 728217be..fd243484 100644 --- a/src/RealTime/GameConnection/BuildingManagerConnection.cs +++ b/src/RealTime/GameConnection/BuildingManagerConnection.cs @@ -448,6 +448,26 @@ public bool IsRealUniqueBuilding(ushort buildingId) && (monumentAI.m_supportEvents & (EventManager.EventType.Football | EventManager.EventType.Concert)) == 0; } + /// + /// Determines whether the building with specified ID is the main building of an Industrial or a Campus area. + /// + /// The building ID to check. + /// + /// true if the building with the specified ID is the main building of an Industrial or a Campus area; + /// otherwise, false. + /// + 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; diff --git a/src/RealTime/GameConnection/IBuildingManagerConnection.cs b/src/RealTime/GameConnection/IBuildingManagerConnection.cs index de78f754..641e943b 100644 --- a/src/RealTime/GameConnection/IBuildingManagerConnection.cs +++ b/src/RealTime/GameConnection/IBuildingManagerConnection.cs @@ -196,5 +196,15 @@ ushort FindActiveBuilding( /// true if the building with the specified ID is a real unique building; otherwise, false. /// bool IsRealUniqueBuilding(ushort buildingId); + + /// + /// Determines whether the building with specified ID is the main building of an Industrial or a Campus area. + /// + /// The building ID to check. + /// + /// true if the building with the specified ID is the main building of an Industrial or a Campus area; + /// otherwise, false. + /// + bool IsAreaMainBuilding(ushort buildingId); } } \ No newline at end of file diff --git a/src/RealTime/GameConnection/Patches/BuildingAIPatch.cs b/src/RealTime/GameConnection/Patches/BuildingAIPatch.cs index 7636a747..0d3feb10 100644 --- a/src/RealTime/GameConnection/Patches/BuildingAIPatch.cs +++ b/src/RealTime/GameConnection/Patches/BuildingAIPatch.cs @@ -47,9 +47,6 @@ internal static class BuildingAIPatch /// Gets the patch for the building AI method 'ProduceGoods'. public static IPatch ProduceGoods { get; } = new PlayerBuildingAI_ProduceGoods(); - /// Gets the patch for the industry DLC building AI method 'HandleCrime'. - public static IPatch HandleCrime { get; } = new IndustryBuildingAI_HandleCrime(); - private sealed class CommercialBuildingA_SimulationStepActive : PatchBase { protected override MethodInfo GetMethod() @@ -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; - } - } - } } }