use of org.apollo.game.model.entity.Npc 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.entity.Npc 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