Search in sources :

Example 1 with Npc

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

the class NpcMovementTask method execute.

@Override
public void execute() {
    int count = RANDOM.nextInt(npcs.size() / 50 + 5);
    for (int iterations = 0; iterations < count; iterations++) {
        Npc npc = npcs.poll();
        if (npc == null) {
            break;
        }
        Position[] boundary = npc.getBoundaries().get();
        Position current = npc.getPosition();
        Position min = boundary[0], max = boundary[1];
        int currentX = current.getX(), currentY = current.getY();
        boolean negativeX = RANDOM.nextBoolean(), negativeY = RANDOM.nextBoolean();
        int x = RANDOM.nextInt(negativeX ? currentX - min.getX() : max.getX() - currentX);
        int y = RANDOM.nextInt(negativeY ? currentY - min.getY() : max.getY() - currentY);
        int dx = negativeX ? -x : x;
        int dy = negativeY ? -y : y;
        Position next = new Position(currentX + dx, currentY + dy);
        Deque<Position> positions = algorithm.find(current, next, boundary);
        WalkingQueue queue = npc.getWalkingQueue();
        Position first = positions.pollFirst();
        if (first != null) {
            queue.addFirstStep(first);
            positions.forEach(queue::addStep);
        }
        npcs.offer(npc);
    }
}
Also used : Npc(org.apollo.game.model.entity.Npc) Position(org.apollo.game.model.Position) WalkingQueue(org.apollo.game.model.entity.WalkingQueue)

Example 2 with Npc

use of org.apollo.game.model.entity.Npc 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 Npc

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

the class World method unregisterNpcs.

/**
 * Unregisters all of the {@link Npc}s in the {@link #oldNpcs queue}.
 */
private void unregisterNpcs() {
    while (!oldNpcs.isEmpty()) {
        Npc npc = oldNpcs.poll();
        Region region = regions.fromPosition(npc.getPosition());
        region.removeEntity(npc);
        npcRepository.remove(npc);
    }
}
Also used : Npc(org.apollo.game.model.entity.Npc) Region(org.apollo.game.model.area.Region)

Example 4 with Npc

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

the class NpcActionVerificationHandler method handle.

@Override
public void handle(Player player, NpcActionMessage message) {
    int index = message.getIndex();
    MobRepository<Npc> repository = world.getNpcRepository();
    if (index < 0 || index >= repository.capacity()) {
        message.terminate();
        return;
    }
    Npc npc = repository.get(index);
    if (npc == null || !player.getPosition().isWithinDistance(npc.getPosition(), player.getViewingDistance() + 1)) {
        // +1 in case it was decremented after the player clicked the action.
        message.terminate();
        return;
    }
    NpcDefinition definition = npc.getDefinition();
    String[] actions = definition.getInteractions();
    int option = message.getOption();
    if (option < 0 || option >= actions.length) {
        message.terminate();
        return;
    }
    if ("null".equals(actions[option])) {
        message.terminate();
        return;
    }
}
Also used : Npc(org.apollo.game.model.entity.Npc) NpcDefinition(org.apollo.cache.def.NpcDefinition)

Example 5 with Npc

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

the class World method registerNpcs.

/**
 * Registers all of the {@link Npc}s in the {@link #queuedNpcs queue}.
 */
private void registerNpcs() {
    while (!queuedNpcs.isEmpty()) {
        Npc npc = queuedNpcs.poll();
        boolean success = npcRepository.add(npc);
        if (success) {
            Region region = regions.fromPosition(npc.getPosition());
            region.addEntity(npc);
            if (npc.hasBoundaries()) {
                npcMovement.addNpc(npc);
            }
        } else {
            logger.warning("Failed to register npc (capacity reached): [count=" + npcRepository.size() + "]");
        }
    }
}
Also used : Npc(org.apollo.game.model.entity.Npc) Region(org.apollo.game.model.area.Region)

Aggregations

Npc (org.apollo.game.model.entity.Npc)7 Region (org.apollo.game.model.area.Region)3 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)3 Set (java.util.Set)2 Position (org.apollo.game.model.Position)2 Player (org.apollo.game.model.entity.Player)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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 NpcDefinition (org.apollo.cache.def.NpcDefinition)1 NpcSynchronizationMessage (org.apollo.game.message.impl.NpcSynchronizationMessage)1 RegionRepository (org.apollo.game.model.area.RegionRepository)1 WalkingQueue (org.apollo.game.model.entity.WalkingQueue)1