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;
}
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();
}
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);
}
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());
}
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));
}
Aggregations