1package com.osroyale.game.world.region;
3import com.google.common.base.Preconditions;
4import com.osroyale.game.world.entity.mob.Mob;
5import com.osroyale.game.world.entity.mob.npc.Npc;
6import com.osroyale.game.world.entity.mob.player.Player;
7import com.osroyale.game.world.position.Position;
8import com.osroyale.util.Utility;
9import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
10import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
13import java.util.function.Consumer;
60 private final Int2ObjectMap<Region> activeRegions =
new Int2ObjectOpenHashMap<>();
69 mob.getRegion().getSurroundingRegions().ifPresent(regions -> {
70 for (
Region region : regions) {
72 if (nearby ==
null)
continue;
73 if (
Utility.getDistance(nearby, mob) > distance)
continue;
74 if (nearby.getCurrentHealth() <= 0 || nearby.isDead())
continue;
75 action.accept(nearby);
88 position.getRegion().getSurroundingRegions().ifPresent(regions -> {
89 for (
Region region : regions) {
90 for (
Player other : region.getPlayers(position.getHeight())) {
91 if (other ==
null)
continue;
92 if (
Utility.getDistance(other, position) > distance)
continue;
93 if (other.getCurrentHealth() <= 0 || other.isDead())
continue;
107 Preconditions.checkArgument(mob !=
null,
"mob is null");
108 List<Player> localPlayers =
new LinkedList<>();
112 localPlayers.add(player);
126 List<Npc> localNpcs =
new LinkedList<>();
146 if (target.getSurroundingRegions().isPresent()) {
147 return target.getSurroundingRegions().get();
150 Set<Region> surrounding =
new HashSet<>();
151 int chunkX = position.getChunkX();
152 int chunkY = position.getChunkY();
154 for (
int y = -1; y <= 1; y++) {
155 for (
int x = -1; x <= 1; x++) {
156 int xx = (chunkX + x * 8 + 6) << 3;
157 int yy = (chunkY + y * 8 + 6) << 3;
160 if (!surrounding.contains(region)) {
161 surrounding.add(region);
166 Region[] x = surrounding.toArray(
new Region[surrounding.size()]);
167 target.setSurroundingRegions(Optional.of(x));
168 return target.getSurroundingRegions().get();
178 return getRegion(position.getX(), position.getY());
189 final int hash = hash(x, y);
191 final Region region = activeRegions.get(hash);
192 if (region !=
null) {
197 activeRegions.put(hash, newRegion);
201 private static int hash(
int x,
int y) {
202 return ((x >> 6) << 8) + (y >> 6);
boolean isWithinDistance(Position other, int radius)
List< Npc > getLocalNpcs(Mob mob)
Region getRegion(final int x, final int y)
static void forNearbyPlayer(Position position, int distance, Consumer< Player > action)
static void forNearbyPlayer(Mob mob, int distance, Consumer< Player > action)
Region[] getSurroundingRegions(Position position)
List< Player > getLocalPlayers(Mob mob)
Region getRegion(Position position)