Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 84d746d7

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