1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
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
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
|
35
|
abstract class TwistyBandagedAbstract extends TwistyObject
|
36
|
{
|
37
|
// the three rotation axis of a 3x3 Cube. Must be normalized.
|
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 static final int[] FACE_COLORS = new int[]
|
46
|
{
|
47
|
COLOR_YELLOW, COLOR_WHITE,
|
48
|
COLOR_BLUE , COLOR_GREEN,
|
49
|
COLOR_RED , COLOR_ORANGE
|
50
|
};
|
51
|
|
52
|
private static final int[][] mDimensions = new int[][]
|
53
|
{
|
54
|
{1,1,1}, // has to be X>=Z>=Y so that all
|
55
|
{2,1,1}, // the faces are horizontal
|
56
|
{3,1,1},
|
57
|
{2,1,2},
|
58
|
{2,2,2}
|
59
|
};
|
60
|
|
61
|
private static final int NUM_STICKERS = 4;
|
62
|
|
63
|
private int[] mBasicAngle;
|
64
|
private Static4D[] mQuats;
|
65
|
private ObjectSticker[] mStickers;
|
66
|
private Static4D[] mInitQuats;
|
67
|
private int[][] mAxisMap;
|
68
|
private int[][] mFaceMap;
|
69
|
ScrambleState[] mStates;
|
70
|
float[][] POSITIONS;
|
71
|
int[] QUAT_INDICES;
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
TwistyBandagedAbstract(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
|
76
|
DistortedEffects effects, int[][] moves, ObjectList list, Resources res, int scrWidth)
|
77
|
{
|
78
|
super(size, size, quat, texture, mesh, effects, moves, list, res, scrWidth);
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
abstract float[][] getPositions();
|
84
|
abstract int[] getQuatIndices();
|
85
|
|
86
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87
|
|
88
|
private void initializeQuats()
|
89
|
{
|
90
|
mQuats = new Static4D[]
|
91
|
{
|
92
|
new Static4D( 0.0f, 0.0f, 0.0f, 1.0f),
|
93
|
new Static4D( 1.0f, 0.0f, 0.0f, 0.0f),
|
94
|
new Static4D( 0.0f, 1.0f, 0.0f, 0.0f),
|
95
|
new Static4D( 0.0f, 0.0f, 1.0f, 0.0f),
|
96
|
|
97
|
new Static4D( SQ2/2, SQ2/2, 0.0f , 0.0f),
|
98
|
new Static4D( SQ2/2, -SQ2/2, 0.0f , 0.0f),
|
99
|
new Static4D( SQ2/2, 0.0f, SQ2/2, 0.0f),
|
100
|
new Static4D(-SQ2/2, 0.0f, SQ2/2, 0.0f),
|
101
|
new Static4D( SQ2/2, 0.0f, 0.0f, SQ2/2),
|
102
|
new Static4D( SQ2/2, 0.0f, 0.0f, -SQ2/2),
|
103
|
new Static4D( 0.0f, SQ2/2, SQ2/2, 0.0f),
|
104
|
new Static4D( 0.0f, SQ2/2, -SQ2/2, 0.0f),
|
105
|
new Static4D( 0.0f, SQ2/2, 0.0f, SQ2/2),
|
106
|
new Static4D( 0.0f, SQ2/2, 0.0f, -SQ2/2),
|
107
|
new Static4D( 0.0f, 0.0f, SQ2/2, SQ2/2),
|
108
|
new Static4D( 0.0f, 0.0f, SQ2/2, -SQ2/2),
|
109
|
|
110
|
new Static4D( 0.5f, 0.5f, 0.5f, 0.5f),
|
111
|
new Static4D( 0.5f, 0.5f, -0.5f, 0.5f),
|
112
|
new Static4D( 0.5f, 0.5f, -0.5f, -0.5f),
|
113
|
new Static4D( 0.5f, -0.5f, 0.5f, -0.5f),
|
114
|
new Static4D( -0.5f, -0.5f, -0.5f, 0.5f),
|
115
|
new Static4D( -0.5f, 0.5f, -0.5f, -0.5f),
|
116
|
new Static4D( -0.5f, 0.5f, 0.5f, -0.5f),
|
117
|
new Static4D( -0.5f, 0.5f, 0.5f, 0.5f)
|
118
|
};
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
int[] getSolvedQuats(int cubit, int numLayers)
|
124
|
{
|
125
|
if( mQuats==null ) initializeQuats();
|
126
|
int status = retCubitSolvedStatus(cubit,numLayers);
|
127
|
return status<0 ? null : buildSolvedQuats(MovementCube.FACE_AXIS[status],mQuats);
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
int getNumCubits()
|
133
|
{
|
134
|
return getPositions().length;
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
|
139
|
private float[] getCubitPosition(int cubit)
|
140
|
{
|
141
|
float[][] pos = getPositions();
|
142
|
|
143
|
return ( cubit>=0 && cubit< pos.length ) ? pos[cubit] : null;
|
144
|
}
|
145
|
|
146
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147
|
|
148
|
private int getQuatIndex(int cubit)
|
149
|
{
|
150
|
int[] indices = getQuatIndices();
|
151
|
return ( cubit>=0 && cubit< indices.length ) ? indices[cubit] : 0;
|
152
|
}
|
153
|
|
154
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
155
|
|
156
|
ObjectShape getObjectShape(int cubit, int numLayers)
|
157
|
{
|
158
|
int variant = getCubitVariant(cubit,numLayers);
|
159
|
|
160
|
final int[][] vert_indices =
|
161
|
{
|
162
|
{2,3,1,0},
|
163
|
{7,6,4,5},
|
164
|
{4,0,1,5},
|
165
|
{7,3,2,6},
|
166
|
{6,2,0,4},
|
167
|
{3,7,5,1},
|
168
|
};
|
169
|
|
170
|
float defHeight = 0.048f;
|
171
|
int[] bandIndices = new int[] { 0,0,1,1,2,2 };
|
172
|
float[][] corners = new float[][] { {0.04f,0.15f} };
|
173
|
int[] cornerIndices = new int[] { 0,0,0,0,0,0,0,0 };
|
174
|
int[] centerIndices = new int[] { 0,1,2,3,4,5,6,7 };
|
175
|
|
176
|
int X = mDimensions[variant][0];
|
177
|
int Y = mDimensions[variant][1];
|
178
|
int Z = mDimensions[variant][2];
|
179
|
|
180
|
int maxXY = Math.max(X,Y);
|
181
|
int maxXZ = Math.max(X,Z);
|
182
|
int maxYZ = Math.max(Y,Z);
|
183
|
|
184
|
double[][] vertices =
|
185
|
{
|
186
|
{+0.5f*X,+0.5f*Y,+0.5f*Z},
|
187
|
{+0.5f*X,+0.5f*Y,-0.5f*Z},
|
188
|
{+0.5f*X,-0.5f*Y,+0.5f*Z},
|
189
|
{+0.5f*X,-0.5f*Y,-0.5f*Z},
|
190
|
{-0.5f*X,+0.5f*Y,+0.5f*Z},
|
191
|
{-0.5f*X,+0.5f*Y,-0.5f*Z},
|
192
|
{-0.5f*X,-0.5f*Y,+0.5f*Z},
|
193
|
{-0.5f*X,-0.5f*Y,-0.5f*Z}
|
194
|
};
|
195
|
|
196
|
float[][] bands= new float[][]
|
197
|
{
|
198
|
{defHeight/maxYZ,65,0.25f,0.5f,5,1,2},
|
199
|
{defHeight/maxXZ,65,0.25f,0.5f,5,1,2},
|
200
|
{defHeight/maxXY,65,0.25f,0.5f,5,1,2}
|
201
|
};
|
202
|
|
203
|
float[][] centers = new float[][]
|
204
|
{
|
205
|
{+0.5f*(X-1),+0.5f*(Y-1),+0.5f*(Z-1)},
|
206
|
{+0.5f*(X-1),+0.5f*(Y-1),-0.5f*(Z-1)},
|
207
|
{+0.5f*(X-1),-0.5f*(Y-1),+0.5f*(Z-1)},
|
208
|
{+0.5f*(X-1),-0.5f*(Y-1),-0.5f*(Z-1)},
|
209
|
{-0.5f*(X-1),+0.5f*(Y-1),+0.5f*(Z-1)},
|
210
|
{-0.5f*(X-1),+0.5f*(Y-1),-0.5f*(Z-1)},
|
211
|
{-0.5f*(X-1),-0.5f*(Y-1),+0.5f*(Z-1)},
|
212
|
{-0.5f*(X-1),-0.5f*(Y-1),-0.5f*(Z-1)}
|
213
|
};
|
214
|
|
215
|
return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
|
216
|
}
|
217
|
|
218
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219
|
|
220
|
Static4D getQuat(int cubit, int numLayers)
|
221
|
{
|
222
|
if( mInitQuats ==null )
|
223
|
{
|
224
|
mInitQuats = new Static4D[]
|
225
|
{
|
226
|
new Static4D( 0.0f, 0.0f, 0.0f, 1.0f), // NULL
|
227
|
new Static4D( SQ2/2, 0.0f, 0.0f, -SQ2/2), // X
|
228
|
new Static4D( 0.0f, SQ2/2, 0.0f, -SQ2/2), // Y
|
229
|
new Static4D( 0.0f, 0.0f, SQ2/2, -SQ2/2), // Z
|
230
|
new Static4D( -0.5f, +0.5f, -0.5f, +0.5f), // ZX
|
231
|
new Static4D( +0.5f, +0.5f, +0.5f, -0.5f), // YX
|
232
|
};
|
233
|
}
|
234
|
|
235
|
return mInitQuats[getQuatIndex(cubit)];
|
236
|
}
|
237
|
|
238
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
239
|
|
240
|
int getNumCubitVariants(int numLayers)
|
241
|
{
|
242
|
return mDimensions.length;
|
243
|
}
|
244
|
|
245
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
246
|
|
247
|
int getCubitVariant(int cubit, int numLayers)
|
248
|
{
|
249
|
float[][] pos = getPositions();
|
250
|
|
251
|
if( cubit>=0 && cubit<pos.length )
|
252
|
{
|
253
|
int numPoints = pos[cubit].length/3;
|
254
|
return numPoints==8 ? 4 : numPoints-1;
|
255
|
}
|
256
|
|
257
|
return 1;
|
258
|
}
|
259
|
|
260
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261
|
|
262
|
int getColor(int face)
|
263
|
{
|
264
|
return FACE_COLORS[face];
|
265
|
}
|
266
|
|
267
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
268
|
|
269
|
ObjectSticker retSticker(int face)
|
270
|
{
|
271
|
if( mStickers==null )
|
272
|
{
|
273
|
mStickers = new ObjectSticker[NUM_STICKERS];
|
274
|
|
275
|
int[][] stickerDimensions = new int[][]
|
276
|
{
|
277
|
{1,1}, // dimensions of the faces of
|
278
|
{2,1}, // the cuboids defined in mDimensions
|
279
|
{3,1},
|
280
|
{2,2}
|
281
|
};
|
282
|
|
283
|
for(int s=0; s<NUM_STICKERS; s++)
|
284
|
{
|
285
|
float X = stickerDimensions[s][0];
|
286
|
float Y = stickerDimensions[s][1];
|
287
|
float MAX = Math.max(X,Y);
|
288
|
X /= (2*MAX);
|
289
|
Y /= (2*MAX);
|
290
|
|
291
|
float R = 0.10f / MAX;
|
292
|
float S = 0.08f / MAX;
|
293
|
float[] coords = { -X,-Y, +X,-Y, +X,+Y, -X,+Y};
|
294
|
float[] radii = new float[] {R,R,R,R};
|
295
|
mStickers[s] = new ObjectSticker(coords,null,radii,S);
|
296
|
}
|
297
|
}
|
298
|
|
299
|
return mStickers[face/NUM_FACE_COLORS];
|
300
|
}
|
301
|
|
302
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
303
|
|
304
|
float[][] getCubitPositions(int size)
|
305
|
{
|
306
|
int numCubits = getNumCubits();
|
307
|
float[][] tmp = new float[numCubits][];
|
308
|
|
309
|
for(int cubit=0; cubit<numCubits; cubit++)
|
310
|
{
|
311
|
tmp[cubit] = getCubitPosition(cubit);
|
312
|
}
|
313
|
|
314
|
return tmp;
|
315
|
}
|
316
|
|
317
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
318
|
|
319
|
Static4D[] getQuats()
|
320
|
{
|
321
|
if( mQuats==null ) initializeQuats();
|
322
|
return mQuats;
|
323
|
}
|
324
|
|
325
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
326
|
|
327
|
boolean shouldResetTextureMaps()
|
328
|
{
|
329
|
return false;
|
330
|
}
|
331
|
|
332
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
333
|
|
334
|
int getNumFaceColors()
|
335
|
{
|
336
|
return FACE_COLORS.length;
|
337
|
}
|
338
|
|
339
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
340
|
|
341
|
float[][] getCuts(int numLayers)
|
342
|
{
|
343
|
float[][] cuts = new float[3][numLayers-1];
|
344
|
|
345
|
for(int i=0; i<numLayers-1; i++)
|
346
|
{
|
347
|
float cut = (2-numLayers)*0.5f + i;
|
348
|
cuts[0][i] = cut;
|
349
|
cuts[1][i] = cut;
|
350
|
cuts[2][i] = cut;
|
351
|
}
|
352
|
|
353
|
return cuts;
|
354
|
}
|
355
|
|
356
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
357
|
|
358
|
int getSolvedFunctionIndex()
|
359
|
{
|
360
|
return 0;
|
361
|
}
|
362
|
|
363
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
364
|
|
365
|
int getNumStickerTypes(int numLayers)
|
366
|
{
|
367
|
return NUM_STICKERS;
|
368
|
}
|
369
|
|
370
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
371
|
|
372
|
int getNumCubitFaces()
|
373
|
{
|
374
|
return FACE_COLORS.length;
|
375
|
}
|
376
|
|
377
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
378
|
|
379
|
float getScreenRatio()
|
380
|
{
|
381
|
return 0.5f;
|
382
|
}
|
383
|
|
384
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
385
|
|
386
|
private int retStickerIndex(int horzSize, int vertSize)
|
387
|
{
|
388
|
switch(horzSize)
|
389
|
{
|
390
|
case 1: return 0;
|
391
|
case 2: return vertSize==1 ? 1:3;
|
392
|
case 3: return 2;
|
393
|
}
|
394
|
|
395
|
return 0;
|
396
|
}
|
397
|
|
398
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
399
|
|
400
|
private int getStickerIndex(int cubitface, int[] dim)
|
401
|
{
|
402
|
switch(cubitface)
|
403
|
{
|
404
|
case 0: case 1: return retStickerIndex(dim[2],dim[1]);
|
405
|
case 2: case 3: return retStickerIndex(dim[0],dim[2]);
|
406
|
case 4: case 5: return retStickerIndex(dim[0],dim[1]);
|
407
|
}
|
408
|
|
409
|
return 0;
|
410
|
}
|
411
|
|
412
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
413
|
|
414
|
int getFaceColor(int cubit, int cubitface, int numLayers)
|
415
|
{
|
416
|
if( mFaceMap==null )
|
417
|
{
|
418
|
// cubitface=2 when rotated by quatIndex=1 gets moved to position mFaceMap[2][1]
|
419
|
mFaceMap = new int[][]
|
420
|
{
|
421
|
{0,0,5,2,4,2},
|
422
|
{1,1,4,3,5,3},
|
423
|
{2,4,2,1,1,4},
|
424
|
{3,5,3,0,0,5},
|
425
|
{4,3,0,4,3,0},
|
426
|
{5,2,1,5,2,1}
|
427
|
};
|
428
|
}
|
429
|
|
430
|
if( mAxisMap==null )
|
431
|
{
|
432
|
// axis=1 when rotated by quatIndex=2 gets moved to axis mAxisMap[1][2]
|
433
|
mAxisMap = new int[][] { {0,0,2,1,2,1}, {1,2,1,0,0,2}, {2,1,0,2,1,0} };
|
434
|
}
|
435
|
|
436
|
int variant = getCubitVariant(cubit,numLayers);
|
437
|
int[] dim = mDimensions[variant];
|
438
|
float[] pos = getCubitPosition(cubit);
|
439
|
int stickerIndex = getStickerIndex(cubitface,dim);
|
440
|
int quatIndex = getQuatIndex(cubit);
|
441
|
int face = mFaceMap[cubitface][quatIndex];
|
442
|
int multiplier = (face%2)==0 ? 1:-1;
|
443
|
int posIndex = face/2;
|
444
|
int dimIndex = mAxisMap[posIndex][quatIndex];
|
445
|
|
446
|
float position = 0.0f;
|
447
|
int len = pos.length/3;
|
448
|
for(int i=0; i<len; i++) position += pos[3*i+posIndex];
|
449
|
position /= len;
|
450
|
|
451
|
boolean reaches = multiplier*position + dim[dimIndex]*0.5f > (numLayers-1)*0.5f;
|
452
|
|
453
|
return reaches ? stickerIndex*NUM_FACE_COLORS + face : NUM_TEXTURES;
|
454
|
}
|
455
|
|
456
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
457
|
|
458
|
float returnMultiplier()
|
459
|
{
|
460
|
return getNumLayers();
|
461
|
}
|
462
|
|
463
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
464
|
// PUBLIC API
|
465
|
|
466
|
public Static3D[] getRotationAxis()
|
467
|
{
|
468
|
return ROT_AXIS;
|
469
|
}
|
470
|
|
471
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
472
|
|
473
|
public int[] getBasicAngle()
|
474
|
{
|
475
|
if( mBasicAngle ==null ) mBasicAngle = new int[] { 4,4,4 };
|
476
|
return mBasicAngle;
|
477
|
}
|
478
|
}
|