44using System . Linq . Expressions ;
55using System . Reflection ;
66using System . Reflection . Emit ;
7+ using LibNoise . Unity . Operator ;
78using UnityEngine ;
89using Object = System . Object ;
910
@@ -12,24 +13,56 @@ namespace MapResourceOverlay
1213 public class ResourceOverlayProvider : OverlayProviderBase
1314 {
1415 private ResourceConfig _activeResource ;
16+ private double _displayMax ;
1517
1618 private delegate Object GetResourceAvailabilityByRealResourceNameDelegate (
1719 int bodyIndex , string resourceName , double lon , double lat ) ;
1820
1921 private Func < object , object > _getAmount ;
2022
2123 private GetResourceAvailabilityByRealResourceNameDelegate _getResourceAvailabilityByRealResourceName ;
24+ private bool _logaritmic ;
25+ private bool _coloredScale ;
2226
2327 public ResourceConfig ActiveResource
2428 {
2529 get { return _activeResource ; }
2630 set
2731 {
2832 _activeResource = value ;
33+ CalculateBase ( ) ;
2934 RequiresRedraw ( ) ;
3035 }
3136 }
3237
38+ public override void Activate ( CelestialBody body )
39+ {
40+ base . Activate ( body ) ;
41+ CalculateBase ( ) ;
42+ RequiresRedraw ( ) ;
43+ }
44+
45+ private void CalculateBase ( )
46+ {
47+ _displayMax = 0 ;
48+ double avg = 0 ;
49+ for ( int lat = 0 ; lat < 180 ; lat ++ )
50+ {
51+ for ( int lon = 0 ; lon < 360 ; lon ++ )
52+ {
53+ var amount = ( double ) _getAmount ( _getResourceAvailabilityByRealResourceName ( _body . flightGlobalsIndex ,
54+ ActiveResource . Resource . ResourceName , lon , lat ) ) ;
55+ if ( amount > _displayMax )
56+ {
57+ _displayMax = amount ;
58+ }
59+ avg += amount ;
60+ }
61+ }
62+ _displayMax *= 1000000 ;
63+ avg = avg / ( 180 * 360 ) ;
64+ }
65+
3366 public override Color32 CalculateColor32 ( double latitude , double longitude , CelestialBody body , bool useScansat ,
3467 bool bright , double cutoff )
3568 {
@@ -40,10 +73,23 @@ public override Color32 CalculateColor32(double latitude, double longitude, Cele
4073 }
4174 var avail = _getResourceAvailabilityByRealResourceName ( body . flightGlobalsIndex ,
4275 ActiveResource . Resource . ResourceName , latitude , longitude ) ;
43- var amount = ( double ) _getAmount ( avail ) ;
44- amount = amount * 1000000 ;
76+ var amount = ( double ) _getAmount ( avail ) * 1000000 ;
4577 if ( amount > cutoff )
4678 {
79+ if ( Logaritmic )
80+ {
81+ amount = ( ( Math . Log ( amount , 2 ) - ( Math . Log ( cutoff , 2 ) ) ) * 255 ) / ( ( Math . Log ( _displayMax , 2 ) - ( Math . Log ( cutoff , 2 ) ) ) ) ;
82+ }
83+ else if ( _coloredScale )
84+ {
85+ var color = ScanSatWrapper . heightToColor ( ( float ) amount , cutoff , ( float ) _displayMax ) ;
86+ color . a = ActiveResource . HighColor . a ;
87+ return color ;
88+ }
89+ else
90+ {
91+ amount = ( ( amount - cutoff ) * 255 ) / ( _displayMax - cutoff ) ;
92+ }
4793 amount = Mathf . Clamp ( ( float ) amount , 0f , 255f ) ;
4894 if ( ! bright )
4995 {
@@ -55,12 +101,25 @@ public override Color32 CalculateColor32(double latitude, double longitude, Cele
55101 }
56102 else
57103 {
58- return new Color32 ( 255 , Convert . ToByte ( amount ) , Convert . ToByte ( amount ) , 150 ) ;
104+ return new Color32 ( 155 , Convert . ToByte ( amount ) , Convert . ToByte ( amount ) , 150 ) ;
59105 }
60106 }
61107 return ActiveResource . LowColor ;
62108 }
63109
110+ public bool Logaritmic
111+ {
112+ get { return _logaritmic ; }
113+ set
114+ {
115+ if ( _logaritmic != value )
116+ {
117+ _logaritmic = value ;
118+ RequiresRedraw ( ) ;
119+ }
120+ }
121+ }
122+
64123 public override OverlayTooltip TooltipContent ( double latitude , double longitude , CelestialBody body )
65124 {
66125 var abundance = ( double ) _getAmount ( _getResourceAvailabilityByRealResourceName ( body . flightGlobalsIndex ,
@@ -81,7 +140,7 @@ public override string GuiName
81140
82141 public List < ResourceConfig > ColorConfigs { get ; set ; }
83142
84- public ResourceOverlayProvider ( )
143+ private void LoadFailsafe ( )
85144 {
86145 var config = new ResourceConfig
87146 {
@@ -113,12 +172,20 @@ public ResourceOverlayProvider()
113172 LowColor = new Color32 ( 0 , 0 , 0 , 0 ) ,
114173 HighColor = new Color32 ( 255 , 0 , 255 , 200 )
115174 } ;
116- ColorConfigs = new List < ResourceConfig > { config , config2 , config3 , config4 , config5 } ;
175+ ColorConfigs = new List < ResourceConfig > { config , config2 , config3 , config4 , config5 } ;
117176 ActiveResource = config ;
118177 }
119178
120179 public override void Load ( ConfigNode node )
121180 {
181+ try
182+ {
183+ InitiateOrs ( ) ;
184+ }
185+ catch ( Exception e )
186+ {
187+ this . Log ( "Couldnt find ORS" + e ) ;
188+ }
122189 try
123190 {
124191 if ( node . HasNode ( "ResourceOverlay" ) )
@@ -135,15 +202,8 @@ public override void Load(ConfigNode node)
135202 }
136203 catch ( Exception e )
137204 {
138- this . Log ( "Could not load config, using default" + e ) ;
139- }
140- try
141- {
142- InitiateOrs ( ) ;
143- }
144- catch ( Exception e )
145- {
146- this . Log ( "Couldnt find ORS" + e ) ;
205+ this . Log ( "Could not load config, using default, because " + e ) ;
206+ LoadFailsafe ( ) ;
147207 }
148208 }
149209
@@ -200,5 +260,37 @@ static Func<object, object> GenerateFunc(MethodInfo method)
200260 instance
201261 ) . Compile ( ) ;
202262 }
263+
264+ public override void DrawGui ( MapOverlayGui gui )
265+ {
266+ base . DrawGui ( gui ) ;
267+ GUILayout . BeginVertical ( ) ;
268+ Logaritmic = GUILayout . Toggle ( Logaritmic , "Logarithmic Scale" ) ;
269+ ColoredScale = GUILayout . Toggle ( ColoredScale , "Colored Scale" ) ;
270+ GUILayout . Space ( 15 ) ;
271+ foreach ( var res in ColorConfigs )
272+ {
273+ if ( GUILayout . Button ( res . Resource . ResourceName ) )
274+ {
275+ ActiveResource = res ;
276+ }
277+ }
278+ GUILayout . EndVertical ( ) ;
279+ }
280+
281+ public bool ColoredScale
282+ {
283+ get { return _coloredScale ; }
284+ set
285+ {
286+ if ( _coloredScale != value )
287+ {
288+ _coloredScale = value ;
289+ RequiresRedraw ( ) ;
290+ }
291+
292+
293+ }
294+ }
203295 }
204296}
0 commit comments