Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyPyraminx.java @ a480ee80

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.library.main.DistortedEffects;
27
import org.distorted.library.main.DistortedTexture;
28
import org.distorted.library.mesh.MeshSquare;
29
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31
import org.distorted.main.R;
32

    
33
import java.util.Random;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class TwistyPyraminx extends TwistyObject
38
{
39
  static final float SCREEN_RATIO = 0.88f;
40

    
41
  static final Static3D[] ROT_AXIS = new Static3D[]
42
         {
43
           new Static3D(     0,-SQ3/3,-SQ6/3),
44
           new Static3D(     0,-SQ3/3,+SQ6/3),
45
           new Static3D(+SQ6/3,+SQ3/3,     0),
46
           new Static3D(-SQ6/3,+SQ3/3,     0),
47
         };
48

    
49
  private static final int[] BASIC_ANGLE = new int[] { 3,3,3,3 };
50

    
51
  private static final int[] FACE_COLORS = new int[]
52
         {
53
           COLOR_GREEN , COLOR_YELLOW,
54
           COLOR_BLUE  , COLOR_RED
55
         };
56

    
57
  // computed with res/raw/compute_quats.c
58
  private static final Static4D[] QUATS = new Static4D[]
59
         {
60
           new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
61
           new Static4D(  0.0f,   1.0f,   0.0f,  0.0f),
62
           new Static4D( SQ2/2,   0.5f,   0.0f,  0.5f),
63
           new Static4D(-SQ2/2,   0.5f,   0.0f,  0.5f),
64
           new Static4D(  0.0f,  -0.5f, -SQ2/2,  0.5f),
65
           new Static4D(  0.0f,  -0.5f,  SQ2/2,  0.5f),
66
           new Static4D( SQ2/2,   0.5f,   0.0f, -0.5f),
67
           new Static4D(-SQ2/2,   0.5f,   0.0f, -0.5f),
68
           new Static4D(  0.0f,  -0.5f, -SQ2/2, -0.5f),
69
           new Static4D(  0.0f,  -0.5f,  SQ2/2, -0.5f),
70
           new Static4D( SQ2/2,   0.0f,  SQ2/2,  0.0f),
71
           new Static4D(-SQ2/2,   0.0f,  SQ2/2,  0.0f)
72
         };
73

    
74
  private static final double[][] VERTICES_TETRA = new double[][]
75
          {
76
             {-0.5, SQ2/4, 0.0},
77
             { 0.5, SQ2/4, 0.0},
78
             { 0.0,-SQ2/4, 0.5},
79
             { 0.0,-SQ2/4,-0.5}
80
          };
81

    
82
  private static final int[][] VERT_INDEXES_TETRA = new int[][]
83
          {
84
             {2,1,0},   // counterclockwise!
85
             {3,0,1},
86
             {3,2,0},
87
             {2,3,1}
88
          };
89

    
90
  private static final double[][] VERTICES_OCTA = new double[][]
91
          {
92
             { 0.5,   0.0, 0.5},
93
             { 0.5,   0.0,-0.5},
94
             {-0.5,   0.0,-0.5},
95
             {-0.5,   0.0, 0.5},
96
             { 0.0, SQ2/2, 0.0},
97
             { 0.0,-SQ2/2, 0.0}
98
          };
99

    
100
  private static final int[][] VERT_INDEXES_OCTA = new int[][]
101
          {
102
             {3,0,4},   // counterclockwise!
103
             {0,1,4},
104
             {1,2,4},
105
             {2,3,4},
106
             {5,0,3},
107
             {5,1,0},
108
             {5,2,1},
109
             {5,3,2}
110
          };
111

    
112
  private static final float[][] STICKERS = new float[][]
113
          {
114
             { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f }
115
          };
116

    
117
  private static float[] mRowChances;
118

    
119
  private static final ObjectSticker[] mStickers;
120

    
121
  static
122
    {
123
    mStickers = new ObjectSticker[STICKERS.length];
124
    final float stroke = 0.08f;
125
    final float radius = 0.06f;
126
    final float[] radii= {radius,radius,radius};
127
    mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke);
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  TwistyPyraminx(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
133
                 DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
134
    {
135
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.PYRA, res, scrWidth);
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  int[] getSolvedQuats(int cubit, int numLayers)
141
    {
142
    int status = retCubitSolvedStatus(cubit,numLayers);
143
    return status<0 ? null : buildSolvedQuats(MovementPyraminx.FACE_AXIS[status],QUATS);
144
    }
145

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

    
148
  private float[] getRowChances(int numLayers)
149
    {
150
    int total = numLayers*(numLayers+1)/2;
151
    float running=0.0f;
152
    float[] chances = new float[numLayers];
153

    
154
    for(int i=0; i<numLayers; i++)
155
      {
156
      running += (numLayers-i);
157
      chances[i] = running / total;
158
      }
159

    
160
    return chances;
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  private void addTetrahedralLattice(int size, int index, float[][] pos)
166
    {
167
    final float DX = 1.0f;
168
    final float DY = SQ2/2;
169
    final float DZ = 1.0f;
170

    
171
    float startX = 0.0f;
172
    float startY =-DY*(size-1)/2;
173
    float startZ = DZ*(size-1)/2;
174

    
175
    for(int layer=0; layer<size; layer++)
176
      {
177
      float currX = startX;
178
      float currY = startY;
179

    
180
      for(int x=0; x<layer+1; x++)
181
        {
182
        float currZ = startZ;
183

    
184
        for(int z=0; z<size-layer; z++)
185
          {
186
          pos[index] = new float[] {currX,currY,currZ};
187
          index++;
188
          currZ -= DZ;
189
          }
190

    
191
        currX += DX;
192
        }
193

    
194
      startX-=DX/2;
195
      startY+=DY;
196
      startZ-=DZ/2;
197
      }
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
// there are (n^3-n)/6 octahedrons and ((n+1)^3 - (n+1))/6 tetrahedrons
202

    
203
  float[][] getCubitPositions(int size)
204
    {
205
    int numOcta = (size-1)*size*(size+1)/6;
206
    int numTetra= size*(size+1)*(size+2)/6;
207
    float[][] ret = new float[numOcta+numTetra][];
208

    
209
    addTetrahedralLattice(size-1,      0,ret);
210
    addTetrahedralLattice(size  ,numOcta,ret);
211

    
212
    return ret;
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  Static4D[] getQuats()
218
    {
219
    return QUATS;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  int getNumFaces()
225
    {
226
    return FACE_COLORS.length;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  int getNumStickerTypes(int numLayers)
232
    {
233
    return STICKERS.length;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  float[][] getCuts(int size)
239
    {
240
    float[][] cuts = new float[4][size-1];
241

    
242
    for(int i=0; i<size-1; i++)
243
      {
244
      float cut = (1.0f-0.25f*size+i)*(SQ6/3);
245
      cuts[0][i] = cut;
246
      cuts[1][i] = cut;
247
      cuts[2][i] = cut;
248
      cuts[3][i] = cut;
249
      }
250

    
251
    return cuts;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  int getNumCubitFaces()
257
    {
258
    return 8;
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  float getScreenRatio()
264
    {
265
    return SCREEN_RATIO;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  boolean shouldResetTextureMaps()
271
    {
272
    return false;
273
    }
274

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

    
277
  private int getNumOctahedrons(int numLayers)
278
    {
279
    return (numLayers-1)*numLayers*(numLayers+1)/6;
280
    }
281

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

    
284
  private int faceColor(int cubit, int axis)
285
    {
286
    return CUBITS[cubit].mRotationRow[axis] == 1 ? axis : NUM_FACES;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  int getFaceColor(int cubit, int cubitface, int size)
292
    {
293
    if( cubit< (size-1)*size*(size+1)/6 )
294
      {
295
      switch( cubitface )
296
        {
297
        case 0: return faceColor(cubit,0);
298
        case 2: return faceColor(cubit,1);
299
        case 5: return faceColor(cubit,3);
300
        case 7: return faceColor(cubit,2);
301
        default:return NUM_FACES;
302
        }
303
      }
304
    else
305
      {
306
      return cubitface<NUM_FACES ? faceColor(cubit,cubitface) : NUM_FACES;
307
      }
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
  ObjectShape getObjectShape(int cubit, int numLayers)
313
    {
314
    int variant = getCubitVariant(cubit,numLayers);
315

    
316
    if( variant==0 )
317
      {
318
      int N = numLayers==3? 6 : 5;
319
      int E = numLayers==3? 2 : 1;
320
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
321
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
322
      float[][] corners   = new float[][] { {0.04f,0.20f} };
323
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
324
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
325
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
326
      return new ObjectShape(VERTICES_OCTA,VERT_INDEXES_OCTA,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
327
      }
328
    else
329
      {
330
      int N = numLayers==3? 6 : 5;
331
      int E = numLayers==3? 2 : 1;
332
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
333
      int[] bandIndices   = new int[] { 0,0,0,0 };
334
      float[][] corners   = new float[][] { {0.06f,0.15f} };
335
      int[] cornerIndices = new int[] { 0,0,0,0 };
336
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
337
      int[] centerIndices = new int[] { 0,0,0,0 };
338
      return new ObjectShape(VERTICES_TETRA,VERT_INDEXES_TETRA,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
339
      }
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
  Static4D getQuat(int cubit, int numLayers)
345
    {
346
    return QUATS[0];
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  int getNumCubitVariants(int numLayers)
352
    {
353
    return 2;
354
    }
355

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

    
358
  int getCubitVariant(int cubit, int numLayers)
359
    {
360
    return cubit<getNumOctahedrons(numLayers) ? 0:1;
361
    }
362

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

    
365
  int getColor(int face)
366
    {
367
    return FACE_COLORS[face];
368
    }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
  ObjectSticker retSticker(int face)
373
    {
374
    return mStickers[face/NUM_FACES];
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378
// SQ6/3 = height of the tetrahedron
379

    
380
  float returnMultiplier()
381
    {
382
    return getNumLayers()/(SQ6/3);
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386
// PUBLIC API
387

    
388
  public Static3D[] getRotationAxis()
389
    {
390
    return ROT_AXIS;
391
    }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
  public int[] getBasicAngle()
396
    {
397
    return BASIC_ANGLE;
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
403
    {
404
    int numLayers = getNumLayers();
405

    
406
    if( mRowChances==null ) mRowChances = getRowChances(numLayers);
407

    
408
    if( curr==0 )
409
      {
410
      scramble[curr][0] = rnd.nextInt(NUM_AXIS);
411
      }
412
    else
413
      {
414
      int newVector = rnd.nextInt(NUM_AXIS-1);
415
      scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
416

    
417
      // Correct the situation when we first rotate the largest layer, then a tip (which doesn't
418
      // intersect anything besides the largest layer!) and then we try to rotate again along
419
      // the same axis like 2 rotations before - which carries the risk we rotate the largest
420
      // layer back to its spot again and the three moves end up being only a single tip rotation.
421
      if( curr>=2 && scramble[curr-1][1]==(numLayers-1) && scramble[curr][0]==scramble[curr-2][0] )
422
        {
423
        for(int ax=0; ax<NUM_AXIS; ax++)
424
          {
425
          if( scramble[curr-1][0]!=ax && scramble[curr-2][0]!=ax )
426
            {
427
            scramble[curr][0]=ax;
428
            break;
429
            }
430
          }
431
        }
432
      }
433

    
434
    float rowFloat = rnd.nextFloat();
435

    
436
    for(int row=0; row<numLayers; row++)
437
      {
438
      if( rowFloat<=mRowChances[row] )
439
        {
440
        scramble[curr][1] = row;
441
        break;
442
        }
443
      }
444

    
445
    switch( rnd.nextInt(2) )
446
      {
447
      case 0: scramble[curr][2] = -1; break;
448
      case 1: scramble[curr][2] =  1; break;
449
      }
450
    }
451

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

    
454
  public int getObjectName(int numLayers)
455
    {
456
    switch(numLayers)
457
      {
458
      case 3: return R.string.pyra3;
459
      case 4: return R.string.pyra4;
460
      case 5: return R.string.pyra5;
461
      }
462
    return R.string.pyra3;
463
    }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

    
467
  public int getInventor(int numLayers)
468
    {
469
    switch(numLayers)
470
      {
471
      case 3: return R.string.pyra3_inventor;
472
      case 4: return R.string.pyra4_inventor;
473
      case 5: return R.string.pyra5_inventor;
474
      }
475
    return R.string.pyra3_inventor;
476
    }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

    
480
  public int getComplexity(int numLayers)
481
    {
482
    switch(numLayers)
483
      {
484
      case 3: return 4;
485
      case 4: return 6;
486
      case 5: return 8;
487
      }
488
    return 4;
489
    }
490
}
(34-34/41)