001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.tridentsdk.server.world.gen;
018
019import java.util.Random;
020
021/**
022 * 
023 */
024public class SimplexOctaveGenerator {
025    private final SimplexNoiseGenerator[] generators;
026    private final double[] amplitudes;
027    private final double[] frequencies;
028    
029    /**
030     * Creates a new SimplexOctave generator with parameters
031     * 
032     * <p>Given the same seed, output will be consistent from one runtime to another.</p>
033     *  
034     * @param octaves number of passes of the generator to use, each one at double the resolution
035     * @param persistence the diminishing value of each successive octave, must be in the range 0 < persistence <= 0.5
036     *                    to avoid producing values that exceed 1                    
037     * @param seed the seed to use for this generator, usually the world seed
038     */
039    public SimplexOctaveGenerator(int octaves, double persistence, int seed) {
040        Random rand = new Random(seed);
041        
042        generators = new SimplexNoiseGenerator[octaves];
043        frequencies = new double[octaves];
044        amplitudes = new double[octaves];
045        
046        for (int i = 0; i < octaves; i++) {
047            generators[i] = new SimplexNoiseGenerator(rand.nextInt());
048            // each freqency will be 2^i, each amplitude is peristence to the octaves - ith power
049            frequencies[i] = Math.pow(2,i);
050            amplitudes[i] = Math.pow(persistence,octaves - i);
051        }
052    }
053    
054    public double noise(int x, int y) {
055        double retVal = 0;
056        for(int i = 0; i < generators.length; i ++) {
057            retVal += generators[i].noise(x / frequencies[i], y / frequencies[i]) * amplitudes[i];
058        }
059        return retVal;
060    }
061    
062    public double noise(int x, int y, int z) {
063        double retVal = 0;
064        for(int i = 0; i < generators.length; i ++) {
065            retVal += generators[i].noise(x / frequencies[i], y / frequencies[i], z / frequencies[i]) * amplitudes[i];
066        }
067        return retVal;
068    }
069}