Search in sources :

Example 1 with Position

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

the class WorldObjectsDecoder method run.

/**
 * Decode the {@code MapObject}s from the cache and register them with the world.
 */
@Override
public void run() {
    Map<Integer, MapIndex> mapIndices = MapIndex.getIndices();
    try {
        for (MapIndex index : mapIndices.values()) {
            MapObjectsDecoder decoder = MapObjectsDecoder.create(fs, index);
            List<MapObject> objects = decoder.decode();
            int mapX = index.getX(), mapY = index.getY();
            for (MapObject object : objects) {
                Position position = new Position(mapX + object.getLocalX(), mapY + object.getLocalY(), object.getHeight());
                StaticGameObject gameObject = new StaticGameObject(world, object.getId(), position, object.getType(), object.getOrientation());
                regionRepository.fromPosition(position).addEntity(gameObject, false);
            }
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}
Also used : Position(org.apollo.game.model.Position) MapObjectsDecoder(org.apollo.cache.map.MapObjectsDecoder) UncheckedIOException(java.io.UncheckedIOException) MapIndex(org.apollo.cache.map.MapIndex) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MapObject(org.apollo.cache.map.MapObject)

Example 2 with Position

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

the class BinaryPlayerSerializer method loadPlayer.

@Override
public PlayerLoaderResponse loadPlayer(PlayerCredentials credentials) throws IOException {
    Path path = getFile(credentials.getUsername());
    if (!Files.exists(path)) {
        Player player = new Player(world, credentials, TUTORIAL_ISLAND_SPAWN);
        credentials.setPassword(SCryptUtil.scrypt(credentials.getPassword(), 16384, 8, 1));
        return new PlayerLoaderResponse(LoginConstants.STATUS_OK, player);
    }
    try (DataInputStream in = new DataInputStream(new BufferedInputStream(Files.newInputStream(path)))) {
        String name = StreamUtil.readString(in);
        String password = StreamUtil.readString(in);
        if (!name.equalsIgnoreCase(credentials.getUsername()) || !SCryptUtil.check(credentials.getPassword(), password)) {
            return new PlayerLoaderResponse(LoginConstants.STATUS_INVALID_CREDENTIALS);
        }
        // Update password to the hashed one.
        credentials.setPassword(password);
        PrivilegeLevel privilege = PrivilegeLevel.valueOf(in.readByte());
        MembershipStatus members = MembershipStatus.valueOf(in.readByte());
        PrivacyState chatPrivacy = PrivacyState.valueOf(in.readByte(), true);
        PrivacyState friendPrivacy = PrivacyState.valueOf(in.readByte(), false);
        PrivacyState tradePrivacy = PrivacyState.valueOf(in.readByte(), false);
        ScreenBrightness brightness = ScreenBrightness.valueOf(in.readByte());
        int x = in.readUnsignedShort();
        int y = in.readUnsignedShort();
        int height = in.readUnsignedByte();
        Gender gender = in.readUnsignedByte() == Gender.MALE.toInteger() ? Gender.MALE : Gender.FEMALE;
        int[] style = new int[7];
        for (int slot = 0; slot < style.length; slot++) {
            style[slot] = in.readUnsignedByte();
        }
        int[] colors = new int[5];
        for (int slot = 0; slot < colors.length; slot++) {
            colors[slot] = in.readUnsignedByte();
        }
        Player player = new Player(world, credentials, new Position(x, y, height));
        player.setPrivilegeLevel(privilege);
        player.setMembers(members);
        player.setChatPrivacy(chatPrivacy);
        player.setFriendPrivacy(friendPrivacy);
        player.setTradePrivacy(tradePrivacy);
        player.setScreenBrightness(brightness);
        player.setAppearance(new Appearance(gender, style, colors));
        readInventory(in, player.getInventory());
        readInventory(in, player.getEquipment());
        readInventory(in, player.getBank());
        int size = in.readUnsignedByte();
        SkillSet skills = player.getSkillSet();
        skills.stopFiringEvents();
        try {
            for (int i = 0; i < size; i++) {
                int level = in.readUnsignedByte();
                double experience = in.readDouble();
                skills.setSkill(i, new Skill(experience, level, SkillSet.getLevelForExperience(experience)));
            }
        } finally {
            skills.calculateCombatLevel();
            skills.startFiringEvents();
        }
        int friendCount = in.readByte();
        List<String> friends = new ArrayList<>(friendCount);
        for (int i = 0; i < friendCount; i++) {
            friends.add(NameUtil.decodeBase37(in.readLong()));
        }
        player.setFriendUsernames(friends);
        int ignoreCount = in.readByte();
        List<String> ignores = new ArrayList<>(ignoreCount);
        for (int times = 0; times < ignoreCount; times++) {
            ignores.add(NameUtil.decodeBase37(in.readLong()));
        }
        player.setIgnoredUsernames(ignores);
        Map<String, Attribute<?>> attributes = readAttributes(in);
        attributes.forEach(player::setAttribute);
        if (player.isBanned()) {
            return new PlayerLoaderResponse(LoginConstants.STATUS_ACCOUNT_DISABLED);
        }
        return new PlayerLoaderResponse(LoginConstants.STATUS_OK, player);
    }
}
Also used : Path(java.nio.file.Path) MembershipStatus(org.apollo.game.model.entity.setting.MembershipStatus) Player(org.apollo.game.model.entity.Player) 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) ArrayList(java.util.ArrayList) PrivacyState(org.apollo.game.model.entity.setting.PrivacyState) Gender(org.apollo.game.model.entity.setting.Gender) DataInputStream(java.io.DataInputStream) PrivilegeLevel(org.apollo.game.model.entity.setting.PrivilegeLevel) Appearance(org.apollo.game.model.Appearance) Skill(org.apollo.game.model.entity.Skill) ScreenBrightness(org.apollo.game.model.entity.setting.ScreenBrightness) BufferedInputStream(java.io.BufferedInputStream) SkillSet(org.apollo.game.model.entity.SkillSet)

Example 3 with Position

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

the class ObjectActionVerificationHandler method handle.

@Override
public void handle(Player player, ObjectActionMessage message) {
    int id = message.getId();
    if (id < 0 || id >= 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) || !containsObject(id, objects)) {
        message.terminate();
        return;
    }
    ObjectDefinition definition = ObjectDefinition.lookup(id);
    if (message.getOption() >= definition.getMenuActions().length) {
        message.terminate();
        return;
    }
}
Also used : Position(org.apollo.game.model.Position) GameObject(org.apollo.game.model.entity.obj.GameObject) Region(org.apollo.game.model.area.Region) ObjectDefinition(org.apollo.cache.def.ObjectDefinition)

Example 4 with Position

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

the class WorldMapDecoder method markTiles.

/**
 * Mark any tiles in the given {@link MapPlane} as blocked or bridged in the {@link CollisionManager}.
 *
 * @param mapX The X coordinate of the map file.
 * @param mapY The Y coordinate of the map file.
 * @param plane The {@link MapPlane} to load tiles from.
 */
private void markTiles(int mapX, int mapY, MapPlane plane) {
    for (int x = 0; x < MapConstants.MAP_WIDTH; x++) {
        for (int y = 0; y < MapConstants.MAP_WIDTH; y++) {
            Tile tile = plane.getTile(x, y);
            Position position = new Position(mapX + x, mapY + y, plane.getLevel());
            if ((tile.getAttributes() & BLOCKED_TILE) == BLOCKED_TILE) {
                collisionManager.block(position);
            }
            if ((tile.getAttributes() & BRIDGE_TILE) == BRIDGE_TILE) {
                collisionManager.markBridged(position);
            }
        }
    }
}
Also used : Position(org.apollo.game.model.Position) Tile(org.apollo.cache.map.Tile)

Example 5 with Position

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

the class PlayerSynchronizationMessageEncoder method putAddPlayerUpdate.

/**
 * Puts an add player update.
 *
 * @param seg The segment.
 * @param message The message.
 * @param builder The builder.
 */
private static void putAddPlayerUpdate(AddPlayerSegment seg, PlayerSynchronizationMessage message, GamePacketBuilder builder) {
    boolean updateRequired = seg.getBlockSet().size() > 0;
    Position player = message.getPosition();
    Position other = seg.getPosition();
    builder.putBits(11, seg.getIndex());
    builder.putBits(1, updateRequired ? 1 : 0);
    // discard walking queue?
    builder.putBits(1, 1);
    builder.putBits(5, other.getY() - player.getY());
    builder.putBits(5, other.getX() - player.getX());
}
Also used : Position(org.apollo.game.model.Position)

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