package de.domisum.exziff;

import de.domisum.exziff.island.heightmap.IslandHeightMapGenerator;
import de.domisum.exziff.island.shape.IslandShapeGenerator;
import de.domisum.exziff.shape.ShapeMap;
import de.domisum.exziff.shape.exporter.ShapeMapImageExporter;
import de.domisum.exziff.world.ChunkClusterLoaderSaver;
import de.domisum.exziff.world.World;
import de.domisum.lib.auxilium.util.FileUtil;
import de.domisum.lib.auxilium.util.ImageUtil;
import de.domisum.lib.auxilium.util.math.RandomUtil;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class TestLauncher
{

	public static void main(String[] args)
	{
		//		islandTest();

		worldTest();
	}


	private static void islandTest()
	{
		try
		{
			FileUtil.deleteDirectoryContents(new File("C:\\Users\\domisum\\testChamber\\testIslands/"));
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}

		Random r = new Random(5);
		for(int i = 0; i < 50; i++)
		{
			long seed = r.nextLong();
			exportIsland(seed+"", seed);
		}
	}

	private static void exportIsland(String fileName, long seed)
	{
		System.out.println("fileName: "+fileName+" seed: "+seed);

		// settings
		int size = 1000;

		// generation
		IslandShapeGenerator islandShapeGenerator = new IslandShapeGenerator(size, size, seed);
		ShapeMap shape = islandShapeGenerator.generate();

		IslandHeightMapGenerator heightMapGenerator = new IslandHeightMapGenerator(shape, seed+0x3094);
		//HeightMap heightMap = heightMapGenerator.generate();


		// exporting
		ShapeMapImageExporter exporter = new ShapeMapImageExporter();
		//HeightMapImageExporter exporter = new HeightMapImageExporter();
		BufferedImage image = exporter.export(shape);

		ImageUtil.writeImage(new File("C:\\Users\\domisum\\testChamber\\testIslands/"+fileName+".png"), image);
	}

	private static void worldTest()
	{
		ChunkClusterLoaderSaver loaderSaver = new ChunkClusterLoaderSaver(new File("C:/Users/domisum/testChamber/testWorld"),
				true);
		World world = new World(loaderSaver);

		long startMs = System.currentTimeMillis();

		for(int i = 0; i < 1000; i++)
			for(int j = 0; j < 1000; j++)
			{
				int dist = RandomUtil.getFromRange(1, 5);
				int max = RandomUtil.getFromRange(50, 60);
				for(int y = 0; y <= max; y += dist)
					world.setMaterialIdAndSubId(i, y, j, (byte) 1, (byte) 0);
			}

		System.out.println("afterAction: "+(System.currentTimeMillis()-startMs));

		world.save();

		System.out.println("afterSave: "+(System.currentTimeMillis()-startMs));
	}

}

@@ -305,24 +305,65 @@
 oaderSaver;%0A
+import de.domisum.exziff.world.Material;%0A
 import de.do
@@ -484,61 +484,8 @@
 til;
-%0Aimport de.domisum.lib.auxilium.util.math.RandomUtil;
 %0A%0Aim
@@ -1859,16 +1859,64 @@
 st()%0A%09%7B%0A
+%09%09ShapeMap shapeMap = generateIslandShape(5);%0A%0A%0A
 %09%09ChunkC
@@ -2135,24 +2135,24 @@
 for(int 
-i
+z
  = 0; 
-i
+z
  %3C 1000;
@@ -2152,17 +2152,17 @@
 %3C 1000; 
-i
+z
 ++)%0A%09%09%09f
@@ -2172,16 +2172,16 @@
 int 
-j
+x
  = 0; 
-j
+x
  %3C 1
@@ -2185,17 +2185,17 @@
 %3C 1000; 
-j
+x
 ++)%0A%09%09%09%7B
@@ -2203,96 +2203,124 @@
 %09%09%09%09
-int dist = RandomUtil.getFromRange(1, 5);%0A%09%09%09%09int max = RandomUtil.getFromRange(50, 60);
+Material mat = shapeMap.get(x, z) ? Material.STONE : Material.WATER;%0A%09%09%09%09int height = shapeMap.get(x, z) ? 70 : 65;%0A
 %0A%09%09%09
@@ -2344,22 +2344,19 @@
  %3C= 
-max; y += dist
+height; y++
 )%0A%09%09
@@ -2390,15 +2390,15 @@
 bId(
-i
+x
 , y, 
-j
+z
 , (b
@@ -2402,17 +2402,22 @@
  (byte) 
-1
+mat.id
 , (byte)
@@ -2599,10 +2599,282 @@
 ));%0A%09%7D%0A%0A
+%09private static ShapeMap generateIslandShape(long seed)%0A%09%7B%0A%09%09// settings%0A%09%09int size = 1000;%0A%0A%09%09// generation%0A%09%09IslandShapeGenerator islandShapeGenerator = new IslandShapeGenerator(size, size, seed);%0A%09%09ShapeMap shape = islandShapeGenerator.generate();%0A%0A%09%09return shape;%0A%09%7D%0A%0A
 %7D%0A

Test exporting a island shape to world