@@ -95,6 +95,13 @@ public class ModuleSystemHeatFissionReactor : PartModule, IContractObjectiveModu
9595 [ KSPField ( isPersistant = true ) ]
9696 public float CurrentElectricalGeneration = 0f ;
9797
98+ /// <summary>
99+ /// Amount of power that could generated by reactor
100+ /// </summary>
101+ [ KSPField ( isPersistant = true ) ]
102+ public float MaxElectricalGeneration = 0f ;
103+
104+
98105 // Reactor Status string
99106 [ KSPField ( isPersistant = false , guiActive = true , guiName = "#LOC_SystemHeat_ModuleSystemHeatFissionReactor_Field_GeneratorStatus" , groupName = "fissionreactor" , groupDisplayName = "#LOC_SystemHeat_ModuleSystemHeatFissionReactor_UIGroup_Title" ) ]
100107 public string GeneratorStatus ;
@@ -663,6 +670,7 @@ public virtual void FixedUpdate()
663670 if ( GeneratesElectricity )
664671 {
665672 CurrentElectricalGeneration = ElectricalGeneration . Evaluate ( CurrentThrottle ) ;
673+ MaxElectricalGeneration = CurrentElectricalGeneration ;
666674 }
667675
668676 }
@@ -695,6 +703,7 @@ public virtual void FixedUpdate()
695703 else
696704 {
697705 CurrentElectricalGeneration = 0f ;
706+ MaxElectricalGeneration = 0f ;
698707 }
699708 }
700709 }
@@ -783,21 +792,34 @@ private void HandleCore()
783792 }
784793 }
785794
795+ private double [ ] ecDeltas = new double [ 60 ] ;
786796 protected virtual float CalculateGoalThrottle ( float timeStep )
787797 {
788- double shipEC = 0d ;
789- double shipMaxEC = 0d ;
790- // Determine need for power
791- part . GetConnectedResourceTotals ( PartResourceLibrary . ElectricityHashcode , out shipEC , out shipMaxEC , true ) ;
792798
793- float maxGeneration = ElectricalGeneration . Evaluate ( 100f ) * CoreIntegrity / 100f ;
799+ // Determine need for power this frame
800+ part . GetConnectedResourceTotals ( PartResourceLibrary . ElectricityHashcode , out double shipEC , out double shipMaxEC , true ) ;
801+ double [ ] ecDeltas2 = new double [ 10 ] ;
802+ for ( int i = 1 ; i < ecDeltas2 . Length ; i ++ )
803+ {
804+ ecDeltas2 [ i ] = ecDeltas [ i - 1 ] ;
805+ }
806+ ecDeltas = ecDeltas2 ;
807+ ecDeltas [ 0 ] = shipMaxEC - shipEC ;
808+
809+ double weightedAvgNeed = 0d ;
810+ for ( int i = 0 ; i < ecDeltas . Length ; i ++ )
811+ {
812+ weightedAvgNeed += ecDeltas [ i ] ;
813+ }
814+ weightedAvgNeed /= ( double ) ecDeltas . Length ;
815+
816+ float maxGeneration = ElectricalGeneration . Evaluate ( 100f ) * CoreIntegrity / 100f * timeStep ;
794817 float minGeneration = ElectricalGeneration . Evaluate ( MinimumThrottle ) * timeStep ;
795- float idealGeneration = Mathf . Min ( maxGeneration * timeStep , ( float ) ( shipMaxEC - shipEC ) ) ;
818+ float idealGeneration = Mathf . Min ( maxGeneration , ( float ) weightedAvgNeed * 1.05f ) ;
796819 float powerToGenerate = Mathf . Max ( minGeneration , idealGeneration ) ;
797820
798- return ( powerToGenerate / timeStep ) / maxGeneration * 100f ;
821+ return ( powerToGenerate / maxGeneration ) * 100f ;
799822 }
800-
801823 public virtual string GetContractObjectiveType ( )
802824 {
803825 return "Generator" ;
@@ -814,7 +836,9 @@ private void HandleResourceActivities(float timeStep)
814836 {
815837
816838 if ( ! ManualControl )
839+ {
817840 CurrentReactorThrottle = CalculateGoalThrottle ( timeStep ) ;
841+ }
818842
819843 fuelCheckPassed = true ;
820844 burnRate = 0d ;
@@ -858,7 +882,7 @@ private void HandleResourceActivities(float timeStep)
858882 }
859883 }
860884 if ( ratio . ResourceName == FuelName )
861- {
885+ {
862886 burnRate = fuelThrottle * ratio . Ratio ;
863887 }
864888 }
@@ -876,10 +900,19 @@ private void HandleResourceActivities(float timeStep)
876900 if ( HighLogic . LoadedSceneIsEditor )
877901 {
878902 CurrentElectricalGeneration = ElectricalGeneration . Evaluate ( CurrentReactorThrottle ) ;
903+ MaxElectricalGeneration = CurrentElectricalGeneration ;
879904 }
880905 if ( fuelCheckPassed )
881906 {
882907 CurrentElectricalGeneration = ElectricalGeneration . Evaluate ( CurrentThrottle ) ;
908+ if ( ManualControl )
909+ {
910+ MaxElectricalGeneration = CurrentElectricalGeneration ;
911+ }
912+ else
913+ {
914+ MaxElectricalGeneration = ElectricalGeneration . Evaluate ( 100f ) * CoreIntegrity / 100f ;
915+ }
883916 this . part . RequestResource ( PartResourceLibrary . ElectricityHashcode , - CurrentElectricalGeneration * timeStep , ResourceFlowMode . ALL_VESSEL ) ;
884917 }
885918 }
0 commit comments