@@ -16,25 +16,32 @@ public class MapOverlay : MonoBehaviour
1616 private CelestialBody _body ;
1717 private Mesh _mesh ;
1818
19- private readonly List < string > _resources = new List < string >
19+ private readonly List < Resource > _resources = new List < Resource >
2020 {
21- "Karbonite" ,
22- "Minerals" ,
23- "Ore" ,
24- "Substrate" ,
25- "Water"
21+ new Resource ( "Karbonite" ) ,
22+ new Resource ( "Minerals" ) ,
23+ new Resource ( "Ore" ) ,
24+ new Resource ( "Substrate" ) ,
25+ new Resource ( "Water" , "Aquifer" )
2626 } ;
2727
2828 private readonly IButton _mapOverlayButton ;
2929 private MapOverlayGui _gui ;
3030 private bool _changed ;
31- private string _selectedResourceName ;
31+ private Resource _selectedResourceName ;
3232 private Coordinates _mouseCoords ;
3333 private CelestialBody targetBody ;
34+ private double _lastRedraw ;
35+ private delegate bool IsCoveredDelegate ( double lon , double lat , CelestialBody body , int mask ) ;
3436
35- [ KSPField ( isPersistant = true ) ] private bool _show ;
37+ private IsCoveredDelegate _scansatIsCoveredDelegate ;
38+ private Type _scansatEnum ;
39+ [ KSPField ( isPersistant = true ) ] public bool _show ;
3640
3741 private Vector2 mouse ;
42+ [ KSPField ( isPersistant = true ) ] public bool _useScansat ;
43+ private int _toolTipId ;
44+ private int _currentLat ;
3845
3946
4047 public MapOverlay ( )
@@ -45,6 +52,7 @@ public MapOverlay()
4552 _mapOverlayButton . Visibility = new GameScenesVisibility ( GameScenes . FLIGHT ) ;
4653 _mapOverlayButton . OnClick += e => ToggleGui ( ) ;
4754 DontDestroyOnLoad ( this ) ;
55+ _toolTipId = new System . Random ( ) . Next ( 65536 ) + Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name . GetHashCode ( ) + "tooltip" . GetHashCode ( ) ;
4856 }
4957
5058 public void ToggleGui ( )
@@ -80,8 +88,7 @@ public void Start()
8088 gameObject . AddComponent < MeshRenderer > ( ) ;
8189 _mesh = gameObject . AddComponent < MeshFilter > ( ) . mesh ;
8290 SelectedResourceName = Resources [ 0 ] ;
83- var overlays = FindObjectsOfType < MapOverlay > ( ) . ToList ( ) ;
84- Debug . Log ( "MapResourceOverlay left: " + overlays . Count ( x => x != this ) ) ;
91+ InitializeScansatIntegration ( ) ;
8592 }
8693
8794 public void Update ( )
@@ -99,14 +106,13 @@ private void updateMapView()
99106 if ( ! Show || MapView . MapCamera == null )
100107 {
101108 gameObject . renderer . enabled = false ;
102- _mouseCoords = null ;
103109 }
104110 else
105111 {
106112 gameObject . renderer . enabled = true ;
107113 targetBody = GetTargetBody ( MapView . MapCamera . target ) ;
108114
109- if ( targetBody != null && targetBody != _body || targetBody != null && _changed )
115+ if ( targetBody != null && ( targetBody != _body || _changed ) )
110116 {
111117 _changed = false ;
112118 Debug . Log ( "new target: " + targetBody . GetName ( ) ) ;
@@ -123,11 +129,40 @@ private void updateMapView()
123129 gameObject . transform . localScale = Vector3 . one * 1000f * radius ;
124130 gameObject . transform . localPosition = ( Vector3 . zero ) ;
125131 gameObject . transform . localRotation = ( Quaternion . identity ) ;
126-
132+ _lastRedraw = Planetarium . GetUniversalTime ( ) ;
133+ }
134+ if ( targetBody != null && UseScansat && _scansatIsCoveredDelegate != null &&
135+ _lastRedraw < Planetarium . GetUniversalTime ( ) )
136+ {
137+ RecalculateColors ( targetBody , SelectedResourceName ) ;
138+ _lastRedraw = Planetarium . GetUniversalTime ( ) ;
127139 }
128-
129140 }
141+ }
142+
143+ private void RecalculateColors ( CelestialBody targetBody , Resource resource )
144+ {
145+ // Longitude |||
146+ const int nbLong = 360 ;
147+ #region Vertices
148+ var colors = _mesh . colors32 ;
130149
150+ colors [ 0 ] = CalculateColor32At ( targetBody , resource , 90 , 0 ) ;
151+ for ( int lat = _currentLat ; lat < _currentLat + 2 ; lat ++ )
152+ {
153+ for ( int lon = 0 ; lon <= nbLong ; lon ++ )
154+ {
155+ colors [ lon + lat * ( nbLong + 1 ) + 1 ] = CalculateColor32At ( targetBody , resource , 90 - lat , lon ) ;
156+ }
157+ }
158+ colors [ colors . Length - 1 ] = CalculateColor32At ( targetBody , resource , - 90 , 0 ) ;
159+ #endregion
160+ _currentLat += 2 ;
161+ if ( _currentLat >= 180 )
162+ {
163+ _currentLat = 0 ;
164+ }
165+ _mesh . colors32 = colors ;
131166 }
132167
133168 public void OnGUI ( )
@@ -138,13 +173,18 @@ public void OnGUI()
138173 {
139174 _mouseCoords = GetMouseCoordinates ( targetBody ) ;
140175 mouse = Event . current . mousePosition ;
176+ if ( UseScansat && _scansatIsCoveredDelegate != null && _mouseCoords != null &&
177+ ! _scansatIsCoveredDelegate ( _mouseCoords . Longitude , _mouseCoords . Latitude , targetBody , GetScansatId ( _selectedResourceName . ScansatName ) ) )
178+ {
179+ _mouseCoords = null ;
180+ }
141181 }
142182 if ( _mouseCoords != null )
143183 {
144184
145- var id = new System . Random ( ) . Next ( 65536 ) + Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name . GetHashCode ( ) + "tooltip" . GetHashCode ( ) ;
185+ _toolTipId = 0 ;
146186 var abundance = ORSPlanetaryResourceMapData . getResourceAvailabilityByRealResourceName (
147- targetBody . flightGlobalsIndex , _selectedResourceName , _mouseCoords . Latitude , _mouseCoords . Longitude )
187+ targetBody . flightGlobalsIndex , _selectedResourceName . ResourceName , _mouseCoords . Latitude , _mouseCoords . Longitude )
148188 . getAmount ( ) ;
149189 string abundanceString ;
150190 if ( abundance > 0.001 )
@@ -155,13 +195,13 @@ public void OnGUI()
155195 {
156196 abundanceString = ( abundance * 1000000.0 ) . ToString ( "0.0" ) + "ppm" ;
157197 }
158- GUI . Window ( id , new Rect ( mouse . x + 10 , mouse . y + 10 , 200f , 55f ) , i =>
198+ GUI . Window ( _toolTipId , new Rect ( mouse . x + 10 , mouse . y + 10 , 200f , 55f ) , i =>
159199 {
160200 GUI . Label ( new Rect ( 5 , 10 , 190 , 20 ) , "Long: " + _mouseCoords . Longitude . ToString ( "###.##" ) + " Lat: " + _mouseCoords . Latitude . ToString ( "####.##" ) ) ;
161201 GUI . Label ( new Rect ( 5 , 30 , 190 , 20 ) , "Amount: " + abundanceString ) ;
162202
163203 } ,
164- _selectedResourceName ) ;
204+ _selectedResourceName . ResourceName ) ;
165205 }
166206 }
167207 }
@@ -210,10 +250,8 @@ public static Coordinates GetMouseCoordinates(CelestialBody targetBody)
210250 }
211251
212252
213- private void evilmesh ( CelestialBody targetBody , string resourceName )
253+ private void evilmesh ( CelestialBody targetBody , Resource resource )
214254 {
215- int bodyIndex = FlightGlobals . Bodies . IndexOf ( targetBody ) ;
216-
217255 _mesh . Clear ( ) ;
218256
219257 const float radius = 1f ;
@@ -229,35 +267,25 @@ private void evilmesh(CelestialBody targetBody, string resourceName)
229267 float _2pi = _pi * 2f ;
230268
231269 vertices [ 0 ] = Vector3 . up * radius ;
232- colors [ 0 ] = new Color32 ( 0 , 0 , 0 , 0 ) ;
270+ colors [ 0 ] = CalculateColor32At ( targetBody , resource , 90 , 0 ) ;
233271 for ( int lat = 0 ; lat < nbLat ; lat ++ )
234272 {
235- float a1 = _pi * ( float ) ( lat + 1 ) / ( nbLat + 1 ) ;
273+ float a1 = _pi * ( lat + 1 ) / ( nbLat + 1 ) ;
236274 float sin1 = Mathf . Sin ( a1 ) ;
237275 float cos1 = Mathf . Cos ( a1 ) ;
238276
239277 for ( int lon = 0 ; lon <= nbLong ; lon ++ )
240278 {
241- float a2 = _2pi * ( float ) ( lon == nbLong ? 0 : lon ) / nbLong ;
279+ float a2 = _2pi * ( lon == nbLong ? 0 : lon ) / nbLong ;
242280 float sin2 = Mathf . Sin ( a2 ) ;
243281 float cos2 = Mathf . Cos ( a2 ) ;
244282
245283 vertices [ lon + lat * ( nbLong + 1 ) + 1 ] = new Vector3 ( sin1 * cos2 , cos1 , sin1 * sin2 ) * radius ;
246- var avail = ORSPlanetaryResourceMapData . getResourceAvailabilityByRealResourceName ( bodyIndex , resourceName , 90 - lat , lon ) ;
247- var amount = avail . getAmount ( ) ;
248- amount = Mathf . Clamp ( ( float ) amount * 500000f , 0f , 255f ) ;
249- if ( amount > 0.0 )
250- {
251- colors [ lon + lat * ( nbLong + 1 ) + 1 ] = new Color32 ( Convert . ToByte ( amount ) , byte . MinValue , byte . MinValue , 70 ) ;
252- }
253- else
254- {
255- colors [ lon + lat * ( nbLong + 1 ) + 1 ] = new Color32 ( byte . MinValue , byte . MinValue , byte . MinValue , 0 ) ;
256- }
284+ colors [ lon + lat * ( nbLong + 1 ) + 1 ] = CalculateColor32At ( targetBody , resource , 90 - lat , lon ) ;
257285 }
258286 }
259287 vertices [ vertices . Length - 1 ] = Vector3 . up * - radius ;
260- colors [ vertices . Length - 1 ] = new Color32 ( 0 , 0 , 0 , 0 ) ;
288+ colors [ vertices . Length - 1 ] = CalculateColor32At ( targetBody , resource , - 90 , 0 ) ;
261289 #endregion
262290
263291 #region Normales
@@ -326,6 +354,29 @@ private void evilmesh(CelestialBody targetBody, string resourceName)
326354 _mesh . Optimize ( ) ;
327355 }
328356
357+ private Color32 CalculateColor32At ( CelestialBody body , Resource resource , double latitude ,
358+ double longitude )
359+ {
360+ if ( UseScansat && _scansatIsCoveredDelegate != null &&
361+ ! _scansatIsCoveredDelegate ( longitude , latitude , body , GetScansatId ( resource . ScansatName ) ) )
362+ {
363+ return new Color32 ( 0 , 0 , 0 , 0 ) ;
364+ }
365+
366+
367+ var avail = ORSPlanetaryResourceMapData . getResourceAvailabilityByRealResourceName ( body . flightGlobalsIndex , resource . ResourceName , latitude , longitude ) ;
368+ var amount = avail . getAmount ( ) ;
369+ amount = Mathf . Clamp ( ( float ) amount * 500000f , 0f , 255f ) ;
370+ if ( amount > 0.0 )
371+ {
372+ return new Color32 ( Convert . ToByte ( amount ) , byte . MinValue , byte . MinValue , 70 ) ;
373+ }
374+ else
375+ {
376+ return new Color32 ( byte . MinValue , byte . MinValue , byte . MinValue , 0 ) ;
377+ }
378+ }
379+
329380 private static CelestialBody GetTargetBody ( MapObject target )
330381 {
331382 if ( target . type == MapObject . MapObjectType . CELESTIALBODY )
@@ -340,15 +391,15 @@ private static CelestialBody GetTargetBody(MapObject target)
340391 public bool Show
341392 {
342393 get { return _show ; }
343- set { _show = value ; }
394+ set { _show = value ; _changed = true ; }
344395 }
345396
346- public List < string > Resources
397+ public List < Resource > Resources
347398 {
348399 get { return _resources ; }
349400 }
350401
351- public string SelectedResourceName
402+ public Resource SelectedResourceName
352403 {
353404 get { return _selectedResourceName ; }
354405 set
@@ -358,7 +409,41 @@ public string SelectedResourceName
358409 }
359410 }
360411
412+ public bool UseScansat
413+ {
414+ get { return _useScansat ; }
415+ set { _useScansat = value ; _changed = true ; }
416+ }
417+
418+ private int GetScansatId ( string resourceName )
419+ {
420+ if ( _scansatEnum != null )
421+ {
422+ return ( int ) _scansatEnum . GetField ( resourceName ) . GetValue ( null ) ;
423+ }
424+ return 0 ;
425+ }
361426
427+ private void InitializeScansatIntegration ( )
428+ {
429+ var scanutil = AssemblyLoader . loadedAssemblies . SelectMany ( x => x . assembly . GetExportedTypes ( ) )
430+ . FirstOrDefault ( x => x . FullName == "SCANsat.SCANUtil" ) ;
431+ var scandata =
432+ AssemblyLoader . loadedAssemblies . SelectMany ( x => x . assembly . GetExportedTypes ( ) )
433+ . FirstOrDefault ( x => x . FullName == "SCANsat.SCANdata" ) ;
434+ if ( scanutil != null && scandata != null )
435+ {
436+ var method = scanutil . GetMethod ( "isCovered" ,
437+ new [ ] { typeof ( double ) , typeof ( double ) , typeof ( CelestialBody ) , typeof ( int ) } ) ;
438+ if ( method != null )
439+ {
440+ _scansatIsCoveredDelegate = ( IsCoveredDelegate ) Delegate . CreateDelegate ( typeof ( IsCoveredDelegate ) , method ) ;
441+ }
442+ var tester = scandata . GetNestedType ( "SCANtype" ) ;
443+ _scansatEnum = tester ;
444+
445+ }
446+ }
362447 }
363448
364449 public class MapOverlayGui : Window < MapOverlayGui >
@@ -374,15 +459,43 @@ public MapOverlayGui(MapOverlay model)
374459
375460 protected override void DrawWindowContents ( int windowId )
376461 {
377- if ( GUILayout . Button ( "Toggle Overlay" ) )
462+ GUILayout . BeginVertical ( ) ;
463+ if ( Model . Show )
464+ {
465+ if ( GUILayout . Button ( "Disable Overlay" ) )
466+ {
467+ Model . Show = false ;
468+ }
469+ }
470+ else
378471 {
379- Model . Show = ! Model . Show ;
472+ if ( GUILayout . Button ( "Enable Overlay" ) )
473+ {
474+ Model . Show = true ;
475+ }
380476 }
477+ if ( Model . UseScansat )
478+ {
479+ if ( GUILayout . Button ( "Disable Scansat" ) )
480+ {
481+ Model . UseScansat = false ;
482+ }
483+ }
484+ else
485+ {
486+ if ( GUILayout . Button ( "Enable Scansat" ) )
487+ {
488+ Model . UseScansat = true ;
489+ }
490+ }
491+
492+
493+
381494 GUILayout . Box ( "" ) ;
382- GUILayout . BeginVertical ( ) ;
495+
383496 foreach ( var res in Model . Resources )
384497 {
385- if ( GUILayout . Button ( res ) )
498+ if ( GUILayout . Button ( res . ResourceName ) )
386499 {
387500 Model . SelectedResourceName = res ;
388501 }
@@ -413,4 +526,15 @@ public double Longitude
413526 }
414527 }
415528
529+ public class Resource
530+ {
531+ public string ResourceName { get ; set ; }
532+ public string ScansatName { get ; set ; }
533+
534+ public Resource ( string resourceName , string scansatName = null )
535+ {
536+ ResourceName = resourceName ;
537+ ScansatName = scansatName ?? resourceName ;
538+ }
539+ }
416540}
0 commit comments