RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
RegionManager.java
1package com.osroyale.game.world.region;
2
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;
11
12import java.util.*;
13import java.util.function.Consumer;
14
56
57public class RegionManager {
58
60 private final Int2ObjectMap<Region> activeRegions = new Int2ObjectOpenHashMap<>();
61
68 public static void forNearbyPlayer(Mob mob, int distance, Consumer<Player> action) {
69 mob.getRegion().getSurroundingRegions().ifPresent(regions -> {
70 for (Region region : regions) {
71 for (Player nearby : region.getPlayers(mob.getHeight())) {
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);
76 }
77 }
78 });
79 }
80
87 public static void forNearbyPlayer(Position position, int distance, Consumer<Player> action) {
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;
94 action.accept(other);
95 }
96 }
97 });
98 }
99
106 public List<Player> getLocalPlayers(Mob mob) {
107 Preconditions.checkArgument(mob != null, "mob is null");
108 List<Player> localPlayers = new LinkedList<>();
109 for (Region region : getSurroundingRegions(mob.getPosition())) {
110 for (Player player : region.getPlayers(mob.getHeight())) {
111 if (mob.getPosition().isWithinDistance(player.getPosition(), Region.VIEW_DISTANCE)) {
112 localPlayers.add(player);
113 }
114 }
115 }
116 return localPlayers;
117 }
118
125 public List<Npc> getLocalNpcs(Mob mob) {
126 List<Npc> localNpcs = new LinkedList<>();
127 for (Region region : getSurroundingRegions(mob.getPosition())) {
128 for (Npc npc : region.getNpcs(mob.getHeight())) {
129 if (mob.getPosition().isWithinDistance(npc.getPosition(), Region.VIEW_DISTANCE)) {
130 localNpcs.add(npc);
131 }
132 }
133 }
134 return localNpcs;
135 }
136
144 Region target = getRegion(position.getX(), position.getY());
145
146 if (target.getSurroundingRegions().isPresent()) {
147 return target.getSurroundingRegions().get();
148 }
149
150 Set<Region> surrounding = new HashSet<>();
151 int chunkX = position.getChunkX();
152 int chunkY = position.getChunkY();
153
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;
158 Region region = getRegion(xx, yy);
159
160 if (!surrounding.contains(region)) {
161 surrounding.add(region);
162 }
163 }
164 }
165
166 Region[] x = surrounding.toArray(new Region[surrounding.size()]);
167 target.setSurroundingRegions(Optional.of(x));
168 return target.getSurroundingRegions().get();
169 }
170
177 public Region getRegion(Position position) {
178 return getRegion(position.getX(), position.getY());
179 }
180
188 public Region getRegion(final int x, final int y) {
189 final int hash = hash(x, y);
190
191 final Region region = activeRegions.get(hash);
192 if (region != null) {
193 return region;
194 }
195
196 final Region newRegion = new Region(x, y);
197 activeRegions.put(hash, newRegion);
198 return newRegion;
199 }
200
201 private static int hash(int x, int y) {
202 return ((x >> 6) << 8) + (y >> 6);
203 }
204
205}
boolean isWithinDistance(Position other, int radius)
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)