Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyCube.java @ ef018c1b

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.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
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
class TwistyCube extends Twisty6
37
{
38
  static final Static3D[] ROT_AXIS = new Static3D[]
39
         {
40
           new Static3D(1,0,0),
41
           new Static3D(0,1,0),
42
           new Static3D(0,0,1)
43
         };
44

    
45
  private ScrambleState[] mStates;
46
  private Static4D[] mQuats;
47
  private float[][] mCuts;
48
  private boolean[][] mLayerRotatable;
49
  private int[] mBasicAngle;
50
  private ObjectSticker[] mStickers;
51
  private Movement mMovement;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  TwistyCube(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
56
             DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
57
    {
58
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.CUBE, res, scrWidth);
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  ScrambleState[] getScrambleStates()
64
    {
65
    if( mStates==null )
66
      {
67
      int size = getNumLayers();
68
      int[][] m = new int[16][];
69
      for(int i=1; i<16; i++) m[i] = createEdges(size,i);
70

    
71
      mStates = new ScrambleState[]
72
        {
73
        new ScrambleState( new int[][] { m[ 1], m[ 2], m[ 3] } ),  // 0
74
        new ScrambleState( new int[][] {  null, m[ 4], m[ 5] } ),  // x
75
        new ScrambleState( new int[][] { m[ 6],  null, m[ 7] } ),  // y
76
        new ScrambleState( new int[][] { m[ 8], m[ 8],  null } ),  // z
77
        new ScrambleState( new int[][] { m[10],  null, m[ 7] } ),  // xy
78
        new ScrambleState( new int[][] { m[11], m[ 9],  null } ),  // xz
79
        new ScrambleState( new int[][] {  null, m[12], m[ 5] } ),  // yx
80
        new ScrambleState( new int[][] { m[ 8], m[13],  null } ),  // yz
81
        new ScrambleState( new int[][] {  null, m[ 4], m[14] } ),  // zx
82
        new ScrambleState( new int[][] { m[ 6],  null, m[15] } ),  // zy
83
        new ScrambleState( new int[][] {  null,  null, m[ 5] } ),  // xyx
84
        new ScrambleState( new int[][] {  null, m[ 4],  null } ),  // xzx
85
        new ScrambleState( new int[][] {  null,  null, m[ 7] } ),  // yxy
86
        new ScrambleState( new int[][] { m[ 6],  null,  null } ),  // yzy
87
        new ScrambleState( new int[][] {  null, m[ 9],  null } ),  // zxz
88
        new ScrambleState( new int[][] { m[ 8],  null,  null } ),  // zyz
89
        };
90
      }
91

    
92
    return mStates;
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  private int[] createEdges(int size, int vertex)
98
    {
99
    int[] ret = new int[9*size];
100

    
101
    for(int l=0; l<size; l++)
102
      {
103
      ret[9*l  ] = l;
104
      ret[9*l+1] =-1;
105
      ret[9*l+2] = vertex;
106
      ret[9*l+3] = l;
107
      ret[9*l+4] = 1;
108
      ret[9*l+5] = vertex;
109
      ret[9*l+6] = l;
110
      ret[9*l+7] = 2;
111
      ret[9*l+8] = vertex;
112
      }
113

    
114
    return ret;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  private void initializeQuats()
120
    {
121
    mQuats = new Static4D[]
122
         {
123
         new Static4D(  0.0f,   0.0f,   0.0f,   1.0f),
124
         new Static4D(  1.0f,   0.0f,   0.0f,   0.0f),
125
         new Static4D(  0.0f,   1.0f,   0.0f,   0.0f),
126
         new Static4D(  0.0f,   0.0f,   1.0f,   0.0f),
127

    
128
         new Static4D( SQ2/2,  SQ2/2,  0.0f ,   0.0f),
129
         new Static4D( SQ2/2, -SQ2/2,  0.0f ,   0.0f),
130
         new Static4D( SQ2/2,   0.0f,  SQ2/2,   0.0f),
131
         new Static4D(-SQ2/2,   0.0f,  SQ2/2,   0.0f),
132
         new Static4D( SQ2/2,   0.0f,   0.0f,  SQ2/2),
133
         new Static4D( SQ2/2,   0.0f,   0.0f, -SQ2/2),
134
         new Static4D(  0.0f,  SQ2/2,  SQ2/2,   0.0f),
135
         new Static4D(  0.0f,  SQ2/2, -SQ2/2,   0.0f),
136
         new Static4D(  0.0f,  SQ2/2,   0.0f,  SQ2/2),
137
         new Static4D(  0.0f,  SQ2/2,   0.0f, -SQ2/2),
138
         new Static4D(  0.0f,   0.0f,  SQ2/2,  SQ2/2),
139
         new Static4D(  0.0f,   0.0f,  SQ2/2, -SQ2/2),
140

    
141
         new Static4D(  0.5f,   0.5f,   0.5f,   0.5f),
142
         new Static4D(  0.5f,   0.5f,  -0.5f,   0.5f),
143
         new Static4D(  0.5f,   0.5f,  -0.5f,  -0.5f),
144
         new Static4D(  0.5f,  -0.5f,   0.5f,  -0.5f),
145
         new Static4D( -0.5f,  -0.5f,  -0.5f,   0.5f),
146
         new Static4D( -0.5f,   0.5f,  -0.5f,  -0.5f),
147
         new Static4D( -0.5f,   0.5f,   0.5f,  -0.5f),
148
         new Static4D( -0.5f,   0.5f,   0.5f,   0.5f)
149
         };
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  int[] getSolvedQuats(int cubit, int numLayers)
155
    {
156
    if( mQuats ==null ) initializeQuats();
157
    int status = retCubitSolvedStatus(cubit,numLayers);
158
    return status<0 ? null : buildSolvedQuats(MovementCube.FACE_AXIS[status], mQuats);
159
    }
160

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

    
163
  ObjectShape getObjectShape(int cubit, int numLayers)
164
    {
165
    int extraI, extraV, num;
166
    float height;
167

    
168
    switch(numLayers)
169
      {
170
      case 2 : num = 6; extraI = 2; extraV = 2; height = 0.045f; break;
171
      case 3 : num = 5; extraI = 2; extraV = 2; height = 0.045f; break;
172
      case 4 : num = 5; extraI = 1; extraV = 1; height = 0.045f; break;
173
      default: num = 5; extraI = 0; extraV = 0; height = 0.045f; break;
174
      }
175

    
176
    double[][] vertices = new double[][]
177
          {
178
              { 0.5, 0.5, 0.5 },
179
              { 0.5, 0.5,-0.5 },
180
              { 0.5,-0.5, 0.5 },
181
              { 0.5,-0.5,-0.5 },
182
              {-0.5, 0.5, 0.5 },
183
              {-0.5, 0.5,-0.5 },
184
              {-0.5,-0.5, 0.5 },
185
              {-0.5,-0.5,-0.5 },
186
          };
187

    
188
    int[][] vert_indices = new int[][]
189
          {
190
              {2,3,1,0},
191
              {7,6,4,5},
192
              {4,0,1,5},
193
              {7,3,2,6},
194
              {6,2,0,4},
195
              {3,7,5,1}
196
          };
197

    
198
    float[][] bands     = new float[][] { {height,35,0.5f,0.7f,num,extraI,extraV} };
199
    int[] bandIndices   = new int[] { 0,0,0,0,0,0};
200
    float[][] corners   = new float[][] { {0.036f,0.12f} };
201
    int[] cornerIndices = new int[] { 0,0,0,0,0,0,0,0 };
202
    float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
203
    int[] centerIndices = new int[] { 0,0,0,0,0,0,0,0 };
204

    
205
    return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  Static4D getQuat(int cubit, int numLayers)
211
    {
212
    if( mQuats ==null ) initializeQuats();
213
    return mQuats[0];
214
    }
215

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

    
218
  int getNumCubitVariants(int numLayers)
219
    {
220
    return 1;
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  int getCubitVariant(int cubit, int numLayers)
226
    {
227
    return 0;
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  ObjectSticker retSticker(int face)
233
    {
234
    if( mStickers==null )
235
      {
236
      final float[][] STICKERS = new float[][]  { { -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f } };
237
      final float radius = 0.10f;
238
      final float stroke = 0.08f;
239
      final float[] radii = {radius,radius,radius,radius};
240
      mStickers = new ObjectSticker[STICKERS.length];
241
      mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke );
242
      }
243

    
244
    return mStickers[face/NUM_FACE_COLORS];
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  float[][] getCubitPositions(int numLayers)
250
    {
251
    int numCubits = numLayers>1 ? 6*numLayers*numLayers - 12*numLayers + 8 : 1;
252
    float[][] tmp = new float[numCubits][];
253

    
254
    float diff = 0.5f*(numLayers-1);
255
    int currentPosition = 0;
256

    
257
    for(int x = 0; x<numLayers; x++)
258
      for(int y = 0; y<numLayers; y++)
259
        for(int z = 0; z<numLayers; z++)
260
          if( x==0 || x==numLayers-1 || y==0 || y==numLayers-1 || z==0 || z==numLayers-1 )
261
            {
262
            tmp[currentPosition++] = new float[] {x-diff,y-diff,z-diff};
263
            }
264

    
265
    return tmp;
266
    }
267

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

    
270
  Static4D[] getQuats()
271
    {
272
    if( mQuats ==null ) initializeQuats();
273
    return mQuats;
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  float[][] getCuts(int numLayers)
279
    {
280
    if( numLayers<2 ) return null;
281

    
282
    if( mCuts==null )
283
      {
284
      mCuts = new float[3][numLayers-1];
285

    
286
      for(int i=0; i<numLayers-1; i++)
287
        {
288
        float cut = (2-numLayers)*0.5f + i;
289
        mCuts[0][i] = cut;
290
        mCuts[1][i] = cut;
291
        mCuts[2][i] = cut;
292
        }
293
      }
294

    
295
    return mCuts;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  private void getLayerRotatable(int numLayers)
301
    {
302
    if( mLayerRotatable==null )
303
      {
304
      int numAxis = ROT_AXIS.length;
305
      boolean[] tmp = new boolean[numLayers];
306
      for(int i=0; i<numLayers; i++) tmp[i] = true;
307
      mLayerRotatable = new boolean[numAxis][];
308
      for(int i=0; i<numAxis; i++) mLayerRotatable[i] = tmp;
309
      }
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  int getSolvedFunctionIndex()
315
    {
316
    return 0;
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  int getNumStickerTypes(int numLayers)
322
    {
323
    return 1;
324
    }
325

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

    
328
  int getNumCubitFaces()
329
    {
330
    return 6;
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

    
335
  int getFaceColor(int cubit, int cubitface, int numLayers)
336
    {
337
    return CUBITS[cubit].mRotationRow[cubitface/2] == (cubitface%2==0 ? (1<<(numLayers-1)):1) ? cubitface : NUM_TEXTURES;
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
// PUBLIC API
342

    
343
  public Static3D[] getRotationAxis()
344
    {
345
    return ROT_AXIS;
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
  public Movement getMovement()
351
    {
352
    if( mMovement==null )
353
      {
354
      int numLayers = getNumLayers();
355
      if( mCuts==null ) getCuts(numLayers);
356
      getLayerRotatable(numLayers);
357

    
358
      mMovement = new MovementCube(mCuts,mLayerRotatable,numLayers);
359
      }
360
    return mMovement;
361
    }
362

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

    
365
  public int[] getBasicAngle()
366
    {
367
    if( mBasicAngle==null ) mBasicAngle = new int[] { 4,4,4 };
368
    return mBasicAngle;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  public int getObjectName(int numLayers)
374
    {
375
    switch(numLayers)
376
      {
377
      case 2: return R.string.cube2;
378
      case 3: return R.string.cube3;
379
      case 4: return R.string.cube4;
380
      case 5: return R.string.cube5;
381
      }
382
    return R.string.cube3;
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  public int getInventor(int numLayers)
388
    {
389
    switch(numLayers)
390
      {
391
      case 2: return R.string.cube2_inventor;
392
      case 3: return R.string.cube3_inventor;
393
      case 4: return R.string.cube4_inventor;
394
      case 5: return R.string.cube5_inventor;
395
      }
396
    return R.string.cube3_inventor;
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
  public int getComplexity(int numLayers)
402
    {
403
    switch(numLayers)
404
      {
405
      case 2: return 4;
406
      case 3: return 6;
407
      case 4: return 8;
408
      case 5: return 10;
409
      }
410
    return 6;
411
    }
412
}
(27-27/48)