diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java b/src/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java index 9a8c8a1b1..0195cd2a2 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java @@ -142,6 +142,7 @@ public class Dungeon { Generator.initArtifacts(); Actor.clear(); + Actor.resetNextID(); PathFinder.setMapSize( Level.WIDTH, Level.HEIGHT ); @@ -459,6 +460,8 @@ public class Dungeon { Potion.save( bundle ); Wand.save( bundle ); Ring.save( bundle ); + + Actor.storeNextID( bundle ); Bundle badges = new Bundle(); Badges.saveLocal( badges ); @@ -517,6 +520,8 @@ public class Dungeon { Generator.reset(); + Actor.restoreNextID( bundle ); + quickslot.reset(); QuickSlotButton.reset(); diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java b/src/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java index 4f1c38736..281ae7dc6 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java @@ -79,17 +79,12 @@ public abstract class Actor implements Bundlable { id = bundle.getInt( ID ); } + private static int nextID = 1; public int id() { if (id > 0) { return id; } else { - int max = 0; - for (Actor a : all) { - if (a.id > max) { - max = a.id; - } - } - return (id = max + 1); + return (id = nextID++); } } @@ -147,6 +142,20 @@ public abstract class Actor implements Bundlable { current = null; } + + private static final String NEXTID = "nextid"; + + public static void storeNextID( Bundle bundle){ + bundle.put( NEXTID, nextID ); + } + + public static void restoreNextID( Bundle bundle){ + nextID = bundle.getInt( NEXTID ); + } + + public static void resetNextID(){ + nextID = 1; + } public static void occupyCell( Char ch ) { chars[ch.pos] = ch; @@ -228,9 +237,7 @@ public abstract class Actor implements Bundlable { return; } - if (actor.id > 0) { - ids.put( actor.id, actor ); - } + ids.put( actor.id(), actor ); all.add( actor ); actor.time += time;