diff --git a/src/org/apollo/fs/decoder/StaticObjectDecoder.java b/src/org/apollo/fs/decoder/StaticObjectDecoder.java index ff74cc9e..3ea829f8 100644 --- a/src/org/apollo/fs/decoder/StaticObjectDecoder.java +++ b/src/org/apollo/fs/decoder/StaticObjectDecoder.java @@ -9,12 +9,12 @@ import java.util.List; import org.apollo.fs.IndexedFileSystem; import org.apollo.fs.archive.Archive; import org.apollo.game.model.Position; -import org.apollo.game.model.obj.StaticObject; +import org.apollo.game.model.obj.GameObject; import org.apollo.util.BufferUtil; import org.apollo.util.CompressionUtil; /** - * Decodes map object data from the {@code map_index.dat} file into {@link StaticObject}s. + * Decodes map object data from the {@code map_index.dat} file into {@link GameObject}s. * * @author Chris Fletcher */ @@ -40,7 +40,7 @@ public final class StaticObjectDecoder { * @return The decoded objects. * @throws IOException If an I/O error occurs. */ - public StaticObject[] decode() throws IOException { + public GameObject[] decode() throws IOException { Archive versionList = Archive.decode(fs.getFile(0, 5)); ByteBuffer buffer = versionList.getEntry("map_index").getBuffer(); @@ -60,17 +60,17 @@ public final class StaticObjectDecoder { boolean members = (buffer.get() & 0xFF) == 1; } - List objects = new ArrayList(); + List objects = new ArrayList(); for (int i = 0; i < indices; i++) { ByteBuffer compressed = fs.getFile(4, landscapes[i]); ByteBuffer uncompressed = ByteBuffer.wrap(CompressionUtil.ungzip(compressed)); - Collection areaObjects = parseArea(areas[i], uncompressed); + Collection areaObjects = parseArea(areas[i], uncompressed); objects.addAll(areaObjects); } - return objects.toArray(new StaticObject[objects.size()]); + return objects.toArray(new GameObject[objects.size()]); } /** @@ -80,8 +80,8 @@ public final class StaticObjectDecoder { * @param buffer The buffer which holds the area's data. * @return A collection of all parsed objects. */ - private Collection parseArea(int area, ByteBuffer buffer) { - List objects = new ArrayList(); + private Collection parseArea(int area, ByteBuffer buffer) { + List objects = new ArrayList(); int x = (area >> 8 & 0xFF) * 64; int y = (area & 0xFF) * 64; @@ -108,7 +108,7 @@ public final class StaticObjectDecoder { Position pos = new Position(x + localX, y + localY, height); - StaticObject object = new StaticObject(id, pos, type, rotation); + GameObject object = new GameObject(id, pos, type, rotation); objects.add(object); } } diff --git a/src/org/apollo/game/model/World.java b/src/org/apollo/game/model/World.java index c7f59644..02c93540 100644 --- a/src/org/apollo/game/model/World.java +++ b/src/org/apollo/game/model/World.java @@ -21,7 +21,7 @@ import org.apollo.game.model.def.EquipmentDefinition; import org.apollo.game.model.def.ItemDefinition; import org.apollo.game.model.def.NpcDefinition; import org.apollo.game.model.def.ObjectDefinition; -import org.apollo.game.model.obj.StaticObject; +import org.apollo.game.model.obj.GameObject; import org.apollo.game.model.sector.Sector; import org.apollo.game.model.sector.SectorCoordinates; import org.apollo.game.model.sector.SectorRepository; @@ -259,7 +259,7 @@ public final class World { logger.info("Loaded " + objDefs.length + " object definitions."); StaticObjectDecoder staticDecoder = new StaticObjectDecoder(fs); - StaticObject[] objects = staticDecoder.decode(); + GameObject[] objects = staticDecoder.decode(); placeEntities(objects); logger.info("Loaded " + objects.length + " static objects."); diff --git a/src/org/apollo/game/model/obj/GameObject.java b/src/org/apollo/game/model/obj/GameObject.java index 8a01387f..b0749a81 100644 --- a/src/org/apollo/game/model/obj/GameObject.java +++ b/src/org/apollo/game/model/obj/GameObject.java @@ -5,32 +5,48 @@ import org.apollo.game.model.Position; import org.apollo.game.model.def.ObjectDefinition; /** - * Represents an in-game object. + * Represents an object in the game world. * + * @author Chris Fletcher * @author Major */ @SuppressWarnings("serial") public final class GameObject extends Entity { /** - * The object definition. + * The object's definition. */ private final ObjectDefinition definition; /** - * Creates the game object. - * - * @param definition The object's definition. + * The object's rotation. */ - public GameObject(ObjectDefinition definition, Position position) { + private final int rotation; + + /** + * The object type. + */ + private final int type; + + /** + * Creates a new static object. + * + * @param id The object's id. + * @param position The position. + * @param type The type code of the object. + * @param rotation The rotation of the object. + */ + public GameObject(int id, Position position, int type, int rotation) { super(position); - this.definition = definition; + this.type = type; + this.rotation = rotation; + definition = ObjectDefinition.lookup(id); } /** - * Gets the object's definition. + * Gets the definition of this object. * - * @return The definition. + * @return The object's definition. */ public ObjectDefinition getDefinition() { return definition; @@ -41,4 +57,28 @@ public final class GameObject extends Entity { return EntityType.GAME_OBJECT; } + /** + * Gets the object's rotation. + * + * @return The rotation. + */ + public int getRotation() { + return rotation; + } + + /** + * Gets the type code of the object. + * + * @return The type. + */ + public int getType() { + return type; + } + + @Override + public String toString() { + return GameObject.class.getName() + " [id=" + definition.getId() + ", type=" + type + ", rotation=" + rotation + + "]"; + } + } \ No newline at end of file diff --git a/src/org/apollo/game/model/obj/StaticObject.java b/src/org/apollo/game/model/obj/StaticObject.java deleted file mode 100644 index b9ec949c..00000000 --- a/src/org/apollo/game/model/obj/StaticObject.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.apollo.game.model.obj; - -import org.apollo.game.model.Entity; -import org.apollo.game.model.Position; -import org.apollo.game.model.def.ObjectDefinition; - -/** - * Represents a static object in the game world. - * - * @author Chris Fletcher - * @author Major - */ -@SuppressWarnings("serial") -public final class StaticObject extends Entity { - - /** - * The object's definition. - */ - private final ObjectDefinition definition; - - /** - * The object's rotation. - */ - private final int rotation; - - /** - * The object type. - */ - private final int type; - - /** - * Creates a new static object. - * - * @param id The object's id. - * @param position The position. - * @param type The type code of the object. - * @param rotation The rotation of the object. - */ - public StaticObject(int id, Position position, int type, int rotation) { - super(position); - this.type = type; - this.rotation = rotation; - definition = ObjectDefinition.lookup(id); - } - - /** - * Gets the definition of this object. - * - * @return The object's definition. - */ - public ObjectDefinition getDefinition() { - return definition; - } - - @Override - public EntityType getEntityType() { - return EntityType.GAME_OBJECT; - } - - /** - * Gets the object's rotation. - * - * @return The rotation. - */ - public int getRotation() { - return rotation; - } - - /** - * Gets the type code of the object. - * - * @return The type. - */ - public int getType() { - return type; - } - - @Override - public String toString() { - return StaticObject.class.getName() + " [id=" + definition.getId() + ", type=" + type + ", rotation=" - + rotation + "]"; - } - -} \ No newline at end of file diff --git a/src/org/apollo/game/model/sector/Sector.java b/src/org/apollo/game/model/sector/Sector.java index ca739471..7f8901d7 100644 --- a/src/org/apollo/game/model/sector/Sector.java +++ b/src/org/apollo/game/model/sector/Sector.java @@ -1,9 +1,13 @@ package org.apollo.game.model.sector; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.apollo.game.model.Entity; +import org.apollo.game.model.Entity.EntityType; +import org.apollo.game.model.Position; /** * Represents an 8x8 area of the map. @@ -23,9 +27,9 @@ public final class Sector { private final SectorCoordinates coordinates; /** - * A list of every entity in this sector. + * A map of positions to entities in that position. */ - private final List entities = new ArrayList<>(); + private final Map> entities = new HashMap<>(); /** * A list of listeners registered to this sector. @@ -56,16 +60,34 @@ public final class Sector { * register it to this sector. * * @param entity The entity. - * @return {@code true} if the entity was added, otherwise {@code false}. + * @return {@code true} if the entity was added successfully, otherwise {@code false}. */ public boolean addEntity(Entity entity) { + Position position = entity.getPosition(); + List entities = this.entities.get(position); + if (entities == null) { + entities = new ArrayList<>(); + } + if (entities.add(entity)) { + this.entities.put(position, entities); notifyListeners(entity); return true; } return false; } + /** + * Checks if this sector contains the specified entity. + * + * @param entity The entity. + * @return {@code true} if this sector contains the entity, otherwise {@code false}. + */ + public boolean contains(Entity entity) { + List entities = this.entities.get(entity.getPosition()); + return entities.contains(entity); + } + /** * Gets this sector's {@link SectorCoordinates}. * @@ -76,12 +98,32 @@ public final class Sector { } /** - * Gets the {@link List} of {@link Entity}s. + * Gets a copy of the {@link List} of {@link Entity}s. * + * @param position The position containing the entities. * @return The list. */ - public List getEntities() { - return new ArrayList<>(entities); + public List getEntities(Position position) { + return new ArrayList<>(entities.get(position)); + } + + /** + * Gets a copy of the {@link List} of {@link Entity}s with the specified {@link EntityType}. + * + * @param position The {@link Position} containing the entities. + * @param type The {@link EntityType}. + * @return The list of entities. + */ + @SuppressWarnings("unchecked") + public List getEntities(Position position, EntityType type) { + List entities = getEntities(position); + List filtered = new ArrayList<>(); + for (Entity entity : entities) { + if (entity.getEntityType() == type) { + filtered.add((T) entity); + } + } + return filtered; } /** @@ -102,6 +144,7 @@ public final class Sector { * @return {@code true} if the entity was removed, otherwise {@code false}. */ public boolean removeEntity(Entity entity) { + List entities = this.entities.get(entity.getPosition()); if (entities.remove(entity)) { notifyListeners(entity); return true; diff --git a/src/org/apollo/game/model/sector/SectorRepository.java b/src/org/apollo/game/model/sector/SectorRepository.java index 7a18de11..d068e575 100644 --- a/src/org/apollo/game/model/sector/SectorRepository.java +++ b/src/org/apollo/game/model/sector/SectorRepository.java @@ -69,7 +69,7 @@ public final class SectorRepository { public boolean contains(SectorCoordinates coordinates) { return sectors.containsKey(coordinates); } - + /** * Gets a {@link Sector} with the specified {@link SectorCoordinates}. If the sector does not exist (i.e. * {@link #sectors}{@code .get()} returns {@code null}) then a new sector is created, submitted to the repository,