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

import io.netty.util.internal.ConcurrentSet;
import sx.lambda.mstojcevich.voxel.VoxelGame;
import sx.lambda.mstojcevich.voxel.api.VoxelGameAPI;
import sx.lambda.mstojcevich.voxel.api.events.worldgen.EventFinishChunkGen;
import sx.lambda.mstojcevich.voxel.block.Block;
import sx.lambda.mstojcevich.voxel.entity.Entity;
import sx.lambda.mstojcevich.voxel.net.packet.client.PacketUnloadChunk;
import sx.lambda.mstojcevich.voxel.util.Vec3i;
import sx.lambda.mstojcevich.voxel.entity.EntityPosition;
import sx.lambda.mstojcevich.voxel.world.chunk.Chunk;
import sx.lambda.mstojcevich.voxel.world.chunk.IChunk;
import sx.lambda.mstojcevich.voxel.world.generation.ChunkGenerator;
import sx.lambda.mstojcevich.voxel.world.generation.SimplexChunkGenerator;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;

import static org.lwjgl.opengl.GL11.*;

public class World implements IWorld {
	
	private static final int CHUNK_SIZE = 16;

    private static final int WORLD_HEIGHT = 128;

    private static final int SEA_LEVEL = 64;

    private final Map<Vec3i, IChunk> chunkMap = new ConcurrentHashMap<>();
    private final Set<IChunk> chunkList = new ConcurrentSet<>();

    private static final float GRAVITY = 4.69f;

    private static final float TERMINAL_VELOCITY = 56;

    private final ChunkGenerator chunkGen;

    private final boolean remote, server;

    private List<Entity> loadedEntities = new CopyOnWriteArrayList<>();

    private Set<IChunk> chunksToRerender = Collections.newSetFromMap(new ConcurrentHashMap<IChunk, Boolean>());

    private Queue<Vec3i> sunlightQueue = new ConcurrentLinkedQueue<>();

    public World(boolean remote, boolean server) {
        this.remote = remote;
        this.server = server;
        if(!remote) {
            this.chunkGen = new SimplexChunkGenerator(this, 200, new Random().nextInt());
        } else {
            this.chunkGen = null;
        }
    }
	
	public int getChunkSize() {
		return CHUNK_SIZE;
	}

    public int getHeight() { return WORLD_HEIGHT; }

    public IChunk getChunkAtPosition(Vec3i position) {
        Vec3i chunkPosition = new Vec3i(
                getChunkPosition(position.x),
                0,
                getChunkPosition(position.z));

        return this.chunkMap.get(chunkPosition);
    }

    public void render() {
        if(!server) {
            if(!chunksToRerender.isEmpty()) {
                processLightQueue();
            }
            for(IChunk c : chunksToRerender) {
                c.rerender();
                chunksToRerender.remove(c);
            }

            long renderStartNS = System.nanoTime();
            /*IntBuffer ids = BufferUtils.createIntBuffer(this.chunkList.size());
            ARBOcclusionQuery.glGenQueriesARB(ids);


            glDepthMask(false);
            glColorMask(false, false, false, false);
            glDisable(GL_BLEND);
            glDisable(GL_TEXTURE_2D);
            // TODO have an object that keeps track of what the GPU supports, check if it supports ARB_occlusion_query2 or has OpenGL 3.3

            List<IChunk> distSortedChunks = new ArrayList<>(chunkList);
            Collections.sort(distSortedChunks, new Comparator<IChunk>() {
                @Override
                public int compare(IChunk o1, IChunk o2) {
                    return (int)VoxelGame.getInstance().getPlayer().getPosition().planeDistance(o1.getStartPosition().x,
                            o1.getStartPosition().z) -
                            (int)VoxelGame.getInstance().getPlayer().getPosition().planeDistance(o2.getStartPosition().x,
                            o2.getStartPosition().z);
                }
            });

            // https://www.opengl.org/registry/specs/ARB/occlusion_query.txt
            int index = 0;
            for (IChunk c : distSortedChunks) {
                if(index == distSortedChunks.size()-1) { // Closest chunk to the player, so the player's chunk
                    continue;
                }

                int queryId = ids.get();
                ARBOcclusionQuery.glBeginQueryARB(ARBOcclusionQuery2.GL_ANY_SAMPLES_PASSED, queryId);

                if (VoxelGame.getInstance().getFrustum().cubeInFrustum(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z, CHUNK_SIZE, c.getHighestPoint())) {
                    int x = c.getStartPosition().x;
                    int y = c.getStartPosition().y;
                    int z = c.getStartPosition().z;
                    int x2 = x + CHUNK_SIZE;
                    float y2 = c.getHighestPoint();
                    int z2 = z + CHUNK_SIZE;
                    // TODO render this into a VBO on chunk rerender
                    glBegin(GL_QUADS);
                    glVertex3f(x, y, z);
                    glVertex3f(x, y, z2);
                    glVertex3f(x, y2, z2);
                    glVertex3f(x, y2, z);
                    glEnd();

                    glBegin(GL_QUADS);
                    glVertex3f(x, y, z);
                    glVertex3f(x, y, z2);
                    glVertex3f(x, y2, z2);
                    glVertex3f(x, y2, z);
                    glEnd();

                    glBegin(GL_QUADS);
                    glVertex3f(x2, y, z);
                    glVertex3f(x2, y2, z);
                    glVertex3f(x2, y2, z2);
                    glVertex3f(x2, y, z2);
                    glEnd();

                    glBegin(GL_QUADS);
                    glVertex3f(x, y, z);
                    glVertex3f(x2, y, z);
                    glVertex3f(x2, y, z2);
                    glVertex3f(x, y, z2);
                    glEnd();

                    glBegin(GL_QUADS);
                    glVertex3f(x2, y2, z);
                    glVertex3f(x, y2, z);
                    glVertex3f(x, y2, z2);
                    glVertex3f(x2, y2, z2);
                    glEnd();

                    glBegin(GL_QUADS);
                    glVertex3f(x2, y, z);
                    glVertex3f(x, y, z);
                    glVertex3f(x, y2, z);
                    glVertex3f(x2, y2, z);
                    glEnd();
                }

                ARBOcclusionQuery.glEndQueryARB(ARBOcclusionQuery2.GL_ANY_SAMPLES_PASSED);
                System.out.println(ARBOcclusionQuery.glGetQueryObjectuiARB(ids.get(index), ARBOcclusionQuery2.GL_ANY_SAMPLES_PASSED) > 0);

                index++;
            }
            glDepthMask(true);
            glColorMask(true, true, true, true);

            glFlush();

            index = 0;
            for(IChunk c : distSortedChunks) {
                if(index == distSortedChunks.size()-1 || ARBOcclusionQuery.glGetQueryObjectuiARB(ids.get(index), ARBOcclusionQuery2.GL_ANY_SAMPLES_PASSED) > 0) {
                    glPushMatrix();
                    glTranslatef(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z);
                    c.render();
                    glPopMatrix();

                }
                index++;
            }
            */

            for (IChunk c : this.chunkList) {
                if (VoxelGame.getInstance().getGameRenderer().getFrustum().cubeInFrustum(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z, CHUNK_SIZE, c.getHighestPoint())) {
                    glPushMatrix();
                    glTranslatef(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z);
                    c.render();
                    glPopMatrix();
                }
            }
            for (IChunk c : this.chunkList) {
                if (VoxelGame.getInstance().getGameRenderer().getFrustum().cubeInFrustum(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z, CHUNK_SIZE, c.getHighestPoint())) {
                    glPushMatrix();
                    glTranslatef(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z);
                    c.renderWater();
                    glPopMatrix();
                }<commit_msg>Remove commented unused code<commit_after>package sx.lambda.mstojcevich.voxel.world;

import io.netty.util.internal.ConcurrentSet;
import sx.lambda.mstojcevich.voxel.VoxelGame;
import sx.lambda.mstojcevich.voxel.api.VoxelGameAPI;
import sx.lambda.mstojcevich.voxel.api.events.worldgen.EventFinishChunkGen;
import sx.lambda.mstojcevich.voxel.block.Block;
import sx.lambda.mstojcevich.voxel.entity.Entity;
import sx.lambda.mstojcevich.voxel.net.packet.client.PacketUnloadChunk;
import sx.lambda.mstojcevich.voxel.util.Vec3i;
import sx.lambda.mstojcevich.voxel.entity.EntityPosition;
import sx.lambda.mstojcevich.voxel.world.chunk.Chunk;
import sx.lambda.mstojcevich.voxel.world.chunk.IChunk;
import sx.lambda.mstojcevich.voxel.world.generation.ChunkGenerator;
import sx.lambda.mstojcevich.voxel.world.generation.SimplexChunkGenerator;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;

import static org.lwjgl.opengl.GL11.*;

public class World implements IWorld {
	
	private static final int CHUNK_SIZE = 16;

    private static final int WORLD_HEIGHT = 128;

    private static final int SEA_LEVEL = 64;

    private final Map<Vec3i, IChunk> chunkMap = new ConcurrentHashMap<>();
    private final Set<IChunk> chunkList = new ConcurrentSet<>();

    private static final float GRAVITY = 4.69f;

    private static final float TERMINAL_VELOCITY = 56;

    private final ChunkGenerator chunkGen;

    private final boolean remote, server;

    private List<Entity> loadedEntities = new CopyOnWriteArrayList<>();

    private Set<IChunk> chunksToRerender = Collections.newSetFromMap(new ConcurrentHashMap<IChunk, Boolean>());

    private Queue<Vec3i> sunlightQueue = new ConcurrentLinkedQueue<>();

    public World(boolean remote, boolean server) {
        this.remote = remote;
        this.server = server;
        if(!remote) {
            this.chunkGen = new SimplexChunkGenerator(this, 200, new Random().nextInt());
        } else {
            this.chunkGen = null;
        }
    }
	
	public int getChunkSize() {
		return CHUNK_SIZE;
	}

    public int getHeight() { return WORLD_HEIGHT; }

    public IChunk getChunkAtPosition(Vec3i position) {
        Vec3i chunkPosition = new Vec3i(
                getChunkPosition(position.x),
                0,
                getChunkPosition(position.z));

        return this.chunkMap.get(chunkPosition);
    }

    public void render() {
        if(!server) {
            if(!chunksToRerender.isEmpty()) {
                processLightQueue();
            }
            for(IChunk c : chunksToRerender) {
                c.rerender();
                chunksToRerender.remove(c);
            }

            long renderStartNS = System.nanoTime();

            for (IChunk c : this.chunkList) {
                if (VoxelGame.getInstance().getGameRenderer().getFrustum().cubeInFrustum(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z, CHUNK_SIZE, c.getHighestPoint())) {
                    glPushMatrix();
                    glTranslatef(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z);
                    c.render();
                    glPopMatrix();
                }
            }
            for (IChunk c : this.chunkList) {
                if (VoxelGame.getInstance().getGameRenderer().getFrustum().cubeInFrustum(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z, CHUNK_SIZE, c.getHighestPoint())) {
                    glPushMatrix();
                    glTranslatef(c.getStartPosition().x, c.getStartPosition().y, c.getStartPosition().z);
                    c.renderWater();
                    glPopMatrix();
                }
{'fraction_non_alphanumeric': 0.10743113921943913, 'fraction_numerical': 0.010568361487892152, 'mean_word_length': 2.0736572890025577, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3313704', 'n_tokens_mistral': 3788, 'n_tokens_neox': 3586, 'n_words': 630}