Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ b72b71a1

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