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 java.util.ArrayList;
|
23
|
import java.util.Locale;
|
24
|
|
25
|
import android.content.Context;
|
26
|
import android.content.SharedPreferences;
|
27
|
|
28
|
import org.distorted.external.RubikFiles;
|
29
|
import org.distorted.main.RubikActivity;
|
30
|
import org.distorted.objectlib.main.ObjectSignatures;
|
31
|
import org.distorted.objectlib.main.ObjectType;
|
32
|
import org.distorted.screens.RubikScreenPlay;
|
33
|
import org.distorted.screens.ScreenList;
|
34
|
|
35
|
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
|
36
|
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
|
37
|
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public class RubikObjectList
|
42
|
{
|
43
|
public static final int DEF_OBJECT= ObjectSignatures.CUBE_3;
|
44
|
public static int MAX_LEVEL;
|
45
|
|
46
|
private static RubikObjectList mThis;
|
47
|
private static int mNumObjects;
|
48
|
private static int mNumExtras;
|
49
|
private static ArrayList<RubikObject> mObjects;
|
50
|
private static int mObject = DEF_OBJECT;
|
51
|
|
52
|
public static class DownloadedObject
|
53
|
{
|
54
|
String shortName;
|
55
|
boolean icon,object,extras;
|
56
|
int numScrambles, objectMinor, extrasMinor;
|
57
|
|
58
|
DownloadedObject(String sName, int scrambles, int oMinor, int eMinor, boolean i, boolean o, boolean e)
|
59
|
{
|
60
|
shortName = sName;
|
61
|
|
62
|
numScrambles= scrambles;
|
63
|
objectMinor = oMinor;
|
64
|
extrasMinor = eMinor;
|
65
|
|
66
|
icon = i;
|
67
|
object = o;
|
68
|
extras = e;
|
69
|
}
|
70
|
}
|
71
|
|
72
|
private static ArrayList<DownloadedObject> mDownloadedObjects;
|
73
|
|
74
|
static
|
75
|
{
|
76
|
int max = Integer.MIN_VALUE;
|
77
|
|
78
|
for (int i=0; i<NUM_OBJECTS; i++)
|
79
|
{
|
80
|
int cur = getDBLevel(i);
|
81
|
if( cur>max ) max = cur;
|
82
|
}
|
83
|
|
84
|
MAX_LEVEL = max;
|
85
|
}
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
private RubikObjectList()
|
90
|
{
|
91
|
mNumObjects= 0;
|
92
|
mNumExtras = 0;
|
93
|
|
94
|
mObjects = new ArrayList<>();
|
95
|
mDownloadedObjects = new ArrayList<>();
|
96
|
|
97
|
createBuiltinObjects();
|
98
|
}
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
|
102
|
private void createBuiltinObjects()
|
103
|
{
|
104
|
for(int i=0; i<NUM_OBJECTS; i++)
|
105
|
{
|
106
|
ObjectType type = ObjectType.getObject(i);
|
107
|
RubikObject obj = new RubikObject(type);
|
108
|
mObjects.add(obj);
|
109
|
mNumObjects++;
|
110
|
|
111
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "creating local object "+type.name() );
|
112
|
|
113
|
if( obj.hasExtras() )
|
114
|
{
|
115
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "has extras "+mNumExtras );
|
116
|
|
117
|
obj.setExtrasOrdinal(mNumExtras);
|
118
|
mNumExtras++;
|
119
|
}
|
120
|
else
|
121
|
{
|
122
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "no extras");
|
123
|
}
|
124
|
}
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
private static boolean internalAddDownloadedObject(DownloadedObject object)
|
130
|
{
|
131
|
String name = object.shortName;
|
132
|
|
133
|
for(RubikObject ro : mObjects )
|
134
|
if( ro.getLowerName().equals(name) )
|
135
|
{
|
136
|
return ro.updateObject(object);
|
137
|
}
|
138
|
|
139
|
RubikObject obj = new RubikObject(object);
|
140
|
mObjects.add(obj);
|
141
|
mNumObjects++;
|
142
|
|
143
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "creating downloaded object "+obj.getUpperName() );
|
144
|
|
145
|
if( obj.hasExtras() )
|
146
|
{
|
147
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "has extras "+mNumExtras );
|
148
|
|
149
|
obj.setExtrasOrdinal(mNumExtras);
|
150
|
mNumExtras++;
|
151
|
}
|
152
|
else
|
153
|
{
|
154
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "no extras");
|
155
|
}
|
156
|
|
157
|
return true;
|
158
|
}
|
159
|
|
160
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
161
|
// PUBLIC API
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
// historically older versions of the app had lower 'maxScrambles' in case of several objects and
|
164
|
// those got remembered in the server-side DB already, so we need to keep using them. This function
|
165
|
// provides a map between 'maxScramble' of an object and its 'dbLevel'. All new objects will have
|
166
|
// those two values the same.
|
167
|
|
168
|
public static int getDBLevel(int ordinal)
|
169
|
{
|
170
|
if( ordinal== ObjectSignatures.CUBE_3 ) return 16;
|
171
|
if( ordinal== ObjectSignatures.CUBE_4 ) return 20;
|
172
|
if( ordinal== ObjectSignatures.CUBE_5 ) return 24;
|
173
|
if( ordinal== ObjectSignatures.BAN2_3 ) return 16;
|
174
|
if( ordinal== ObjectSignatures.BAN4_3 ) return 16;
|
175
|
if( ordinal== ObjectSignatures.PYRA_4 ) return 15;
|
176
|
if( ordinal== ObjectSignatures.PYRA_5 ) return 20;
|
177
|
if( ordinal== ObjectSignatures.MEGA_5 ) return 35;
|
178
|
if( ordinal== ObjectSignatures.DIAM_2 ) return 10;
|
179
|
if( ordinal== ObjectSignatures.DIAM_3 ) return 18;
|
180
|
if( ordinal== ObjectSignatures.REDI_3 ) return 14;
|
181
|
if( ordinal== ObjectSignatures.HELI_3 ) return 18;
|
182
|
if( ordinal== ObjectSignatures.SKEW_3 ) return 17;
|
183
|
if( ordinal== ObjectSignatures.REX_3 ) return 16;
|
184
|
if( ordinal== ObjectSignatures.MIRR_3 ) return 16;
|
185
|
|
186
|
// in 1.9.6 & 1.9.7 there is a bug with downloadable objects (in this very function!):
|
187
|
// All of those have DBLevel equal to CUBE_3's DBlevel (in 1.9.7!), i.e. 17.
|
188
|
// This will be a problem when we release a new version of the app which has some of the
|
189
|
// previously downloadable objects built-in. Thus: in case of those, we need to keep using
|
190
|
// 17.
|
191
|
|
192
|
if( ObjectType.wasDownloadableButNowIsBuiltIn(ordinal) )
|
193
|
{
|
194
|
return 17;
|
195
|
}
|
196
|
|
197
|
return ObjectType.getObject(ordinal).getNumScramble();
|
198
|
}
|
199
|
|
200
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
201
|
|
202
|
public static void addDownloadedObject(Context context, String shortName, int numScrambles, int objectMinor,
|
203
|
int extrasMinor, boolean icon, boolean object, boolean extras)
|
204
|
{
|
205
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" icon="+icon+" object="+object+" extras="+extras);
|
206
|
|
207
|
for( DownloadedObject obj : mDownloadedObjects )
|
208
|
{
|
209
|
if( obj.shortName.equals(shortName) )
|
210
|
{
|
211
|
obj.icon |= icon;
|
212
|
obj.object|= object;
|
213
|
obj.extras|= extras;
|
214
|
|
215
|
if( !obj.object ) objectMinor=-1;
|
216
|
if( !obj.extras ) extrasMinor=-1;
|
217
|
|
218
|
obj.objectMinor = objectMinor;
|
219
|
obj.extrasMinor = extrasMinor;
|
220
|
|
221
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
|
222
|
|
223
|
try
|
224
|
{
|
225
|
RubikActivity ract = (RubikActivity)context;
|
226
|
ract.reloadObject(shortName);
|
227
|
}
|
228
|
catch(Exception ex)
|
229
|
{
|
230
|
android.util.Log.e("D", "exception trying to reload object: "+ex.getMessage() );
|
231
|
}
|
232
|
|
233
|
return;
|
234
|
}
|
235
|
}
|
236
|
|
237
|
if( !object ) objectMinor=-1;
|
238
|
if( !extras ) extrasMinor=-1;
|
239
|
|
240
|
DownloadedObject obj = new DownloadedObject(shortName,numScrambles,objectMinor,extrasMinor,icon,object,extras);
|
241
|
if ( internalAddDownloadedObject(obj) )
|
242
|
{
|
243
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Adding new downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
|
244
|
mDownloadedObjects.add(obj);
|
245
|
}
|
246
|
else
|
247
|
{
|
248
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" is already built-in, deleting");
|
249
|
|
250
|
mDownloadedObjects.remove(obj);
|
251
|
RubikFiles files = RubikFiles.getInstance();
|
252
|
files.deleteIcon(context,shortName);
|
253
|
files.deleteJsonObject(context,shortName);
|
254
|
files.deleteJsonExtras(context,shortName);
|
255
|
}
|
256
|
}
|
257
|
|
258
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
259
|
|
260
|
public static void setMeshState(int ordinal, int state)
|
261
|
{
|
262
|
if( ordinal>=0 && ordinal<mNumObjects ) mObjects.get(ordinal).setMeshState(state);
|
263
|
}
|
264
|
|
265
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266
|
|
267
|
public static int getMeshState(int ordinal)
|
268
|
{
|
269
|
return (ordinal>=0 && ordinal<mNumObjects) ? mObjects.get(ordinal).getMeshState() : MESH_NICE;
|
270
|
}
|
271
|
|
272
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
273
|
|
274
|
public static void savePreferences(SharedPreferences.Editor editor)
|
275
|
{
|
276
|
RubikObject obj = getObject(mObject);
|
277
|
if( obj!=null ) editor.putString("rol_objName", obj.getUpperName() );
|
278
|
|
279
|
int numDownloaded = mDownloadedObjects.size();
|
280
|
|
281
|
if( numDownloaded>0 )
|
282
|
{
|
283
|
StringBuilder downloadedObjects = new StringBuilder();
|
284
|
|
285
|
for(int i=0; i<numDownloaded; i++)
|
286
|
{
|
287
|
if( i>0 ) downloadedObjects.append(',');
|
288
|
|
289
|
DownloadedObject object = mDownloadedObjects.get(i);
|
290
|
downloadedObjects.append(object.shortName);
|
291
|
downloadedObjects.append(' ');
|
292
|
downloadedObjects.append(object.numScrambles);
|
293
|
downloadedObjects.append(' ');
|
294
|
downloadedObjects.append(object.objectMinor);
|
295
|
downloadedObjects.append(' ');
|
296
|
downloadedObjects.append(object.extrasMinor);
|
297
|
downloadedObjects.append(' ');
|
298
|
downloadedObjects.append(object.icon ? "1":"0");
|
299
|
downloadedObjects.append(' ');
|
300
|
downloadedObjects.append(object.object ? "1":"0");
|
301
|
downloadedObjects.append(' ');
|
302
|
downloadedObjects.append(object.extras ? "1":"0");
|
303
|
}
|
304
|
|
305
|
String objects = downloadedObjects.toString();
|
306
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "saving: "+objects);
|
307
|
editor.putString("rol_downloaded", objects );
|
308
|
}
|
309
|
else
|
310
|
{
|
311
|
editor.putString("rol_downloaded", "" );
|
312
|
}
|
313
|
}
|
314
|
|
315
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
316
|
|
317
|
public static void saveMeshState(SharedPreferences.Editor editor)
|
318
|
{
|
319
|
for(int i=0; i<mNumObjects; i++)
|
320
|
{
|
321
|
RubikObject obj = getObject(i);
|
322
|
|
323
|
if( obj!=null )
|
324
|
{
|
325
|
String name = obj.getUpperName();
|
326
|
editor.putInt("rol_"+name, obj.getMeshState() );
|
327
|
}
|
328
|
}
|
329
|
}
|
330
|
|
331
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
332
|
|
333
|
public static void restorePreferences(Context context, SharedPreferences preferences)
|
334
|
{
|
335
|
if( mThis==null ) mThis = new RubikObjectList();
|
336
|
|
337
|
String downloaded = preferences.getString("rol_downloaded","");
|
338
|
|
339
|
if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", downloaded);
|
340
|
|
341
|
if( !downloaded.equals(""))
|
342
|
{
|
343
|
String[] dObjects = downloaded.split(",");
|
344
|
|
345
|
for(String dObj : dObjects)
|
346
|
{
|
347
|
String[] parts = dObj.split(" ");
|
348
|
|
349
|
if( parts.length==7 )
|
350
|
{
|
351
|
String name = parts[0];
|
352
|
String scra = parts[1];
|
353
|
String objM = parts[2];
|
354
|
String extM = parts[3];
|
355
|
String icon = parts[4];
|
356
|
String obje = parts[5];
|
357
|
String extr = parts[6];
|
358
|
|
359
|
int scrambles = Integer.parseInt(scra);
|
360
|
int oMinor = Integer.parseInt(objM);
|
361
|
int eMinor = Integer.parseInt(extM);
|
362
|
|
363
|
boolean bIcon = icon.equals("1");
|
364
|
boolean bObje = obje.equals("1");
|
365
|
boolean bExtr = extr.equals("1");
|
366
|
|
367
|
addDownloadedObject(context,name,scrambles,oMinor,eMinor,bIcon,bObje,bExtr);
|
368
|
}
|
369
|
}
|
370
|
}
|
371
|
|
372
|
RubikObject object = getObject(DEF_OBJECT);
|
373
|
String defName = object==null ? "CUBE_3" : object.getUpperName();
|
374
|
String objName= preferences.getString("rol_objName",defName);
|
375
|
mObject = getOrdinal(objName);
|
376
|
if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
|
381
|
public static void restoreMeshState(SharedPreferences preferences)
|
382
|
{
|
383
|
for(int i=0; i<mNumObjects; i++)
|
384
|
{
|
385
|
RubikObject obj = getObject(i);
|
386
|
|
387
|
if( obj!=null )
|
388
|
{
|
389
|
String name = obj.getUpperName();
|
390
|
int meshState= preferences.getInt("rol_"+name,MESH_NICE);
|
391
|
obj.setMeshState(meshState);
|
392
|
}
|
393
|
}
|
394
|
}
|
395
|
|
396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
397
|
|
398
|
public static boolean setCurrObject(RubikActivity act, int ordinal)
|
399
|
{
|
400
|
if( mObject!=ordinal )
|
401
|
{
|
402
|
mObject = ordinal;
|
403
|
return true;
|
404
|
}
|
405
|
|
406
|
return false;
|
407
|
}
|
408
|
|
409
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
410
|
|
411
|
public static int getCurrObject()
|
412
|
{
|
413
|
return mObject;
|
414
|
}
|
415
|
|
416
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
417
|
|
418
|
public static RubikObject getObject(int ordinal)
|
419
|
{
|
420
|
if( mThis==null ) mThis = new RubikObjectList();
|
421
|
return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
|
422
|
}
|
423
|
|
424
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
425
|
|
426
|
public static RubikObject getObject(String shortUpperName)
|
427
|
{
|
428
|
if( mThis==null ) mThis = new RubikObjectList();
|
429
|
|
430
|
for(int i=0; i<mNumObjects; i++)
|
431
|
{
|
432
|
RubikObject object = mObjects.get(i);
|
433
|
if( object.getUpperName().equals(shortUpperName) )
|
434
|
{
|
435
|
return object;
|
436
|
}
|
437
|
}
|
438
|
|
439
|
return null;
|
440
|
}
|
441
|
|
442
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
443
|
|
444
|
public static int getNumObjects()
|
445
|
{
|
446
|
if( mThis==null ) mThis = new RubikObjectList();
|
447
|
return mNumObjects;
|
448
|
}
|
449
|
|
450
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
451
|
|
452
|
public static int getOrdinal(String name)
|
453
|
{
|
454
|
if( mThis==null ) mThis = new RubikObjectList();
|
455
|
|
456
|
String lowerName = name.toLowerCase(Locale.ENGLISH);
|
457
|
|
458
|
for(int i=0; i<mNumObjects; i++)
|
459
|
{
|
460
|
RubikObject obj = mObjects.get(i);
|
461
|
if( obj.getLowerName().equals(lowerName) ) return i;
|
462
|
}
|
463
|
|
464
|
return -1;
|
465
|
}
|
466
|
|
467
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
468
|
|
469
|
public static int getNumExtrasObjects()
|
470
|
{
|
471
|
return mNumExtras;
|
472
|
}
|
473
|
|
474
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
475
|
|
476
|
public static int getNumTutorialObjects()
|
477
|
{
|
478
|
return mNumExtras;
|
479
|
}
|
480
|
|
481
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
482
|
|
483
|
public static int getObjectOrdinal(int extrasOrdinal)
|
484
|
{
|
485
|
for(int i=extrasOrdinal; i<mNumObjects; i++)
|
486
|
{
|
487
|
RubikObject object = getObject(i);
|
488
|
int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
|
489
|
if( extOrd==extrasOrdinal ) return i;
|
490
|
}
|
491
|
|
492
|
return -1;
|
493
|
}
|
494
|
|
495
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
496
|
|
497
|
public static int getExtrasOrdinal(int objectOrdinal)
|
498
|
{
|
499
|
RubikObject object = getObject(objectOrdinal);
|
500
|
return object!=null ? object.getExtrasOrdinal() : -1;
|
501
|
}
|
502
|
|
503
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
504
|
|
505
|
public static int getTutorialOrdinal(int objectOrdinal)
|
506
|
{
|
507
|
RubikObject object = getObject(objectOrdinal);
|
508
|
return object!=null ? object.getExtrasOrdinal() : -1;
|
509
|
}
|
510
|
|
511
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
512
|
|
513
|
public static int getLocalObjectMinor(int objectOrdinal)
|
514
|
{
|
515
|
RubikObject object = getObject(objectOrdinal);
|
516
|
return object!=null ? object.getObjectMinor() : -1;
|
517
|
}
|
518
|
|
519
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
520
|
|
521
|
public static int getLocalExtrasMinor(int objectOrdinal)
|
522
|
{
|
523
|
RubikObject object = getObject(objectOrdinal);
|
524
|
return object!=null ? object.getExtrasMinor() : -1;
|
525
|
}
|
526
|
}
|