Project

General

Profile

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

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

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

    
20
package org.distorted.objectlib.main;
21

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

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

    
47
import org.distorted.objectlib.helpers.FactoryCubit;
48
import org.distorted.objectlib.helpers.FactorySticker;
49
import org.distorted.objectlib.helpers.ObjectLibInterface;
50
import org.distorted.objectlib.helpers.ObjectShape;
51
import org.distorted.objectlib.helpers.ObjectSticker;
52
import org.distorted.objectlib.helpers.ScrambleState;
53
import org.distorted.objectlib.json.JsonReader;
54

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

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

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

    
80
  public static final int TEXTURE_HEIGHT = 256;
81
  static final int NUM_STICKERS_IN_ROW = 4;
82

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

    
88
  private static final float MAX_SIZE_CHANGE = 1.35f;
89
  private static final float MIN_SIZE_CHANGE = 0.75f;
90

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

    
94
  protected int NUM_FACE_COLORS;
95
  protected int NUM_TEXTURES;
96
  protected Cubit[] CUBITS;
97

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

    
105
  private int mNumCubitFaces;
106
  private Static3D[] mAxis;
107
  private float[][] mCuts;
108
  private int[] mNumCuts;
109
  private float[][] mOrigPos;
110
  private Static4D mQuat;
111
  private final int[] mNumLayers;
112
  private final float mSize;
113
  private DistortedEffects mEffects;
114
  private VertexEffectRotate mRotateEffect;
115
  private Dynamic1D mRotationAngle;
116
  private Static3D mRotationAxis;
117
  private Static3D mObjectScale;
118
  private int[] mQuatDebug;
119
  private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
120
  private DistortedTexture mTexture;
121
  private float mInitScreenRatio;
122
  private int mSolvedFunctionIndex;
123
  private 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 mCurrentRotAxis;
131
  private MeshBase mMesh;
132
  private TwistyObjectScrambler mScrambler;
133
  private Movement mMovement;
134
  private boolean[][] mLayerRotatable;
135
  private int[][][] mEnabled;
136
  private DistortedNode mNode;
137
  private ObjectLibInterface mInterface;
138
  private Bitmap mBitmap;
139

    
140
  //////////////////// SOLVED1 ////////////////////////
141

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  TwistyObject(InputStream jsonStream, Static4D quat, Static3D move, float scale, InputStream meshStream)
149
    {
150
    JsonReader reader = new JsonReader();
151
    reader.parseJsonFile(jsonStream);
152
    setReader(reader);
153

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

    
179
    int numAxis = mAxis.length;
180
    SHIFT = -1;
181

    
182
    mCuts = getCuts(mNumLayers);
183
    mNumCuts = new int[numAxis];
184
    for(int i=0; i<numAxis; i++)
185
      {
186
      if( SHIFT<mNumLayers[i] ) SHIFT = mNumLayers[i];
187
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
188
      }
189

    
190
    OBJECT_QUATS = getQuats();
191
    NUM_CUBITS  = mOrigPos.length;
192
    NUM_FACE_COLORS = getNumFaceColors();
193
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
194
    NUM_AXIS = mAxis.length;
195
    NUM_QUATS = OBJECT_QUATS.length;
196

    
197
    int scramblingType = getScrambleType();
198
    ScrambleState[] states = getScrambleStates();
199
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,mNumLayers,states);
200

    
201
    boolean bandaged=false;
202

    
203
    for(int c=0; c<NUM_CUBITS; c++)
204
      {
205
      if( mOrigPos[c].length>3 )
206
        {
207
        bandaged=true;
208
        break;
209
        }
210
      }
211

    
212
    mIsBandaged = bandaged;
213
    mQuatDebug = new int[NUM_CUBITS];
214

    
215
    mRotationAngle= new Dynamic1D();
216
    mRotationAxis = new Static3D(1,0,0);
217
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
218

    
219
    mRotationAngleStatic = new Static1D(0);
220
    mRotationAngleMiddle = new Static1D(0);
221
    mRotationAngleFinal  = new Static1D(0);
222

    
223
    mObjectScale = new Static3D(scale,scale,scale);
224
    setObjectRatioNow(scale,720);
225

    
226
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
227
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(mQuat, CENTER);
228
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
229

    
230
    mNumTexCols = NUM_STICKERS_IN_ROW;
231
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
232

    
233
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
234

    
235
    CUBITS = new Cubit[NUM_CUBITS];
236
    createMeshAndCubits(stream);
237
    createDataStructuresForSolved(mNumLayers);
238

    
239
    mTexture = new DistortedTexture();
240
    mEffects = new DistortedEffects();
241

    
242
    setTexture();
243

    
244
    for(int q=0; q<NUM_QUATS; q++)
245
      {
246
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
247
      vq.setMeshAssociation(0,q);
248
      mEffects.apply(vq);
249
      }
250

    
251
    mEffects.apply(mRotateEffect);
252
    mEffects.apply(quatEffect);
253
    mEffects.apply(scaleEffect);
254
    mEffects.apply(moveEffect);
255

    
256
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  private Static3D getPos(float[] origPos)
262
    {
263
    int len = origPos.length/3;
264
    float sumX = 0.0f;
265
    float sumY = 0.0f;
266
    float sumZ = 0.0f;
267

    
268
    for(int i=0; i<len; i++)
269
      {
270
      sumX += origPos[3*i  ];
271
      sumY += origPos[3*i+1];
272
      sumZ += origPos[3*i+2];
273
      }
274

    
275
    sumX /= len;
276
    sumY /= len;
277
    sumZ /= len;
278

    
279
    return new Static3D(sumX,sumY,sumZ);
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  private void createMeshAndCubits(InputStream stream)
285
    {
286
    if( stream!=null )
287
      {
288
      DataInputStream dos = new DataInputStream(stream);
289
      mMesh = new MeshFile(dos);
290

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

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

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

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

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

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

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

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

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

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

    
351
    return mesh;
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

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

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

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

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

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

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

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

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

    
395
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
396
    {
397
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
398
    int variant = getCubitVariant(cubit,numLayers);
399

    
400
    for(int face=0; face<mNumCubitFaces; face++)
401
      {
402
      varColor = getVariantFaceColor(variant,face,numLayers);
403
      cubColor = getCubitFaceColor(cubit,face,numLayers);
404

    
405
      if( varColor>=0 && cubColor>=0 )
406
        {
407
        numNonBlack++;
408
        nonBlackIndex = cubColor;
409
        }
410
      }
411

    
412
    if( numNonBlack==0 ) return -1;
413
    if( numNonBlack>=2 ) return -2;
414

    
415
    return nonBlackIndex;
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  public boolean shouldResetTextureMaps()
421
    {
422
    return false;
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

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

    
436
    int len = quats.length;
437
    int place = 0;
438

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

    
446
           if( x!=0.0f ) { a = qx/x; }
447
      else if( y!=0.0f ) { a = qy/y; }
448
      else               { a = qz/z; }
449

    
450
      dx = a*x-qx;
451
      dy = a*y-qy;
452
      dz = a*z-qz;
453

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

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

    
467
    return null;
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

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

    
478
    for(int c=1; c<NUM_CUBITS; c++)
479
      {
480
      q1 = CUBITS[c].mQuatIndex;
481

    
482
      if( q1==q ) continue;
483

    
484
      skip = false;
485
      solved = mSolvedQuats[c];
486
      len = solved==null ? 0:solved.length;
487

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

    
497
      if( !skip ) return false;
498
      }
499

    
500
    return true;
501
    }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

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

    
512
    float x = result.get0();
513
    float y = result.get1();
514
    float z = result.get2();
515

    
516
    for(int c=0; c<NUM_CUBITS; c++)
517
      {
518
      float[] cent = mOrigPos[c];
519

    
520
      float qx = cent[0] - x;
521
      float qy = cent[1] - y;
522
      float qz = cent[2] - z;
523

    
524
      if( qx>-MAXDIFF && qx<MAXDIFF &&
525
          qy>-MAXDIFF && qy<MAXDIFF &&
526
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
527
      }
528

    
529
    return -1;
530
    }
531

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

    
539
  private boolean isSolved1()
540
    {
541
    if( mScramble==null )
542
      {
543
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
544
      mColors   = new int[NUM_CUBITS];
545

    
546
      for(int q=0; q<NUM_QUATS; q++)
547
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
548
      }
549

    
550
    if( mFaceMap==null )
551
      {
552
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
553
      }
554

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

    
561
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
562
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
563
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
564

    
565
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
566
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
567
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
568

    
569
    return false;
570
    }
571

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

    
590
  private boolean isSolved2()
591
    {
592
    int qX = CUBITS[0].mQuatIndex;
593
    int qY = CUBITS[1].mQuatIndex;
594
    int qZ = CUBITS[4].mQuatIndex;
595

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

    
603
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
604
    }
605

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

    
614
  private boolean isSolved3()
615
    {
616
    int index = CUBITS[0].mQuatIndex;
617

    
618
    if( CUBITS[1].mQuatIndex!=index ) return false;
619

    
620
    boolean solved = true;
621

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

    
631
    if( solved ) return true;
632

    
633
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
634
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
635

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

    
641
    return true;
642
    }
643

    
644
///////////////////////////////////////////////////////////////////////////////////////////////////
645

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

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

    
662
    return ret;
663
    }
664

    
665
///////////////////////////////////////////////////////////////////////////////////////////////////
666

    
667
  private int computeSingleRow(int axisIndex,float casted)
668
    {
669
    int num = mNumCuts[axisIndex];
670

    
671
    for(int i=0; i<num; i++)
672
      {
673
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
674
      }
675

    
676
    return (1<<num);
677
    }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680

    
681
  private boolean wasRotateApplied()
682
    {
683
    return mEffects.exists(mRotateEffect.getID());
684
    }
685

    
686
///////////////////////////////////////////////////////////////////////////////////////////////////
687

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

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

    
697
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
698
    {
699
    Static3D axis = mAxis[axisIndex];
700

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

    
707
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
708
    }
709

    
710
///////////////////////////////////////////////////////////////////////////////////////////////////
711

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

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

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

    
737
///////////////////////////////////////////////////////////////////////////////////////////////////
738

    
739
  public int getScrambleType()
740
    {
741
    return 0;
742
    }
743

    
744
///////////////////////////////////////////////////////////////////////////////////////////////////
745

    
746
  int computeBitmapFromRow(int rowBitmap, int axis)
747
    {
748
    if( mIsBandaged )
749
      {
750
      int bitmap, initBitmap=0;
751

    
752
      while( initBitmap!=rowBitmap )
753
        {
754
        initBitmap = rowBitmap;
755

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

    
764
    return rowBitmap;
765
    }
766

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

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

    
777
    float x = pos[offset  ];
778
    float y = pos[offset+1];
779
    float z = pos[offset+2];
780

    
781
    float xo,yo,zo;
782

    
783
    for(int i=0; i<NUM_CUBITS; i++)
784
      {
785
      int len = mOrigPos[i].length / 3;
786

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

    
793
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
794

    
795
        if( currError<minError )
796
          {
797
          minError = currError;
798
          minErrorIndex1 = i;
799
          minErrorIndex2 = j;
800
          }
801
        }
802
      }
803

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

    
812
///////////////////////////////////////////////////////////////////////////////////////////////////
813
// remember about the double cover or unit quaternions!
814

    
815
  int mulQuat(int q1, int q2)
816
    {
817
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
818

    
819
    float rX = result.get0();
820
    float rY = result.get1();
821
    float rZ = result.get2();
822
    float rW = result.get3();
823

    
824
    final float MAX_ERROR = 0.1f;
825
    float dX,dY,dZ,dW;
826

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

    
834
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
835
          dY<MAX_ERROR && dY>-MAX_ERROR &&
836
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
837
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
838

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

    
844
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
845
          dY<MAX_ERROR && dY>-MAX_ERROR &&
846
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
847
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
848
      }
849

    
850
    return -1;
851
    }
852

    
853
///////////////////////////////////////////////////////////////////////////////////////////////////
854

    
855
  private float getAngle()
856
    {
857
    int pointNum = mRotationAngle.getNumPoints();
858

    
859
    if( pointNum>=1 )
860
      {
861
      return mRotationAngle.getPoint(pointNum-1).get0();
862
      }
863
    else
864
      {
865
      mInterface.reportProblem("points in RotationAngle: "+pointNum);
866
      return 0;
867
      }
868
    }
869

    
870
///////////////////////////////////////////////////////////////////////////////////////////////////
871

    
872
  void setLibInterface(ObjectLibInterface inter)
873
    {
874
    mInterface = inter;
875
    }
876

    
877
///////////////////////////////////////////////////////////////////////////////////////////////////
878

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

    
885
///////////////////////////////////////////////////////////////////////////////////////////////////
886

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

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

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

    
909
///////////////////////////////////////////////////////////////////////////////////////////////////
910

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

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

    
928
      return mRotateEffect.getID();
929
      }
930

    
931
    return 0;
932
    }
933

    
934
///////////////////////////////////////////////////////////////////////////////////////////////////
935

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

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

    
952
      return mRotateEffect.getID();
953
      }
954

    
955
    return 0;
956
    }
957

    
958
///////////////////////////////////////////////////////////////////////////////////////////////////
959

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

    
965
///////////////////////////////////////////////////////////////////////////////////////////////////
966

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

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

    
988
///////////////////////////////////////////////////////////////////////////////////////////////////
989

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

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

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

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

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

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

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

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

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

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

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

    
1044
///////////////////////////////////////////////////////////////////////////////////////////////////
1045

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

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

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

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

    
1075
///////////////////////////////////////////////////////////////////////////////////////////////////
1076

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

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

    
1085
  private void createTexture()
1086
    {
1087
    Paint paint = new Paint();
1088
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1089
    Canvas canvas = new Canvas(mBitmap);
1090

    
1091
    paint.setAntiAlias(true);
1092
    paint.setTextAlign(Paint.Align.CENTER);
1093
    paint.setStyle(Paint.Style.FILL);
1094

    
1095
    paint.setColor(COLOR_BLACK);
1096
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1097

    
1098
    int texture = 0;
1099
    FactorySticker factory = FactorySticker.getInstance();
1100

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

    
1112
///////////////////////////////////////////////////////////////////////////////////////////////////
1113

    
1114
  void setTexture()
1115
    {
1116
    if( mBitmap==null ) createTexture();
1117

    
1118
    if( !mTexture.setTexture(mBitmap) )
1119
      {
1120
      int max = DistortedLibrary.getMaxTextureSize();
1121
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max);
1122
      }
1123
    }
1124

    
1125
///////////////////////////////////////////////////////////////////////////////////////////////////
1126

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

    
1134
///////////////////////////////////////////////////////////////////////////////////////////////////
1135

    
1136
  void setObjectRatio(float sizeChange, int nodeSize)
1137
    {
1138
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1139

    
1140
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1141
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1142

    
1143
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1144
    }
1145

    
1146
///////////////////////////////////////////////////////////////////////////////////////////////////
1147

    
1148
  void setNodeSize(int nodeSize)
1149
    {
1150
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1151
    }
1152

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

    
1155
  float getObjectRatio()
1156
    {
1157
    return mObjectScreenRatio*mInitScreenRatio;
1158
    }
1159

    
1160
///////////////////////////////////////////////////////////////////////////////////////////////////
1161

    
1162
  public float getRatio()
1163
    {
1164
    return mObjectScreenRatio;
1165
    }
1166

    
1167
///////////////////////////////////////////////////////////////////////////////////////////////////
1168

    
1169
  boolean isSolved()
1170
    {
1171
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1172
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1173
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1174
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1175

    
1176
    return false;
1177
    }
1178

    
1179
///////////////////////////////////////////////////////////////////////////////////////////////////
1180
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1181
// during setting up the initial position in the solver.
1182

    
1183
  int getCubit(float[] point3D)
1184
    {
1185
    float dist, minDist = Float.MAX_VALUE;
1186
    int currentBest=-1;
1187
    float multiplier = mNumLayers[0];
1188

    
1189
    point3D[0] *= multiplier;
1190
    point3D[1] *= multiplier;
1191
    point3D[2] *= multiplier;
1192

    
1193
    for(int i=0; i<NUM_CUBITS; i++)
1194
      {
1195
      dist = CUBITS[i].getDistSquared(point3D);
1196
      if( dist<minDist )
1197
        {
1198
        minDist = dist;
1199
        currentBest = i;
1200
        }
1201
      }
1202

    
1203
    return currentBest;
1204
    }
1205

    
1206
///////////////////////////////////////////////////////////////////////////////////////////////////
1207

    
1208
  int computeNearestAngle(int axis, float angle, float speed)
1209
    {
1210
    int[] basicArray = getBasicAngle();
1211
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1212
    int nearestAngle = 360/basicAngle;
1213

    
1214
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1215
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1216

    
1217
    if( tmp!=0 ) return nearestAngle*tmp;
1218

    
1219
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1220
    }
1221

    
1222
///////////////////////////////////////////////////////////////////////////////////////////////////
1223
// INTERNAL API - those are called from 'effects' package
1224
///////////////////////////////////////////////////////////////////////////////////////////////////
1225

    
1226
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1227
    {
1228
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1229
    }
1230

    
1231
///////////////////////////////////////////////////////////////////////////////////////////////////
1232

    
1233
  public Static4D getRotationQuat()
1234
    {
1235
    return mQuat;
1236
    }
1237

    
1238
///////////////////////////////////////////////////////////////////////////////////////////////////
1239

    
1240
  public float getSize()
1241
    {
1242
    return mSize;
1243
    }
1244

    
1245
///////////////////////////////////////////////////////////////////////////////////////////////////
1246

    
1247
  public void apply(Effect effect, int position)
1248
    {
1249
    mEffects.apply(effect, position);
1250
    }
1251

    
1252
///////////////////////////////////////////////////////////////////////////////////////////////////
1253

    
1254
  public void remove(long effectID)
1255
    {
1256
    mEffects.abortById(effectID);
1257
    }
1258

    
1259
///////////////////////////////////////////////////////////////////////////////////////////////////
1260

    
1261
  public MeshBase getObjectMesh()
1262
    {
1263
    return mMesh;
1264
    }
1265

    
1266
///////////////////////////////////////////////////////////////////////////////////////////////////
1267

    
1268
  public DistortedEffects getObjectEffects()
1269
    {
1270
    return mEffects;
1271
    }
1272

    
1273
///////////////////////////////////////////////////////////////////////////////////////////////////
1274
// PUBLIC API
1275
///////////////////////////////////////////////////////////////////////////////////////////////////
1276

    
1277
  public int getCubitFaceColorIndex(int cubit, int face)
1278
    {
1279
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1280

    
1281
    int x = (int)(texMap.get0()/texMap.get2());
1282
    int y = (int)(texMap.get1()/texMap.get3());
1283

    
1284
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1285
    }
1286

    
1287
///////////////////////////////////////////////////////////////////////////////////////////////////
1288

    
1289
  public int[] getNumLayers()
1290
    {
1291
    return mNumLayers;
1292
    }
1293

    
1294
///////////////////////////////////////////////////////////////////////////////////////////////////
1295

    
1296
  public synchronized void solve()
1297
    {
1298
    for(int i=0; i<NUM_CUBITS; i++)
1299
      {
1300
      CUBITS[i].solve();
1301
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1302
      }
1303
    }
1304

    
1305
///////////////////////////////////////////////////////////////////////////////////////////////////
1306

    
1307
  public Bitmap getStickerBitmap()
1308
    {
1309
    return mBitmap;
1310
    }
1311

    
1312
///////////////////////////////////////////////////////////////////////////////////////////////////
1313

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

    
1319
///////////////////////////////////////////////////////////////////////////////////////////////////
1320

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

    
1326
///////////////////////////////////////////////////////////////////////////////////////////////////
1327

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

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

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

    
1358
///////////////////////////////////////////////////////////////////////////////////////////////////
1359

    
1360
  public String getShortName()
1361
    {
1362
    return getObjectType().name();
1363
    }
1364

    
1365
///////////////////////////////////////////////////////////////////////////////////////////////////
1366

    
1367
  protected void setReader(JsonReader reader)
1368
    {
1369
    // empty
1370
    }
1371

    
1372
///////////////////////////////////////////////////////////////////////////////////////////////////
1373

    
1374
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1375

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

    
1401
  // not only for JSON
1402
  public abstract Static3D[] getRotationAxis();
1403
  public abstract int[] getBasicAngle();
1404
  public abstract int getNumFaces();
1405
  public abstract String getObjectName();
1406
  public abstract String getInventor();
1407
  public abstract int getYearOfInvention();
1408
  public abstract int getComplexity();
1409
  public abstract int getFOV();
1410
  }
(14-14/18)