@@ -18,6 +18,7 @@ permissions and limitations under the Licenses.
1818using System ;
1919using MCGalaxy . Network ;
2020using MCGalaxy . Util ;
21+ using MCGalaxy . Util . Imaging ;
2122
2223namespace MCGalaxy . Generator
2324{
@@ -37,49 +38,47 @@ public static bool Generate(Player p, Level lvl, MapGenArgs args) {
3738
3839 byte [ ] data = HttpUtil . DownloadImage ( url , p ) ;
3940 if ( data == null ) return false ;
40- IBitmap2D bmp = ImageUtils . DecodeImage ( data , p ) ;
41+
42+ Bitmap2D bmp = ImageUtils . DecodeImage ( data , p ) ;
4143 if ( bmp == null ) return false ;
4244
4345 int index = 0 , oneY = lvl . Width * lvl . Length ;
4446 int lvlWidth = lvl . Width ;
4547 int lvlLength = lvl . Length ;
4648
47- using ( bmp ) {
48- bool resized = bmp . Width != lvlWidth || bmp . Height != lvlLength ;
49-
50- if ( resized ) {
51- p . Message ( "&cHeightmap size ({0}x{1}) does not match Width x Length ({2}x{3}) of the level" ,
52- bmp . Width , bmp . Height , lvlWidth , lvlLength ) ;
53- p . Message ( "&cAs such, the map may not look accurate." ) ;
54- }
55- bmp . LockBits ( ) ;
56-
57- byte [ ] hmap = resized ? ResizeHeightmap ( bmp , lvlWidth , lvlLength )
58- : ComputeHeightmap ( bmp ) ;
49+ bool resized = bmp . Width != lvlWidth || bmp . Height != lvlLength ;
50+
51+ if ( resized ) {
52+ p . Message ( "&cHeightmap size ({0}x{1}) does not match Width x Length ({2}x{3}) of the level" ,
53+ bmp . Width , bmp . Height , lvlWidth , lvlLength ) ;
54+ p . Message ( "&cAs such, the map may not look accurate." ) ;
55+ }
56+
57+ byte [ ] hmap = resized ? ResizeHeightmap ( bmp , lvlWidth , lvlLength )
58+ : ComputeHeightmap ( bmp ) ;
5959
60- for ( int z = 0 ; z < lvlLength ; z ++ )
61- for ( int x = 0 ; x < lvlWidth ; x ++ )
60+ for ( int z = 0 ; z < lvlLength ; z ++ )
61+ for ( int x = 0 ; x < lvlWidth ; x ++ )
62+ {
63+ int height = hmap [ index ] ;
64+ byte layer = biome . Ground , top = biome . Surface ;
65+
66+ if (
67+ IsCliff ( height , hmap , lvl , x - 1 , z ) ||
68+ IsCliff ( height , hmap , lvl , x + 1 , z ) ||
69+ IsCliff ( height , hmap , lvl , x , z - 1 ) ||
70+ IsCliff ( height , hmap , lvl , x , z + 1 ) )
6271 {
63- int height = hmap [ index ] ;
64- byte layer = biome . Ground , top = biome . Surface ;
65-
66- if (
67- IsCliff ( height , hmap , lvl , x - 1 , z ) ||
68- IsCliff ( height , hmap , lvl , x + 1 , z ) ||
69- IsCliff ( height , hmap , lvl , x , z - 1 ) ||
70- IsCliff ( height , hmap , lvl , x , z + 1 ) )
71- {
72- layer = biome . Cliff ; top = biome . Cliff ;
73- }
74-
75- // remap from 0..255 to 0..lvl.Height
76- height = height * lvl . Height / 255 ;
77- for ( int y = 0 ; y < height - 1 ; y ++ )
78- lvl . blocks [ index + oneY * y ] = layer ;
79- if ( height > 0 )
80- lvl . blocks [ index + oneY * ( height - 1 ) ] = top ;
81- index ++ ;
72+ layer = biome . Cliff ; top = biome . Cliff ;
8273 }
74+
75+ // remap from 0..255 to 0..lvl.Height
76+ height = height * lvl . Height / 255 ;
77+ for ( int y = 0 ; y < height - 1 ; y ++ )
78+ lvl . blocks [ index + oneY * y ] = layer ;
79+ if ( height > 0 )
80+ lvl . blocks [ index + oneY * ( height - 1 ) ] = top ;
81+ index ++ ;
8382 }
8483 return true ;
8584 }
@@ -91,20 +90,21 @@ static bool IsCliff(int height, byte[] hmap, Level lvl, int x, int z) {
9190 return height >= neighbourHeight + 2 ;
9291 }
9392
94- static byte [ ] ComputeHeightmap ( IBitmap2D bmp ) {
93+ static byte [ ] ComputeHeightmap ( Bitmap2D bmp ) {
9594 byte [ ] hmap = new byte [ bmp . Width * bmp . Height ] ;
9695 int i = 0 ;
9796
9897 for ( int y = 0 ; y < bmp . Height ; y ++ )
9998 for ( int x = 0 ; x < bmp . Width ; x ++ )
10099 {
101- hmap [ i ++ ] = bmp . Get ( x , y ) . R ;
100+ hmap [ i ] = bmp . Pixels [ i ] . R ;
101+ i ++ ;
102102 }
103103 return hmap ;
104104 }
105105
106106 // Calculates adjusted X/Y coordinates using nearest neighbour resizing
107- static byte [ ] ResizeHeightmap ( IBitmap2D bmp , int dstWidth , int dstHeight ) {
107+ static byte [ ] ResizeHeightmap ( Bitmap2D bmp , int dstWidth , int dstHeight ) {
108108 byte [ ] hmap = new byte [ dstWidth * dstHeight ] ;
109109 int i = 0 ;
110110
0 commit comments