RuneHive-Game
Loading...
Searching...
No Matches
Region.java
Go to the documentation of this file.
1package com.runehive.game.world.region;
2
3import com.runehive.game.world.Interactable;
4import com.runehive.game.world.entity.mob.npc.Npc;
5import com.runehive.game.world.entity.mob.player.Player;
6import com.runehive.game.world.items.ground.GroundItem;
7import com.runehive.game.world.object.GameObject;
8import com.runehive.game.world.pathfinding.TraversalMap;
9import com.runehive.game.world.position.Position;
10import com.runehive.util.Utility;
11
12import java.util.*;
13
14import static com.runehive.game.world.position.Position.HEIGHT_LEVELS;
15
16/**
17 * Represents a single region.
18 *
19 * @author Graham Edgecombe
20 */
21public class Region {
22 private static final int CHUNK_SIZE = 8;
23 public static final int SIZE = CHUNK_SIZE * 8;
24 public static final int VIEW_DISTANCE = SIZE / 4 - 1;
25 public static final Map<Position, GameObject> ACTIVE_OBJECT = new HashMap<>();
26 public static final Set<Position> SKIPPED_OBJECTS = new HashSet<>();
27
28 /** The region id. */
29 private int id;
30
31 /** The region coordinates. */
32 private int x, y;
33
34 /** The region blocks that execute different heights. */
35 private final RegionBlock[] blocks = new RegionBlock[HEIGHT_LEVELS];
36
37 /** The surrounded regions. */
38 private Optional<Region[]> surroundingRegions = Optional.empty();
39
40 /** Creates a region. */
41 public Region(int x, int y) {
42 this.x = x;
43 this.y = y;
44 this.id = ((x >> 6) << 8) + (y >> 6);
45 }
46
47 /** @return region x-coordinate */
48 public int getX() {
49 return x;
50 }
51
52 /** @return region y-coordinate */
53 public int getY() {
54 return y;
55 }
56
57 /**
58 * Gets a single tile in this region from the specified height, x and y
59 * coordinates.
60 *
61 * @param height The height.
62 * @param x The x coordinate.
63 * @param y The y coordinate.
64 * @return The tile in this region for the specified attributes.
65 */
66 public int getFlags(int height, int x, int y) {
67 return getBlock(height).getFlags(x, y);
68 }
69
70 /**
71 * Gets a single tile in this region from the specified height, x and y
72 * coordinates.
73 *
74 * @param height The height.
75 * @param x The x coordinate.
76 * @param y The y coordinate.
77 * @return The tile in this region for the specified attributes.
78 */
79 public void setFlags(int height, int x, int y, int flags) {
80 getBlock(height).setFlags(x, y, flags);
81 }
82
83 /**
84 * Gets a single tile in this region from the specified height, x and y
85 * coordinates.
86 *
87 * @param height The height.
88 * @param x The x coordinate.
89 * @param y The y coordinate.
90 * @return The tile in this region for the specified attributes.
91 */
92 public void unsetFlags(int height, int x, int y, int flags) {
93 getBlock(height).unsetFlags(x, y, flags);
94 }
95
96 public Optional<Region[]> getSurroundingRegions() {
97 return surroundingRegions;
98 }
99
101 this.surroundingRegions = surroundingRegions;
102 }
103
104 /** @return the players in this region */
105 public Collection<Player> getPlayers(int height) {
106 return getBlock(height).getPlayers();
107 }
108
109 /** @return the npcs in this region */
110 public Collection<Npc> getNpcs(int height) {
111 return getBlock(height).getNpcs();
112 }
113
114 /** @return the ground item with the given id at the given position */
116 return getBlock(position.getHeight()).getGroundItem(id, position);
117 }
118
119 /** Adds a player to this region. */
120 public void addPlayer(Player player) {
121 getBlock(player.getHeight()).addPlayer(player);
122 }
123
124 /** Removes a player from this region. */
125 public void removePlayer(Player player) {
126 getBlock(player.getHeight()).removePlayer(player);
127 }
128
129 /** Adds an npc to this region. */
130 public void addNpc(Npc npc) {
131 getBlock(npc.getHeight()).addNpc(npc);
132 }
133
134 /** Removes an npc from this region. */
135 public void removeNpc(Npc npc) {
136 getBlock(npc.getHeight()).removeNpc(npc);
137 }
138
139 /** Adds an object to this region. */
140 public void addObject(GameObject object) {
141 getBlock(object.getHeight()).addObject(object);
142 }
143
144 /** Removes an object from this region. */
145 public void removeObject(GameObject object) {
146 getBlock(object.getHeight()).removeObject(object);
147 }
148
149 /** Adds a ground item to this region. */
150 public void addGroundItem(GroundItem item) {
151 getBlock(item.getHeight()).addGroundItem(item);
152 }
153
154 /** Adds a ground item to this region. */
155 public void removeGroundItem(GroundItem item) {
156 getBlock(item.getHeight()).removeGroundItem(item);
157 }
158
159 public boolean containsNpc(int height, Npc npc) {
160 return getBlock(height).containsNpc(npc);
161 }
162
163 public boolean containsPlayer(int height, Player player) {
164 return getBlock(height).containsPlayer(player);
165 }
166
167 /** @return {@code true} if object is in region */
168 public boolean containsObject(int height, GameObject object) {
169 return getBlock(height).containsObject(object);
170 }
171
172 /** @return {@code true} if object occupies position */
174 return getBlock(position.getHeight()).containsObject(position);
175 }
176
177 /** @return a set of ground items on the position */
178 public Set<GroundItem> getGroundItems(Position position) {
179 return getBlock(position.getHeight()).getGroundItems(position);
180 }
181
182 /** @return {@code true} if object with given id occupies position */
184 return getBlock(position.getHeight()).getGameObject(id, position);
185 }
186
188 return getBlock(position.getHeight()).getCustomObject(id, position);
189 }
190
191 public List<GameObject> getObjects(Position position) {
192 return getBlock(position.getHeight()).getGameObjects(position);
193 }
194
195 /** Sends ground items in this region to the {@code player}. */
196 public void sendGroundItems(Player player) {
197 getBlock(player.getHeight()).sendGroundItems(player);
198 }
199
200 /** Sends game objects in this region to the {@code player}. */
201 public void sendGameObjects(Player player) {
202 getBlock(player.getHeight()).sendGameObjects(player);
203 }
204
205 /** @return {@code true} if the region contains the position */
206 public boolean contains(Position position) {
207 return getX() == position.getRegionX() && getY() == position.getRegionY();
208 }
209
210 /** @return {@code true} if the region contains the position */
211 public boolean contains(int x, int y) {
212 return getX() == ((x >> 3) - 6) >> 3 && getY() == ((y >> 3) - 6) >> 3;
213 }
214
215 private RegionBlock getBlock(int height) {
216 if (height > HEIGHT_LEVELS || height < 0) {
217 throw new IllegalArgumentException("Height is out of bounds. Received (" + height + "), expected range [0, " + HEIGHT_LEVELS + "].");
218 }
219
220 if (blocks[height] == null) {
221 blocks[height] = new RegionBlock();
222 }
223
224 return blocks[height];
225 }
226
227 public static boolean reachable(Interactable source, Interactable target) {
228 return reachable(source.getPosition(), source.width(), source.length(), target.getPosition(), target.width(), target.length());
229 }
230
231 public static boolean reachable(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength) {
232 if (Utility.inside(source, sourceWidth, sourceLength, target, targetWidth, targetLength)) {
233 return targetWidth == 0 || targetLength == 0;
234 }
235
236 int x, y;
237 int sourceTopX = source.getX() + sourceWidth - 1;
238 int sourceTopY = source.getY() + sourceLength - 1;
239 int targetTopX = target.getX() + targetWidth - 1;
240 int targetTopY = target.getY() + targetLength - 1;
241
242 if (sourceTopY == target.getY() - 1 && source.getX() >= target.getX() && source.getX() <= targetTopX) {
243 for (x = 0, y = sourceLength - 1; x < sourceWidth; x++)
244 if (TraversalMap.blockedNorth(source.transform(x, y)))
245 return false;
246 return true;
247 }
248
249 if (sourceTopX == target.getX() - 1 && source.getY() >= target.getY() && source.getY() <= targetTopY) {
250 for (x = 0, y = 0; y < sourceLength; y++)
251 if (TraversalMap.blockedEast(source.transform(x, y)))
252 return false;
253 return true;
254 }
255
256 if (source.getY() == targetTopY + 1 && source.getX() >= target.getX() && source.getX() <= targetTopX) {
257 for (x = 0, y = 0; x < sourceWidth; x++)
258 if (TraversalMap.blockedSouth(source.transform(x, y)))
259 return false;
260 return true;
261 }
262
263 if (source.getX() == targetTopX + 1 && source.getY() >= target.getY() && source.getY() <= targetTopY) {
264 for (x = sourceWidth - 1, y = 0; y < sourceLength; y++)
265 if (TraversalMap.blockedWest(source.transform(x, y)))
266 return false;
267 return true;
268 }
269
270 return false;
271 }
272
273 public void skip(GameObject gameObject) {
274 getBlock(gameObject.getHeight()).skip(gameObject);
275 }
276
277 @Override
278 public String toString() {
279 return "Region[" + x + "_" + y + "]";
280 }
281
282 public int getId() {
283 return id;
284 }
285
286}
Represents a non-player character in the in-game world.
Definition Npc.java:29
This class represents a character controlled by a player.
Definition Player.java:125
Represents a single Ground item on the world map.
Contains traversal data for a set of regions.
static boolean blockedSouth(Position position)
static boolean blockedNorth(Position position)
static boolean blockedEast(Position position)
static boolean blockedWest(Position position)
Represents a single tile on the game world.
Definition Position.java:14
int getY()
Gets the absolute y coordinate.
Definition Position.java:46
int getX()
Gets the absolute x coordinate.
Definition Position.java:41
Position transform(int diffX, int diffY, int diffZ)
Creates a new location based on this location.
void removePlayer(Player player)
Removes a player from this block.
GameObject getCustomObject(int id, Position position)
void addPlayer(Player player)
Adds a player to this block.
void removeGroundItem(GroundItem item)
Adds a ground item to this block.
void removeNpc(Npc npc)
Removes an npc from this block.
void removeObject(GameObject object)
Removes an object from this block.
Set< GroundItem > getGroundItems(Position position)
Gets a Set of GroundItems.
GameObject getGameObject(int id, Position position)
void unsetFlags(int x, int y, int flag)
Gets a single tile in this region from the specified height, x and y coordinates.
List< GameObject > getGameObjects(Position position)
void addObject(GameObject object)
Adds an object to this block.
GroundItem getGroundItem(int id, Position position)
The method that retrieves the item with id on position.
void addNpc(Npc npc)
Adds an npc to this block.
void sendGroundItems(Player player)
The method which handles updating when the specified player enters a new region.
void addGroundItem(GroundItem item)
Adds a ground item to this block.
void setFlags(int x, int y, int flag)
Gets a single tile in this region from the specified height, x and y coordinates.
int getFlags(int x, int y)
Gets a single tile in this region from the specified height, x and y coordinates.
boolean containsObject(GameObject object)
boolean containsObject(int height, GameObject object)
Definition Region.java:168
GameObject getGameObject(int id, Position position)
Definition Region.java:183
void sendGroundItems(Player player)
Sends ground items in this region to the player.
Definition Region.java:196
RegionBlock getBlock(int height)
Definition Region.java:215
void sendGameObjects(Player player)
Sends game objects in this region to the player.
Definition Region.java:201
final RegionBlock[] blocks
The region blocks that execute different heights.
Definition Region.java:35
boolean contains(int x, int y)
Definition Region.java:211
Set< GroundItem > getGroundItems(Position position)
Definition Region.java:178
boolean containsNpc(int height, Npc npc)
Definition Region.java:159
List< GameObject > getObjects(Position position)
Definition Region.java:191
void addObject(GameObject object)
Adds an object to this region.
Definition Region.java:140
void removeObject(GameObject object)
Removes an object from this region.
Definition Region.java:145
void unsetFlags(int height, int x, int y, int flags)
Gets a single tile in this region from the specified height, x and y coordinates.
Definition Region.java:92
GameObject getCustomObject(int id, Position position)
Definition Region.java:187
Optional< Region[]> surroundingRegions
The surrounded regions.
Definition Region.java:38
Collection< Npc > getNpcs(int height)
Definition Region.java:110
void setFlags(int height, int x, int y, int flags)
Gets a single tile in this region from the specified height, x and y coordinates.
Definition Region.java:79
Collection< Player > getPlayers(int height)
Definition Region.java:105
boolean contains(Position position)
Definition Region.java:206
int getFlags(int height, int x, int y)
Gets a single tile in this region from the specified height, x and y coordinates.
Definition Region.java:66
boolean containsPlayer(int height, Player player)
Definition Region.java:163
int x
The region coordinates.
Definition Region.java:32
void removeGroundItem(GroundItem item)
Adds a ground item to this region.
Definition Region.java:155
void addGroundItem(GroundItem item)
Adds a ground item to this region.
Definition Region.java:150
static final Map< Position, GameObject > ACTIVE_OBJECT
Definition Region.java:25
static final Set< Position > SKIPPED_OBJECTS
Definition Region.java:26
void addNpc(Npc npc)
Adds an npc to this region.
Definition Region.java:130
static boolean reachable(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength)
Definition Region.java:231
static boolean reachable(Interactable source, Interactable target)
Definition Region.java:227
void removeNpc(Npc npc)
Removes an npc from this region.
Definition Region.java:135
void setSurroundingRegions(Optional< Region[]> surroundingRegions)
Definition Region.java:100
Region(int x, int y)
Creates a region.
Definition Region.java:41
void skip(GameObject gameObject)
Definition Region.java:273
boolean containsObject(Position position)
Definition Region.java:173
void addPlayer(Player player)
Adds a player to this region.
Definition Region.java:120
GroundItem getGroundItem(int id, Position position)
Definition Region.java:115
Optional< Region[]> getSurroundingRegions()
Definition Region.java:96
void removePlayer(Player player)
Removes a player from this region.
Definition Region.java:125
Handles miscellaneous methods.
Definition Utility.java:27
static boolean inside(Interactable source, Interactable target)
Definition Utility.java:783
An object implementing Interactable has uses.