RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Region.java
1package com.osroyale.game.world.region;
2
3import com.osroyale.game.world.Interactable;
4import com.osroyale.game.world.entity.mob.npc.Npc;
5import com.osroyale.game.world.entity.mob.player.Player;
6import com.osroyale.game.world.items.ground.GroundItem;
7import com.osroyale.game.world.object.GameObject;
8import com.osroyale.game.world.pathfinding.TraversalMap;
9import com.osroyale.game.world.position.Position;
10import com.osroyale.util.Utility;
11
12import java.util.*;
13
14import static com.osroyale.game.world.position.Position.HEIGHT_LEVELS;
15
57
58public class Region {
59 private static final int CHUNK_SIZE = 8;
60 public static final int SIZE = CHUNK_SIZE * 8;
61 public static final int VIEW_DISTANCE = SIZE / 4 - 1;
62 public static final Map<Position, GameObject> ACTIVE_OBJECT = new HashMap<>();
63 public static final Set<Position> SKIPPED_OBJECTS = new HashSet<>();
64
66 private int id;
67
69 private int x, y;
70
72 private final RegionBlock[] blocks = new RegionBlock[HEIGHT_LEVELS];
73
75 private Optional<Region[]> surroundingRegions = Optional.empty();
76
78 public Region(int x, int y) {
79 this.x = x;
80 this.y = y;
81 this.id = ((x >> 6) << 8) + (y >> 6);
82 }
83
85 public int getX() {
86 return x;
87 }
88
90 public int getY() {
91 return y;
92 }
93
103 public int getFlags(int height, int x, int y) {
104 return getBlock(height).getFlags(x, y);
105 }
106
116 public void setFlags(int height, int x, int y, int flags) {
117 getBlock(height).setFlags(x, y, flags);
118 }
119
129 public void unsetFlags(int height, int x, int y, int flags) {
130 getBlock(height).unsetFlags(x, y, flags);
131 }
132
133 public Optional<Region[]> getSurroundingRegions() {
134 return surroundingRegions;
135 }
136
137 public void setSurroundingRegions(Optional<Region[]> surroundingRegions) {
138 this.surroundingRegions = surroundingRegions;
139 }
140
142 public Collection<Player> getPlayers(int height) {
143 return getBlock(height).getPlayers();
144 }
145
147 public Collection<Npc> getNpcs(int height) {
148 return getBlock(height).getNpcs();
149 }
150
152 public GroundItem getGroundItem(int id, Position position) {
153 return getBlock(position.getHeight()).getGroundItem(id, position);
154 }
155
157 public void addPlayer(Player player) {
158 getBlock(player.getHeight()).addPlayer(player);
159 }
160
162 public void removePlayer(Player player) {
163 getBlock(player.getHeight()).removePlayer(player);
164 }
165
167 public void addNpc(Npc npc) {
168 getBlock(npc.getHeight()).addNpc(npc);
169 }
170
172 public void removeNpc(Npc npc) {
173 getBlock(npc.getHeight()).removeNpc(npc);
174 }
175
177 public void addObject(GameObject object) {
178 getBlock(object.getHeight()).addObject(object);
179 }
180
182 public void removeObject(GameObject object) {
183 getBlock(object.getHeight()).removeObject(object);
184 }
185
187 public void addGroundItem(GroundItem item) {
188 getBlock(item.getHeight()).addGroundItem(item);
189 }
190
192 public void removeGroundItem(GroundItem item) {
193 getBlock(item.getHeight()).removeGroundItem(item);
194 }
195
196 public boolean containsNpc(int height, Npc npc) {
197 return getBlock(height).containsNpc(npc);
198 }
199
200 public boolean containsPlayer(int height, Player player) {
201 return getBlock(height).containsPlayer(player);
202 }
203
205 public boolean containsObject(int height, GameObject object) {
206 return getBlock(height).containsObject(object);
207 }
208
210 public boolean containsObject(Position position) {
211 return getBlock(position.getHeight()).containsObject(position);
212 }
213
215 public Set<GroundItem> getGroundItems(Position position) {
216 return getBlock(position.getHeight()).getGroundItems(position);
217 }
218
220 public GameObject getGameObject(int id, Position position) {
221 return getBlock(position.getHeight()).getGameObject(id, position);
222 }
223
224 public GameObject getCustomObject(int id, Position position) {
225 return getBlock(position.getHeight()).getCustomObject(id, position);
226 }
227
228 public List<GameObject> getObjects(Position position) {
229 return getBlock(position.getHeight()).getGameObjects(position);
230 }
231
233 public void sendGroundItems(Player player) {
234 getBlock(player.getHeight()).sendGroundItems(player);
235 }
236
238 public void sendGameObjects(Player player) {
239 getBlock(player.getHeight()).sendGameObjects(player);
240 }
241
243 public boolean contains(Position position) {
244 return getX() == position.getRegionX() && getY() == position.getRegionY();
245 }
246
248 public boolean contains(int x, int y) {
249 return getX() == ((x >> 3) - 6) >> 3 && getY() == ((y >> 3) - 6) >> 3;
250 }
251
252 private RegionBlock getBlock(int height) {
253 if (height > HEIGHT_LEVELS || height < 0) {
254 throw new IllegalArgumentException("Height is out of bounds. Received (" + height + "), expected range [0, " + HEIGHT_LEVELS + "].");
255 }
256
257 if (blocks[height] == null) {
258 blocks[height] = new RegionBlock();
259 }
260
261 return blocks[height];
262 }
263
264 public static boolean reachable(Interactable source, Interactable target) {
265 return reachable(source.getPosition(), source.width(), source.length(), target.getPosition(), target.width(), target.length());
266 }
267
268 public static boolean reachable(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength) {
269 if (Utility.inside(source, sourceWidth, sourceLength, target, targetWidth, targetLength)) {
270 return targetWidth == 0 || targetLength == 0;
271 }
272
273 int x, y;
274 int sourceTopX = source.getX() + sourceWidth - 1;
275 int sourceTopY = source.getY() + sourceLength - 1;
276 int targetTopX = target.getX() + targetWidth - 1;
277 int targetTopY = target.getY() + targetLength - 1;
278
279 if (sourceTopY == target.getY() - 1 && source.getX() >= target.getX() && source.getX() <= targetTopX) {
280 for (x = 0, y = sourceLength - 1; x < sourceWidth; x++)
281 if (TraversalMap.blockedNorth(source.transform(x, y)))
282 return false;
283 return true;
284 }
285
286 if (sourceTopX == target.getX() - 1 && source.getY() >= target.getY() && source.getY() <= targetTopY) {
287 for (x = 0, y = 0; y < sourceLength; y++)
288 if (TraversalMap.blockedEast(source.transform(x, y)))
289 return false;
290 return true;
291 }
292
293 if (source.getY() == targetTopY + 1 && source.getX() >= target.getX() && source.getX() <= targetTopX) {
294 for (x = 0, y = 0; x < sourceWidth; x++)
295 if (TraversalMap.blockedSouth(source.transform(x, y)))
296 return false;
297 return true;
298 }
299
300 if (source.getX() == targetTopX + 1 && source.getY() >= target.getY() && source.getY() <= targetTopY) {
301 for (x = sourceWidth - 1, y = 0; y < sourceLength; y++)
302 if (TraversalMap.blockedWest(source.transform(x, y)))
303 return false;
304 return true;
305 }
306
307 return false;
308 }
309
310 public void skip(GameObject gameObject) {
311 getBlock(gameObject.getHeight()).skip(gameObject);
312 }
313
314 @Override
315 public String toString() {
316 return "Region[" + x + "_" + y + "]";
317 }
318
319 public int getId() {
320 return id;
321 }
322
323}
void addGroundItem(GroundItem item)
Definition Region.java:187
void removeGroundItem(GroundItem item)
Definition Region.java:192
GroundItem getGroundItem(int id, Position position)
Definition Region.java:152
GameObject getGameObject(int id, Position position)
Definition Region.java:220
void addObject(GameObject object)
Definition Region.java:177
int getFlags(int height, int x, int y)
Definition Region.java:103
boolean containsObject(Position position)
Definition Region.java:210
void setFlags(int height, int x, int y, int flags)
Definition Region.java:116
boolean contains(Position position)
Definition Region.java:243
Set< GroundItem > getGroundItems(Position position)
Definition Region.java:215
boolean containsObject(int height, GameObject object)
Definition Region.java:205
boolean contains(int x, int y)
Definition Region.java:248
Collection< Npc > getNpcs(int height)
Definition Region.java:147
void unsetFlags(int height, int x, int y, int flags)
Definition Region.java:129
void removeObject(GameObject object)
Definition Region.java:182
void sendGameObjects(Player player)
Definition Region.java:238
void sendGroundItems(Player player)
Definition Region.java:233
Collection< Player > getPlayers(int height)
Definition Region.java:142