Search in sources :

Example 1 with MovementSegment

use of org.apollo.game.sync.seg.MovementSegment in project apollo by apollo-rsps.

the class PlayerSynchronizationTask method run.

@Override
public void run() {
    Position lastKnownRegion = player.getLastKnownRegion();
    boolean regionChanged = player.hasRegionChanged();
    int[] appearanceTickets = player.getAppearanceTickets();
    SynchronizationBlockSet blockSet = player.getBlockSet();
    if (blockSet.contains(ChatBlock.class)) {
        blockSet = blockSet.clone();
        blockSet.remove(ChatBlock.class);
    }
    Position position = player.getPosition();
    SynchronizationSegment segment = (player.isTeleporting() || player.hasRegionChanged()) ? new TeleportSegment(blockSet, position) : new MovementSegment(blockSet, player.getDirections());
    List<Player> localPlayers = player.getLocalPlayerList();
    int oldCount = localPlayers.size();
    List<SynchronizationSegment> segments = new ArrayList<>();
    int distance = player.getViewingDistance();
    for (Iterator<Player> iterator = localPlayers.iterator(); iterator.hasNext(); ) {
        Player other = iterator.next();
        if (removeable(position, distance, other)) {
            iterator.remove();
            segments.add(new RemoveMobSegment());
        } else {
            segments.add(new MovementSegment(other.getBlockSet(), other.getDirections()));
        }
    }
    int added = 0, count = localPlayers.size();
    RegionRepository repository = player.getWorld().getRegionRepository();
    Region current = repository.fromPosition(position);
    Set<RegionCoordinates> regions = current.getSurrounding();
    regions.add(current.getCoordinates());
    Stream<Player> players = regions.stream().map(repository::get).flatMap(region -> region.getEntities(EntityType.PLAYER));
    Iterator<Player> iterator = players.iterator();
    while (iterator.hasNext()) {
        if (count >= MAXIMUM_LOCAL_PLAYERS) {
            player.flagExcessivePlayers();
            break;
        } else if (added >= NEW_PLAYERS_PER_CYCLE) {
            break;
        }
        Player other = iterator.next();
        Position local = other.getPosition();
        if (other != player && local.isWithinDistance(position, distance) && !localPlayers.contains(other)) {
            localPlayers.add(other);
            count++;
            added++;
            blockSet = other.getBlockSet();
            int index = other.getIndex();
            if (!blockSet.contains(AppearanceBlock.class) && !hasCachedAppearance(appearanceTickets, index - 1, other.getAppearanceTicket())) {
                blockSet = blockSet.clone();
                blockSet.add(SynchronizationBlock.createAppearanceBlock(other));
            }
            segments.add(new AddPlayerSegment(blockSet, index, local));
        }
    }
    PlayerSynchronizationMessage message = new PlayerSynchronizationMessage(lastKnownRegion, position, regionChanged, segment, oldCount, segments);
    player.send(message);
}
Also used : Player(org.apollo.game.model.entity.Player) Position(org.apollo.game.model.Position) ArrayList(java.util.ArrayList) TeleportSegment(org.apollo.game.sync.seg.TeleportSegment) SynchronizationBlockSet(org.apollo.game.sync.block.SynchronizationBlockSet) MovementSegment(org.apollo.game.sync.seg.MovementSegment) RemoveMobSegment(org.apollo.game.sync.seg.RemoveMobSegment) RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) RegionRepository(org.apollo.game.model.area.RegionRepository) AddPlayerSegment(org.apollo.game.sync.seg.AddPlayerSegment) Region(org.apollo.game.model.area.Region) SynchronizationSegment(org.apollo.game.sync.seg.SynchronizationSegment) PlayerSynchronizationMessage(org.apollo.game.message.impl.PlayerSynchronizationMessage)

Example 2 with MovementSegment

use of org.apollo.game.sync.seg.MovementSegment in project apollo by apollo-rsps.

the class PlayerSynchronizationMessageEncoder method putMovementUpdate.

/**
 * Puts a movement update for the specified segment.
 *
 * @param seg The segment.
 * @param message The message.
 * @param builder The builder.
 */
private static void putMovementUpdate(SynchronizationSegment seg, PlayerSynchronizationMessage message, GamePacketBuilder builder) {
    boolean updateRequired = seg.getBlockSet().size() > 0;
    if (seg.getType() == SegmentType.TELEPORT) {
        Position pos = ((TeleportSegment) seg).getDestination();
        builder.putBits(1, 1);
        builder.putBits(2, 3);
        builder.putBits(1, message.hasRegionChanged() ? 0 : 1);
        builder.putBits(2, pos.getHeight());
        builder.putBits(7, pos.getLocalY(message.getLastKnownRegion()));
        builder.putBits(7, pos.getLocalX(message.getLastKnownRegion()));
        builder.putBits(1, updateRequired ? 1 : 0);
    } else if (seg.getType() == SegmentType.RUN) {
        Direction[] directions = ((MovementSegment) seg).getDirections();
        builder.putBits(1, 1);
        builder.putBits(2, 2);
        builder.putBits(3, directions[0].toInteger());
        builder.putBits(3, directions[1].toInteger());
        builder.putBits(1, updateRequired ? 1 : 0);
    } else if (seg.getType() == SegmentType.WALK) {
        Direction[] directions = ((MovementSegment) seg).getDirections();
        builder.putBits(1, 1);
        builder.putBits(2, 1);
        builder.putBits(3, directions[0].toInteger());
        builder.putBits(1, updateRequired ? 1 : 0);
    } else {
        if (updateRequired) {
            builder.putBits(1, 1);
            builder.putBits(2, 0);
        } else {
            builder.putBits(1, 0);
        }
    }
}
Also used : Position(org.apollo.game.model.Position) TeleportSegment(org.apollo.game.sync.seg.TeleportSegment) Direction(org.apollo.game.model.Direction) MovementSegment(org.apollo.game.sync.seg.MovementSegment)

Example 3 with MovementSegment

use of org.apollo.game.sync.seg.MovementSegment in project apollo by apollo-rsps.

the class PlayerSynchronizationMessageEncoder method putMovementUpdate.

/**
 * Puts a movement update for the specified segment.
 *
 * @param seg The segment.
 * @param message The message.
 * @param builder The builder.
 */
private static void putMovementUpdate(SynchronizationSegment seg, PlayerSynchronizationMessage message, GamePacketBuilder builder) {
    boolean updateRequired = seg.getBlockSet().size() > 0;
    if (seg.getType() == SegmentType.TELEPORT) {
        Position position = ((TeleportSegment) seg).getDestination();
        builder.putBits(1, 1);
        builder.putBits(2, 3);
        builder.putBits(2, position.getHeight());
        builder.putBits(1, message.hasRegionChanged() ? 0 : 1);
        builder.putBits(1, updateRequired ? 1 : 0);
        builder.putBits(7, position.getLocalY(message.getLastKnownRegion()));
        builder.putBits(7, position.getLocalX(message.getLastKnownRegion()));
    } else if (seg.getType() == SegmentType.RUN) {
        Direction[] directions = ((MovementSegment) seg).getDirections();
        builder.putBits(1, 1);
        builder.putBits(2, 2);
        builder.putBits(3, directions[0].toInteger());
        builder.putBits(3, directions[1].toInteger());
        builder.putBits(1, updateRequired ? 1 : 0);
    } else if (seg.getType() == SegmentType.WALK) {
        Direction[] directions = ((MovementSegment) seg).getDirections();
        builder.putBits(1, 1);
        builder.putBits(2, 1);
        builder.putBits(3, directions[0].toInteger());
        builder.putBits(1, updateRequired ? 1 : 0);
    } else {
        if (updateRequired) {
            builder.putBits(1, 1);
            builder.putBits(2, 0);
        } else {
            builder.putBits(1, 0);
        }
    }
}
Also used : Position(org.apollo.game.model.Position) TeleportSegment(org.apollo.game.sync.seg.TeleportSegment) Direction(org.apollo.game.model.Direction) MovementSegment(org.apollo.game.sync.seg.MovementSegment)

Example 4 with MovementSegment

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

Position (org.apollo.game.model.Position)4 MovementSegment (org.apollo.game.sync.seg.MovementSegment)4 TeleportSegment (org.apollo.game.sync.seg.TeleportSegment)3 ArrayList (java.util.ArrayList)2 Direction (org.apollo.game.model.Direction)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 RemoveMobSegment (org.apollo.game.sync.seg.RemoveMobSegment)2 SynchronizationSegment (org.apollo.game.sync.seg.SynchronizationSegment)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 AddNpcSegment (org.apollo.game.sync.seg.AddNpcSegment)1 AddPlayerSegment (org.apollo.game.sync.seg.AddPlayerSegment)1