Project

General

Profile

Download (18.2 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ aec5cc91

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.objects;
21

    
22
import android.content.res.Resources;
23

    
24
import org.distorted.helpers.ObjectShape;
25
import org.distorted.helpers.ObjectSticker;
26
import org.distorted.helpers.ScrambleState;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29
import org.distorted.library.mesh.MeshSquare;
30
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32
import org.distorted.main.R;
33

    
34
import java.util.Random;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class TwistyDiamond extends TwistyObject
39
{
40
  // the four rotation axis of a Diamond. Must be normalized.
41
  static final Static3D[] ROT_AXIS = new Static3D[]
42
         {
43
           new Static3D(+SQ6/3,+SQ3/3,     0),
44
           new Static3D(-SQ6/3,+SQ3/3,     0),
45
           new Static3D(     0,-SQ3/3,-SQ6/3),
46
           new Static3D(     0,-SQ3/3,+SQ6/3)
47
         };
48

    
49
  private static final int[] FACE_COLORS = new int[]
50
         {
51
           COLOR_ORANGE, COLOR_VIOLET,
52
           COLOR_WHITE , COLOR_BLUE  ,
53
           COLOR_YELLOW, COLOR_RED   ,
54
           COLOR_GREEN , COLOR_GREY
55
         };
56

    
57
  private static final float DIST = 0.50f;
58
  private static final int FACES_PER_CUBIT =8;
59

    
60
  private int mCurrState;
61
  private int mIndexExcluded;
62
  private final ScrambleState[] mStates;
63
  private int[][] mScrambleTable;
64
  private int[] mNumOccurences;
65
  private int[] mBasicAngle;
66
  private int[] mFaceMap;
67
  private Static4D[] mQuats;
68
  private int[] mTetraToFaceMap;
69
  private ObjectSticker[] mStickers;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  TwistyDiamond(int size, Static4D quat, DistortedTexture texture,
74
                MeshSquare mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
75
    {
76
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.DIAM, res, scrWidth);
77

    
78
    int[] tmp = new int[3*2*size];
79

    
80
    for(int i=0; i<2*size; i++)
81
      {
82
      tmp[3*i  ] = (i<size) ?  i:i-size;
83
      tmp[3*i+1] = (i%2==0) ? -1:1;
84
      tmp[3*i+2] = 0;
85
      }
86

    
87
    mStates = new ScrambleState[]
88
      {
89
      new ScrambleState( new int[][] {tmp,tmp,tmp,tmp} )
90
      };
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  private void initializeQuats()
96
    {
97
    mQuats = new Static4D[]
98
         {
99
          new Static4D(  0.0f,  0.0f,   0.0f,  1.0f ),
100
          new Static4D(  0.0f,  1.0f,   0.0f,  0.0f ),
101
          new Static4D(+SQ2/2,  0.0f, -SQ2/2,  0.0f ),
102
          new Static4D(-SQ2/2,  0.0f, -SQ2/2,  0.0f ),
103

    
104
          new Static4D(+SQ2/2,  0.5f,   0.0f,  0.5f ),
105
          new Static4D(-SQ2/2,  0.5f,   0.0f,  0.5f ),
106
          new Static4D(  0.0f,  0.5f, +SQ2/2,  0.5f ),
107
          new Static4D(  0.0f,  0.5f, -SQ2/2,  0.5f ),
108
          new Static4D(+SQ2/2,  0.5f,   0.0f, -0.5f ),
109
          new Static4D(-SQ2/2,  0.5f,   0.0f, -0.5f ),
110
          new Static4D(  0.0f,  0.5f, +SQ2/2, -0.5f ),
111
          new Static4D(  0.0f,  0.5f, -SQ2/2, -0.5f )
112
         };
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  int[] getSolvedQuats(int cubit, int numLayers)
118
    {
119
    if( mQuats==null ) initializeQuats();
120
    if( mFaceMap==null ) mFaceMap = new int[] {4,0,6,2,7,3,5,1};
121
    int status = retCubitSolvedStatus(cubit,numLayers);
122
    return status<0 ? null : buildSolvedQuats(MovementDiamond.FACE_AXIS[mFaceMap[status]],mQuats);
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  float getScreenRatio()
128
    {
129
    return 0.65f;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  Static4D[] getQuats()
135
    {
136
    if( mQuats==null ) initializeQuats();
137
    return mQuats;
138
    }
139

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

    
142
  int getNumFaces()
143
    {
144
    return FACE_COLORS.length;
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  boolean shouldResetTextureMaps()
150
    {
151
    return false;
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  int getSolvedFunctionIndex()
157
    {
158
    return 0;
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  int getNumStickerTypes(int numLayers)
164
    {
165
    return 1;
166
    }
167

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

    
170
  float[][] getCuts(int numLayers)
171
    {
172
    if( numLayers<2 )
173
      {
174
      return null;
175
      }
176
    else
177
      {
178
      float[][] cuts = new float[4][numLayers-1];
179
      float dist = SQ6*0.666f*DIST;
180
      float cut  = 0.5f*dist*(2-numLayers);
181

    
182
      for(int i=0; i<numLayers-1; i++)
183
        {
184
        cuts[0][i] = cut;
185
        cuts[1][i] = cut;
186
        cuts[2][i] = cut;
187
        cuts[3][i] = cut;
188
        cut += dist;
189
        }
190

    
191
      return cuts;
192
      }
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  int getNumCubitFaces()
198
    {
199
    return FACES_PER_CUBIT;
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  private int getNumOctahedrons(int layers)
205
    {
206
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  private int getNumTetrahedrons(int layers)
212
    {
213
    return 4*layers*(layers-1);
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
219
    {
220
    float x = DIST*(layers-1);
221
    float z = DIST*(layers+1);
222

    
223
    for(int i=0; i<layers; i++, index++)
224
      {
225
      z -= 2*DIST;
226
      centers[index][0] = x;
227
      centers[index][1] = height;
228
      centers[index][2] = z;
229
      }
230

    
231
    for(int i=0; i<layers-1; i++, index++)
232
      {
233
      x -= 2*DIST;
234
      centers[index][0] = x;
235
      centers[index][1] = height;
236
      centers[index][2] = z;
237
      }
238

    
239
    for(int i=0; i<layers-1; i++, index++)
240
      {
241
      z += 2*DIST;
242
      centers[index][0] = x;
243
      centers[index][1] = height;
244
      centers[index][2] = z;
245
      }
246

    
247
    for(int i=0; i<layers-2; i++, index++)
248
      {
249
      x += 2*DIST;
250
      centers[index][0] = x;
251
      centers[index][1] = height;
252
      centers[index][2] = z;
253
      }
254

    
255
    return index;
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
261
    {
262
    float x = DIST*(layers-1);
263
    float z = DIST*layers;
264

    
265
    for(int i=0; i<layers-1; i++, index++)
266
      {
267
      z -= 2*DIST;
268
      centers[index][0] = x;
269
      centers[index][1] = height;
270
      centers[index][2] = z;
271
      }
272

    
273
    x += DIST;
274
    z -= DIST;
275

    
276
    for(int i=0; i<layers-1; i++, index++)
277
      {
278
      x -= 2*DIST;
279
      centers[index][0] = x;
280
      centers[index][1] = height;
281
      centers[index][2] = z;
282
      }
283

    
284
    x -= DIST;
285
    z -= DIST;
286

    
287
    for(int i=0; i<layers-1; i++, index++)
288
      {
289
      z += 2*DIST;
290
      centers[index][0] = x;
291
      centers[index][1] = height;
292
      centers[index][2] = z;
293
      }
294

    
295
    x -= DIST;
296
    z += DIST;
297

    
298
    for(int i=0; i<layers-1; i++, index++)
299
      {
300
      x += 2*DIST;
301
      centers[index][0] = x;
302
      centers[index][1] = height;
303
      centers[index][2] = z;
304
      }
305

    
306
    return index;
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  float[][] getCubitPositions(int layers)
312
    {
313
    int numO = getNumOctahedrons(layers);
314
    int numT = getNumTetrahedrons(layers);
315
    int index = 0;
316
    float height = 0.0f;
317

    
318
    float[][] CENTERS = new float[numO+numT][3];
319

    
320
    index = createOctaPositions(CENTERS,index,layers,height);
321

    
322
    for(int i=layers-1; i>0; i--)
323
      {
324
      height += SQ2*DIST;
325
      index = createOctaPositions(CENTERS,index,i,+height);
326
      index = createOctaPositions(CENTERS,index,i,-height);
327
      }
328

    
329
    height = DIST*SQ2/2;
330

    
331
    for(int i=layers; i>1; i--)
332
      {
333
      index = createTetraPositions(CENTERS,index,i,+height);
334
      index = createTetraPositions(CENTERS,index,i,-height);
335
      height += SQ2*DIST;
336
      }
337

    
338
    return CENTERS;
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
344
    {
345
    if( mTetraToFaceMap==null ) mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
346

    
347
    for(int i=numLayers-1; i>0; i--)
348
      {
349
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
350
      tetra -= 8*i;
351
      }
352

    
353
    return -1;
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  ObjectShape getObjectShape(int cubit, int numLayers)
359
    {
360
    int variant = getCubitVariant(cubit,numLayers);
361
    int N = numLayers>3 ? 5:6;
362
    int E = numLayers>2 ? (numLayers>3 ? 0:1):2;
363

    
364
    if( variant==0 )
365
      {
366
      double[][] vertices = new double[][]
367
          {
368
             { 0.5,   0.0, 0.5},
369
             { 0.5,   0.0,-0.5},
370
             {-0.5,   0.0,-0.5},
371
             {-0.5,   0.0, 0.5},
372
             { 0.0, SQ2/2, 0.0},
373
             { 0.0,-SQ2/2, 0.0}
374
          };
375

    
376
      int[][] vert_indices = new int[][]
377
          {
378
             {3,0,4},
379
             {0,1,4},
380
             {1,2,4},
381
             {2,3,4},
382
             {5,0,3},
383
             {5,1,0},
384
             {5,2,1},
385
             {5,3,2}
386
          };
387

    
388
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
389
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
390
      float[][] corners   = new float[][] { {0.04f,0.20f} };
391
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
392
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
393
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
394
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
395
      }
396
    else
397
      {
398
      double[][] vertices = new double[][] { {-0.5, SQ2/4, 0.0}, { 0.5, SQ2/4, 0.0}, { 0.0,-SQ2/4, 0.5}, { 0.0,-SQ2/4,-0.5} };
399
      int[][] vert_indices = new int[][]  { {2,1,0}, {2,3,1}, {3,2,0}, {3,0,1} };
400
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
401
      int[] bandIndices   = new int[] { 0,0,0,0 };
402
      float[][] corners   = new float[][] { {0.08f,0.15f} };
403
      int[] cornerIndices = new int[] { 0,0,0,0 };
404
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
405
      int[] centerIndices = new int[] { 0,0,0,0 };
406
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
407
      }
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  Static4D getQuat(int cubit, int numLayers)
413
    {
414
    if( mQuats==null ) initializeQuats();
415
    int numO = getNumOctahedrons(numLayers);
416

    
417
    if( cubit<numO ) return mQuats[0];
418

    
419
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
420
      {
421
      case 0: return mQuats[0];                         // unit quat
422
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
423
      case 2: return mQuats[1];                         // 180 along Y
424
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
425
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
426
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
427
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
428
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
429
      }
430

    
431
    return null;
432
    }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

    
436
  int getNumCubitVariants(int numLayers)
437
    {
438
    return 2;
439
    }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

    
443
  int getCubitVariant(int cubit, int numLayers)
444
    {
445
    return cubit<getNumOctahedrons(numLayers) ? 0 : 1;
446
    }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

    
450
  int getFaceColor(int cubit, int cubitface, int size)
451
    {
452
    int numO = getNumOctahedrons(size);
453

    
454
    if( cubit<numO )
455
      {
456
      int axis = 0;
457
      int layer= 1;
458

    
459
      switch(cubitface)
460
        {
461
        case 0: axis = 2; layer =             1; break;
462
        case 1: axis = 0; layer = (1<<(size-1)); break;
463
        case 2: axis = 3; layer =             1; break;
464
        case 3: axis = 1; layer = (1<<(size-1)); break;
465
        case 4: axis = 3; layer = (1<<(size-1)); break;
466
        case 5: axis = 1; layer =             1; break;
467
        case 6: axis = 2; layer = (1<<(size-1)); break;
468
        case 7: axis = 0; layer =             1; break;
469
        }
470

    
471
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
472
      }
473
    else
474
      {
475
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
476
      }
477
    }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

    
481
  int getColor(int face)
482
    {
483
    return FACE_COLORS[face];
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  ObjectSticker retSticker(int face)
489
    {
490
    if( mStickers==null )
491
      {
492
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
493
      float radius = 0.06f;
494
      float stroke = 0.07f;
495
      float[] radii = new float[] {radius,radius,radius};
496
      mStickers     = new ObjectSticker[STICKERS.length];
497
      mStickers[0]  = new ObjectSticker(STICKERS[0],null,radii,stroke);
498
      }
499

    
500
    return mStickers[face/NUM_FACES];
501
    }
502

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

    
505
  float returnMultiplier()
506
    {
507
    return 1.5f;
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
  private void initializeScrambling()
513
    {
514
    int numLayers = getNumLayers();
515

    
516
    if( mScrambleTable ==null )
517
      {
518
      mScrambleTable = new int[NUM_AXIS][numLayers];
519
      }
520
    if( mNumOccurences ==null )
521
      {
522
      int max=0;
523

    
524
      for (ScrambleState mState : mStates)
525
        {
526
        int tmp = mState.getTotal(-1);
527
        if (max < tmp) max = tmp;
528
        }
529

    
530
      mNumOccurences = new int[max];
531
      }
532

    
533
    for(int i=0; i<NUM_AXIS; i++)
534
      for(int j=0; j<numLayers; j++) mScrambleTable[i][j] = 0;
535
    }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538
// PUBLIC API
539

    
540
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int totalScrambles)
541
    {
542
    if( curr==0 )
543
      {
544
      mCurrState     = 0;
545
      mIndexExcluded =-1;
546
      initializeScrambling();
547
      }
548

    
549
    int[] info= mStates[mCurrState].getRandom(rnd, mIndexExcluded, mScrambleTable, mNumOccurences);
550

    
551
    scramble[curr][0] = info[0];
552
    scramble[curr][1] = info[1];
553
    scramble[curr][2] = info[2];
554

    
555
    mCurrState     = info[3];
556
    mIndexExcluded = info[0];
557
    }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560

    
561
  public Static3D[] getRotationAxis()
562
    {
563
    return ROT_AXIS;
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567

    
568
  public int[] getBasicAngle()
569
    {
570
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
571
    return mBasicAngle;
572
    }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575

    
576
  public int getObjectName(int numLayers)
577
    {
578
    switch(numLayers)
579
      {
580
      case 2: return R.string.diam2;
581
      case 3: return R.string.diam3;
582
      case 4: return R.string.diam4;
583
      }
584

    
585
    return 0;
586
    }
587

    
588
///////////////////////////////////////////////////////////////////////////////////////////////////
589

    
590
  public int getInventor(int numLayers)
591
    {
592
    switch(numLayers)
593
      {
594
      case 2: return R.string.diam2_inventor;
595
      case 3: return R.string.diam3_inventor;
596
      case 4: return R.string.diam4_inventor;
597
      }
598

    
599
    return 0;
600
    }
601

    
602
///////////////////////////////////////////////////////////////////////////////////////////////////
603

    
604
  public int getComplexity(int numLayers)
605
    {
606
    switch(numLayers)
607
      {
608
      case 2: return 4;
609
      case 3: return 6;
610
      case 4: return 8;
611
      }
612

    
613
    return 0;
614
    }
615
}
(23-23/41)