+
+ while ( (int)tile_cache.size() >= max_cache_size ) {
+ sgdVec3 abs_view_pos;
+ float dist;
+ float max_dist = 0.0;
+ int max_index = -1;
+
+ // we need to free the furthest entry
+ tile_map_iterator current = tile_cache.begin();
+ tile_map_iterator end = tile_cache.end();
+
+ for ( ; current != end; ++current ) {
+ long index = current->first;
+ FGTileEntry *e = current->second;
+
+ if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
+ // calculate approximate distance from view point
+ sgdCopyVec3( abs_view_pos,
+ globals->get_current_view()->get_absolute_view_pos() );
+
+ SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = "
+ << abs_view_pos[0] << ","
+ << abs_view_pos[1] << ","
+ << abs_view_pos[2] );
+ SG_LOG( SG_TERRAIN, SG_DEBUG,
+ " ref point = " << e->center );
+
+ sgdVec3 center;
+ sgdSetVec3( center,
+ e->center.x(), e->center.y(), e->center.z() );
+ dist = sgdDistanceVec3( center, abs_view_pos );
+
+ SG_LOG( SG_TERRAIN, SG_DEBUG, " distance = " << dist );
+
+ if ( dist > max_dist ) {
+ max_dist = dist;
+ max_index = index;
+ }
+ } else {
+ SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
+ << " pending models = " << e->get_pending_models() );
+ }
+ }
+
+ // If we made it this far, then there were no open cache entries.
+ // We will instead free the furthest cache entry and return true
+
+ SG_LOG( SG_TERRAIN, SG_INFO, " max_dist = " << max_dist );
+ SG_LOG( SG_TERRAIN, SG_INFO, " index = " << max_index );
+ if ( max_index >= 0 ) {
+ entry_free( max_index );
+ return true;
+ } else {
+ SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
+ "cache is full, but no entries available for removal." );
+ return false;
+ }
+ }
+
+ SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
+ "FGNewCache::make_space()." );
+ return false;