<commit_before>package sx.lambda.mstojcevich.voxel.world.chunk

import groovy.transform.CompileStatic
import org.lwjgl.BufferUtils
import org.lwjgl.opengl.GL15
import sx.lambda.mstojcevich.voxel.api.VoxelGameAPI
import sx.lambda.mstojcevich.voxel.api.events.render.EventChunkRender
import sx.lambda.mstojcevich.voxel.block.Block
import sx.lambda.mstojcevich.voxel.block.NormalBlockRenderer
import sx.lambda.mstojcevich.voxel.VoxelGame
import sx.lambda.mstojcevich.voxel.util.Vec3i
import sx.lambda.mstojcevich.voxel.world.IWorld

import java.nio.FloatBuffer
import java.nio.IntBuffer

import static org.lwjgl.opengl.GL11.*

@CompileStatic
public class Chunk implements IChunk {

    private final Block[][][] blockList

    private final transient IWorld parentWorld

    private final int size
    private final int height

    private transient int displayList = -1

    private Vec3i startPosition

    private int highestPoint

    private int blockCount
    private transient int numVisibleSides = 0

    private static final boolean USE_VBO = true
    private transient int vboVertexHandle = -1
    private transient int vboTextureHandle = -1
    private transient int vboNormalHandle = -1
    private transient int vboColorHandle = -1

    public Chunk(IWorld world, Vec3i startPosition) {
        this.parentWorld = world
        this.startPosition = startPosition;
        this.size = world.getChunkSize()
        this.height = world.getHeight()
        this.blockList = new Block[size][height][size]

        for (int x = 0; x < size; x++) {
            for (int z = 0; z < size; z++) {
                int distFromSeaLevel = world.getHeightAboveSeaLevel(startPosition.x + x, startPosition.z + z)
                int yMax = world.getSeaLevel() + distFromSeaLevel
                blockCount += Math.max(yMax, world.getSeaLevel())
                if(yMax < world.getSeaLevel()) {
                    for (int y = yMax; y < world.getSeaLevel(); y++) {
                        Block blockType = Block.WATER
                        if(y == yMax) {
                            blockType = Block.SAND
                        }
                        blockList[x][y][z] = blockType
                    }
                }
                for (int y = 0; y < yMax; y++) {
                    Block blockType = Block.STONE

                    if(y == world.getSeaLevel()-1 && y == yMax-1) {
                        blockCount++
                        blockList[x][y+1][z] = Block.SAND
                    }

                    if(y == yMax-1) {
                        blockType = Block.GRASS
                    } else if(y > yMax-5) {
                        blockType = Block.DIRT
                    }
                    blockList[x][y][z] = blockType
                    highestPoint = Math.max(highestPoint, y + 1)
                }
            }
        }
    }

    @Override
    public void rerender() {
        if (USE_VBO) {
            if(vboVertexHandle == -1 || vboVertexHandle == 0) {
                IntBuffer buffer = BufferUtils.createIntBuffer(4)
                GL15.glGenBuffers(buffer)
                vboVertexHandle = buffer.get(0)
                vboTextureHandle = buffer.get(1)
                vboNormalHandle = buffer.get(2)
                vboColorHandle = buffer.get(3)

                glEnableClientState(GL_NORMAL_ARRAY)
                glEnableClientState(GL_VERTEX_ARRAY)
                glEnableClientState(GL_TEXTURE_COORD_ARRAY)
                glEnableClientState(GL_COLOR_ARRAY)
            }

            numVisibleSides = 6*blockCount
            boolean[][][] shouldRenderTop = new boolean[size][height][size];
            boolean[][][] shouldRenderBottom = new boolean[size][height][size];
            boolean[][][] shouldRenderLeft = new boolean[size][height][size];
            boolean[][][] shouldRenderRight = new boolean[size][height][size];
            boolean[][][] shouldRenderFront = new boolean[size][height][size];
            boolean[][][] shouldRenderBack = new boolean[size][height][size];
            float[][][] lightLevels = new float[size][height][size];
            for (int x = 0; x < size; x++) {
                for (int z = 0; z < size; z++) {
                    float lightLevel = 1.0f
                    boolean firstBlock = false
                    for (int y = height-1; y >= 0; y--) {
                        Block block = blockList[x][y][z]
                        if (block != null) {
                            if(!firstBlock) {
                                firstBlock = true
                                lightLevel = 1.0f
                            }
                            int zed = z
                            shouldRenderTop[x][y][z] = true
                            if (z == size - 1)
                                shouldRenderTop[x][y][z] = true
                            else if (blockList[x][y][zed + 1] != null) {
                                shouldRenderTop[x][y][z] = false
                                numVisibleSides--
                            }

                            shouldRenderLeft[x][y][z] = true
                            if (x == 0)
                                shouldRenderLeft[x][y][z] = true
                            else if (blockList[x - 1][y][z] != null) {
                                shouldRenderLeft[x][y][z] = false
                                numVisibleSides--
                            }

                            shouldRenderRight[x][y][z] = true
                            if (x == size - 1)
                                shouldRenderRight[x][y][z] = true
                            else if (blockList[x + 1][y][z] != null) {
                                shouldRenderRight[x][y][z] = false
                                numVisibleSides--
                            }

                            shouldRenderFront[x][y][z] = true
                            if (y == 0)
                                shouldRenderFront[x][y][z] = true
                            else if (blockList[x][y - 1][z] != null) {
                                shouldRenderFront[x][y][z] = false
                                numVisibleSides--
                            }

                            shouldRenderBack[x][y][z] = true
                            if (y == height - 1)
                                shouldRenderBack[x][y][z] = true
                            else if (blockList[x][y + 1][z] != null) {
                                shouldRenderBack[x][y][z] = false
                                numVisibleSides--
                            }

                            shouldRenderBottom[x][y][z] = true
                            if (z == 0)
                                shouldRenderBottom[x][y][z] = true
                            else if (blockList[x][y][zed - 1] != null) {
                                shouldRenderBottom[x][y][z] = false
                                numVisibleSides--
                            }

                            lightLevels[x][y][z] = lightLevel
                        } else if(!firstBlock) {
                            lightLevels[x][y][z] = 1.0f
                        } else {
                            lightLevels[x][y][z] = lightLevel
                        }
                        lightLevel *= 0.8f
                    }
                }
            }

            FloatBuffer vertexPosData = BufferUtils.createFloatBuffer(numVisibleSides*4*3)
            FloatBuffer textureData = BufferUtils.createFloatBuffer(numVisibleSides*4*2)
            FloatBuffer normalData = BufferUtils.createFloatBuffer(numVisibleSides*4*3)
            FloatBuffer colorData = BufferUtils.createFloatBuffer(numVisibleSides*4*3)
            for (int x = 0; x < size; x++) {
                for (int z = 0; z < size; z++) {
                    boolean firstBlock = false
                    for (int y = height-1; y >= 0; y--) {
                        Block block = blockList[x][y][z]
                        if(block != null) {
                            if(!firstBlock) {<commit_msg>Water changes, fix ocean floor not rendering<commit_after>package sx.lambda.mstojcevich.voxel.world.chunk

import groovy.transform.CompileStatic
import org.lwjgl.BufferUtils
import org.lwjgl.opengl.GL15
import sx.lambda.mstojcevich.voxel.api.VoxelGameAPI
import sx.lambda.mstojcevich.voxel.api.events.render.EventChunkRender
import sx.lambda.mstojcevich.voxel.block.Block
import sx.lambda.mstojcevich.voxel.block.NormalBlockRenderer
import sx.lambda.mstojcevich.voxel.VoxelGame
import sx.lambda.mstojcevich.voxel.util.Vec3i
import sx.lambda.mstojcevich.voxel.world.IWorld

import java.nio.FloatBuffer
import java.nio.IntBuffer

import static org.lwjgl.opengl.GL11.*

@CompileStatic
public class Chunk implements IChunk {

    private final Block[][][] blockList

    private final transient IWorld parentWorld

    private final int size
    private final int height

    private transient int displayList = -1

    private Vec3i startPosition

    private int highestPoint

    private int blockCount
    private transient int numVisibleSides = 0

    private static final boolean USE_VBO = true
    private transient int vboVertexHandle = -1
    private transient int vboTextureHandle = -1
    private transient int vboNormalHandle = -1
    private transient int vboColorHandle = -1

    public Chunk(IWorld world, Vec3i startPosition) {
        this.parentWorld = world
        this.startPosition = startPosition;
        this.size = world.getChunkSize()
        this.height = world.getHeight()
        this.blockList = new Block[size][height][size]

        for (int x = 0; x < size; x++) {
            for (int z = 0; z < size; z++) {
                int distFromSeaLevel = world.getHeightAboveSeaLevel(startPosition.x + x, startPosition.z + z)
                int yMax = world.getSeaLevel() + distFromSeaLevel
                blockCount += Math.max(yMax, world.getSeaLevel())
                if(yMax < world.getSeaLevel()) {
                    for (int y = yMax; y < world.getSeaLevel(); y++) {
                        Block blockType = Block.WATER
                        if(y == yMax) {
                            blockType = Block.SAND
                        }
                        blockList[x][y][z] = blockType
                    }
                }
                for (int y = 0; y < yMax; y++) {
                    Block blockType = Block.STONE

                    if(y == world.getSeaLevel()-1 && y == yMax-1) {
                        blockCount++
                        blockList[x][y+1][z] = Block.SAND
                    }

                    if(y == yMax-1) {
                        blockType = Block.GRASS
                    } else if(y > yMax-5) {
                        blockType = Block.DIRT
                    }
                    blockList[x][y][z] = blockType
                    highestPoint = Math.max(highestPoint, y + 1)
                }
            }
        }
    }

    @Override
    public void rerender() {
        if (USE_VBO) {
            if(vboVertexHandle == -1 || vboVertexHandle == 0) {
                IntBuffer buffer = BufferUtils.createIntBuffer(4)
                GL15.glGenBuffers(buffer)
                vboVertexHandle = buffer.get(0)
                vboTextureHandle = buffer.get(1)
                vboNormalHandle = buffer.get(2)
                vboColorHandle = buffer.get(3)

                glEnableClientState(GL_NORMAL_ARRAY)
                glEnableClientState(GL_VERTEX_ARRAY)
                glEnableClientState(GL_TEXTURE_COORD_ARRAY)
                glEnableClientState(GL_COLOR_ARRAY)
            }

            numVisibleSides = 6*blockCount
            boolean[][][] shouldRenderTop = new boolean[size][height][size];
            boolean[][][] shouldRenderBottom = new boolean[size][height][size];
            boolean[][][] shouldRenderLeft = new boolean[size][height][size];
            boolean[][][] shouldRenderRight = new boolean[size][height][size];
            boolean[][][] shouldRenderFront = new boolean[size][height][size];
            boolean[][][] shouldRenderBack = new boolean[size][height][size];
            float[][][] lightLevels = new float[size][height][size];
            for (int x = 0; x < size; x++) {
                for (int z = 0; z < size; z++) {
                    float lightLevel = 1.0f
                    boolean firstBlock = false
                    for (int y = height-1; y >= 0; y--) {
                        Block block = blockList[x][y][z]
                        if (block != null) {
                            if(!firstBlock) {
                                firstBlock = true
                                lightLevel = 1.0f
                            }
                            int zed = z
                            shouldRenderTop[x][y][z] = true
                            if (z == size - 1)
                                shouldRenderTop[x][y][z] = true
                            else if (blockList[x][y][zed + 1] != null) {
                                if(!blockList[x][y][zed+1].isTransparent() || blockList[x][y][z].isTransparent()) {
                                    shouldRenderTop[x][y][z] = false
                                    numVisibleSides--
                                }
                            }

                            shouldRenderLeft[x][y][z] = true
                            if (x == 0)
                                shouldRenderLeft[x][y][z] = true
                            else if (blockList[x - 1][y][z] != null) {
                                if(!blockList[x - 1][y][z].isTransparent() || blockList[x][y][z].isTransparent()) {
                                    shouldRenderLeft[x][y][z] = false
                                    numVisibleSides--
                                }
                            }

                            shouldRenderRight[x][y][z] = true
                            if (x == size - 1)
                                shouldRenderRight[x][y][z] = true
                            else if (blockList[x + 1][y][z] != null) {
                                if(!blockList[x + 1][y][z].isTransparent() || blockList[x][y][z].isTransparent()) {
                                    shouldRenderRight[x][y][z] = false
                                    numVisibleSides--
                                }
                            }

                            shouldRenderFront[x][y][z] = true
                            if (y == 0)
                                shouldRenderFront[x][y][z] = true
                            else if (blockList[x][y - 1][z] != null) {
                                if(!blockList[x][y-1][z].isTransparent() || blockList[x][y][z].isTransparent()) {
                                    shouldRenderFront[x][y][z] = false
                                    numVisibleSides--
                                }
                            }

                            shouldRenderBack[x][y][z] = true
                            if (y == height - 1)
                                shouldRenderBack[x][y][z] = true
                            else if (blockList[x][y + 1][z] != null) {
                                if(!blockList[x][y+1][z].isTransparent() || blockList[x][y][z].isTransparent()) {
                                    shouldRenderBack[x][y][z] = false
                                    numVisibleSides--
                                }
                            }

                            shouldRenderBottom[x][y][z] = true
                            if (z == 0)
                                shouldRenderBottom[x][y][z] = true
                            else if (blockList[x][y][zed - 1] != null) {
                                if(!blockList[x][y][zed-1].isTransparent() || blockList[x][y][z].isTransparent()) {
                                    shouldRenderBottom[x][y][z] = false
                                    numVisibleSides--
                                }
                            }

                            lightLevels[x][y][z] = lightLevel
                        } else if(!firstBlock) {
                            lightLevels[x][y][z] = 1.0f
                        } else {
                            lightLevels[x][y][z] = lightLevel
                        }
                        lightLevel *= 0.8f
                    }
                }
            }

            FloatBuffer vertexPosData = BufferUtils.createFloatBuffer(numVisibleSides*4*3)
            FloatBuffer textureData = BufferUtils.createFloatBuffer(numVisibleSides*4*2)
            FloatBuffer normalData = BufferUtils.createFloatBuffer(numVisibleSides*4*3)
            FloatBuffer colorData = BufferUtils.createFloatBuffer(numVisibleSides*4*3)
            for (int x = 0; x < size; x++) {
                for (int z = 0; z < size; z++) {
                    boolean firstBlock = false
                    for (int y = height-1; y >= 0; y--) {
                        Block block = blockList[x][y][z]
                        if(block != null) {
                            if(!firstBlock) {
{'fraction_non_alphanumeric': 0.10297961317302666, 'fraction_numerical': 0.0082476621943428, 'mean_word_length': 1.1866903733807468, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7151419', 'n_tokens_mistral': 4724, 'n_tokens_neox': 4381, 'n_words': 886}