Project

General

Profile

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

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

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
        try
223
          {
224
          RubikActivity ract = (RubikActivity)context;
225
          ract.reloadObject(shortName);
226
          }
227
        catch(Exception ex)
228
          {
229
          android.util.Log.e("D", "exception trying to reload object: "+ex.getMessage() );
230
          }
231

    
232
        return;
233
        }
234
      }
235

    
236
    if( !object ) objectMinor=-1;
237
    if( !extras ) extrasMinor=-1;
238

    
239
    DownloadedObject obj = new DownloadedObject(shortName,numScrambles,objectMinor,extrasMinor,icon,object,extras);
240
    if ( internalAddDownloadedObject(obj) )
241
      {
242
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Adding new downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
243
      mDownloadedObjects.add(obj);
244
      }
245
    else
246
      {
247
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" is already built-in, deleting");
248

    
249
      mDownloadedObjects.remove(obj);
250
      RubikFiles files = RubikFiles.getInstance();
251
      files.deleteIcon(context,shortName);
252
      files.deleteJsonObject(context,shortName);
253
      files.deleteJsonExtras(context,shortName);
254
      }
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  public static void setMeshState(int ordinal, int state)
260
    {
261
    if( ordinal>=0 && ordinal<mNumObjects ) mObjects.get(ordinal).setMeshState(state);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public static int getMeshState(int ordinal)
267
    {
268
    return (ordinal>=0 && ordinal<mNumObjects) ? mObjects.get(ordinal).getMeshState() : MESH_NICE;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public static void savePreferences(SharedPreferences.Editor editor)
274
    {
275
    RubikObject obj = getObject(mObject);
276
    if( obj!=null ) editor.putString("rol_objName", obj.getUpperName() );
277

    
278
    int numDownloaded = mDownloadedObjects.size();
279

    
280
    if( numDownloaded>0 )
281
      {
282
      StringBuilder downloadedObjects = new StringBuilder();
283

    
284
      for(int i=0; i<numDownloaded; i++)
285
        {
286
        if( i>0 ) downloadedObjects.append(',');
287

    
288
        DownloadedObject object = mDownloadedObjects.get(i);
289
        downloadedObjects.append(object.shortName);
290
        downloadedObjects.append(' ');
291
        downloadedObjects.append(object.numScrambles);
292
        downloadedObjects.append(' ');
293
        downloadedObjects.append(object.objectMinor);
294
        downloadedObjects.append(' ');
295
        downloadedObjects.append(object.extrasMinor);
296
        downloadedObjects.append(' ');
297
        downloadedObjects.append(object.icon   ? "1":"0");
298
        downloadedObjects.append(' ');
299
        downloadedObjects.append(object.object ? "1":"0");
300
        downloadedObjects.append(' ');
301
        downloadedObjects.append(object.extras ? "1":"0");
302
        }
303

    
304
      String objects = downloadedObjects.toString();
305
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "saving: "+objects);
306
      editor.putString("rol_downloaded", objects );
307
      }
308
    else
309
      {
310
      editor.putString("rol_downloaded", "" );
311
      }
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  public static void saveMeshState(SharedPreferences.Editor editor)
317
    {
318
    for(int i=0; i<mNumObjects; i++)
319
      {
320
      RubikObject obj = getObject(i);
321

    
322
      if( obj!=null )
323
        {
324
        String name = obj.getUpperName();
325
        editor.putInt("rol_"+name, obj.getMeshState() );
326
        }
327
      }
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  public static void restorePreferences(Context context, SharedPreferences preferences)
333
    {
334
    if( mThis==null ) mThis = new RubikObjectList();
335

    
336
    String downloaded = preferences.getString("rol_downloaded","");
337

    
338
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", downloaded);
339

    
340
    if( !downloaded.equals(""))
341
      {
342
      String[] dObjects = downloaded.split(",");
343

    
344
      for(String dObj : dObjects)
345
        {
346
        String[] parts = dObj.split(" ");
347

    
348
        if( parts.length==7 )
349
          {
350
          String name = parts[0];
351
          String scra = parts[1];
352
          String objM = parts[2];
353
          String extM = parts[3];
354
          String icon = parts[4];
355
          String obje = parts[5];
356
          String extr = parts[6];
357

    
358
          int scrambles = Integer.parseInt(scra);
359
          int oMinor    = Integer.parseInt(objM);
360
          int eMinor    = Integer.parseInt(extM);
361

    
362
          boolean bIcon = icon.equals("1");
363
          boolean bObje = obje.equals("1");
364
          boolean bExtr = extr.equals("1");
365

    
366
          addDownloadedObject(context,name,scrambles,oMinor,eMinor,bIcon,bObje,bExtr);
367
          }
368
        }
369
      }
370

    
371
    RubikObject object = getObject(DEF_OBJECT);
372
    String defName = object==null ? "CUBE_3" : object.getUpperName();
373
    String objName= preferences.getString("rol_objName",defName);
374
    mObject = getOrdinal(objName);
375
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
376
    }
377

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
  public static void restoreMeshState(SharedPreferences preferences)
381
    {
382
    for(int i=0; i<mNumObjects; i++)
383
      {
384
      RubikObject obj = getObject(i);
385

    
386
      if( obj!=null )
387
        {
388
        String name  = obj.getUpperName();
389
        int meshState= preferences.getInt("rol_"+name,MESH_NICE);
390
        obj.setMeshState(meshState);
391
        }
392
      }
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
  public static boolean setCurrObject(RubikActivity act, int ordinal)
398
    {
399
    if( mObject!=ordinal )
400
      {
401
      mObject = ordinal;
402
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
403
      play.setCurrObject(act);
404
      return true;
405
      }
406

    
407
    return false;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  public static int getCurrObject()
413
    {
414
    return mObject;
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  public static RubikObject getObject(int ordinal)
420
    {
421
    if( mThis==null ) mThis = new RubikObjectList();
422
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
  public static RubikObject getObject(String shortUpperName)
428
    {
429
    if( mThis==null ) mThis = new RubikObjectList();
430

    
431
    for(int i=0; i<mNumObjects; i++)
432
      {
433
      RubikObject object = mObjects.get(i);
434
      if( object.getUpperName().equals(shortUpperName) )
435
        {
436
        return object;
437
        }
438
      }
439

    
440
    return null;
441
    }
442

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

    
445
  public static int getNumObjects()
446
    {
447
    if( mThis==null ) mThis = new RubikObjectList();
448
    return mNumObjects;
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  public static int getOrdinal(String name)
454
    {
455
    if( mThis==null ) mThis = new RubikObjectList();
456

    
457
    String lowerName = name.toLowerCase();
458

    
459
    for(int i=0; i<mNumObjects; i++)
460
      {
461
      RubikObject obj = mObjects.get(i);
462
      if( obj.getLowerName().equals(lowerName) ) return i;
463
      }
464

    
465
    return -1;
466
    }
467

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

    
470
  public static int getNumExtrasObjects()
471
    {
472
    return mNumExtras;
473
    }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
  public static int getNumTutorialObjects()
478
    {
479
    return mNumExtras;
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  public static int getObjectOrdinal(int extrasOrdinal)
485
    {
486
    for(int i=extrasOrdinal; i<mNumObjects; i++)
487
      {
488
      RubikObject object = getObject(i);
489
      int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
490
      if( extOrd==extrasOrdinal ) return i;
491
      }
492

    
493
    return -1;
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public static int getExtrasOrdinal(int objectOrdinal)
499
    {
500
    RubikObject object = getObject(objectOrdinal);
501
    return object!=null ? object.getExtrasOrdinal() : -1;
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
  public static int getTutorialOrdinal(int objectOrdinal)
507
    {
508
    RubikObject object = getObject(objectOrdinal);
509
    return object!=null ? object.getExtrasOrdinal() : -1;
510
    }
511

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

    
514
  public static int getLocalObjectMinor(int objectOrdinal)
515
    {
516
    RubikObject object = getObject(objectOrdinal);
517
    return object!=null ? object.getObjectMinor() : -1;
518
    }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

    
522
  public static int getLocalExtrasMinor(int objectOrdinal)
523
    {
524
    RubikObject object = getObject(objectOrdinal);
525
    return object!=null ? object.getExtrasMinor() : -1;
526
    }
527
}
(2-2/2)