Search in sources :

Example 6 with Npc

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();
}
Also used : Npc(org.apollo.game.model.entity.Npc) PhasedSynchronizationTask(org.apollo.game.sync.task.PhasedSynchronizationTask) Player(org.apollo.game.model.entity.Player) Set(java.util.Set) PlayerSynchronizationTask(org.apollo.game.sync.task.PlayerSynchronizationTask) PrePlayerSynchronizationTask(org.apollo.game.sync.task.PrePlayerSynchronizationTask) PostPlayerSynchronizationTask(org.apollo.game.sync.task.PostPlayerSynchronizationTask) PrePlayerSynchronizationTask(org.apollo.game.sync.task.PrePlayerSynchronizationTask) NpcSynchronizationTask(org.apollo.game.sync.task.NpcSynchronizationTask) PlayerSynchronizationTask(org.apollo.game.sync.task.PlayerSynchronizationTask) PrePlayerSynchronizationTask(org.apollo.game.sync.task.PrePlayerSynchronizationTask) SynchronizationTask(org.apollo.game.sync.task.SynchronizationTask) PostNpcSynchronizationTask(org.apollo.game.sync.task.PostNpcSynchronizationTask) PostPlayerSynchronizationTask(org.apollo.game.sync.task.PostPlayerSynchronizationTask) PreNpcSynchronizationTask(org.apollo.game.sync.task.PreNpcSynchronizationTask) PhasedSynchronizationTask(org.apollo.game.sync.task.PhasedSynchronizationTask) RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) NpcSynchronizationTask(org.apollo.game.sync.task.NpcSynchronizationTask) PostNpcSynchronizationTask(org.apollo.game.sync.task.PostNpcSynchronizationTask) PreNpcSynchronizationTask(org.apollo.game.sync.task.PreNpcSynchronizationTask) PreNpcSynchronizationTask(org.apollo.game.sync.task.PreNpcSynchronizationTask) PostPlayerSynchronizationTask(org.apollo.game.sync.task.PostPlayerSynchronizationTask) PostNpcSynchronizationTask(org.apollo.game.sync.task.PostNpcSynchronizationTask) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with Npc

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));
}
Also used : Npc(org.apollo.game.model.entity.Npc) Position(org.apollo.game.model.Position) ArrayList(java.util.ArrayList) NpcSynchronizationMessage(org.apollo.game.message.impl.NpcSynchronizationMessage) RemoveMobSegment(org.apollo.game.sync.seg.RemoveMobSegment) MovementSegment(org.apollo.game.sync.seg.MovementSegment) RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) AddNpcSegment(org.apollo.game.sync.seg.AddNpcSegment) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region) SynchronizationSegment(org.apollo.game.sync.seg.SynchronizationSegment)

Aggregations

Npc (org.apollo.game.model.entity.Npc)7 Region (org.apollo.game.model.area.Region)3 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)3 Set (java.util.Set)2 Position (org.apollo.game.model.Position)2 Player (org.apollo.game.model.entity.Player)2 NpcSynchronizationTask (org.apollo.game.sync.task.NpcSynchronizationTask)2 PlayerSynchronizationTask (org.apollo.game.sync.task.PlayerSynchronizationTask)2 PostNpcSynchronizationTask (org.apollo.game.sync.task.PostNpcSynchronizationTask)2 PostPlayerSynchronizationTask (org.apollo.game.sync.task.PostPlayerSynchronizationTask)2 PreNpcSynchronizationTask (org.apollo.game.sync.task.PreNpcSynchronizationTask)2 PrePlayerSynchronizationTask (org.apollo.game.sync.task.PrePlayerSynchronizationTask)2 SynchronizationTask (org.apollo.game.sync.task.SynchronizationTask)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 NpcDefinition (org.apollo.cache.def.NpcDefinition)1 NpcSynchronizationMessage (org.apollo.game.message.impl.NpcSynchronizationMessage)1 RegionRepository (org.apollo.game.model.area.RegionRepository)1 WalkingQueue (org.apollo.game.model.entity.WalkingQueue)1