11using KSP . Game ;
2- using KSP . Sim . Definitions ;
2+ using KSP . Sim . impl ;
33
44namespace MicroMod
55{
@@ -16,7 +16,7 @@ internal bool GetBodies()
1616 if ( this . Bodies . Count > 0 )
1717 return true ;
1818
19- Dictionary < string , CelestialBodyCore > bodies = GameManager . Instance ? . Game ? . CelestialBodies ? . GetAllBodiesData ( ) ;
19+ List < CelestialBodyComponent > bodies = GameManager . Instance ? . Game ? . UniverseModel ? . GetAllCelestialBodies ( ) ;
2020
2121 if ( bodies == null || bodies . Count == 0 )
2222 return false ;
@@ -25,37 +25,118 @@ internal bool GetBodies()
2525 {
2626 this . Bodies . Add ( new CelestialBody
2727 {
28- Name = body . Value . data . bodyName ,
29- GravityASL = body . Value . data . gravityASL ,
30- HasAtmosphere = body . Value . data . hasAtmosphere ,
31- IsHomeWorld = body . Value . data . isHomeWorld
28+ Name = body . Name ,
29+ DisplayName = body . Name ,
30+ GravityASL = body . gravityASL ,
31+ HasAtmosphere = body . hasAtmosphere ,
32+ IsHomeWorld = body . isHomeWorld ,
33+ CelestialBodyComponent = body ,
3234 } ) ;
3335 }
3436
37+ // Reorder and format all celestial bodies so they form a tree-like structure
38+ TryReorderBodies ( ) ;
39+
3540 return true ;
3641 }
3742
43+ private void TryReorderBodies ( )
44+ {
45+ // Grab all stars in CelestialBodies
46+ List < CelestialBodyComponent > stars = Bodies
47+ . Select ( b => b . CelestialBodyComponent )
48+ . Where ( c => c . IsStar )
49+ . ToList ( ) ;
50+
51+ if ( stars == null || stars . Count == 0 )
52+ return ;
53+
54+ List < CelestialBody > reorderedBodies = new ( ) ;
55+
56+ // Iterate through all stars and grab all planets and moons that are orbiting them
57+ foreach ( var star in stars )
58+ {
59+ reorderedBodies . AddRange ( InstantiateCelestialBodies ( star , 0 ) ) ;
60+ }
61+
62+ this . Bodies = reorderedBodies ;
63+ }
64+
65+ /// <summary>
66+ /// Instantiates a list of planets and moons in the CelestialBodyComponent + bodies that are orbiting it
67+ /// </summary>
68+ /// <param name="cel"></param>
69+ /// <param name="level">Indicates how much indentation format needs to have</param>
70+ /// <returns></returns>
71+ private List < CelestialBody > InstantiateCelestialBodies ( CelestialBodyComponent cel , int level )
72+ {
73+ List < CelestialBody > instantiatedBodies = new ( ) ;
74+ instantiatedBodies . Add ( InstantiateCelestialBody ( cel , level ) ) ;
75+
76+ foreach ( CelestialBodyComponent body in cel . orbitingBodies )
77+ {
78+ instantiatedBodies . AddRange ( InstantiateCelestialBodies ( body , level + 1 ) ) ;
79+ }
80+
81+ return instantiatedBodies ;
82+ }
83+
84+
85+ /// <summary>
86+ /// Instantiates a single CelestialBody and formats it according to level of indentation
87+ /// </summary>
88+ /// <param name="cel"></param>
89+ /// <param name="level">Indicates how much indentation format needs to have</param>
90+ /// <returns></returns>
91+ private CelestialBody InstantiateCelestialBody ( CelestialBodyComponent cel , int level )
92+ {
93+ CelestialBody body = new ( )
94+ {
95+ Name = cel . Name ,
96+ DisplayName = cel . Name ,
97+ GravityASL = cel . gravityASL ,
98+ HasAtmosphere = cel . hasAtmosphere ,
99+ IsHomeWorld = cel . isHomeWorld ,
100+ CelestialBodyComponent = cel ,
101+ } ;
102+
103+ if ( cel . isHomeWorld )
104+ body . DisplayName = $ "{ body . DisplayName } *";
105+
106+ if ( level > 0 )
107+ {
108+ body . DisplayName = $ "└ { body . DisplayName } ";
109+
110+ for ( int i = 0 ; i < level ; i ++ )
111+ body . DisplayName = $ " { body . DisplayName } ";
112+ }
113+
114+ return body ;
115+ }
116+
38117 /// <summary>
39118 /// Calculates what factor needs to be used for HomeWorld's TWR in order to compensate for gravity of the selected body
40119 /// </summary>
41120 /// <param name="bodyName">Name of the CelestialBody for which the TWR factor is calculated</param>
42- /// <returns>TWR factor that needs to be multiplied with HomeWorld's TWR to get TWR at the selected body and information if target body has an atmosphere </returns>
43- internal ( double twrFactor , bool hasAtmosphere ) GetTwrFactor ( string bodyName )
121+ /// <returns>TWR factor that needs to be multiplied with HomeWorld's TWR to get TWR at the selected body</returns>
122+ internal double GetTwrFactor ( string bodyName )
44123 {
45- if ( Bodies . Count == 0 ) return ( 0 , false ) ;
124+ if ( Bodies . Count == 0 ) return 0 ;
46125 CelestialBody homeWorld = Bodies . Find ( b => b . IsHomeWorld ) ;
47126 CelestialBody targetBody = Bodies . Find ( t => t . Name . ToLowerInvariant ( ) == bodyName . ToLowerInvariant ( ) ) ?? null ;
48- if ( targetBody == null ) return ( 0 , false ) ;
127+ if ( targetBody == null ) return 0 ;
49128
50- return ( homeWorld . GravityASL / targetBody . GravityASL , targetBody . HasAtmosphere ) ;
129+ return homeWorld . GravityASL / targetBody . GravityASL ;
51130 }
52131 }
53132
54133 internal class CelestialBody
55134 {
56- public string Name ;
57- public double GravityASL ;
58- public bool HasAtmosphere ;
59- public bool IsHomeWorld ;
135+ internal string Name ;
136+ internal string DisplayName ;
137+ internal double GravityASL ;
138+ internal bool HasAtmosphere ;
139+ internal bool IsHomeWorld ;
140+ internal CelestialBodyComponent CelestialBodyComponent ;
60141 }
61142}
0 commit comments