Project

General

Profile

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

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

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