Search in sources :

Example 31 with Position

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

the class AStarPathfindingAlgorithm method find.

@Override
public Deque<Position> find(Position origin, Position target) {
    Map<Position, Node> nodes = new HashMap<>();
    Node start = new Node(origin), end = new Node(target);
    nodes.put(origin, start);
    nodes.put(target, end);
    Set<Node> open = new HashSet<>();
    Queue<Node> sorted = new PriorityQueue<>();
    open.add(start);
    sorted.add(start);
    do {
        Node active = getCheapest(sorted);
        Position position = active.getPosition();
        if (position.equals(target)) {
            break;
        }
        open.remove(active);
        active.close();
        int x = position.getX(), y = position.getY();
        for (int nextX = x - 1; nextX <= x + 1; nextX++) {
            for (int nextY = y - 1; nextY <= y + 1; nextY++) {
                if (nextX == x && nextY == y) {
                    continue;
                }
                Position adjacent = new Position(nextX, nextY);
                Direction direction = Direction.between(adjacent, position);
                if (traversable(adjacent, direction)) {
                    Node node = nodes.computeIfAbsent(adjacent, Node::new);
                    compare(active, node, open, sorted, heuristic);
                }
            }
        }
    } while (!open.isEmpty());
    Deque<Position> shortest = new ArrayDeque<>();
    Node active = end;
    if (active.hasParent()) {
        Position position = active.getPosition();
        while (!origin.equals(position)) {
            shortest.addFirst(position);
            // If the target has a parent then all of the others will.
            active = active.getParent();
            position = active.getPosition();
        }
    }
    return shortest;
}
Also used : Position(org.apollo.game.model.Position) HashMap(java.util.HashMap) PriorityQueue(java.util.PriorityQueue) Direction(org.apollo.game.model.Direction) ArrayDeque(java.util.ArrayDeque) HashSet(java.util.HashSet)

Example 32 with Position

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

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

the class NpcSynchronizationMessageEncoder method putTurnToPositionBlock.

/**
 * Puts a turn to position block into the specified builder.
 *
 * @param block The block.
 * @param builder The builder.
 */
private static void putTurnToPositionBlock(TurnToPositionBlock block, GamePacketBuilder builder) {
    Position position = block.getPosition();
    builder.put(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD, position.getX() * 2 + 1);
    builder.put(DataType.SHORT, DataOrder.LITTLE, position.getY() * 2 + 1);
}
Also used : Position(org.apollo.game.model.Position)

Example 34 with Position

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

the class NpcSynchronizationMessageEncoder method putAddNpcUpdate.

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

Example 35 with Position

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

the class SecondObjectActionMessageDecoder method decode.

@Override
public ObjectActionMessage decode(GamePacket packet) {
    GamePacketReader reader = new GamePacketReader(packet);
    int id = (int) reader.getUnsigned(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD);
    int y = (int) reader.getUnsigned(DataType.SHORT, DataOrder.LITTLE);
    int x = (int) reader.getUnsigned(DataType.SHORT, DataTransformation.ADD);
    return new ObjectActionMessage(2, id, new Position(x, y));
}
Also used : ObjectActionMessage(org.apollo.game.message.impl.ObjectActionMessage) GamePacketReader(org.apollo.net.codec.game.GamePacketReader) 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