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