Project

General

Profile

Download (40.7 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 3e6b6e37

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objects;
21

    
22
import android.content.SharedPreferences;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27

    
28
import com.google.firebase.crashlytics.FirebaseCrashlytics;
29

    
30
import org.distorted.helpers.FactoryCubit;
31
import org.distorted.helpers.FactorySticker;
32
import org.distorted.helpers.ObjectShape;
33
import org.distorted.helpers.ObjectSticker;
34
import org.distorted.helpers.QuatHelper;
35
import org.distorted.helpers.ScrambleState;
36
import org.distorted.library.effect.Effect;
37
import org.distorted.library.effect.MatrixEffectMove;
38
import org.distorted.library.effect.MatrixEffectQuaternion;
39
import org.distorted.library.effect.MatrixEffectScale;
40
import org.distorted.library.effect.VertexEffectQuaternion;
41
import org.distorted.library.effect.VertexEffectRotate;
42
import org.distorted.library.main.DistortedEffects;
43
import org.distorted.library.main.DistortedLibrary;
44
import org.distorted.library.main.DistortedNode;
45
import org.distorted.library.main.DistortedTexture;
46
import org.distorted.library.mesh.MeshBase;
47
import org.distorted.library.mesh.MeshFile;
48
import org.distorted.library.mesh.MeshJoined;
49
import org.distorted.library.mesh.MeshSquare;
50
import org.distorted.library.message.EffectListener;
51
import org.distorted.library.type.Dynamic1D;
52
import org.distorted.library.type.Static1D;
53
import org.distorted.library.type.Static3D;
54
import org.distorted.library.type.Static4D;
55
import org.distorted.main.BuildConfig;
56

    
57
import java.io.DataInputStream;
58
import java.io.IOException;
59
import java.io.InputStream;
60
import java.util.Random;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
public abstract class TwistyObject extends DistortedNode
65
  {
66
  public static final int COLOR_YELLOW = 0xffffff00;
67
  public static final int COLOR_WHITE  = 0xffffffff;
68
  public static final int COLOR_BLUE   = 0xff0000ff;
69
  public static final int COLOR_GREEN  = 0xff00bb00;
70
  public static final int COLOR_RED    = 0xff990000;
71
  public static final int COLOR_ORANGE = 0xffff6200;
72
  public static final int COLOR_GREY   = 0xff727c7b;
73
  public static final int COLOR_VIOLET = 0xff7700bb;
74
  public static final int COLOR_BLACK  = 0xff000000;
75

    
76
  public static final int TEXTURE_HEIGHT = 256;
77
  static final int NUM_STICKERS_IN_ROW = 4;
78

    
79
  static final float SQ2 = (float)Math.sqrt(2);
80
  static final float SQ3 = (float)Math.sqrt(3);
81
  static final float SQ5 = (float)Math.sqrt(5);
82
  static final float SQ6 = (float)Math.sqrt(6);
83

    
84
  private static final float NODE_RATIO = 1.60f;
85
  private static final float MAX_SIZE_CHANGE = 1.35f;
86
  private static final float MIN_SIZE_CHANGE = 0.75f;
87

    
88
  private static final Static3D CENTER = new Static3D(0,0,0);
89
  private static final int POST_ROTATION_MILLISEC = 500;
90

    
91
  MeshBase[] mMeshes;
92
  final Static4D[] OBJECT_QUATS;
93
  final Cubit[] CUBITS;
94
  final int NUM_FACE_COLORS;
95
  final int NUM_TEXTURES;
96
  final int NUM_CUBITS;
97
  final int NUM_AXIS;
98
  final int NUM_QUATS;
99

    
100
  private final int mNumCubitFaces;
101
  private final Static3D[] mAxis;
102
  private final float[][] mCuts;
103
  private final int[] mNumCuts;
104
  private final int mNodeSize;
105
  private final float[][] mOrigPos;
106
  private final Static3D mNodeScale;
107
  private final Static4D mQuat;
108
  private final int mNumLayers, mRealSize;
109
  private final ObjectList mList;
110
  private final DistortedEffects mEffects;
111
  private final VertexEffectRotate mRotateEffect;
112
  private final Dynamic1D mRotationAngle;
113
  private final Static3D mRotationAxis;
114
  private final Static3D mObjectScale;
115
  private final int[] mQuatDebug;
116
  private final float mCameraDist;
117
  private final Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
118
  private final DistortedTexture mTexture;
119
  private final float mInitScreenRatio;
120
  private final int mSolvedFunctionIndex;
121
  private final boolean mIsBandaged;
122
  private float mObjectScreenRatio;
123
  private int[][] mSolvedQuats;
124
  private int[][] mQuatMult;
125
  private int[] mTmpQuats;
126
  private int mNumTexRows, mNumTexCols;
127
  private int mRotRowBitmap;
128
  private int mRotAxis;
129
  private MeshBase mMesh;
130
  private final TwistyObjectScrambler mScrambler;
131

    
132
  //////////////////// SOLVED1 ////////////////////////
133

    
134
  private int[] mFaceMap;
135
  private int[][] mScramble;
136
  private int[] mColors;
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  TwistyObject(int numLayers, int realSize, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
141
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
142
    {
143
    super(nodeTexture,nodeEffects,nodeMesh);
144

    
145
    mNodeSize = screenWidth;
146

    
147
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
148

    
149
    mNumLayers = numLayers;
150
    mRealSize = realSize;
151
    mList = list;
152
    mOrigPos = getCubitPositions(mNumLayers);
153
    mAxis = getRotationAxis();
154
    mInitScreenRatio = getScreenRatio();
155
    mObjectScreenRatio = 1.0f;
156
    mNumCubitFaces = getNumCubitFaces();
157
    mSolvedFunctionIndex = getSolvedFunctionIndex();
158

    
159
    mCuts = getCuts(mNumLayers);
160
    mNumCuts = new int[mAxis.length];
161
    if( mCuts==null ) for(int i=0; i<mAxis.length; i++) mNumCuts[i] = 0;
162
    else              for(int i=0; i<mAxis.length; i++) mNumCuts[i] = mCuts[i].length;
163

    
164
    OBJECT_QUATS = getQuats();
165
    NUM_CUBITS  = mOrigPos.length;
166
    NUM_FACE_COLORS = getNumFaceColors();
167
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
168
    NUM_AXIS = mAxis.length;
169
    NUM_QUATS = OBJECT_QUATS.length;
170

    
171
    int scramblingType = getScrambleType();
172
    ScrambleState[] states = getScrambleStates();
173
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,numLayers,states);
174

    
175
    boolean bandaged=false;
176

    
177
    for(int c=0; c<NUM_CUBITS; c++)
178
      {
179
      if( mOrigPos[c].length>3 )
180
        {
181
        bandaged=true;
182
        break;
183
        }
184
      }
185

    
186
    mIsBandaged = bandaged;
187

    
188
    mQuatDebug = new int[NUM_CUBITS];
189

    
190
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
191
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
192

    
193
    mNodeScale= new Static3D(1,NODE_RATIO,1);
194
    mQuat = quat;
195

    
196
    mRotationAngle= new Dynamic1D();
197
    mRotationAxis = new Static3D(1,0,0);
198
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
199

    
200
    mRotationAngleStatic = new Static1D(0);
201
    mRotationAngleMiddle = new Static1D(0);
202
    mRotationAngleFinal  = new Static1D(0);
203

    
204
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
205
    mObjectScale = new Static3D(scale,scale,scale);
206
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
207
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(quat, CENTER);
208

    
209
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
210
    nodeEffects.apply(nodeScaleEffect);
211

    
212
    mNumTexCols = NUM_STICKERS_IN_ROW;
213
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
214

    
215
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
216

    
217
    CUBITS = new Cubit[NUM_CUBITS];
218
    createMeshAndCubits(list,res);
219
    createDataStructuresForSolved(numLayers);
220

    
221
    mTexture = new DistortedTexture();
222
    mEffects = new DistortedEffects();
223

    
224
    for(int q=0; q<NUM_QUATS; q++)
225
      {
226
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
227
      vq.setMeshAssociation(0,q);
228
      mEffects.apply(vq);
229
      }
230

    
231
    mEffects.apply(mRotateEffect);
232
    mEffects.apply(quatEffect);
233
    mEffects.apply(scaleEffect);
234

    
235
    // Now postprocessed effects (the glow when you solve an object) require component centers. In
236
    // order for the effect to be in front of the object, we need to set the center to be behind it.
237
    getMesh().setComponentCenter(0,0,0,-0.1f);
238

    
239
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
240

    
241
    setupPosition(moves);
242

    
243
    float fov = list.getFOV();
244
    double halfFOV = fov * (Math.PI/360);
245
    mCameraDist = 0.5f*NODE_RATIO / (float)Math.tan(halfFOV);
246

    
247
    setProjection( fov, 0.1f);
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
  private Static3D getPos(float[] origPos)
253
    {
254
    int len = origPos.length/3;
255
    float sumX = 0.0f;
256
    float sumY = 0.0f;
257
    float sumZ = 0.0f;
258

    
259
    for(int i=0; i<len; i++)
260
      {
261
      sumX += origPos[3*i  ];
262
      sumY += origPos[3*i+1];
263
      sumZ += origPos[3*i+2];
264
      }
265

    
266
    sumX /= len;
267
    sumY /= len;
268
    sumZ /= len;
269

    
270
    return new Static3D(sumX,sumY,sumZ);
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  private void createMeshAndCubits(ObjectList list, Resources res)
276
    {
277
    int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mNumLayers);
278
    int resourceID= list.getResourceIDs()[sizeIndex];
279

    
280
    if( resourceID!=0 )
281
      {
282
      InputStream is = res.openRawResource(resourceID);
283
      DataInputStream dos = new DataInputStream(is);
284
      mMesh = new MeshFile(dos);
285

    
286
      try
287
        {
288
        is.close();
289
        }
290
      catch(IOException e)
291
        {
292
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
293
        }
294

    
295
      for(int i=0; i<NUM_CUBITS; i++)
296
        {
297
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
298
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
299
        }
300

    
301
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
302
      }
303
    else
304
      {
305
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
306

    
307
      for(int i=0; i<NUM_CUBITS; i++)
308
        {
309
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
310
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
311
        Static3D pos = getPos(mOrigPos[i]);
312
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
313
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
314
        }
315

    
316
      mMesh = new MeshJoined(cubitMesh);
317
      resetAllTextureMaps();
318
      }
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
  private MeshBase createCubitMesh(int cubit, int numLayers)
324
    {
325
    int variant = getCubitVariant(cubit,numLayers);
326

    
327
    if( mMeshes==null )
328
      {
329
      FactoryCubit factory = FactoryCubit.getInstance();
330
      factory.clear();
331
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
332
      }
333

    
334
    if( mMeshes[variant]==null )
335
      {
336
      ObjectShape shape = getObjectShape(cubit,numLayers);
337
      FactoryCubit factory = FactoryCubit.getInstance();
338
      factory.createNewFaceTransform(shape);
339
      mMeshes[variant] = factory.createRoundedSolid(shape);
340
      }
341

    
342
    MeshBase mesh = mMeshes[variant].copy(true);
343
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
344
    mesh.apply(quat,0xffffffff,0);
345

    
346
    return mesh;
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  private void createDataStructuresForSolved(int numLayers)
352
    {
353
    mTmpQuats = new int[NUM_QUATS];
354
    mSolvedQuats = new int[NUM_CUBITS][];
355

    
356
    for(int c=0; c<NUM_CUBITS; c++)
357
      {
358
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
359
      }
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363
// This is used to build internal data structures for the generic 'isSolved()'
364
//
365
// if this is an internal cubit (all faces black): return -1
366
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
367
// Color index, i.e. the index into the 'FACE_COLORS' table.
368
// else (edge or corner cubit, more than one non-black face): return -2.
369

    
370
  int retCubitSolvedStatus(int cubit, int numLayers)
371
    {
372
    int numNonBlack=0, nonBlackIndex=-1, color;
373

    
374
    for(int face=0; face<mNumCubitFaces; face++)
375
      {
376
      color = getFaceColor(cubit,face,numLayers);
377

    
378
      if( color<NUM_TEXTURES )
379
        {
380
        numNonBlack++;
381
        nonBlackIndex = color%NUM_FACE_COLORS;
382
        }
383
      }
384

    
385
    if( numNonBlack==0 ) return -1;
386
    if( numNonBlack>=2 ) return -2;
387

    
388
    return nonBlackIndex;
389
    }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

    
393
  int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
394
    {
395
    final float MAXD = 0.0001f;
396
    float x = faceAx.get0();
397
    float y = faceAx.get1();
398
    float z = faceAx.get2();
399
    float a,dx,dy,dz,qx,qy,qz;
400
    Static4D quat;
401

    
402
    int len = quats.length;
403
    int place = 0;
404

    
405
    for(int q=1; q<len; q++)
406
      {
407
      quat = quats[q];
408
      qx = quat.get0();
409
      qy = quat.get1();
410
      qz = quat.get2();
411

    
412
           if( x!=0.0f ) { a = qx/x; }
413
      else if( y!=0.0f ) { a = qy/y; }
414
      else               { a = qz/z; }
415

    
416
      dx = a*x-qx;
417
      dy = a*y-qy;
418
      dz = a*z-qz;
419

    
420
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
421
        {
422
        mTmpQuats[place++] = q;
423
        }
424
      }
425

    
426
    if( place!=0 )
427
      {
428
      int[] ret = new int[place];
429
      System.arraycopy(mTmpQuats,0,ret,0,place);
430
      return ret;
431
      }
432

    
433
    return null;
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437

    
438
  private int getMultQuat(int index1, int index2)
439
    {
440
    if( mQuatMult==null )
441
      {
442
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
443

    
444
      for(int i=0; i<NUM_QUATS; i++)
445
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
446
      }
447

    
448
    if( mQuatMult[index1][index2]==-1 )
449
      {
450
      mQuatMult[index1][index2] = mulQuat(index1,index2);
451
      }
452

    
453
    return mQuatMult[index1][index2];
454
    }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
  public boolean isSolved()
459
    {
460
    if( mSolvedFunctionIndex==0 ) return isSolved0();
461
    if( mSolvedFunctionIndex==1 ) return isSolved1();
462
    if( mSolvedFunctionIndex==2 ) return isSolved2();
463
    if( mSolvedFunctionIndex==3 ) return isSolved3();
464

    
465
    return false;
466
    }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
  public boolean isSolved0()
471
    {
472
    int len, q1,q = CUBITS[0].mQuatIndex;
473
    int[] solved;
474
    boolean skip;
475

    
476
    for(int c=1; c<NUM_CUBITS; c++)
477
      {
478
      q1 = CUBITS[c].mQuatIndex;
479

    
480
      if( q1==q ) continue;
481

    
482
      skip = false;
483
      solved = mSolvedQuats[c];
484
      len = solved==null ? 0:solved.length;
485

    
486
      for(int i=0; i<len; i++)
487
        {
488
        if( q1==getMultQuat(q,solved[i]) )
489
          {
490
          skip = true;
491
          break;
492
          }
493
        }
494

    
495
      if( !skip ) return false;
496
      }
497

    
498
    return true;
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

    
503
  private int computeScramble(int quatNum, int centerNum)
504
    {
505
    float MAXDIFF = 0.01f;
506
    float[] center= mOrigPos[centerNum];
507
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
508
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
509

    
510
    float x = result.get0();
511
    float y = result.get1();
512
    float z = result.get2();
513

    
514
    for(int c=0; c<NUM_CUBITS; c++)
515
      {
516
      float[] cent = mOrigPos[c];
517

    
518
      float qx = cent[0] - x;
519
      float qy = cent[1] - y;
520
      float qz = cent[2] - z;
521

    
522
      if( qx>-MAXDIFF && qx<MAXDIFF &&
523
          qy>-MAXDIFF && qy<MAXDIFF &&
524
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
525
      }
526

    
527
    return -1;
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531
// Dino4 uses this. It is solved if and only if groups of cubits
532
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
533
// or
534
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
535
// are all the same color.
536

    
537
  public boolean isSolved1()
538
    {
539
    if( mScramble==null )
540
      {
541
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
542
      mColors   = new int[NUM_CUBITS];
543

    
544
      for(int q=0; q<NUM_QUATS; q++)
545
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
546
      }
547

    
548
    if( mFaceMap==null )
549
      {
550
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
551
      }
552

    
553
    for(int c=0; c<NUM_CUBITS; c++)
554
      {
555
      int index = mScramble[CUBITS[c].mQuatIndex][c];
556
      mColors[index] = mFaceMap[c];
557
      }
558

    
559
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
560
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
561
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
562

    
563
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
564
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
565
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
566

    
567
    return false;
568
    }
569

    
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571
// Dino6 uses this. It is solved if and only if:
572
//
573
// All four 'X' cubits (i.e. those whose longest edge goes along the X axis) are rotated
574
// by the same quaternion qX, similarly all four 'Y' cubits by the same qY and all four 'Z'
575
// by the same qZ, and then either:
576
//
577
// a) qX = qY = qZ
578
// b) qY = qX*Q2 and qZ = qX*Q8  (i.e. swap of WHITE and YELLOW faces)
579
// c) qX = qY*Q2 and qZ = qY*Q10 (i.e. swap of BLUE and GREEN faces)
580
// d) qX = qZ*Q8 and qY = qZ*Q10 (i.e. swap of RED and BROWN faces)
581
//
582
// BUT: cases b), c) and d) are really the same - it's all just a mirror image of the original.
583
//
584
// X cubits: 0, 2, 8, 10
585
// Y cubits: 1, 3, 9, 11
586
// Z cubits: 4, 5, 6, 7
587

    
588
  public boolean isSolved2()
589
    {
590
    int qX = CUBITS[0].mQuatIndex;
591
    int qY = CUBITS[1].mQuatIndex;
592
    int qZ = CUBITS[4].mQuatIndex;
593

    
594
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
595
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
596
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
597
      {
598
      return false;
599
      }
600

    
601
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
602
    }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605
// Square-2 is solved iff
606
// a) all of its cubits are rotated with the same quat
607
// b) its two 'middle' cubits are rotated with the same quat, the 6 'front' and 6 'back'
608
// edges and corners with this quat multiplied by QUATS[18] (i.e. those are upside down)
609
// and all the 12 left and right edges and corners also with the same quat multiplied by
610
// QUATS[12] - i.e. also upside down.
611

    
612
  public boolean isSolved3()
613
    {
614
    int index = CUBITS[0].mQuatIndex;
615

    
616
    if( CUBITS[1].mQuatIndex!=index ) return false;
617

    
618
    boolean solved = true;
619

    
620
    for(int i=2; i<NUM_CUBITS; i++)
621
      {
622
      if( CUBITS[i].mQuatIndex!=index )
623
        {
624
        solved = false;
625
        break;
626
        }
627
      }
628

    
629
    if( solved ) return true;
630

    
631
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
632
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
633

    
634
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
635
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
636
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
637
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
638

    
639
    return true;
640
    }
641

    
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643

    
644
  public void setObjectRatio(float sizeChange)
645
    {
646
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
647

    
648
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
649
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
650

    
651
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
652
    mObjectScale.set(scale,scale,scale);
653
    }
654

    
655
///////////////////////////////////////////////////////////////////////////////////////////////////
656

    
657
  public float getObjectRatio()
658
    {
659
    return mObjectScreenRatio*mInitScreenRatio;
660
    }
661

    
662
///////////////////////////////////////////////////////////////////////////////////////////////////
663

    
664
  int computeRow(float[] pos, int axisIndex)
665
    {
666
    int ret=0;
667
    int len = pos.length / 3;
668
    Static3D axis = mAxis[axisIndex];
669
    float axisX = axis.get0();
670
    float axisY = axis.get1();
671
    float axisZ = axis.get2();
672
    float casted;
673

    
674
    for(int i=0; i<len; i++)
675
      {
676
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
677
      ret |= computeSingleRow(axisIndex,casted);
678
      }
679

    
680
    return ret;
681
    }
682

    
683
///////////////////////////////////////////////////////////////////////////////////////////////////
684

    
685
  private int computeSingleRow(int axisIndex,float casted)
686
    {
687
    int num = mNumCuts[axisIndex];
688

    
689
    for(int i=0; i<num; i++)
690
      {
691
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
692
      }
693

    
694
    return (1<<num);
695
    }
696

    
697
///////////////////////////////////////////////////////////////////////////////////////////////////
698

    
699
  private boolean wasRotateApplied()
700
    {
701
    return mEffects.exists(mRotateEffect.getID());
702
    }
703

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705

    
706
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
707
    {
708
    return (CUBITS[cubit].mRotationRow[axis] & rowBitmap) != 0;
709
    }
710

    
711
///////////////////////////////////////////////////////////////////////////////////////////////////
712
// note the minus in front of the sin() - we rotate counterclockwise
713
// when looking towards the direction where the axis increases in values.
714

    
715
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
716
    {
717
    Static3D axis = mAxis[axisIndex];
718

    
719
    while( angleInDegrees<0 ) angleInDegrees += 360;
720
    angleInDegrees %= 360;
721
    
722
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
723
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
724

    
725
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
726
    }
727

    
728
///////////////////////////////////////////////////////////////////////////////////////////////////
729

    
730
  private synchronized void setupPosition(int[][] moves)
731
    {
732
    if( moves!=null )
733
      {
734
      Static4D quat;
735
      int index, axis, rowBitmap, angle;
736
      int[] basic = getBasicAngle();
737

    
738
      for(int[] move: moves)
739
        {
740
        axis     = move[0];
741
        rowBitmap= move[1];
742
        angle    = move[2]*(360/basic[axis]);
743
        quat     = makeQuaternion(axis,angle);
744

    
745
        for(int j=0; j<NUM_CUBITS; j++)
746
          if( belongsToRotation(j,axis,rowBitmap) )
747
            {
748
            index = CUBITS[j].removeRotationNow(quat);
749
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
750
            }
751
        }
752
      }
753
    }
754

    
755
///////////////////////////////////////////////////////////////////////////////////////////////////
756

    
757
  int getScrambleType()
758
    {
759
    return 0;
760
    }
761

    
762
///////////////////////////////////////////////////////////////////////////////////////////////////
763

    
764
  int computeBitmapFromRow(int rowBitmap, int axis)
765
    {
766
    if( mIsBandaged )
767
      {
768
      int bitmap, initBitmap=0;
769

    
770
      while( initBitmap!=rowBitmap )
771
        {
772
        initBitmap = rowBitmap;
773

    
774
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
775
          {
776
          bitmap = CUBITS[cubit].mRotationRow[axis];
777
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
778
          }
779
        }
780
      }
781

    
782
    return rowBitmap;
783
    }
784

    
785
///////////////////////////////////////////////////////////////////////////////////////////////////
786
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
787
// Do so only if minimal Error is appropriately low (shape-shifting puzzles - Square-1)
788

    
789
  void clampPos(float[] pos, int offset)
790
    {
791
    float currError, minError = Float.MAX_VALUE;
792
    int minErrorIndex1 = -1;
793
    int minErrorIndex2 = -1;
794

    
795
    float x = pos[offset  ];
796
    float y = pos[offset+1];
797
    float z = pos[offset+2];
798

    
799
    float xo,yo,zo;
800

    
801
    for(int i=0; i<NUM_CUBITS; i++)
802
      {
803
      int len = mOrigPos[i].length / 3;
804

    
805
      for(int j=0; j<len; j++)
806
        {
807
        xo = mOrigPos[i][3*j  ];
808
        yo = mOrigPos[i][3*j+1];
809
        zo = mOrigPos[i][3*j+2];
810

    
811
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
812

    
813
        if( currError<minError )
814
          {
815
          minError = currError;
816
          minErrorIndex1 = i;
817
          minErrorIndex2 = j;
818
          }
819
        }
820
      }
821

    
822
    if( minError< 0.1f ) // TODO: 0.1 ?
823
      {
824
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
825
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
826
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
827
      }
828
    }
829

    
830
///////////////////////////////////////////////////////////////////////////////////////////////////
831
// remember about the double cover or unit quaternions!
832

    
833
  int mulQuat(int q1, int q2)
834
    {
835
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
836

    
837
    float rX = result.get0();
838
    float rY = result.get1();
839
    float rZ = result.get2();
840
    float rW = result.get3();
841

    
842
    final float MAX_ERROR = 0.1f;
843
    float dX,dY,dZ,dW;
844

    
845
    for(int i=0; i<NUM_QUATS; i++)
846
      {
847
      dX = OBJECT_QUATS[i].get0() - rX;
848
      dY = OBJECT_QUATS[i].get1() - rY;
849
      dZ = OBJECT_QUATS[i].get2() - rZ;
850
      dW = OBJECT_QUATS[i].get3() - rW;
851

    
852
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
853
          dY<MAX_ERROR && dY>-MAX_ERROR &&
854
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
855
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
856

    
857
      dX = OBJECT_QUATS[i].get0() + rX;
858
      dY = OBJECT_QUATS[i].get1() + rY;
859
      dZ = OBJECT_QUATS[i].get2() + rZ;
860
      dW = OBJECT_QUATS[i].get3() + rW;
861

    
862
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
863
          dY<MAX_ERROR && dY>-MAX_ERROR &&
864
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
865
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
866
      }
867

    
868
    return -1;
869
    }
870

    
871
///////////////////////////////////////////////////////////////////////////////////////////////////
872

    
873
  public int getCubitFaceColorIndex(int cubit, int face)
874
    {
875
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
876

    
877
    int x = (int)(texMap.get0()/texMap.get2());
878
    int y = (int)(texMap.get1()/texMap.get3());
879

    
880
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
881
    }
882

    
883
///////////////////////////////////////////////////////////////////////////////////////////////////
884
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
885

    
886
  public void createTexture()
887
    {
888
    Bitmap bitmap;
889

    
890
    Paint paint = new Paint();
891
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
892
    Canvas canvas = new Canvas(bitmap);
893

    
894
    paint.setAntiAlias(true);
895
    paint.setTextAlign(Paint.Align.CENTER);
896
    paint.setStyle(Paint.Style.FILL);
897

    
898
    paint.setColor(COLOR_BLACK);
899
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
900

    
901
    int face = 0;
902
    FactorySticker factory = FactorySticker.getInstance();
903

    
904
    for(int row=0; row<mNumTexRows; row++)
905
      for(int col=0; col<mNumTexCols; col++)
906
        {
907
        if( face>=NUM_TEXTURES ) break;
908
        ObjectSticker sticker = retSticker(face);
909
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, getColor(face%NUM_FACE_COLORS), sticker);
910
        face++;
911
        }
912

    
913
    if( !mTexture.setTexture(bitmap) )
914
      {
915
      int max = DistortedLibrary.getMaxTextureSize();
916
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
917
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
918
      }
919
    }
920

    
921
///////////////////////////////////////////////////////////////////////////////////////////////////
922

    
923
  public int getNumLayers()
924
    {
925
    return mNumLayers;
926
    }
927

    
928
///////////////////////////////////////////////////////////////////////////////////////////////////
929

    
930
  public void continueRotation(float angleInDegrees)
931
    {
932
    mRotationAngleStatic.set0(angleInDegrees);
933
    }
934

    
935
///////////////////////////////////////////////////////////////////////////////////////////////////
936

    
937
  public Static4D getRotationQuat()
938
      {
939
      return mQuat;
940
      }
941

    
942
///////////////////////////////////////////////////////////////////////////////////////////////////
943

    
944
  public void recomputeScaleFactor(int scrWidth)
945
    {
946
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
947
    }
948

    
949
///////////////////////////////////////////////////////////////////////////////////////////////////
950

    
951
  public void savePreferences(SharedPreferences.Editor editor)
952
    {
953
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
954
    }
955

    
956
///////////////////////////////////////////////////////////////////////////////////////////////////
957

    
958
  public synchronized void restorePreferences(SharedPreferences preferences)
959
    {
960
    boolean error = false;
961

    
962
    for(int i=0; i<NUM_CUBITS; i++)
963
      {
964
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
965

    
966
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
967
        {
968
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
969
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
970
        }
971
      else
972
        {
973
        error = true;
974
        }
975
      }
976

    
977
    if( error )
978
      {
979
      for(int i=0; i<NUM_CUBITS; i++)
980
        {
981
        CUBITS[i].solve();
982
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
983
        }
984
      recordQuatsState("Failed to restorePreferences");
985
      }
986
    }
987

    
988
///////////////////////////////////////////////////////////////////////////////////////////////////
989

    
990
  public void recordQuatsState(String message)
991
    {
992
    StringBuilder quats = new StringBuilder();
993

    
994
    for(int j=0; j<NUM_CUBITS; j++)
995
      {
996
      quats.append(mQuatDebug[j]);
997
      quats.append(" ");
998
      }
999

    
1000
    if( BuildConfig.DEBUG )
1001
      {
1002
      android.util.Log.e("quats" , quats.toString());
1003
      android.util.Log.e("object", mList.name()+"_"+mNumLayers);
1004
      }
1005
    else
1006
      {
1007
      Exception ex = new Exception(message);
1008
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
1009
      crashlytics.setCustomKey("quats" , quats.toString());
1010
      crashlytics.setCustomKey("object", mList.name()+"_"+mNumLayers );
1011
      crashlytics.recordException(ex);
1012
      }
1013
    }
1014

    
1015
///////////////////////////////////////////////////////////////////////////////////////////////////
1016

    
1017
  public void releaseResources()
1018
    {
1019
    mTexture.markForDeletion();
1020
    mMesh.markForDeletion();
1021
    mEffects.markForDeletion();
1022

    
1023
    for(int j=0; j<NUM_CUBITS; j++)
1024
      {
1025
      CUBITS[j].releaseResources();
1026
      }
1027
    }
1028

    
1029
///////////////////////////////////////////////////////////////////////////////////////////////////
1030

    
1031
  public void apply(Effect effect, int position)
1032
    {
1033
    mEffects.apply(effect, position);
1034
    }
1035

    
1036
///////////////////////////////////////////////////////////////////////////////////////////////////
1037

    
1038
  public void remove(long effectID)
1039
    {
1040
    mEffects.abortById(effectID);
1041
    }
1042

    
1043
///////////////////////////////////////////////////////////////////////////////////////////////////
1044

    
1045
  public synchronized void solve()
1046
    {
1047
    for(int i=0; i<NUM_CUBITS; i++)
1048
      {
1049
      CUBITS[i].solve();
1050
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1051
      }
1052
    }
1053

    
1054
///////////////////////////////////////////////////////////////////////////////////////////////////
1055

    
1056
  public void resetAllTextureMaps()
1057
    {
1058
    final float ratioW = 1.0f/mNumTexCols;
1059
    final float ratioH = 1.0f/mNumTexRows;
1060
    int color, row, col;
1061

    
1062
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1063
      {
1064
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1065

    
1066
      for(int cubitface=0; cubitface<mNumCubitFaces; cubitface++)
1067
        {
1068
        color = getFaceColor(cubit,cubitface,mNumLayers);
1069
        row = (mNumTexRows-1) - color/mNumTexCols;
1070
        col = color%mNumTexCols;
1071
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1072
        }
1073

    
1074
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1075
      }
1076
    }
1077

    
1078
///////////////////////////////////////////////////////////////////////////////////////////////////
1079

    
1080
  public void setTextureMap(int cubit, int face, int newColor)
1081
    {
1082
    final float ratioW = 1.0f/mNumTexCols;
1083
    final float ratioH = 1.0f/mNumTexRows;
1084
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1085
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1086
    int col = newColor%mNumTexCols;
1087

    
1088
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1089
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1090
    }
1091

    
1092
///////////////////////////////////////////////////////////////////////////////////////////////////
1093

    
1094
  public synchronized void beginNewRotation(int axis, int row )
1095
    {
1096
    if( axis<0 || axis>=NUM_AXIS )
1097
      {
1098
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1099
      return;
1100
      }
1101
    if( row<0 || row>=mNumLayers )
1102
      {
1103
      android.util.Log.e("object", "invalid rotation row: "+row);
1104
      return;
1105
      }
1106

    
1107
    mRotAxis     = axis;
1108
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1109
    mRotationAngleStatic.set0(0.0f);
1110
    mRotationAxis.set( mAxis[axis] );
1111
    mRotationAngle.add(mRotationAngleStatic);
1112
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
1113
    }
1114

    
1115
///////////////////////////////////////////////////////////////////////////////////////////////////
1116

    
1117
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
1118
    {
1119
    if( wasRotateApplied() )
1120
      {
1121
      mRotAxis     = axis;
1122
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
1123

    
1124
      mRotationAngleStatic.set0(0.0f);
1125
      mRotationAxis.set( mAxis[axis] );
1126
      mRotationAngle.setDuration(durationMillis);
1127
      mRotationAngle.resetToBeginning();
1128
      mRotationAngle.add(new Static1D(0));
1129
      mRotationAngle.add(new Static1D(angle));
1130
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
1131
      mRotateEffect.notifyWhenFinished(listener);
1132

    
1133
      return mRotateEffect.getID();
1134
      }
1135

    
1136
    return 0;
1137
    }
1138

    
1139
///////////////////////////////////////////////////////////////////////////////////////////////////
1140

    
1141
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
1142
    {
1143
    if( wasRotateApplied() )
1144
      {
1145
      float angle = getAngle();
1146
      mRotationAngleStatic.set0(angle);
1147
      mRotationAngleFinal.set0(nearestAngleInDegrees);
1148
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
1149

    
1150
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1151
      mRotationAngle.resetToBeginning();
1152
      mRotationAngle.removeAll();
1153
      mRotationAngle.add(mRotationAngleStatic);
1154
      mRotationAngle.add(mRotationAngleMiddle);
1155
      mRotationAngle.add(mRotationAngleFinal);
1156
      mRotateEffect.notifyWhenFinished(listener);
1157

    
1158
      return mRotateEffect.getID();
1159
      }
1160

    
1161
    return 0;
1162
    }
1163

    
1164
///////////////////////////////////////////////////////////////////////////////////////////////////
1165

    
1166
  private float getAngle()
1167
    {
1168
    int pointNum = mRotationAngle.getNumPoints();
1169

    
1170
    if( pointNum>=1 )
1171
      {
1172
      return mRotationAngle.getPoint(pointNum-1).get0();
1173
      }
1174
    else
1175
      {
1176
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
1177
      crashlytics.log("points in RotationAngle: "+pointNum);
1178
      return 0;
1179
      }
1180
    }
1181

    
1182
///////////////////////////////////////////////////////////////////////////////////////////////////
1183

    
1184
  public synchronized void removeRotationNow()
1185
    {
1186
    float angle = getAngle();
1187
    double nearestAngleInRadians = angle*Math.PI/180;
1188
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
1189
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
1190
    float axisX = mAxis[mRotAxis].get0();
1191
    float axisY = mAxis[mRotAxis].get1();
1192
    float axisZ = mAxis[mRotAxis].get2();
1193
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
1194

    
1195
    mRotationAngle.removeAll();
1196
    mRotationAngleStatic.set0(0);
1197

    
1198
    for(int i=0; i<NUM_CUBITS; i++)
1199
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
1200
        {
1201
        int index = CUBITS[i].removeRotationNow(quat);
1202
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
1203
        }
1204
    }
1205

    
1206
///////////////////////////////////////////////////////////////////////////////////////////////////
1207

    
1208
  public void initializeObject(int[][] moves)
1209
    {
1210
    solve();
1211
    setupPosition(moves);
1212
    }
1213

    
1214
///////////////////////////////////////////////////////////////////////////////////////////////////
1215

    
1216
  public int getCubit(float[] point3D)
1217
    {
1218
    float dist, minDist = Float.MAX_VALUE;
1219
    int currentBest=-1;
1220
    float multiplier = returnMultiplier();
1221

    
1222
    point3D[0] *= multiplier;
1223
    point3D[1] *= multiplier;
1224
    point3D[2] *= multiplier;
1225

    
1226
    for(int i=0; i<NUM_CUBITS; i++)
1227
      {
1228
      dist = CUBITS[i].getDistSquared(point3D);
1229
      if( dist<minDist )
1230
        {
1231
        minDist = dist;
1232
        currentBest = i;
1233
        }
1234
      }
1235

    
1236
    return currentBest;
1237
    }
1238

    
1239
///////////////////////////////////////////////////////////////////////////////////////////////////
1240

    
1241
  public int computeNearestAngle(int axis, float angle, float speed)
1242
    {
1243
    int[] basicArray = getBasicAngle();
1244
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1245
    int nearestAngle = 360/basicAngle;
1246

    
1247
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1248
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1249

    
1250
    if( tmp!=0 ) return nearestAngle*tmp;
1251

    
1252
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1253
    }
1254

    
1255
///////////////////////////////////////////////////////////////////////////////////////////////////
1256

    
1257
  public float getCameraDist()
1258
    {
1259
    return mCameraDist;
1260
    }
1261

    
1262
///////////////////////////////////////////////////////////////////////////////////////////////////
1263

    
1264
  public int getNodeSize()
1265
    {
1266
    return mNodeSize;
1267
    }
1268

    
1269
///////////////////////////////////////////////////////////////////////////////////////////////////
1270

    
1271
  public ObjectList getObjectList()
1272
    {
1273
    return mList;
1274
    }
1275

    
1276
///////////////////////////////////////////////////////////////////////////////////////////////////
1277

    
1278
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1279
    {
1280
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1281
    }
1282

    
1283
///////////////////////////////////////////////////////////////////////////////////////////////////
1284

    
1285
  abstract float getScreenRatio();
1286
  abstract float[][] getCubitPositions(int numLayers);
1287
  abstract Static4D[] getQuats();
1288
  abstract int getNumFaceColors();
1289
  abstract int getNumStickerTypes(int numLayers);
1290
  abstract int getNumCubitFaces();
1291
  abstract ObjectSticker retSticker(int face);
1292
  abstract int getColor(int face);
1293
  abstract int getFaceColor(int cubit, int cubitface, int numLayers);
1294
  abstract float returnMultiplier();
1295
  abstract float[][] getCuts(int numLayers);
1296
  abstract boolean shouldResetTextureMaps();
1297
  abstract int getCubitVariant(int cubit, int numLayers);
1298
  abstract int getNumCubitVariants(int numLayers);
1299
  abstract Static4D getQuat(int cubit, int numLayers);
1300
  abstract ObjectShape getObjectShape(int cubit, int numLayers);
1301
  abstract int[] getSolvedQuats(int cubit, int numLayers);
1302
  abstract int getSolvedFunctionIndex();
1303
  abstract ScrambleState[] getScrambleStates();
1304

    
1305
  public abstract Static3D[] getRotationAxis();
1306
  public abstract int[] getBasicAngle();
1307
  public abstract int getObjectName(int numLayers);
1308
  public abstract int getInventor(int numLayers);
1309
  public abstract int getComplexity(int numLayers);
1310
  }
(34-34/43)