Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ e26eb4e7

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.message.EffectListener;
45
import org.distorted.library.type.Dynamic1D;
46
import org.distorted.library.type.Static1D;
47
import org.distorted.library.type.Static3D;
48
import org.distorted.library.type.Static4D;
49

    
50
import org.distorted.objectlib.BuildConfig;
51
import org.distorted.objectlib.helpers.FactoryCubit;
52
import org.distorted.objectlib.helpers.FactorySticker;
53
import org.distorted.objectlib.helpers.ObjectShape;
54
import org.distorted.objectlib.helpers.ObjectSticker;
55
import org.distorted.objectlib.helpers.ScrambleState;
56

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

    
62
import static org.distorted.objectlib.main.Movement.MOVEMENT_TETRAHEDRON;
63
import static org.distorted.objectlib.main.Movement.MOVEMENT_HEXAHEDRON;
64
import static org.distorted.objectlib.main.Movement.MOVEMENT_OCTAHEDRON;
65
import static org.distorted.objectlib.main.Movement.MOVEMENT_DODECAHEDRON;
66
import static org.distorted.objectlib.main.Movement.MOVEMENT_SHAPECHANGE;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
public abstract class TwistyObject
71
  {
72
  public static final int COLOR_YELLOW = 0xffffff00;
73
  public static final int COLOR_WHITE  = 0xffffffff;
74
  public static final int COLOR_BLUE   = 0xff0000ff;
75
  public static final int COLOR_GREEN  = 0xff00bb00;
76
  public static final int COLOR_RED    = 0xff990000;
77
  public static final int COLOR_ORANGE = 0xffff6200;
78
  public static final int COLOR_GREY   = 0xff727c7b;
79
  public static final int COLOR_VIOLET = 0xff7700bb;
80
  public static final int COLOR_BLACK  = 0xff000000;
81

    
82
  public static final int TEXTURE_HEIGHT = 256;
83
  static final int NUM_STICKERS_IN_ROW = 4;
84

    
85
  public static final float SQ2 = (float)Math.sqrt(2);
86
  public static final float SQ3 = (float)Math.sqrt(3);
87
  public static final float SQ5 = (float)Math.sqrt(5);
88
  public static final float SQ6 = (float)Math.sqrt(6);
89

    
90
  private static final float MAX_SIZE_CHANGE = 1.35f;
91
  private static final float MIN_SIZE_CHANGE = 0.75f;
92

    
93
  private static final Static3D CENTER = new Static3D(0,0,0);
94
  private static final int POST_ROTATION_MILLISEC = 500;
95

    
96
  protected final int NUM_FACE_COLORS;
97
  protected final int NUM_TEXTURES;
98
  protected final Cubit[] CUBITS;
99

    
100
  MeshBase[] mMeshes;
101
  final Static4D[] OBJECT_QUATS;
102
  final int NUM_CUBITS;
103
  final int NUM_AXIS;
104
  final int NUM_QUATS;
105

    
106
  private final int mNumCubitFaces;
107
  private final Static3D[] mAxis;
108
  private final float[][] mCuts;
109
  private final int[] mNumCuts;
110
  private final float[][] mOrigPos;
111
  private final Static4D mQuat;
112
  private final int[] mNumLayers;
113
  private final float mSize;
114
  private final DistortedEffects mEffects;
115
  private final VertexEffectRotate mRotateEffect;
116
  private final Dynamic1D mRotationAngle;
117
  private final Static3D mRotationAxis;
118
  private final Static3D mObjectScale;
119
  private final int[] mQuatDebug;
120
  private final Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
121
  private final DistortedTexture mTexture;
122
  private final float mInitScreenRatio;
123
  private final int mSolvedFunctionIndex;
124
  private final boolean mIsBandaged;
125
  private float mObjectScreenRatio;
126
  private int[][] mSolvedQuats;
127
  private int[][] mQuatMult;
128
  private int[] mTmpQuats;
129
  private int mNumTexRows, mNumTexCols;
130
  private int mRotRowBitmap;
131
  private int mCurrentRotAxis;
132
  private MeshBase mMesh;
133
  private final TwistyObjectScrambler mScrambler;
134
  private Movement mMovement;
135
  private boolean[][] mLayerRotatable;
136
  private int[][][] mEnabled;
137
  private DistortedNode mNode;
138

    
139
  //////////////////// SOLVED1 ////////////////////////
140

    
141
  private int[] mFaceMap;
142
  private int[][] mScramble;
143
  private int[] mColors;
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  TwistyObject(int[] numLayers, float size, Static4D quat, Static3D move, Resources res)
148
    {
149
    mNumLayers = numLayers;
150
    mSize = size;
151
    mOrigPos = getCubitPositions(mNumLayers);
152
    mAxis = getRotationAxis();
153
    mInitScreenRatio = getScreenRatio();
154
    mNumCubitFaces = getNumCubitFaces();
155
    mSolvedFunctionIndex = getSolvedFunctionIndex();
156

    
157
    mCuts = getCuts(mNumLayers);
158
    mNumCuts = new int[mAxis.length];
159
    for(int i=0; i<mAxis.length; i++)
160
      {
161
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
162
      }
163

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

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

    
175
    boolean bandaged=false;
176

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

    
186
    mIsBandaged = bandaged;
187
    mQuatDebug = new int[NUM_CUBITS];
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
    mObjectScale = new Static3D(1,1,1);
199
    setObjectRatioNow(1.0f,720);
200

    
201
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
202
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(quat, CENTER);
203
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
204

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

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

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

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

    
217
    createTexture();
218

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

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

    
231
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  private Static3D getPos(float[] origPos)
237
    {
238
    int len = origPos.length/3;
239
    float sumX = 0.0f;
240
    float sumY = 0.0f;
241
    float sumZ = 0.0f;
242

    
243
    for(int i=0; i<len; i++)
244
      {
245
      sumX += origPos[3*i  ];
246
      sumY += origPos[3*i+1];
247
      sumZ += origPos[3*i+2];
248
      }
249

    
250
    sumX /= len;
251
    sumY /= len;
252
    sumZ /= len;
253

    
254
    return new Static3D(sumX,sumY,sumZ);
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  private void createMeshAndCubits(Resources res)
260
    {
261
    int resourceID= getResource(mNumLayers);
262

    
263
    if( resourceID!=0 && !ObjectControl.isInCreateMesh() )
264
      {
265
      InputStream is = res.openRawResource(resourceID);
266
      DataInputStream dos = new DataInputStream(is);
267
      mMesh = new MeshFile(dos);
268

    
269
      try
270
        {
271
        is.close();
272
        }
273
      catch(IOException e)
274
        {
275
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
276
        }
277

    
278
      for(int i=0; i<NUM_CUBITS; i++)
279
        {
280
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
281
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
282
        }
283

    
284
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
285
      }
286
    else
287
      {
288
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
289

    
290
      for(int i=0; i<NUM_CUBITS; i++)
291
        {
292
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
293
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
294
        Static3D pos = getPos(mOrigPos[i]);
295
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
296
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
297
        }
298

    
299
      mMesh = new MeshJoined(cubitMesh);
300
      resetAllTextureMaps();
301
      }
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  private MeshBase createCubitMesh(int cubit, int[] numLayers)
307
    {
308
    int variant = getCubitVariant(cubit,numLayers);
309

    
310
    if( mMeshes==null )
311
      {
312
      FactoryCubit factory = FactoryCubit.getInstance();
313
      factory.clear();
314
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
315
      }
316

    
317
    if( mMeshes[variant]==null )
318
      {
319
      ObjectShape shape = getObjectShape(variant);
320
      FactoryCubit factory = FactoryCubit.getInstance();
321
      factory.createNewFaceTransform(shape);
322
      mMeshes[variant] = factory.createRoundedSolid(shape);
323
      }
324

    
325
    MeshBase mesh = mMeshes[variant].copy(true);
326
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
327
    mesh.apply(quat,0xffffffff,0);
328

    
329
    return mesh;
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  private void createDataStructuresForSolved(int[] numLayers)
335
    {
336
    mTmpQuats = new int[NUM_QUATS];
337
    mSolvedQuats = new int[NUM_CUBITS][];
338

    
339
    for(int c=0; c<NUM_CUBITS; c++)
340
      {
341
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
342
      }
343
    }
344

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

    
347
  private int getMultQuat(int index1, int index2)
348
    {
349
    if( mQuatMult==null )
350
      {
351
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
352

    
353
      for(int i=0; i<NUM_QUATS; i++)
354
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
355
      }
356

    
357
    if( mQuatMult[index1][index2]==-1 )
358
      {
359
      mQuatMult[index1][index2] = mulQuat(index1,index2);
360
      }
361

    
362
    return mQuatMult[index1][index2];
363
    }
364

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

    
373
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
374
    {
375
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
376
    int variant = getCubitVariant(cubit,numLayers);
377

    
378
    for(int face=0; face<mNumCubitFaces; face++)
379
      {
380
      varColor = getVariantFaceColor(variant,face,numLayers);
381
      cubColor = getCubitFaceColor(cubit,face,numLayers);
382

    
383
      if( varColor>=0 && cubColor>=0 )
384
        {
385
        numNonBlack++;
386
        nonBlackIndex = cubColor;
387
        }
388
      }
389

    
390
    if( numNonBlack==0 ) return -1;
391
    if( numNonBlack>=2 ) return -2;
392

    
393
    return nonBlackIndex;
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  protected boolean shouldResetTextureMaps()
399
    {
400
    return false;
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
406
    {
407
    final float MAXD = 0.0001f;
408
    float x = faceAx.get0();
409
    float y = faceAx.get1();
410
    float z = faceAx.get2();
411
    float a,dx,dy,dz,qx,qy,qz;
412
    Static4D quat;
413

    
414
    int len = quats.length;
415
    int place = 0;
416

    
417
    for(int q=1; q<len; q++)
418
      {
419
      quat = quats[q];
420
      qx = quat.get0();
421
      qy = quat.get1();
422
      qz = quat.get2();
423

    
424
           if( x!=0.0f ) { a = qx/x; }
425
      else if( y!=0.0f ) { a = qy/y; }
426
      else               { a = qz/z; }
427

    
428
      dx = a*x-qx;
429
      dy = a*y-qy;
430
      dz = a*z-qz;
431

    
432
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
433
        {
434
        mTmpQuats[place++] = q;
435
        }
436
      }
437

    
438
    if( place!=0 )
439
      {
440
      int[] ret = new int[place];
441
      System.arraycopy(mTmpQuats,0,ret,0,place);
442
      return ret;
443
      }
444

    
445
    return null;
446
    }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

    
450
  private boolean isSolved0()
451
    {
452
    int len, q1,q = CUBITS[0].mQuatIndex;
453
    int[] solved;
454
    boolean skip;
455

    
456
    for(int c=1; c<NUM_CUBITS; c++)
457
      {
458
      q1 = CUBITS[c].mQuatIndex;
459

    
460
      if( q1==q ) continue;
461

    
462
      skip = false;
463
      solved = mSolvedQuats[c];
464
      len = solved==null ? 0:solved.length;
465

    
466
      for(int i=0; i<len; i++)
467
        {
468
        if( q1==getMultQuat(q,solved[i]) )
469
          {
470
          skip = true;
471
          break;
472
          }
473
        }
474

    
475
      if( !skip ) return false;
476
      }
477

    
478
    return true;
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  private int computeScramble(int quatNum, int centerNum)
484
    {
485
    float MAXDIFF = 0.01f;
486
    float[] center= mOrigPos[centerNum];
487
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
488
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
489

    
490
    float x = result.get0();
491
    float y = result.get1();
492
    float z = result.get2();
493

    
494
    for(int c=0; c<NUM_CUBITS; c++)
495
      {
496
      float[] cent = mOrigPos[c];
497

    
498
      float qx = cent[0] - x;
499
      float qy = cent[1] - y;
500
      float qz = cent[2] - z;
501

    
502
      if( qx>-MAXDIFF && qx<MAXDIFF &&
503
          qy>-MAXDIFF && qy<MAXDIFF &&
504
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
505
      }
506

    
507
    return -1;
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511
// Dino4 uses this. It is solved if and only if groups of cubits
512
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
513
// or
514
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
515
// are all the same color.
516

    
517
  private boolean isSolved1()
518
    {
519
    if( mScramble==null )
520
      {
521
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
522
      mColors   = new int[NUM_CUBITS];
523

    
524
      for(int q=0; q<NUM_QUATS; q++)
525
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
526
      }
527

    
528
    if( mFaceMap==null )
529
      {
530
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
531
      }
532

    
533
    for(int c=0; c<NUM_CUBITS; c++)
534
      {
535
      int index = mScramble[CUBITS[c].mQuatIndex][c];
536
      mColors[index] = mFaceMap[c];
537
      }
538

    
539
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
540
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
541
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
542

    
543
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
544
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
545
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
546

    
547
    return false;
548
    }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551
// Dino6 uses this. It is solved if and only if:
552
//
553
// All four 'X' cubits (i.e. those whose longest edge goes along the X axis) are rotated
554
// by the same quaternion qX, similarly all four 'Y' cubits by the same qY and all four 'Z'
555
// by the same qZ, and then either:
556
//
557
// a) qX = qY = qZ
558
// b) qY = qX*Q2 and qZ = qX*Q8  (i.e. swap of WHITE and YELLOW faces)
559
// c) qX = qY*Q2 and qZ = qY*Q10 (i.e. swap of BLUE and GREEN faces)
560
// d) qX = qZ*Q8 and qY = qZ*Q10 (i.e. swap of RED and BROWN faces)
561
//
562
// BUT: cases b), c) and d) are really the same - it's all just a mirror image of the original.
563
//
564
// X cubits: 0, 2, 8, 10
565
// Y cubits: 1, 3, 9, 11
566
// Z cubits: 4, 5, 6, 7
567

    
568
  private boolean isSolved2()
569
    {
570
    int qX = CUBITS[0].mQuatIndex;
571
    int qY = CUBITS[1].mQuatIndex;
572
    int qZ = CUBITS[4].mQuatIndex;
573

    
574
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
575
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
576
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
577
      {
578
      return false;
579
      }
580

    
581
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585
// Square-2 is solved iff
586
// a) all of its cubits are rotated with the same quat
587
// b) its two 'middle' cubits are rotated with the same quat, the 6 'front' and 6 'back'
588
// edges and corners with this quat multiplied by QUATS[18] (i.e. those are upside down)
589
// and all the 12 left and right edges and corners also with the same quat multiplied by
590
// QUATS[12] - i.e. also upside down.
591

    
592
  private boolean isSolved3()
593
    {
594
    int index = CUBITS[0].mQuatIndex;
595

    
596
    if( CUBITS[1].mQuatIndex!=index ) return false;
597

    
598
    boolean solved = true;
599

    
600
    for(int i=2; i<NUM_CUBITS; i++)
601
      {
602
      if( CUBITS[i].mQuatIndex!=index )
603
        {
604
        solved = false;
605
        break;
606
        }
607
      }
608

    
609
    if( solved ) return true;
610

    
611
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
612
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
613

    
614
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
615
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
616
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
617
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
618

    
619
    return true;
620
    }
621

    
622
///////////////////////////////////////////////////////////////////////////////////////////////////
623

    
624
  int computeRow(float[] pos, int axisIndex)
625
    {
626
    int ret=0;
627
    int len = pos.length / 3;
628
    Static3D axis = mAxis[axisIndex];
629
    float axisX = axis.get0();
630
    float axisY = axis.get1();
631
    float axisZ = axis.get2();
632
    float casted;
633

    
634
    for(int i=0; i<len; i++)
635
      {
636
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
637
      ret |= computeSingleRow(axisIndex,casted);
638
      }
639

    
640
    return ret;
641
    }
642

    
643
///////////////////////////////////////////////////////////////////////////////////////////////////
644

    
645
  private int computeSingleRow(int axisIndex,float casted)
646
    {
647
    int num = mNumCuts[axisIndex];
648

    
649
    for(int i=0; i<num; i++)
650
      {
651
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
652
      }
653

    
654
    return (1<<num);
655
    }
656

    
657
///////////////////////////////////////////////////////////////////////////////////////////////////
658

    
659
  private boolean wasRotateApplied()
660
    {
661
    return mEffects.exists(mRotateEffect.getID());
662
    }
663

    
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665

    
666
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
667
    {
668
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
669
    }
670

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672
// note the minus in front of the sin() - we rotate counterclockwise
673
// when looking towards the direction where the axis increases in values.
674

    
675
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
676
    {
677
    Static3D axis = mAxis[axisIndex];
678

    
679
    while( angleInDegrees<0 ) angleInDegrees += 360;
680
    angleInDegrees %= 360;
681
    
682
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
683
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
684

    
685
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
686
    }
687

    
688
///////////////////////////////////////////////////////////////////////////////////////////////////
689

    
690
  private synchronized void setupPosition(int[][] moves)
691
    {
692
    if( moves!=null )
693
      {
694
      Static4D quat;
695
      int index, axis, rowBitmap, angle;
696
      int[] basic = getBasicAngle();
697

    
698
      for(int[] move: moves)
699
        {
700
        axis     = move[0];
701
        rowBitmap= move[1];
702
        angle    = move[2]*(360/basic[axis]);
703
        quat     = makeQuaternion(axis,angle);
704

    
705
        for(int j=0; j<NUM_CUBITS; j++)
706
          if( belongsToRotation(j,axis,rowBitmap) )
707
            {
708
            index = CUBITS[j].removeRotationNow(quat);
709
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
710
            }
711
        }
712
      }
713
    }
714

    
715
///////////////////////////////////////////////////////////////////////////////////////////////////
716

    
717
  public int getScrambleType()
718
    {
719
    return 0;
720
    }
721

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

    
724
  int computeBitmapFromRow(int rowBitmap, int axis)
725
    {
726
    if( mIsBandaged )
727
      {
728
      int bitmap, initBitmap=0;
729

    
730
      while( initBitmap!=rowBitmap )
731
        {
732
        initBitmap = rowBitmap;
733

    
734
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
735
          {
736
          bitmap = CUBITS[cubit].getRotRow(axis);
737
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
738
          }
739
        }
740
      }
741

    
742
    return rowBitmap;
743
    }
744

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

    
749
  void clampPos(float[] pos, int offset)
750
    {
751
    float currError, minError = Float.MAX_VALUE;
752
    int minErrorIndex1 = -1;
753
    int minErrorIndex2 = -1;
754

    
755
    float x = pos[offset  ];
756
    float y = pos[offset+1];
757
    float z = pos[offset+2];
758

    
759
    float xo,yo,zo;
760

    
761
    for(int i=0; i<NUM_CUBITS; i++)
762
      {
763
      int len = mOrigPos[i].length / 3;
764

    
765
      for(int j=0; j<len; j++)
766
        {
767
        xo = mOrigPos[i][3*j  ];
768
        yo = mOrigPos[i][3*j+1];
769
        zo = mOrigPos[i][3*j+2];
770

    
771
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
772

    
773
        if( currError<minError )
774
          {
775
          minError = currError;
776
          minErrorIndex1 = i;
777
          minErrorIndex2 = j;
778
          }
779
        }
780
      }
781

    
782
    if( minError< 0.1f ) // TODO: 0.1 ?
783
      {
784
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
785
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
786
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
787
      }
788
    }
789

    
790
///////////////////////////////////////////////////////////////////////////////////////////////////
791
// remember about the double cover or unit quaternions!
792

    
793
  int mulQuat(int q1, int q2)
794
    {
795
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
796

    
797
    float rX = result.get0();
798
    float rY = result.get1();
799
    float rZ = result.get2();
800
    float rW = result.get3();
801

    
802
    final float MAX_ERROR = 0.1f;
803
    float dX,dY,dZ,dW;
804

    
805
    for(int i=0; i<NUM_QUATS; i++)
806
      {
807
      dX = OBJECT_QUATS[i].get0() - rX;
808
      dY = OBJECT_QUATS[i].get1() - rY;
809
      dZ = OBJECT_QUATS[i].get2() - rZ;
810
      dW = OBJECT_QUATS[i].get3() - rW;
811

    
812
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
813
          dY<MAX_ERROR && dY>-MAX_ERROR &&
814
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
815
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
816

    
817
      dX = OBJECT_QUATS[i].get0() + rX;
818
      dY = OBJECT_QUATS[i].get1() + rY;
819
      dZ = OBJECT_QUATS[i].get2() + rZ;
820
      dW = OBJECT_QUATS[i].get3() + rW;
821

    
822
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
823
          dY<MAX_ERROR && dY>-MAX_ERROR &&
824
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
825
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
826
      }
827

    
828
    return -1;
829
    }
830

    
831
///////////////////////////////////////////////////////////////////////////////////////////////////
832

    
833
  private float getAngle()
834
    {
835
    int pointNum = mRotationAngle.getNumPoints();
836

    
837
    if( pointNum>=1 )
838
      {
839
      return mRotationAngle.getPoint(pointNum-1).get0();
840
      }
841
    else
842
      {
843
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
844
      crashlytics.log("points in RotationAngle: "+pointNum);
845
      return 0;
846
      }
847
    }
848

    
849
///////////////////////////////////////////////////////////////////////////////////////////////////
850

    
851
  private void recordQuatsState(String message)
852
    {
853
    StringBuilder quats = new StringBuilder();
854

    
855
    for(int j=0; j<NUM_CUBITS; j++)
856
      {
857
      quats.append(mQuatDebug[j]);
858
      quats.append(" ");
859
      }
860

    
861
    String name = intGetObjectType(mNumLayers).name();
862

    
863
    if( BuildConfig.DEBUG )
864
      {
865
      android.util.Log.e("quats" , quats.toString());
866
      android.util.Log.e("object", name);
867
      }
868
    else
869
      {
870
      Exception ex = new Exception(message);
871
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
872
      crashlytics.setCustomKey("quats" , quats.toString());
873
      crashlytics.setCustomKey("object", name);
874
      crashlytics.recordException(ex);
875
      }
876
    }
877

    
878
///////////////////////////////////////////////////////////////////////////////////////////////////
879

    
880
  void initializeObject(int[][] moves)
881
    {
882
    solve();
883
    setupPosition(moves);
884
    }
885

    
886
///////////////////////////////////////////////////////////////////////////////////////////////////
887

    
888
  synchronized void removeRotationNow()
889
    {
890
    float angle = getAngle();
891
    double nearestAngleInRadians = angle*Math.PI/180;
892
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
893
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
894
    float axisX = mAxis[mCurrentRotAxis].get0();
895
    float axisY = mAxis[mCurrentRotAxis].get1();
896
    float axisZ = mAxis[mCurrentRotAxis].get2();
897
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
898

    
899
    mRotationAngle.removeAll();
900
    mRotationAngleStatic.set0(0);
901

    
902
    for(int i=0; i<NUM_CUBITS; i++)
903
      if( belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap) )
904
        {
905
        int index = CUBITS[i].removeRotationNow(quat);
906
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
907
        }
908
    }
909

    
910
///////////////////////////////////////////////////////////////////////////////////////////////////
911

    
912
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
913
    {
914
    if( wasRotateApplied() )
915
      {
916
      float angle = getAngle();
917
      mRotationAngleStatic.set0(angle);
918
      mRotationAngleFinal.set0(nearestAngleInDegrees);
919
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
920

    
921
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
922
      mRotationAngle.resetToBeginning();
923
      mRotationAngle.removeAll();
924
      mRotationAngle.add(mRotationAngleStatic);
925
      mRotationAngle.add(mRotationAngleMiddle);
926
      mRotationAngle.add(mRotationAngleFinal);
927
      mRotateEffect.notifyWhenFinished(listener);
928

    
929
      return mRotateEffect.getID();
930
      }
931

    
932
    return 0;
933
    }
934

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

    
937
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
938
    {
939
    if( wasRotateApplied() )
940
      {
941
      mCurrentRotAxis = axis;
942
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
943

    
944
      mRotationAngleStatic.set0(0.0f);
945
      mRotationAxis.set( mAxis[axis] );
946
      mRotationAngle.setDuration(durationMillis);
947
      mRotationAngle.resetToBeginning();
948
      mRotationAngle.add(new Static1D(0));
949
      mRotationAngle.add(new Static1D(angle));
950
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
951
      mRotateEffect.notifyWhenFinished(listener);
952

    
953
      return mRotateEffect.getID();
954
      }
955

    
956
    return 0;
957
    }
958

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

    
961
  void continueRotation(float angleInDegrees)
962
    {
963
    mRotationAngleStatic.set0(angleInDegrees);
964
    }
965

    
966
///////////////////////////////////////////////////////////////////////////////////////////////////
967

    
968
  synchronized void beginNewRotation(int axis, int row )
969
    {
970
    if( axis<0 || axis>=NUM_AXIS )
971
      {
972
      android.util.Log.e("object", "invalid rotation axis: "+axis);
973
      return;
974
      }
975
    if( row<0 || row>=mNumLayers[axis] )
976
      {
977
      android.util.Log.e("object", "invalid rotation row: "+row);
978
      return;
979
      }
980

    
981
    mCurrentRotAxis = axis;
982
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
983
    mRotationAngleStatic.set0(0.0f);
984
    mRotationAxis.set( mAxis[axis] );
985
    mRotationAngle.add(mRotationAngleStatic);
986
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
987
    }
988

    
989
///////////////////////////////////////////////////////////////////////////////////////////////////
990

    
991
  void setTextureMap(int cubit, int face, int newColor)
992
    {
993
    final float ratioW = 1.0f/mNumTexCols;
994
    final float ratioH = 1.0f/mNumTexRows;
995
    final Static4D[] maps = new Static4D[mNumCubitFaces];
996
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
997
    int col = newColor%mNumTexCols;
998

    
999
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1000
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1001
    }
1002

    
1003
///////////////////////////////////////////////////////////////////////////////////////////////////
1004

    
1005
  void resetAllTextureMaps()
1006
    {
1007
    final float ratioW = 1.0f/mNumTexCols;
1008
    final float ratioH = 1.0f/mNumTexRows;
1009
    int cubColor, varColor, color, variant, row, col;
1010

    
1011
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1012
      {
1013
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1014
      variant = getCubitVariant(cubit,mNumLayers);
1015

    
1016
      for(int face=0; face<mNumCubitFaces; face++)
1017
        {
1018
        cubColor = getCubitFaceColor(cubit,face,mNumLayers);
1019
        varColor = getVariantFaceColor(variant,face,mNumLayers);
1020
        color    = cubColor<0 || varColor<0 ? NUM_TEXTURES : varColor*NUM_FACE_COLORS + cubColor;
1021
        row      = (mNumTexRows-1) - color/mNumTexCols;
1022
        col      = color%mNumTexCols;
1023

    
1024
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1025
        }
1026

    
1027
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1028
      }
1029
    }
1030

    
1031
///////////////////////////////////////////////////////////////////////////////////////////////////
1032

    
1033
  void releaseResources()
1034
    {
1035
    mTexture.markForDeletion();
1036
    mMesh.markForDeletion();
1037
    mEffects.markForDeletion();
1038

    
1039
    for(int j=0; j<NUM_CUBITS; j++)
1040
      {
1041
      CUBITS[j].releaseResources();
1042
      }
1043
    }
1044

    
1045
///////////////////////////////////////////////////////////////////////////////////////////////////
1046

    
1047
  synchronized void restorePreferences(SharedPreferences preferences)
1048
    {
1049
    boolean error = false;
1050

    
1051
    for(int i=0; i<NUM_CUBITS; i++)
1052
      {
1053
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1054

    
1055
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1056
        {
1057
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1058
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1059
        }
1060
      else
1061
        {
1062
        error = true;
1063
        }
1064
      }
1065

    
1066
    if( error )
1067
      {
1068
      for(int i=0; i<NUM_CUBITS; i++)
1069
        {
1070
        CUBITS[i].solve();
1071
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1072
        }
1073
      recordQuatsState("Failed to restorePreferences");
1074
      }
1075
    }
1076

    
1077
///////////////////////////////////////////////////////////////////////////////////////////////////
1078

    
1079
  void savePreferences(SharedPreferences.Editor editor)
1080
    {
1081
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1082
    }
1083

    
1084
///////////////////////////////////////////////////////////////////////////////////////////////////
1085
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1086

    
1087
  void createTexture()
1088
    {
1089
    Bitmap bitmap;
1090

    
1091
    Paint paint = new Paint();
1092
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1093
    Canvas canvas = new Canvas(bitmap);
1094

    
1095
    paint.setAntiAlias(true);
1096
    paint.setTextAlign(Paint.Align.CENTER);
1097
    paint.setStyle(Paint.Style.FILL);
1098

    
1099
    paint.setColor(COLOR_BLACK);
1100
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1101

    
1102
    int texture = 0;
1103
    FactorySticker factory = FactorySticker.getInstance();
1104

    
1105
    for(int row=0; row<mNumTexRows; row++)
1106
      for(int col=0; col<mNumTexCols; col++)
1107
        {
1108
        if( texture>=NUM_TEXTURES ) break;
1109
        ObjectSticker sticker = retSticker(texture/NUM_FACE_COLORS);
1110
        int color = getColor(texture%NUM_FACE_COLORS);
1111
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, color, sticker);
1112
        texture++;
1113
        }
1114

    
1115
    if( !mTexture.setTexture(bitmap) )
1116
      {
1117
      int max = DistortedLibrary.getMaxTextureSize();
1118
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
1119
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
1120
      }
1121
    }
1122

    
1123
///////////////////////////////////////////////////////////////////////////////////////////////////
1124

    
1125
  void setObjectRatioNow(float sc, int nodeMinSize)
1126
    {
1127
    mObjectScreenRatio = sc;
1128
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeMinSize/mSize;
1129
    mObjectScale.set(scale,scale,scale);
1130
    }
1131

    
1132
///////////////////////////////////////////////////////////////////////////////////////////////////
1133

    
1134
  void setObjectRatio(float sizeChange, int nodeMinSize)
1135
    {
1136
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1137

    
1138
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1139
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1140

    
1141
    setObjectRatioNow(mObjectScreenRatio, nodeMinSize);
1142
    }
1143

    
1144
///////////////////////////////////////////////////////////////////////////////////////////////////
1145

    
1146
  float getObjectRatio()
1147
    {
1148
    return mObjectScreenRatio*mInitScreenRatio;
1149
    }
1150

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

    
1153
  public float getRatio()
1154
    {
1155
    return mObjectScreenRatio;
1156
    }
1157

    
1158
///////////////////////////////////////////////////////////////////////////////////////////////////
1159

    
1160
  boolean isSolved()
1161
    {
1162
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1163
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1164
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1165
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1166

    
1167
    return false;
1168
    }
1169

    
1170
///////////////////////////////////////////////////////////////////////////////////////////////////
1171
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1172
// during setting up the initial position in the solver.
1173

    
1174
  int getCubit(float[] point3D)
1175
    {
1176
    float dist, minDist = Float.MAX_VALUE;
1177
    int currentBest=-1;
1178
    float multiplier = mNumLayers[0];
1179

    
1180
    point3D[0] *= multiplier;
1181
    point3D[1] *= multiplier;
1182
    point3D[2] *= multiplier;
1183

    
1184
    for(int i=0; i<NUM_CUBITS; i++)
1185
      {
1186
      dist = CUBITS[i].getDistSquared(point3D);
1187
      if( dist<minDist )
1188
        {
1189
        minDist = dist;
1190
        currentBest = i;
1191
        }
1192
      }
1193

    
1194
    return currentBest;
1195
    }
1196

    
1197
///////////////////////////////////////////////////////////////////////////////////////////////////
1198

    
1199
  int computeNearestAngle(int axis, float angle, float speed)
1200
    {
1201
    int[] basicArray = getBasicAngle();
1202
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1203
    int nearestAngle = 360/basicAngle;
1204

    
1205
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1206
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1207

    
1208
    if( tmp!=0 ) return nearestAngle*tmp;
1209

    
1210
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1211
    }
1212

    
1213
///////////////////////////////////////////////////////////////////////////////////////////////////
1214
// INTERNAL API - those are called from 'effects' package
1215
///////////////////////////////////////////////////////////////////////////////////////////////////
1216

    
1217
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1218
    {
1219
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1220
    }
1221

    
1222
///////////////////////////////////////////////////////////////////////////////////////////////////
1223

    
1224
  public Static4D getRotationQuat()
1225
    {
1226
    return mQuat;
1227
    }
1228

    
1229
///////////////////////////////////////////////////////////////////////////////////////////////////
1230

    
1231
  public float getSize()
1232
    {
1233
    return mSize;
1234
    }
1235

    
1236
///////////////////////////////////////////////////////////////////////////////////////////////////
1237

    
1238
  public void apply(Effect effect, int position)
1239
    {
1240
    mEffects.apply(effect, position);
1241
    }
1242

    
1243
///////////////////////////////////////////////////////////////////////////////////////////////////
1244

    
1245
  public void remove(long effectID)
1246
    {
1247
    mEffects.abortById(effectID);
1248
    }
1249

    
1250
///////////////////////////////////////////////////////////////////////////////////////////////////
1251

    
1252
  public MeshBase getObjectMesh()
1253
    {
1254
    return mMesh;
1255
    }
1256

    
1257
///////////////////////////////////////////////////////////////////////////////////////////////////
1258

    
1259
  public DistortedEffects getObjectEffects()
1260
    {
1261
    return mEffects;
1262
    }
1263

    
1264
///////////////////////////////////////////////////////////////////////////////////////////////////
1265
// PUBLIC API
1266
///////////////////////////////////////////////////////////////////////////////////////////////////
1267

    
1268
  public int getCubitFaceColorIndex(int cubit, int face)
1269
    {
1270
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1271

    
1272
    int x = (int)(texMap.get0()/texMap.get2());
1273
    int y = (int)(texMap.get1()/texMap.get3());
1274

    
1275
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1276
    }
1277

    
1278
///////////////////////////////////////////////////////////////////////////////////////////////////
1279

    
1280
  public int[] getNumLayers()
1281
    {
1282
    return mNumLayers;
1283
    }
1284

    
1285
///////////////////////////////////////////////////////////////////////////////////////////////////
1286

    
1287
  public synchronized void solve()
1288
    {
1289
    for(int i=0; i<NUM_CUBITS; i++)
1290
      {
1291
      CUBITS[i].solve();
1292
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1293
      }
1294
    }
1295

    
1296
///////////////////////////////////////////////////////////////////////////////////////////////////
1297

    
1298
  public DistortedNode getNode()
1299
    {
1300
    return mNode;
1301
    }
1302

    
1303
///////////////////////////////////////////////////////////////////////////////////////////////////
1304

    
1305
  public ObjectType getObjectType()
1306
    {
1307
    return intGetObjectType(mNumLayers);
1308
    }
1309

    
1310
///////////////////////////////////////////////////////////////////////////////////////////////////
1311

    
1312
  public Movement getMovement()
1313
    {
1314
    if( mMovement==null )
1315
      {
1316
      int[] numLayers = getNumLayers();
1317
      if( mCuts==null ) getCuts(numLayers);
1318
      if( mLayerRotatable==null ) mLayerRotatable = getLayerRotatable(numLayers);
1319
      if( mEnabled==null ) mEnabled = getEnabled();
1320

    
1321
      int movementType = getMovementType();
1322
      int movementSplit= getMovementSplit();
1323

    
1324
      switch(movementType)
1325
        {
1326
        case MOVEMENT_TETRAHEDRON : mMovement = new Movement4(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1327
                                    break;
1328
        case MOVEMENT_HEXAHEDRON  : mMovement = new Movement6(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1329
                                    break;
1330
        case MOVEMENT_OCTAHEDRON  : mMovement = new Movement8(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1331
                                    break;
1332
        case MOVEMENT_DODECAHEDRON: mMovement = new Movement12(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1333
                                    break;
1334
        case MOVEMENT_SHAPECHANGE : float[] dist3D = getDist3D(numLayers);
1335
                                    mMovement = new MovementC(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled,dist3D);
1336
                                    break;
1337
        }
1338
      }
1339
    return mMovement;
1340
    }
1341

    
1342
///////////////////////////////////////////////////////////////////////////////////////////////////
1343

    
1344
  protected abstract float getScreenRatio();
1345
  protected abstract int getColor(int face);
1346
  protected abstract int getResource(int[] numLayers);
1347
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1348

    
1349
  // for JSON only
1350
  public abstract int getSolvedFunctionIndex();
1351
  public abstract int getMovementType();
1352
  public abstract int getMovementSplit();
1353
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1354
  public abstract int[][][] getEnabled();
1355
  public abstract float[] getDist3D(int[] numLayers);
1356
  public abstract ScrambleState[] getScrambleStates();
1357
  public abstract float[][] getCuts(int[] numLayers);
1358
  public abstract Static4D[] getQuats();
1359
  public abstract int getNumStickerTypes(int[] numLayers);
1360
  public abstract ObjectSticker retSticker(int sticker);
1361
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1362
  public abstract ObjectShape getObjectShape(int variant);
1363
  public abstract int getNumCubitVariants(int[] numLayers);
1364
  public abstract float[][] getCubitPositions(int[] numLayers);
1365
  public abstract Static4D getQuat(int cubit, int[] numLayers);
1366
  public abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1367
  public abstract int getCubitFaceColor(int cubit, int face, int[] numLayers);
1368
  public abstract int getVariantFaceColor(int variant, int face, int[] numLayers);
1369
  public abstract int getNumFaceColors();
1370
  public abstract int getNumCubitFaces();
1371

    
1372
  // not only for JSON
1373
  public abstract Static3D[] getRotationAxis();
1374
  public abstract int[] getBasicAngle();
1375
  public abstract int getNumFaces();
1376
  public abstract String getObjectName();
1377
  public abstract String getInventor();
1378
  public abstract int getYearOfInvention();
1379
  public abstract int getComplexity();
1380
  public abstract int getFOV();
1381
  }
(15-15/17)