Search in sources :

Example 6 with SynchronizationSegment

use of org.apollo.game.sync.seg.SynchronizationSegment 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

SynchronizationSegment (org.apollo.game.sync.seg.SynchronizationSegment)6 SegmentType (org.apollo.game.sync.seg.SegmentType)4 GamePacketBuilder (org.apollo.net.codec.game.GamePacketBuilder)4 AddNpcSegment (org.apollo.game.sync.seg.AddNpcSegment)3 AddPlayerSegment (org.apollo.game.sync.seg.AddPlayerSegment)3 ArrayList (java.util.ArrayList)2 Position (org.apollo.game.model.Position)2 Region (org.apollo.game.model.area.Region)2 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)2 RegionRepository (org.apollo.game.model.area.RegionRepository)2 MovementSegment (org.apollo.game.sync.seg.MovementSegment)2 RemoveMobSegment (org.apollo.game.sync.seg.RemoveMobSegment)2 NpcSynchronizationMessage (org.apollo.game.message.impl.NpcSynchronizationMessage)1 PlayerSynchronizationMessage (org.apollo.game.message.impl.PlayerSynchronizationMessage)1 Npc (org.apollo.game.model.entity.Npc)1 Player (org.apollo.game.model.entity.Player)1 SynchronizationBlockSet (org.apollo.game.sync.block.SynchronizationBlockSet)1 TeleportSegment (org.apollo.game.sync.seg.TeleportSegment)1