Project

General

Profile

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

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

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

    
20
package org.distorted.objectlib.main;
21

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

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

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

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

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

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

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

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

    
92
  protected final int NUM_FACE_COLORS;
93
  protected final int NUM_TEXTURES;
94
  protected final Cubit[] CUBITS;
95

    
96
  MeshBase[] mMeshes;
97
  final Static4D[] OBJECT_QUATS;
98
  final int NUM_CUBITS;
99
  final int NUM_AXIS;
100
  final int NUM_QUATS;
101

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

    
134
  //////////////////// SOLVED1 ////////////////////////
135

    
136
  private int[] mFaceMap;
137
  private int[][] mScramble;
138
  private int[] mColors;
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  TwistyObject(int[] numLayers, int realSize, Static4D quat, Static3D move, DistortedTexture nodeTexture,
143
               MeshSquare nodeMesh, DistortedEffects nodeEffects, Resources res, int screenWidth)
144
    {
145
    super(nodeTexture,nodeEffects,nodeMesh);
146

    
147
    mNodeSize = screenWidth;
148

    
149
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
150

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

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

    
167
    OBJECT_QUATS = getQuats();
168
    NUM_CUBITS  = mOrigPos.length;
169
    NUM_FACE_COLORS = getNumFaceColors();
170
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
171
    NUM_AXIS = mAxis.length;
172
    NUM_QUATS = OBJECT_QUATS.length;
173

    
174
    int scramblingType = getScrambleType();
175
    ScrambleState[] states = getScrambleStates();
176
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,numLayers,states);
177

    
178
    boolean bandaged=false;
179

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

    
189
    mIsBandaged = bandaged;
190

    
191
    mQuatDebug = new int[NUM_CUBITS];
192

    
193
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
194
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
195

    
196
    mNodeScale= new Static3D(screenWidth,NODE_RATIO*screenWidth,screenWidth);
197
    mQuat = quat;
198

    
199
    mRotationAngle= new Dynamic1D();
200
    mRotationAxis = new Static3D(1,0,0);
201
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
202

    
203
    mRotationAngleStatic = new Static1D(0);
204
    mRotationAngleMiddle = new Static1D(0);
205
    mRotationAngleFinal  = new Static1D(0);
206

    
207
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
208
    mObjectScale = new Static3D(scale,scale,scale);
209
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
210
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(quat, CENTER);
211
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
212

    
213
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
214
    nodeEffects.apply(nodeScaleEffect);
215

    
216
    mNumTexCols = NUM_STICKERS_IN_ROW;
217
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
218

    
219
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
220

    
221
    CUBITS = new Cubit[NUM_CUBITS];
222
    createMeshAndCubits(res);
223
    createDataStructuresForSolved(numLayers);
224

    
225
    mTexture = new DistortedTexture();
226
    mEffects = new DistortedEffects();
227

    
228
    createTexture();
229

    
230
    for(int q=0; q<NUM_QUATS; q++)
231
      {
232
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
233
      vq.setMeshAssociation(0,q);
234
      mEffects.apply(vq);
235
      }
236

    
237
    mEffects.apply(mRotateEffect);
238
    mEffects.apply(quatEffect);
239
    mEffects.apply(scaleEffect);
240
    mEffects.apply(moveEffect);
241

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

    
246
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
247

    
248
    float fov = getFOV();
249
    double halfFOV = fov * (Math.PI/360);
250
    mCameraDist = 0.5f*NODE_RATIO / (float)Math.tan(halfFOV);
251

    
252
    setProjection( fov, 0.1f);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

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

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

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

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  private void createMeshAndCubits(Resources res)
281
    {
282
    int resourceID= getResource(mNumLayers);
283

    
284
    if( resourceID!=0 && !ObjectControl.isInCreateMesh() )
285
      {
286
      InputStream is = res.openRawResource(resourceID);
287
      DataInputStream dos = new DataInputStream(is);
288
      mMesh = new MeshFile(dos);
289

    
290
      try
291
        {
292
        is.close();
293
        }
294
      catch(IOException e)
295
        {
296
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
297
        }
298

    
299
      for(int i=0; i<NUM_CUBITS; i++)
300
        {
301
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
302
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
303
        }
304

    
305
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
306
      }
307
    else
308
      {
309
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
310

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

    
320
      mMesh = new MeshJoined(cubitMesh);
321
      resetAllTextureMaps();
322
      }
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  private MeshBase createCubitMesh(int cubit, int[] numLayers)
328
    {
329
    int variant = getCubitVariant(cubit,numLayers);
330

    
331
    if( mMeshes==null )
332
      {
333
      FactoryCubit factory = FactoryCubit.getInstance();
334
      factory.clear();
335
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
336
      }
337

    
338
    if( mMeshes[variant]==null )
339
      {
340
      ObjectShape shape = getObjectShape(cubit,numLayers);
341
      FactoryCubit factory = FactoryCubit.getInstance();
342
      factory.createNewFaceTransform(shape);
343
      mMeshes[variant] = factory.createRoundedSolid(shape);
344
      }
345

    
346
    MeshBase mesh = mMeshes[variant].copy(true);
347
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
348
    mesh.apply(quat,0xffffffff,0);
349

    
350
    return mesh;
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  private void createDataStructuresForSolved(int[] numLayers)
356
    {
357
    mTmpQuats = new int[NUM_QUATS];
358
    mSolvedQuats = new int[NUM_CUBITS][];
359

    
360
    for(int c=0; c<NUM_CUBITS; c++)
361
      {
362
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
363
      }
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  private int getMultQuat(int index1, int index2)
369
    {
370
    if( mQuatMult==null )
371
      {
372
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
373

    
374
      for(int i=0; i<NUM_QUATS; i++)
375
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
376
      }
377

    
378
    if( mQuatMult[index1][index2]==-1 )
379
      {
380
      mQuatMult[index1][index2] = mulQuat(index1,index2);
381
      }
382

    
383
    return mQuatMult[index1][index2];
384
    }
385

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

    
394
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
395
    {
396
    int numNonBlack=0, nonBlackIndex=-1, color;
397

    
398
    for(int face=0; face<mNumCubitFaces; face++)
399
      {
400
      color = getFaceColor(cubit,face,numLayers);
401

    
402
      if( color<NUM_TEXTURES )
403
        {
404
        numNonBlack++;
405
        nonBlackIndex = color%NUM_FACE_COLORS;
406
        }
407
      }
408

    
409
    if( numNonBlack==0 ) return -1;
410
    if( numNonBlack>=2 ) return -2;
411

    
412
    return nonBlackIndex;
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  protected boolean shouldResetTextureMaps()
418
    {
419
    return false;
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

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

    
433
    int len = quats.length;
434
    int place = 0;
435

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

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

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

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

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

    
464
    return null;
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

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

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

    
479
      if( q1==q ) continue;
480

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

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

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

    
497
    return true;
498
    }
499

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

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

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

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

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

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

    
526
    return -1;
527
    }
528

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

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

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

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

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

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

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

    
566
    return false;
567
    }
568

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

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

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

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

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

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

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

    
617
    boolean solved = true;
618

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

    
628
    if( solved ) return true;
629

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

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

    
638
    return true;
639
    }
640

    
641
///////////////////////////////////////////////////////////////////////////////////////////////////
642

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

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

    
659
    return ret;
660
    }
661

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

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

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

    
673
    return (1<<num);
674
    }
675

    
676
///////////////////////////////////////////////////////////////////////////////////////////////////
677

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

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

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

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

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

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

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

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

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

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

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

    
734
///////////////////////////////////////////////////////////////////////////////////////////////////
735

    
736
  protected int getScrambleType()
737
    {
738
    return 0;
739
    }
740

    
741
///////////////////////////////////////////////////////////////////////////////////////////////////
742

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

    
749
      while( initBitmap!=rowBitmap )
750
        {
751
        initBitmap = rowBitmap;
752

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

    
761
    return rowBitmap;
762
    }
763

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

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

    
774
    float x = pos[offset  ];
775
    float y = pos[offset+1];
776
    float z = pos[offset+2];
777

    
778
    float xo,yo,zo;
779

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

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

    
790
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
791

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

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

    
809
///////////////////////////////////////////////////////////////////////////////////////////////////
810
// remember about the double cover or unit quaternions!
811

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

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

    
821
    final float MAX_ERROR = 0.1f;
822
    float dX,dY,dZ,dW;
823

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

    
831
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
832
          dY<MAX_ERROR && dY>-MAX_ERROR &&
833
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
834
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
835

    
836
      dX = OBJECT_QUATS[i].get0() + rX;
837
      dY = OBJECT_QUATS[i].get1() + rY;
838
      dZ = OBJECT_QUATS[i].get2() + rZ;
839
      dW = OBJECT_QUATS[i].get3() + rW;
840

    
841
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
842
          dY<MAX_ERROR && dY>-MAX_ERROR &&
843
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
844
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
845
      }
846

    
847
    return -1;
848
    }
849

    
850
///////////////////////////////////////////////////////////////////////////////////////////////////
851

    
852
  private float getAngle()
853
    {
854
    int pointNum = mRotationAngle.getNumPoints();
855

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

    
868
///////////////////////////////////////////////////////////////////////////////////////////////////
869

    
870
  private void recordQuatsState(String message)
871
    {
872
    StringBuilder quats = new StringBuilder();
873

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

    
880
    String name = intGetObjectType(mNumLayers).name();
881

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

    
897
///////////////////////////////////////////////////////////////////////////////////////////////////
898

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

    
905
///////////////////////////////////////////////////////////////////////////////////////////////////
906

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

    
918
    mRotationAngle.removeAll();
919
    mRotationAngleStatic.set0(0);
920

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

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

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

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

    
948
      return mRotateEffect.getID();
949
      }
950

    
951
    return 0;
952
    }
953

    
954
///////////////////////////////////////////////////////////////////////////////////////////////////
955

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

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

    
972
      return mRotateEffect.getID();
973
      }
974

    
975
    return 0;
976
    }
977

    
978
///////////////////////////////////////////////////////////////////////////////////////////////////
979

    
980
  void continueRotation(float angleInDegrees)
981
    {
982
    mRotationAngleStatic.set0(angleInDegrees);
983
    }
984

    
985
///////////////////////////////////////////////////////////////////////////////////////////////////
986

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

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

    
1008
///////////////////////////////////////////////////////////////////////////////////////////////////
1009

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

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

    
1022
///////////////////////////////////////////////////////////////////////////////////////////////////
1023

    
1024
  void resetAllTextureMaps()
1025
    {
1026
    final float ratioW = 1.0f/mNumTexCols;
1027
    final float ratioH = 1.0f/mNumTexRows;
1028
    int color, row, col;
1029

    
1030
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1031
      {
1032
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1033

    
1034
      for(int cubitface=0; cubitface<mNumCubitFaces; cubitface++)
1035
        {
1036
        color = getFaceColor(cubit,cubitface,mNumLayers);
1037
        row = (mNumTexRows-1) - color/mNumTexCols;
1038
        col = color%mNumTexCols;
1039
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1040
        }
1041

    
1042
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1043
      }
1044
    }
1045

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

    
1048
  void releaseResources()
1049
    {
1050
    mTexture.markForDeletion();
1051
    mMesh.markForDeletion();
1052
    mEffects.markForDeletion();
1053

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

    
1060
///////////////////////////////////////////////////////////////////////////////////////////////////
1061

    
1062
  synchronized void restorePreferences(SharedPreferences preferences)
1063
    {
1064
    boolean error = false;
1065

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

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

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

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

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

    
1099
///////////////////////////////////////////////////////////////////////////////////////////////////
1100

    
1101
  void recomputeScaleFactor(int scrWidth)
1102
    {
1103
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
1104
    }
1105

    
1106
///////////////////////////////////////////////////////////////////////////////////////////////////
1107
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1108

    
1109
  void createTexture()
1110
    {
1111
    Bitmap bitmap;
1112

    
1113
    Paint paint = new Paint();
1114
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1115
    Canvas canvas = new Canvas(bitmap);
1116

    
1117
    paint.setAntiAlias(true);
1118
    paint.setTextAlign(Paint.Align.CENTER);
1119
    paint.setStyle(Paint.Style.FILL);
1120

    
1121
    paint.setColor(COLOR_BLACK);
1122
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1123

    
1124
    int face = 0;
1125
    FactorySticker factory = FactorySticker.getInstance();
1126

    
1127
    for(int row=0; row<mNumTexRows; row++)
1128
      for(int col=0; col<mNumTexCols; col++)
1129
        {
1130
        if( face>=NUM_TEXTURES ) break;
1131
        ObjectSticker sticker = retSticker(face);
1132
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, getColor(face%NUM_FACE_COLORS), sticker);
1133
        face++;
1134
        }
1135

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

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

    
1146
  void setObjectRatioNow(float sc)
1147
    {
1148
    mObjectScreenRatio = sc;
1149
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
1150
    mObjectScale.set(scale,scale,scale);
1151
    }
1152

    
1153
///////////////////////////////////////////////////////////////////////////////////////////////////
1154

    
1155
  void setObjectRatio(float sizeChange)
1156
    {
1157
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1158

    
1159
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1160
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1161

    
1162
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
1163
    mObjectScale.set(scale,scale,scale);
1164
    }
1165

    
1166
///////////////////////////////////////////////////////////////////////////////////////////////////
1167

    
1168
  float getObjectRatio()
1169
    {
1170
    return mObjectScreenRatio*mInitScreenRatio;
1171
    }
1172

    
1173
///////////////////////////////////////////////////////////////////////////////////////////////////
1174

    
1175
  public float getRatio()
1176
    {
1177
    return mObjectScreenRatio;
1178
    }
1179

    
1180
///////////////////////////////////////////////////////////////////////////////////////////////////
1181

    
1182
  boolean isSolved()
1183
    {
1184
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1185
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1186
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1187
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1188

    
1189
    return false;
1190
    }
1191

    
1192
///////////////////////////////////////////////////////////////////////////////////////////////////
1193
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1194
// during setting up the initial position in the solver.
1195

    
1196
  int getCubit(float[] point3D)
1197
    {
1198
    float dist, minDist = Float.MAX_VALUE;
1199
    int currentBest=-1;
1200
    float multiplier = mNumLayers[0];
1201

    
1202
    point3D[0] *= multiplier;
1203
    point3D[1] *= multiplier;
1204
    point3D[2] *= multiplier;
1205

    
1206
    for(int i=0; i<NUM_CUBITS; i++)
1207
      {
1208
      dist = CUBITS[i].getDistSquared(point3D);
1209
      if( dist<minDist )
1210
        {
1211
        minDist = dist;
1212
        currentBest = i;
1213
        }
1214
      }
1215

    
1216
    return currentBest;
1217
    }
1218

    
1219
///////////////////////////////////////////////////////////////////////////////////////////////////
1220

    
1221
  int computeNearestAngle(int axis, float angle, float speed)
1222
    {
1223
    int[] basicArray = getBasicAngle();
1224
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1225
    int nearestAngle = 360/basicAngle;
1226

    
1227
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1228
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1229

    
1230
    if( tmp!=0 ) return nearestAngle*tmp;
1231

    
1232
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1233
    }
1234

    
1235
///////////////////////////////////////////////////////////////////////////////////////////////////
1236

    
1237
  float getCameraDist()
1238
    {
1239
    return mCameraDist;
1240
    }
1241

    
1242
///////////////////////////////////////////////////////////////////////////////////////////////////
1243
// INTERNAL API - those are called from 'effects' package
1244
///////////////////////////////////////////////////////////////////////////////////////////////////
1245

    
1246
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1247
    {
1248
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1249
    }
1250

    
1251
///////////////////////////////////////////////////////////////////////////////////////////////////
1252

    
1253
  public int getNodeSize()
1254
    {
1255
    return mNodeSize;
1256
    }
1257

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

    
1260
  public Static4D getRotationQuat()
1261
      {
1262
      return mQuat;
1263
      }
1264

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

    
1267
  public void apply(Effect effect, int position)
1268
    {
1269
    mEffects.apply(effect, position);
1270
    }
1271

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

    
1274
  public void remove(long effectID)
1275
    {
1276
    mEffects.abortById(effectID);
1277
    }
1278

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

    
1281
  public MeshBase getObjectMesh()
1282
    {
1283
    return mMesh;
1284
    }
1285

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

    
1288
  public DistortedEffects getObjectEffects()
1289
    {
1290
    return mEffects;
1291
    }
1292

    
1293
///////////////////////////////////////////////////////////////////////////////////////////////////
1294
// PUBLIC API
1295
///////////////////////////////////////////////////////////////////////////////////////////////////
1296

    
1297
  public int getCubitFaceColorIndex(int cubit, int face)
1298
    {
1299
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1300

    
1301
    int x = (int)(texMap.get0()/texMap.get2());
1302
    int y = (int)(texMap.get1()/texMap.get3());
1303

    
1304
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1305
    }
1306

    
1307
///////////////////////////////////////////////////////////////////////////////////////////////////
1308

    
1309
  public int[] getNumLayers()
1310
    {
1311
    return mNumLayers;
1312
    }
1313

    
1314
///////////////////////////////////////////////////////////////////////////////////////////////////
1315

    
1316
  public synchronized void solve()
1317
    {
1318
    for(int i=0; i<NUM_CUBITS; i++)
1319
      {
1320
      CUBITS[i].solve();
1321
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1322
      }
1323
    }
1324

    
1325
///////////////////////////////////////////////////////////////////////////////////////////////////
1326

    
1327
  public ObjectType getObjectType()
1328
    {
1329
    return intGetObjectType(mNumLayers);
1330
    }
1331

    
1332
///////////////////////////////////////////////////////////////////////////////////////////////////
1333

    
1334
  protected abstract int getFOV();
1335
  protected abstract float getScreenRatio();
1336
  protected abstract int getNumFaceColors();
1337
  protected abstract int getColor(int face);
1338
  protected abstract float[][] getCuts(int[] numLayers);
1339
  protected abstract int getNumCubitFaces();
1340
  protected abstract Static4D[] getQuats();
1341
  protected abstract float[][] getCubitPositions(int[] numLayers);
1342
  protected abstract int getCubitVariant(int cubit, int[] numLayers);
1343
  protected abstract int getNumCubitVariants(int[] numLayers);
1344
  protected abstract Static4D getQuat(int cubit, int[] numLayers);
1345
  protected abstract ObjectShape getObjectShape(int cubit, int[] numLayers);
1346
  protected abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1347
  protected abstract int getSolvedFunctionIndex();
1348
  protected abstract ScrambleState[] getScrambleStates();
1349
  protected abstract int getNumStickerTypes(int[] numLayers);
1350
  protected abstract ObjectSticker retSticker(int face);
1351
  protected abstract int getFaceColor(int cubit, int cubitface, int[] numLayers);
1352
  protected abstract int getResource(int[] numLayers);
1353
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1354
  protected abstract Movement getMovement();
1355

    
1356
  public abstract Static3D[] getRotationAxis();
1357
  public abstract int[] getBasicAngle();
1358
  public abstract int getNumFaces();
1359
  public abstract int getObjectName(int[] numLayers);
1360
  public abstract int getInventor(int[] numLayers);
1361
  public abstract int getComplexity(int[] numLayers);
1362
  }
(14-14/15)