Search in sources :

Example 1 with RegionCoordinates

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

the class PrePlayerSynchronizationTask method run.

@Override
public void run() {
    Position old = player.getPosition();
    player.getWalkingQueue().pulse();
    boolean local = true;
    if (player.isTeleporting()) {
        player.resetViewingDistance();
        local = false;
    }
    Position position = player.getPosition();
    if (!player.hasLastKnownRegion() || isRegionUpdateRequired()) {
        player.setRegionChanged(true);
        local = false;
        player.setLastKnownRegion(position);
        player.send(new RegionChangeMessage(position));
    }
    RegionRepository repository = player.getWorld().getRegionRepository();
    Set<RegionCoordinates> oldViewable = repository.fromPosition(old).getSurrounding();
    Set<RegionCoordinates> newViewable = repository.fromPosition(position).getSurrounding();
    Set<RegionCoordinates> differences = new HashSet<>(newViewable);
    differences.retainAll(oldViewable);
    Set<RegionCoordinates> full = new HashSet<>(newViewable);
    if (local) {
        full.removeAll(oldViewable);
    }
    sendUpdates(player.getLastKnownRegion(), differences, full);
}
Also used : RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) RegionChangeMessage(org.apollo.game.message.impl.RegionChangeMessage) HashSet(java.util.HashSet)

Example 2 with RegionCoordinates

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

the class SequentialClientSynchronizer method synchronize.

@Override
public void synchronize(MobRepository<Player> players, MobRepository<Npc> npcs) {
    Map<RegionCoordinates, Set<RegionUpdateMessage>> encodes = new HashMap<>(), updates = new HashMap<>();
    for (Player player : players) {
        SynchronizationTask task = new PrePlayerSynchronizationTask(player, encodes, updates);
        task.run();
    }
    for (Npc npc : npcs) {
        SynchronizationTask task = new PreNpcSynchronizationTask(npc);
        task.run();
    }
    for (Player player : players) {
        SynchronizationTask task = new PlayerSynchronizationTask(player);
        task.run();
        task = new NpcSynchronizationTask(player);
        task.run();
    }
    for (Player player : players) {
        SynchronizationTask task = new PostPlayerSynchronizationTask(player);
        task.run();
    }
    for (Npc npc : npcs) {
        SynchronizationTask task = new PostNpcSynchronizationTask(npc);
        task.run();
    }
}
Also used : Npc(org.apollo.game.model.entity.Npc) Player(org.apollo.game.model.entity.Player) Set(java.util.Set) PlayerSynchronizationTask(org.apollo.game.sync.task.PlayerSynchronizationTask) PrePlayerSynchronizationTask(org.apollo.game.sync.task.PrePlayerSynchronizationTask) PostPlayerSynchronizationTask(org.apollo.game.sync.task.PostPlayerSynchronizationTask) HashMap(java.util.HashMap) PrePlayerSynchronizationTask(org.apollo.game.sync.task.PrePlayerSynchronizationTask) NpcSynchronizationTask(org.apollo.game.sync.task.NpcSynchronizationTask) PlayerSynchronizationTask(org.apollo.game.sync.task.PlayerSynchronizationTask) PrePlayerSynchronizationTask(org.apollo.game.sync.task.PrePlayerSynchronizationTask) SynchronizationTask(org.apollo.game.sync.task.SynchronizationTask) PostNpcSynchronizationTask(org.apollo.game.sync.task.PostNpcSynchronizationTask) PostPlayerSynchronizationTask(org.apollo.game.sync.task.PostPlayerSynchronizationTask) PreNpcSynchronizationTask(org.apollo.game.sync.task.PreNpcSynchronizationTask) RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) NpcSynchronizationTask(org.apollo.game.sync.task.NpcSynchronizationTask) PostNpcSynchronizationTask(org.apollo.game.sync.task.PostNpcSynchronizationTask) PreNpcSynchronizationTask(org.apollo.game.sync.task.PreNpcSynchronizationTask) PreNpcSynchronizationTask(org.apollo.game.sync.task.PreNpcSynchronizationTask) PostPlayerSynchronizationTask(org.apollo.game.sync.task.PostPlayerSynchronizationTask) PostNpcSynchronizationTask(org.apollo.game.sync.task.PostNpcSynchronizationTask)

Example 3 with RegionCoordinates

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

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

the class StaticGameObject method viewableBy.

@Override
public boolean viewableBy(Player player, World world) {
    RegionRepository repository = world.getRegionRepository();
    RegionCoordinates coordinates = position.getRegionCoordinates();
    return repository.get(coordinates).contains(this);
}
Also used : RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) RegionRepository(org.apollo.game.model.area.RegionRepository)

Example 5 with RegionCoordinates

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

the class UpdateOperation method getPositionOffset.

/**
 * Gets the position offset for the specified {@link Position}.
 *
 * @param position The Position.
 * @return The position offset.
 */
private final int getPositionOffset(Position position) {
    RegionCoordinates coordinates = region.getCoordinates();
    int dx = position.getX() - coordinates.getAbsoluteX();
    int dy = position.getY() - coordinates.getAbsoluteY();
    Preconditions.checkArgument(dx >= 0 && dx < Region.SIZE, position + " not in expected Region of " + region + ".");
    Preconditions.checkArgument(dy >= 0 && dy < Region.SIZE, position + " not in expected Region of " + region + ".");
    return dx << 4 | dy;
}
Also used : RegionCoordinates(org.apollo.game.model.area.RegionCoordinates)

Aggregations

RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)8 RegionRepository (org.apollo.game.model.area.RegionRepository)5 Position (org.apollo.game.model.Position)3 Npc (org.apollo.game.model.entity.Npc)3 Player (org.apollo.game.model.entity.Player)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 Region (org.apollo.game.model.area.Region)2 MovementSegment (org.apollo.game.sync.seg.MovementSegment)2 RemoveMobSegment (org.apollo.game.sync.seg.RemoveMobSegment)2 SynchronizationSegment (org.apollo.game.sync.seg.SynchronizationSegment)2 NpcSynchronizationTask (org.apollo.game.sync.task.NpcSynchronizationTask)2 PlayerSynchronizationTask (org.apollo.game.sync.task.PlayerSynchronizationTask)2 PostNpcSynchronizationTask (org.apollo.game.sync.task.PostNpcSynchronizationTask)2 PostPlayerSynchronizationTask (org.apollo.game.sync.task.PostPlayerSynchronizationTask)2 PreNpcSynchronizationTask (org.apollo.game.sync.task.PreNpcSynchronizationTask)2 PrePlayerSynchronizationTask (org.apollo.game.sync.task.PrePlayerSynchronizationTask)2 SynchronizationTask (org.apollo.game.sync.task.SynchronizationTask)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1