use of org.apollo.game.model.area.RegionCoordinates in project apollo by apollo-rsps.
the class PrePlayerSynchronizationTask method sendUpdates.
/**
* Sends the updates for a {@link Region}
*
* @param position The {@link Position} of the last known region.
* @param differences The {@link Set} of {@link RegionCoordinates} of Regions that changed.
* @param full The {@link Set} of {@link RegionCoordinates} of Regions that require a full update.
*/
private void sendUpdates(Position position, Set<RegionCoordinates> differences, Set<RegionCoordinates> full) {
RegionRepository repository = player.getWorld().getRegionRepository();
int height = position.getHeight();
for (RegionCoordinates coordinates : differences) {
Set<RegionUpdateMessage> messages = updates.computeIfAbsent(coordinates, coords -> repository.get(coords).getUpdates(height));
if (!messages.isEmpty()) {
player.send(new GroupedRegionUpdateMessage(position, coordinates, messages));
}
}
for (RegionCoordinates coordinates : full) {
Set<RegionUpdateMessage> messages = encodes.computeIfAbsent(coordinates, coords -> repository.get(coords).encode(height));
if (!messages.isEmpty()) {
player.send(new ClearRegionMessage(position, coordinates));
player.send(new GroupedRegionUpdateMessage(position, coordinates, messages));
}
}
}
use of org.apollo.game.model.area.RegionCoordinates in project apollo by apollo-rsps.
the class ParallelClientSynchronizer method synchronize.
@Override
public void synchronize(MobRepository<Player> players, MobRepository<Npc> npcs) {
int playerCount = players.size();
int npcCount = npcs.size();
Map<RegionCoordinates, Set<RegionUpdateMessage>> encodes = new ConcurrentHashMap<>();
Map<RegionCoordinates, Set<RegionUpdateMessage>> updates = new ConcurrentHashMap<>();
phaser.bulkRegister(playerCount);
for (Player player : players) {
SynchronizationTask task = new PrePlayerSynchronizationTask(player, encodes, updates);
executor.submit(new PhasedSynchronizationTask(phaser, task));
}
phaser.arriveAndAwaitAdvance();
phaser.bulkRegister(npcCount);
for (Npc npc : npcs) {
SynchronizationTask task = new PreNpcSynchronizationTask(npc);
executor.submit(new PhasedSynchronizationTask(phaser, task));
}
phaser.arriveAndAwaitAdvance();
phaser.bulkRegister(playerCount);
for (Player player : players) {
SynchronizationTask task = new PlayerSynchronizationTask(player);
executor.submit(new PhasedSynchronizationTask(phaser, task));
}
phaser.arriveAndAwaitAdvance();
phaser.bulkRegister(playerCount);
for (Player player : players) {
SynchronizationTask task = new NpcSynchronizationTask(player);
executor.submit(new PhasedSynchronizationTask(phaser, task));
}
phaser.arriveAndAwaitAdvance();
phaser.bulkRegister(playerCount);
for (Player player : players) {
SynchronizationTask task = new PostPlayerSynchronizationTask(player);
executor.submit(new PhasedSynchronizationTask(phaser, task));
}
phaser.arriveAndAwaitAdvance();
phaser.bulkRegister(npcCount);
for (Npc npc : npcs) {
SynchronizationTask task = new PostNpcSynchronizationTask(npc);
executor.submit(new PhasedSynchronizationTask(phaser, task));
}
phaser.arriveAndAwaitAdvance();
}
use of org.apollo.game.model.area.RegionCoordinates in project apollo by apollo-rsps.
the class NpcSynchronizationTask method run.
@Override
public void run() {
List<Npc> locals = player.getLocalNpcList();
List<SynchronizationSegment> segments = new ArrayList<>();
int originalCount = locals.size();
final Position playerPosition = player.getPosition();
int distance = player.getViewingDistance();
for (Iterator<Npc> iterator = locals.iterator(); iterator.hasNext(); ) {
Npc npc = iterator.next();
Position position = npc.getPosition();
if (!npc.isActive() || npc.isTeleporting() || position.getLongestDelta(playerPosition) > distance || !position.isWithinDistance(playerPosition, distance)) {
iterator.remove();
segments.add(new RemoveMobSegment());
} else {
segments.add(new MovementSegment(npc.getBlockSet(), npc.getDirections()));
}
}
int added = 0, count = locals.size();
RegionRepository repository = player.getWorld().getRegionRepository();
Region current = repository.fromPosition(playerPosition);
Set<RegionCoordinates> regions = current.getSurrounding();
regions.add(current.getCoordinates());
Stream<Npc> npcs = regions.stream().map(repository::get).flatMap(region -> region.getEntities(EntityType.NPC));
Iterator<Npc> iterator = npcs.iterator();
while (iterator.hasNext()) {
if (count >= MAXIMUM_LOCAL_NPCS) {
player.flagExcessiveNpcs();
break;
} else if (added >= NEW_NPCS_PER_CYCLE) {
break;
}
Npc npc = iterator.next();
Position position = npc.getPosition();
if (position.isWithinDistance(playerPosition, distance) && !locals.contains(npc)) {
locals.add(npc);
count++;
added++;
npc.turnTo(npc.getFacingPosition());
segments.add(new AddNpcSegment(npc.getBlockSet(), npc.getIndex(), position, npc.getId()));
}
}
player.send(new NpcSynchronizationMessage(playerPosition, segments, originalCount));
}
Aggregations