<commit_before>package Hexel.chunk;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import Hexel.Engine;
import Hexel.blocks.types.BlockTransparent;
import Hexel.math.HexGeometry;
import Hexel.math.Vector2d;
import Hexel.math.Vector3i;
import Hexel.rendering.Camera;
import Hexel.rendering.GLChunk;
import Hexel.rendering.Renderer;
import Hexel.things.types.Thing;

public class ChunkVisibilityManager {

	private Chunks chunks;
	private Renderer renderer;

	public Set<Vector3i> renderedChunks = Collections.newSetFromMap(new ConcurrentHashMap<Vector3i, Boolean>());
	public Set<Vector3i> simulatedChunks = Collections.newSetFromMap(new ConcurrentHashMap<Vector3i, Boolean>());
	
	public int renderDistance = 2*30;
	
	private Engine engine;
	private int lastLightLevel = 0;

	public ChunkVisibilityManager(Engine engine, Chunks chunks, Renderer renderer) {
		this.engine = engine;
		this.chunks = chunks;
		this.renderer = renderer;
	}

	public void updateVisibility(Camera camera) {
		Vector2d pos = new Vector2d();
		HexGeometry.cartesianToHex(camera.getCameraX(), camera.getCameraY(), pos);

		int cx = (int) Math.floor(pos.x / 16.0);
		int cy = (int) Math.floor(pos.y / 16.0);
		int cz = (int) Math.floor(camera.getCameraZ() / 16.0);

		ArrayList<Vector3i> points = new ArrayList<Vector3i>();
		ArrayList<Vector3i> inRenderRange = new ArrayList<Vector3i>();
		ArrayList<Vector3i> inSimRange = new ArrayList<Vector3i>();

		int lightLevel = (int) (BlockTransparent.MAX_LIGHT_LEVEL*engine.getAmbientLight());
		boolean resetLights = lastLightLevel != lightLevel;
		
		int renderedChunksRange = 4;
		int simulatedChunksRange = 2;
		for (int x = -renderedChunksRange; x <= renderedChunksRange; x++) {
			for (int y = -renderedChunksRange; y <= renderedChunksRange; y++) {
				for (int z = -renderedChunksRange; z <= renderedChunksRange; z++) {
					int ncx = cx + x;
					int ncy = cy + y;
					int ncz = cz + z;
					Vector3i cpos = new Vector3i(ncx, ncy, ncz);
					inRenderRange.add(cpos);
					Chunk chunk = this.chunks.getChunk(cpos);
					if (!chunk.needFirstSim){
						if (!this.renderedChunks.contains(cpos)) {
							points.add(cpos);
							chunk.setDirty(false);
						} else {
							if ((resetLights || chunk.isDirty()) && !chunk.beingUpdated) {
								chunk.setDirty(false);
								points.add(cpos);
							}
	
						}
					}
					if (points.size() > 10) {
						this.renderer.loadChunks(points);
						this.renderedChunks.addAll(points);
						points = new ArrayList<Vector3i>();
					}
					if (x >= -simulatedChunksRange && x <= simulatedChunksRange){
						if (y >= -simulatedChunksRange && y <= simulatedChunksRange){
							if (z >= -simulatedChunksRange && z <= simulatedChunksRange){
								inSimRange.add(cpos);
								this.simulatedChunks.add(cpos);
							}
						}
					}
				}
			}
		}
		this.renderer.loadChunks(points);
		this.renderedChunks.addAll(points);
		
		lastLightLevel = lightLevel;

		Iterator<Vector3i> iter = this.renderedChunks.iterator();
		ArrayList<Vector3i> toRemove = new ArrayList<Vector3i>();
		while (iter.hasNext()) {
			Vector3i position = iter.next();

			if (!inRenderRange.contains(position) && !points.contains(position)) {
				toRemove.add(position);
			}
		}
		this.renderedChunks.removeAll(toRemove);
		
		HashMap<Vector3i, GLChunk> glChunkTable = this.renderer.getGLChunkTable();
		for (Vector3i p : new HashSet<Vector3i>(glChunkTable.keySet())) {
			if (!inRenderRange.contains(p)) {
				this.renderer.unloadGLChunk(p);
			}
		}

		iter = this.simulatedChunks.iterator();
		toRemove = new ArrayList<Vector3i>();
		while (iter.hasNext()) {
			Vector3i position = iter.next();

			if (!inSimRange.contains(position)) {
				toRemove.add(position);
			}
		}
		this.simulatedChunks.removeAll(toRemove);
	}
}<commit_msg>support code for eventual chunks at a distance<commit_after>package Hexel.chunk;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import Hexel.Engine;
import Hexel.blocks.types.BlockTransparent;
import Hexel.math.HexGeometry;
import Hexel.math.Vector2d;
import Hexel.math.Vector3i;
import Hexel.rendering.Camera;
import Hexel.rendering.GLChunk;
import Hexel.rendering.Renderer;
import Hexel.things.types.Thing;

public class ChunkVisibilityManager {

	private Chunks chunks;
	private Renderer renderer;

	public Set<Vector3i> renderedChunks = Collections.newSetFromMap(new ConcurrentHashMap<Vector3i, Boolean>());
	public Set<Vector3i> simulatedChunks = Collections.newSetFromMap(new ConcurrentHashMap<Vector3i, Boolean>());
	public Set<Vector3i> distanceChunks = Collections.newSetFromMap(new ConcurrentHashMap<Vector3i, Boolean>());

	public int renderDistance = 2*30;

	private Engine engine;
	private int lastLightLevel = 0;

	public ChunkVisibilityManager(Engine engine, Chunks chunks, Renderer renderer) {
		this.engine = engine;
		this.chunks = chunks;
		this.renderer = renderer;
	}

	public void updateVisibility(Camera camera) {
		Vector2d pos = new Vector2d();
		HexGeometry.cartesianToHex(camera.getCameraX(), camera.getCameraY(), pos);

		int cx = (int) Math.floor(pos.x / 16.0);
		int cy = (int) Math.floor(pos.y / 16.0);
		int cz = (int) Math.floor(camera.getCameraZ() / 16.0);

		ArrayList<Vector3i> points = new ArrayList<Vector3i>();
		ArrayList<Vector3i> inRenderRange = new ArrayList<Vector3i>();
		ArrayList<Vector3i> inSimRange = new ArrayList<Vector3i>();
		ArrayList<Vector3i> inDistRange = new ArrayList<Vector3i>();

		int lightLevel = (int) (BlockTransparent.MAX_LIGHT_LEVEL*engine.getAmbientLight());
		boolean resetLights = lastLightLevel != lightLevel;

		int distanceChunksRange = 8;
		int renderedChunksRange = 4;
		int simulatedChunksRange = 2;
		for (int x = -distanceChunksRange; x <= distanceChunksRange; x++) {
			for (int y = -distanceChunksRange; y <= distanceChunksRange; y++) {
				for (int z = -distanceChunksRange; z <= distanceChunksRange; z++) {
					int ncx = cx + x;
					int ncy = cy + y;
					int ncz = cz + z;
					Vector3i cpos = new Vector3i(ncx, ncy, ncz);
					
					boolean pInRenderRange = false;

					if (x >= -renderedChunksRange && x <= renderedChunksRange){
						if (y >= -renderedChunksRange && y <= renderedChunksRange){
							if (z >= -renderedChunksRange && z <= renderedChunksRange){
								pInRenderRange = true;
								inRenderRange.add(cpos);
								Chunk chunk = this.chunks.getChunk(cpos);
								if (!chunk.needFirstSim){
									if (!this.renderedChunks.contains(cpos)) {
										points.add(cpos);
										chunk.setDirty(false);
									} else {
										if ((resetLights || chunk.isDirty()) && !chunk.beingUpdated) {
											chunk.setDirty(false);
											points.add(cpos);
										}

									}
								}
								if (points.size() > 10) {
									this.renderer.loadChunks(points);
									this.renderedChunks.addAll(points);
									points = new ArrayList<Vector3i>();
								}
								if (x >= -simulatedChunksRange && x <= simulatedChunksRange){
									if (y >= -simulatedChunksRange && y <= simulatedChunksRange){
										if (z >= -simulatedChunksRange && z <= simulatedChunksRange){
											inSimRange.add(cpos);
											this.simulatedChunks.add(cpos);
										}
									}
								}
							}
						}
						if (!pInRenderRange){
							inDistRange.add(cpos);
						}
					}
				}
			}
		}
		this.renderer.loadChunks(points);
		this.renderedChunks.addAll(points);

		lastLightLevel = lightLevel;

		HashMap<Vector3i, GLChunk> glChunkTable = this.renderer.getGLChunkTable();
		for (Vector3i p : new HashSet<Vector3i>(glChunkTable.keySet())) {
			if (!inRenderRange.contains(p)) {
				this.renderer.unloadGLChunk(p);
			}
		}

		Iterator<Vector3i> iter = this.renderedChunks.iterator();
		ArrayList<Vector3i> toRemove = new ArrayList<Vector3i>();
		while (iter.hasNext()) {
			Vector3i position = iter.next();
			if (!inRenderRange.contains(position) && !points.contains(position)) {
				toRemove.add(position);
			}
		}
		this.renderedChunks.removeAll(toRemove);

		iter = this.simulatedChunks.iterator();
		toRemove = new ArrayList<Vector3i>();
		while (iter.hasNext()) {
			Vector3i position = iter.next();
			if (!inSimRange.contains(position)) {
				toRemove.add(position);
			}
		}
		this.simulatedChunks.removeAll(toRemove);

		iter = this.distanceChunks.iterator();
		toRemove = new ArrayList<Vector3i>();
		while (iter.hasNext()) {
			Vector3i position = iter.next();
			if (!inDistRange.contains(position)) {
				toRemove.add(position);
			}
		}
		this.distanceChunks.removeAll(toRemove);
	}
}
{'fraction_non_alphanumeric': 0.128516333938294, 'fraction_numerical': 0.010549001814882032, 'mean_word_length': 3.7174959871589084, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '131954', 'n_tokens_mistral': 3550, 'n_tokens_neox': 2854, 'n_words': 575}