From 071a3d99b9f8a1f0c896fb17a8066e7d193d64a4 Mon Sep 17 00:00:00 2001
From: curt <curt>
Date: Tue, 25 Nov 2003 01:08:39 +0000
Subject: [PATCH] The code to find the highest hit below you didn't work quite
 right when reinitializing at a new location.  This should fix the problem.

---
 src/Scenery/hitlist.cxx | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/src/Scenery/hitlist.cxx b/src/Scenery/hitlist.cxx
index bdbb74e5e..aa0e3b546 100644
--- a/src/Scenery/hitlist.cxx
+++ b/src/Scenery/hitlist.cxx
@@ -527,9 +527,11 @@ bool fgCurrentElev( sgdVec3 abs_view_pos, double max_alt_m,
     hit_list->Intersect( globals->get_scenery()->get_terrain_branch(),
                          orig, dir );
 
-    int this_hit=0;
+    int this_hit = -1;
+    int max_hit = -1;
     Point3D geoc;
     double hit_elev = -9999;
+    double max_elev = -9999;
     Point3D sc(scenery_center[0], scenery_center[1], scenery_center[2]) ;
 
     int hitcount = hit_list->num_hits();
@@ -547,6 +549,16 @@ bool fgCurrentElev( sgdVec3 abs_view_pos, double max_alt_m,
             hit_elev = alt;
             this_hit = i;
         }
+        if ( alt > hit_elev ) {
+            max_elev = alt;
+            max_hit = i;
+        }
+    }
+
+    if ( this_hit < 0 ) {
+        // no hits below us, take the max hit 
+        this_hit = max_hit;
+        hit_elev = max_elev;
     }
 
     if ( hit_elev > -9000 ) {
@@ -607,9 +619,11 @@ bool fgCurrentElev( sgdVec3 abs_view_pos, double max_alt_m,
     sgdSetMat4(xform,fxform);
     hit_list->Intersect( terra_transform, xform, orig, dir );
 
-    int this_hit=0;
+    int this_hit = -1;
+    int max_hit = -1;
     Point3D geoc;
     double hit_elev = -9999;
+    double max_elev = -9999;
     Point3D sc(scenery_center[0], scenery_center[1], scenery_center[2]) ;
 
     int hitcount = hit_list->num_hits();
@@ -627,6 +641,16 @@ bool fgCurrentElev( sgdVec3 abs_view_pos, double max_alt_m,
             this_hit = i;
             // cout << "  it's a keeper" << endl;
         }
+        if ( alt > hit_elev ) {
+            max_elev = alt;
+            max_hit = i;
+        }
+    }
+
+    if ( this_hit < 0 ) {
+        // no hits below us, take the max hit 
+        this_hit = max_hit;
+        hit_elev = max_elev;
     }
 
     if ( hit_elev > -9000 ) {