Search in sources :

Example 1 with SynchronizationSegment

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

the class PlayerSynchronizationMessageEncoder method encode.

@Override
public GamePacket encode(PlayerSynchronizationMessage message) {
    GamePacketBuilder builder = new GamePacketBuilder(81, PacketType.VARIABLE_SHORT);
    builder.switchToBitAccess();
    GamePacketBuilder blockBuilder = new GamePacketBuilder();
    putMovementUpdate(message.getSegment(), message, builder);
    putBlocks(message.getSegment(), blockBuilder);
    builder.putBits(8, message.getLocalPlayers());
    for (SynchronizationSegment segment : message.getSegments()) {
        SegmentType type = segment.getType();
        if (type == SegmentType.REMOVE_MOB) {
            putRemovePlayerUpdate(builder);
        } else if (type == SegmentType.ADD_MOB) {
            putAddPlayerUpdate((AddPlayerSegment) segment, message, builder);
            putBlocks(segment, blockBuilder);
        } else {
            putMovementUpdate(segment, message, builder);
            putBlocks(segment, blockBuilder);
        }
    }
    if (blockBuilder.getLength() > 0) {
        builder.putBits(11, 2047);
        builder.switchToByteAccess();
        builder.putRawBuilder(blockBuilder);
    } else {
        builder.switchToByteAccess();
    }
    return builder.toGamePacket();
}
Also used : SegmentType(org.apollo.game.sync.seg.SegmentType) AddPlayerSegment(org.apollo.game.sync.seg.AddPlayerSegment) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) SynchronizationSegment(org.apollo.game.sync.seg.SynchronizationSegment)

Example 2 with SynchronizationSegment

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

the class NpcSynchronizationMessageEncoder method encode.

@Override
public GamePacket encode(NpcSynchronizationMessage message) {
    GamePacketBuilder builder = new GamePacketBuilder(65, PacketType.VARIABLE_SHORT);
    builder.switchToBitAccess();
    GamePacketBuilder blockBuilder = new GamePacketBuilder();
    builder.putBits(8, message.getLocalNpcCount());
    for (SynchronizationSegment segment : message.getSegments()) {
        SegmentType type = segment.getType();
        if (type == SegmentType.REMOVE_MOB) {
            putRemoveMobUpdate(builder);
        } else if (type == SegmentType.ADD_MOB) {
            putAddNpcUpdate((AddNpcSegment) segment, message, builder);
            putBlocks(segment, blockBuilder);
        } else {
            putMovementUpdate(segment, message, builder);
            putBlocks(segment, blockBuilder);
        }
    }
    if (blockBuilder.getLength() > 0) {
        builder.putBits(14, 16383);
        builder.switchToByteAccess();
        builder.putRawBuilder(blockBuilder);
    } else {
        builder.switchToByteAccess();
    }
    return builder.toGamePacket();
}
Also used : SegmentType(org.apollo.game.sync.seg.SegmentType) AddNpcSegment(org.apollo.game.sync.seg.AddNpcSegment) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) SynchronizationSegment(org.apollo.game.sync.seg.SynchronizationSegment)

Example 3 with SynchronizationSegment

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

the class PlayerSynchronizationMessageEncoder method encode.

@Override
public GamePacket encode(PlayerSynchronizationMessage message) {
    GamePacketBuilder builder = new GamePacketBuilder(90, PacketType.VARIABLE_SHORT);
    builder.switchToBitAccess();
    GamePacketBuilder blockBuilder = new GamePacketBuilder();
    putMovementUpdate(message.getSegment(), message, builder);
    putBlocks(message.getSegment(), blockBuilder);
    builder.putBits(8, message.getLocalPlayers());
    for (SynchronizationSegment segment : message.getSegments()) {
        SegmentType type = segment.getType();
        if (type == SegmentType.REMOVE_MOB) {
            putRemovePlayerUpdate(builder);
        } else if (type == SegmentType.ADD_MOB) {
            putAddPlayerUpdate((AddPlayerSegment) segment, message, builder);
            putBlocks(segment, blockBuilder);
        } else {
            putMovementUpdate(segment, message, builder);
            putBlocks(segment, blockBuilder);
        }
    }
    if (blockBuilder.getLength() > 0) {
        builder.putBits(11, 2047);
        builder.switchToByteAccess();
        builder.putRawBuilder(blockBuilder);
    } else {
        builder.switchToByteAccess();
    }
    return builder.toGamePacket();
}
Also used : SegmentType(org.apollo.game.sync.seg.SegmentType) AddPlayerSegment(org.apollo.game.sync.seg.AddPlayerSegment) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) SynchronizationSegment(org.apollo.game.sync.seg.SynchronizationSegment)

Example 4 with SynchronizationSegment

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

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

the class NpcSynchronizationMessageEncoder method encode.

@Override
public GamePacket encode(NpcSynchronizationMessage message) {
    GamePacketBuilder builder = new GamePacketBuilder(71, PacketType.VARIABLE_SHORT);
    builder.switchToBitAccess();
    GamePacketBuilder blockBuilder = new GamePacketBuilder();
    builder.putBits(8, message.getLocalNpcCount());
    for (SynchronizationSegment segment : message.getSegments()) {
        SegmentType type = segment.getType();
        if (type == SegmentType.REMOVE_MOB) {
            putRemoveNpcUpdate(builder);
        } else if (type == SegmentType.ADD_MOB) {
            putAddNpcUpdate((AddNpcSegment) segment, message, builder);
            putBlocks(segment, blockBuilder);
        } else {
            putMovementUpdate(segment, message, builder);
            putBlocks(segment, blockBuilder);
        }
    }
    if (blockBuilder.getLength() > 0) {
        builder.putBits(14, 16383);
        builder.switchToByteAccess();
        builder.putRawBuilder(blockBuilder);
    } else {
        builder.switchToByteAccess();
    }
    return builder.toGamePacket();
}
Also used : SegmentType(org.apollo.game.sync.seg.SegmentType) AddNpcSegment(org.apollo.game.sync.seg.AddNpcSegment) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) 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