v0.2.4: reworked actor ID system, IDs are now garunteed unique

This commit is contained in:
Evan Debenham 2015-02-13 00:49:07 -05:00
parent e4cfe7586a
commit 37af7dfec3
2 changed files with 22 additions and 10 deletions

View File

@ -142,6 +142,7 @@ public class Dungeon {
Generator.initArtifacts(); Generator.initArtifacts();
Actor.clear(); Actor.clear();
Actor.resetNextID();
PathFinder.setMapSize( Level.WIDTH, Level.HEIGHT ); PathFinder.setMapSize( Level.WIDTH, Level.HEIGHT );
@ -460,6 +461,8 @@ public class Dungeon {
Wand.save( bundle ); Wand.save( bundle );
Ring.save( bundle ); Ring.save( bundle );
Actor.storeNextID( bundle );
Bundle badges = new Bundle(); Bundle badges = new Bundle();
Badges.saveLocal( badges ); Badges.saveLocal( badges );
bundle.put( BADGES, badges ); bundle.put( BADGES, badges );
@ -517,6 +520,8 @@ public class Dungeon {
Generator.reset(); Generator.reset();
Actor.restoreNextID( bundle );
quickslot.reset(); quickslot.reset();
QuickSlotButton.reset(); QuickSlotButton.reset();

View File

@ -79,17 +79,12 @@ public abstract class Actor implements Bundlable {
id = bundle.getInt( ID ); id = bundle.getInt( ID );
} }
private static int nextID = 1;
public int id() { public int id() {
if (id > 0) { if (id > 0) {
return id; return id;
} else { } else {
int max = 0; return (id = nextID++);
for (Actor a : all) {
if (a.id > max) {
max = a.id;
}
}
return (id = max + 1);
} }
} }
@ -148,6 +143,20 @@ public abstract class Actor implements Bundlable {
current = null; 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 ) { public static void occupyCell( Char ch ) {
chars[ch.pos] = ch; chars[ch.pos] = ch;
} }
@ -228,9 +237,7 @@ public abstract class Actor implements Bundlable {
return; return;
} }
if (actor.id > 0) { ids.put( actor.id(), actor );
ids.put( actor.id, actor );
}
all.add( actor ); all.add( actor );
actor.time += time; actor.time += time;