Project

General

Profile

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

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

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 0c52af30 Leszek Koltunski
22 ccf9fec5 Leszek Koltunski
import android.content.res.Resources;
23 0c52af30 Leszek Koltunski
24 f10a88a8 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 0c52af30 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29 efa8aa48 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
30 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32 6fd4a72c Leszek Koltunski
import org.distorted.main.R;
33 0c52af30 Leszek Koltunski
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36 efa81f0c Leszek Koltunski
class TwistyCube extends Twisty6
37 0c52af30 Leszek Koltunski
{
38 ad38d800 Leszek Koltunski
  static final Static3D[] ROT_AXIS = new Static3D[]
39 efef689c Leszek Koltunski
         {
40
           new Static3D(1,0,0),
41
           new Static3D(0,1,0),
42
           new Static3D(0,0,1)
43
         };
44
45 91792184 Leszek Koltunski
  private ScrambleState[] mStates;
46 7ff38997 Leszek Koltunski
  private Static4D[] mQuats;
47
  private int[] mBasicAngle;
48
  private ObjectSticker[] mStickers;
49 e9a87113 Leszek Koltunski
  private Movement mMovement;
50 caccea6e Leszek Koltunski
51 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 d90c55cc Leszek Koltunski
  TwistyCube(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
54
             DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
55 411c6285 Leszek Koltunski
    {
56 db875721 Leszek Koltunski
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.CUBE, res, scrWidth);
57 91792184 Leszek Koltunski
    }
58 caccea6e Leszek Koltunski
59 91792184 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60 caccea6e Leszek Koltunski
61 91792184 Leszek Koltunski
  ScrambleState[] getScrambleStates()
62
    {
63
    if( mStates==null )
64 caccea6e Leszek Koltunski
      {
65 91792184 Leszek Koltunski
      int size = getNumLayers();
66
      int[][] m = new int[16][];
67
      for(int i=1; i<16; i++) m[i] = createEdges(size,i);
68
69
      mStates = new ScrambleState[]
70
        {
71
        new ScrambleState( new int[][] { m[ 1], m[ 2], m[ 3] } ),  // 0
72
        new ScrambleState( new int[][] {  null, m[ 4], m[ 5] } ),  // x
73
        new ScrambleState( new int[][] { m[ 6],  null, m[ 7] } ),  // y
74
        new ScrambleState( new int[][] { m[ 8], m[ 8],  null } ),  // z
75
        new ScrambleState( new int[][] { m[10],  null, m[ 7] } ),  // xy
76
        new ScrambleState( new int[][] { m[11], m[ 9],  null } ),  // xz
77
        new ScrambleState( new int[][] {  null, m[12], m[ 5] } ),  // yx
78
        new ScrambleState( new int[][] { m[ 8], m[13],  null } ),  // yz
79
        new ScrambleState( new int[][] {  null, m[ 4], m[14] } ),  // zx
80
        new ScrambleState( new int[][] { m[ 6],  null, m[15] } ),  // zy
81
        new ScrambleState( new int[][] {  null,  null, m[ 5] } ),  // xyx
82
        new ScrambleState( new int[][] {  null, m[ 4],  null } ),  // xzx
83
        new ScrambleState( new int[][] {  null,  null, m[ 7] } ),  // yxy
84
        new ScrambleState( new int[][] { m[ 6],  null,  null } ),  // yzy
85
        new ScrambleState( new int[][] {  null, m[ 9],  null } ),  // zxz
86
        new ScrambleState( new int[][] { m[ 8],  null,  null } ),  // zyz
87
        };
88
      }
89
90
    return mStates;
91 caccea6e Leszek Koltunski
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95
  private int[] createEdges(int size, int vertex)
96
    {
97
    int[] ret = new int[9*size];
98
99
    for(int l=0; l<size; l++)
100
      {
101
      ret[9*l  ] = l;
102
      ret[9*l+1] =-1;
103
      ret[9*l+2] = vertex;
104
      ret[9*l+3] = l;
105
      ret[9*l+4] = 1;
106
      ret[9*l+5] = vertex;
107
      ret[9*l+6] = l;
108
      ret[9*l+7] = 2;
109
      ret[9*l+8] = vertex;
110
      }
111
112
    return ret;
113 411c6285 Leszek Koltunski
    }
114
115 7ff38997 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
116
117
  private void initializeQuats()
118
    {
119
    mQuats = new Static4D[]
120
         {
121
         new Static4D(  0.0f,   0.0f,   0.0f,   1.0f),
122
         new Static4D(  1.0f,   0.0f,   0.0f,   0.0f),
123
         new Static4D(  0.0f,   1.0f,   0.0f,   0.0f),
124
         new Static4D(  0.0f,   0.0f,   1.0f,   0.0f),
125
126
         new Static4D( SQ2/2,  SQ2/2,  0.0f ,   0.0f),
127
         new Static4D( SQ2/2, -SQ2/2,  0.0f ,   0.0f),
128
         new Static4D( SQ2/2,   0.0f,  SQ2/2,   0.0f),
129
         new Static4D(-SQ2/2,   0.0f,  SQ2/2,   0.0f),
130
         new Static4D( SQ2/2,   0.0f,   0.0f,  SQ2/2),
131
         new Static4D( SQ2/2,   0.0f,   0.0f, -SQ2/2),
132
         new Static4D(  0.0f,  SQ2/2,  SQ2/2,   0.0f),
133
         new Static4D(  0.0f,  SQ2/2, -SQ2/2,   0.0f),
134
         new Static4D(  0.0f,  SQ2/2,   0.0f,  SQ2/2),
135
         new Static4D(  0.0f,  SQ2/2,   0.0f, -SQ2/2),
136
         new Static4D(  0.0f,   0.0f,  SQ2/2,  SQ2/2),
137
         new Static4D(  0.0f,   0.0f,  SQ2/2, -SQ2/2),
138
139
         new Static4D(  0.5f,   0.5f,   0.5f,   0.5f),
140
         new Static4D(  0.5f,   0.5f,  -0.5f,   0.5f),
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
         };
148
    }
149
150 a480ee80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
151
152
  int[] getSolvedQuats(int cubit, int numLayers)
153
    {
154 7ff38997 Leszek Koltunski
    if( mQuats ==null ) initializeQuats();
155 a480ee80 Leszek Koltunski
    int status = retCubitSolvedStatus(cubit,numLayers);
156 7ff38997 Leszek Koltunski
    return status<0 ? null : buildSolvedQuats(MovementCube.FACE_AXIS[status], mQuats);
157 a480ee80 Leszek Koltunski
    }
158
159 f10a88a8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161
  ObjectShape getObjectShape(int cubit, int numLayers)
162
    {
163
    int extraI, extraV, num;
164
    float height;
165
166
    switch(numLayers)
167
      {
168
      case 2 : num = 6; extraI = 2; extraV = 2; height = 0.045f; break;
169
      case 3 : num = 5; extraI = 2; extraV = 2; height = 0.045f; break;
170
      case 4 : num = 5; extraI = 1; extraV = 1; height = 0.045f; break;
171
      default: num = 5; extraI = 0; extraV = 0; height = 0.045f; break;
172
      }
173
174 7ff38997 Leszek Koltunski
    double[][] vertices = new double[][]
175
          {
176
              { 0.5, 0.5, 0.5 },
177
              { 0.5, 0.5,-0.5 },
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
          };
185
186
    int[][] vert_indices = new int[][]
187
          {
188 d90c55cc Leszek Koltunski
              {2,3,1,0},
189 7ff38997 Leszek Koltunski
              {7,6,4,5},
190
              {4,0,1,5},
191
              {7,3,2,6},
192
              {6,2,0,4},
193
              {3,7,5,1}
194
          };
195
196 f10a88a8 Leszek Koltunski
    float[][] bands     = new float[][] { {height,35,0.5f,0.7f,num,extraI,extraV} };
197
    int[] bandIndices   = new int[] { 0,0,0,0,0,0};
198
    float[][] corners   = new float[][] { {0.036f,0.12f} };
199
    int[] cornerIndices = new int[] { 0,0,0,0,0,0,0,0 };
200
    float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
201
    int[] centerIndices = new int[] { 0,0,0,0,0,0,0,0 };
202
203 7ff38997 Leszek Koltunski
    return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
204 f10a88a8 Leszek Koltunski
    }
205
206 ac940e24 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208 3e605536 Leszek Koltunski
  Static4D getQuat(int cubit, int numLayers)
209 ac940e24 Leszek Koltunski
    {
210 7ff38997 Leszek Koltunski
    if( mQuats ==null ) initializeQuats();
211
    return mQuats[0];
212 3e605536 Leszek Koltunski
    }
213 f10a88a8 Leszek Koltunski
214 3e605536 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
215 ac940e24 Leszek Koltunski
216 3e605536 Leszek Koltunski
  int getNumCubitVariants(int numLayers)
217
    {
218
    return 1;
219
    }
220 ac940e24 Leszek Koltunski
221 3e605536 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
222 f10a88a8 Leszek Koltunski
223 3e605536 Leszek Koltunski
  int getCubitVariant(int cubit, int numLayers)
224
    {
225
    return 0;
226 ac940e24 Leszek Koltunski
    }
227
228 9c06394a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
229 7289fd6c Leszek Koltunski
230 9c06394a Leszek Koltunski
  ObjectSticker retSticker(int face)
231
    {
232 7ff38997 Leszek Koltunski
    if( mStickers==null )
233
      {
234
      final float[][] STICKERS = new float[][]  { { -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f } };
235
      final float radius = 0.10f;
236
      final float stroke = 0.08f;
237
      final float[] radii = {radius,radius,radius,radius};
238
      mStickers = new ObjectSticker[STICKERS.length];
239
      mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke );
240
      }
241
242 abf36986 Leszek Koltunski
    return mStickers[face/NUM_FACE_COLORS];
243 7289fd6c Leszek Koltunski
    }
244
245 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247 b1f2ccf5 Leszek Koltunski
  float[][] getCubitPositions(int numLayers)
248 a10ada2a Leszek Koltunski
    {
249 b1f2ccf5 Leszek Koltunski
    int numCubits = numLayers>1 ? 6*numLayers*numLayers - 12*numLayers + 8 : 1;
250 e6cf7283 Leszek Koltunski
    float[][] tmp = new float[numCubits][];
251 beb325a0 Leszek Koltunski
252 b1f2ccf5 Leszek Koltunski
    float diff = 0.5f*(numLayers-1);
253 a10ada2a Leszek Koltunski
    int currentPosition = 0;
254 beb325a0 Leszek Koltunski
255 b1f2ccf5 Leszek Koltunski
    for(int x = 0; x<numLayers; x++)
256
      for(int y = 0; y<numLayers; y++)
257
        for(int z = 0; z<numLayers; z++)
258
          if( x==0 || x==numLayers-1 || y==0 || y==numLayers-1 || z==0 || z==numLayers-1 )
259 beb325a0 Leszek Koltunski
            {
260 e6cf7283 Leszek Koltunski
            tmp[currentPosition++] = new float[] {x-diff,y-diff,z-diff};
261 a10ada2a Leszek Koltunski
            }
262 47ba5ddc Leszek Koltunski
263 a10ada2a Leszek Koltunski
    return tmp;
264
    }
265 47ba5ddc Leszek Koltunski
266 beb325a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 10585385 Leszek Koltunski
  Static4D[] getQuats()
269 a10ada2a Leszek Koltunski
    {
270 7ff38997 Leszek Koltunski
    if( mQuats ==null ) initializeQuats();
271
    return mQuats;
272 a10ada2a Leszek Koltunski
    }
273 47ba5ddc Leszek Koltunski
274 7403cdfa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
275
276 e6734aa9 Leszek Koltunski
  float[][] getCuts(int numLayers)
277 7403cdfa Leszek Koltunski
    {
278 e6734aa9 Leszek Koltunski
    float[][] cuts = new float[3][numLayers-1];
279 a97e02b7 Leszek Koltunski
280 a64e07d0 Leszek Koltunski
    for(int i=0; i<numLayers-1; i++)
281 a97e02b7 Leszek Koltunski
      {
282 e6734aa9 Leszek Koltunski
      float cut = (2-numLayers)*0.5f + i;
283
      cuts[0][i] = cut;
284
      cuts[1][i] = cut;
285
      cuts[2][i] = cut;
286 a97e02b7 Leszek Koltunski
      }
287
288
    return cuts;
289 7403cdfa Leszek Koltunski
    }
290
291 169219a7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
292
293
  int getSolvedFunctionIndex()
294
    {
295
    return 0;
296
    }
297
298 eab9d8f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
299
300 a64e07d0 Leszek Koltunski
  int getNumStickerTypes(int numLayers)
301 eab9d8f8 Leszek Koltunski
    {
302 7ff38997 Leszek Koltunski
    return 1;
303 eab9d8f8 Leszek Koltunski
    }
304
305 8f53e513 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307
  int getNumCubitFaces()
308
    {
309 efa81f0c Leszek Koltunski
    return 6;
310 f0fa83ae Leszek Koltunski
    }
311
312 f6d06256 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
313
314 f10a88a8 Leszek Koltunski
  int getFaceColor(int cubit, int cubitface, int numLayers)
315 f6d06256 Leszek Koltunski
    {
316 d464f54f Leszek Koltunski
    return CUBITS[cubit].mRotationRow[cubitface/2] == (cubitface%2==0 ? (1<<(numLayers-1)):1) ? cubitface : NUM_TEXTURES;
317 f6d06256 Leszek Koltunski
    }
318
319 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
320
// PUBLIC API
321
322 12ad3fca Leszek Koltunski
  public Static3D[] getRotationAxis()
323
    {
324 ad38d800 Leszek Koltunski
    return ROT_AXIS;
325 12ad3fca Leszek Koltunski
    }
326
327 e9a87113 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
328
329
  public Movement getMovement()
330
    {
331
    if( mMovement==null ) mMovement = new MovementCube();
332
    return mMovement;
333
    }
334
335 12ad3fca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
336
337 925ed78f Leszek Koltunski
  public int[] getBasicAngle()
338 e844c116 Leszek Koltunski
    {
339 d90c55cc Leszek Koltunski
    if( mBasicAngle==null ) mBasicAngle = new int[] { 4,4,4 };
340 7ff38997 Leszek Koltunski
    return mBasicAngle;
341 e844c116 Leszek Koltunski
    }
342 39e74052 Leszek Koltunski
343 f0336037 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
344 6fd4a72c Leszek Koltunski
345
  public int getObjectName(int numLayers)
346
    {
347
    switch(numLayers)
348
      {
349
      case 2: return R.string.cube2;
350
      case 3: return R.string.cube3;
351
      case 4: return R.string.cube4;
352
      case 5: return R.string.cube5;
353
      }
354
    return R.string.cube3;
355
    }
356
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
359
  public int getInventor(int numLayers)
360
    {
361
    switch(numLayers)
362
      {
363
      case 2: return R.string.cube2_inventor;
364
      case 3: return R.string.cube3_inventor;
365
      case 4: return R.string.cube4_inventor;
366
      case 5: return R.string.cube5_inventor;
367
      }
368
    return R.string.cube3_inventor;
369
    }
370
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372
373
  public int getComplexity(int numLayers)
374
    {
375
    switch(numLayers)
376
      {
377
      case 2: return 4;
378
      case 3: return 6;
379
      case 4: return 8;
380
      case 5: return 10;
381
      }
382
    return 6;
383
    }
384 0c52af30 Leszek Koltunski
}