package zmaster587.advancedRocketry.world.decoration;

import net.minecraft.block.Block;
import net.minecraft.world.World;
import net.minecraft.world.gen.MapGenBase;

public class MapGenCrater extends MapGenBase {
    
	int chancePerChunk;
	
	public MapGenCrater(int chancePerChunk) {
		this.chancePerChunk = chancePerChunk;
	}
	
	@Override
	protected void func_151538_a(World world, int rangeX,
			int rangeZ, int chunkX, int chunkZ,
			Block[] chunkArray) {
		
		if(rand.nextInt(chancePerChunk) == Math.abs(rangeX) % chancePerChunk && rand.nextInt(chancePerChunk) == Math.abs(rangeZ) % chancePerChunk) {

			int radius = rand.nextInt(56) + 8; //64; 8 -> 64
			
			//TODO: make hemisphere from surface and line the side with ore of some kind

			int depth = radius*radius;
			
			int xCoord = -rangeX + chunkX;
			int zCoord =  -rangeZ + chunkZ;
			
			for(int x = 15; x >= 0; x--) {
				for(int z = 15; z >= 0; z--) {
					for(int y = 254; y >= 0; y--) {
						int index = (x * 16 + z) * 256 + y;
						
						if(chunkArray[index] != null && chunkArray[index].isOpaqueCube()) {
							int count = ( depth - ( ((xCoord*16)+x)*((xCoord*16)+x) + ((zCoord*16)+z)*((zCoord*16)+z) ) )/(radius*2);
							
							for(int dist = 0; dist < count; dist++) {
								index = (x * 16 + z) * 256 + y-dist;
								chunkArray[index] = null;
							}
							
							int ridgeSize = 12;
							
							if(count <= 0 && count > -2*ridgeSize) {

								
								for(int dist = 0; dist < ((ridgeSize*ridgeSize) - (count+ridgeSize)*(count+ridgeSize))/(ridgeSize*2); dist++) {
									index = (x * 16 + z) * 256 + y + dist;
									chunkArray[index] = world.getBiomeGenForCoords(rangeX, rangeZ).topBlock;
								}
								
								
							}
							
							if(count > 1)
								chunkArray[(x * 16 + z) * 256 + y-count] = world.getBiomeGenForCoords(rangeX, rangeZ).topBlock;
							break;
						}
					}
				}
			}
		}
	}
}
@@ -211,13 +211,9 @@
 e %7B%0A
-    
 %0A
+
 %09int
@@ -229,18 +229,17 @@
 rChunk;%0A
-%09
 %0A
+
 %09public 
@@ -316,18 +316,17 @@
 unk;%0A%09%7D%0A
-%09
 %0A
+
 %09@Overri
@@ -447,19 +447,17 @@
 rray) %7B%0A
-%09%09
 %0A
+
 %09%09if(ran
@@ -644,20 +644,17 @@
 8 -%3E 64%0A
-%09%09%09
 %0A
+
 %09%09%09//TOD
@@ -756,20 +756,17 @@
 radius;%0A
-%09%09%09
 %0A
+
 %09%09%09int x
@@ -826,20 +826,17 @@
 chunkZ;%0A
-%09%09%09
 %0A
+
 %09%09%09for(i
@@ -975,23 +975,17 @@
 56 + y;%0A
-%09%09%09%09%09%09
 %0A
+
 %09%09%09%09%09%09if
@@ -1163,24 +1163,17 @@
 ius*2);%0A
-%09%09%09%09%09%09%09
 %0A
+
 %09%09%09%09%09%09%09f
@@ -1205,32 +1205,58 @@
 ount; dist++) %7B%0A
+%09%09%09%09%09%09%09%09if(y-dist %3E 2) %7B%0A%09
 %09%09%09%09%09%09%09%09index = 
@@ -1284,32 +1284,33 @@
 y-dist;%0A%09%09%09%09%09%09%09%09
+%09
 chunkArray%5Bindex
@@ -1319,16 +1319,26 @@
 = null;%0A
+%09%09%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09%09%7D
@@ -1334,32 +1334,25 @@
 %09%7D%0A%09%09%09%09%09%09%09%7D%0A
-%09%09%09%09%09%09%09
 %0A
+
 %09%09%09%09%09%09%09int r
@@ -1366,24 +1366,17 @@
 e = 12;%0A
-%09%09%09%09%09%09%09
 %0A
+
 %09%09%09%09%09%09%09i
@@ -1416,25 +1416,17 @@
 ize) %7B%0A%0A
-%09%09%09%09%09%09%09%09
 %0A
+
 %09%09%09%09%09%09%09%09
@@ -1681,25 +1681,9 @@
 %09%09%7D%0A
-%09%09%09%09%09%09%09%09%0A%09%09%09%09%09%09%09%09
+%0A
 %0A%09%09%09
@@ -1688,23 +1688,16 @@
 %09%09%09%09%09%09%7D%0A
-%09%09%09%09%09%09%09
 %0A%09%09%09%09%09%09%09

Fix possible issue with negative y values, possible fix for #529