1
1
/*
2
- * To change this license header, choose License Headers in Project Properties.
3
- * To change this template file, choose Tools | Templates
4
- * and open the template in the editor.
5
- */
2
+ * Copyright (c) 2020-2022 Martin Prout
3
+ */
6
4
package monkstone ;
7
5
8
- import monkstone .noise .OpenSimplex2F ;
6
+ import monkstone .noise .OpenSimplex2 ;
9
7
import org .jruby .Ruby ;
10
8
import org .jruby .RubyFixnum ;
11
9
import org .jruby .RubyFloat ;
22
20
@ JRubyModule (name = "FastNoise" )
23
21
public class FastNoiseModuleJava {
24
22
25
- static OpenSimplex2F ng = new OpenSimplex2F ( System . currentTimeMillis () );
26
-
23
+ static OpenSimplex2 ng = new OpenSimplex2 ( );
24
+ static long seed = System . currentTimeMillis ();
27
25
/**
28
26
*
29
27
* @param runtime Ruby
@@ -32,6 +30,23 @@ public static void createNoiseModule(Ruby runtime) {
32
30
RubyModule noiseModule = runtime .defineModule ("FastNoise" );
33
31
noiseModule .defineAnnotatedMethods (FastNoiseModuleJava .class );
34
32
}
33
+
34
+
35
+ /**
36
+ * Utility method
37
+ *
38
+ * @param obj
39
+ * @return parse float value of object or zero
40
+ */
41
+ private static double jvalue (IRubyObject obj ) {
42
+ if (obj instanceof RubyFloat rubyFloat ) {
43
+ return rubyFloat .getValue ();
44
+ }
45
+ if (obj instanceof RubyFixnum rubyFixnum ) {
46
+ return rubyFixnum .getDoubleValue ();
47
+ }
48
+ return 0 ;
49
+ }
35
50
36
51
/**
37
52
*
@@ -42,34 +57,37 @@ public static void createNoiseModule(Ruby runtime) {
42
57
*/
43
58
@ JRubyMethod (name = "tnoise" , rest = true , module = true )
44
59
public static IRubyObject terrainNoiseImpl (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
45
- double result = 0 ;
46
60
double one ;
47
61
double two ;
48
62
double three ;
49
63
double four ;
50
- switch (args .length ) {
51
- case 2 :
52
- two = args [1 ] instanceof RubyFloat ? ((RubyFloat ) args [1 ]).getValue () : ((RubyFixnum ) args [1 ]).getDoubleValue ();
53
- one = args [0 ] instanceof RubyFloat ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
54
- result = ng .noise2_XBeforeY (one , two );
55
- break ;
56
- case 3 :
57
- three = args [2 ] instanceof RubyFloat ? ((RubyFloat ) args [2 ]).getValue () : ((RubyFixnum ) args [2 ]).getDoubleValue ();
58
- two = args [1 ] instanceof RubyFloat ? ((RubyFloat ) args [1 ]).getValue () : ((RubyFixnum ) args [1 ]).getDoubleValue ();
59
- one = args [0 ] instanceof RubyFloat ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
60
- result = ng .noise3_XYBeforeZ (one , two , three );
61
- break ;
62
- case 4 :
63
- four = args [3 ] instanceof RubyFloat ? ((RubyFloat ) args [3 ]).getValue () : ((RubyFixnum ) args [3 ]).getDoubleValue ();
64
- three = args [2 ] instanceof RubyFloat ? ((RubyFloat ) args [2 ]).getValue () : ((RubyFixnum ) args [2 ]).getDoubleValue ();
65
- two = args [1 ] instanceof RubyFloat ? ((RubyFloat ) args [1 ]).getValue () : ((RubyFixnum ) args [1 ]).getDoubleValue ();
66
- one = args [0 ] instanceof RubyFloat ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
67
- result = ng .noise4_XYBeforeZW (one , two , three , four );
68
- break ;
69
- default :
70
- throw new RuntimeException ("Min 2D Max 4D Noise" );
64
+ double result = switch (args .length ) {
65
+ case 2 -> {
66
+ two = jvalue (args [1 ]);
67
+ one = jvalue (args [0 ]);
68
+ //yield ng.noise2_XBeforeY(one, two);
69
+ yield ng .noise2 (seed , one , two );
70
+ }
71
+ case 3 -> {
72
+ three = jvalue (args [2 ]);
73
+ two = jvalue (args [1 ]);
74
+ one = jvalue (args [0 ]);
75
+ yield ng .noise3_ImproveXY (seed , one , two , three );
76
+ }
77
+ case 4 -> {
78
+ four = jvalue (args [3 ]);
79
+ three = jvalue (args [2 ]);
80
+ two = jvalue (args [1 ]);
81
+ one = jvalue (args [0 ]);
82
+ yield ng .noise4_ImproveXYZ_ImproveXY (seed , one , two , three , four );
83
+ }
84
+ default -> { yield 2 ; } // yield an invalid value for noise
85
+ };
86
+ if (result != 2 ) {
87
+ return RubyFloat .newFloat (context .runtime , result );
88
+ } else {
89
+ throw new RuntimeException ("Min 2D Max 4D Noise" );
71
90
}
72
- return RubyFloat .newFloat (context .runtime , result );
73
91
}
74
92
75
93
/**
@@ -81,47 +99,51 @@ public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject re
81
99
*/
82
100
@ JRubyMethod (name = "noise" , rest = true , module = true )
83
101
public static IRubyObject noiseImpl (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
84
- double result = 0 ;
85
102
double one ;
86
103
double two ;
87
104
double three ;
88
105
double four ;
89
- switch (args .length ) {
90
- case 1 :
91
- one = args [0 ] instanceof RubyFloat ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
92
- result = ng .noise2 (one , 0 );
93
- break ;
94
- case 2 :
95
- two = args [1 ] instanceof RubyFloat ? ((RubyFloat ) args [1 ]).getValue () : ((RubyFixnum ) args [1 ]).getDoubleValue ();
96
- one = args [0 ] instanceof RubyFloat ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
97
- result = ng .noise2 (one , two );
98
- break ;
99
- case 3 :
100
- three = args [2 ] instanceof RubyFloat ? ((RubyFloat ) args [2 ]).getValue () : ((RubyFixnum ) args [2 ]).getDoubleValue ();
101
- two = args [1 ] instanceof RubyFloat ? ((RubyFloat ) args [1 ]).getValue () : ((RubyFixnum ) args [1 ]).getDoubleValue ();
102
- one = args [0 ] instanceof RubyFloat ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
103
- result = ng .noise3_Classic (one , two , three );
104
- break ;
105
- case 4 :
106
- four = args [3 ] instanceof RubyFloat ? ((RubyFloat ) args [3 ]).getValue () : ((RubyFixnum ) args [3 ]).getDoubleValue ();
107
- three = args [2 ] instanceof RubyFloat ? ((RubyFloat ) args [2 ]).getValue () : ((RubyFixnum ) args [2 ]).getDoubleValue ();
108
- two = args [1 ] instanceof RubyFloat ? ((RubyFloat ) args [1 ]).getValue () : ((RubyFixnum ) args [1 ]).getDoubleValue ();
109
- one = args [0 ] instanceof RubyFloat ? ((RubyFloat ) args [0 ]).getValue () : ((RubyFixnum ) args [0 ]).getDoubleValue ();
110
- result = ng .noise4_Classic (one , two , three , four );
111
- break ;
112
- default :
113
- throw new RuntimeException ("Maximum of 4D Noise" );
106
+ double result = switch (args .length ) {
107
+ case 1 -> {
108
+ one = jvalue (args [0 ]);
109
+ yield ng .noise2 (seed , one , 0 );
110
+ }
111
+ case 2 -> {
112
+ two = jvalue (args [1 ]);
113
+ one = jvalue (args [0 ]);
114
+ yield ng .noise2 (seed , one , two );
115
+ }
116
+ case 3 -> {
117
+ three = jvalue (args [2 ]);
118
+ two = jvalue (args [1 ]);
119
+ one = jvalue (args [0 ]);
120
+ yield ng .noise3_ImproveXY (seed , one , two , three );
121
+ }
122
+ case 4 -> {
123
+ four = jvalue (args [3 ]);
124
+ three = jvalue (args [2 ]);
125
+ two = jvalue (args [1 ]);
126
+ one = jvalue (args [0 ]);
127
+ yield ng .noise4_ImproveXY_ImproveZW (seed , one , two , three , four );
128
+ }
129
+ default -> { yield 2 ; } // yield an invalid value for noise
130
+ };
131
+ if (result != 2 ) {
132
+ return RubyFloat .newFloat (context .runtime , result );
133
+ } else {
134
+ throw new RuntimeException ("Min 2D Max 4D Noise" );
114
135
}
115
- return RubyFloat .newFloat (context .runtime , result );
116
136
}
137
+ }
138
+
117
139
// @JRubyMethod(name = "noise_seed", rest = true, module = true)
118
140
// public static IRubyObject noiseSeedImpl(ThreadContext context, IRubyObject recv, IRubyObject arg) {
119
141
// long seed;
120
142
// if (arg instanceof RubyNumeric) {
121
143
// seed = ((RubyNumeric) arg).getLongValue();
122
- // ng = new OpenSimplex2F (seed);
144
+ // ng = new OpenSimplex2 (seed);
123
145
// return RubyBoolean.newBoolean(context.runtime, true);
124
146
// }
125
147
// return RubyBoolean.newBoolean(context.runtime, false);
126
148
// }
127
- }
149
+ // }
0 commit comments