Search in sources :

Example 6 with Direction

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

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

the class CollisionManager method apply.

/**
 * Apply a {@link CollisionUpdate} to the game world.
 *
 * @param update The update to apply.
 */
public void apply(CollisionUpdate update) {
    Region prev = null;
    CollisionUpdateType type = update.getType();
    Map<Position, Collection<DirectionFlag>> map = update.getFlags().asMap();
    for (Map.Entry<Position, Collection<DirectionFlag>> entry : map.entrySet()) {
        Position position = entry.getKey();
        int height = position.getHeight();
        if (bridges.contains(new Position(position.getX(), position.getY(), 1))) {
            if (--height < 0) {
                continue;
            }
        }
        if (prev == null || !prev.contains(position)) {
            prev = regions.fromPosition(position);
        }
        int localX = position.getX() % Region.SIZE;
        int localY = position.getY() % Region.SIZE;
        CollisionMatrix matrix = prev.getMatrix(height);
        CollisionFlag[] mobs = CollisionFlag.mobs();
        CollisionFlag[] projectiles = CollisionFlag.projectiles();
        for (DirectionFlag flag : entry.getValue()) {
            Direction direction = flag.getDirection();
            if (direction == Direction.NONE) {
                continue;
            }
            int orientation = direction.toInteger();
            if (flag.isImpenetrable()) {
                flag(type, matrix, localX, localY, projectiles[orientation]);
            }
            flag(type, matrix, localX, localY, mobs[orientation]);
        }
    }
}
Also used : Position(org.apollo.game.model.Position) DirectionFlag(org.apollo.game.model.area.collision.CollisionUpdate.DirectionFlag) Direction(org.apollo.game.model.Direction) Region(org.apollo.game.model.area.Region) Collection(java.util.Collection) Map(java.util.Map)

Example 8 with Direction

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

the class WalkingQueue method pulse.

/**
 * Pulses this WalkingQueue.
 */
public void pulse() {
    Position position = mob.getPosition();
    int height = position.getHeight();
    Direction firstDirection = Direction.NONE;
    Direction secondDirection = Direction.NONE;
    World world = mob.getWorld();
    CollisionManager collisionManager = world.getCollisionManager();
    Position next = points.poll();
    if (next != null) {
        firstDirection = Direction.between(position, next);
        if (!collisionManager.traversable(position, EntityType.NPC, firstDirection)) {
            clear();
            firstDirection = Direction.NONE;
        } else {
            previousPoints.add(next);
            position = new Position(next.getX(), next.getY(), height);
            mob.setLastDirection(firstDirection);
            if (running) {
                next = points.poll();
                if (next != null) {
                    secondDirection = Direction.between(position, next);
                    if (!collisionManager.traversable(position, EntityType.NPC, secondDirection)) {
                        clear();
                        secondDirection = Direction.NONE;
                    } else {
                        previousPoints.add(next);
                        position = new Position(next.getX(), next.getY(), height);
                        mob.setLastDirection(secondDirection);
                    }
                }
            }
        }
    }
    mob.setDirections(firstDirection, secondDirection);
    mob.setPosition(position);
}
Also used : CollisionManager(org.apollo.game.model.area.collision.CollisionManager) Position(org.apollo.game.model.Position) World(org.apollo.game.model.World) Direction(org.apollo.game.model.Direction)

Example 9 with Direction

use of org.apollo.game.model.Direction 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 position = ((TeleportSegment) seg).getDestination();
        builder.putBits(1, 1);
        builder.putBits(2, 3);
        builder.putBits(2, position.getHeight());
        builder.putBits(1, message.hasRegionChanged() ? 0 : 1);
        builder.putBits(1, updateRequired ? 1 : 0);
        builder.putBits(7, position.getLocalY(message.getLastKnownRegion()));
        builder.putBits(7, position.getLocalX(message.getLastKnownRegion()));
    } 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 10 with Direction

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

the class CollisionManagerTests method assertUntraversable.

/**
 * Helper test assertion that a position is untraversable in a given direction.
 *
 * @param collisionManager The {@link CollisionManager} to check.
 * @param position The {@link Position}.
 * @param directions The {@link Direction}s to assert.
 */
private static void assertUntraversable(CollisionManager collisionManager, Position position, Direction... directions) {
    for (Direction direction : directions) {
        boolean traversable = collisionManager.traversable(position, EntityType.NPC, direction);
        String message = String.format("Can walk %s from tile at (%d,%d), should not be able to", direction.toString(), position.getX(), position.getY());
        Assert.assertFalse(message, traversable);
    }
}
Also used : Direction(org.apollo.game.model.Direction)

Aggregations

Direction (org.apollo.game.model.Direction)10 Position (org.apollo.game.model.Position)8 Region (org.apollo.game.model.area.Region)2 MovementSegment (org.apollo.game.sync.seg.MovementSegment)2 TeleportSegment (org.apollo.game.sync.seg.TeleportSegment)2 ArrayDeque (java.util.ArrayDeque)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 PriorityQueue (java.util.PriorityQueue)1 World (org.apollo.game.model.World)1 CollisionManager (org.apollo.game.model.area.collision.CollisionManager)1 DirectionFlag (org.apollo.game.model.area.collision.CollisionUpdate.DirectionFlag)1