1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.objectlib.effects.scramble;
|
11
|
|
12
|
import java.lang.reflect.Method;
|
13
|
import java.util.Random;
|
14
|
|
15
|
import org.distorted.library.effect.Effect;
|
16
|
import org.distorted.library.main.DistortedEffects;
|
17
|
import org.distorted.library.message.EffectListener;
|
18
|
|
19
|
import org.distorted.objectlib.main.ObjectPreRender;
|
20
|
import org.distorted.objectlib.main.TwistyObject;
|
21
|
import org.distorted.objectlib.effects.BaseEffect;
|
22
|
import org.distorted.objectlib.helpers.MovesFinished;
|
23
|
import org.distorted.objectlib.main.TwistyObjectNode;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
|
27
|
public abstract class ScrambleEffect extends BaseEffect implements EffectListener, MovesFinished
|
28
|
{
|
29
|
public enum Type
|
30
|
{
|
31
|
NONE (ScrambleEffectNone.class ),
|
32
|
ROTATIONS (ScrambleEffectRotations.class ),
|
33
|
;
|
34
|
|
35
|
final Class<? extends ScrambleEffect> effect;
|
36
|
|
37
|
Type(Class<? extends ScrambleEffect> effect)
|
38
|
{
|
39
|
this.effect= effect;
|
40
|
}
|
41
|
}
|
42
|
|
43
|
private static final int NUM_EFFECTS = Type.values().length;
|
44
|
private static final int FAKE_EFFECT_ID = -3;
|
45
|
private static final Type[] types;
|
46
|
|
47
|
static
|
48
|
{
|
49
|
int i=0;
|
50
|
types = new Type[NUM_EFFECTS];
|
51
|
|
52
|
for(Type type: Type.values())
|
53
|
{
|
54
|
types[i++] = type;
|
55
|
}
|
56
|
}
|
57
|
|
58
|
private int mEffectReturned;
|
59
|
private int mNumScramblesLeft;
|
60
|
private int mDurationPerDegree;
|
61
|
private final Random mRnd;
|
62
|
private int[][] mBasicAngle;
|
63
|
private boolean mRotReady, mPluginReady;
|
64
|
|
65
|
ObjectPreRender mPre;
|
66
|
TwistyObject mObject;
|
67
|
TwistyObjectNode mObjectNode;
|
68
|
Effect[] mNodeEffects;
|
69
|
int[] mNodeEffectPosition;
|
70
|
Effect[] mCubeEffects;
|
71
|
int[] mCubeEffectPosition;
|
72
|
int mCubeEffectNumber, mNodeEffectNumber;
|
73
|
int mNumScrambles;
|
74
|
int[][] mScrambles;
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
ScrambleEffect()
|
79
|
{
|
80
|
mRnd = new Random( System.currentTimeMillis() );
|
81
|
}
|
82
|
|
83
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84
|
|
85
|
abstract void createEffects(int duration, int numScrambles);
|
86
|
abstract void effectFinishedPlugin(final long effectID);
|
87
|
|
88
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
|
90
|
private void createBaseEffects(int duration, int numScrambles)
|
91
|
{
|
92
|
mNumScramblesLeft = numScrambles;
|
93
|
int absAngle, angle, axis, row, basicDegrees, totalDegrees = 0;
|
94
|
|
95
|
for(int scramble=0; scramble<mNumScramblesLeft; scramble++)
|
96
|
{
|
97
|
mObject.randomizeNewScramble(mScrambles, mRnd, scramble, numScrambles);
|
98
|
int[] s = mScrambles[scramble];
|
99
|
axis = s[0];
|
100
|
row = s[1];
|
101
|
angle = s[2];
|
102
|
absAngle = (angle<0 ? -angle : angle);
|
103
|
basicDegrees = 360/mBasicAngle[axis][row];
|
104
|
totalDegrees += absAngle*basicDegrees;
|
105
|
}
|
106
|
|
107
|
// tablebase scrambling can return a scramble which is shorter than requested,
|
108
|
// padding the end with three 0s. Take note of that.
|
109
|
for(int scramble=0; scramble<mNumScramblesLeft; scramble++)
|
110
|
{
|
111
|
int[] s = mScrambles[scramble];
|
112
|
if( s[0]==0 && s[1]==0 && s[2]==0 ) mNumScramblesLeft = scramble;
|
113
|
}
|
114
|
|
115
|
mDurationPerDegree = duration/totalDegrees;
|
116
|
mNumScrambles = 0;
|
117
|
|
118
|
mRotReady = false;
|
119
|
mPluginReady = false;
|
120
|
|
121
|
addNewScramble();
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
|
126
|
private void addNewScramble()
|
127
|
{
|
128
|
if( mNumScramblesLeft>0 )
|
129
|
{
|
130
|
int axis = mScrambles[mNumScrambles][0];
|
131
|
int row = mScrambles[mNumScrambles][1];
|
132
|
int angle= mScrambles[mNumScrambles][2];
|
133
|
|
134
|
mNumScramblesLeft--;
|
135
|
mPre.addRotation(this, axis, (1<<row), angle, mDurationPerDegree);
|
136
|
mNumScrambles++;
|
137
|
}
|
138
|
else
|
139
|
{
|
140
|
mRotReady = true;
|
141
|
if( mPluginReady ) mPre.effectFinished(FAKE_EFFECT_ID);
|
142
|
}
|
143
|
}
|
144
|
|
145
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146
|
|
147
|
private void assignEffects()
|
148
|
{
|
149
|
for(int i=0; i<mCubeEffectNumber; i++)
|
150
|
{
|
151
|
mObject.applyEffect(mCubeEffects[i],mCubeEffectPosition[i]);
|
152
|
mCubeEffects[i].notifyWhenFinished(this);
|
153
|
}
|
154
|
|
155
|
DistortedEffects nodeEffects = mObjectNode.getEffects();
|
156
|
|
157
|
for(int i=0; i<mNodeEffectNumber; i++)
|
158
|
{
|
159
|
nodeEffects.apply(mNodeEffects[i],mNodeEffectPosition[i]);
|
160
|
mNodeEffects[i].notifyWhenFinished(this);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
|
166
|
private void disassignEffects()
|
167
|
{
|
168
|
for(int i=0; i<mCubeEffectNumber; i++)
|
169
|
{
|
170
|
mObject.removeEffect(mCubeEffects[i].getID());
|
171
|
}
|
172
|
|
173
|
DistortedEffects nodeEffects = mObjectNode.getEffects();
|
174
|
|
175
|
for(int i=0; i<mNodeEffectNumber; i++)
|
176
|
{
|
177
|
nodeEffects.abortById(mNodeEffects[i].getID());
|
178
|
}
|
179
|
}
|
180
|
|
181
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182
|
// PUBLIC API
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
|
185
|
@SuppressWarnings("unused")
|
186
|
public static String[] getNames()
|
187
|
{
|
188
|
String[] names = new String[NUM_EFFECTS];
|
189
|
|
190
|
for( int i=0; i<NUM_EFFECTS; i++)
|
191
|
{
|
192
|
names[i] = types[i].name();
|
193
|
}
|
194
|
|
195
|
return names;
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
@SuppressWarnings("unused")
|
201
|
public static ScrambleEffect create(int ordinal) throws InstantiationException, IllegalAccessException
|
202
|
{
|
203
|
return types[ordinal].effect.newInstance();
|
204
|
}
|
205
|
|
206
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207
|
|
208
|
public void onActionFinished(final long effectID)
|
209
|
{
|
210
|
addNewScramble();
|
211
|
}
|
212
|
|
213
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
214
|
|
215
|
private void effectFinishedAction(final long effectID)
|
216
|
{
|
217
|
mEffectReturned++;
|
218
|
effectFinishedPlugin(effectID);
|
219
|
|
220
|
if( mEffectReturned == mCubeEffectNumber+mNodeEffectNumber )
|
221
|
{
|
222
|
disassignEffects();
|
223
|
|
224
|
mPluginReady = true;
|
225
|
if( mRotReady ) mPre.effectFinished(FAKE_EFFECT_ID);
|
226
|
}
|
227
|
}
|
228
|
|
229
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230
|
|
231
|
public void effectFinished(final long effectID)
|
232
|
{
|
233
|
for(int i=0; i<mCubeEffectNumber; i++)
|
234
|
{
|
235
|
long id = mCubeEffects[i].getID();
|
236
|
|
237
|
if( effectID == id )
|
238
|
{
|
239
|
effectFinishedAction(effectID);
|
240
|
return;
|
241
|
}
|
242
|
}
|
243
|
|
244
|
for(int i=0; i<mNodeEffectNumber; i++)
|
245
|
{
|
246
|
long id = mNodeEffects[i].getID();
|
247
|
|
248
|
if( effectID == id )
|
249
|
{
|
250
|
effectFinishedAction(effectID);
|
251
|
return;
|
252
|
}
|
253
|
}
|
254
|
}
|
255
|
|
256
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
257
|
|
258
|
@SuppressWarnings("unused")
|
259
|
public long start(int duration, ObjectPreRender pre)
|
260
|
{
|
261
|
mObject = pre.getObject();
|
262
|
mObjectNode = pre.getObjectNode();
|
263
|
mPre = pre;
|
264
|
|
265
|
// NOT mController.solve() !! This would be a very subtle bug. We need to do this immediately,
|
266
|
// because here we are already inside the mController.preRender() function (doing 'scrambleObjectNow')
|
267
|
// and doing a delayed 'solve()' here would mean we'd be sometimes first doing the first rotation,
|
268
|
// and only after it - the solve.
|
269
|
mObject.solve();
|
270
|
|
271
|
mBasicAngle = mObject.getBasicAngles();
|
272
|
|
273
|
int numScrambles = pre.getNumScrambles();
|
274
|
mScrambles = new int[numScrambles][3];
|
275
|
int dura = (int)(duration*Math.pow(numScrambles,0.66f));
|
276
|
createBaseEffects(dura,numScrambles);
|
277
|
createEffects (dura,numScrambles);
|
278
|
|
279
|
if( mCubeEffectNumber==0 && mNodeEffectNumber==0 )
|
280
|
{
|
281
|
throw new RuntimeException("Cube and Node Plugin Effects not created!");
|
282
|
}
|
283
|
|
284
|
assignEffects();
|
285
|
|
286
|
return FAKE_EFFECT_ID;
|
287
|
}
|
288
|
|
289
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290
|
|
291
|
@SuppressWarnings("unused")
|
292
|
public static void enableEffects()
|
293
|
{
|
294
|
Method method;
|
295
|
|
296
|
for(Type type: Type.values())
|
297
|
{
|
298
|
try
|
299
|
{
|
300
|
method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
|
301
|
}
|
302
|
catch(NoSuchMethodException ex)
|
303
|
{
|
304
|
android.util.Log.e("ScrambleEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
|
305
|
method = null;
|
306
|
}
|
307
|
|
308
|
try
|
309
|
{
|
310
|
if( method!=null ) method.invoke(null);
|
311
|
}
|
312
|
catch(Exception ex)
|
313
|
{
|
314
|
android.util.Log.e("ScrambleEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
|
315
|
}
|
316
|
}
|
317
|
}
|
318
|
}
|