package common.pathfinding;

import common.init.Blocks;
import common.util.BlockPos;
import common.world.Chunk;
import common.world.IBlockAccess;
import common.world.State;
import common.world.World;

public class PathCache implements IBlockAccess
{
    protected final int chunkX;
    protected final int chunkZ;
    protected final Chunk[][] data;
    
    public PathCache(World world, BlockPos pos1, BlockPos pos2)
    {
        this.chunkX = pos1.getX() >> 4;
        this.chunkZ = pos1.getZ() >> 4;
        int cx2 = pos2.getX() >> 4;
        int cz2 = pos2.getZ() >> 4;
        this.data = new Chunk[cx2 - this.chunkX + 1][cz2 - this.chunkZ + 1];
        for (int x = this.chunkX; x <= cx2; ++x)
        {
            for (int z = this.chunkZ; z <= cz2; ++z)
            {
                this.data[x - this.chunkX][z - this.chunkZ] = world.getChunk(x, z);
            }
        }
    }

    public State getState(BlockPos pos)
    {
        if (pos.getY() >= -World.MAX_SIZE_Y && pos.getY() < World.MAX_SIZE_Y)
        {
            int i = (pos.getX() >> 4) - this.chunkX;
            int j = (pos.getZ() >> 4) - this.chunkZ;

            if (i >= 0 && i < this.data.length && j >= 0 && j < this.data[i].length)
            {
                Chunk chunk = this.data[i][j];

                if (chunk != null)
                {
                    return chunk.getState(pos);
                }
            }
        }

        return Blocks.air.getState();
    }
}
