Added biome and slope readouts.
[VesselSimulator.git] / KerbalEngineer / Flight / Readouts / Surface / Slope.cs
blob:a/KerbalEngineer/Flight/Readouts/Surface/Slope.cs -> blob:b/KerbalEngineer/Flight/Readouts/Surface/Slope.cs
--- a/KerbalEngineer/Flight/Readouts/Surface/Slope.cs
+++ b/KerbalEngineer/Flight/Readouts/Surface/Slope.cs
@@ -1,1 +1,99 @@
-
+// 

+//     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/>.

+// 

+

+#region Using Directives

+

+using System;

+

+using KerbalEngineer.Extensions;

+

+using UnityEngine;

+

+#endregion

+

+namespace KerbalEngineer.Flight.Readouts.Surface

+{

+    public class Slope : ReadoutModule

+    {

+        public Slope()

+        {

+            this.Name = "Slope";

+            this.Category = ReadoutCategory.Surface;

+            this.HelpString = "Shows the slope of the terrain below your vessel.";

+            this.IsDefault = true;

+        }

+

+        public override void Draw()

+        {

+            this.DrawLine(this.GetSlopeAngleAndHeading());

+        }

+

+        private string GetSlopeAngleAndHeading()

+        {

+            try

+            {

+                var result = "--° @ ---°";

+                var mainBody = FlightGlobals.ActiveVessel.mainBody;

+                var rad = (FlightGlobals.ActiveVessel.CoM - mainBody.position).normalized;

+                RaycastHit hit;

+                if (Physics.Raycast(FlightGlobals.ActiveVessel.CoM, -rad, out hit, Mathf.Infinity, 1 << 15)) // Just "Local Scenery" please

+                {

+                    var norm = hit.normal;

+                    norm = norm.normalized;

+                    var raddotnorm = Vector3d.Dot(rad, norm);

+                    if (raddotnorm > 1.0)

+                    {

+                        raddotnorm = 1.0;

+                    }

+                    else if (raddotnorm < 0.0)

+                    {

+                        raddotnorm = 0.0;

+                    }

+                    var slope = Math.Acos(raddotnorm) * 180 / Math.PI;

+                    result = slope.ToAngle("F1");

+                    if (slope < 0.05)

+                    {

+                        result += " @ ---°";

+                    }

+                    else

+                    {

+                        var side = Vector3d.Cross(rad, norm).normalized;

+                        var east = Vector3d.Cross(rad, Vector3d.up).normalized;

+                        var north = Vector3d.Cross(rad, east).normalized;

+                        var sidedoteast = Vector3d.Dot(side, east);

+                        var direction = Math.Acos(sidedoteast) * 180 / Math.PI;

+                        var sidedotnorth = Vector3d.Dot(side, north);

+                        if (sidedotnorth < 0)

+                        {

+                            direction = 360 - direction;

+                        }

+                        result += " @ " + direction.ToAngle("F0");

+                    }

+                }

+

+                return result;

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "Surface->Slope->GetSlopeAngleAndHeading");

+                return "--° @ ---°";

+            }

+        }

+    }

+}