Search in sources :

Example 16 with Position

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

the class PrePlayerSynchronizationTask method isRegionUpdateRequired.

/**
 * Checks if a region update is required.
 *
 * @return {@code true} if a Region update is required, {@code false} if not.
 */
private boolean isRegionUpdateRequired() {
    Position current = player.getPosition();
    Position last = player.getLastKnownRegion();
    int deltaX = current.getLocalX(last);
    int deltaY = current.getLocalY(last);
    return deltaX <= Position.MAX_DISTANCE || deltaX >= Region.VIEWPORT_WIDTH - Position.MAX_DISTANCE - 1 || deltaY <= Position.MAX_DISTANCE || deltaY >= Region.VIEWPORT_WIDTH - Position.MAX_DISTANCE - 1;
}
Also used : Position(org.apollo.game.model.Position)

Example 17 with Position

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

the class ObjectActionVerificationHandlerTests method terminateIfNoObject.

@Test
public void terminateIfNoObject() throws Exception {
    Position playerPosition = new Position(3200, 3200);
    Position objectPosition = new Position(3200, 3201);
    World world = mock(World.class);
    Region region = mock(Region.class);
    RegionRepository regionRepository = mock(RegionRepository.class);
    Player player = mock(Player.class);
    Set<Entity> entitySet = new HashSet<>();
    when(world.getRegionRepository()).thenReturn(regionRepository);
    when(regionRepository.fromPosition(objectPosition)).thenReturn(region);
    when(player.getPosition()).thenReturn(playerPosition);
    when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)).thenReturn(entitySet);
    ObjectActionMessage objectActionMessage = new ObjectActionMessage(1, 4151, objectPosition);
    ObjectActionVerificationHandler objectActionVerificationHandler = new ObjectActionVerificationHandler(world);
    objectActionVerificationHandler.handle(player, objectActionMessage);
    assertTrue("ObjectVerificationHandler: message not terminated when no object exists!", objectActionMessage.terminated());
}
Also used : Entity(org.apollo.game.model.entity.Entity) Player(org.apollo.game.model.entity.Player) ObjectActionMessage(org.apollo.game.message.impl.ObjectActionMessage) Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region) World(org.apollo.game.model.World) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 18 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);
    int x = (int) reader.getUnsigned(DataType.SHORT);
    int y = (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)

Example 19 with Position

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

the class PlayerSynchronizationTask method run.

@Override
public void run() {
    Position lastKnownRegion = player.getLastKnownRegion();
    boolean regionChanged = player.hasRegionChanged();
    int[] appearanceTickets = player.getAppearanceTickets();
    SynchronizationBlockSet blockSet = player.getBlockSet();
    if (blockSet.contains(ChatBlock.class)) {
        blockSet = blockSet.clone();
        blockSet.remove(ChatBlock.class);
    }
    Position position = player.getPosition();
    SynchronizationSegment segment = (player.isTeleporting() || player.hasRegionChanged()) ? new TeleportSegment(blockSet, position) : new MovementSegment(blockSet, player.getDirections());
    List<Player> localPlayers = player.getLocalPlayerList();
    int oldCount = localPlayers.size();
    List<SynchronizationSegment> segments = new ArrayList<>();
    int distance = player.getViewingDistance();
    for (Iterator<Player> iterator = localPlayers.iterator(); iterator.hasNext(); ) {
        Player other = iterator.next();
        if (removeable(position, distance, other)) {
            iterator.remove();
            segments.add(new RemoveMobSegment());
        } else {
            segments.add(new MovementSegment(other.getBlockSet(), other.getDirections()));
        }
    }
    int added = 0, count = localPlayers.size();
    RegionRepository repository = player.getWorld().getRegionRepository();
    Region current = repository.fromPosition(position);
    Set<RegionCoordinates> regions = current.getSurrounding();
    regions.add(current.getCoordinates());
    Stream<Player> players = regions.stream().map(repository::get).flatMap(region -> region.getEntities(EntityType.PLAYER));
    Iterator<Player> iterator = players.iterator();
    while (iterator.hasNext()) {
        if (count >= MAXIMUM_LOCAL_PLAYERS) {
            player.flagExcessivePlayers();
            break;
        } else if (added >= NEW_PLAYERS_PER_CYCLE) {
            break;
        }
        Player other = iterator.next();
        Position local = other.getPosition();
        if (other != player && local.isWithinDistance(position, distance) && !localPlayers.contains(other)) {
            localPlayers.add(other);
            count++;
            added++;
            blockSet = other.getBlockSet();
            int index = other.getIndex();
            if (!blockSet.contains(AppearanceBlock.class) && !hasCachedAppearance(appearanceTickets, index - 1, other.getAppearanceTicket())) {
                blockSet = blockSet.clone();
                blockSet.add(SynchronizationBlock.createAppearanceBlock(other));
            }
            segments.add(new AddPlayerSegment(blockSet, index, local));
        }
    }
    PlayerSynchronizationMessage message = new PlayerSynchronizationMessage(lastKnownRegion, position, regionChanged, segment, oldCount, segments);
    player.send(message);
}
Also used : Player(org.apollo.game.model.entity.Player) Position(org.apollo.game.model.Position) ArrayList(java.util.ArrayList) TeleportSegment(org.apollo.game.sync.seg.TeleportSegment) SynchronizationBlockSet(org.apollo.game.sync.block.SynchronizationBlockSet) MovementSegment(org.apollo.game.sync.seg.MovementSegment) RemoveMobSegment(org.apollo.game.sync.seg.RemoveMobSegment) RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) RegionRepository(org.apollo.game.model.area.RegionRepository) AddPlayerSegment(org.apollo.game.sync.seg.AddPlayerSegment) Region(org.apollo.game.model.area.Region) SynchronizationSegment(org.apollo.game.sync.seg.SynchronizationSegment) PlayerSynchronizationMessage(org.apollo.game.message.impl.PlayerSynchronizationMessage)

Example 20 with Position

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

the class Mob method setPosition.

/**
 * Sets the {@link Position} of this mob.
 * <p>
 * This method may be intercepted using a {@link MobPositionUpdateEvent}, which can be terminated like any
 * other. Plugins that intercept this Event <strong>must</strong> be cautious, because movement will not be
 * possible (even through mechanisms such as teleporting) if the Event is terminated.
 *
 * @param position The Position.
 */
public final void setPosition(Position position) {
    if (!position.equals(this.position) && world.submit(new MobPositionUpdateEvent(this, position))) {
        Position old = this.position;
        RegionRepository repository = world.getRegionRepository();
        Region current = repository.fromPosition(old), next = repository.fromPosition(position);
        current.removeEntity(this);
        // addEntity relies on the position being updated, so do that first.
        this.position = position;
        next.addEntity(this);
    }
}
Also used : MobPositionUpdateEvent(org.apollo.game.model.event.impl.MobPositionUpdateEvent) Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region)

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