Project

General

Profile

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

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

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.SharedPreferences;
25

    
26
import org.distorted.main.RubikActivity;
27
import org.distorted.objectlib.main.ObjectConstants;
28
import org.distorted.objectlib.main.ObjectType;
29
import org.distorted.screens.RubikScreenPlay;
30
import org.distorted.screens.ScreenList;
31

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

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class RubikObjectList
39
{
40
  public static final int DEF_OBJECT= ObjectConstants.CUBE_3;
41
  public static int MAX_LEVEL;
42

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

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

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

    
59
      numScrambles= scrambles;
60
      objectMinor = oMinor;
61
      extrasMinor = eMinor;
62

    
63
      icon   = i;
64
      object = o;
65
      extras = e;
66
      }
67
    }
68

    
69
  private static ArrayList<DownloadedObject> mDownloadedObjects;
70

    
71
  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
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  private RubikObjectList()
87
    {
88
    mNumObjects= 0;
89
    mNumExtras = 0;
90

    
91
    mObjects           = new ArrayList<>();
92
    mDownloadedObjects = new ArrayList<>();
93

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

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

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

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

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

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

    
136
    RubikObject obj = new RubikObject(object);
137
    mObjects.add(obj);
138
    mNumObjects++;
139

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

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

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

    
154
    return true;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
// PUBLIC API
159
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    if( ordinal==ObjectConstants.CUBE_3 ) return 16;
168
    if( ordinal==ObjectConstants.CUBE_4 ) return 20;
169
    if( ordinal==ObjectConstants.CUBE_5 ) return 24;
170
    if( ordinal==ObjectConstants.PYRA_4 ) return 15;
171
    if( ordinal==ObjectConstants.PYRA_5 ) return 20;
172
    if( ordinal==ObjectConstants.MEGA_5 ) return 35;
173
    if( ordinal==ObjectConstants.DIAM_2 ) return 10;
174
    if( ordinal==ObjectConstants.DIAM_3 ) return 18;
175
    if( ordinal==ObjectConstants.REDI_3 ) return 14;
176
    if( ordinal==ObjectConstants.HELI_3 ) return 18;
177
    if( ordinal==ObjectConstants.SKEW_3 ) return 17;
178
    if( ordinal==ObjectConstants.REX_3  ) return 16;
179
    if( ordinal==ObjectConstants.MIRR_3 ) return 16;
180

    
181
    // in 1.9.6 & 1.9.7 there is a bug with downloadable objects (in this very function!):
182
    // All of those have DBLevel equal to CUBE_3's DBlevel (in 1.9.7!), i.e. 17.
183
    // This will be a problem when we release a new version of the app which has some of the
184
    // previously downloadable objects built-in. Thus: in case of those, we need to keep using
185
    // 17.
186

    
187
    if( ObjectType.wasDownloadableButNowIsBuiltIn(ordinal) )
188
      {
189
      return 17;
190
      }
191

    
192
    return ObjectType.getObject(ordinal).getNumScramble();
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  public static void addDownloadedObject(String shortName, int numScrambles, int objectMinor, int extrasMinor,
198
                                         boolean icon, boolean object, boolean extras)
199
    {
200
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" icon="+icon+" object="+object+" extras="+extras);
201

    
202
    for( DownloadedObject obj : mDownloadedObjects )
203
      {
204
      if( obj.shortName.equals(shortName) )
205
        {
206
        obj.icon  |= icon;
207
        obj.object|= object;
208
        obj.extras|= extras;
209

    
210
        if( !obj.object ) objectMinor=-1;
211
        if( !obj.extras ) extrasMinor=-1;
212

    
213
        obj.objectMinor = objectMinor;
214
        obj.extrasMinor = extrasMinor;
215

    
216
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
217

    
218
        return;
219
        }
220
      }
221

    
222
    if( !object ) objectMinor=-1;
223
    if( !extras ) extrasMinor=-1;
224

    
225
    DownloadedObject obj = new DownloadedObject(shortName,numScrambles,objectMinor,extrasMinor,icon,object,extras);
226
    if ( internalAddDownloadedObject(obj) )
227
      {
228
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Adding new downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
229
      mDownloadedObjects.add(obj);
230
      }
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  public static void setMeshState(int ordinal, int state)
236
    {
237
    if( ordinal>=0 && ordinal<mNumObjects ) mObjects.get(ordinal).setMeshState(state);
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  public static int getMeshState(int ordinal)
243
    {
244
    return (ordinal>=0 && ordinal<mNumObjects) ? mObjects.get(ordinal).getMeshState() : MESH_NICE;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  public static void savePreferences(SharedPreferences.Editor editor)
250
    {
251
    RubikObject obj = getObject(mObject);
252
    if( obj!=null ) editor.putString("rol_objName", obj.getUpperName() );
253

    
254
    int numDownloaded = mDownloadedObjects.size();
255

    
256
    if( numDownloaded>0 )
257
      {
258
      StringBuilder downloadedObjects = new StringBuilder();
259

    
260
      for(int i=0; i<numDownloaded; i++)
261
        {
262
        if( i>0 ) downloadedObjects.append(',');
263

    
264
        DownloadedObject object = mDownloadedObjects.get(i);
265
        downloadedObjects.append(object.shortName);
266
        downloadedObjects.append(' ');
267
        downloadedObjects.append(object.numScrambles);
268
        downloadedObjects.append(' ');
269
        downloadedObjects.append(object.objectMinor);
270
        downloadedObjects.append(' ');
271
        downloadedObjects.append(object.extrasMinor);
272
        downloadedObjects.append(' ');
273
        downloadedObjects.append(object.icon   ? "1":"0");
274
        downloadedObjects.append(' ');
275
        downloadedObjects.append(object.object ? "1":"0");
276
        downloadedObjects.append(' ');
277
        downloadedObjects.append(object.extras ? "1":"0");
278
        }
279

    
280
      String objects = downloadedObjects.toString();
281
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "saving: "+objects);
282

    
283
      editor.putString("rol_downloaded", objects );
284
      }
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  public static void saveMeshState(SharedPreferences.Editor editor)
290
    {
291
    for(int i=0; i<mNumObjects; i++)
292
      {
293
      RubikObject obj = getObject(i);
294

    
295
      if( obj!=null )
296
        {
297
        String name = obj.getUpperName();
298
        editor.putInt("rol_"+name, obj.getMeshState() );
299
        }
300
      }
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
  public static void restorePreferences(SharedPreferences preferences)
306
    {
307
    if( mThis==null ) mThis = new RubikObjectList();
308

    
309
    String downloaded = preferences.getString("rol_downloaded","");
310

    
311
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", downloaded);
312

    
313
    if( !downloaded.equals(""))
314
      {
315
      String[] dObjects = downloaded.split(",");
316

    
317
      for(String dObj : dObjects)
318
        {
319
        String[] parts = dObj.split(" ");
320

    
321
        if( parts.length==7 )
322
          {
323
          String name = parts[0];
324
          String scra = parts[1];
325
          String objM = parts[2];
326
          String extM = parts[3];
327
          String icon = parts[4];
328
          String obje = parts[5];
329
          String extr = parts[6];
330

    
331
          int scrambles = Integer.parseInt(scra);
332
          int oMinor    = Integer.parseInt(objM);
333
          int eMinor    = Integer.parseInt(extM);
334

    
335
          boolean bIcon = icon.equals("1");
336
          boolean bObje = obje.equals("1");
337
          boolean bExtr = extr.equals("1");
338

    
339
          addDownloadedObject(name,scrambles,oMinor,eMinor,bIcon,bObje,bExtr);
340
          }
341
        }
342
      }
343

    
344
    RubikObject object = getObject(DEF_OBJECT);
345
    String defName = object==null ? "CUBE_3" : object.getUpperName();
346
    String objName= preferences.getString("rol_objName",defName);
347
    mObject = getOrdinal(objName);
348
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  public static void restoreMeshState(SharedPreferences preferences)
354
    {
355
    for(int i=0; i<mNumObjects; i++)
356
      {
357
      RubikObject obj = getObject(i);
358

    
359
      if( obj!=null )
360
        {
361
        String name  = obj.getUpperName();
362
        int meshState= preferences.getInt("rol_"+name,MESH_NICE);
363
        obj.setMeshState(meshState);
364
        }
365
      }
366
    }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
  public static boolean setCurrObject(RubikActivity act, int ordinal)
371
    {
372
    if( mObject!=ordinal )
373
      {
374
      mObject = ordinal;
375
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
376
      play.setCurrObject(act);
377
      return true;
378
      }
379

    
380
    return false;
381
    }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
  public static int getCurrObject()
386
    {
387
    return mObject;
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
  public static RubikObject getObject(int ordinal)
393
    {
394
    if( mThis==null ) mThis = new RubikObjectList();
395
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  public static int getNumObjects()
401
    {
402
    if( mThis==null ) mThis = new RubikObjectList();
403
    return mNumObjects;
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  public static int getOrdinal(String name)
409
    {
410
    if( mThis==null ) mThis = new RubikObjectList();
411

    
412
    String lowerName = name.toLowerCase();
413

    
414
    for(int i=0; i<mNumObjects; i++)
415
      {
416
      RubikObject obj = mObjects.get(i);
417
      if( obj.getLowerName().equals(lowerName) ) return i;
418
      }
419

    
420
    return -1;
421
    }
422

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

    
425
  public static int getNumExtrasObjects()
426
    {
427
    return mNumExtras;
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  public static int getNumTutorialObjects()
433
    {
434
    return mNumExtras;
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  public static int getObjectOrdinal(int extrasOrdinal)
440
    {
441
    for(int i=extrasOrdinal; i<mNumObjects; i++)
442
      {
443
      RubikObject object = getObject(i);
444
      int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
445
      if( extOrd==extrasOrdinal ) return i;
446
      }
447

    
448
    return -1;
449
    }
450

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

    
453
  public static int getExtrasOrdinal(int objectOrdinal)
454
    {
455
    RubikObject object = getObject(objectOrdinal);
456
    return object!=null ? object.getExtrasOrdinal() : -1;
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
  public static int getTutorialOrdinal(int objectOrdinal)
462
    {
463
    RubikObject object = getObject(objectOrdinal);
464
    return object!=null ? object.getExtrasOrdinal() : -1;
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

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

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

    
477
  public static int getLocalExtrasMinor(int objectOrdinal)
478
    {
479
    RubikObject object = getObject(objectOrdinal);
480
    return object!=null ? object.getExtrasMinor() : -1;
481
    }
482
}
(2-2/2)