Project

General

Profile

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

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

1 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 e844c116 Leszek Koltunski
22 ccf9fec5 Leszek Koltunski
import android.content.res.Resources;
23 e844c116 Leszek Koltunski
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
26
import org.distorted.library.main.DistortedEffects;
27
import org.distorted.library.main.DistortedTexture;
28
import org.distorted.library.mesh.MeshBase;
29 efa8aa48 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
30 e844c116 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 e844c116 Leszek Koltunski
34 7c969a6d Leszek Koltunski
import java.util.Random;
35
36 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38 9c2f0c91 Leszek Koltunski
public class TwistyPyraminx extends TwistyObject
39 e844c116 Leszek Koltunski
{
40 ad38d800 Leszek Koltunski
  static final Static3D[] ROT_AXIS = new Static3D[]
41 e844c116 Leszek Koltunski
         {
42 ac940e24 Leszek Koltunski
           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 ad38d800 Leszek Koltunski
         };
47
48 e844c116 Leszek Koltunski
  private static final int[] FACE_COLORS = new int[]
49
         {
50 ece1b58d Leszek Koltunski
           COLOR_GREEN , COLOR_YELLOW,
51
           COLOR_BLUE  , COLOR_RED
52 e844c116 Leszek Koltunski
         };
53
54 9f4c44fe Leszek Koltunski
  // computed with res/raw/compute_quats.c
55 10585385 Leszek Koltunski
  private static final Static4D[] QUATS = new Static4D[]
56 e844c116 Leszek Koltunski
         {
57 10585385 Leszek Koltunski
           new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
58 ac940e24 Leszek Koltunski
           new Static4D(  0.0f,   1.0f,   0.0f,  0.0f),
59
           new Static4D( SQ2/2,   0.5f,   0.0f,  0.5f),
60
           new Static4D(-SQ2/2,   0.5f,   0.0f,  0.5f),
61
           new Static4D(  0.0f,  -0.5f, -SQ2/2,  0.5f),
62
           new Static4D(  0.0f,  -0.5f,  SQ2/2,  0.5f),
63
           new Static4D( SQ2/2,   0.5f,   0.0f, -0.5f),
64
           new Static4D(-SQ2/2,   0.5f,   0.0f, -0.5f),
65
           new Static4D(  0.0f,  -0.5f, -SQ2/2, -0.5f),
66
           new Static4D(  0.0f,  -0.5f,  SQ2/2, -0.5f),
67
           new Static4D( SQ2/2,   0.0f,  SQ2/2,  0.0f),
68
           new Static4D(-SQ2/2,   0.0f,  SQ2/2,  0.0f)
69 e844c116 Leszek Koltunski
         };
70
71 ac940e24 Leszek Koltunski
  private static MeshBase mOctaMesh, mTetraMesh;
72 49f67f9b Leszek Koltunski
73 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 ac940e24 Leszek Koltunski
  TwistyPyraminx(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
76
                 DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
77 e844c116 Leszek Koltunski
    {
78 db875721 Leszek Koltunski
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.PYRA, res, scrWidth);
79 e844c116 Leszek Koltunski
    }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83 e6cf7283 Leszek Koltunski
  private void addTetrahedralLattice(int size, int index, float[][] pos)
84 49f67f9b Leszek Koltunski
    {
85 ac940e24 Leszek Koltunski
    final float DX = 1.0f;
86
    final float DY = SQ2/2;
87
    final float DZ = 1.0f;
88 49f67f9b Leszek Koltunski
89 ac940e24 Leszek Koltunski
    float startX = 0.0f;
90
    float startY =-DY*(size-1)/2;
91
    float startZ = DZ*(size-1)/2;
92 769409d2 Leszek Koltunski
93 ac940e24 Leszek Koltunski
    for(int layer=0; layer<size; layer++)
94 49f67f9b Leszek Koltunski
      {
95 ac940e24 Leszek Koltunski
      float currX = startX;
96
      float currY = startY;
97
98
      for(int x=0; x<layer+1; x++)
99
        {
100
        float currZ = startZ;
101
102
        for(int z=0; z<size-layer; z++)
103
          {
104 e6cf7283 Leszek Koltunski
          pos[index] = new float[] {currX,currY,currZ};
105 ac940e24 Leszek Koltunski
          index++;
106
          currZ -= DZ;
107
          }
108
109
        currX += DX;
110
        }
111
112
      startX-=DX/2;
113
      startY+=DY;
114
      startZ-=DZ/2;
115 49f67f9b Leszek Koltunski
      }
116
    }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119 ac940e24 Leszek Koltunski
// there are (n^3-n)/6 octahedrons and ((n+1)^3 - (n+1))/6 tetrahedrons
120 49f67f9b Leszek Koltunski
121 e6cf7283 Leszek Koltunski
  float[][] getCubitPositions(int size)
122 e844c116 Leszek Koltunski
    {
123 ac940e24 Leszek Koltunski
    int numOcta = (size-1)*size*(size+1)/6;
124
    int numTetra= size*(size+1)*(size+2)/6;
125 e6cf7283 Leszek Koltunski
    float[][] ret = new float[numOcta+numTetra][];
126 49f67f9b Leszek Koltunski
127 ac940e24 Leszek Koltunski
    addTetrahedralLattice(size-1,      0,ret);
128
    addTetrahedralLattice(size  ,numOcta,ret);
129 49f67f9b Leszek Koltunski
130 ac940e24 Leszek Koltunski
    return ret;
131 e844c116 Leszek Koltunski
    }
132
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135 10585385 Leszek Koltunski
  Static4D[] getQuats()
136 e844c116 Leszek Koltunski
    {
137 10585385 Leszek Koltunski
    return QUATS;
138 e844c116 Leszek Koltunski
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142
  int getNumFaces()
143
    {
144
    return FACE_COLORS.length;
145
    }
146
147 eab9d8f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
148
149 a64e07d0 Leszek Koltunski
  int getNumStickerTypes(int numLayers)
150 eab9d8f8 Leszek Koltunski
    {
151
    return 1;
152
    }
153
154 7403cdfa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156 a97e02b7 Leszek Koltunski
  float[] getCuts(int size)
157 7403cdfa Leszek Koltunski
    {
158 a97e02b7 Leszek Koltunski
    float[] cuts = new float[size-1];
159
160
    for(int i=0; i<size-1; i++)
161
      {
162
      cuts[i] = (1.0f-0.25f*size+i)*(SQ6/3);
163
      }
164
165
    return cuts;
166 7403cdfa Leszek Koltunski
    }
167
168 8f53e513 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170
  int getNumCubitFaces()
171
    {
172 ac940e24 Leszek Koltunski
    return 8;
173 8f53e513 Leszek Koltunski
    }
174
175 f0fa83ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
  float getScreenRatio()
178
    {
179 7381193e Leszek Koltunski
    return 0.82f;
180 f0fa83ae Leszek Koltunski
    }
181
182 eaee1ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
  boolean shouldResetTextureMaps()
185
    {
186
    return false;
187
    }
188
189 f6d06256 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191 ac940e24 Leszek Koltunski
  private int faceColor(int cubit, int axis)
192 f6d06256 Leszek Koltunski
    {
193 f0450fcc Leszek Koltunski
    return CUBITS[cubit].mRotationRow[axis] == 1 ? axis : NUM_FACES;
194 f6d06256 Leszek Koltunski
    }
195
196 f0fa83ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198 ac940e24 Leszek Koltunski
  int getFaceColor(int cubit, int cubitface, int size)
199 e844c116 Leszek Koltunski
    {
200 ac940e24 Leszek Koltunski
    if( cubit< (size-1)*size*(size+1)/6 )
201 40ab026e Leszek Koltunski
      {
202 ac940e24 Leszek Koltunski
      switch( cubitface )
203
        {
204
        case 0: return faceColor(cubit,0);
205
        case 2: return faceColor(cubit,1);
206
        case 5: return faceColor(cubit,3);
207
        case 7: return faceColor(cubit,2);
208
        default:return NUM_FACES;
209
        }
210 40ab026e Leszek Koltunski
      }
211 ac940e24 Leszek Koltunski
    else
212 89a11f7b Leszek Koltunski
      {
213 ac940e24 Leszek Koltunski
      return cubitface<NUM_FACES ? faceColor(cubit,cubitface) : NUM_FACES;
214 89a11f7b Leszek Koltunski
      }
215 e844c116 Leszek Koltunski
    }
216
217 40ab026e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219 a64e07d0 Leszek Koltunski
  MeshBase createCubitMesh(int cubit, int numLayers)
220 40ab026e Leszek Koltunski
    {
221 d99f3a48 Leszek Koltunski
    if( cubit< (numLayers-1)*numLayers*(numLayers+1)/6 )
222 40ab026e Leszek Koltunski
      {
223 b89898c5 Leszek Koltunski
      if( mOctaMesh==null ) mOctaMesh = FactoryCubit.getInstance().createOctaMesh();
224 ac940e24 Leszek Koltunski
      return mOctaMesh.copy(true);
225 40ab026e Leszek Koltunski
      }
226
    else
227
      {
228 b89898c5 Leszek Koltunski
      if( mTetraMesh==null ) mTetraMesh = FactoryCubit.getInstance().createTetraMesh();
229 ac940e24 Leszek Koltunski
      return mTetraMesh.copy(true);
230 40ab026e Leszek Koltunski
      }
231
    }
232
233 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235 ae755eda Leszek Koltunski
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
236 7289fd6c Leszek Koltunski
    {
237 ae755eda Leszek Koltunski
    float E = 0.75f;
238
    float F = 0.50f;
239 76c2bd07 Leszek Koltunski
    float R = 0.06f;
240
    float S = 0.08f;
241
    float[] vertices = { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
242
243 b89898c5 Leszek Koltunski
    FactorySticker factory = FactorySticker.getInstance();
244 ae755eda Leszek Koltunski
    factory.drawRoundedPolygon(canvas, paint, left, top, vertices, S, FACE_COLORS[face], R);
245 7289fd6c Leszek Koltunski
    }
246
247 fb377dae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
248 7403cdfa Leszek Koltunski
// SQ6/3 = height of the tetrahedron
249 fb377dae Leszek Koltunski
250
  float returnMultiplier()
251
    {
252 d99f3a48 Leszek Koltunski
    return getNumLayers()/(SQ6/3);
253 fb377dae Leszek Koltunski
    }
254
255 7c969a6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
256
257 a64e07d0 Leszek Koltunski
  float[] getRowChances(int numLayers)
258 7c969a6d Leszek Koltunski
    {
259 d99f3a48 Leszek Koltunski
    int total = numLayers*(numLayers+1)/2;
260 7c969a6d Leszek Koltunski
    float running=0.0f;
261 d99f3a48 Leszek Koltunski
    float[] chances = new float[numLayers];
262 7c969a6d Leszek Koltunski
263 d99f3a48 Leszek Koltunski
    for(int i=0; i<numLayers; i++)
264 7c969a6d Leszek Koltunski
      {
265 d99f3a48 Leszek Koltunski
      running += (numLayers-i);
266 7c969a6d Leszek Koltunski
      chances[i] = running / total;
267
      }
268
269
    return chances;
270
    }
271
272 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
273
// PUBLIC API
274
275 12ad3fca Leszek Koltunski
  public Static3D[] getRotationAxis()
276
    {
277 ad38d800 Leszek Koltunski
    return ROT_AXIS;
278 12ad3fca Leszek Koltunski
    }
279
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282 e844c116 Leszek Koltunski
  public int getBasicAngle()
283
    {
284
    return 3;
285
    }
286 39e74052 Leszek Koltunski
287 7c969a6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289 5043d5d0 Leszek Koltunski
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
290 7c969a6d Leszek Koltunski
    {
291 5043d5d0 Leszek Koltunski
    if( num==0 )
292 5cf34c5f Leszek Koltunski
      {
293 5043d5d0 Leszek Koltunski
      scramble[num][0] = rnd.nextInt(ROTATION_AXIS.length);
294 7c969a6d Leszek Koltunski
      }
295
    else
296
      {
297 bbc6471c Leszek Koltunski
      int newVector = rnd.nextInt(ROTATION_AXIS.length-1);
298 5043d5d0 Leszek Koltunski
      scramble[num][0] = (newVector>=scramble[num-1][0] ? newVector+1 : newVector);
299 5cf34c5f Leszek Koltunski
      }
300 e46e17fb Leszek Koltunski
301 7c969a6d Leszek Koltunski
    float rowFloat = rnd.nextFloat();
302 e46e17fb Leszek Koltunski
303 7c969a6d Leszek Koltunski
    for(int row=0; row<mRowChances.length; row++)
304
      {
305 bbc6471c Leszek Koltunski
      if( rowFloat<=mRowChances[row] )
306
        {
307 5043d5d0 Leszek Koltunski
        scramble[num][1] = row;
308 bbc6471c Leszek Koltunski
        break;
309
        }
310 7c969a6d Leszek Koltunski
      }
311
312 5043d5d0 Leszek Koltunski
    switch( rnd.nextInt(2) )
313
      {
314
      case 0: scramble[num][2] = -1; break;
315
      case 1: scramble[num][2] =  1; break;
316
      }
317 e46e17fb Leszek Koltunski
    }
318 f0336037 Leszek Koltunski
319 6b6504fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
320
321
  public boolean isSolved()
322
    {
323
    int index = CUBITS[0].mQuatIndex;
324
325
    for(int i=1; i<NUM_CUBITS; i++)
326
      {
327 722b2512 Leszek Koltunski
      if( thereIsVisibleDifference(CUBITS[i], index) ) return false;
328 6b6504fe Leszek Koltunski
      }
329
330
    return true;
331
    }
332
333 221a4090 Leszek Koltunski
////////////////////////////////////////////////////////////////////////
334 ee35e63c Leszek Koltunski
// only needed for solvers - there are no Pyraminx solvers ATM)
335 f0336037 Leszek Koltunski
336 20931cf6 Leszek Koltunski
  public String retObjectString()
337 f0336037 Leszek Koltunski
    {
338
    return "";
339
    }
340 6fd4a72c Leszek Koltunski
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
343
  public int getObjectName(int numLayers)
344
    {
345
    switch(numLayers)
346
      {
347
      case 3: return R.string.pyra3;
348
      case 4: return R.string.pyra4;
349
      case 5: return R.string.pyra5;
350
      }
351
    return R.string.pyra3;
352
    }
353
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
356
  public int getInventor(int numLayers)
357
    {
358
    switch(numLayers)
359
      {
360
      case 3: return R.string.pyra3_inventor;
361
      case 4: return R.string.pyra4_inventor;
362
      case 5: return R.string.pyra5_inventor;
363
      }
364
    return R.string.pyra3_inventor;
365
    }
366
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368
369
  public int getComplexity(int numLayers)
370
    {
371
    switch(numLayers)
372
      {
373
      case 3: return 4;
374
      case 4: return 6;
375
      case 5: return 8;
376
      }
377
    return 4;
378
    }
379 e844c116 Leszek Koltunski
}