11/*
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+ */
64package monkstone ;
75
8- import monkstone .noise .OpenSimplex2F ;
6+ import monkstone .noise .OpenSimplex2 ;
97import org .jruby .Ruby ;
108import org .jruby .RubyFixnum ;
119import org .jruby .RubyFloat ;
2220@ JRubyModule (name = "FastNoise" )
2321public class FastNoiseModuleJava {
2422
25- static OpenSimplex2F ng = new OpenSimplex2F ( System . currentTimeMillis () );
26-
23+ static OpenSimplex2 ng = new OpenSimplex2 ( );
24+ static long seed = System . currentTimeMillis ();
2725 /**
2826 *
2927 * @param runtime Ruby
@@ -32,6 +30,23 @@ public static void createNoiseModule(Ruby runtime) {
3230 RubyModule noiseModule = runtime .defineModule ("FastNoise" );
3331 noiseModule .defineAnnotatedMethods (FastNoiseModuleJava .class );
3432 }
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+ }
3550
3651 /**
3752 *
@@ -42,34 +57,37 @@ public static void createNoiseModule(Ruby runtime) {
4257 */
4358 @ JRubyMethod (name = "tnoise" , rest = true , module = true )
4459 public static IRubyObject terrainNoiseImpl (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
45- double result = 0 ;
4660 double one ;
4761 double two ;
4862 double three ;
4963 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" );
7190 }
72- return RubyFloat .newFloat (context .runtime , result );
7391 }
7492
7593 /**
@@ -81,47 +99,51 @@ public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject re
8199 */
82100 @ JRubyMethod (name = "noise" , rest = true , module = true )
83101 public static IRubyObject noiseImpl (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
84- double result = 0 ;
85102 double one ;
86103 double two ;
87104 double three ;
88105 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" );
114135 }
115- return RubyFloat .newFloat (context .runtime , result );
116136 }
137+ }
138+
117139// @JRubyMethod(name = "noise_seed", rest = true, module = true)
118140// public static IRubyObject noiseSeedImpl(ThreadContext context, IRubyObject recv, IRubyObject arg) {
119141// long seed;
120142// if (arg instanceof RubyNumeric) {
121143// seed = ((RubyNumeric) arg).getLongValue();
122- // ng = new OpenSimplex2F (seed);
144+ // ng = new OpenSimplex2 (seed);
123145// return RubyBoolean.newBoolean(context.runtime, true);
124146// }
125147// return RubyBoolean.newBoolean(context.runtime, false);
126148// }
127- }
149+ // }
0 commit comments