99using Timberborn . StatusSystem ;
1010using Timberborn . Workshops ;
1111using UnityDev . Utils . LogUtilsLite ;
12- using UnityDev . Utils . Reflections ;
1312
1413// ReSharper disable once CheckNamespace
1514namespace IgorZ . SmartPower {
@@ -23,20 +22,17 @@ namespace IgorZ.SmartPower {
2322/// consumes only a fraction of the nominal power.
2423/// </remarks>
2524public class SmartMechanicalBuilding : MechanicalBuilding {
26- static readonly ReflectedAction < MechanicalBuilding > UpdateActiveAndPoweredMethod = new (
27- "UpdateActiveAndPowered" , throwOnFailure : true ) ;
2825
29- const string StandbyStatusIcon = "igorz.smartpower/ui_icons/status-icon-standby" ;
30- const float NonFuelRecipeIdleStateConsumption = 0.1f ;
31- const string PowerSavingModeLocKey = "IgorZ.SmartPower.MechanicalBuilding.PowerSavingModeStatus" ;
26+ #region API
3227
33- MechanicalNode _mechanicalNode ;
34- Manufactory _manufactory ;
35- Enterable _enterable ;
36- ILoc _loc ;
37- StatusToggle _standbyStatus ;
28+ /// <summary>Indicates that this building logic must is handled by the smart behavior.</summary>
29+ // ReSharper disable once MemberCanBePrivate.Global
30+ public bool NeedsSmartLogic => _mechanicalNode . IsConsumer && ! _mechanicalNode . IsGenerator ;
31+
32+ // ReSharper disable once MemberCanBePrivate.Global
33+ /// <summary>Indicates that teh building has a working place that should have workers.</summary>
34+ public bool HasWorkingPlaces { get ; private set ; }
3835
39- #region API
4036 /// <summary>
4137 /// Indicates that the building is expected to be staffed and working, but no workers are currently at the working
4238 /// place(s).
@@ -71,53 +67,74 @@ private set {
7167 }
7268 }
7369 bool _idleState ;
70+
7471 #endregion
7572
76- #region MechanicalBuilding overrides
73+ #region TickableComponent overrides
74+
7775 /// <inheritdoc/>
7876 public override void StartTickable ( ) {
7977 base . StartTickable ( ) ;
80- _mechanicalNode = GetComponentFast < MechanicalNode > ( ) ;
81- _manufactory = GetComponentFast < Manufactory > ( ) ;
82- _enterable = GetComponentFast < Enterable > ( ) ;
83- _standbyStatus = StatusToggle . CreateNormalStatus ( StandbyStatusIcon , _loc . T ( PowerSavingModeLocKey ) ) ;
84- var subject = GetComponentFast < StatusSubject > ( ) ;
85- subject . RegisterStatus ( _standbyStatus ) ;
86- UpdateNodeCharacteristics ( ) ;
78+ if ( NeedsSmartLogic ) {
79+ SmartUpdateNodeCharacteristics ( ) ;
80+ }
8781 }
8882
8983 /// <inheritdoc/>
9084 public override void Tick ( ) {
91- if ( ! _mechanicalNode . IsConsumer || _mechanicalNode . IsGenerator ) {
85+ if ( ! NeedsSmartLogic ) {
9286 base . Tick ( ) ;
9387 return ;
9488 }
95- UpdateNodeCharacteristics ( ) ;
96- UpdateActiveAndPoweredMethod . Invoke ( this ) ;
89+ SmartUpdateNodeCharacteristics ( ) ;
90+ UpdateActiveAndPowered ( ) ;
9791 }
92+
9893 #endregion
9994
10095 #region Implementation
101- #pragma warning disable CS1591
96+
97+ const string StandbyStatusIcon = "igorz.smartpower/ui_icons/status-icon-standby" ;
98+ const float NonFuelRecipeIdleStateConsumption = 0.1f ;
99+ const string PowerSavingModeLocKey = "IgorZ.SmartPower.MechanicalBuilding.PowerSavingModeStatus" ;
100+
101+ Manufactory _manufactory ;
102+ Enterable _enterable ;
103+ ILoc _loc ;
104+ StatusToggle _standbyStatus ;
105+
106+ /// <inheritdoc cref="MechanicalBuilding.Awake" />
107+ public new void Awake ( ) {
108+ base . Awake ( ) ;
109+ _manufactory = GetComponentFast < Manufactory > ( ) ;
110+ _enterable = GetComponentFast < Enterable > ( ) ;
111+ _standbyStatus = StatusToggle . CreateNormalStatus ( StandbyStatusIcon , _loc . T ( PowerSavingModeLocKey ) ) ;
112+ var subject = GetComponentFast < StatusSubject > ( ) ;
113+ subject . RegisterStatus ( _standbyStatus ) ;
114+ HasWorkingPlaces = GetComponentFast < Workshop > ( ) != null ;
115+ }
116+
117+ /// <summary>It must be public for the injection logic to work.</summary>
102118 [ Inject ]
103119 public void InjectDependencies ( ILoc loc ) {
104120 _loc = loc ;
105121 }
106122
107- void UpdateNodeCharacteristics ( ) {
123+ void SmartUpdateNodeCharacteristics ( ) {
108124 if ( ConsumptionDisabled ) {
109125 _mechanicalNode . UpdateInput ( 0 ) ;
110126 AllWorkersOut = MissingIngredients = BlockedOutput = NoFuel = StandbyMode = false ;
111127 return ;
112128 }
113129 var hasRecipe = _manufactory . HasCurrentRecipe ;
114- AllWorkersOut = hasRecipe && _enterable != null && _enterable . NumberOfEnterersInside == 0 ;
130+ AllWorkersOut = HasWorkingPlaces && hasRecipe && _enterable != null && _enterable . NumberOfEnterersInside == 0 ;
115131 MissingIngredients = hasRecipe && ! _manufactory . HasAllIngredients ;
116132 BlockedOutput = hasRecipe && ! _manufactory . Inventory . HasUnreservedCapacity ( _manufactory . CurrentRecipe . Products ) ;
117133 NoFuel = hasRecipe && ! _manufactory . HasFuel ;
118134 StandbyMode = AllWorkersOut || MissingIngredients || NoFuel || BlockedOutput ;
119135 _mechanicalNode . UpdateInput ( StandbyMode ? NonFuelRecipeIdleStateConsumption : 1.0f ) ;
120136 }
137+
121138 #endregion
122139}
123140
0 commit comments