Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 6f3af598

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

    
24
import android.content.Context;
25
import android.content.SharedPreferences;
26

    
27
import org.distorted.external.RubikFiles;
28
import org.distorted.main.RubikActivity;
29
import org.distorted.objectlib.main.ObjectSignatures;
30
import org.distorted.objectlib.main.ObjectType;
31
import org.distorted.screens.RubikScreenPlay;
32
import org.distorted.screens.ScreenList;
33

    
34
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
35
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
36
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class RubikObjectList
41
{
42
  public static final int DEF_OBJECT= ObjectSignatures.CUBE_3;
43
  public static int MAX_LEVEL;
44

    
45
  private static RubikObjectList mThis;
46
  private static int mNumObjects;
47
  private static int mNumExtras;
48
  private static ArrayList<RubikObject> mObjects;
49
  private static int mObject = DEF_OBJECT;
50

    
51
  public static class DownloadedObject
52
    {
53
    String shortName;
54
    boolean icon,object,extras;
55
    int numScrambles, objectMinor, extrasMinor;
56

    
57
    DownloadedObject(String sName, int scrambles, int oMinor, int eMinor, boolean i, boolean o, boolean e)
58
      {
59
      shortName = sName;
60

    
61
      numScrambles= scrambles;
62
      objectMinor = oMinor;
63
      extrasMinor = eMinor;
64

    
65
      icon   = i;
66
      object = o;
67
      extras = e;
68
      }
69
    }
70

    
71
  private static ArrayList<DownloadedObject> mDownloadedObjects;
72

    
73
  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
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  private RubikObjectList()
89
    {
90
    mNumObjects= 0;
91
    mNumExtras = 0;
92

    
93
    mObjects           = new ArrayList<>();
94
    mDownloadedObjects = new ArrayList<>();
95

    
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
      RubikObject obj = new RubikObject(type);
107
      mObjects.add(obj);
108
      mNumObjects++;
109

    
110
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "creating local object "+type.name() );
111

    
112
      if( obj.hasExtras() )
113
        {
114
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "has extras "+mNumExtras );
115

    
116
        obj.setExtrasOrdinal(mNumExtras);
117
        mNumExtras++;
118
        }
119
      else
120
        {
121
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "no extras");
122
        }
123
      }
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  private static boolean internalAddDownloadedObject(DownloadedObject object)
129
    {
130
    String name = object.shortName;
131

    
132
    for(RubikObject ro : mObjects )
133
      if( ro.getLowerName().equals(name) )
134
        {
135
        return ro.updateObject(object);
136
        }
137

    
138
    RubikObject obj = new RubikObject(object);
139
    mObjects.add(obj);
140
    mNumObjects++;
141

    
142
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "creating downloaded object "+obj.getUpperName() );
143

    
144
    if( obj.hasExtras() )
145
      {
146
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "has extras "+mNumExtras );
147

    
148
      obj.setExtrasOrdinal(mNumExtras);
149
      mNumExtras++;
150
      }
151
    else
152
      {
153
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "no extras");
154
      }
155

    
156
    return true;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
// PUBLIC API
161
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    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

    
185
    // 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
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  public static void addDownloadedObject(Context context, String shortName, int numScrambles, int objectMinor,
202
                                         int extrasMinor, boolean icon, boolean object, boolean extras)
203
    {
204
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" icon="+icon+" object="+object+" extras="+extras);
205

    
206
    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
        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

    
222
        return;
223
        }
224
      }
225

    
226
    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
      {
232
      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
      }
235
    else
236
      {
237
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" is already built-in, deleting");
238

    
239
      mDownloadedObjects.remove(obj);
240
      RubikFiles files = RubikFiles.getInstance();
241
      files.deleteIcon(context,shortName);
242
      files.deleteJsonObject(context,shortName);
243
      files.deleteJsonExtras(context,shortName);
244
      }
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  public static void savePreferences(SharedPreferences.Editor editor)
264
    {
265
    RubikObject obj = getObject(mObject);
266
    if( obj!=null ) editor.putString("rol_objName", obj.getUpperName() );
267

    
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
        downloadedObjects.append(object.numScrambles);
282
        downloadedObjects.append(' ');
283
        downloadedObjects.append(object.objectMinor);
284
        downloadedObjects.append(' ');
285
        downloadedObjects.append(object.extrasMinor);
286
        downloadedObjects.append(' ');
287
        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
      String objects = downloadedObjects.toString();
295
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "saving: "+objects);
296
      editor.putString("rol_downloaded", objects );
297
      }
298
    else
299
      {
300
      editor.putString("rol_downloaded", "" );
301
      }
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  public static void saveMeshState(SharedPreferences.Editor editor)
307
    {
308
    for(int i=0; i<mNumObjects; i++)
309
      {
310
      RubikObject obj = getObject(i);
311

    
312
      if( obj!=null )
313
        {
314
        String name = obj.getUpperName();
315
        editor.putInt("rol_"+name, obj.getMeshState() );
316
        }
317
      }
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  public static void restorePreferences(Context context, SharedPreferences preferences)
323
    {
324
    if( mThis==null ) mThis = new RubikObjectList();
325

    
326
    String downloaded = preferences.getString("rol_downloaded","");
327

    
328
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", downloaded);
329

    
330
    if( !downloaded.equals(""))
331
      {
332
      String[] dObjects = downloaded.split(",");
333

    
334
      for(String dObj : dObjects)
335
        {
336
        String[] parts = dObj.split(" ");
337

    
338
        if( parts.length==7 )
339
          {
340
          String name = parts[0];
341
          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

    
352
          boolean bIcon = icon.equals("1");
353
          boolean bObje = obje.equals("1");
354
          boolean bExtr = extr.equals("1");
355

    
356
          addDownloadedObject(context,name,scrambles,oMinor,eMinor,bIcon,bObje,bExtr);
357
          }
358
        }
359
      }
360

    
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
    }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
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
        String name  = obj.getUpperName();
379
        int meshState= preferences.getInt("rol_"+name,MESH_NICE);
380
        obj.setMeshState(meshState);
381
        }
382
      }
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
  public static RubikObject getObject(int ordinal)
410
    {
411
    if( mThis==null ) mThis = new RubikObjectList();
412
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  public static int getNumObjects()
418
    {
419
    if( mThis==null ) mThis = new RubikObjectList();
420
    return mNumObjects;
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  public static int getOrdinal(String name)
426
    {
427
    if( mThis==null ) mThis = new RubikObjectList();
428

    
429
    String lowerName = name.toLowerCase();
430

    
431
    for(int i=0; i<mNumObjects; i++)
432
      {
433
      RubikObject obj = mObjects.get(i);
434
      if( obj.getLowerName().equals(lowerName) ) return i;
435
      }
436

    
437
    return -1;
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
  public static int getNumExtrasObjects()
443
    {
444
    return mNumExtras;
445
    }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
  public static int getNumTutorialObjects()
450
    {
451
    return mNumExtras;
452
    }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
  public static int getObjectOrdinal(int extrasOrdinal)
457
    {
458
    for(int i=extrasOrdinal; i<mNumObjects; i++)
459
      {
460
      RubikObject object = getObject(i);
461
      int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
462
      if( extOrd==extrasOrdinal ) return i;
463
      }
464

    
465
    return -1;
466
    }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
  public static int getExtrasOrdinal(int objectOrdinal)
471
    {
472
    RubikObject object = getObject(objectOrdinal);
473
    return object!=null ? object.getExtrasOrdinal() : -1;
474
    }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

    
478
  public static int getTutorialOrdinal(int objectOrdinal)
479
    {
480
    RubikObject object = getObject(objectOrdinal);
481
    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
    }
499
}
(2-2/2)