Package transientlibs.objects.general

Source Code of transientlibs.objects.general.Dice

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.objects.general;

import transientlibs.objects.primitives.Interval;
import java.util.ArrayList;
import java.util.Random;
import transientlibs.slick2d.util.Log;
import static transientlibs.objects.general.Dice.rng;
import transientlibs.processors.combat.RollOutcome;
import transientlibs.processors.combat.RollOutcomeList;

/**
*
* @author kibertoad
*/
public class Dice {

    public ArrayList<Interval> rollOptions = new ArrayList<Interval>();
    public int nextIndex = 0;
    public int resultID;
    public int rolledValue;
    public int rolledRandomValue;
   
    public int maxValue; //max value that can be rolled
   
    public static final Random rng = new Random();

    public int gaussianRoll(int maxValue) {
        //double logvalue = rng.nextGaussian();
        //rolledValue = (int) ((logvalue + 2) / 4 * maxValue);
        rolledValue = (int) ((rng.nextGaussian() + 2) / 4 * maxValue);
        return rolledValue;

        //Log.info("gauss: "+logvalue);
        //        Log.info("Rolled gaussian: "+rolledValue);
    }

    public void addOption(int intervalSize, int setID) {
        if (intervalSize > 0) {
            rollOptions.add(new Interval(nextIndex, nextIndex + intervalSize, setID));

            nextIndex = nextIndex + intervalSize + 1;
        }
    }

    public void reset() {
        rollOptions.clear();
        nextIndex = 0;
        resultID = -1;
        rolledValue = -1;

    }

    /**Pick a random option from a weighted list */
    public void rollDice() {
        rolledValue = rng.nextInt(nextIndex);
        resultID = Interval.returnFittingInterval(rollOptions, rolledValue).id;
    }
   
    /**Just generate a random value */
    public void rollDiceForValue() {
        rolledValue = rng.nextInt(maxValue);
    }

    public static int getRandomValue(int min, int max) {
        int roll = rng.nextInt(max - min) + min;

        return roll;
    }

    public static Node returnRandomElement(ArrayList<Node> list) {
        int roll = rng.nextInt(list.size());

        return list.get(roll);
    }

    public RollOutcome returnOutcome(RollOutcomeList outcomeCodes) {

        for (RollOutcome ro : outcomeCodes.elements) {
            if ((ro.minRollValue <= this.rolledValue) && (ro.maxRollValue >= this.rolledValue)) {
                return ro;
            }
        }

        return null;
    }
}


/*
class RollOption {

    Interval interval;
    int ID;

    public RollOption(int from, int to, int setID) {
        interval = new Interval(from, to);
        ID = setID;
    }
    *

}
*
*/
TOP

Related Classes of transientlibs.objects.general.Dice

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.