Project

General

Profile

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

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

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 MAX_SIZE_CHANGE = 1.35f;
86
  private static final float MIN_SIZE_CHANGE = 0.75f;
87

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

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

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

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

    
133
  //////////////////// SOLVED1 ////////////////////////
134

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

    
146
    mNodeW = surfaceW;
147
    mNodeH = surfaceH;
148

    
149
    resizeFBO(mNodeW,mNodeH);
150

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

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

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

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

    
177
    boolean bandaged=false;
178

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

    
188
    mIsBandaged = bandaged;
189

    
190
    mQuatDebug = new int[NUM_CUBITS];
191

    
192
    mNodeScale= new Static3D(surfaceW,surfaceH,surfaceW);
193
    mQuat = quat;
194

    
195
    mRotationAngle= new Dynamic1D();
196
    mRotationAxis = new Static3D(1,0,0);
197
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
198

    
199
    mRotationAngleStatic = new Static1D(0);
200
    mRotationAngleMiddle = new Static1D(0);
201
    mRotationAngleFinal  = new Static1D(0);
202

    
203
    mObjectScale = new Static3D(1,1,1);
204
    setObjectRatioNow(1.0f);
205

    
206
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
207
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(quat, CENTER);
208
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
209

    
210
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
211
    nodeEffects.apply(nodeScaleEffect);
212

    
213
    mNumTexCols = NUM_STICKERS_IN_ROW;
214
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
215

    
216
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
217

    
218
    CUBITS = new Cubit[NUM_CUBITS];
219
    createMeshAndCubits(res);
220
    createDataStructuresForSolved(numLayers);
221

    
222
    mTexture = new DistortedTexture();
223
    mEffects = new DistortedEffects();
224

    
225
    createTexture();
226

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

    
234
    mEffects.apply(mRotateEffect);
235
    mEffects.apply(quatEffect);
236
    mEffects.apply(scaleEffect);
237
    mEffects.apply(moveEffect);
238

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

    
243
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
244

    
245
    float fov = getFOV();
246
    double halfFOV = fov * (Math.PI/360);
247
    mCameraDist = (0.5f*mNodeH) / ((float)Math.tan(halfFOV)*mNodeW);
248

    
249
    setProjection( fov, 0.1f);
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

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

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

    
268
    sumX /= len;
269
    sumY /= len;
270
    sumZ /= len;
271

    
272
    return new Static3D(sumX,sumY,sumZ);
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  private void createMeshAndCubits(Resources res)
278
    {
279
    int resourceID= getResource(mNumLayers);
280

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

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

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

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

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

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

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

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

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

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

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

    
347
    return mesh;
348
    }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

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

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

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

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

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

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

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

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

    
391
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
392
    {
393
    int numNonBlack=0, nonBlackIndex=-1, color;
394

    
395
    for(int face=0; face<mNumCubitFaces; face++)
396
      {
397
      color = getFaceColor(cubit,face,numLayers);
398

    
399
      if( color<NUM_TEXTURES )
400
        {
401
        numNonBlack++;
402
        nonBlackIndex = color%NUM_FACE_COLORS;
403
        }
404
      }
405

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

    
409
    return nonBlackIndex;
410
    }
411

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

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

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

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

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

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

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

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

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

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

    
461
    return null;
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

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

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

    
476
      if( q1==q ) continue;
477

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

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

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

    
494
    return true;
495
    }
496

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498

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

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

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

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

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

    
523
    return -1;
524
    }
525

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

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

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

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

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

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

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

    
563
    return false;
564
    }
565

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

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

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

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

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

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

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

    
614
    boolean solved = true;
615

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

    
625
    if( solved ) return true;
626

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

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

    
635
    return true;
636
    }
637

    
638
///////////////////////////////////////////////////////////////////////////////////////////////////
639

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

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

    
656
    return ret;
657
    }
658

    
659
///////////////////////////////////////////////////////////////////////////////////////////////////
660

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

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

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

    
673
///////////////////////////////////////////////////////////////////////////////////////////////////
674

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

    
680
///////////////////////////////////////////////////////////////////////////////////////////////////
681

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

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

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

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

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

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705

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

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

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

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

    
733
  protected int getScrambleType()
734
    {
735
    return 0;
736
    }
737

    
738
///////////////////////////////////////////////////////////////////////////////////////////////////
739

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

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

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

    
758
    return rowBitmap;
759
    }
760

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

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

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

    
775
    float xo,yo,zo;
776

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

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

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

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

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

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

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

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

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

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

    
828
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
829
          dY<MAX_ERROR && dY>-MAX_ERROR &&
830
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
831
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
832

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

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

    
844
    return -1;
845
    }
846

    
847
///////////////////////////////////////////////////////////////////////////////////////////////////
848

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

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

    
865
///////////////////////////////////////////////////////////////////////////////////////////////////
866

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

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

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

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

    
894
///////////////////////////////////////////////////////////////////////////////////////////////////
895

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

    
902
///////////////////////////////////////////////////////////////////////////////////////////////////
903

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

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

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

    
926
///////////////////////////////////////////////////////////////////////////////////////////////////
927

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

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

    
945
      return mRotateEffect.getID();
946
      }
947

    
948
    return 0;
949
    }
950

    
951
///////////////////////////////////////////////////////////////////////////////////////////////////
952

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

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

    
969
      return mRotateEffect.getID();
970
      }
971

    
972
    return 0;
973
    }
974

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

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

    
982
///////////////////////////////////////////////////////////////////////////////////////////////////
983

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

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

    
1005
///////////////////////////////////////////////////////////////////////////////////////////////////
1006

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

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

    
1019
///////////////////////////////////////////////////////////////////////////////////////////////////
1020

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

    
1027
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1028
      {
1029
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1030

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

    
1039
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1040
      }
1041
    }
1042

    
1043
///////////////////////////////////////////////////////////////////////////////////////////////////
1044

    
1045
  void releaseResources()
1046
    {
1047
    mTexture.markForDeletion();
1048
    mMesh.markForDeletion();
1049
    mEffects.markForDeletion();
1050

    
1051
    for(int j=0; j<NUM_CUBITS; j++)
1052
      {
1053
      CUBITS[j].releaseResources();
1054
      }
1055
    }
1056

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

    
1059
  synchronized void restorePreferences(SharedPreferences preferences)
1060
    {
1061
    boolean error = false;
1062

    
1063
    for(int i=0; i<NUM_CUBITS; i++)
1064
      {
1065
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1066

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

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

    
1089
///////////////////////////////////////////////////////////////////////////////////////////////////
1090

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

    
1096
///////////////////////////////////////////////////////////////////////////////////////////////////
1097

    
1098
  void recomputeScaleFactor(int surfaceW, int surfaceH)
1099
    {
1100
    mNodeScale.set(surfaceW,surfaceH,surfaceW);
1101
    }
1102

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

    
1106
  void createTexture()
1107
    {
1108
    Bitmap bitmap;
1109

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

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

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

    
1121
    int face = 0;
1122
    FactorySticker factory = FactorySticker.getInstance();
1123

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

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

    
1141
///////////////////////////////////////////////////////////////////////////////////////////////////
1142

    
1143
  void setObjectRatioNow(float sc)
1144
    {
1145
    mObjectScreenRatio = sc;
1146
    int nodeMinSize = Math.min(mNodeW,mNodeH);
1147
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeMinSize/mRealSize;
1148
    mObjectScale.set(scale,scale,scale);
1149
    }
1150

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

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

    
1157
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1158
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1159

    
1160
    setObjectRatioNow(mObjectScreenRatio);
1161
    }
1162

    
1163
///////////////////////////////////////////////////////////////////////////////////////////////////
1164

    
1165
  float getObjectRatio()
1166
    {
1167
    return mObjectScreenRatio*mInitScreenRatio;
1168
    }
1169

    
1170
///////////////////////////////////////////////////////////////////////////////////////////////////
1171

    
1172
  public float getRatio()
1173
    {
1174
    return mObjectScreenRatio;
1175
    }
1176

    
1177
///////////////////////////////////////////////////////////////////////////////////////////////////
1178

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

    
1186
    return false;
1187
    }
1188

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

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

    
1199
    point3D[0] *= multiplier;
1200
    point3D[1] *= multiplier;
1201
    point3D[2] *= multiplier;
1202

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

    
1213
    return currentBest;
1214
    }
1215

    
1216
///////////////////////////////////////////////////////////////////////////////////////////////////
1217

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

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

    
1227
    if( tmp!=0 ) return nearestAngle*tmp;
1228

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

    
1232
///////////////////////////////////////////////////////////////////////////////////////////////////
1233

    
1234
  float getCameraDist()
1235
    {
1236
    return mCameraDist;
1237
    }
1238

    
1239
///////////////////////////////////////////////////////////////////////////////////////////////////
1240
// INTERNAL API - those are called from 'effects' package
1241
///////////////////////////////////////////////////////////////////////////////////////////////////
1242

    
1243
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1244
    {
1245
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1246
    }
1247

    
1248
///////////////////////////////////////////////////////////////////////////////////////////////////
1249

    
1250
  public int getNodeWidth()
1251
    {
1252
    return mNodeW;
1253
    }
1254

    
1255
///////////////////////////////////////////////////////////////////////////////////////////////////
1256

    
1257
  public Static4D getRotationQuat()
1258
      {
1259
      return mQuat;
1260
      }
1261

    
1262
///////////////////////////////////////////////////////////////////////////////////////////////////
1263

    
1264
  public void apply(Effect effect, int position)
1265
    {
1266
    mEffects.apply(effect, position);
1267
    }
1268

    
1269
///////////////////////////////////////////////////////////////////////////////////////////////////
1270

    
1271
  public void remove(long effectID)
1272
    {
1273
    mEffects.abortById(effectID);
1274
    }
1275

    
1276
///////////////////////////////////////////////////////////////////////////////////////////////////
1277

    
1278
  public MeshBase getObjectMesh()
1279
    {
1280
    return mMesh;
1281
    }
1282

    
1283
///////////////////////////////////////////////////////////////////////////////////////////////////
1284

    
1285
  public DistortedEffects getObjectEffects()
1286
    {
1287
    return mEffects;
1288
    }
1289

    
1290
///////////////////////////////////////////////////////////////////////////////////////////////////
1291
// PUBLIC API
1292
///////////////////////////////////////////////////////////////////////////////////////////////////
1293

    
1294
  public int getCubitFaceColorIndex(int cubit, int face)
1295
    {
1296
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1297

    
1298
    int x = (int)(texMap.get0()/texMap.get2());
1299
    int y = (int)(texMap.get1()/texMap.get3());
1300

    
1301
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1302
    }
1303

    
1304
///////////////////////////////////////////////////////////////////////////////////////////////////
1305

    
1306
  public int[] getNumLayers()
1307
    {
1308
    return mNumLayers;
1309
    }
1310

    
1311
///////////////////////////////////////////////////////////////////////////////////////////////////
1312

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

    
1322
///////////////////////////////////////////////////////////////////////////////////////////////////
1323

    
1324
  public ObjectType getObjectType()
1325
    {
1326
    return intGetObjectType(mNumLayers);
1327
    }
1328

    
1329
///////////////////////////////////////////////////////////////////////////////////////////////////
1330

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

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