package com.khorn.terraincontrol.generator.resourcegens;

import com.khorn.terraincontrol.LocalWorld;
import com.khorn.terraincontrol.TerrainControl;
import com.khorn.terraincontrol.exception.InvalidConfigException;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CactusGen extends Resource
{
    private int minAltitude;
    private int maxAltitude;
    private List<Integer> sourceBlocks;

    @Override
    public void spawn(LocalWorld world, Random rand, boolean villageInChunk, int x, int z)
    {
        int y = rand.nextInt(maxAltitude - minAltitude) + minAltitude;

        for (int i = 0; i < 10; i++)
        {
            int j = x + rand.nextInt(8) - rand.nextInt(8);
            int k = y + rand.nextInt(4) - rand.nextInt(4);
            int m = z + rand.nextInt(8) - rand.nextInt(8);
            if (world.isEmpty(j, k, m))
            {
                int n = 1 + rand.nextInt(rand.nextInt(3) + 1);
                for (int i1 = 0; i1 < n; i1++)
                {
                    int id = world.getTypeId(j, k + i1 - 1, m);
                    if (sourceBlocks.contains(id))
                    {
                        world.setBlock(j, k + i1, m, blockId, blockData, false, false, false);
                    }
                }
            }
        }
    }

    @Override
    public String makeString()
    {
        return "Cactus(" + makeMaterial(blockId, blockData) + "," + frequency + "," + rarity + "," + minAltitude + "," + maxAltitude + makeMaterial(sourceBlocks) + ")";
    }

    @Override
    public void load(List<String> args) throws InvalidConfigException
    {
        assureSize(6, args);

        blockId = readBlockId(args.get(0));
        blockData = readBlockData(args.get(0));
        frequency = readInt(args.get(1), 1, 100);
        rarity = readRarity(args.get(2));
        minAltitude = readInt(args.get(3), TerrainControl.worldDepth, TerrainControl.worldHeight);
        maxAltitude = readInt(args.get(4), minAltitude + 1, TerrainControl.worldHeight);
        sourceBlocks = new ArrayList<Integer>();
        for (int i = 5; i < args.size(); i++)
        {
            sourceBlocks.add(readBlockId(args.get(i)));
        }
    }
}

@@ -674,17 +674,23 @@
     int 
-j
+cactusX
  = x + r
@@ -739,17 +739,27 @@
     int 
-k
+cactusBaseY
  = y + r
@@ -808,17 +808,23 @@
     int 
-m
+cactusZ
  = z + r
@@ -848,32 +848,63 @@
 rand.nextInt(8);
+%0A%0A            // Check position
 %0A            if 
@@ -904,16 +904,17 @@
     if (
+!
 world.is
@@ -923,36 +923,99 @@
 pty(
-j, k, m))%0A            %7B%0A    
+cactusX, cactusBaseY, cactusZ))%0A                continue;%0A%0A            // Check foundation%0A
     
@@ -1030,96 +1030,232 @@
 int 
-n = 1 + rand.nextInt(rand.nextInt(3) + 1);%0A                for (int i1 = 0; i1 %3C n; i1++
+id = world.getTypeId(cactusX, cactusBaseY - 1, cactusZ);%0A            if (!sourceBlocks.contains(id))%0A                continue;%0A%0A            // Check neighbors%0A            if (!world.isEmpty(cactusX - 1, cactusBaseY, cactusZ)
 )%0A  
@@ -1260,33 +1260,41 @@
                 
-%7B
+continue;
 %0A               
@@ -1294,132 +1294,427 @@
     
-        int id = world.getTypeId(j, k + i1 - 1, m);%0A                    if (sourceBlocks.contains(id))%0A                    %7B
+if (!world.isEmpty(cactusX + 1, cactusBaseY, cactusZ))%0A                continue;%0A            if (!world.isEmpty(cactusX, cactusBaseY, cactusZ + 1))%0A                continue;%0A            if (!world.isEmpty(cactusX, cactusBaseY, cactusZ + 1))%0A                continue;%0A%0A            // Spawn cactus%0A            int cactusHeight = 1 + rand.nextInt(rand.nextInt(3) + 1);%0A            for (int dY = 0; dY %3C cactusHeight; dY++)
 %0A   
@@ -1711,32 +1711,37 @@
  dY++)%0A         
+   %7B%0A
                w
@@ -1739,16 +1739,17 @@
         
+ 
 world.se
@@ -1759,20 +1759,42 @@
 ock(
-j, k + i1, m
+cactusX, cactusBaseY + dY, cactusZ
 , bl
@@ -1837,48 +1837,8 @@
 e);%0A
-                    %7D%0A                %7D%0A
     

Improve cactus spawning. Fixes #235.