Search in sources :

Example 1 with RegionUpdateMessage

use of org.apollo.game.message.impl.RegionUpdateMessage in project apollo by apollo-rsps.

the class GroupedRegionUpdateMessageEncoder method encode.

@Override
public GamePacket encode(GroupedRegionUpdateMessage message) {
    GamePacketBuilder builder = new GamePacketBuilder(60, PacketType.VARIABLE_SHORT);
    Position base = message.getLastKnownRegion(), region = message.getRegionPosition();
    builder.put(DataType.BYTE, region.getLocalY(base));
    builder.put(DataType.BYTE, DataTransformation.NEGATE, region.getLocalX(base));
    for (RegionUpdateMessage update : message.getMessages()) {
        @SuppressWarnings("unchecked") MessageEncoder<RegionUpdateMessage> encoder = (MessageEncoder<RegionUpdateMessage>) release.getMessageEncoder(update.getClass());
        GamePacket packet = encoder.encode(update);
        builder.put(DataType.BYTE, packet.getOpcode());
        builder.putBytes(packet.getPayload());
    }
    return builder.toGamePacket();
}
Also used : GamePacket(org.apollo.net.codec.game.GamePacket) Position(org.apollo.game.model.Position) RegionUpdateMessage(org.apollo.game.message.impl.RegionUpdateMessage) GroupedRegionUpdateMessage(org.apollo.game.message.impl.GroupedRegionUpdateMessage) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) MessageEncoder(org.apollo.net.release.MessageEncoder)

Example 2 with RegionUpdateMessage

use of org.apollo.game.message.impl.RegionUpdateMessage in project apollo by apollo-rsps.

the class Region method record.

/**
 * Records the specified {@link GroupableEntity} as being updated this pulse.
 *
 * @param entity The GroupableEntity.
 * @param update The {@link EntityUpdateType}.
 * @throws UnsupportedOperationException If the specified Entity cannot be operated on in this manner.
 */
private <T extends Entity & GroupableEntity> void record(T entity, EntityUpdateType update) {
    UpdateOperation<?> operation = entity.toUpdateOperation(this, update);
    RegionUpdateMessage message = operation.toMessage(), inverse = operation.inverse();
    int height = entity.getPosition().getHeight();
    Set<RegionUpdateMessage> updates = this.updates.get(height);
    EntityType type = entity.getEntityType();
    if (type == EntityType.STATIC_OBJECT) {
        // TODO set/clear collision matrix values
        if (update == EntityUpdateType.REMOVE) {
            removedObjects.get(height).add(message);
        } else {
            // TODO should this really be possible?
            removedObjects.get(height).remove(inverse);
        }
    } else if (update == EntityUpdateType.REMOVE && !type.isTransient()) {
        updates.remove(inverse);
    }
    updates.add(message);
}
Also used : EntityType(org.apollo.game.model.entity.EntityType) RegionUpdateMessage(org.apollo.game.message.impl.RegionUpdateMessage)

Example 3 with RegionUpdateMessage

use of org.apollo.game.message.impl.RegionUpdateMessage in project apollo by apollo-rsps.

the class GroupedRegionUpdateMessageEncoder method encode.

@Override
public GamePacket encode(GroupedRegionUpdateMessage message) {
    GamePacketBuilder builder = new GamePacketBuilder(183, PacketType.VARIABLE_SHORT);
    Position base = message.getLastKnownRegion(), region = message.getRegionPosition();
    builder.put(DataType.BYTE, region.getLocalX(base));
    builder.put(DataType.BYTE, DataTransformation.ADD, region.getLocalY(base));
    for (RegionUpdateMessage update : message.getMessages()) {
        @SuppressWarnings("unchecked") MessageEncoder<RegionUpdateMessage> encoder = (MessageEncoder<RegionUpdateMessage>) release.getMessageEncoder(update.getClass());
        GamePacket packet = encoder.encode(update);
        builder.put(DataType.BYTE, packet.getOpcode());
        builder.putBytes(packet.getPayload());
    }
    return builder.toGamePacket();
}
Also used : GamePacket(org.apollo.net.codec.game.GamePacket) Position(org.apollo.game.model.Position) RegionUpdateMessage(org.apollo.game.message.impl.RegionUpdateMessage) GroupedRegionUpdateMessage(org.apollo.game.message.impl.GroupedRegionUpdateMessage) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) MessageEncoder(org.apollo.net.release.MessageEncoder)

Example 4 with RegionUpdateMessage

use of org.apollo.game.message.impl.RegionUpdateMessage in project apollo by apollo-rsps.

the class PrePlayerSynchronizationTask method sendUpdates.

/**
 * Sends the updates for a {@link Region}
 *
 * @param position The {@link Position} of the last known region.
 * @param differences The {@link Set} of {@link RegionCoordinates} of Regions that changed.
 * @param full The {@link Set} of {@link RegionCoordinates} of Regions that require a full update.
 */
private void sendUpdates(Position position, Set<RegionCoordinates> differences, Set<RegionCoordinates> full) {
    RegionRepository repository = player.getWorld().getRegionRepository();
    int height = position.getHeight();
    for (RegionCoordinates coordinates : differences) {
        Set<RegionUpdateMessage> messages = updates.computeIfAbsent(coordinates, coords -> repository.get(coords).getUpdates(height));
        if (!messages.isEmpty()) {
            player.send(new GroupedRegionUpdateMessage(position, coordinates, messages));
        }
    }
    for (RegionCoordinates coordinates : full) {
        Set<RegionUpdateMessage> messages = encodes.computeIfAbsent(coordinates, coords -> repository.get(coords).encode(height));
        if (!messages.isEmpty()) {
            player.send(new ClearRegionMessage(position, coordinates));
            player.send(new GroupedRegionUpdateMessage(position, coordinates, messages));
        }
    }
}
Also used : RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) GroupedRegionUpdateMessage(org.apollo.game.message.impl.GroupedRegionUpdateMessage) RegionRepository(org.apollo.game.model.area.RegionRepository) RegionUpdateMessage(org.apollo.game.message.impl.RegionUpdateMessage) GroupedRegionUpdateMessage(org.apollo.game.message.impl.GroupedRegionUpdateMessage) ClearRegionMessage(org.apollo.game.message.impl.ClearRegionMessage)

Aggregations

RegionUpdateMessage (org.apollo.game.message.impl.RegionUpdateMessage)4 GroupedRegionUpdateMessage (org.apollo.game.message.impl.GroupedRegionUpdateMessage)3 Position (org.apollo.game.model.Position)2 GamePacket (org.apollo.net.codec.game.GamePacket)2 GamePacketBuilder (org.apollo.net.codec.game.GamePacketBuilder)2 MessageEncoder (org.apollo.net.release.MessageEncoder)2 ClearRegionMessage (org.apollo.game.message.impl.ClearRegionMessage)1 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)1 RegionRepository (org.apollo.game.model.area.RegionRepository)1 EntityType (org.apollo.game.model.entity.EntityType)1