Conflicts:
	src/org/apollo/game/model/World.java
	src/org/apollo/game/model/area/Region.java
This commit is contained in:
atomicint
2015-04-10 15:43:02 -04:00
2 changed files with 260 additions and 238 deletions
+2 -2
View File
@@ -385,12 +385,12 @@ public final class World {
} }
/** /**
* Adds entities to regions in the {@link RegionRepository}. * Adds entities to regions in the {@link RegionRepository}. By default, we do not notify listeners.
* *
* @param entities The entities. * @param entities The entities.
*/ */
private void placeEntities(Entity... entities) { private void placeEntities(Entity... entities) {
Arrays.stream(entities).forEach(entity -> regions.fromPosition(entity.getPosition()).addEntity(entity)); Arrays.stream(entities).forEach(entity -> regions.fromPosition(entity.getPosition()).addEntity(entity, false));
} }
} }
+33 -11
View File
@@ -41,7 +41,7 @@ public final class Region {
@Override @Override
public void execute(Region region, Entity entity, EntityUpdateType update) { public void execute(Region region, Entity entity, EntityUpdateType update) {
EntityType type = entity.getEntityType(); EntityType type = entity.getEntityType();
if (type != EntityType.PLAYER && type != EntityType.NPC && (type != EntityType.STATIC_OBJECT || update == EntityUpdateType.REMOVE)) { if (type != EntityType.PLAYER && type != EntityType.NPC) {
region.record(entity, update); region.record(entity, update);
} }
} }
@@ -83,7 +83,7 @@ public final class Region {
/** /**
* The Set containing RegionUpdateMessages which can be sent to add every non-Mob Entity in this Region. * The Set containing RegionUpdateMessages which can be sent to add every non-Mob Entity in this Region.
*/ */
private final List<List<RegionUpdateMessage>> snapshots = new ArrayList<>(Position.HEIGHT_LEVELS); private final List<Map<Entity, RegionUpdateMessage>> snapshots = new ArrayList<>(Position.HEIGHT_LEVELS);
/** /**
* The Set containing UpdateOperations. * The Set containing UpdateOperations.
@@ -110,40 +110,57 @@ public final class Region {
listeners.add(new UpdateRegionListener()); listeners.add(new UpdateRegionListener());
for (int height = 0; height < Position.HEIGHT_LEVELS; height++) { for (int height = 0; height < Position.HEIGHT_LEVELS; height++) {
snapshots.add(new ArrayList<>()); snapshots.add(new HashMap<>());
updates.add(new ArrayList<>(DEFAULT_SET_SIZE)); updates.add(new ArrayList<>(DEFAULT_SET_SIZE));
} }
} }
/** /**
* Adds a {@link Entity} from to Region. Note that this does not spawn the Entity, or do any other action other than * Adds a {@link Entity} to the Region. Note that this does not spawn the Entity, or do any other action other than
* register it to this Region. * register it to this Region.
* *
* @param entity The Entity. * @param entity The Entity.
* @param notify A flag indicating whether the {@link RegionListener}s for this Region should be notified.
* @throws IllegalArgumentException If the Entity does not belong in this Region. * @throws IllegalArgumentException If the Entity does not belong in this Region.
*/ */
public void addEntity(Entity entity) { public void addEntity(Entity entity, boolean notify) {
Position position = entity.getPosition(); Position position = entity.getPosition();
checkPosition(position); checkPosition(position);
Set<Entity> local = entities.computeIfAbsent(position, key -> new HashSet<>(DEFAULT_SET_SIZE)); Set<Entity> local = entities.computeIfAbsent(position, key -> new HashSet<>(DEFAULT_SET_SIZE));
local.add(entity); local.add(entity);
if ((System.currentTimeMillis() - start) / 1000 > 10 && entity instanceof GameObject) { if ((System.currentTimeMillis() - start) / 1000 > 10 && (entity instanceof GameObject)) {
System.out.println("Adding entity " + entity + " to " + entity.getPosition()); System.out.println("Adding entity " + entity + " to " + entity.getPosition());
} }
if (notify) {
notifyListeners(entity, EntityUpdateType.ADD); notifyListeners(entity, EntityUpdateType.ADD);
} }
}
/**
* Adds a {@link Entity} to the Region. Note that this does not spawn the Entity, or do any other action other than
* register it to this Region.
* <p/>
* By default, this method notifies RegionListeners for this region of the addition.
*
* @param entity The Entity.
* @throws IllegalArgumentException If the Entity does not belong in this Region.
*/
public void addEntity(Entity entity) {
addEntity(entity, true);
}
/** /**
* Checks if this Region contains the specified Entity. * Checks if this Region contains the specified Entity.
* <p> * <p/>
* This method operates in constant time. * This method operates in constant time.
* *
* @param entity The Entity. * @param entity The Entity.
* @return {@code true} if this Region contains the Entity, otherwise {@code false}. * @return {@code true} if this Region contains the Entity, otherwise {@code false}.
*/ */
public boolean contains(Entity entity) { public boolean contains(Entity entity) {
Position position = entity.getPosition(); Position position = entity.getPosition();
Set<Entity> local = entities.get(position); Set<Entity> local = entities.get(position);
@@ -169,7 +186,7 @@ public final class Region {
*/ */
public Set<Entity> getEntities(Position position) { public Set<Entity> getEntities(Position position) {
Set<Entity> set = entities.get(position); Set<Entity> set = entities.get(position);
return set == null ? ImmutableSet.of() : ImmutableSet.copyOf(set); return (set == null) ? ImmutableSet.of() : ImmutableSet.copyOf(set);
} }
/** /**
@@ -211,7 +228,7 @@ public final class Region {
* @return The Set of RegionUpdateMessages. * @return The Set of RegionUpdateMessages.
*/ */
public List<RegionUpdateMessage> getSnapshot(int height) { public List<RegionUpdateMessage> getSnapshot(int height) {
List<RegionUpdateMessage> copy = new ArrayList<>(snapshots.get(height)); List<RegionUpdateMessage> copy = new ArrayList<>(snapshots.get(height).values());
Collections.sort(copy); Collections.sort(copy);
return ImmutableList.copyOf(copy); return ImmutableList.copyOf(copy);
} }
@@ -224,7 +241,7 @@ public final class Region {
* @return The Set of RegionUpdateMessages. * @return The Set of RegionUpdateMessages.
*/ */
public List<RegionUpdateMessage> getUpdates(int height) { public List<RegionUpdateMessage> getUpdates(int height) {
List<RegionUpdateMessage> original = updates.get(height); List<RegionUpdateMessage> original = this.updates.get(height);
List<RegionUpdateMessage> updates = new ArrayList<>(original); List<RegionUpdateMessage> updates = new ArrayList<>(original);
original.clear(); original.clear();
@@ -304,7 +321,12 @@ public final class Region {
int height = entity.getPosition().getHeight(); int height = entity.getPosition().getHeight();
updates.get(height).add(message); updates.get(height).add(message);
snapshots.get(height).add(message); snapshots.get(height).remove(entity);
if ((entity.getEntityType() == EntityType.STATIC_OBJECT && type == EntityUpdateType.REMOVE) ||
(entity.getEntityType() != EntityType.STATIC_OBJECT && type == EntityUpdateType.ADD)) {
snapshots.get(height).put(entity, message);
}
} }
} }