numutils.java

来自「Java mulitplayer strategy game. Adaptati」· Java 代码 · 共 195 行

JAVA
195
字号
/*
 * Created on 2004-08-24
 *
 */
package net.sf.jawp.util;

import java.io.Serializable;
import java.util.Random;

/**
 * Simplifies numerical calculations on semi-floats.
 * 
 * Semi-float is an integer which is interpereted as float.
 * Usually some least important bits represent fractional part.
 *  (by default 10 last ) 
 * 
 * @version $Revision: 1.10 $
 *          <p>
 *          <b>The latest changes:</b><br/> $Date: 2005/10/18 07:49:34 $<br/>
 *          $Author: overmindx $
 *          </p>
 * @author jarek
 */

public class NumUtils implements Serializable
{
	/*
	 * for future use private static final ZLog log =
	 * ZLog.getLog(NumUtils.class);
	 */

	private static final long serialVersionUID = 101010;

	/*
	 * all below are simple hacks to improve performance
	 * coding like this 
	 * can spare You up to 3 seconds per each century ( or waste ca. 3 years )
	 */
	/**
	 * this is base (number of digital places after zero...)?
	 * 
	 * 10 means that 1024 represents value 1 in ourt system
	 */
	private static final int BASE = 10;

	/**
	 * the logic value 1 in system
	 */
	private static final int IDENT = 1 << BASE;

	/**
	 * logic value 1/2 in this system
	 */
	private static final int HALF = IDENT >> 1;

	/**
	 * mask for retrieving fractional part
	 */
	private static final int MASK = IDENT - 1;

	private final Random rand;

	/**
	 * gaussian
	 */
	private final int[] normal = new int[IDENT];

	
	public NumUtils()
	{
		this(1);
	}
	
	public NumUtils(final int rseed)
	{
		this.rand = new Random(rseed);
		init();
	}
	
	public static int floor(final int val)
	{
		return (val >> BASE);
	}

	/**
	 * section initiaqlizes gaussian table
	 * 
	 */
	private  void init()
	{
		/*
		 * final int cntbase = 2;
		 * 
		 * final int cnt = 1<<base;
		 */
		/*
		 * final double var = (1024*1024)/12; final double svar = Math.sqrt( var );
		 * final double scnt = Math.sqrt( cnt); final double dd = svar * scnt;
		 */
		final double z = 2.5;
		for (int i = 0; i < normal.length; i++)
		{
			double g = rand.nextGaussian(); // mean = 0 (-5 ..5) is our
											// interesting
			g = g + z; // (0...10)
			g = g / (z + z); // 0..1
			g *= (double) IDENT;
			// System.out.println("generated g:"+g);
			normal[i] = Math.max(0, (int) g);
			// System.out.println("generated:"+normal[i]+" for "+i);
		}
	}

	
	public static int fract(final int val)
	{
		return (val & NumUtils.MASK);
	}

	public static int round(final int val)
	{
		return NumUtils.floor((val + NumUtils.HALF));
	}

	public static int getIdent()
	{
		return NumUtils.IDENT;
	}

	public static int getLogicValue(final int bVal)
	{
		return bVal << BASE;
	}

	public static int getBase()
	{
		return BASE;
	}

	public int randomGauss(final int val)
	{
		return randomGauss() * val;
	}

	public int randomGauss()
	{
		final int res = normal[random()];
		// assert log.debug("random g="+res);
		return res;
	}

	/**
	 * random number (0-val)
	 */
	public int random(final int val)
	{
		return random() * val;
	}

	/**
	 * random number (0-1(logical))
	 * 
	 * @return
	 */
	public int random()
	{
		return fract(rand.nextInt());
	}

	/**
	 * return internal Random generator
	 */
	public Random getRandom()
	{
		return this.rand;
	}
	
	/**
	 * mostly for debug purposes
	 */
	public static String formatPosValDebug(final int pos)
	{
		return String.valueOf(pos) + "(" + NumUtils.round(pos) + ")";
	}

	/**
	 * mostly for debug purposes
	 */
	public static String formatPositionDebug(final int xpos, final int ypos)
	{
		return "[" + formatPosValDebug(xpos) + "," + formatPosValDebug(ypos)
				+ "]";
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?