use of org.apollo.game.model.area.Region in project apollo by apollo-rsps.
the class ObjectActionVerificationHandler method handle.
@Override
public void handle(Player player, ObjectActionMessage message) {
int id = message.getId();
if (id < 0 || id >= ObjectDefinition.count()) {
message.terminate();
return;
}
Position position = message.getPosition();
Region region = world.getRegionRepository().fromPosition(position);
Set<GameObject> objects = region.getEntities(position, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT);
if (!player.getPosition().isWithinDistance(position, 15) || !containsObject(id, objects)) {
message.terminate();
return;
}
ObjectDefinition definition = ObjectDefinition.lookup(id);
if (message.getOption() >= definition.getMenuActions().length) {
message.terminate();
return;
}
}
use of org.apollo.game.model.area.Region 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());
}
use of org.apollo.game.model.area.Region 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);
}
use of org.apollo.game.model.area.Region 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.area.Region in project apollo by apollo-rsps.
the class World method spawn.
/**
* Spawns the specified {@link Entity}, which must not be a {@link Player}
* or an {@link Npc}, which have their own register methods.
*
* @param entity The Entity.
*/
public void spawn(Entity entity) {
EntityType type = entity.getEntityType();
Preconditions.checkArgument(type != EntityType.PLAYER && type != EntityType.NPC, "Cannot spawn a Mob.");
Region region = regions.fromPosition(entity.getPosition());
region.addEntity(entity);
}
Aggregations