Search in sources :

Example 1 with AddNpcSegment

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

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

Example 3 with AddNpcSegment

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

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