Search in sources :

Example 16 with Region

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

the class CollisionManager method apply.

/**
 * Apply a {@link CollisionUpdate} to the game world.
 *
 * @param update The update to apply.
 */
public void apply(CollisionUpdate update) {
    Region prev = null;
    CollisionUpdateType type = update.getType();
    Map<Position, Collection<DirectionFlag>> map = update.getFlags().asMap();
    for (Map.Entry<Position, Collection<DirectionFlag>> entry : map.entrySet()) {
        Position position = entry.getKey();
        int height = position.getHeight();
        if (bridges.contains(new Position(position.getX(), position.getY(), 1))) {
            if (--height < 0) {
                continue;
            }
        }
        if (prev == null || !prev.contains(position)) {
            prev = regions.fromPosition(position);
        }
        int localX = position.getX() % Region.SIZE;
        int localY = position.getY() % Region.SIZE;
        CollisionMatrix matrix = prev.getMatrix(height);
        CollisionFlag[] mobs = CollisionFlag.mobs();
        CollisionFlag[] projectiles = CollisionFlag.projectiles();
        for (DirectionFlag flag : entry.getValue()) {
            Direction direction = flag.getDirection();
            if (direction == Direction.NONE) {
                continue;
            }
            int orientation = direction.toInteger();
            if (flag.isImpenetrable()) {
                flag(type, matrix, localX, localY, projectiles[orientation]);
            }
            flag(type, matrix, localX, localY, mobs[orientation]);
        }
    }
}
Also used : Position(org.apollo.game.model.Position) DirectionFlag(org.apollo.game.model.area.collision.CollisionUpdate.DirectionFlag) Direction(org.apollo.game.model.Direction) Region(org.apollo.game.model.area.Region) Collection(java.util.Collection) Map(java.util.Map)

Example 17 with Region

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

the class ObjectActionVerificationHandlerTests method terminateIfOutOfRange.

@Test
public void terminateIfOutOfRange() throws Exception {
    Position playerPosition = new Position(3200, 3200);
    Position objectPosition = new Position(3200, 3216);
    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<>();
    entitySet.add(new StaticGameObject(world, 4151, objectPosition, 0, 0));
    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 out of range!", 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) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) World(org.apollo.game.model.World) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 18 with Region

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

the class CollisionManagerTests method createCollisionManager.

/**
 * Sets up dependencies for and creates a stub {@link CollisionManager}, then builds the collision matrices
 * using the {@code objects} given.
 *
 * @param objects The objects to build collision matrices from.
 * @return A new {@link CollisionManager} with a valid {@link RegionRepository} and every {@link CollisionMatrix}
 * built.
 */
private static CollisionManager createCollisionManager(MapObject... objects) {
    World world = new World();
    RegionRepository regions = world.getRegionRepository();
    CollisionManager collisionManager = world.getCollisionManager();
    for (MapObject object : objects) {
        // treat local coordinates as absolute for simplicity
        int x = object.getLocalX(), y = object.getLocalY();
        int height = object.getHeight();
        Position position = new Position(x, y, height);
        Region region = regions.fromPosition(position);
        region.addEntity(new StaticGameObject(world, object.getId(), position, object.getType(), object.getOrientation()), false);
    }
    collisionManager.build(false);
    return collisionManager;
}
Also used : Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) World(org.apollo.game.model.World) MapObject(org.apollo.cache.map.MapObject)

Example 19 with Region

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

the class NpcSynchronizationTask method run.

@Override
public void run() {
    List<Npc> locals = player.getLocalNpcList();
    List<SynchronizationSegment> segments = new ArrayList<>();
    int originalCount = locals.size();
    final Position playerPosition = player.getPosition();
    int distance = player.getViewingDistance();
    for (Iterator<Npc> iterator = locals.iterator(); iterator.hasNext(); ) {
        Npc npc = iterator.next();
        Position position = npc.getPosition();
        if (!npc.isActive() || npc.isTeleporting() || position.getLongestDelta(playerPosition) > distance || !position.isWithinDistance(playerPosition, distance)) {
            iterator.remove();
            segments.add(new RemoveMobSegment());
        } else {
            segments.add(new MovementSegment(npc.getBlockSet(), npc.getDirections()));
        }
    }
    int added = 0, count = locals.size();
    RegionRepository repository = player.getWorld().getRegionRepository();
    Region current = repository.fromPosition(playerPosition);
    Set<RegionCoordinates> regions = current.getSurrounding();
    regions.add(current.getCoordinates());
    Stream<Npc> npcs = regions.stream().map(repository::get).flatMap(region -> region.getEntities(EntityType.NPC));
    Iterator<Npc> iterator = npcs.iterator();
    while (iterator.hasNext()) {
        if (count >= MAXIMUM_LOCAL_NPCS) {
            player.flagExcessiveNpcs();
            break;
        } else if (added >= NEW_NPCS_PER_CYCLE) {
            break;
        }
        Npc npc = iterator.next();
        Position position = npc.getPosition();
        if (position.isWithinDistance(playerPosition, distance) && !locals.contains(npc)) {
            locals.add(npc);
            count++;
            added++;
            npc.turnTo(npc.getFacingPosition());
            segments.add(new AddNpcSegment(npc.getBlockSet(), npc.getIndex(), position, npc.getId()));
        }
    }
    player.send(new NpcSynchronizationMessage(playerPosition, segments, originalCount));
}
Also used : Npc(org.apollo.game.model.entity.Npc) Position(org.apollo.game.model.Position) ArrayList(java.util.ArrayList) NpcSynchronizationMessage(org.apollo.game.message.impl.NpcSynchronizationMessage) RemoveMobSegment(org.apollo.game.sync.seg.RemoveMobSegment) MovementSegment(org.apollo.game.sync.seg.MovementSegment) RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) AddNpcSegment(org.apollo.game.sync.seg.AddNpcSegment) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region) SynchronizationSegment(org.apollo.game.sync.seg.SynchronizationSegment)

Aggregations

Region (org.apollo.game.model.area.Region)19 Position (org.apollo.game.model.Position)14 RegionRepository (org.apollo.game.model.area.RegionRepository)9 World (org.apollo.game.model.World)6 Player (org.apollo.game.model.entity.Player)6 HashSet (java.util.HashSet)5 Entity (org.apollo.game.model.entity.Entity)5 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Inventory (org.apollo.game.model.inv.Inventory)4 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 Npc (org.apollo.game.model.entity.Npc)3 GameObject (org.apollo.game.model.entity.obj.GameObject)3 ArrayList (java.util.ArrayList)2 ObjectActionMessage (org.apollo.game.message.impl.ObjectActionMessage)2 Direction (org.apollo.game.model.Direction)2 Item (org.apollo.game.model.Item)2 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)2 MovementSegment (org.apollo.game.sync.seg.MovementSegment)2