<commit_before>/*******************************************************************************
 * This file is part of TERMINAL RECALL 
 * Copyright (c) 2012, 2013 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the COPYING and CREDITS files for more details.
 * 
 * Contributors:
 *      chuck - initial API and implementation
 ******************************************************************************/
package org.jtrfp.trcl.core;


import java.awt.Color;
import java.io.IOException;
import java.nio.ByteOrder;
import java.util.Map.Entry;
import java.util.concurrent.Callable;

import javax.media.opengl.GL3;

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.jtrfp.jtrfp.FileLoadException;
import org.jtrfp.trcl.BackdropSystem;
import org.jtrfp.trcl.HUDSystem;
import org.jtrfp.trcl.InterpolatingAltitudeMap;
import org.jtrfp.trcl.KeyStatus;
import org.jtrfp.trcl.MatrixWindow;
import org.jtrfp.trcl.NAVSystem;
import org.jtrfp.trcl.ObjectDefinitionWindow;
import org.jtrfp.trcl.ObjectListWindow;
import org.jtrfp.trcl.OutputDump;
import org.jtrfp.trcl.OverworldSystem;
import org.jtrfp.trcl.World;
import org.jtrfp.trcl.dbg.Reporter;
import org.jtrfp.trcl.file.VOXFile;
import org.jtrfp.trcl.flow.Game;
import org.jtrfp.trcl.flow.Mission;
import org.jtrfp.trcl.gpu.GPU;
import org.jtrfp.trcl.obj.CollisionManager;
import org.jtrfp.trcl.obj.Player;
import org.jtrfp.trcl.tools.Util;

public final class TR{
	public static final double 	unitCircle			=65535;
	public static final double 	crossPlatformScalar		=16;//Shrinks everything so that we can use floats instead of ints
	public static final double 	mapSquareSize			=Math.pow(2, 20)/crossPlatformScalar;
	public static final double 	mapWidth			=mapSquareSize*256;
	public static final double 	mapCartOffset			=mapWidth/2.;//This is the scaled-down version, not the full version
	public static final double 	visibilityDiameterInMapSquares	=35;
	public static final int 	terrainChunkSideLengthInSquares	=4;//Keep at power of two for now. 4x4 = 16. 16x6 = 96. 96 vertices per GLSL block means 1 chunk per block.
	public static final double 	antiGamma			=1.6;
	public static final boolean 	ANIMATED_TERRAIN		=false;
	
	public final TRFutureTask<GPU> 		gpu;
	private Player 				player;
	public final RootWindow 		rootWindow;
	private Color [] 			globalPalette, 
						darkIsClearPalette;
	private final KeyStatus 		keyStatus;
	private ResourceManager 		resourceManager;
	public final ThreadManager 		threadManager;
	public final TRFutureTask<Renderer> 	renderer;
	private final CollisionManager 		collisionManager	= new CollisionManager(this);
	private final Reporter 			reporter		= new Reporter();
	private OverworldSystem 		overworldSystem;
	private InterpolatingAltitudeMap 	altitudeMap;
	private BackdropSystem 			backdropSystem;
	private Game 				game			= new Game();
	private NAVSystem 			navSystem;
	private HUDSystem 			hudSystem;
	private Mission 			currentMission;
	
	public final TRFutureTask<MatrixWindow> 		matrixWindow ;
	public final TRFutureTask<ObjectListWindow> 		objectListWindow;
	public final TRFutureTask<ObjectDefinitionWindow> 	objectDefinitionWindow;
	private TRConfiguration 				trConfig;
	private GL3 						glCache;
	private ByteOrder 					byteOrder;
	private final World 					world;
	
	/**
	 * Converts legacy coordinate to modern coordinate
	 * @param v
	 * @return
	 * @since Oct 17, 2012
	 */
    public static double legacy2Modern(double v) {
	return v < 0 ? (v + 268435456) / crossPlatformScalar : v
		/ crossPlatformScalar;
    }

    public static double modern2Legacy(double v) {
	v *= crossPlatformScalar;
	return v > 134217727 ? v - 268435456 : v;
    }
	public static Vector3D twosComplimentSubtract(Vector3D l, Vector3D r){
	    return new Vector3D(
		    deltaRollover(l.getX()-r.getX()),
		    deltaRollover(l.getY()-r.getY()),
		    deltaRollover(l.getZ()-r.getZ()));
	}
	public static double twosComplimentDistance(Vector3D l, Vector3D r){
	    return twosComplimentSubtract(l,r).getNorm();
	}
	public static double deltaRollover(double v){
	    if(v>mapCartOffset)return v-mapWidth;
	    else if(v<-mapCartOffset)return v+mapWidth;
	    return v;
	}
	
	public TR(){
	    	try{new OutputDump();}
	    	catch(Exception e){e.printStackTrace();}
	    	AutoInitializable.Initializer.initialize(this);
	    	rootWindow = new RootWindow(this);
		keyStatus = new KeyStatus(rootWindow);
		threadManager = new ThreadManager(this);
		gpu = new TRFutureTask<GPU>(this,new Callable<GPU>(){
		    @Override
		    public GPU call() throws Exception {
			Thread.currentThread().setName("GPU constructor");
			return new GPU(TR.this);
		    }});
		threadManager.threadPool.submit(gpu);
		System.out.println("Initializing graphics engine...");
		    renderer=new TRFutureTask<Renderer>(this,new Callable<Renderer>(){
			@Override
			public Renderer call() throws Exception {
			    Thread.currentThread().setName("Renderer constructor.");
			    return new Renderer(TR.this.gpu.get());
			}//end call()
		    });threadManager.threadPool.submit(renderer);
		    matrixWindow=new TRFutureTask<MatrixWindow>(this,new Callable<MatrixWindow>(){
			@Override
			public MatrixWindow call() throws Exception {
			    Thread.currentThread().setName("MatrixWindow constructor.");
			    return new MatrixWindow(TR.this);
			}//end call()
		    });threadManager.threadPool.submit(matrixWindow);
		    objectListWindow=new TRFutureTask<ObjectListWindow>(this,new Callable<ObjectListWindow>(){
			@Override
			public ObjectListWindow call() throws Exception {
			    Thread.currentThread().setName("ObjectListWindow constructor.");
			    return new ObjectListWindow(TR.this);
			}//end call()
		    });threadManager.threadPool.submit(objectListWindow);
		    objectDefinitionWindow=new TRFutureTask<ObjectDefinitionWindow>(this,new Callable<ObjectDefinitionWindow>(){
			@Override
			public ObjectDefinitionWindow call() throws Exception {
			    Thread.currentThread().setName("ObjectDefinitionWindow constructor.");
			    return new ObjectDefinitionWindow(TR.this);
			}//end call()
		    });threadManager.threadPool.submit(objectDefinitionWindow);
		    System.out.println("...Done");
		setResourceManager(new ResourceManager(this));
		world = new World(
				256*mapSquareSize,
				14.*mapSquareSize,
				256*mapSquareSize,
				mapSquareSize*visibilityDiameterInMapSquares/2., this);
		renderer.get().setRootGrid(world);
		}//end constructor
	
    public void showStopper(final Exception e) {
	System.err.println("==== SHOWSTOPPER ====");
			    e.printStackTrace();
	System.err.println("======================");
	System.err.println("\nToo full of fail to continue. Exiting...\n\n");
	System.exit(-1);
    }

    public static int bidiMod(int v, int mod) {
	while (v < 0)
	    v += mod;
	v %= mod;
	return v;
    }

	/**
	 * @return the trConfig
	 */
	public TRConfiguration getTrConfig(){
	    	if(trConfig==null)trConfig=new TRConfiguration();
		return trConfig;
		}

    /**
     * @param trConfig
     *            the trConfig to set
     */
    public void setTrConfig(TRConfiguration trConfig) {
	this.trConfig = trConfig;
    }

    /**
     * @return the resourceManager
     */
    public ResourceManager getResourceManager() {
	return resourceManager;<commit_msg>Semantic fix.<commit_after>/*******************************************************************************
 * This file is part of TERMINAL RECALL 
 * Copyright (c) 2012, 2013 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the COPYING and CREDITS files for more details.
 * 
 * Contributors:
 *      chuck - initial API and implementation
 ******************************************************************************/
package org.jtrfp.trcl.core;


import java.awt.Color;
import java.io.IOException;
import java.nio.ByteOrder;
import java.util.Map.Entry;
import java.util.concurrent.Callable;

import javax.media.opengl.GL3;

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.jtrfp.jtrfp.FileLoadException;
import org.jtrfp.trcl.BackdropSystem;
import org.jtrfp.trcl.HUDSystem;
import org.jtrfp.trcl.InterpolatingAltitudeMap;
import org.jtrfp.trcl.KeyStatus;
import org.jtrfp.trcl.MatrixWindow;
import org.jtrfp.trcl.NAVSystem;
import org.jtrfp.trcl.ObjectDefinitionWindow;
import org.jtrfp.trcl.ObjectListWindow;
import org.jtrfp.trcl.OutputDump;
import org.jtrfp.trcl.OverworldSystem;
import org.jtrfp.trcl.World;
import org.jtrfp.trcl.dbg.Reporter;
import org.jtrfp.trcl.file.VOXFile;
import org.jtrfp.trcl.flow.Game;
import org.jtrfp.trcl.flow.Mission;
import org.jtrfp.trcl.gpu.GPU;
import org.jtrfp.trcl.obj.CollisionManager;
import org.jtrfp.trcl.obj.Player;
import org.jtrfp.trcl.tools.Util;

public final class TR{
	public static final double 	unitCircle			=65535;
	public static final double 	crossPlatformScalar		=16;//Shrinks everything so that we can use floats instead of ints
	public static final double 	mapSquareSize			=Math.pow(2, 20)/crossPlatformScalar;
	public static final double 	mapWidth			=mapSquareSize*256;
	public static final double 	mapCartOffset			=mapWidth/2.;//This is the scaled-down version, not the full version
	public static final double 	visibilityDiameterInMapSquares	=35;
	public static final int 	terrainChunkSideLengthInSquares	=4;//Keep at power of two for now. 4x4 = 16. 16x6 = 96. 96 vertices per GLSL block means 1 chunk per block.
	public static final double 	antiGamma			=1.6;
	public static final boolean 	ANIMATED_TERRAIN		=false;
	
	public final TRFutureTask<GPU> 		gpu;
	private Player 				player;
	public final RootWindow 		rootWindow;
	private Color [] 			globalPalette, 
						darkIsClearPalette;
	private final KeyStatus 		keyStatus;
	private ResourceManager 		resourceManager;
	public final ThreadManager 		threadManager;
	public final TRFutureTask<Renderer> 	renderer;
	private final CollisionManager 		collisionManager	= new CollisionManager(this);
	private final Reporter 			reporter		= new Reporter();
	private OverworldSystem 		overworldSystem;
	private InterpolatingAltitudeMap 	altitudeMap;
	private BackdropSystem 			backdropSystem;
	private Game 				game			= new Game();
	private NAVSystem 			navSystem;
	private HUDSystem 			hudSystem;
	private Mission 			currentMission;
	
	public final TRFutureTask<MatrixWindow> 		matrixWindow ;
	public final TRFutureTask<ObjectListWindow> 		objectListWindow;
	public final TRFutureTask<ObjectDefinitionWindow> 	objectDefinitionWindow;
	private TRConfiguration 				trConfig;
	private GL3 						glCache;
	private ByteOrder 					byteOrder;
	private final World 					world;
	
	/**
	 * Converts legacy coordinate to modern coordinate
	 * @param v
	 * @return
	 * @since Oct 17, 2012
	 */
    public static double legacy2Modern(double v) {
	return v < 0 ? (v + 268435456) / crossPlatformScalar : v
		/ crossPlatformScalar;
    }

    public static double modern2Legacy(double v) {
	v *= crossPlatformScalar;
	return v > 134217727 ? v - 268435456 : v;
    }
	public static Vector3D twosComplimentSubtract(Vector3D l, Vector3D r){
	    return new Vector3D(
		    deltaRollover(l.getX()-r.getX()),
		    deltaRollover(l.getY()-r.getY()),
		    deltaRollover(l.getZ()-r.getZ()));
	}
	public static double twosComplimentDistance(Vector3D l, Vector3D r){
	    return twosComplimentSubtract(l,r).getNorm();
	}
	public static double deltaRollover(double v){
	    if(v>mapCartOffset)return v-mapWidth;
	    else if(v<-mapCartOffset)return v+mapWidth;
	    return v;
	}
	
	public TR(){
	    	try{new OutputDump();}
	    	catch(Exception e){e.printStackTrace();}
	    	AutoInitializable.Initializer.initialize(this);
	    	rootWindow = new RootWindow(this);
		keyStatus = new KeyStatus(rootWindow);
		threadManager = new ThreadManager(this);
		gpu = new TRFutureTask<GPU>(this,new Callable<GPU>(){
		    @Override
		    public GPU call() throws Exception {
			Thread.currentThread().setName("GPU constructor");
			return new GPU(TR.this);
		    }});
		threadManager.threadPool.submit(gpu);
		System.out.println("Initializing graphics engine...");
		    renderer=new TRFutureTask<Renderer>(this,new Callable<Renderer>(){
			@Override
			public Renderer call() throws Exception {
			    Thread.currentThread().setName("Renderer constructor.");
			    return new Renderer(TR.this.gpu.get());
			}//end call()
		    });threadManager.threadPool.submit(renderer);
		    matrixWindow=new TRFutureTask<MatrixWindow>(this,new Callable<MatrixWindow>(){
			@Override
			public MatrixWindow call() throws Exception {
			    Thread.currentThread().setName("MatrixWindow constructor.");
			    return new MatrixWindow(TR.this);
			}//end call()
		    });threadManager.threadPool.submit(matrixWindow);
		    objectListWindow=new TRFutureTask<ObjectListWindow>(this,new Callable<ObjectListWindow>(){
			@Override
			public ObjectListWindow call() throws Exception {
			    Thread.currentThread().setName("ObjectListWindow constructor.");
			    return new ObjectListWindow(TR.this);
			}//end call()
		    });threadManager.threadPool.submit(objectListWindow);
		    objectDefinitionWindow=new TRFutureTask<ObjectDefinitionWindow>(this,new Callable<ObjectDefinitionWindow>(){
			@Override
			public ObjectDefinitionWindow call() throws Exception {
			    Thread.currentThread().setName("ObjectDefinitionWindow constructor.");
			    return new ObjectDefinitionWindow(TR.this);
			}//end call()
		    });threadManager.threadPool.submit(objectDefinitionWindow);
		    System.out.println("...Done");
		setResourceManager(new ResourceManager(this));
		world = new World(
				256*mapSquareSize,
				14.*mapSquareSize,
				256*mapSquareSize,
				mapSquareSize*visibilityDiameterInMapSquares/2., this);
		renderer.get().setRootGrid(world);
		}//end constructor
	
    public void showStopper(final Exception e) {
	System.err.println("==== SHOWSTOPPER ====");
			    e.printStackTrace();
	System.err.println("======================");
	System.err.println("\nIrrecoverable. Exiting...\n\n");
	System.exit(-1);
    }

    public static int bidiMod(int v, int mod) {
	while (v < 0)
	    v += mod;
	v %= mod;
	return v;
    }

	/**
	 * @return the trConfig
	 */
	public TRConfiguration getTrConfig(){
	    	if(trConfig==null)trConfig=new TRConfiguration();
		return trConfig;
		}

    /**
     * @param trConfig
     *            the trConfig to set
     */
    public void setTrConfig(TRConfiguration trConfig) {
	this.trConfig = trConfig;
    }

    /**
     * @return the resourceManager
     */
    public ResourceManager getResourceManager() {
	return resourceManager;
{'fraction_non_alphanumeric': 0.12130505709624796, 'fraction_numerical': 0.012789559543230015, 'mean_word_length': 4.198778833107191, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2966931', 'n_tokens_mistral': 5141, 'n_tokens_neox': 4577, 'n_words': 1159}