aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/bartworks/util/NoiseUtil/BartsNoise.java
blob: 2130f27ccd1b7f31075338e67e6d4f184b9aae4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (c) 2018-2019 bartimaeusnek Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions: The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

package bartworks.util.NoiseUtil;

import java.util.Random;

import bartworks.API.INoiseGen;
import bartworks.util.MathUtils;
import gregtech.api.objects.XSTR;

public class BartsNoise implements INoiseGen {

    public void setUsePhase(boolean usePhase) {
        this.usePhase = usePhase;
    }

    public void setRandom(Random random) {
        this.random = random;
    }

    boolean usePhase;
    int octaves;
    double frequency;
    double amplitude;
    long seed;
    Random random;

    public enum NoiseColor {

        Red(-1),
        Pink(-0.5),
        White(0),
        Blue(0.5),
        Violet(1);

        NoiseColor(double num) {
            this.ampl = num;
        }

        double ampl;

        public BartsNoise getColoredNoise() {
            BartsNoise noise = new BartsNoise();
            noise.setAmplitude(this.ampl);
            return noise;
        }
    }

    public BartsNoise(int octaves, double frequency, double amplitude, long seed) {
        this.octaves = octaves;
        this.frequency = frequency;
        this.amplitude = amplitude;
        this.seed = seed;
        this.random = new XSTR(seed);
    }

    public BartsNoise() {
        this.seed = new XSTR().nextLong();
        this.random = new XSTR(this.seed);
        this.octaves = 1;
        this.frequency = this.random.nextGaussian();
        this.amplitude = this.random.nextGaussian();
    }

    public BartsNoise(long seed) {
        this.seed = seed;
        this.random = new XSTR(seed);
        this.octaves = 1;
        this.frequency = this.random.nextGaussian();
        this.amplitude = this.random.nextGaussian();
    }

    public BartsNoise copy() {
        return new BartsNoise(this.octaves, this.frequency, this.amplitude, this.seed);
    }

    public BartsNoise copy(long seed) {
        return new BartsNoise(this.octaves, this.frequency, this.amplitude, seed);
    }

    public double getCosNoise(double x, double y) {
        double pr = x * this.frequency;
        double r1 = Math.cos(pr);
        if (r1 < 0) r1 = Math.abs(r1);
        double result = Math.pow(r1, this.amplitude);
        double pr2 = y * this.frequency;
        double r2 = Math.cos(pr2);
        if (r2 < 0) r2 = Math.abs(r2);
        double result2 = Math.pow(r2, this.amplitude);
        result *= result2;
        if (result == Double.POSITIVE_INFINITY) result = Double.MAX_VALUE;
        if (result == Double.NEGATIVE_INFINITY) result = Double.MIN_VALUE;
        return MathUtils.wrap(result, 1D);
    }

    double getNonOctavedNoise(double x, double y) {
        double phase = SimplexNoise
            .noise(Math.pow(x * this.frequency, this.amplitude), Math.pow(y * this.frequency, this.amplitude));
        return MathUtils.wrap(phase, 1);
    }

    public double getNeighbouringNoise(int x, int y) {
        return (this.getNoiseSingle(x - 1, y - 1) + this.getNoiseSingle(x, y - 1)
            + this.getNoiseSingle(x - 1, y)
            + this.getNoiseSingle(x + 1, y)
            + this.getNoiseSingle(x, y + 1)
            + this.getNoiseSingle(x + 1, y + 1)
            + this.getNoiseSingle(x - 1, y + 1)
            + this.getNoiseSingle(x + 1, y - 1)) / 8;
    }

    public double getNoiseSingle(int x, int y) {
        double result = 0;
        for (double i = 1; i <= this.octaves; i++) {
            result += 1d / i * this.getNonOctavedNoise(i * x, i * y);
        }
        return result;
    }

    @Override
    public double getNoise(int x, int y) {
        double result = 0;
        for (double i = 1; i <= this.octaves; i++) {
            result += 1d / i * this.getNonOctavedNoise(i * x, y);
        }
        // result = (this.getNeighbouringNoise(x,y)+result)/2;
        return MathUtils.wrap(result, 1D);
    }

    @Override
    public double[][] getNoiseForRegion(int xStart, int zStart, int xEnd, int zEnd) {
        // double[][] results = new double[Math.abs(xEnd)-Math.abs(xStart)][Math.abs(zEnd)-Math.abs(zStart)];
        // for (int i = xStart; i < xEnd; i++) {
        // for (int j = zStart; j < zEnd; j++) {
        // results
        // }
        // }
        return new double[0][0];
    }

    @Override
    public void setAmplitude(double amplitude) {
        this.amplitude = amplitude;
    }

    @Override
    public void setOctaves(int octaves) {
        this.octaves = octaves;
    }

    @Override
    public void setFrequency(double freq) {
        this.frequency = freq;
    }

    @Override
    public void setSeed(long seed) {
        this.seed = seed;
    }
}