From dbeff25d2cab1368931b80b732ea83722b6e372d Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Sat, 6 Jun 2015 00:17:07 -0400 Subject: [PATCH] v0.3.0c: refactored finding a character, no longer need to maintain chars[] array --- .../shatteredpixeldungeon/actors/Actor.java | 29 +++++-------------- .../shatteredpixeldungeon/actors/Char.java | 4 --- .../actors/mobs/npcs/Ghost.java | 3 -- .../actors/mobs/npcs/Imp.java | 3 +- .../actors/mobs/npcs/Wandmaker.java | 1 - .../items/wands/CursedWand.java | 1 - .../levels/RegularLevel.java | 2 -- .../levels/painters/BlacksmithPainter.java | 1 - .../levels/painters/PoolPainter.java | 1 - .../levels/painters/StatuePainter.java | 1 - .../levels/traps/SummoningTrap.java | 7 ----- .../scenes/GameScene.java | 2 -- 12 files changed, 9 insertions(+), 46 deletions(-) diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java b/src/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java index 1ff403f0b..8f9fb99fd 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java @@ -102,13 +102,10 @@ public abstract class Actor implements Bundlable { private static float now = 0; - private static Char[] chars = new Char[Level.LENGTH]; - public static void clear() { now = 0; - - Arrays.fill( chars, null ); + all.clear(); ids.clear(); @@ -160,14 +157,6 @@ public abstract class Actor implements Bundlable { public static void resetNextID(){ nextID = 1; } - - public static void occupyCell( Char ch ) { - chars[ch.pos] = ch; - } - - public static void freeCell( int pos ) { - chars[pos] = null; - } /*protected*/public void next() { if (current == this) { @@ -186,8 +175,7 @@ public abstract class Actor implements Bundlable { do { now = Float.MAX_VALUE; current = null; - - Arrays.fill( chars, null ); + for (Actor actor : all) { @@ -197,11 +185,7 @@ public abstract class Actor implements Bundlable { now = actor.time; current = actor; } - - if (actor instanceof Char) { - Char ch = (Char)actor; - chars[ch.pos] = ch; - } + } if (current != null) { @@ -247,7 +231,6 @@ public abstract class Actor implements Bundlable { if (actor instanceof Char) { Char ch = (Char)actor; - chars[ch.pos] = ch; for (Buff buff : ch.buffs()) { all.add( buff ); buff.onAdd(); @@ -268,7 +251,11 @@ public abstract class Actor implements Bundlable { } public static Char findChar( int pos ) { - return chars[pos]; + for (Actor actor : all){ + if (actor instanceof Char && ((Char)actor).pos == pos) + return (Char)actor; + } + return null; } public static Actor findById( int id ) { diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/actors/Char.java b/src/com/shatteredpixel/shatteredpixeldungeon/actors/Char.java index 94c46e735..1be64e112 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/actors/Char.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/actors/Char.java @@ -279,7 +279,6 @@ public abstract class Char extends Actor { public void destroy() { HP = 0; Actor.remove( this ); - Actor.freeCell( pos ); } public void die( Object src ) { @@ -404,10 +403,7 @@ public abstract class Char extends Actor { Door.leave( pos ); } - Actor.freeCell(pos); pos = step; - Actor.occupyCell( this ); - if (flying && Dungeon.level.map[pos] == Terrain.DOOR) { Door.enter( pos ); diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Ghost.java b/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Ghost.java index 684596aee..c48005132 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Ghost.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Ghost.java @@ -181,8 +181,6 @@ public class Ghost extends NPC { } if (newPos != -1) { - Actor.freeCell(pos); - CellEmitter.get(pos).start(Speck.factory(Speck.LIGHT), 0.2f, 3); pos = newPos; sprite.place(pos); @@ -317,7 +315,6 @@ public class Ghost extends NPC { ghost.pos = level.randomRespawnCell(); } while (ghost.pos == -1); level.mobs.add( ghost ); - Actor.occupyCell( ghost ); spawned = true; //dungeon depth determines type of quest. diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Imp.java b/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Imp.java index af8345537..9465891b2 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Imp.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Imp.java @@ -217,9 +217,8 @@ public class Imp extends NPC { npc.pos = level.randomRespawnCell(); } while (npc.pos == -1 || level.heaps.get( npc.pos ) != null); level.mobs.add( npc ); - Actor.occupyCell( npc ); - spawned = true; + spawned = true; alternative = Random.Int( 2 ) == 0; given = false; diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Wandmaker.java b/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Wandmaker.java index 2aec9f548..a46866109 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Wandmaker.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Wandmaker.java @@ -214,7 +214,6 @@ public class Wandmaker extends NPC { npc.pos = room.random(); } while (level.map[npc.pos] == Terrain.ENTRANCE || level.map[npc.pos] == Terrain.SIGN); level.mobs.add( npc ); - Actor.occupyCell( npc ); spawned = true; alternative = Random.Int( 2 ) == 0; diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/items/wands/CursedWand.java b/src/com/shatteredpixel/shatteredpixeldungeon/items/wands/CursedWand.java index 7718c578c..355508003 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/items/wands/CursedWand.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/items/wands/CursedWand.java @@ -271,7 +271,6 @@ public class CursedWand { sheep.pos = ch.pos; ch.sprite.killAndErase(); Actor.remove(ch); - Actor.freeCell(ch.pos); Dungeon.level.mobs.remove(ch); HealthIndicator.instance.target(null); GameScene.add(sheep); diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/levels/RegularLevel.java b/src/com/shatteredpixel/shatteredpixeldungeon/levels/RegularLevel.java index 6352bbe48..e7c7815a8 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/levels/RegularLevel.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/levels/RegularLevel.java @@ -591,7 +591,6 @@ public abstract class RegularLevel extends Level { if (Actor.findChar(mob.pos) == null && Level.passable[mob.pos]) { mobsToSpawn--; mobs.add(mob); - Actor.occupyCell(mob); //TODO: perhaps externalize this logic into a method. Do I want to make mobs more likely to clump deeper down? if (mobsToSpawn > 0 && Random.Int(4) == 0){ @@ -601,7 +600,6 @@ public abstract class RegularLevel extends Level { if (Actor.findChar(mob.pos) == null && Level.passable[mob.pos]) { mobsToSpawn--; mobs.add(mob); - Actor.occupyCell(mob); } } } diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/BlacksmithPainter.java b/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/BlacksmithPainter.java index 87cc6bc64..b82da577a 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/BlacksmithPainter.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/BlacksmithPainter.java @@ -63,6 +63,5 @@ public class BlacksmithPainter extends Painter { npc.pos = room.random( 1 ); } while (level.heaps.get( npc.pos ) != null); level.mobs.add( npc ); - Actor.occupyCell( npc ); } } diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/PoolPainter.java b/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/PoolPainter.java index b54c4c969..6e0046530 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/PoolPainter.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/PoolPainter.java @@ -77,7 +77,6 @@ public class PoolPainter extends Painter { piranha.pos = room.random(); } while (level.map[piranha.pos] != Terrain.WATER|| Actor.findChar( piranha.pos ) != null); level.mobs.add( piranha ); - Actor.occupyCell( piranha ); } } diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/StatuePainter.java b/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/StatuePainter.java index 3f6492afd..1b0108448 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/StatuePainter.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/levels/painters/StatuePainter.java @@ -67,6 +67,5 @@ public class StatuePainter extends Painter { Statue statue = new Statue(); statue.pos = cx + cy * Level.WIDTH; level.mobs.add( statue ); - Actor.occupyCell( statue ); } } diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/levels/traps/SummoningTrap.java b/src/com/shatteredpixel/shatteredpixeldungeon/levels/traps/SummoningTrap.java index cce32964d..d4b525fa9 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/levels/traps/SummoningTrap.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/levels/traps/SummoningTrap.java @@ -48,12 +48,6 @@ public class SummoningTrap extends Trap { return; } - Char c = Actor.findChar( pos ); - - if (c != null) { - Actor.occupyCell( c ); - } - int nMobs = 1; if (Random.Int( 2 ) == 0) { nMobs++; @@ -79,7 +73,6 @@ public class SummoningTrap extends Trap { int index = Random.index( candidates ); DUMMY.pos = candidates.get( index ); - Actor.occupyCell( DUMMY ); respawnPoints.add( candidates.remove( index ) ); nMobs--; diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java b/src/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java index 21b2e86c0..afdf61701 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java @@ -546,14 +546,12 @@ public class GameScene extends PixelScene { public static void add( Mob mob ) { Dungeon.level.mobs.add( mob ); Actor.add( mob ); - Actor.occupyCell( mob ); scene.addMobSprite( mob ); } public static void add( Mob mob, float delay ) { Dungeon.level.mobs.add( mob ); Actor.addDelayed( mob, delay ); - Actor.occupyCell( mob ); scene.addMobSprite( mob ); }