Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyJing.java @ 4f8cda80

1 df9739f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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 94a4edcf Leszek Koltunski
import org.distorted.helpers.ObjectShape;
25 9c06394a Leszek Koltunski
import org.distorted.helpers.ObjectSticker;
26 6cf89a3e Leszek Koltunski
import org.distorted.helpers.ScrambleState;
27 df9739f8 Leszek Koltunski
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 TwistyJing extends TwistyObject
39
{
40
  static final Static3D[] ROT_AXIS = new Static3D[]
41
         {
42
           new Static3D(     0,-SQ3/3,-SQ6/3),
43
           new Static3D(     0,-SQ3/3,+SQ6/3),
44
           new Static3D(+SQ6/3,+SQ3/3,     0),
45
           new Static3D(-SQ6/3,+SQ3/3,     0),
46
         };
47
48
  private static final int[] FACE_COLORS = new int[]
49
         {
50
           COLOR_GREEN , COLOR_YELLOW,
51
           COLOR_BLUE  , COLOR_RED
52
         };
53
54 4f8cda80 Leszek Koltunski
  static final float F = 0.24f;  // length of the edge of the corner cubit divided by
55
                                 // the length of the edge of the whole tetrahedron.
56
                                 // keep < 0.25.
57 9c06394a Leszek Koltunski
58 0ad6b867 Leszek Koltunski
  private int mCurrState;
59
  private int mIndexExcluded;
60 6cf89a3e Leszek Koltunski
  private final ScrambleState[] mStates;
61 e1dc3366 Leszek Koltunski
  private int[][] mScrambleTable;
62
  private int[] mNumOccurences;
63 4f8cda80 Leszek Koltunski
  private int[] mBasicAngle;
64
  private int[] mRotQuat;
65
  private Static4D[] mQuats;
66
  private float[][] mCenters;
67
  private int[][] mFaceMap;
68
  private ObjectSticker[] mStickers;
69 0ad6b867 Leszek Koltunski
70 df9739f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72
  TwistyJing(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
73
             DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
74
    {
75
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.JING, res, scrWidth);
76 0ad6b867 Leszek Koltunski
77
    int[] tmp = {0,-1,0, 0,1,0, 1,-1,0, 1,1,0 };
78
79 6cf89a3e Leszek Koltunski
    mStates = new ScrambleState[]
80 0ad6b867 Leszek Koltunski
      {
81 6cf89a3e Leszek Koltunski
      new ScrambleState( new int[][] {tmp,tmp,tmp,tmp} )
82 0ad6b867 Leszek Koltunski
      };
83 df9739f8 Leszek Koltunski
    }
84
85 4f8cda80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87
  private void initializeQuats()
88
    {
89
    mQuats = new Static4D[]
90
         {
91
         new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
92
         new Static4D(  0.0f,   1.0f,   0.0f,  0.0f),
93
         new Static4D( SQ2/2,   0.5f,   0.0f,  0.5f),
94
         new Static4D(-SQ2/2,   0.5f,   0.0f,  0.5f),
95
         new Static4D(  0.0f,  -0.5f, -SQ2/2,  0.5f),
96
         new Static4D(  0.0f,  -0.5f,  SQ2/2,  0.5f),
97
         new Static4D( SQ2/2,   0.5f,   0.0f, -0.5f),
98
         new Static4D(-SQ2/2,   0.5f,   0.0f, -0.5f),
99
         new Static4D(  0.0f,  -0.5f, -SQ2/2, -0.5f),
100
         new Static4D(  0.0f,  -0.5f,  SQ2/2, -0.5f),
101
         new Static4D( SQ2/2,   0.0f,  SQ2/2,  0.0f),
102
         new Static4D(-SQ2/2,   0.0f,  SQ2/2,  0.0f)
103
         };
104
    }
105
106 a480ee80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108
  int[] getSolvedQuats(int cubit, int numLayers)
109
    {
110 4f8cda80 Leszek Koltunski
    if( mQuats==null ) initializeQuats();
111 a480ee80 Leszek Koltunski
    int status = retCubitSolvedStatus(cubit,numLayers);
112 4f8cda80 Leszek Koltunski
    return status<0 ? null : buildSolvedQuats(MovementJing.FACE_AXIS[status],mQuats);
113 a480ee80 Leszek Koltunski
    }
114
115 df9739f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
116
117
  float[][] getCubitPositions(int size)
118
    {
119 4f8cda80 Leszek Koltunski
    if( mCenters==null )
120
      {
121
      mCenters = new float[][]
122
         {
123
           { 0.000f, -SQ2/4, 0.500f },
124
           { 0.000f, -SQ2/4,-0.500f },
125
           {-0.500f,  SQ2/4, 0.000f },
126
           { 0.500f,  SQ2/4, 0.000f },
127
128
           { 0.000f, -SQ2/4, 0.000f },
129
           {-0.250f, 0.000f, 0.250f },
130
           { 0.250f, 0.000f, 0.250f },
131
           {-0.250f, 0.000f,-0.250f },
132
           { 0.250f, 0.000f,-0.250f },
133
           { 0.000f,  SQ2/4, 0.000f },
134
135
           { 0.000f, SQ2/12, 1.0f/6 },
136
           { 0.000f, SQ2/12,-1.0f/6 },
137
           {-1.0f/6,-SQ2/12, 0.000f },
138
           { 1.0f/6,-SQ2/12, 0.000f },
139
         };
140
      }
141
142
    return mCenters;
143 df9739f8 Leszek Koltunski
    }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147
  Static4D[] getQuats()
148
    {
149 4f8cda80 Leszek Koltunski
    if( mQuats==null ) initializeQuats();
150
    return mQuats;
151 df9739f8 Leszek Koltunski
    }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
  int getNumFaces()
156
    {
157
    return FACE_COLORS.length;
158
    }
159
160 169219a7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
161
162
  int getSolvedFunctionIndex()
163
    {
164
    return 0;
165
    }
166
167 df9739f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168
169
  int getNumStickerTypes(int numLayers)
170
    {
171 4f8cda80 Leszek Koltunski
    return 3;
172 df9739f8 Leszek Koltunski
    }
173
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
176
  float[][] getCuts(int size)
177
    {
178 48154f68 Leszek Koltunski
    float[] cut = { (F-0.25f)*(SQ6/3) };
179 df9739f8 Leszek Koltunski
    return new float[][] { cut,cut,cut,cut };
180
    }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
  int getNumCubitFaces()
185
    {
186
    return 6;
187
    }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191
  float getScreenRatio()
192
    {
193 f074c272 Leszek Koltunski
    return 2*TwistyPyraminx.SCREEN_RATIO;
194 df9739f8 Leszek Koltunski
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198
  boolean shouldResetTextureMaps()
199
    {
200
    return false;
201
    }
202
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205
  int getFaceColor(int cubit, int cubitface, int size)
206
    {
207 4f8cda80 Leszek Koltunski
    if( mFaceMap==null )
208
      {
209
      // Colors of the faces of cubits.
210
      // GREEN 0 YELLOW 1 BLUE  2 RED 3
211
      // GREEN 4 YELLOW 5 BLUE  6 RED 7
212
      // GREEN 8 YELLOW 9 BLUE 10 RED 11
213
      mFaceMap = new int[][]
214
         {
215
           {  0,  3,  2, 12, 12, 12 },
216
           {  1,  2,  3, 12, 12, 12 },
217
           {  1,  0,  2, 12, 12, 12 },
218
           {  1,  3,  0, 12, 12, 12 },
219
220
           {  7,  6, 12, 12, 12, 12 },
221
           {  4,  6, 12, 12, 12, 12 },
222
           {  7,  4, 12, 12, 12, 12 },
223
           {  5,  6, 12, 12, 12, 12 },
224
           {  7,  5, 12, 12, 12, 12 },
225
           {  4,  5, 12, 12, 12, 12 },
226
227
           {  8, 12, 12, 12, 12, 12 },
228
           {  9, 12, 12, 12, 12, 12 },
229
           { 10, 12, 12, 12, 12, 12 },
230
           { 11, 12, 12, 12, 12, 12 },
231
         };
232
      }
233
234 df9739f8 Leszek Koltunski
    return mFaceMap[cubit][cubitface];
235
    }
236
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239 94a4edcf Leszek Koltunski
  ObjectShape getObjectShape(int cubit, int numLayers)
240 df9739f8 Leszek Koltunski
    {
241 94a4edcf Leszek Koltunski
    int variant = getCubitVariant(cubit,numLayers);
242 df9739f8 Leszek Koltunski
243 4f8cda80 Leszek Koltunski
    final float X = F/2;
244
    final float Y = F*SQ2/2;
245
    final float Z =-F/2;
246
    final float L = (1-3*F);
247
    final float X2= L/2;
248
    final float Y2= L*SQ2/2;
249
    final float Z2=-L/2;
250
    final float D = F/L;
251
252 94a4edcf Leszek Koltunski
    if( variant==0 )
253 df9739f8 Leszek Koltunski
      {
254 4f8cda80 Leszek Koltunski
      double[][] vertices = new double[][]
255
          {
256
             { 0.0, 0.0, 0.0 },
257
             {   X,   Y,   Z },
258
             { 0.0, 2*Y, 2*Z },
259
             {  -X,   Y,   Z },
260
             { 0.0, 0.0,    -F },
261
             {   X,   Y,   Z-F },
262
             { 0.0, 2*Y, 2*Z-F },
263
             {  -X,   Y,   Z-F },
264
          };
265
      int[][] vert_indices = new int[][]
266
          {
267
             {0,1,2,3},
268
             {1,0,4,5},
269
             {7,4,0,3},
270
             {1,5,6,2},
271
             {7,3,2,6},
272
             {4,7,6,5}
273
          };
274
275 94a4edcf Leszek Koltunski
      float[][] bands     = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
276
      int[] bandIndices   = new int[] { 0,0,0,1,1,1 };
277
      float[][] corners   = new float[][] { {0.08f,0.20f*F},{0.07f,0.20f*F} };
278
      int[] cornerIndices = new int[] { 0,1,1,-1,1,-1,-1,-1 };
279
      float[][] centers   = new float[][] { { 0.0f, F*SQ2/2, -F} };
280
      int[] centerIndices = new int[] { 0,0,0,-1,0,-1,-1,-1 };
281 4f8cda80 Leszek Koltunski
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
282 df9739f8 Leszek Koltunski
      }
283 94a4edcf Leszek Koltunski
    else if( variant==1 )
284 df9739f8 Leszek Koltunski
      {
285 4f8cda80 Leszek Koltunski
      double[][] vertices = new double[][]
286
          {
287
             { 0.0, 0.0,     0.5-F },
288
             {   X,   Y,   Z+0.5-F },
289
             { 0.0, 2*Y, 2*Z+0.5-F },
290
             {  -X,   Y,   Z+0.5-F },
291
             { 0.0, 0.0,    -0.5+F },
292
             {   X,   Y,  -Z-0.5+F },
293
             { 0.0, 2*Y,-2*Z-0.5+F },
294
             {  -X,   Y,  -Z-0.5+F },
295
          };
296
      int[][] vert_indices = new int[][]
297
          {
298
             {0,4,5,1},
299
             {3,7,4,0},
300
             {0,1,2,3},
301
             {4,7,6,5},
302
             {1,5,6,2},
303
             {2,6,7,3}
304
          };
305
306 94a4edcf Leszek Koltunski
      float[][] bands     = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
307
      int[] bandIndices   = new int[] { 0,0,1,1,1,1 };
308
      float[][] corners   = new float[][] { {0.07f,0.20f*F} };
309
      int[] cornerIndices = new int[] { 0,0,-1,0,0,0,-1,0 };
310
      float[][] centers   = new float[][] { { 0, F*SQ2/2, 0 } };
311
      int[] centerIndices = new int[] { 0,0,-1,0,0,0,-1,0 };
312 4f8cda80 Leszek Koltunski
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
313 df9739f8 Leszek Koltunski
      }
314
    else
315
      {
316 4f8cda80 Leszek Koltunski
      double[][] vertices = new double[][]
317
          {
318
             {    0.0,     -2*Y2/3,   -2*Z2/3 },
319
             {      X2,       Y2/3,      Z2/3 },
320
             {     -X2,       Y2/3,      Z2/3 },
321
             {    0.0,     -2*Y2/3,-2*Z2/3+2*D*Z2 },
322
             {  X2-D*X2, Y2/3-D*Y2, Z2/3+D*Z2 },
323
             { -X2+D*X2, Y2/3-D*Y2, Z2/3+D*Z2 },
324
          };
325
      int[][] vert_indices = new int[][]
326
          {
327
             {0,1,2},
328
             {3,5,4},
329
             {0,3,4,1},
330
             {5,3,0,2},
331
             {4,5,2,1}
332
          };
333
334 668423be Leszek Koltunski
      float[][] bands     = new float[][] { {0.020f,35,0.20f*(1-3*F),0.6f*(1-3*F),5,1,1}, {0.001f,35,0.05f*(1-3*F),0.1f*(1-3*F),5,1,1} };
335 94a4edcf Leszek Koltunski
      int[] bandIndices   = new int[] { 0,1,1,1,1,1 };
336
      float[][] corners   = new float[][] { {0.04f,0.15f} };
337
      int[] cornerIndices = new int[] { 0,0,0,-1,-1,-1 };
338
      float[][] centers   = new float[][] { { 0, -2*Y/3, 4*Z/3 } };
339
      int[] centerIndices = new int[] { 0,0,0,-1,-1,-1 };
340 4f8cda80 Leszek Koltunski
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
341 94a4edcf Leszek Koltunski
      }
342
    }
343
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345
346 3e605536 Leszek Koltunski
  Static4D getQuat(int cubit, int numLayers)
347 94a4edcf Leszek Koltunski
    {
348 4f8cda80 Leszek Koltunski
    if( mQuats==null ) initializeQuats();
349
    if( mRotQuat ==null ) mRotQuat = new int[] {0,1,2,7,0,2,7,6,3,10,0,1,3,5};
350
    return mQuats[mRotQuat[cubit]];
351 94a4edcf Leszek Koltunski
    }
352
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
355 3e605536 Leszek Koltunski
  int getNumCubitVariants(int numLayers)
356 94a4edcf Leszek Koltunski
    {
357
    return 3;
358
    }
359
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
362
  int getCubitVariant(int cubit, int numLayers)
363
    {
364
    return cubit<4 ? 0 : (cubit<10?1:2);
365
    }
366
367 df9739f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
368
369 9c06394a Leszek Koltunski
  int getColor(int face)
370 df9739f8 Leszek Koltunski
    {
371 9c06394a Leszek Koltunski
    return FACE_COLORS[face];
372
    }
373 df9739f8 Leszek Koltunski
374 9c06394a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
375 df9739f8 Leszek Koltunski
376 9c06394a Leszek Koltunski
  ObjectSticker retSticker(int face)
377
    {
378 4f8cda80 Leszek Koltunski
    if( mStickers==null )
379
      {
380
      float[][] STICKERS = new float[][]
381
          {
382
             { 0.0f, -0.5f, 0.28867516f, 0.0f, 0.0f, 0.5f, -0.28867516f, 0.0f },
383
             { -0.5f, 0.11983269f, 0.27964598f, -0.43146032f, 0.3200817f, 0.007388768f, -0.09972769f, 0.30423886f },
384
             { 0.0f, -0.5f, 0.43301272f, 0.25f, -0.43301272f, 0.25f },
385
          };
386
387
      mStickers = new ObjectSticker[STICKERS.length];
388
      final float R1 = 0.05f;
389
      final float R2 = 0.10f;
390
      final float R3 = 0.03f;
391
      final float[][] radii  = { { R1,R1,R1,R1 },{ R3,R3,R2,R2 },{ R1,R1,R1 } };
392
      final float[] strokes = { 0.06f, 0.03f, 0.05f };
393
394
      for(int s=0; s<STICKERS.length; s++)
395
        {
396
        mStickers[s] = new ObjectSticker(STICKERS[s],null,radii[s],strokes[s]);
397
        }
398
      }
399
400 9c06394a Leszek Koltunski
    return mStickers[face/NUM_FACES];
401 df9739f8 Leszek Koltunski
    }
402
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404
// SQ6/3 = height of the tetrahedron
405
406
  float returnMultiplier()
407
    {
408
    return getNumLayers()/(SQ6/3);
409
    }
410
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412
413 e1dc3366 Leszek Koltunski
  private void initializeScrambling()
414 df9739f8 Leszek Koltunski
    {
415 e1dc3366 Leszek Koltunski
    int numLayers = getNumLayers();
416 df9739f8 Leszek Koltunski
417 e1dc3366 Leszek Koltunski
    if( mScrambleTable ==null )
418
      {
419
      mScrambleTable = new int[NUM_AXIS][numLayers];
420
      }
421
    if( mNumOccurences ==null )
422
      {
423
      int max=0;
424 df9739f8 Leszek Koltunski
425 e1dc3366 Leszek Koltunski
      for (ScrambleState mState : mStates)
426
        {
427
        int tmp = mState.getTotal(-1);
428
        if (max < tmp) max = tmp;
429
        }
430
431
      mNumOccurences = new int[max];
432
      }
433
434
    for(int i=0; i<NUM_AXIS; i++)
435
      for(int j=0; j<numLayers; j++) mScrambleTable[i][j] = 0;
436 df9739f8 Leszek Koltunski
    }
437
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439 e1dc3366 Leszek Koltunski
// PUBLIC API
440 df9739f8 Leszek Koltunski
441 0ad6b867 Leszek Koltunski
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int totalScrambles)
442 df9739f8 Leszek Koltunski
    {
443
    if( curr==0 )
444
      {
445 0ad6b867 Leszek Koltunski
      mCurrState     = 0;
446
      mIndexExcluded =-1;
447 e1dc3366 Leszek Koltunski
      initializeScrambling();
448 df9739f8 Leszek Koltunski
      }
449
450 e1dc3366 Leszek Koltunski
    int[] info= mStates[mCurrState].getRandom(rnd, mIndexExcluded, mScrambleTable, mNumOccurences);
451 df9739f8 Leszek Koltunski
452 0ad6b867 Leszek Koltunski
    scramble[curr][0] = info[0];
453
    scramble[curr][1] = info[1];
454
    scramble[curr][2] = info[2];
455
456
    mCurrState     = info[3];
457
    mIndexExcluded = info[0];
458 df9739f8 Leszek Koltunski
    }
459
460 e1dc3366 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
461
462
  public Static3D[] getRotationAxis()
463
    {
464
    return ROT_AXIS;
465
    }
466
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468
469
  public int[] getBasicAngle()
470
    {
471 4f8cda80 Leszek Koltunski
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
472
    return mBasicAngle;
473 e1dc3366 Leszek Koltunski
    }
474
475 df9739f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
476
477
  public int getObjectName(int numLayers)
478
    {
479
    return R.string.jing;
480
    }
481
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483
484
  public int getInventor(int numLayers)
485
    {
486
    return R.string.jing_inventor;
487
    }
488
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490
491
  public int getComplexity(int numLayers)
492
    {
493 d5ca9927 Leszek Koltunski
    return 4;
494 df9739f8 Leszek Koltunski
    }
495
}