Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 23afe4c4

1 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 a706f8e0 Leszek Koltunski
import org.distorted.library.main.QuatHelper;
38 29b82486 Leszek Koltunski
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 a57e6870 Leszek Koltunski
47 198c5bf0 Leszek Koltunski
import org.distorted.objectlib.helpers.FactoryCubit;
48
import org.distorted.objectlib.helpers.FactorySticker;
49 d887aa16 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectLibInterface;
50 198c5bf0 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectShape;
51
import org.distorted.objectlib.helpers.ObjectSticker;
52
import org.distorted.objectlib.helpers.ScrambleState;
53 82eb152a Leszek Koltunski
import org.distorted.objectlib.json.JsonReader;
54 23afe4c4 Leszek Koltunski
import org.distorted.objectlib.movement.Movement;
55
import org.distorted.objectlib.movement.MovementCuboids;
56
import org.distorted.objectlib.movement.MovementDodecahedron;
57
import org.distorted.objectlib.movement.MovementHexahedron;
58
import org.distorted.objectlib.movement.MovementOctahedron;
59
import org.distorted.objectlib.movement.MovementTetrahedron;
60 29b82486 Leszek Koltunski
61
import java.io.DataInputStream;
62
import java.io.IOException;
63
import java.io.InputStream;
64
import java.util.Random;
65
66 23afe4c4 Leszek Koltunski
import static org.distorted.objectlib.movement.Movement.MOVEMENT_TETRAHEDRON;
67
import static org.distorted.objectlib.movement.Movement.MOVEMENT_HEXAHEDRON;
68
import static org.distorted.objectlib.movement.Movement.MOVEMENT_OCTAHEDRON;
69
import static org.distorted.objectlib.movement.Movement.MOVEMENT_DODECAHEDRON;
70
import static org.distorted.objectlib.movement.Movement.MOVEMENT_SHAPECHANGE;
71 59c20632 Leszek Koltunski
72 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 7ba38dd4 Leszek Koltunski
public abstract class TwistyObject
75 29b82486 Leszek Koltunski
  {
76
  public static final int COLOR_YELLOW = 0xffffff00;
77
  public static final int COLOR_WHITE  = 0xffffffff;
78
  public static final int COLOR_BLUE   = 0xff0000ff;
79
  public static final int COLOR_GREEN  = 0xff00bb00;
80
  public static final int COLOR_RED    = 0xff990000;
81
  public static final int COLOR_ORANGE = 0xffff6200;
82
  public static final int COLOR_GREY   = 0xff727c7b;
83
  public static final int COLOR_VIOLET = 0xff7700bb;
84
  public static final int COLOR_BLACK  = 0xff000000;
85
86
  public static final int TEXTURE_HEIGHT = 256;
87
  static final int NUM_STICKERS_IN_ROW = 4;
88
89
  public static final float SQ2 = (float)Math.sqrt(2);
90
  public static final float SQ3 = (float)Math.sqrt(3);
91
  public static final float SQ5 = (float)Math.sqrt(5);
92
  public static final float SQ6 = (float)Math.sqrt(6);
93
94
  private static final float MAX_SIZE_CHANGE = 1.35f;
95
  private static final float MIN_SIZE_CHANGE = 0.75f;
96
97
  private static final Static3D CENTER = new Static3D(0,0,0);
98
  private static final int POST_ROTATION_MILLISEC = 500;
99
100 82eb152a Leszek Koltunski
  protected int NUM_FACE_COLORS;
101
  protected int NUM_TEXTURES;
102
  protected Cubit[] CUBITS;
103 29b82486 Leszek Koltunski
104
  MeshBase[] mMeshes;
105 82eb152a Leszek Koltunski
  Static4D[] OBJECT_QUATS;
106
  int NUM_CUBITS;
107
  int NUM_AXIS;
108
  int NUM_QUATS;
109 332e1fb0 Leszek Koltunski
  int SHIFT;
110 82eb152a Leszek Koltunski
111
  private int mNumCubitFaces;
112
  private Static3D[] mAxis;
113
  private float[][] mCuts;
114
  private int[] mNumCuts;
115
  private float[][] mOrigPos;
116
  private Static4D mQuat;
117 a57e6870 Leszek Koltunski
  private final int[] mNumLayers;
118 59c20632 Leszek Koltunski
  private final float mSize;
119 82eb152a Leszek Koltunski
  private DistortedEffects mEffects;
120
  private VertexEffectRotate mRotateEffect;
121
  private Dynamic1D mRotationAngle;
122
  private Static3D mRotationAxis;
123
  private Static3D mObjectScale;
124
  private int[] mQuatDebug;
125
  private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
126
  private DistortedTexture mTexture;
127
  private float mInitScreenRatio;
128
  private int mSolvedFunctionIndex;
129
  private boolean mIsBandaged;
130 29b82486 Leszek Koltunski
  private float mObjectScreenRatio;
131
  private int[][] mSolvedQuats;
132
  private int[][] mQuatMult;
133
  private int[] mTmpQuats;
134
  private int mNumTexRows, mNumTexCols;
135
  private int mRotRowBitmap;
136 59c20632 Leszek Koltunski
  private int mCurrentRotAxis;
137 29b82486 Leszek Koltunski
  private MeshBase mMesh;
138 82eb152a Leszek Koltunski
  private TwistyObjectScrambler mScrambler;
139 59c20632 Leszek Koltunski
  private Movement mMovement;
140
  private boolean[][] mLayerRotatable;
141
  private int[][][] mEnabled;
142 7ba38dd4 Leszek Koltunski
  private DistortedNode mNode;
143 d887aa16 Leszek Koltunski
  private ObjectLibInterface mInterface;
144 d5c71d02 Leszek Koltunski
  private Bitmap mBitmap;
145 29b82486 Leszek Koltunski
146
  //////////////////// SOLVED1 ////////////////////////
147
148
  private int[] mFaceMap;
149
  private int[][] mScramble;
150
  private int[] mColors;
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154 64c209f5 Leszek Koltunski
  TwistyObject(InputStream jsonStream, Static4D quat, Static3D move, float scale, InputStream meshStream)
155 82eb152a Leszek Koltunski
    {
156 e1a86bf2 Leszek Koltunski
    JsonReader reader = new JsonReader();
157 82eb152a Leszek Koltunski
    reader.parseJsonFile(jsonStream);
158
    setReader(reader);
159
160
    mNumLayers = reader.getNumLayers();
161
    mSize      = reader.getSize();
162 64c209f5 Leszek Koltunski
    initialize(quat,move,scale,meshStream);
163 82eb152a Leszek Koltunski
    }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167 64c209f5 Leszek Koltunski
  TwistyObject(int[] numLayers, float size, Static4D quat, Static3D move, float scale, InputStream meshStream)
168 29b82486 Leszek Koltunski
    {
169
    mNumLayers = numLayers;
170 82eb152a Leszek Koltunski
    mSize      = size;
171 64c209f5 Leszek Koltunski
    initialize(quat,move,scale,meshStream);
172 82eb152a Leszek Koltunski
    }
173
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
176 64c209f5 Leszek Koltunski
  private void initialize(Static4D quat, Static3D move, float scale, InputStream stream)
177 82eb152a Leszek Koltunski
    {
178
    mQuat = quat;
179 29b82486 Leszek Koltunski
    mOrigPos = getCubitPositions(mNumLayers);
180
    mAxis = getRotationAxis();
181
    mInitScreenRatio = getScreenRatio();
182
    mNumCubitFaces = getNumCubitFaces();
183
    mSolvedFunctionIndex = getSolvedFunctionIndex();
184
185 332e1fb0 Leszek Koltunski
    int numAxis = mAxis.length;
186
    SHIFT = -1;
187
188 29b82486 Leszek Koltunski
    mCuts = getCuts(mNumLayers);
189 332e1fb0 Leszek Koltunski
    mNumCuts = new int[numAxis];
190
    for(int i=0; i<numAxis; i++)
191 dfdb26a9 Leszek Koltunski
      {
192 332e1fb0 Leszek Koltunski
      if( SHIFT<mNumLayers[i] ) SHIFT = mNumLayers[i];
193 dfdb26a9 Leszek Koltunski
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
194
      }
195 29b82486 Leszek Koltunski
196
    OBJECT_QUATS = getQuats();
197
    NUM_CUBITS  = mOrigPos.length;
198
    NUM_FACE_COLORS = getNumFaceColors();
199
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
200
    NUM_AXIS = mAxis.length;
201
    NUM_QUATS = OBJECT_QUATS.length;
202
203
    int scramblingType = getScrambleType();
204
    ScrambleState[] states = getScrambleStates();
205 82eb152a Leszek Koltunski
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,mNumLayers,states);
206 29b82486 Leszek Koltunski
207
    boolean bandaged=false;
208
209
    for(int c=0; c<NUM_CUBITS; c++)
210
      {
211
      if( mOrigPos[c].length>3 )
212
        {
213
        bandaged=true;
214
        break;
215
        }
216
      }
217
218
    mIsBandaged = bandaged;
219
    mQuatDebug = new int[NUM_CUBITS];
220
221
    mRotationAngle= new Dynamic1D();
222
    mRotationAxis = new Static3D(1,0,0);
223
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
224
225
    mRotationAngleStatic = new Static1D(0);
226
    mRotationAngleMiddle = new Static1D(0);
227
    mRotationAngleFinal  = new Static1D(0);
228
229 64c209f5 Leszek Koltunski
    mObjectScale = new Static3D(scale,scale,scale);
230
    setObjectRatioNow(scale,720);
231 e7daa161 Leszek Koltunski
232 29b82486 Leszek Koltunski
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
233 82eb152a Leszek Koltunski
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(mQuat, CENTER);
234 b80e6524 Leszek Koltunski
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
235 29b82486 Leszek Koltunski
236
    mNumTexCols = NUM_STICKERS_IN_ROW;
237
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
238
239
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
240
241
    CUBITS = new Cubit[NUM_CUBITS];
242 82eb152a Leszek Koltunski
    createMeshAndCubits(stream);
243
    createDataStructuresForSolved(mNumLayers);
244 29b82486 Leszek Koltunski
245
    mTexture = new DistortedTexture();
246
    mEffects = new DistortedEffects();
247
248 d5c71d02 Leszek Koltunski
    setTexture();
249 9a316d92 Leszek Koltunski
250 29b82486 Leszek Koltunski
    for(int q=0; q<NUM_QUATS; q++)
251
      {
252
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
253
      vq.setMeshAssociation(0,q);
254
      mEffects.apply(vq);
255
      }
256
257
    mEffects.apply(mRotateEffect);
258
    mEffects.apply(quatEffect);
259
    mEffects.apply(scaleEffect);
260 b80e6524 Leszek Koltunski
    mEffects.apply(moveEffect);
261 29b82486 Leszek Koltunski
262 7ba38dd4 Leszek Koltunski
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
263 29b82486 Leszek Koltunski
    }
264
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
267
  private Static3D getPos(float[] origPos)
268
    {
269
    int len = origPos.length/3;
270
    float sumX = 0.0f;
271
    float sumY = 0.0f;
272
    float sumZ = 0.0f;
273
274
    for(int i=0; i<len; i++)
275
      {
276
      sumX += origPos[3*i  ];
277
      sumY += origPos[3*i+1];
278
      sumZ += origPos[3*i+2];
279
      }
280
281
    sumX /= len;
282
    sumY /= len;
283
    sumZ /= len;
284
285
    return new Static3D(sumX,sumY,sumZ);
286
    }
287
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290 82eb152a Leszek Koltunski
  private void createMeshAndCubits(InputStream stream)
291 29b82486 Leszek Koltunski
    {
292 82eb152a Leszek Koltunski
    if( stream!=null )
293 29b82486 Leszek Koltunski
      {
294 82eb152a Leszek Koltunski
      DataInputStream dos = new DataInputStream(stream);
295 29b82486 Leszek Koltunski
      mMesh = new MeshFile(dos);
296
297
      try
298
        {
299 82eb152a Leszek Koltunski
        stream.close();
300 29b82486 Leszek Koltunski
        }
301
      catch(IOException e)
302
        {
303
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
304
        }
305
306
      for(int i=0; i<NUM_CUBITS; i++)
307
        {
308
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
309
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
310
        }
311
312
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
313
      }
314
    else
315
      {
316
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
317
318
      for(int i=0; i<NUM_CUBITS; i++)
319
        {
320
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
321
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
322
        Static3D pos = getPos(mOrigPos[i]);
323
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
324
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
325
        }
326
327
      mMesh = new MeshJoined(cubitMesh);
328
      resetAllTextureMaps();
329
      }
330
    }
331
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333
334 a57e6870 Leszek Koltunski
  private MeshBase createCubitMesh(int cubit, int[] numLayers)
335 29b82486 Leszek Koltunski
    {
336
    int variant = getCubitVariant(cubit,numLayers);
337
338
    if( mMeshes==null )
339
      {
340
      FactoryCubit factory = FactoryCubit.getInstance();
341
      factory.clear();
342
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
343
      }
344
345
    if( mMeshes[variant]==null )
346
      {
347 e30c522a Leszek Koltunski
      ObjectShape shape = getObjectShape(variant);
348 29b82486 Leszek Koltunski
      FactoryCubit factory = FactoryCubit.getInstance();
349
      factory.createNewFaceTransform(shape);
350
      mMeshes[variant] = factory.createRoundedSolid(shape);
351
      }
352
353
    MeshBase mesh = mMeshes[variant].copy(true);
354
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
355
    mesh.apply(quat,0xffffffff,0);
356
357
    return mesh;
358
    }
359
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
362 a57e6870 Leszek Koltunski
  private void createDataStructuresForSolved(int[] numLayers)
363 29b82486 Leszek Koltunski
    {
364
    mTmpQuats = new int[NUM_QUATS];
365
    mSolvedQuats = new int[NUM_CUBITS][];
366
367
    for(int c=0; c<NUM_CUBITS; c++)
368
      {
369
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
370
      }
371
    }
372
373 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
374
375
  private int getMultQuat(int index1, int index2)
376
    {
377
    if( mQuatMult==null )
378
      {
379
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
380
381
      for(int i=0; i<NUM_QUATS; i++)
382
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
383
      }
384
385
    if( mQuatMult[index1][index2]==-1 )
386
      {
387
      mQuatMult[index1][index2] = mulQuat(index1,index2);
388
      }
389
390
    return mQuatMult[index1][index2];
391
    }
392
393 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
394
// This is used to build internal data structures for the generic 'isSolved()'
395
//
396
// if this is an internal cubit (all faces black): return -1
397
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
398
// Color index, i.e. the index into the 'FACE_COLORS' table.
399
// else (edge or corner cubit, more than one non-black face): return -2.
400
401 a57e6870 Leszek Koltunski
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
402 29b82486 Leszek Koltunski
    {
403 a75ae1ee Leszek Koltunski
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
404
    int variant = getCubitVariant(cubit,numLayers);
405 29b82486 Leszek Koltunski
406
    for(int face=0; face<mNumCubitFaces; face++)
407
      {
408 a75ae1ee Leszek Koltunski
      varColor = getVariantFaceColor(variant,face,numLayers);
409
      cubColor = getCubitFaceColor(cubit,face,numLayers);
410 29b82486 Leszek Koltunski
411 a75ae1ee Leszek Koltunski
      if( varColor>=0 && cubColor>=0 )
412 29b82486 Leszek Koltunski
        {
413
        numNonBlack++;
414 a75ae1ee Leszek Koltunski
        nonBlackIndex = cubColor;
415 29b82486 Leszek Koltunski
        }
416
      }
417
418
    if( numNonBlack==0 ) return -1;
419
    if( numNonBlack>=2 ) return -2;
420
421
    return nonBlackIndex;
422
    }
423
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425
426 0f72365b Leszek Koltunski
  public boolean shouldResetTextureMaps()
427 29b82486 Leszek Koltunski
    {
428
    return false;
429
    }
430
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432
433 c8bc83d9 Leszek Koltunski
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
434 29b82486 Leszek Koltunski
    {
435
    final float MAXD = 0.0001f;
436
    float x = faceAx.get0();
437
    float y = faceAx.get1();
438
    float z = faceAx.get2();
439
    float a,dx,dy,dz,qx,qy,qz;
440
    Static4D quat;
441
442
    int len = quats.length;
443
    int place = 0;
444
445
    for(int q=1; q<len; q++)
446
      {
447
      quat = quats[q];
448
      qx = quat.get0();
449
      qy = quat.get1();
450
      qz = quat.get2();
451
452
           if( x!=0.0f ) { a = qx/x; }
453
      else if( y!=0.0f ) { a = qy/y; }
454
      else               { a = qz/z; }
455
456
      dx = a*x-qx;
457
      dy = a*y-qy;
458
      dz = a*z-qz;
459
460
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
461
        {
462
        mTmpQuats[place++] = q;
463
        }
464
      }
465
466
    if( place!=0 )
467
      {
468
      int[] ret = new int[place];
469
      System.arraycopy(mTmpQuats,0,ret,0,place);
470
      return ret;
471
      }
472
473
    return null;
474
    }
475
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477
478 c8bc83d9 Leszek Koltunski
  private boolean isSolved0()
479 29b82486 Leszek Koltunski
    {
480
    int len, q1,q = CUBITS[0].mQuatIndex;
481
    int[] solved;
482
    boolean skip;
483
484
    for(int c=1; c<NUM_CUBITS; c++)
485
      {
486
      q1 = CUBITS[c].mQuatIndex;
487
488
      if( q1==q ) continue;
489
490
      skip = false;
491
      solved = mSolvedQuats[c];
492
      len = solved==null ? 0:solved.length;
493
494
      for(int i=0; i<len; i++)
495
        {
496
        if( q1==getMultQuat(q,solved[i]) )
497
          {
498
          skip = true;
499
          break;
500
          }
501
        }
502
503
      if( !skip ) return false;
504
      }
505
506
    return true;
507
    }
508
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510
511
  private int computeScramble(int quatNum, int centerNum)
512
    {
513
    float MAXDIFF = 0.01f;
514
    float[] center= mOrigPos[centerNum];
515
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
516
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
517
518
    float x = result.get0();
519
    float y = result.get1();
520
    float z = result.get2();
521
522
    for(int c=0; c<NUM_CUBITS; c++)
523
      {
524
      float[] cent = mOrigPos[c];
525
526
      float qx = cent[0] - x;
527
      float qy = cent[1] - y;
528
      float qz = cent[2] - z;
529
530
      if( qx>-MAXDIFF && qx<MAXDIFF &&
531
          qy>-MAXDIFF && qy<MAXDIFF &&
532
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
533
      }
534
535
    return -1;
536
    }
537
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539
// Dino4 uses this. It is solved if and only if groups of cubits
540
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
541
// or
542
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
543
// are all the same color.
544
545 c8bc83d9 Leszek Koltunski
  private boolean isSolved1()
546 29b82486 Leszek Koltunski
    {
547
    if( mScramble==null )
548
      {
549
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
550
      mColors   = new int[NUM_CUBITS];
551
552
      for(int q=0; q<NUM_QUATS; q++)
553
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
554
      }
555
556
    if( mFaceMap==null )
557
      {
558
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
559
      }
560
561
    for(int c=0; c<NUM_CUBITS; c++)
562
      {
563
      int index = mScramble[CUBITS[c].mQuatIndex][c];
564
      mColors[index] = mFaceMap[c];
565
      }
566
567
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
568
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
569
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
570
571
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
572
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
573
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
574
575
    return false;
576
    }
577
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579
// Dino6 uses this. It is solved if and only if:
580
//
581
// All four 'X' cubits (i.e. those whose longest edge goes along the X axis) are rotated
582
// by the same quaternion qX, similarly all four 'Y' cubits by the same qY and all four 'Z'
583
// by the same qZ, and then either:
584
//
585
// a) qX = qY = qZ
586
// b) qY = qX*Q2 and qZ = qX*Q8  (i.e. swap of WHITE and YELLOW faces)
587
// c) qX = qY*Q2 and qZ = qY*Q10 (i.e. swap of BLUE and GREEN faces)
588
// d) qX = qZ*Q8 and qY = qZ*Q10 (i.e. swap of RED and BROWN faces)
589
//
590
// BUT: cases b), c) and d) are really the same - it's all just a mirror image of the original.
591
//
592
// X cubits: 0, 2, 8, 10
593
// Y cubits: 1, 3, 9, 11
594
// Z cubits: 4, 5, 6, 7
595
596 c8bc83d9 Leszek Koltunski
  private boolean isSolved2()
597 29b82486 Leszek Koltunski
    {
598
    int qX = CUBITS[0].mQuatIndex;
599
    int qY = CUBITS[1].mQuatIndex;
600
    int qZ = CUBITS[4].mQuatIndex;
601
602
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
603
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
604
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
605
      {
606
      return false;
607
      }
608
609
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
610
    }
611
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613
// Square-2 is solved iff
614
// a) all of its cubits are rotated with the same quat
615
// b) its two 'middle' cubits are rotated with the same quat, the 6 'front' and 6 'back'
616
// edges and corners with this quat multiplied by QUATS[18] (i.e. those are upside down)
617
// and all the 12 left and right edges and corners also with the same quat multiplied by
618
// QUATS[12] - i.e. also upside down.
619
620 c8bc83d9 Leszek Koltunski
  private boolean isSolved3()
621 29b82486 Leszek Koltunski
    {
622
    int index = CUBITS[0].mQuatIndex;
623
624
    if( CUBITS[1].mQuatIndex!=index ) return false;
625
626
    boolean solved = true;
627
628
    for(int i=2; i<NUM_CUBITS; i++)
629
      {
630
      if( CUBITS[i].mQuatIndex!=index )
631
        {
632
        solved = false;
633
        break;
634
        }
635
      }
636
637
    if( solved ) return true;
638
639
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
640
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
641
642
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
643
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
644
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
645
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
646
647
    return true;
648
    }
649
650
///////////////////////////////////////////////////////////////////////////////////////////////////
651
652
  int computeRow(float[] pos, int axisIndex)
653
    {
654
    int ret=0;
655
    int len = pos.length / 3;
656
    Static3D axis = mAxis[axisIndex];
657
    float axisX = axis.get0();
658
    float axisY = axis.get1();
659
    float axisZ = axis.get2();
660
    float casted;
661
662
    for(int i=0; i<len; i++)
663
      {
664
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
665
      ret |= computeSingleRow(axisIndex,casted);
666
      }
667
668
    return ret;
669
    }
670
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672
673
  private int computeSingleRow(int axisIndex,float casted)
674
    {
675
    int num = mNumCuts[axisIndex];
676
677
    for(int i=0; i<num; i++)
678
      {
679
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
680
      }
681
682
    return (1<<num);
683
    }
684
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686
687
  private boolean wasRotateApplied()
688
    {
689
    return mEffects.exists(mRotateEffect.getID());
690
    }
691
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693
694
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
695
    {
696
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
697
    }
698
699
///////////////////////////////////////////////////////////////////////////////////////////////////
700
// note the minus in front of the sin() - we rotate counterclockwise
701
// when looking towards the direction where the axis increases in values.
702
703
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
704
    {
705
    Static3D axis = mAxis[axisIndex];
706
707
    while( angleInDegrees<0 ) angleInDegrees += 360;
708
    angleInDegrees %= 360;
709
    
710
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
711
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
712
713
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
714
    }
715
716
///////////////////////////////////////////////////////////////////////////////////////////////////
717
718
  private synchronized void setupPosition(int[][] moves)
719
    {
720
    if( moves!=null )
721
      {
722
      Static4D quat;
723
      int index, axis, rowBitmap, angle;
724
      int[] basic = getBasicAngle();
725
726
      for(int[] move: moves)
727
        {
728
        axis     = move[0];
729
        rowBitmap= move[1];
730
        angle    = move[2]*(360/basic[axis]);
731
        quat     = makeQuaternion(axis,angle);
732
733
        for(int j=0; j<NUM_CUBITS; j++)
734
          if( belongsToRotation(j,axis,rowBitmap) )
735
            {
736
            index = CUBITS[j].removeRotationNow(quat);
737
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
738
            }
739
        }
740
      }
741
    }
742
743
///////////////////////////////////////////////////////////////////////////////////////////////////
744
745 f9a81f52 Leszek Koltunski
  public int getScrambleType()
746 29b82486 Leszek Koltunski
    {
747
    return 0;
748
    }
749
750
///////////////////////////////////////////////////////////////////////////////////////////////////
751
752
  int computeBitmapFromRow(int rowBitmap, int axis)
753
    {
754
    if( mIsBandaged )
755
      {
756
      int bitmap, initBitmap=0;
757
758
      while( initBitmap!=rowBitmap )
759
        {
760
        initBitmap = rowBitmap;
761
762
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
763
          {
764
          bitmap = CUBITS[cubit].getRotRow(axis);
765
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
766
          }
767
        }
768
      }
769
770
    return rowBitmap;
771
    }
772
773
///////////////////////////////////////////////////////////////////////////////////////////////////
774
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
775
// Do so only if minimal Error is appropriately low (shape-shifting puzzles - Square-1)
776
777
  void clampPos(float[] pos, int offset)
778
    {
779
    float currError, minError = Float.MAX_VALUE;
780
    int minErrorIndex1 = -1;
781
    int minErrorIndex2 = -1;
782
783
    float x = pos[offset  ];
784
    float y = pos[offset+1];
785
    float z = pos[offset+2];
786
787
    float xo,yo,zo;
788
789
    for(int i=0; i<NUM_CUBITS; i++)
790
      {
791
      int len = mOrigPos[i].length / 3;
792
793
      for(int j=0; j<len; j++)
794
        {
795
        xo = mOrigPos[i][3*j  ];
796
        yo = mOrigPos[i][3*j+1];
797
        zo = mOrigPos[i][3*j+2];
798
799
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
800
801
        if( currError<minError )
802
          {
803
          minError = currError;
804
          minErrorIndex1 = i;
805
          minErrorIndex2 = j;
806
          }
807
        }
808
      }
809
810
    if( minError< 0.1f ) // TODO: 0.1 ?
811
      {
812
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
813
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
814
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
815
      }
816
    }
817
818
///////////////////////////////////////////////////////////////////////////////////////////////////
819
// remember about the double cover or unit quaternions!
820
821
  int mulQuat(int q1, int q2)
822
    {
823
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
824
825
    float rX = result.get0();
826
    float rY = result.get1();
827
    float rZ = result.get2();
828
    float rW = result.get3();
829
830
    final float MAX_ERROR = 0.1f;
831
    float dX,dY,dZ,dW;
832
833
    for(int i=0; i<NUM_QUATS; i++)
834
      {
835
      dX = OBJECT_QUATS[i].get0() - rX;
836
      dY = OBJECT_QUATS[i].get1() - rY;
837
      dZ = OBJECT_QUATS[i].get2() - rZ;
838
      dW = OBJECT_QUATS[i].get3() - rW;
839
840
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
841
          dY<MAX_ERROR && dY>-MAX_ERROR &&
842
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
843
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
844
845
      dX = OBJECT_QUATS[i].get0() + rX;
846
      dY = OBJECT_QUATS[i].get1() + rY;
847
      dZ = OBJECT_QUATS[i].get2() + rZ;
848
      dW = OBJECT_QUATS[i].get3() + rW;
849
850
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
851
          dY<MAX_ERROR && dY>-MAX_ERROR &&
852
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
853
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
854
      }
855
856
    return -1;
857
    }
858
859 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
860
861
  private float getAngle()
862
    {
863
    int pointNum = mRotationAngle.getNumPoints();
864
865
    if( pointNum>=1 )
866
      {
867
      return mRotationAngle.getPoint(pointNum-1).get0();
868
      }
869
    else
870
      {
871 d887aa16 Leszek Koltunski
      mInterface.reportProblem("points in RotationAngle: "+pointNum);
872 c8bc83d9 Leszek Koltunski
      return 0;
873
      }
874
    }
875
876 d887aa16 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
877
878
  void setLibInterface(ObjectLibInterface inter)
879
    {
880
    mInterface = inter;
881 c8bc83d9 Leszek Koltunski
    }
882
883 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
884 198c5bf0 Leszek Koltunski
885 880beeea Leszek Koltunski
  void initializeObject(int[][] moves)
886 7c111294 Leszek Koltunski
    {
887
    solve();
888
    setupPosition(moves);
889
    }
890 198c5bf0 Leszek Koltunski
891 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
892 198c5bf0 Leszek Koltunski
893 880beeea Leszek Koltunski
  synchronized void removeRotationNow()
894 7c111294 Leszek Koltunski
    {
895
    float angle = getAngle();
896
    double nearestAngleInRadians = angle*Math.PI/180;
897
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
898
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
899 59c20632 Leszek Koltunski
    float axisX = mAxis[mCurrentRotAxis].get0();
900
    float axisY = mAxis[mCurrentRotAxis].get1();
901
    float axisZ = mAxis[mCurrentRotAxis].get2();
902 7c111294 Leszek Koltunski
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
903 198c5bf0 Leszek Koltunski
904 7c111294 Leszek Koltunski
    mRotationAngle.removeAll();
905
    mRotationAngleStatic.set0(0);
906
907
    for(int i=0; i<NUM_CUBITS; i++)
908 59c20632 Leszek Koltunski
      if( belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap) )
909 198c5bf0 Leszek Koltunski
        {
910 7c111294 Leszek Koltunski
        int index = CUBITS[i].removeRotationNow(quat);
911
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
912 198c5bf0 Leszek Koltunski
        }
913
    }
914
915 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
916
917 880beeea Leszek Koltunski
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
918 c8bc83d9 Leszek Koltunski
    {
919 7c111294 Leszek Koltunski
    if( wasRotateApplied() )
920
      {
921
      float angle = getAngle();
922
      mRotationAngleStatic.set0(angle);
923
      mRotationAngleFinal.set0(nearestAngleInDegrees);
924
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
925 c8bc83d9 Leszek Koltunski
926 7c111294 Leszek Koltunski
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
927
      mRotationAngle.resetToBeginning();
928
      mRotationAngle.removeAll();
929
      mRotationAngle.add(mRotationAngleStatic);
930
      mRotationAngle.add(mRotationAngleMiddle);
931
      mRotationAngle.add(mRotationAngleFinal);
932
      mRotateEffect.notifyWhenFinished(listener);
933 c8bc83d9 Leszek Koltunski
934 7c111294 Leszek Koltunski
      return mRotateEffect.getID();
935
      }
936 c8bc83d9 Leszek Koltunski
937 7c111294 Leszek Koltunski
    return 0;
938 c8bc83d9 Leszek Koltunski
    }
939
940
///////////////////////////////////////////////////////////////////////////////////////////////////
941
942 880beeea Leszek Koltunski
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
943 c8bc83d9 Leszek Koltunski
    {
944 7c111294 Leszek Koltunski
    if( wasRotateApplied() )
945
      {
946 59c20632 Leszek Koltunski
      mCurrentRotAxis = axis;
947 7c111294 Leszek Koltunski
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
948 c8bc83d9 Leszek Koltunski
949 7c111294 Leszek Koltunski
      mRotationAngleStatic.set0(0.0f);
950
      mRotationAxis.set( mAxis[axis] );
951
      mRotationAngle.setDuration(durationMillis);
952
      mRotationAngle.resetToBeginning();
953
      mRotationAngle.add(new Static1D(0));
954
      mRotationAngle.add(new Static1D(angle));
955 332e1fb0 Leszek Koltunski
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
956 7c111294 Leszek Koltunski
      mRotateEffect.notifyWhenFinished(listener);
957 c8bc83d9 Leszek Koltunski
958 7c111294 Leszek Koltunski
      return mRotateEffect.getID();
959
      }
960
961
    return 0;
962 c8bc83d9 Leszek Koltunski
    }
963
964 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
965
966 880beeea Leszek Koltunski
  void continueRotation(float angleInDegrees)
967
    {
968
    mRotationAngleStatic.set0(angleInDegrees);
969
    }
970
971
///////////////////////////////////////////////////////////////////////////////////////////////////
972
973
  synchronized void beginNewRotation(int axis, int row )
974
    {
975
    if( axis<0 || axis>=NUM_AXIS )
976
      {
977
      android.util.Log.e("object", "invalid rotation axis: "+axis);
978
      return;
979
      }
980 a57e6870 Leszek Koltunski
    if( row<0 || row>=mNumLayers[axis] )
981 880beeea Leszek Koltunski
      {
982
      android.util.Log.e("object", "invalid rotation row: "+row);
983
      return;
984
      }
985
986 59c20632 Leszek Koltunski
    mCurrentRotAxis = axis;
987 880beeea Leszek Koltunski
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
988
    mRotationAngleStatic.set0(0.0f);
989
    mRotationAxis.set( mAxis[axis] );
990
    mRotationAngle.add(mRotationAngleStatic);
991 332e1fb0 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
992 880beeea Leszek Koltunski
    }
993
994
///////////////////////////////////////////////////////////////////////////////////////////////////
995
996
  void setTextureMap(int cubit, int face, int newColor)
997 29b82486 Leszek Koltunski
    {
998 7c111294 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
999
    final float ratioH = 1.0f/mNumTexRows;
1000
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1001
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1002
    int col = newColor%mNumTexCols;
1003
1004
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1005
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1006 29b82486 Leszek Koltunski
    }
1007
1008
///////////////////////////////////////////////////////////////////////////////////////////////////
1009
1010 880beeea Leszek Koltunski
  void resetAllTextureMaps()
1011 29b82486 Leszek Koltunski
    {
1012 7c111294 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
1013
    final float ratioH = 1.0f/mNumTexRows;
1014 a75ae1ee Leszek Koltunski
    int cubColor, varColor, color, variant, row, col;
1015 29b82486 Leszek Koltunski
1016 7c111294 Leszek Koltunski
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1017 29b82486 Leszek Koltunski
      {
1018 7c111294 Leszek Koltunski
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1019 a75ae1ee Leszek Koltunski
      variant = getCubitVariant(cubit,mNumLayers);
1020 29b82486 Leszek Koltunski
1021 a75ae1ee Leszek Koltunski
      for(int face=0; face<mNumCubitFaces; face++)
1022 7c111294 Leszek Koltunski
        {
1023 a75ae1ee Leszek Koltunski
        cubColor = getCubitFaceColor(cubit,face,mNumLayers);
1024
        varColor = getVariantFaceColor(variant,face,mNumLayers);
1025
        color    = cubColor<0 || varColor<0 ? NUM_TEXTURES : varColor*NUM_FACE_COLORS + cubColor;
1026
        row      = (mNumTexRows-1) - color/mNumTexCols;
1027
        col      = color%mNumTexCols;
1028
1029
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1030 7c111294 Leszek Koltunski
        }
1031 29b82486 Leszek Koltunski
1032 7c111294 Leszek Koltunski
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1033
      }
1034 29b82486 Leszek Koltunski
    }
1035
1036
///////////////////////////////////////////////////////////////////////////////////////////////////
1037
1038 880beeea Leszek Koltunski
  void releaseResources()
1039 29b82486 Leszek Koltunski
    {
1040 7c111294 Leszek Koltunski
    mTexture.markForDeletion();
1041
    mMesh.markForDeletion();
1042
    mEffects.markForDeletion();
1043
1044
    for(int j=0; j<NUM_CUBITS; j++)
1045
      {
1046
      CUBITS[j].releaseResources();
1047
      }
1048 29b82486 Leszek Koltunski
    }
1049
1050
///////////////////////////////////////////////////////////////////////////////////////////////////
1051
1052 880beeea Leszek Koltunski
  synchronized void restorePreferences(SharedPreferences preferences)
1053 29b82486 Leszek Koltunski
    {
1054
    boolean error = false;
1055
1056
    for(int i=0; i<NUM_CUBITS; i++)
1057
      {
1058
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1059
1060
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1061
        {
1062
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1063
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1064
        }
1065
      else
1066
        {
1067
        error = true;
1068
        }
1069
      }
1070
1071
    if( error )
1072
      {
1073
      for(int i=0; i<NUM_CUBITS; i++)
1074
        {
1075
        CUBITS[i].solve();
1076
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1077
        }
1078
      }
1079
    }
1080
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082
1083 880beeea Leszek Koltunski
  void savePreferences(SharedPreferences.Editor editor)
1084 29b82486 Leszek Koltunski
    {
1085 7c111294 Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1086 29b82486 Leszek Koltunski
    }
1087
1088
///////////////////////////////////////////////////////////////////////////////////////////////////
1089 7c111294 Leszek Koltunski
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1090 29b82486 Leszek Koltunski
1091 d5c71d02 Leszek Koltunski
  private void createTexture()
1092 29b82486 Leszek Koltunski
    {
1093 7c111294 Leszek Koltunski
    Paint paint = new Paint();
1094 d5c71d02 Leszek Koltunski
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1095
    Canvas canvas = new Canvas(mBitmap);
1096 29b82486 Leszek Koltunski
1097 7c111294 Leszek Koltunski
    paint.setAntiAlias(true);
1098
    paint.setTextAlign(Paint.Align.CENTER);
1099
    paint.setStyle(Paint.Style.FILL);
1100
1101
    paint.setColor(COLOR_BLACK);
1102
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1103
1104 43a4ccff Leszek Koltunski
    int texture = 0;
1105 7c111294 Leszek Koltunski
    FactorySticker factory = FactorySticker.getInstance();
1106
1107
    for(int row=0; row<mNumTexRows; row++)
1108
      for(int col=0; col<mNumTexCols; col++)
1109 29b82486 Leszek Koltunski
        {
1110 43a4ccff Leszek Koltunski
        if( texture>=NUM_TEXTURES ) break;
1111
        ObjectSticker sticker = retSticker(texture/NUM_FACE_COLORS);
1112
        int color = getColor(texture%NUM_FACE_COLORS);
1113
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, color, sticker);
1114
        texture++;
1115 29b82486 Leszek Koltunski
        }
1116 d5c71d02 Leszek Koltunski
    }
1117
1118
///////////////////////////////////////////////////////////////////////////////////////////////////
1119
1120
  void setTexture()
1121
    {
1122
    if( mBitmap==null ) createTexture();
1123 29b82486 Leszek Koltunski
1124 d5c71d02 Leszek Koltunski
    if( !mTexture.setTexture(mBitmap) )
1125 7c111294 Leszek Koltunski
      {
1126
      int max = DistortedLibrary.getMaxTextureSize();
1127 d5c71d02 Leszek Koltunski
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max);
1128 29b82486 Leszek Koltunski
      }
1129
    }
1130
1131 ee6bb8d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1132
1133 45e0065d Leszek Koltunski
  void setObjectRatioNow(float sc, int nodeSize)
1134 ee6bb8d7 Leszek Koltunski
    {
1135 64c209f5 Leszek Koltunski
    mObjectScreenRatio = sc;
1136 45e0065d Leszek Koltunski
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1137 ee6bb8d7 Leszek Koltunski
    mObjectScale.set(scale,scale,scale);
1138 23afe4c4 Leszek Koltunski
1139
    if( mMovement==null ) mMovement = getMovement();
1140
    mMovement.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1141 ee6bb8d7 Leszek Koltunski
    }
1142
1143 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1144
1145 45e0065d Leszek Koltunski
  void setObjectRatio(float sizeChange, int nodeSize)
1146 29b82486 Leszek Koltunski
    {
1147 7c111294 Leszek Koltunski
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1148 29b82486 Leszek Koltunski
1149 7c111294 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1150
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1151
1152 45e0065d Leszek Koltunski
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1153
    }
1154
1155
///////////////////////////////////////////////////////////////////////////////////////////////////
1156
1157
  void setNodeSize(int nodeSize)
1158
    {
1159
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1160 29b82486 Leszek Koltunski
    }
1161
1162 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1163
1164
  public float getRatio()
1165
    {
1166
    return mObjectScreenRatio;
1167
    }
1168
1169 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1170 29b82486 Leszek Koltunski
1171 880beeea Leszek Koltunski
  boolean isSolved()
1172 7c111294 Leszek Koltunski
    {
1173 880beeea Leszek Koltunski
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1174
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1175
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1176
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1177 7c111294 Leszek Koltunski
1178 880beeea Leszek Koltunski
    return false;
1179 29b82486 Leszek Koltunski
    }
1180
1181
///////////////////////////////////////////////////////////////////////////////////////////////////
1182 a57e6870 Leszek Koltunski
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1183
// during setting up the initial position in the solver.
1184 29b82486 Leszek Koltunski
1185 880beeea Leszek Koltunski
  int getCubit(float[] point3D)
1186 29b82486 Leszek Koltunski
    {
1187 880beeea Leszek Koltunski
    float dist, minDist = Float.MAX_VALUE;
1188
    int currentBest=-1;
1189 a57e6870 Leszek Koltunski
    float multiplier = mNumLayers[0];
1190 880beeea Leszek Koltunski
1191
    point3D[0] *= multiplier;
1192
    point3D[1] *= multiplier;
1193
    point3D[2] *= multiplier;
1194
1195
    for(int i=0; i<NUM_CUBITS; i++)
1196
      {
1197
      dist = CUBITS[i].getDistSquared(point3D);
1198
      if( dist<minDist )
1199
        {
1200
        minDist = dist;
1201
        currentBest = i;
1202
        }
1203
      }
1204
1205
    return currentBest;
1206 7c111294 Leszek Koltunski
    }
1207 29b82486 Leszek Koltunski
1208 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1209 29b82486 Leszek Koltunski
1210 880beeea Leszek Koltunski
  int computeNearestAngle(int axis, float angle, float speed)
1211 7c111294 Leszek Koltunski
    {
1212 880beeea Leszek Koltunski
    int[] basicArray = getBasicAngle();
1213
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1214
    int nearestAngle = 360/basicAngle;
1215
1216
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1217
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1218
1219
    if( tmp!=0 ) return nearestAngle*tmp;
1220
1221
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1222 7c111294 Leszek Koltunski
    }
1223 29b82486 Leszek Koltunski
1224 880beeea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1225
// INTERNAL API - those are called from 'effects' package
1226 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1227
1228 880beeea Leszek Koltunski
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1229 29b82486 Leszek Koltunski
    {
1230 880beeea Leszek Koltunski
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1231 7c111294 Leszek Koltunski
    }
1232 29b82486 Leszek Koltunski
1233
///////////////////////////////////////////////////////////////////////////////////////////////////
1234
1235 880beeea Leszek Koltunski
  public Static4D getRotationQuat()
1236 59c20632 Leszek Koltunski
    {
1237
    return mQuat;
1238
    }
1239
1240
///////////////////////////////////////////////////////////////////////////////////////////////////
1241
1242
  public float getSize()
1243
    {
1244
    return mSize;
1245
    }
1246 7c111294 Leszek Koltunski
1247 2df35810 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1248
1249
  public void apply(Effect effect, int position)
1250
    {
1251
    mEffects.apply(effect, position);
1252
    }
1253
1254
///////////////////////////////////////////////////////////////////////////////////////////////////
1255
1256
  public void remove(long effectID)
1257
    {
1258
    mEffects.abortById(effectID);
1259
    }
1260
1261 3e9df6aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1262
1263 758b028d Leszek Koltunski
  public MeshBase getObjectMesh()
1264 3e9df6aa Leszek Koltunski
    {
1265
    return mMesh;
1266
    }
1267
1268 758b028d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1269
1270
  public DistortedEffects getObjectEffects()
1271
    {
1272
    return mEffects;
1273
    }
1274
1275 880beeea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1276
// PUBLIC API
1277 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1278
1279 880beeea Leszek Koltunski
  public int getCubitFaceColorIndex(int cubit, int face)
1280 29b82486 Leszek Koltunski
    {
1281 880beeea Leszek Koltunski
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1282 29b82486 Leszek Koltunski
1283 880beeea Leszek Koltunski
    int x = (int)(texMap.get0()/texMap.get2());
1284
    int y = (int)(texMap.get1()/texMap.get3());
1285 29b82486 Leszek Koltunski
1286 880beeea Leszek Koltunski
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1287 29b82486 Leszek Koltunski
    }
1288
1289
///////////////////////////////////////////////////////////////////////////////////////////////////
1290
1291 a57e6870 Leszek Koltunski
  public int[] getNumLayers()
1292 29b82486 Leszek Koltunski
    {
1293 880beeea Leszek Koltunski
    return mNumLayers;
1294
    }
1295 29b82486 Leszek Koltunski
1296
///////////////////////////////////////////////////////////////////////////////////////////////////
1297
1298 880beeea Leszek Koltunski
  public synchronized void solve()
1299 29b82486 Leszek Koltunski
    {
1300 880beeea Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
1301
      {
1302
      CUBITS[i].solve();
1303
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1304
      }
1305 29b82486 Leszek Koltunski
    }
1306
1307 d5c71d02 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1308
1309
  public Bitmap getStickerBitmap()
1310
    {
1311
    return mBitmap;
1312
    }
1313
1314 7ba38dd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1315
1316
  public DistortedNode getNode()
1317
    {
1318
    return mNode;
1319
    }
1320
1321 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1322
1323 c8bc83d9 Leszek Koltunski
  public ObjectType getObjectType()
1324 29b82486 Leszek Koltunski
    {
1325 c8bc83d9 Leszek Koltunski
    return intGetObjectType(mNumLayers);
1326 29b82486 Leszek Koltunski
    }
1327
1328 59c20632 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1329
1330
  public Movement getMovement()
1331
    {
1332
    if( mMovement==null )
1333
      {
1334
      int[] numLayers = getNumLayers();
1335
      if( mCuts==null ) getCuts(numLayers);
1336
      if( mLayerRotatable==null ) mLayerRotatable = getLayerRotatable(numLayers);
1337
      if( mEnabled==null ) mEnabled = getEnabled();
1338
1339
      int movementType = getMovementType();
1340
      int movementSplit= getMovementSplit();
1341
1342
      switch(movementType)
1343
        {
1344 f86b282a Leszek Koltunski
        case MOVEMENT_TETRAHEDRON : mMovement = new MovementTetrahedron(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1345 59c20632 Leszek Koltunski
                                    break;
1346 f86b282a Leszek Koltunski
        case MOVEMENT_HEXAHEDRON  : mMovement = new MovementHexahedron(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1347 59c20632 Leszek Koltunski
                                    break;
1348 f86b282a Leszek Koltunski
        case MOVEMENT_OCTAHEDRON  : mMovement = new MovementOctahedron(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1349 59c20632 Leszek Koltunski
                                    break;
1350 f86b282a Leszek Koltunski
        case MOVEMENT_DODECAHEDRON: mMovement = new MovementDodecahedron(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1351 59c20632 Leszek Koltunski
                                    break;
1352
        case MOVEMENT_SHAPECHANGE : float[] dist3D = getDist3D(numLayers);
1353 f86b282a Leszek Koltunski
                                    mMovement = new MovementCuboids(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled,dist3D);
1354 59c20632 Leszek Koltunski
                                    break;
1355
        }
1356
      }
1357
    return mMovement;
1358
    }
1359
1360 e1a86bf2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1361
1362
  public String getShortName()
1363
    {
1364
    return getObjectType().name();
1365
    }
1366
1367 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1368
1369 82eb152a Leszek Koltunski
  protected void setReader(JsonReader reader)
1370
    {
1371
    // empty
1372
    }
1373
1374
///////////////////////////////////////////////////////////////////////////////////////////////////
1375
1376 a57e6870 Leszek Koltunski
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1377 59c20632 Leszek Koltunski
1378
  // for JSON only
1379
  public abstract int getSolvedFunctionIndex();
1380
  public abstract int getMovementType();
1381
  public abstract int getMovementSplit();
1382
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1383
  public abstract int[][][] getEnabled();
1384
  public abstract float[] getDist3D(int[] numLayers);
1385 f9a81f52 Leszek Koltunski
  public abstract ScrambleState[] getScrambleStates();
1386 7bbfc84f Leszek Koltunski
  public abstract float[][] getCuts(int[] numLayers);
1387 1bb09f88 Leszek Koltunski
  public abstract Static4D[] getQuats();
1388
  public abstract int getNumStickerTypes(int[] numLayers);
1389
  public abstract ObjectSticker retSticker(int sticker);
1390 e30c522a Leszek Koltunski
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1391
  public abstract ObjectShape getObjectShape(int variant);
1392
  public abstract int getNumCubitVariants(int[] numLayers);
1393 7b832206 Leszek Koltunski
  public abstract float[][] getCubitPositions(int[] numLayers);
1394
  public abstract Static4D getQuat(int cubit, int[] numLayers);
1395
  public abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1396 a75ae1ee Leszek Koltunski
  public abstract int getCubitFaceColor(int cubit, int face, int[] numLayers);
1397
  public abstract int getVariantFaceColor(int variant, int face, int[] numLayers);
1398
  public abstract int getNumFaceColors();
1399
  public abstract int getNumCubitFaces();
1400 82eb152a Leszek Koltunski
  public abstract float getScreenRatio();
1401
  public abstract int getColor(int face);
1402 7b832206 Leszek Koltunski
1403 a75ae1ee Leszek Koltunski
  // not only for JSON
1404 29b82486 Leszek Koltunski
  public abstract Static3D[] getRotationAxis();
1405
  public abstract int[] getBasicAngle();
1406 2289cab1 Leszek Koltunski
  public abstract int getNumFaces();
1407 e26eb4e7 Leszek Koltunski
  public abstract String getObjectName();
1408
  public abstract String getInventor();
1409
  public abstract int getYearOfInvention();
1410
  public abstract int getComplexity();
1411 7ba38dd4 Leszek Koltunski
  public abstract int getFOV();
1412 29b82486 Leszek Koltunski
  }