public ushort FindClosestLand( int x, int y, ushort skipid, int range, ProvinceList provinces, AdjacencyTable adjacent ) { int halfwidth = (range >> 1); int halfheight = (range >> 1); ushort closest_id = skipid; // -- Build a bitmap around this point x = Lightmap.BaseNormalizeX( x ); int max_distance = MaxDistance; ushort[,] map = ExportBitmapGrid( x-halfwidth, y-halfheight, range, range ); for ( int my=0; my<range; ++my ) { for ( int mx=0; mx<range; ++mx ) { // -- Skip for TI and pixels belonging to this province if ( map[mx,my] == Province.TerraIncognitaID || map[mx,my] == skipid ) continue; if ( !provinces[map[mx,my]].IsLandNoTI() ) continue; // Only land // Also check if adjacency exists if ( adjacent != null && skipid != 0 ) { int adj = adjacent.GetAdjacencyIndex( skipid, map[mx,my] ); if ( adj < 0 || adj >= 16 ) continue; } // -- This pixel qualifies: check the distance. // Normally, we should need a sqrt() too, but it doesn't really matter here, // as we just want "the smallest" and not how small it actually is. int distance = ((mx-halfwidth)*(mx-halfwidth)) + ((my-halfheight)*(my-halfheight)); if ( distance < max_distance ) { max_distance = distance; closest_id = map[mx,my]; } } } return closest_id; }