Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 255492a0

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

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

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

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

    
83
  private static final float NODE_RATIO = 1.40f;
84
  private static final float MAX_SIZE_CHANGE = 1.35f;
85
  private static final float MIN_SIZE_CHANGE = 0.75f;
86

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

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

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

    
130
  //////////////////// SOLVED1 ////////////////////////
131

    
132
  private int[] mFaceMap;
133
  private int[][] mScramble;
134
  private int[] mColors;
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

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

    
143
    mNodeSize = screenWidth;
144

    
145
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
146

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

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

    
162
    OBJECT_QUATS = getQuats();
163
    NUM_CUBITS  = mOrigPos.length;
164
    NUM_FACES = getNumFaces();
165
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACES;
166
    NUM_AXIS = mAxis.length;
167
    NUM_QUATS = OBJECT_QUATS.length;
168

    
169
    boolean bandaged=false;
170

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

    
180
    mIsBandaged = bandaged;
181

    
182
    mQuatDebug = new int[NUM_CUBITS];
183

    
184
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
185
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
186

    
187
    mNodeScale= new Static3D(1,NODE_RATIO,1);
188
    mQuat = quat;
189

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

    
194
    mRotationAngleStatic = new Static1D(0);
195
    mRotationAngleMiddle = new Static1D(0);
196
    mRotationAngleFinal  = new Static1D(0);
197

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

    
203
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
204
    nodeEffects.apply(nodeScaleEffect);
205

    
206
    mNumTexCols = NUM_STICKERS_IN_ROW;
207
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
208

    
209
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
210

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

    
215
    mTexture = new DistortedTexture();
216
    mEffects = new DistortedEffects();
217

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

    
225
    mEffects.apply(mRotateEffect);
226
    mEffects.apply(quatEffect);
227
    mEffects.apply(scaleEffect);
228

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

    
233
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
234

    
235
    setupPosition(moves);
236

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

    
241
    setProjection( fov, 0.1f);
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

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

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

    
260
    sumX /= len;
261
    sumY /= len;
262
    sumZ /= len;
263

    
264
    return new Static3D(sumX,sumY,sumZ);
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

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

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

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

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

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

    
310
      mMesh = new MeshJoined(cubitMesh);
311
      resetAllTextureMaps();
312
      }
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

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

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

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

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

    
340
    return mesh;
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

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

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

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

    
364
  int retCubitSolvedStatus(int cubit, int numLayers)
365
    {
366
    int numNonBlack=0, nonBlackIndex=-1, color;
367

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

    
372
      if( color<NUM_TEXTURES )
373
        {
374
        numNonBlack++;
375
        nonBlackIndex = color%NUM_FACES;
376
        }
377
      }
378

    
379
    if( numNonBlack==0 ) return -1;
380
    if( numNonBlack>=2 ) return -2;
381

    
382
    return nonBlackIndex;
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
388
    {
389
    final float MAXD = 0.0001f;
390
    float x = faceAx.get0();
391
    float y = faceAx.get1();
392
    float z = faceAx.get2();
393
    float a,dx,dy,dz,qx,qy,qz;
394
    Static4D quat;
395

    
396
    int len = quats.length;
397
    int place = 0;
398

    
399
    for(int q=1; q<len; q++)
400
      {
401
      quat = quats[q];
402
      qx = quat.get0();
403
      qy = quat.get1();
404
      qz = quat.get2();
405

    
406
           if( x!=0.0f ) { a = qx/x; }
407
      else if( y!=0.0f ) { a = qy/y; }
408
      else               { a = qz/z; }
409

    
410
      dx = a*x-qx;
411
      dy = a*y-qy;
412
      dz = a*z-qz;
413

    
414
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
415
        {
416
        mTmpQuats[place++] = q;
417
        }
418
      }
419

    
420
    if( place!=0 )
421
      {
422
      int[] ret = new int[place];
423
      System.arraycopy(mTmpQuats,0,ret,0,place);
424
      return ret;
425
      }
426

    
427
    return null;
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  private int getMultQuat(int index1, int index2)
433
    {
434
    if( mQuatMult==null )
435
      {
436
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
437

    
438
      for(int i=0; i<NUM_QUATS; i++)
439
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
440
      }
441

    
442
    if( mQuatMult[index1][index2]==-1 )
443
      {
444
      mQuatMult[index1][index2] = mulQuat(index1,index2);
445
      }
446

    
447
    return mQuatMult[index1][index2];
448
    }
449

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

    
452
  public boolean isSolved()
453
    {
454
    if( mSolvedFunctionIndex==0 ) return isSolved0();
455
    if( mSolvedFunctionIndex==1 ) return isSolved1();
456
    if( mSolvedFunctionIndex==2 ) return isSolved2();
457
    if( mSolvedFunctionIndex==3 ) return isSolved3();
458

    
459
    return false;
460
    }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
  public boolean isSolved0()
465
    {
466
    int len, q1,q = CUBITS[0].mQuatIndex;
467
    int[] solved;
468
    boolean skip;
469

    
470
    for(int c=1; c<NUM_CUBITS; c++)
471
      {
472
      q1 = CUBITS[c].mQuatIndex;
473

    
474
      if( q1==q ) continue;
475

    
476
      skip = false;
477
      solved = mSolvedQuats[c];
478
      len = solved==null ? 0:solved.length;
479

    
480
      for(int i=0; i<len; i++)
481
        {
482
        if( q1==getMultQuat(q,solved[i]) )
483
          {
484
          skip = true;
485
          break;
486
          }
487
        }
488

    
489
      if( !skip ) return false;
490
      }
491

    
492
    return true;
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

    
497
  private int computeScramble(int quatNum, int centerNum)
498
    {
499
    float MAXDIFF = 0.01f;
500
    float[] center= mOrigPos[centerNum];
501
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
502
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
503

    
504
    float x = result.get0();
505
    float y = result.get1();
506
    float z = result.get2();
507

    
508
    for(int c=0; c<NUM_CUBITS; c++)
509
      {
510
      float[] cent = mOrigPos[c];
511

    
512
      float qx = cent[0] - x;
513
      float qy = cent[1] - y;
514
      float qz = cent[2] - z;
515

    
516
      if( qx>-MAXDIFF && qx<MAXDIFF &&
517
          qy>-MAXDIFF && qy<MAXDIFF &&
518
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
519
      }
520

    
521
    return -1;
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525
// Dino4 uses this. It is solved if and only if groups of cubits
526
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
527
// or
528
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
529
// are all the same color.
530

    
531
  public boolean isSolved1()
532
    {
533
    if( mScramble==null )
534
      {
535
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
536
      mColors   = new int[NUM_CUBITS];
537

    
538
      for(int q=0; q<NUM_QUATS; q++)
539
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
540
      }
541

    
542
    if( mFaceMap==null )
543
      {
544
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
545
      }
546

    
547
    for(int c=0; c<NUM_CUBITS; c++)
548
      {
549
      int index = mScramble[CUBITS[c].mQuatIndex][c];
550
      mColors[index] = mFaceMap[c];
551
      }
552

    
553
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
554
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
555
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
556

    
557
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
558
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
559
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
560

    
561
    return false;
562
    }
563

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

    
582
  public boolean isSolved2()
583
    {
584
    int qX = CUBITS[0].mQuatIndex;
585
    int qY = CUBITS[1].mQuatIndex;
586
    int qZ = CUBITS[4].mQuatIndex;
587

    
588
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
589
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
590
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
591
      {
592
      return false;
593
      }
594

    
595
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
596
    }
597

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

    
606
  public boolean isSolved3()
607
    {
608
    int index = CUBITS[0].mQuatIndex;
609

    
610
    if( CUBITS[1].mQuatIndex!=index ) return false;
611

    
612
    boolean solved = true;
613

    
614
    for(int i=2; i<NUM_CUBITS; i++)
615
      {
616
      if( CUBITS[i].mQuatIndex!=index )
617
        {
618
        solved = false;
619
        break;
620
        }
621
      }
622

    
623
    if( solved ) return true;
624

    
625
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
626
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
627

    
628
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
629
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
630
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
631
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
632

    
633
    return true;
634
    }
635

    
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637

    
638
  public void setObjectRatio(float sizeChange)
639
    {
640
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
641

    
642
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
643
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
644

    
645
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
646
    mObjectScale.set(scale,scale,scale);
647
    }
648

    
649
///////////////////////////////////////////////////////////////////////////////////////////////////
650

    
651
  public float getObjectRatio()
652
    {
653
    return mObjectScreenRatio*mInitScreenRatio;
654
    }
655

    
656
///////////////////////////////////////////////////////////////////////////////////////////////////
657

    
658
  int computeRow(float[] pos, int axisIndex)
659
    {
660
    int ret=0;
661
    int len = pos.length / 3;
662
    Static3D axis = mAxis[axisIndex];
663
    float axisX = axis.get0();
664
    float axisY = axis.get1();
665
    float axisZ = axis.get2();
666
    float casted;
667

    
668
    for(int i=0; i<len; i++)
669
      {
670
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
671
      ret |= computeSingleRow(axisIndex,casted);
672
      }
673

    
674
    return ret;
675
    }
676

    
677
///////////////////////////////////////////////////////////////////////////////////////////////////
678

    
679
  private int computeSingleRow(int axisIndex,float casted)
680
    {
681
    int num = mNumCuts[axisIndex];
682

    
683
    for(int i=0; i<num; i++)
684
      {
685
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
686
      }
687

    
688
    return (1<<num);
689
    }
690

    
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692

    
693
  private boolean wasRotateApplied()
694
    {
695
    return mEffects.exists(mRotateEffect.getID());
696
    }
697

    
698
///////////////////////////////////////////////////////////////////////////////////////////////////
699

    
700
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
701
    {
702
    return (CUBITS[cubit].mRotationRow[axis] & rowBitmap) != 0;
703
    }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706
// note the minus in front of the sin() - we rotate counterclockwise
707
// when looking towards the direction where the axis increases in values.
708

    
709
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
710
    {
711
    Static3D axis = mAxis[axisIndex];
712

    
713
    while( angleInDegrees<0 ) angleInDegrees += 360;
714
    angleInDegrees %= 360;
715
    
716
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
717
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
718

    
719
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
720
    }
721

    
722
///////////////////////////////////////////////////////////////////////////////////////////////////
723

    
724
  private synchronized void setupPosition(int[][] moves)
725
    {
726
    if( moves!=null )
727
      {
728
      Static4D quat;
729
      int index, axis, rowBitmap, angle;
730
      int[] basic = getBasicAngle();
731

    
732
      for(int[] move: moves)
733
        {
734
        axis     = move[0];
735
        rowBitmap= move[1];
736
        angle    = move[2]*(360/basic[axis]);
737
        quat     = makeQuaternion(axis,angle);
738

    
739
        for(int j=0; j<NUM_CUBITS; j++)
740
          if( belongsToRotation(j,axis,rowBitmap) )
741
            {
742
            index = CUBITS[j].removeRotationNow(quat);
743
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
744
            }
745
        }
746
      }
747
    }
748

    
749
///////////////////////////////////////////////////////////////////////////////////////////////////
750

    
751
  int computeBitmapFromRow(int rowBitmap, int axis)
752
    {
753
    if( mIsBandaged )
754
      {
755
      int bitmap, initBitmap=0;
756

    
757
      while( initBitmap!=rowBitmap )
758
        {
759
        initBitmap = rowBitmap;
760

    
761
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
762
          {
763
          bitmap = CUBITS[cubit].mRotationRow[axis];
764
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
765
          }
766
        }
767
      }
768

    
769
    return rowBitmap;
770
    }
771

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

    
776
  void clampPos(float[] pos, int offset)
777
    {
778
    float currError, minError = Float.MAX_VALUE;
779
    int minErrorIndex1 = -1;
780
    int minErrorIndex2 = -1;
781

    
782
    float x = pos[offset  ];
783
    float y = pos[offset+1];
784
    float z = pos[offset+2];
785

    
786
    float xo,yo,zo;
787

    
788
    for(int i=0; i<NUM_CUBITS; i++)
789
      {
790
      int len = mOrigPos[i].length / 3;
791

    
792
      for(int j=0; j<len; j++)
793
        {
794
        xo = mOrigPos[i][3*j  ];
795
        yo = mOrigPos[i][3*j+1];
796
        zo = mOrigPos[i][3*j+2];
797

    
798
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
799

    
800
        if( currError<minError )
801
          {
802
          minError = currError;
803
          minErrorIndex1 = i;
804
          minErrorIndex2 = j;
805
          }
806
        }
807
      }
808

    
809
    if( minError< 0.1f ) // TODO: 0.1 ?
810
      {
811
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
812
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
813
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
814
      }
815
    }
816

    
817
///////////////////////////////////////////////////////////////////////////////////////////////////
818
// remember about the double cover or unit quaternions!
819

    
820
  int mulQuat(int q1, int q2)
821
    {
822
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
823

    
824
    float rX = result.get0();
825
    float rY = result.get1();
826
    float rZ = result.get2();
827
    float rW = result.get3();
828

    
829
    final float MAX_ERROR = 0.1f;
830
    float dX,dY,dZ,dW;
831

    
832
    for(int i=0; i<NUM_QUATS; i++)
833
      {
834
      dX = OBJECT_QUATS[i].get0() - rX;
835
      dY = OBJECT_QUATS[i].get1() - rY;
836
      dZ = OBJECT_QUATS[i].get2() - rZ;
837
      dW = OBJECT_QUATS[i].get3() - rW;
838

    
839
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
840
          dY<MAX_ERROR && dY>-MAX_ERROR &&
841
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
842
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
843

    
844
      dX = OBJECT_QUATS[i].get0() + rX;
845
      dY = OBJECT_QUATS[i].get1() + rY;
846
      dZ = OBJECT_QUATS[i].get2() + rZ;
847
      dW = OBJECT_QUATS[i].get3() + rW;
848

    
849
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
850
          dY<MAX_ERROR && dY>-MAX_ERROR &&
851
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
852
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
853
      }
854

    
855
    return -1;
856
    }
857

    
858
///////////////////////////////////////////////////////////////////////////////////////////////////
859

    
860
  public int getCubitFaceColorIndex(int cubit, int face)
861
    {
862
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
863

    
864
    int x = (int)(texMap.get0()/texMap.get2());
865
    int y = (int)(texMap.get1()/texMap.get3());
866

    
867
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
868
    }
869

    
870
///////////////////////////////////////////////////////////////////////////////////////////////////
871
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
872

    
873
  public void createTexture()
874
    {
875
    Bitmap bitmap;
876

    
877
    Paint paint = new Paint();
878
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
879
    Canvas canvas = new Canvas(bitmap);
880

    
881
    paint.setAntiAlias(true);
882
    paint.setTextAlign(Paint.Align.CENTER);
883
    paint.setStyle(Paint.Style.FILL);
884

    
885
    paint.setColor(COLOR_BLACK);
886
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
887

    
888
    int face = 0;
889
    FactorySticker factory = FactorySticker.getInstance();
890

    
891
    for(int row=0; row<mNumTexRows; row++)
892
      for(int col=0; col<mNumTexCols; col++)
893
        {
894
        if( face>=NUM_TEXTURES ) break;
895
        ObjectSticker sticker = retSticker(face);
896
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, getColor(face%NUM_FACES), sticker);
897
        face++;
898
        }
899

    
900
    if( !mTexture.setTexture(bitmap) )
901
      {
902
      int max = DistortedLibrary.getMaxTextureSize();
903
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
904
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
905
      }
906
    }
907

    
908
///////////////////////////////////////////////////////////////////////////////////////////////////
909

    
910
  public int getNumLayers()
911
    {
912
    return mNumLayers;
913
    }
914

    
915
///////////////////////////////////////////////////////////////////////////////////////////////////
916

    
917
  public void continueRotation(float angleInDegrees)
918
    {
919
    mRotationAngleStatic.set0(angleInDegrees);
920
    }
921

    
922
///////////////////////////////////////////////////////////////////////////////////////////////////
923

    
924
  public Static4D getRotationQuat()
925
      {
926
      return mQuat;
927
      }
928

    
929
///////////////////////////////////////////////////////////////////////////////////////////////////
930

    
931
  public void recomputeScaleFactor(int scrWidth)
932
    {
933
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
934
    }
935

    
936
///////////////////////////////////////////////////////////////////////////////////////////////////
937

    
938
  public void savePreferences(SharedPreferences.Editor editor)
939
    {
940
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
941
    }
942

    
943
///////////////////////////////////////////////////////////////////////////////////////////////////
944

    
945
  public synchronized void restorePreferences(SharedPreferences preferences)
946
    {
947
    boolean error = false;
948

    
949
    for(int i=0; i<NUM_CUBITS; i++)
950
      {
951
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
952

    
953
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
954
        {
955
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
956
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
957
        }
958
      else
959
        {
960
        error = true;
961
        }
962
      }
963

    
964
    if( error )
965
      {
966
      for(int i=0; i<NUM_CUBITS; i++)
967
        {
968
        CUBITS[i].solve();
969
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
970
        }
971
      recordQuatsState("Failed to restorePreferences");
972
      }
973
    }
974

    
975
///////////////////////////////////////////////////////////////////////////////////////////////////
976

    
977
  public void recordQuatsState(String message)
978
    {
979
    StringBuilder quats = new StringBuilder();
980

    
981
    for(int j=0; j<NUM_CUBITS; j++)
982
      {
983
      quats.append(mQuatDebug[j]);
984
      quats.append(" ");
985
      }
986

    
987
    if( BuildConfig.DEBUG )
988
      {
989
      android.util.Log.e("quats" , quats.toString());
990
      android.util.Log.e("object", mList.name()+"_"+mNumLayers);
991
      }
992
    else
993
      {
994
      Exception ex = new Exception(message);
995
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
996
      crashlytics.setCustomKey("quats" , quats.toString());
997
      crashlytics.setCustomKey("object", mList.name()+"_"+mNumLayers );
998
      crashlytics.recordException(ex);
999
      }
1000
    }
1001

    
1002
///////////////////////////////////////////////////////////////////////////////////////////////////
1003

    
1004
  public void releaseResources()
1005
    {
1006
    mTexture.markForDeletion();
1007
    mMesh.markForDeletion();
1008
    mEffects.markForDeletion();
1009

    
1010
    for(int j=0; j<NUM_CUBITS; j++)
1011
      {
1012
      CUBITS[j].releaseResources();
1013
      }
1014
    }
1015

    
1016
///////////////////////////////////////////////////////////////////////////////////////////////////
1017

    
1018
  public void apply(Effect effect, int position)
1019
    {
1020
    mEffects.apply(effect, position);
1021
    }
1022

    
1023
///////////////////////////////////////////////////////////////////////////////////////////////////
1024

    
1025
  public void remove(long effectID)
1026
    {
1027
    mEffects.abortById(effectID);
1028
    }
1029

    
1030
///////////////////////////////////////////////////////////////////////////////////////////////////
1031

    
1032
  public synchronized void solve()
1033
    {
1034
    for(int i=0; i<NUM_CUBITS; i++)
1035
      {
1036
      CUBITS[i].solve();
1037
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1038
      }
1039
    }
1040

    
1041
///////////////////////////////////////////////////////////////////////////////////////////////////
1042

    
1043
  public void resetAllTextureMaps()
1044
    {
1045
    final float ratioW = 1.0f/mNumTexCols;
1046
    final float ratioH = 1.0f/mNumTexRows;
1047
    int color, row, col;
1048

    
1049
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1050
      {
1051
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1052

    
1053
      for(int cubitface=0; cubitface<mNumCubitFaces; cubitface++)
1054
        {
1055
        color = getFaceColor(cubit,cubitface,mNumLayers);
1056
        row = (mNumTexRows-1) - color/mNumTexCols;
1057
        col = color%mNumTexCols;
1058
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1059
        }
1060

    
1061
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1062
      }
1063
    }
1064

    
1065
///////////////////////////////////////////////////////////////////////////////////////////////////
1066

    
1067
  public void setTextureMap(int cubit, int face, int newColor)
1068
    {
1069
    final float ratioW = 1.0f/mNumTexCols;
1070
    final float ratioH = 1.0f/mNumTexRows;
1071
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1072
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1073
    int col = newColor%mNumTexCols;
1074

    
1075
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1076
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1077
    }
1078

    
1079
///////////////////////////////////////////////////////////////////////////////////////////////////
1080

    
1081
  public synchronized void beginNewRotation(int axis, int row )
1082
    {
1083
    if( axis<0 || axis>=NUM_AXIS )
1084
      {
1085
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1086
      return;
1087
      }
1088
    if( row<0 || row>=mNumLayers )
1089
      {
1090
      android.util.Log.e("object", "invalid rotation row: "+row);
1091
      return;
1092
      }
1093

    
1094
    mRotAxis     = axis;
1095
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1096
    mRotationAngleStatic.set0(0.0f);
1097
    mRotationAxis.set( mAxis[axis] );
1098
    mRotationAngle.add(mRotationAngleStatic);
1099
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
1100
    }
1101

    
1102
///////////////////////////////////////////////////////////////////////////////////////////////////
1103

    
1104
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
1105
    {
1106
    if( wasRotateApplied() )
1107
      {
1108
      mRotAxis     = axis;
1109
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
1110

    
1111
      mRotationAngleStatic.set0(0.0f);
1112
      mRotationAxis.set( mAxis[axis] );
1113
      mRotationAngle.setDuration(durationMillis);
1114
      mRotationAngle.resetToBeginning();
1115
      mRotationAngle.add(new Static1D(0));
1116
      mRotationAngle.add(new Static1D(angle));
1117
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
1118
      mRotateEffect.notifyWhenFinished(listener);
1119

    
1120
      return mRotateEffect.getID();
1121
      }
1122

    
1123
    return 0;
1124
    }
1125

    
1126
///////////////////////////////////////////////////////////////////////////////////////////////////
1127

    
1128
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
1129
    {
1130
    if( wasRotateApplied() )
1131
      {
1132
      float angle = getAngle();
1133
      mRotationAngleStatic.set0(angle);
1134
      mRotationAngleFinal.set0(nearestAngleInDegrees);
1135
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
1136

    
1137
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1138
      mRotationAngle.resetToBeginning();
1139
      mRotationAngle.removeAll();
1140
      mRotationAngle.add(mRotationAngleStatic);
1141
      mRotationAngle.add(mRotationAngleMiddle);
1142
      mRotationAngle.add(mRotationAngleFinal);
1143
      mRotateEffect.notifyWhenFinished(listener);
1144

    
1145
      return mRotateEffect.getID();
1146
      }
1147

    
1148
    return 0;
1149
    }
1150

    
1151
///////////////////////////////////////////////////////////////////////////////////////////////////
1152

    
1153
  private float getAngle()
1154
    {
1155
    int pointNum = mRotationAngle.getNumPoints();
1156

    
1157
    if( pointNum>=1 )
1158
      {
1159
      return mRotationAngle.getPoint(pointNum-1).get0();
1160
      }
1161
    else
1162
      {
1163
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
1164
      crashlytics.log("points in RotationAngle: "+pointNum);
1165
      return 0;
1166
      }
1167
    }
1168

    
1169
///////////////////////////////////////////////////////////////////////////////////////////////////
1170

    
1171
  public synchronized void removeRotationNow()
1172
    {
1173
    float angle = getAngle();
1174
    double nearestAngleInRadians = angle*Math.PI/180;
1175
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
1176
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
1177
    float axisX = mAxis[mRotAxis].get0();
1178
    float axisY = mAxis[mRotAxis].get1();
1179
    float axisZ = mAxis[mRotAxis].get2();
1180
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
1181

    
1182
    mRotationAngle.removeAll();
1183
    mRotationAngleStatic.set0(0);
1184

    
1185
    for(int i=0; i<NUM_CUBITS; i++)
1186
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
1187
        {
1188
        int index = CUBITS[i].removeRotationNow(quat);
1189
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
1190
        }
1191
    }
1192

    
1193
///////////////////////////////////////////////////////////////////////////////////////////////////
1194

    
1195
  public void initializeObject(int[][] moves)
1196
    {
1197
    solve();
1198
    setupPosition(moves);
1199
    }
1200

    
1201
///////////////////////////////////////////////////////////////////////////////////////////////////
1202

    
1203
  public int getCubit(float[] point3D)
1204
    {
1205
    float dist, minDist = Float.MAX_VALUE;
1206
    int currentBest=-1;
1207
    float multiplier = returnMultiplier();
1208

    
1209
    point3D[0] *= multiplier;
1210
    point3D[1] *= multiplier;
1211
    point3D[2] *= multiplier;
1212

    
1213
    for(int i=0; i<NUM_CUBITS; i++)
1214
      {
1215
      dist = CUBITS[i].getDistSquared(point3D);
1216
      if( dist<minDist )
1217
        {
1218
        minDist = dist;
1219
        currentBest = i;
1220
        }
1221
      }
1222

    
1223
    return currentBest;
1224
    }
1225

    
1226
///////////////////////////////////////////////////////////////////////////////////////////////////
1227

    
1228
  public int computeNearestAngle(int axis, float angle, float speed)
1229
    {
1230
    int[] basicArray = getBasicAngle();
1231
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1232
    int nearestAngle = 360/basicAngle;
1233

    
1234
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1235
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1236

    
1237
    if( tmp!=0 ) return nearestAngle*tmp;
1238

    
1239
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1240
    }
1241

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

    
1244
  public float getCameraDist()
1245
    {
1246
    return mCameraDist;
1247
    }
1248

    
1249
///////////////////////////////////////////////////////////////////////////////////////////////////
1250

    
1251
  public int getNodeSize()
1252
    {
1253
    return mNodeSize;
1254
    }
1255

    
1256
///////////////////////////////////////////////////////////////////////////////////////////////////
1257

    
1258
  public ObjectList getObjectList()
1259
    {
1260
    return mList;
1261
    }
1262

    
1263
///////////////////////////////////////////////////////////////////////////////////////////////////
1264

    
1265
  abstract float getScreenRatio();
1266
  abstract float[][] getCubitPositions(int numLayers);
1267
  abstract Static4D[] getQuats();
1268
  abstract int getNumFaces();
1269
  abstract int getNumStickerTypes(int numLayers);
1270
  abstract int getNumCubitFaces();
1271
  abstract ObjectSticker retSticker(int face);
1272
  abstract int getColor(int face);
1273
  abstract int getFaceColor(int cubit, int cubitface, int numLayers);
1274
  abstract float returnMultiplier();
1275
  abstract float[][] getCuts(int numLayers);
1276
  abstract boolean shouldResetTextureMaps();
1277
  abstract int getCubitVariant(int cubit, int numLayers);
1278
  abstract int getNumCubitVariants(int numLayers);
1279
  abstract Static4D getQuat(int cubit, int numLayers);
1280
  abstract ObjectShape getObjectShape(int cubit, int numLayers);
1281
  abstract int[] getSolvedQuats(int cubit, int numLayers);
1282
  abstract int getSolvedFunctionIndex();
1283

    
1284
  public abstract Static3D[] getRotationAxis();
1285
  public abstract int[] getBasicAngle();
1286
  public abstract void randomizeNewScramble(int[][] scramble, Random rnd, int curScramble, int totScrambles);
1287
  public abstract int getObjectName(int numLayers);
1288
  public abstract int getInventor(int numLayers);
1289
  public abstract int getComplexity(int numLayers);
1290
  }
(33-33/41)