Search in sources :

Example 46 with Position

use of org.apollo.game.model.Position 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 47 with Position

use of org.apollo.game.model.Position in project apollo by apollo-rsps.

the class PositionHintIconMessageEncoder method encode.

@Override
public GamePacket encode(PositionHintIconMessage message) {
    GamePacketBuilder builder = new GamePacketBuilder(199);
    HintIconMessage.Type type = message.getType();
    if (type == Type.PLAYER || type == Type.NPC) {
        throw new IllegalStateException("Unsupported hint icon type " + type + ".");
    }
    builder.put(DataType.BYTE, type.getValue());
    Position position = message.getPosition();
    builder.put(DataType.SHORT, position.getX());
    builder.put(DataType.SHORT, position.getY());
    builder.put(DataType.BYTE, message.getHeight());
    return builder.toGamePacket();
}
Also used : Position(org.apollo.game.model.Position) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) Type(org.apollo.game.message.impl.HintIconMessage.Type) HintIconMessage(org.apollo.game.message.impl.HintIconMessage) PositionHintIconMessage(org.apollo.game.message.impl.PositionHintIconMessage)

Example 48 with Position

use of org.apollo.game.model.Position in project apollo by apollo-rsps.

the class SendProjectileMessageEncoder method encode.

@Override
public GamePacket encode(SendProjectileMessage message) {
    Projectile projectile = message.getProjectile();
    Position source = projectile.getPosition();
    Position destination = projectile.getDestination();
    GamePacketBuilder builder = new GamePacketBuilder(181);
    builder.put(DataType.BYTE, message.getPositionOffset());
    builder.put(DataType.BYTE, destination.getX() - source.getX());
    builder.put(DataType.BYTE, destination.getY() - source.getY());
    builder.put(DataType.SHORT, projectile.getTarget());
    builder.put(DataType.SHORT, projectile.getGraphic());
    builder.put(DataType.BYTE, projectile.getStartHeight());
    builder.put(DataType.BYTE, projectile.getEndHeight());
    builder.put(DataType.SHORT, projectile.getDelay());
    builder.put(DataType.SHORT, projectile.getLifetime());
    builder.put(DataType.BYTE, projectile.getPitch());
    builder.put(DataType.BYTE, projectile.getOffset());
    return builder.toGamePacket();
}
Also used : Position(org.apollo.game.model.Position) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) Projectile(org.apollo.game.model.entity.Projectile)

Example 49 with Position

use of org.apollo.game.model.Position in project apollo by apollo-rsps.

the class BinaryPlayerSerializer method savePlayer.

@Override
public void savePlayer(Player player) throws IOException {
    Path file = getFile(player.getUsername());
    try (DataOutputStream out = new DataOutputStream(Files.newOutputStream(file))) {
        StreamUtil.writeString(out, player.getUsername());
        StreamUtil.writeString(out, player.getCredentials().getPassword());
        out.writeByte(player.getPrivilegeLevel().toInteger());
        out.writeByte(player.getMembershipStatus().getValue());
        out.writeByte(player.getChatPrivacy().toInteger(true));
        out.writeByte(player.getFriendPrivacy().toInteger(false));
        out.writeByte(player.getTradePrivacy().toInteger(false));
        out.writeByte(player.getScreenBrightness().toInteger());
        Position position = player.getPosition();
        out.writeShort(position.getX());
        out.writeShort(position.getY());
        out.writeByte(position.getHeight());
        Appearance appearance = player.getAppearance();
        out.writeByte(appearance.getGender().toInteger());
        int[] style = appearance.getStyle();
        for (int element : style) {
            out.writeByte(element);
        }
        int[] colors = appearance.getColors();
        for (int color : colors) {
            out.writeByte(color);
        }
        writeInventory(out, player.getInventory());
        writeInventory(out, player.getEquipment());
        writeInventory(out, player.getBank());
        SkillSet skills = player.getSkillSet();
        out.writeByte(skills.size());
        for (int id = 0; id < skills.size(); id++) {
            Skill skill = skills.getSkill(id);
            out.writeByte(skill.getCurrentLevel());
            out.writeDouble(skill.getExperience());
        }
        List<String> usernames = player.getFriendUsernames();
        out.writeByte(usernames.size());
        for (String username : usernames) {
            out.writeLong(NameUtil.encodeBase37(username));
        }
        usernames = player.getIgnoredUsernames();
        out.writeByte(usernames.size());
        for (String username : usernames) {
            out.writeLong(NameUtil.encodeBase37(username));
        }
        Set<Entry<String, Attribute<?>>> attributes = player.getAttributes().entrySet();
        attributes.removeIf(e -> AttributeMap.getDefinition(e.getKey()).getPersistence() != AttributePersistence.PERSISTENT);
        out.writeInt(attributes.size());
        for (Entry<String, Attribute<?>> entry : attributes) {
            String name = entry.getKey();
            StreamUtil.writeString(out, name);
            Attribute<?> attribute = entry.getValue();
            out.writeByte(attribute.getType().getValue());
            out.write(attribute.encode());
        }
    }
}
Also used : Path(java.nio.file.Path) Position(org.apollo.game.model.Position) NumericalAttribute(org.apollo.game.model.entity.attr.NumericalAttribute) BooleanAttribute(org.apollo.game.model.entity.attr.BooleanAttribute) Attribute(org.apollo.game.model.entity.attr.Attribute) StringAttribute(org.apollo.game.model.entity.attr.StringAttribute) DataOutputStream(java.io.DataOutputStream) Appearance(org.apollo.game.model.Appearance) Skill(org.apollo.game.model.entity.Skill) Entry(java.util.Map.Entry) SkillSet(org.apollo.game.model.entity.SkillSet)

Example 50 with Position

use of org.apollo.game.model.Position in project apollo by apollo-rsps.

the class ItemOnObjectVerificationHandler method handle.

@Override
public void handle(Player player, ItemOnObjectMessage message) {
    if (message.getInterfaceId() != SynchronizationInventoryListener.INVENTORY_ID && message.getInterfaceId() != BankConstants.SIDEBAR_INVENTORY_ID) {
        message.terminate();
        return;
    }
    Inventory inventory = player.getInventory();
    int slot = message.getSlot();
    if (slot < 0 || slot >= inventory.capacity()) {
        message.terminate();
        return;
    }
    Item item = inventory.get(slot);
    if (item == null || item.getId() != message.getId()) {
        message.terminate();
        return;
    }
    int objectId = message.getObjectId();
    if (objectId < 0 || objectId >= ObjectDefinition.count()) {
        message.terminate();
        return;
    }
    Position position = message.getPosition();
    Region region = world.getRegionRepository().fromPosition(position);
    Set<GameObject> objects = region.getEntities(position, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT);
    if (!player.getPosition().isWithinDistance(position, 15) || !ObjectActionVerificationHandler.containsObject(objectId, objects)) {
        message.terminate();
        return;
    }
}
Also used : Item(org.apollo.game.model.Item) Position(org.apollo.game.model.Position) GameObject(org.apollo.game.model.entity.obj.GameObject) Region(org.apollo.game.model.area.Region) Inventory(org.apollo.game.model.inv.Inventory)

Aggregations

Position (org.apollo.game.model.Position)66 Region (org.apollo.game.model.area.Region)14 RegionRepository (org.apollo.game.model.area.RegionRepository)10 GamePacketReader (org.apollo.net.codec.game.GamePacketReader)10 GamePacketBuilder (org.apollo.net.codec.game.GamePacketBuilder)9 ObjectActionMessage (org.apollo.game.message.impl.ObjectActionMessage)8 Direction (org.apollo.game.model.Direction)8 Entity (org.apollo.game.model.entity.Entity)8 HashSet (java.util.HashSet)7 World (org.apollo.game.model.World)7 Player (org.apollo.game.model.entity.Player)7 Test (org.junit.Test)7 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 ArrayList (java.util.ArrayList)3 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)3 GroupableEntity (org.apollo.game.model.area.update.GroupableEntity)3 Inventory (org.apollo.game.model.inv.Inventory)3 MovementSegment (org.apollo.game.sync.seg.MovementSegment)3