1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | // // Kerbal Engineer Redux // // Copyright (C) 2014 CYBUTEK // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. // namespace KerbalEngineer { using System; using System.Collections.Generic; using System.Linq; /* * * With thanks to Nathaniel R. Lewis (aka. Teknoman117) (linux.robotdude@gmail.com) for working out * the best way of getting the celestial body information dynamically using PSystemManager. * */ public static class CelestialBodies { static CelestialBodies() { try { SystemBody = new BodyInfo(PSystemManager.Instance.localBodies.Find(b => b.referenceBody == null || b.referenceBody == b)); if (!SetSelectedBody("Kerbin")) { SelectedBody = SystemBody; SelectedBody.SetSelected(true); } } catch (Exception ex) { Logger.Exception(ex); } } public static BodyInfo SelectedBody { get; private set; } public static BodyInfo SystemBody { get; private set; } /// <summary> /// Gets a body given a supplied body name. /// </summary> public static BodyInfo GetBodyInfo(string bodyName) { try { return SystemBody.GetBodyInfo(bodyName); } catch (Exception ex) { Logger.Exception(ex); } return null; } /// <summary> /// Sets the selected body to one matching the supplied body name. Returns true if successful. /// </summary> public static bool SetSelectedBody(string bodyName) { try { BodyInfo body = GetBodyInfo(bodyName); if (body != null) { if (SelectedBody != null) { SelectedBody.SetSelected(false); } SelectedBody = body; SelectedBody.SetSelected(true); return true; } } catch (Exception ex) { Logger.Exception(ex); } return false; } public class BodyInfo { public BodyInfo(CelestialBody body, BodyInfo parent = null) { try { // Set the body information. CelestialBody = body; Name = body.bodyName; Gravity = 9.81 * body.GeeASL; Parent = parent; // Set orbiting bodies information. Children = new List<BodyInfo>(); foreach (CelestialBody orbitingBody in body.orbitingBodies) { Children.Add(new BodyInfo(orbitingBody, this)); } SelectedDepth = 0; } catch (Exception ex) { Logger.Exception(ex); } } public CelestialBody CelestialBody { get; private set; } public List<BodyInfo> Children { get; private set; } public double Gravity { get; private set; } public string Name { get; private set; } public BodyInfo Parent { get; private set; } public bool Selected { get; private set; } public int SelectedDepth { get; private set; } public BodyInfo GetBodyInfo(string bodyName) { try { // This is the searched body. if (String.Equals(Name, bodyName, StringComparison.CurrentCultureIgnoreCase)) { return this; } // Check to see if any of this bodies children are the searched body. foreach (BodyInfo child in Children) { BodyInfo body = child.GetBodyInfo(bodyName); if (body != null) { return body; } } } catch (Exception ex) { Logger.Exception(ex); } // A body with the specified name was not found. return null; } public double GetDensity(double altitude) { return CelestialBody.GetDensity(GetPressure(altitude), GetTemperature(altitude)); } public double GetPressure(double altitude) { return CelestialBody.GetPressure(altitude); } public double GetTemperature(double altitude) { return CelestialBody.GetTemperature(altitude); } public double GetAtmospheres(double altitude) { return GetPressure(altitude) * PhysicsGlobals.KpaToAtmospheres; } public void SetSelected(bool state, int depth = 0) { Selected = state; SelectedDepth = depth; if (Parent != null) { Parent.SetSelected(state, depth + 1); } } public override string ToString() { string log = "\n" + Name + "\n\tGravity: " + Gravity + "\n\tSelected: " + Selected; return Children.Aggregate(log, (current, child) => current + "\n" + child); } } } } |