Project

General

Profile

Download (41 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 0310ac32

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.objectlib.main;
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.library.effect.Effect;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectQuaternion;
35
import org.distorted.library.effect.VertexEffectRotate;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.main.QuatHelper;
41
import org.distorted.library.mesh.MeshBase;
42
import org.distorted.library.mesh.MeshFile;
43
import org.distorted.library.mesh.MeshJoined;
44
import org.distorted.library.mesh.MeshSquare;
45
import org.distorted.library.message.EffectListener;
46
import org.distorted.library.type.Dynamic1D;
47
import org.distorted.library.type.Static1D;
48
import org.distorted.library.type.Static3D;
49
import org.distorted.library.type.Static4D;
50
import org.distorted.objectlib.BuildConfig;
51

    
52
import java.io.DataInputStream;
53
import java.io.IOException;
54
import java.io.InputStream;
55
import java.util.Random;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
public abstract class TwistyObject extends DistortedNode
60
  {
61
  public static final int COLOR_YELLOW = 0xffffff00;
62
  public static final int COLOR_WHITE  = 0xffffffff;
63
  public static final int COLOR_BLUE   = 0xff0000ff;
64
  public static final int COLOR_GREEN  = 0xff00bb00;
65
  public static final int COLOR_RED    = 0xff990000;
66
  public static final int COLOR_ORANGE = 0xffff6200;
67
  public static final int COLOR_GREY   = 0xff727c7b;
68
  public static final int COLOR_VIOLET = 0xff7700bb;
69
  public static final int COLOR_BLACK  = 0xff000000;
70

    
71
  public static final int TEXTURE_HEIGHT = 256;
72
  static final int NUM_STICKERS_IN_ROW = 4;
73

    
74
  public static final float SQ2 = (float)Math.sqrt(2);
75
  public static final float SQ3 = (float)Math.sqrt(3);
76
  public static final float SQ5 = (float)Math.sqrt(5);
77
  public static final float SQ6 = (float)Math.sqrt(6);
78

    
79
  private static final float NODE_RATIO = 1.60f;
80
  private static final float MAX_SIZE_CHANGE = 1.35f;
81
  private static final float MIN_SIZE_CHANGE = 0.75f;
82

    
83
  private static final Static3D CENTER = new Static3D(0,0,0);
84
  private static final int POST_ROTATION_MILLISEC = 500;
85

    
86
  protected final int NUM_FACE_COLORS;
87
  protected final int NUM_TEXTURES;
88
  protected final Cubit[] CUBITS;
89

    
90
  MeshBase[] mMeshes;
91
  final Static4D[] OBJECT_QUATS;
92
  final int NUM_CUBITS;
93
  final int NUM_AXIS;
94
  final int NUM_QUATS;
95

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

    
128
  //////////////////// SOLVED1 ////////////////////////
129

    
130
  private int[] mFaceMap;
131
  private int[][] mScramble;
132
  private int[] mColors;
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  TwistyObject(int numLayers, int realSize, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
137
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
138
    {
139
    super(nodeTexture,nodeEffects,nodeMesh);
140

    
141
    mNodeSize = screenWidth;
142

    
143
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
144

    
145
    mNumLayers = numLayers;
146
    mRealSize = realSize;
147
    mList = list;
148
    mOrigPos = getCubitPositions(mNumLayers);
149
    mAxis = getRotationAxis();
150
    mInitScreenRatio = getScreenRatio();
151
    mObjectScreenRatio = 1.0f;
152
    mNumCubitFaces = getNumCubitFaces();
153
    mSolvedFunctionIndex = getSolvedFunctionIndex();
154

    
155
    mCuts = getCuts(mNumLayers);
156
    mNumCuts = new int[mAxis.length];
157
    if( mCuts==null ) for(int i=0; i<mAxis.length; i++) mNumCuts[i] = 0;
158
    else              for(int i=0; i<mAxis.length; i++) mNumCuts[i] = mCuts[i].length;
159

    
160
    OBJECT_QUATS = getQuats();
161
    NUM_CUBITS  = mOrigPos.length;
162
    NUM_FACE_COLORS = getNumFaceColors();
163
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
164
    NUM_AXIS = mAxis.length;
165
    NUM_QUATS = OBJECT_QUATS.length;
166

    
167
    int scramblingType = getScrambleType();
168
    ScrambleState[] states = getScrambleStates();
169
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,numLayers,states);
170

    
171
    boolean bandaged=false;
172

    
173
    for(int c=0; c<NUM_CUBITS; c++)
174
      {
175
      if( mOrigPos[c].length>3 )
176
        {
177
        bandaged=true;
178
        break;
179
        }
180
      }
181

    
182
    mIsBandaged = bandaged;
183

    
184
    mQuatDebug = new int[NUM_CUBITS];
185

    
186
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
187
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
188

    
189
    mNodeScale= new Static3D(screenWidth,NODE_RATIO*screenWidth,screenWidth);
190
    mQuat = quat;
191

    
192
    mRotationAngle= new Dynamic1D();
193
    mRotationAxis = new Static3D(1,0,0);
194
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
195

    
196
    mRotationAngleStatic = new Static1D(0);
197
    mRotationAngleMiddle = new Static1D(0);
198
    mRotationAngleFinal  = new Static1D(0);
199

    
200
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
201
    mObjectScale = new Static3D(scale,scale,scale);
202
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
203
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(quat, CENTER);
204

    
205
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
206
    nodeEffects.apply(nodeScaleEffect);
207

    
208
    mNumTexCols = NUM_STICKERS_IN_ROW;
209
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
210

    
211
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
212

    
213
    CUBITS = new Cubit[NUM_CUBITS];
214
    createMeshAndCubits(list,res);
215
    createDataStructuresForSolved(numLayers);
216

    
217
    mTexture = new DistortedTexture();
218
    mEffects = new DistortedEffects();
219

    
220
    for(int q=0; q<NUM_QUATS; q++)
221
      {
222
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
223
      vq.setMeshAssociation(0,q);
224
      mEffects.apply(vq);
225
      }
226

    
227
    mEffects.apply(mRotateEffect);
228
    mEffects.apply(quatEffect);
229
    mEffects.apply(scaleEffect);
230

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

    
235
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
236

    
237
    setupPosition(moves);
238

    
239
    float fov = getFOV();
240
    double halfFOV = fov * (Math.PI/360);
241
    mCameraDist = 0.5f*NODE_RATIO / (float)Math.tan(halfFOV);
242

    
243
    setProjection( fov, 0.1f);
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  private Static3D getPos(float[] origPos)
249
    {
250
    int len = origPos.length/3;
251
    float sumX = 0.0f;
252
    float sumY = 0.0f;
253
    float sumZ = 0.0f;
254

    
255
    for(int i=0; i<len; i++)
256
      {
257
      sumX += origPos[3*i  ];
258
      sumY += origPos[3*i+1];
259
      sumZ += origPos[3*i+2];
260
      }
261

    
262
    sumX /= len;
263
    sumY /= len;
264
    sumZ /= len;
265

    
266
    return new Static3D(sumX,sumY,sumZ);
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  private void createMeshAndCubits(ObjectList list, Resources res)
272
    {
273
    int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mNumLayers);
274
    int resourceID= list.getResourceIDs()[sizeIndex];
275

    
276
    if( resourceID!=0 )
277
      {
278
      InputStream is = res.openRawResource(resourceID);
279
      DataInputStream dos = new DataInputStream(is);
280
      mMesh = new MeshFile(dos);
281

    
282
      try
283
        {
284
        is.close();
285
        }
286
      catch(IOException e)
287
        {
288
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
289
        }
290

    
291
      for(int i=0; i<NUM_CUBITS; i++)
292
        {
293
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
294
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
295
        }
296

    
297
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
298
      }
299
    else
300
      {
301
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
302

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

    
312
      mMesh = new MeshJoined(cubitMesh);
313
      resetAllTextureMaps();
314
      }
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  private MeshBase createCubitMesh(int cubit, int numLayers)
320
    {
321
    int variant = getCubitVariant(cubit,numLayers);
322

    
323
    if( mMeshes==null )
324
      {
325
      FactoryCubit factory = FactoryCubit.getInstance();
326
      factory.clear();
327
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
328
      }
329

    
330
    if( mMeshes[variant]==null )
331
      {
332
      ObjectShape shape = getObjectShape(cubit,numLayers);
333
      FactoryCubit factory = FactoryCubit.getInstance();
334
      factory.createNewFaceTransform(shape);
335
      mMeshes[variant] = factory.createRoundedSolid(shape);
336
      }
337

    
338
    MeshBase mesh = mMeshes[variant].copy(true);
339
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
340
    mesh.apply(quat,0xffffffff,0);
341

    
342
    return mesh;
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  private void createDataStructuresForSolved(int numLayers)
348
    {
349
    mTmpQuats = new int[NUM_QUATS];
350
    mSolvedQuats = new int[NUM_CUBITS][];
351

    
352
    for(int c=0; c<NUM_CUBITS; c++)
353
      {
354
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
355
      }
356
    }
357

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

    
366
  public int retCubitSolvedStatus(int cubit, int numLayers)
367
    {
368
    int numNonBlack=0, nonBlackIndex=-1, color;
369

    
370
    for(int face=0; face<mNumCubitFaces; face++)
371
      {
372
      color = getFaceColor(cubit,face,numLayers);
373

    
374
      if( color<NUM_TEXTURES )
375
        {
376
        numNonBlack++;
377
        nonBlackIndex = color%NUM_FACE_COLORS;
378
        }
379
      }
380

    
381
    if( numNonBlack==0 ) return -1;
382
    if( numNonBlack>=2 ) return -2;
383

    
384
    return nonBlackIndex;
385
    }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
  public boolean shouldResetTextureMaps()
390
    {
391
    return false;
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

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

    
405
    int len = quats.length;
406
    int place = 0;
407

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

    
415
           if( x!=0.0f ) { a = qx/x; }
416
      else if( y!=0.0f ) { a = qy/y; }
417
      else               { a = qz/z; }
418

    
419
      dx = a*x-qx;
420
      dy = a*y-qy;
421
      dz = a*z-qz;
422

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

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

    
436
    return null;
437
    }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
  private int getMultQuat(int index1, int index2)
442
    {
443
    if( mQuatMult==null )
444
      {
445
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
446

    
447
      for(int i=0; i<NUM_QUATS; i++)
448
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
449
      }
450

    
451
    if( mQuatMult[index1][index2]==-1 )
452
      {
453
      mQuatMult[index1][index2] = mulQuat(index1,index2);
454
      }
455

    
456
    return mQuatMult[index1][index2];
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
  public boolean isSolved()
462
    {
463
    if( mSolvedFunctionIndex==0 ) return isSolved0();
464
    if( mSolvedFunctionIndex==1 ) return isSolved1();
465
    if( mSolvedFunctionIndex==2 ) return isSolved2();
466
    if( mSolvedFunctionIndex==3 ) return isSolved3();
467

    
468
    return false;
469
    }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472

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

    
479
    for(int c=1; c<NUM_CUBITS; c++)
480
      {
481
      q1 = CUBITS[c].mQuatIndex;
482

    
483
      if( q1==q ) continue;
484

    
485
      skip = false;
486
      solved = mSolvedQuats[c];
487
      len = solved==null ? 0:solved.length;
488

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

    
498
      if( !skip ) return false;
499
      }
500

    
501
    return true;
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

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

    
513
    float x = result.get0();
514
    float y = result.get1();
515
    float z = result.get2();
516

    
517
    for(int c=0; c<NUM_CUBITS; c++)
518
      {
519
      float[] cent = mOrigPos[c];
520

    
521
      float qx = cent[0] - x;
522
      float qy = cent[1] - y;
523
      float qz = cent[2] - z;
524

    
525
      if( qx>-MAXDIFF && qx<MAXDIFF &&
526
          qy>-MAXDIFF && qy<MAXDIFF &&
527
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
528
      }
529

    
530
    return -1;
531
    }
532

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

    
540
  public boolean isSolved1()
541
    {
542
    if( mScramble==null )
543
      {
544
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
545
      mColors   = new int[NUM_CUBITS];
546

    
547
      for(int q=0; q<NUM_QUATS; q++)
548
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
549
      }
550

    
551
    if( mFaceMap==null )
552
      {
553
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
554
      }
555

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

    
562
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
563
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
564
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
565

    
566
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
567
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
568
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
569

    
570
    return false;
571
    }
572

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

    
591
  public boolean isSolved2()
592
    {
593
    int qX = CUBITS[0].mQuatIndex;
594
    int qY = CUBITS[1].mQuatIndex;
595
    int qZ = CUBITS[4].mQuatIndex;
596

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

    
604
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
605
    }
606

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

    
615
  public boolean isSolved3()
616
    {
617
    int index = CUBITS[0].mQuatIndex;
618

    
619
    if( CUBITS[1].mQuatIndex!=index ) return false;
620

    
621
    boolean solved = true;
622

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

    
632
    if( solved ) return true;
633

    
634
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
635
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
636

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

    
642
    return true;
643
    }
644

    
645
///////////////////////////////////////////////////////////////////////////////////////////////////
646

    
647
  public void setObjectRatio(float sizeChange)
648
    {
649
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
650

    
651
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
652
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
653

    
654
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
655
    mObjectScale.set(scale,scale,scale);
656
    }
657

    
658
///////////////////////////////////////////////////////////////////////////////////////////////////
659

    
660
  public float getObjectRatio()
661
    {
662
    return mObjectScreenRatio*mInitScreenRatio;
663
    }
664

    
665
///////////////////////////////////////////////////////////////////////////////////////////////////
666

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

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

    
683
    return ret;
684
    }
685

    
686
///////////////////////////////////////////////////////////////////////////////////////////////////
687

    
688
  private int computeSingleRow(int axisIndex,float casted)
689
    {
690
    int num = mNumCuts[axisIndex];
691

    
692
    for(int i=0; i<num; i++)
693
      {
694
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
695
      }
696

    
697
    return (1<<num);
698
    }
699

    
700
///////////////////////////////////////////////////////////////////////////////////////////////////
701

    
702
  private boolean wasRotateApplied()
703
    {
704
    return mEffects.exists(mRotateEffect.getID());
705
    }
706

    
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708

    
709
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
710
    {
711
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
712
    }
713

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

    
718
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
719
    {
720
    Static3D axis = mAxis[axisIndex];
721

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

    
728
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
729
    }
730

    
731
///////////////////////////////////////////////////////////////////////////////////////////////////
732

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

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

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

    
758
///////////////////////////////////////////////////////////////////////////////////////////////////
759

    
760
  protected int getScrambleType()
761
    {
762
    return 0;
763
    }
764

    
765
///////////////////////////////////////////////////////////////////////////////////////////////////
766

    
767
  int computeBitmapFromRow(int rowBitmap, int axis)
768
    {
769
    if( mIsBandaged )
770
      {
771
      int bitmap, initBitmap=0;
772

    
773
      while( initBitmap!=rowBitmap )
774
        {
775
        initBitmap = rowBitmap;
776

    
777
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
778
          {
779
          bitmap = CUBITS[cubit].getRotRow(axis);
780
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
781
          }
782
        }
783
      }
784

    
785
    return rowBitmap;
786
    }
787

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

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

    
798
    float x = pos[offset  ];
799
    float y = pos[offset+1];
800
    float z = pos[offset+2];
801

    
802
    float xo,yo,zo;
803

    
804
    for(int i=0; i<NUM_CUBITS; i++)
805
      {
806
      int len = mOrigPos[i].length / 3;
807

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

    
814
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
815

    
816
        if( currError<minError )
817
          {
818
          minError = currError;
819
          minErrorIndex1 = i;
820
          minErrorIndex2 = j;
821
          }
822
        }
823
      }
824

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

    
833
///////////////////////////////////////////////////////////////////////////////////////////////////
834
// remember about the double cover or unit quaternions!
835

    
836
  int mulQuat(int q1, int q2)
837
    {
838
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
839

    
840
    float rX = result.get0();
841
    float rY = result.get1();
842
    float rZ = result.get2();
843
    float rW = result.get3();
844

    
845
    final float MAX_ERROR = 0.1f;
846
    float dX,dY,dZ,dW;
847

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

    
855
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
856
          dY<MAX_ERROR && dY>-MAX_ERROR &&
857
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
858
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
859

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

    
865
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
866
          dY<MAX_ERROR && dY>-MAX_ERROR &&
867
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
868
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
869
      }
870

    
871
    return -1;
872
    }
873

    
874
///////////////////////////////////////////////////////////////////////////////////////////////////
875

    
876
  public int getCubitFaceColorIndex(int cubit, int face)
877
    {
878
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
879

    
880
    int x = (int)(texMap.get0()/texMap.get2());
881
    int y = (int)(texMap.get1()/texMap.get3());
882

    
883
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
884
    }
885

    
886
///////////////////////////////////////////////////////////////////////////////////////////////////
887
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
888

    
889
  public void createTexture()
890
    {
891
    Bitmap bitmap;
892

    
893
    Paint paint = new Paint();
894
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
895
    Canvas canvas = new Canvas(bitmap);
896

    
897
    paint.setAntiAlias(true);
898
    paint.setTextAlign(Paint.Align.CENTER);
899
    paint.setStyle(Paint.Style.FILL);
900

    
901
    paint.setColor(COLOR_BLACK);
902
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
903

    
904
    int face = 0;
905
    FactorySticker factory = FactorySticker.getInstance();
906

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

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

    
924
///////////////////////////////////////////////////////////////////////////////////////////////////
925

    
926
  public int getNumLayers()
927
    {
928
    return mNumLayers;
929
    }
930

    
931
///////////////////////////////////////////////////////////////////////////////////////////////////
932

    
933
  public void continueRotation(float angleInDegrees)
934
    {
935
    mRotationAngleStatic.set0(angleInDegrees);
936
    }
937

    
938
///////////////////////////////////////////////////////////////////////////////////////////////////
939

    
940
  public Static4D getRotationQuat()
941
      {
942
      return mQuat;
943
      }
944

    
945
///////////////////////////////////////////////////////////////////////////////////////////////////
946

    
947
  public void recomputeScaleFactor(int scrWidth)
948
    {
949
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
950
    }
951

    
952
///////////////////////////////////////////////////////////////////////////////////////////////////
953

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

    
959
///////////////////////////////////////////////////////////////////////////////////////////////////
960

    
961
  public synchronized void restorePreferences(SharedPreferences preferences)
962
    {
963
    boolean error = false;
964

    
965
    for(int i=0; i<NUM_CUBITS; i++)
966
      {
967
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
968

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

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

    
991
///////////////////////////////////////////////////////////////////////////////////////////////////
992

    
993
  public void recordQuatsState(String message)
994
    {
995
    StringBuilder quats = new StringBuilder();
996

    
997
    for(int j=0; j<NUM_CUBITS; j++)
998
      {
999
      quats.append(mQuatDebug[j]);
1000
      quats.append(" ");
1001
      }
1002

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

    
1018
///////////////////////////////////////////////////////////////////////////////////////////////////
1019

    
1020
  public void releaseResources()
1021
    {
1022
    mTexture.markForDeletion();
1023
    mMesh.markForDeletion();
1024
    mEffects.markForDeletion();
1025

    
1026
    for(int j=0; j<NUM_CUBITS; j++)
1027
      {
1028
      CUBITS[j].releaseResources();
1029
      }
1030
    }
1031

    
1032
///////////////////////////////////////////////////////////////////////////////////////////////////
1033

    
1034
  public void apply(Effect effect, int position)
1035
    {
1036
    mEffects.apply(effect, position);
1037
    }
1038

    
1039
///////////////////////////////////////////////////////////////////////////////////////////////////
1040

    
1041
  public void remove(long effectID)
1042
    {
1043
    mEffects.abortById(effectID);
1044
    }
1045

    
1046
///////////////////////////////////////////////////////////////////////////////////////////////////
1047

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

    
1057
///////////////////////////////////////////////////////////////////////////////////////////////////
1058

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

    
1065
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1066
      {
1067
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1068

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

    
1077
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1078
      }
1079
    }
1080

    
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082

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

    
1091
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1092
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1093
    }
1094

    
1095
///////////////////////////////////////////////////////////////////////////////////////////////////
1096

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

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

    
1118
///////////////////////////////////////////////////////////////////////////////////////////////////
1119

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

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

    
1136
      return mRotateEffect.getID();
1137
      }
1138

    
1139
    return 0;
1140
    }
1141

    
1142
///////////////////////////////////////////////////////////////////////////////////////////////////
1143

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

    
1153
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1154
      mRotationAngle.resetToBeginning();
1155
      mRotationAngle.removeAll();
1156
      mRotationAngle.add(mRotationAngleStatic);
1157
      mRotationAngle.add(mRotationAngleMiddle);
1158
      mRotationAngle.add(mRotationAngleFinal);
1159
      mRotateEffect.notifyWhenFinished(listener);
1160

    
1161
      return mRotateEffect.getID();
1162
      }
1163

    
1164
    return 0;
1165
    }
1166

    
1167
///////////////////////////////////////////////////////////////////////////////////////////////////
1168

    
1169
  private float getAngle()
1170
    {
1171
    int pointNum = mRotationAngle.getNumPoints();
1172

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

    
1185
///////////////////////////////////////////////////////////////////////////////////////////////////
1186

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

    
1198
    mRotationAngle.removeAll();
1199
    mRotationAngleStatic.set0(0);
1200

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

    
1209
///////////////////////////////////////////////////////////////////////////////////////////////////
1210

    
1211
  public void initializeObject(int[][] moves)
1212
    {
1213
    solve();
1214
    setupPosition(moves);
1215
    }
1216

    
1217
///////////////////////////////////////////////////////////////////////////////////////////////////
1218

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

    
1225
    point3D[0] *= multiplier;
1226
    point3D[1] *= multiplier;
1227
    point3D[2] *= multiplier;
1228

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

    
1239
    return currentBest;
1240
    }
1241

    
1242
///////////////////////////////////////////////////////////////////////////////////////////////////
1243

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

    
1250
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1251
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1252

    
1253
    if( tmp!=0 ) return nearestAngle*tmp;
1254

    
1255
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1256
    }
1257

    
1258
///////////////////////////////////////////////////////////////////////////////////////////////////
1259

    
1260
  public float getCameraDist()
1261
    {
1262
    return mCameraDist;
1263
    }
1264

    
1265
///////////////////////////////////////////////////////////////////////////////////////////////////
1266

    
1267
  public int getNodeSize()
1268
    {
1269
    return mNodeSize;
1270
    }
1271

    
1272
///////////////////////////////////////////////////////////////////////////////////////////////////
1273

    
1274
  public ObjectList getObjectList()
1275
    {
1276
    return mList;
1277
    }
1278

    
1279
///////////////////////////////////////////////////////////////////////////////////////////////////
1280

    
1281
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1282
    {
1283
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1284
    }
1285

    
1286
///////////////////////////////////////////////////////////////////////////////////////////////////
1287

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

    
1308
  public abstract Movement getMovement();
1309
  public abstract Static3D[] getRotationAxis();
1310
  public abstract int[] getBasicAngle();
1311
  public abstract int getObjectName(int numLayers);
1312
  public abstract int getInventor(int numLayers);
1313
  public abstract int getComplexity(int numLayers);
1314
  }
(19-19/20)