Project

General

Profile

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

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

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.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26

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

    
29
import org.distorted.library.effect.Effect;
30
import org.distorted.library.effect.MatrixEffectMove;
31
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectQuaternion;
34
import org.distorted.library.effect.VertexEffectRotate;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedNode;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.main.QuatHelper;
40
import org.distorted.library.mesh.MeshBase;
41
import org.distorted.library.mesh.MeshFile;
42
import org.distorted.library.mesh.MeshJoined;
43
import org.distorted.library.message.EffectListener;
44
import org.distorted.library.type.Dynamic1D;
45
import org.distorted.library.type.Static1D;
46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
48

    
49
import org.distorted.objectlib.BuildConfig;
50
import org.distorted.objectlib.helpers.FactoryCubit;
51
import org.distorted.objectlib.helpers.FactorySticker;
52
import org.distorted.objectlib.helpers.ObjectShape;
53
import org.distorted.objectlib.helpers.ObjectSticker;
54
import org.distorted.objectlib.helpers.ScrambleState;
55
import org.distorted.objectlib.json.JsonReader;
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 int NUM_FACE_COLORS;
97
  protected int NUM_TEXTURES;
98
  protected Cubit[] CUBITS;
99

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

    
106
  private int mNumCubitFaces;
107
  private Static3D[] mAxis;
108
  private float[][] mCuts;
109
  private int[] mNumCuts;
110
  private float[][] mOrigPos;
111
  private Static4D mQuat;
112
  private final int[] mNumLayers;
113
  private final float mSize;
114
  private DistortedEffects mEffects;
115
  private VertexEffectRotate mRotateEffect;
116
  private Dynamic1D mRotationAngle;
117
  private Static3D mRotationAxis;
118
  private Static3D mObjectScale;
119
  private int[] mQuatDebug;
120
  private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
121
  private DistortedTexture mTexture;
122
  private float mInitScreenRatio;
123
  private int mSolvedFunctionIndex;
124
  private 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 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(InputStream jsonStream, Static4D quat, Static3D move, InputStream meshStream)
148
    {
149
    JsonReader reader = JsonReader.getInstance();
150
    reader.parseJsonFile(jsonStream);
151
    setReader(reader);
152

    
153
    mNumLayers = reader.getNumLayers();
154
    mSize      = reader.getSize();
155
    initialize(quat,move,meshStream);
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  TwistyObject(int[] numLayers, float size, Static4D quat, Static3D move, InputStream meshStream)
161
    {
162
    mNumLayers = numLayers;
163
    mSize      = size;
164
    initialize(quat,move,meshStream);
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  private void initialize(Static4D quat, Static3D move, InputStream stream)
170
    {
171
    mQuat = quat;
172
    mOrigPos = getCubitPositions(mNumLayers);
173
    mAxis = getRotationAxis();
174
    mInitScreenRatio = getScreenRatio();
175
    mNumCubitFaces = getNumCubitFaces();
176
    mSolvedFunctionIndex = getSolvedFunctionIndex();
177

    
178
    mCuts = getCuts(mNumLayers);
179
    mNumCuts = new int[mAxis.length];
180
    for(int i=0; i<mAxis.length; i++)
181
      {
182
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
183
      }
184

    
185
    OBJECT_QUATS = getQuats();
186
    NUM_CUBITS  = mOrigPos.length;
187
    NUM_FACE_COLORS = getNumFaceColors();
188
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
189
    NUM_AXIS = mAxis.length;
190
    NUM_QUATS = OBJECT_QUATS.length;
191

    
192
    int scramblingType = getScrambleType();
193
    ScrambleState[] states = getScrambleStates();
194
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,mNumLayers,states);
195

    
196
    boolean bandaged=false;
197

    
198
    for(int c=0; c<NUM_CUBITS; c++)
199
      {
200
      if( mOrigPos[c].length>3 )
201
        {
202
        bandaged=true;
203
        break;
204
        }
205
      }
206

    
207
    mIsBandaged = bandaged;
208
    mQuatDebug = new int[NUM_CUBITS];
209

    
210
    mRotationAngle= new Dynamic1D();
211
    mRotationAxis = new Static3D(1,0,0);
212
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
213

    
214
    mRotationAngleStatic = new Static1D(0);
215
    mRotationAngleMiddle = new Static1D(0);
216
    mRotationAngleFinal  = new Static1D(0);
217

    
218
    mObjectScale = new Static3D(1,1,1);
219
    setObjectRatioNow(1.0f,720);
220

    
221
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
222
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(mQuat, CENTER);
223
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
224

    
225
    mNumTexCols = NUM_STICKERS_IN_ROW;
226
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
227

    
228
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
229

    
230
    CUBITS = new Cubit[NUM_CUBITS];
231
    createMeshAndCubits(stream);
232
    createDataStructuresForSolved(mNumLayers);
233

    
234
    mTexture = new DistortedTexture();
235
    mEffects = new DistortedEffects();
236

    
237
    createTexture();
238

    
239
    for(int q=0; q<NUM_QUATS; q++)
240
      {
241
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
242
      vq.setMeshAssociation(0,q);
243
      mEffects.apply(vq);
244
      }
245

    
246
    mEffects.apply(mRotateEffect);
247
    mEffects.apply(quatEffect);
248
    mEffects.apply(scaleEffect);
249
    mEffects.apply(moveEffect);
250

    
251
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  private Static3D getPos(float[] origPos)
257
    {
258
    int len = origPos.length/3;
259
    float sumX = 0.0f;
260
    float sumY = 0.0f;
261
    float sumZ = 0.0f;
262

    
263
    for(int i=0; i<len; i++)
264
      {
265
      sumX += origPos[3*i  ];
266
      sumY += origPos[3*i+1];
267
      sumZ += origPos[3*i+2];
268
      }
269

    
270
    sumX /= len;
271
    sumY /= len;
272
    sumZ /= len;
273

    
274
    return new Static3D(sumX,sumY,sumZ);
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  private void createMeshAndCubits(InputStream stream)
280
    {
281
    if( stream!=null )
282
      {
283
      DataInputStream dos = new DataInputStream(stream);
284
      mMesh = new MeshFile(dos);
285

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

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

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

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

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

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

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

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

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

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

    
346
    return mesh;
347
    }
348

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

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

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

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  private int getMultQuat(int index1, int index2)
365
    {
366
    if( mQuatMult==null )
367
      {
368
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
369

    
370
      for(int i=0; i<NUM_QUATS; i++)
371
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
372
      }
373

    
374
    if( mQuatMult[index1][index2]==-1 )
375
      {
376
      mQuatMult[index1][index2] = mulQuat(index1,index2);
377
      }
378

    
379
    return mQuatMult[index1][index2];
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383
// This is used to build internal data structures for the generic 'isSolved()'
384
//
385
// if this is an internal cubit (all faces black): return -1
386
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
387
// Color index, i.e. the index into the 'FACE_COLORS' table.
388
// else (edge or corner cubit, more than one non-black face): return -2.
389

    
390
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
391
    {
392
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
393
    int variant = getCubitVariant(cubit,numLayers);
394

    
395
    for(int face=0; face<mNumCubitFaces; face++)
396
      {
397
      varColor = getVariantFaceColor(variant,face,numLayers);
398
      cubColor = getCubitFaceColor(cubit,face,numLayers);
399

    
400
      if( varColor>=0 && cubColor>=0 )
401
        {
402
        numNonBlack++;
403
        nonBlackIndex = cubColor;
404
        }
405
      }
406

    
407
    if( numNonBlack==0 ) return -1;
408
    if( numNonBlack>=2 ) return -2;
409

    
410
    return nonBlackIndex;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  protected boolean shouldResetTextureMaps()
416
    {
417
    return false;
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
423
    {
424
    final float MAXD = 0.0001f;
425
    float x = faceAx.get0();
426
    float y = faceAx.get1();
427
    float z = faceAx.get2();
428
    float a,dx,dy,dz,qx,qy,qz;
429
    Static4D quat;
430

    
431
    int len = quats.length;
432
    int place = 0;
433

    
434
    for(int q=1; q<len; q++)
435
      {
436
      quat = quats[q];
437
      qx = quat.get0();
438
      qy = quat.get1();
439
      qz = quat.get2();
440

    
441
           if( x!=0.0f ) { a = qx/x; }
442
      else if( y!=0.0f ) { a = qy/y; }
443
      else               { a = qz/z; }
444

    
445
      dx = a*x-qx;
446
      dy = a*y-qy;
447
      dz = a*z-qz;
448

    
449
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
450
        {
451
        mTmpQuats[place++] = q;
452
        }
453
      }
454

    
455
    if( place!=0 )
456
      {
457
      int[] ret = new int[place];
458
      System.arraycopy(mTmpQuats,0,ret,0,place);
459
      return ret;
460
      }
461

    
462
    return null;
463
    }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

    
467
  private boolean isSolved0()
468
    {
469
    int len, q1,q = CUBITS[0].mQuatIndex;
470
    int[] solved;
471
    boolean skip;
472

    
473
    for(int c=1; c<NUM_CUBITS; c++)
474
      {
475
      q1 = CUBITS[c].mQuatIndex;
476

    
477
      if( q1==q ) continue;
478

    
479
      skip = false;
480
      solved = mSolvedQuats[c];
481
      len = solved==null ? 0:solved.length;
482

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

    
492
      if( !skip ) return false;
493
      }
494

    
495
    return true;
496
    }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

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

    
507
    float x = result.get0();
508
    float y = result.get1();
509
    float z = result.get2();
510

    
511
    for(int c=0; c<NUM_CUBITS; c++)
512
      {
513
      float[] cent = mOrigPos[c];
514

    
515
      float qx = cent[0] - x;
516
      float qy = cent[1] - y;
517
      float qz = cent[2] - z;
518

    
519
      if( qx>-MAXDIFF && qx<MAXDIFF &&
520
          qy>-MAXDIFF && qy<MAXDIFF &&
521
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
522
      }
523

    
524
    return -1;
525
    }
526

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

    
534
  private boolean isSolved1()
535
    {
536
    if( mScramble==null )
537
      {
538
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
539
      mColors   = new int[NUM_CUBITS];
540

    
541
      for(int q=0; q<NUM_QUATS; q++)
542
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
543
      }
544

    
545
    if( mFaceMap==null )
546
      {
547
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
548
      }
549

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

    
556
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
557
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
558
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
559

    
560
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
561
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
562
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
563

    
564
    return false;
565
    }
566

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

    
585
  private boolean isSolved2()
586
    {
587
    int qX = CUBITS[0].mQuatIndex;
588
    int qY = CUBITS[1].mQuatIndex;
589
    int qZ = CUBITS[4].mQuatIndex;
590

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

    
598
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
599
    }
600

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

    
609
  private boolean isSolved3()
610
    {
611
    int index = CUBITS[0].mQuatIndex;
612

    
613
    if( CUBITS[1].mQuatIndex!=index ) return false;
614

    
615
    boolean solved = true;
616

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

    
626
    if( solved ) return true;
627

    
628
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
629
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
630

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

    
636
    return true;
637
    }
638

    
639
///////////////////////////////////////////////////////////////////////////////////////////////////
640

    
641
  int computeRow(float[] pos, int axisIndex)
642
    {
643
    int ret=0;
644
    int len = pos.length / 3;
645
    Static3D axis = mAxis[axisIndex];
646
    float axisX = axis.get0();
647
    float axisY = axis.get1();
648
    float axisZ = axis.get2();
649
    float casted;
650

    
651
    for(int i=0; i<len; i++)
652
      {
653
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
654
      ret |= computeSingleRow(axisIndex,casted);
655
      }
656

    
657
    return ret;
658
    }
659

    
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661

    
662
  private int computeSingleRow(int axisIndex,float casted)
663
    {
664
    int num = mNumCuts[axisIndex];
665

    
666
    for(int i=0; i<num; i++)
667
      {
668
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
669
      }
670

    
671
    return (1<<num);
672
    }
673

    
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675

    
676
  private boolean wasRotateApplied()
677
    {
678
    return mEffects.exists(mRotateEffect.getID());
679
    }
680

    
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682

    
683
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
684
    {
685
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
686
    }
687

    
688
///////////////////////////////////////////////////////////////////////////////////////////////////
689
// note the minus in front of the sin() - we rotate counterclockwise
690
// when looking towards the direction where the axis increases in values.
691

    
692
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
693
    {
694
    Static3D axis = mAxis[axisIndex];
695

    
696
    while( angleInDegrees<0 ) angleInDegrees += 360;
697
    angleInDegrees %= 360;
698
    
699
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
700
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
701

    
702
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
703
    }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706

    
707
  private synchronized void setupPosition(int[][] moves)
708
    {
709
    if( moves!=null )
710
      {
711
      Static4D quat;
712
      int index, axis, rowBitmap, angle;
713
      int[] basic = getBasicAngle();
714

    
715
      for(int[] move: moves)
716
        {
717
        axis     = move[0];
718
        rowBitmap= move[1];
719
        angle    = move[2]*(360/basic[axis]);
720
        quat     = makeQuaternion(axis,angle);
721

    
722
        for(int j=0; j<NUM_CUBITS; j++)
723
          if( belongsToRotation(j,axis,rowBitmap) )
724
            {
725
            index = CUBITS[j].removeRotationNow(quat);
726
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
727
            }
728
        }
729
      }
730
    }
731

    
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733

    
734
  public int getScrambleType()
735
    {
736
    return 0;
737
    }
738

    
739
///////////////////////////////////////////////////////////////////////////////////////////////////
740

    
741
  int computeBitmapFromRow(int rowBitmap, int axis)
742
    {
743
    if( mIsBandaged )
744
      {
745
      int bitmap, initBitmap=0;
746

    
747
      while( initBitmap!=rowBitmap )
748
        {
749
        initBitmap = rowBitmap;
750

    
751
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
752
          {
753
          bitmap = CUBITS[cubit].getRotRow(axis);
754
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
755
          }
756
        }
757
      }
758

    
759
    return rowBitmap;
760
    }
761

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

    
766
  void clampPos(float[] pos, int offset)
767
    {
768
    float currError, minError = Float.MAX_VALUE;
769
    int minErrorIndex1 = -1;
770
    int minErrorIndex2 = -1;
771

    
772
    float x = pos[offset  ];
773
    float y = pos[offset+1];
774
    float z = pos[offset+2];
775

    
776
    float xo,yo,zo;
777

    
778
    for(int i=0; i<NUM_CUBITS; i++)
779
      {
780
      int len = mOrigPos[i].length / 3;
781

    
782
      for(int j=0; j<len; j++)
783
        {
784
        xo = mOrigPos[i][3*j  ];
785
        yo = mOrigPos[i][3*j+1];
786
        zo = mOrigPos[i][3*j+2];
787

    
788
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
789

    
790
        if( currError<minError )
791
          {
792
          minError = currError;
793
          minErrorIndex1 = i;
794
          minErrorIndex2 = j;
795
          }
796
        }
797
      }
798

    
799
    if( minError< 0.1f ) // TODO: 0.1 ?
800
      {
801
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
802
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
803
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
804
      }
805
    }
806

    
807
///////////////////////////////////////////////////////////////////////////////////////////////////
808
// remember about the double cover or unit quaternions!
809

    
810
  int mulQuat(int q1, int q2)
811
    {
812
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
813

    
814
    float rX = result.get0();
815
    float rY = result.get1();
816
    float rZ = result.get2();
817
    float rW = result.get3();
818

    
819
    final float MAX_ERROR = 0.1f;
820
    float dX,dY,dZ,dW;
821

    
822
    for(int i=0; i<NUM_QUATS; i++)
823
      {
824
      dX = OBJECT_QUATS[i].get0() - rX;
825
      dY = OBJECT_QUATS[i].get1() - rY;
826
      dZ = OBJECT_QUATS[i].get2() - rZ;
827
      dW = OBJECT_QUATS[i].get3() - rW;
828

    
829
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
830
          dY<MAX_ERROR && dY>-MAX_ERROR &&
831
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
832
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return 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

    
845
    return -1;
846
    }
847

    
848
///////////////////////////////////////////////////////////////////////////////////////////////////
849

    
850
  private float getAngle()
851
    {
852
    int pointNum = mRotationAngle.getNumPoints();
853

    
854
    if( pointNum>=1 )
855
      {
856
      return mRotationAngle.getPoint(pointNum-1).get0();
857
      }
858
    else
859
      {
860
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
861
      crashlytics.log("points in RotationAngle: "+pointNum);
862
      return 0;
863
      }
864
    }
865

    
866
///////////////////////////////////////////////////////////////////////////////////////////////////
867

    
868
  private void recordQuatsState(String message)
869
    {
870
    StringBuilder quats = new StringBuilder();
871

    
872
    for(int j=0; j<NUM_CUBITS; j++)
873
      {
874
      quats.append(mQuatDebug[j]);
875
      quats.append(" ");
876
      }
877

    
878
    String name = intGetObjectType(mNumLayers).name();
879

    
880
    if( BuildConfig.DEBUG )
881
      {
882
      android.util.Log.e("quats" , quats.toString());
883
      android.util.Log.e("object", name);
884
      }
885
    else
886
      {
887
      Exception ex = new Exception(message);
888
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
889
      crashlytics.setCustomKey("quats" , quats.toString());
890
      crashlytics.setCustomKey("object", name);
891
      crashlytics.recordException(ex);
892
      }
893
    }
894

    
895
///////////////////////////////////////////////////////////////////////////////////////////////////
896

    
897
  void initializeObject(int[][] moves)
898
    {
899
    solve();
900
    setupPosition(moves);
901
    }
902

    
903
///////////////////////////////////////////////////////////////////////////////////////////////////
904

    
905
  synchronized void removeRotationNow()
906
    {
907
    float angle = getAngle();
908
    double nearestAngleInRadians = angle*Math.PI/180;
909
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
910
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
911
    float axisX = mAxis[mCurrentRotAxis].get0();
912
    float axisY = mAxis[mCurrentRotAxis].get1();
913
    float axisZ = mAxis[mCurrentRotAxis].get2();
914
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
915

    
916
    mRotationAngle.removeAll();
917
    mRotationAngleStatic.set0(0);
918

    
919
    for(int i=0; i<NUM_CUBITS; i++)
920
      if( belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap) )
921
        {
922
        int index = CUBITS[i].removeRotationNow(quat);
923
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
924
        }
925
    }
926

    
927
///////////////////////////////////////////////////////////////////////////////////////////////////
928

    
929
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
930
    {
931
    if( wasRotateApplied() )
932
      {
933
      float angle = getAngle();
934
      mRotationAngleStatic.set0(angle);
935
      mRotationAngleFinal.set0(nearestAngleInDegrees);
936
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
937

    
938
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
939
      mRotationAngle.resetToBeginning();
940
      mRotationAngle.removeAll();
941
      mRotationAngle.add(mRotationAngleStatic);
942
      mRotationAngle.add(mRotationAngleMiddle);
943
      mRotationAngle.add(mRotationAngleFinal);
944
      mRotateEffect.notifyWhenFinished(listener);
945

    
946
      return mRotateEffect.getID();
947
      }
948

    
949
    return 0;
950
    }
951

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

    
954
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
955
    {
956
    if( wasRotateApplied() )
957
      {
958
      mCurrentRotAxis = axis;
959
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
960

    
961
      mRotationAngleStatic.set0(0.0f);
962
      mRotationAxis.set( mAxis[axis] );
963
      mRotationAngle.setDuration(durationMillis);
964
      mRotationAngle.resetToBeginning();
965
      mRotationAngle.add(new Static1D(0));
966
      mRotationAngle.add(new Static1D(angle));
967
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
968
      mRotateEffect.notifyWhenFinished(listener);
969

    
970
      return mRotateEffect.getID();
971
      }
972

    
973
    return 0;
974
    }
975

    
976
///////////////////////////////////////////////////////////////////////////////////////////////////
977

    
978
  void continueRotation(float angleInDegrees)
979
    {
980
    mRotationAngleStatic.set0(angleInDegrees);
981
    }
982

    
983
///////////////////////////////////////////////////////////////////////////////////////////////////
984

    
985
  synchronized void beginNewRotation(int axis, int row )
986
    {
987
    if( axis<0 || axis>=NUM_AXIS )
988
      {
989
      android.util.Log.e("object", "invalid rotation axis: "+axis);
990
      return;
991
      }
992
    if( row<0 || row>=mNumLayers[axis] )
993
      {
994
      android.util.Log.e("object", "invalid rotation row: "+row);
995
      return;
996
      }
997

    
998
    mCurrentRotAxis = axis;
999
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1000
    mRotationAngleStatic.set0(0.0f);
1001
    mRotationAxis.set( mAxis[axis] );
1002
    mRotationAngle.add(mRotationAngleStatic);
1003
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
1004
    }
1005

    
1006
///////////////////////////////////////////////////////////////////////////////////////////////////
1007

    
1008
  void setTextureMap(int cubit, int face, int newColor)
1009
    {
1010
    final float ratioW = 1.0f/mNumTexCols;
1011
    final float ratioH = 1.0f/mNumTexRows;
1012
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1013
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1014
    int col = newColor%mNumTexCols;
1015

    
1016
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1017
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1018
    }
1019

    
1020
///////////////////////////////////////////////////////////////////////////////////////////////////
1021

    
1022
  void resetAllTextureMaps()
1023
    {
1024
    final float ratioW = 1.0f/mNumTexCols;
1025
    final float ratioH = 1.0f/mNumTexRows;
1026
    int cubColor, varColor, color, variant, row, col;
1027

    
1028
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1029
      {
1030
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1031
      variant = getCubitVariant(cubit,mNumLayers);
1032

    
1033
      for(int face=0; face<mNumCubitFaces; face++)
1034
        {
1035
        cubColor = getCubitFaceColor(cubit,face,mNumLayers);
1036
        varColor = getVariantFaceColor(variant,face,mNumLayers);
1037
        color    = cubColor<0 || varColor<0 ? NUM_TEXTURES : varColor*NUM_FACE_COLORS + cubColor;
1038
        row      = (mNumTexRows-1) - color/mNumTexCols;
1039
        col      = color%mNumTexCols;
1040

    
1041
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1042
        }
1043

    
1044
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1045
      }
1046
    }
1047

    
1048
///////////////////////////////////////////////////////////////////////////////////////////////////
1049

    
1050
  void releaseResources()
1051
    {
1052
    mTexture.markForDeletion();
1053
    mMesh.markForDeletion();
1054
    mEffects.markForDeletion();
1055

    
1056
    for(int j=0; j<NUM_CUBITS; j++)
1057
      {
1058
      CUBITS[j].releaseResources();
1059
      }
1060
    }
1061

    
1062
///////////////////////////////////////////////////////////////////////////////////////////////////
1063

    
1064
  synchronized void restorePreferences(SharedPreferences preferences)
1065
    {
1066
    boolean error = false;
1067

    
1068
    for(int i=0; i<NUM_CUBITS; i++)
1069
      {
1070
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1071

    
1072
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1073
        {
1074
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1075
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1076
        }
1077
      else
1078
        {
1079
        error = true;
1080
        }
1081
      }
1082

    
1083
    if( error )
1084
      {
1085
      for(int i=0; i<NUM_CUBITS; i++)
1086
        {
1087
        CUBITS[i].solve();
1088
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1089
        }
1090
      recordQuatsState("Failed to restorePreferences");
1091
      }
1092
    }
1093

    
1094
///////////////////////////////////////////////////////////////////////////////////////////////////
1095

    
1096
  void savePreferences(SharedPreferences.Editor editor)
1097
    {
1098
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1099
    }
1100

    
1101
///////////////////////////////////////////////////////////////////////////////////////////////////
1102
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1103

    
1104
  void createTexture()
1105
    {
1106
    Bitmap bitmap;
1107

    
1108
    Paint paint = new Paint();
1109
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1110
    Canvas canvas = new Canvas(bitmap);
1111

    
1112
    paint.setAntiAlias(true);
1113
    paint.setTextAlign(Paint.Align.CENTER);
1114
    paint.setStyle(Paint.Style.FILL);
1115

    
1116
    paint.setColor(COLOR_BLACK);
1117
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1118

    
1119
    int texture = 0;
1120
    FactorySticker factory = FactorySticker.getInstance();
1121

    
1122
    for(int row=0; row<mNumTexRows; row++)
1123
      for(int col=0; col<mNumTexCols; col++)
1124
        {
1125
        if( texture>=NUM_TEXTURES ) break;
1126
        ObjectSticker sticker = retSticker(texture/NUM_FACE_COLORS);
1127
        int color = getColor(texture%NUM_FACE_COLORS);
1128
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, color, sticker);
1129
        texture++;
1130
        }
1131

    
1132
    if( !mTexture.setTexture(bitmap) )
1133
      {
1134
      int max = DistortedLibrary.getMaxTextureSize();
1135
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
1136
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
1137
      }
1138
    }
1139

    
1140
///////////////////////////////////////////////////////////////////////////////////////////////////
1141

    
1142
  void setObjectRatioNow(float sc, int nodeMinSize)
1143
    {
1144
    mObjectScreenRatio = sc;
1145
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeMinSize/mSize;
1146
    mObjectScale.set(scale,scale,scale);
1147
    }
1148

    
1149
///////////////////////////////////////////////////////////////////////////////////////////////////
1150

    
1151
  void setObjectRatio(float sizeChange, int nodeMinSize)
1152
    {
1153
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1154

    
1155
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1156
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1157

    
1158
    setObjectRatioNow(mObjectScreenRatio, nodeMinSize);
1159
    }
1160

    
1161
///////////////////////////////////////////////////////////////////////////////////////////////////
1162

    
1163
  float getObjectRatio()
1164
    {
1165
    return mObjectScreenRatio*mInitScreenRatio;
1166
    }
1167

    
1168
///////////////////////////////////////////////////////////////////////////////////////////////////
1169

    
1170
  public float getRatio()
1171
    {
1172
    return mObjectScreenRatio;
1173
    }
1174

    
1175
///////////////////////////////////////////////////////////////////////////////////////////////////
1176

    
1177
  boolean isSolved()
1178
    {
1179
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1180
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1181
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1182
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1183

    
1184
    return false;
1185
    }
1186

    
1187
///////////////////////////////////////////////////////////////////////////////////////////////////
1188
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1189
// during setting up the initial position in the solver.
1190

    
1191
  int getCubit(float[] point3D)
1192
    {
1193
    float dist, minDist = Float.MAX_VALUE;
1194
    int currentBest=-1;
1195
    float multiplier = mNumLayers[0];
1196

    
1197
    point3D[0] *= multiplier;
1198
    point3D[1] *= multiplier;
1199
    point3D[2] *= multiplier;
1200

    
1201
    for(int i=0; i<NUM_CUBITS; i++)
1202
      {
1203
      dist = CUBITS[i].getDistSquared(point3D);
1204
      if( dist<minDist )
1205
        {
1206
        minDist = dist;
1207
        currentBest = i;
1208
        }
1209
      }
1210

    
1211
    return currentBest;
1212
    }
1213

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

    
1216
  int computeNearestAngle(int axis, float angle, float speed)
1217
    {
1218
    int[] basicArray = getBasicAngle();
1219
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1220
    int nearestAngle = 360/basicAngle;
1221

    
1222
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1223
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1224

    
1225
    if( tmp!=0 ) return nearestAngle*tmp;
1226

    
1227
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1228
    }
1229

    
1230
///////////////////////////////////////////////////////////////////////////////////////////////////
1231
// INTERNAL API - those are called from 'effects' package
1232
///////////////////////////////////////////////////////////////////////////////////////////////////
1233

    
1234
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1235
    {
1236
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1237
    }
1238

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

    
1241
  public Static4D getRotationQuat()
1242
    {
1243
    return mQuat;
1244
    }
1245

    
1246
///////////////////////////////////////////////////////////////////////////////////////////////////
1247

    
1248
  public float getSize()
1249
    {
1250
    return mSize;
1251
    }
1252

    
1253
///////////////////////////////////////////////////////////////////////////////////////////////////
1254

    
1255
  public void apply(Effect effect, int position)
1256
    {
1257
    mEffects.apply(effect, position);
1258
    }
1259

    
1260
///////////////////////////////////////////////////////////////////////////////////////////////////
1261

    
1262
  public void remove(long effectID)
1263
    {
1264
    mEffects.abortById(effectID);
1265
    }
1266

    
1267
///////////////////////////////////////////////////////////////////////////////////////////////////
1268

    
1269
  public MeshBase getObjectMesh()
1270
    {
1271
    return mMesh;
1272
    }
1273

    
1274
///////////////////////////////////////////////////////////////////////////////////////////////////
1275

    
1276
  public DistortedEffects getObjectEffects()
1277
    {
1278
    return mEffects;
1279
    }
1280

    
1281
///////////////////////////////////////////////////////////////////////////////////////////////////
1282
// PUBLIC API
1283
///////////////////////////////////////////////////////////////////////////////////////////////////
1284

    
1285
  public int getCubitFaceColorIndex(int cubit, int face)
1286
    {
1287
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1288

    
1289
    int x = (int)(texMap.get0()/texMap.get2());
1290
    int y = (int)(texMap.get1()/texMap.get3());
1291

    
1292
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1293
    }
1294

    
1295
///////////////////////////////////////////////////////////////////////////////////////////////////
1296

    
1297
  public int[] getNumLayers()
1298
    {
1299
    return mNumLayers;
1300
    }
1301

    
1302
///////////////////////////////////////////////////////////////////////////////////////////////////
1303

    
1304
  public synchronized void solve()
1305
    {
1306
    for(int i=0; i<NUM_CUBITS; i++)
1307
      {
1308
      CUBITS[i].solve();
1309
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1310
      }
1311
    }
1312

    
1313
///////////////////////////////////////////////////////////////////////////////////////////////////
1314

    
1315
  public DistortedNode getNode()
1316
    {
1317
    return mNode;
1318
    }
1319

    
1320
///////////////////////////////////////////////////////////////////////////////////////////////////
1321

    
1322
  public ObjectType getObjectType()
1323
    {
1324
    return intGetObjectType(mNumLayers);
1325
    }
1326

    
1327
///////////////////////////////////////////////////////////////////////////////////////////////////
1328

    
1329
  public Movement getMovement()
1330
    {
1331
    if( mMovement==null )
1332
      {
1333
      int[] numLayers = getNumLayers();
1334
      if( mCuts==null ) getCuts(numLayers);
1335
      if( mLayerRotatable==null ) mLayerRotatable = getLayerRotatable(numLayers);
1336
      if( mEnabled==null ) mEnabled = getEnabled();
1337

    
1338
      int movementType = getMovementType();
1339
      int movementSplit= getMovementSplit();
1340

    
1341
      switch(movementType)
1342
        {
1343
        case MOVEMENT_TETRAHEDRON : mMovement = new Movement4(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1344
                                    break;
1345
        case MOVEMENT_HEXAHEDRON  : mMovement = new Movement6(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1346
                                    break;
1347
        case MOVEMENT_OCTAHEDRON  : mMovement = new Movement8(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1348
                                    break;
1349
        case MOVEMENT_DODECAHEDRON: mMovement = new Movement12(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1350
                                    break;
1351
        case MOVEMENT_SHAPECHANGE : float[] dist3D = getDist3D(numLayers);
1352
                                    mMovement = new MovementC(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled,dist3D);
1353
                                    break;
1354
        }
1355
      }
1356
    return mMovement;
1357
    }
1358

    
1359
///////////////////////////////////////////////////////////////////////////////////////////////////
1360

    
1361
  protected void setReader(JsonReader reader)
1362
    {
1363
    // empty
1364
    }
1365

    
1366
///////////////////////////////////////////////////////////////////////////////////////////////////
1367

    
1368
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1369

    
1370
  // for JSON only
1371
  public abstract int getSolvedFunctionIndex();
1372
  public abstract int getMovementType();
1373
  public abstract int getMovementSplit();
1374
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1375
  public abstract int[][][] getEnabled();
1376
  public abstract float[] getDist3D(int[] numLayers);
1377
  public abstract ScrambleState[] getScrambleStates();
1378
  public abstract float[][] getCuts(int[] numLayers);
1379
  public abstract Static4D[] getQuats();
1380
  public abstract int getNumStickerTypes(int[] numLayers);
1381
  public abstract ObjectSticker retSticker(int sticker);
1382
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1383
  public abstract ObjectShape getObjectShape(int variant);
1384
  public abstract int getNumCubitVariants(int[] numLayers);
1385
  public abstract float[][] getCubitPositions(int[] numLayers);
1386
  public abstract Static4D getQuat(int cubit, int[] numLayers);
1387
  public abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1388
  public abstract int getCubitFaceColor(int cubit, int face, int[] numLayers);
1389
  public abstract int getVariantFaceColor(int variant, int face, int[] numLayers);
1390
  public abstract int getNumFaceColors();
1391
  public abstract int getNumCubitFaces();
1392
  public abstract float getScreenRatio();
1393
  public abstract int getColor(int face);
1394

    
1395
  // not only for JSON
1396
  public abstract Static3D[] getRotationAxis();
1397
  public abstract int[] getBasicAngle();
1398
  public abstract int getNumFaces();
1399
  public abstract String getObjectName();
1400
  public abstract String getInventor();
1401
  public abstract int getYearOfInvention();
1402
  public abstract int getComplexity();
1403
  public abstract int getFOV();
1404
  }
(16-16/18)