RuneHive-Game
Loading...
Searching...
No Matches
RegionBlock.java
Go to the documentation of this file.
1package com.runehive.game.world.region;
2
3import com.runehive.game.world.entity.mob.npc.Npc;
4import com.runehive.game.world.entity.mob.player.Player;
5import com.runehive.game.world.items.ground.GroundItem;
6import com.runehive.game.world.object.CustomGameObject;
7import com.runehive.game.world.object.GameObject;
8import com.runehive.game.world.position.Position;
9import com.runehive.net.packet.out.SendAddObject;
10import com.runehive.net.packet.out.SendGroundItem;
11import com.runehive.net.packet.out.SendRemoveObject;
12
13import java.util.*;
14import java.util.concurrent.ConcurrentHashMap;
15import java.util.concurrent.ConcurrentLinkedDeque;
16
17import static com.runehive.game.world.region.Region.SIZE;
18
19/**
20 * Represents a single region.
21 *
22 * @author Graham Edgecombe
23 */
24public class RegionBlock {
25
26 /** The clipping flags within the region. */
27 private int[] flags;
28
29 /** A list of players in this region. */
30 private Deque<Player> players = new ConcurrentLinkedDeque<>();
31
32 /** A list of npcs in this region. */
33 private Deque<Npc> npcs = new ConcurrentLinkedDeque<>();
34
35 /** A list of objects in this region. */
36 private Map<Position, List<GameObject>> objects;
37
38 /** A list of removed objects in this region. */
39 private Deque<GameObject> skipped;
40
41 /** A list of ground items in this region. */
42 private Map<Position, Set<GroundItem>> groundItems;
43
44 /** @return the players in this block */
45 public Collection<Player> getPlayers() {
46 return players;
47 }
48
49 /** @return the npcs in this block */
50 public Collection<Npc> getNpcs() {
51 return npcs;
52 }
53
54 /**
55 * Gets a {@link Set} of {@link GroundItem}s. If none then creating the
56 * set.
57 *
58 * @param position the position to grab from.
59 * @return the set of item nodes on the specified position.
60 */
61 public Set<GroundItem> getGroundItems(Position position) {
62 return getGroundItems().getOrDefault(position, ConcurrentHashMap.newKeySet());
63 }
64
65 /**
66 * The method that retrieves the item with {@code id} on {@code position}.
67 *
68 * @param id the identifier to retrieve the item with.
69 * @param position the position to retrieve the item on.
70 * @return the item instance wrapped in an optional, or an empty optional if
71 * no item is found.
72 */
74 for (GroundItem item : getGroundItems(position)) {
75 if (item.isRegistered() && item.item.matchesId(id)) {
76 return item;
77 }
78 }
79 return null;
80 }
81
82 /** Adds a player to this block. */
83 void addPlayer(Player player) {
84 players.add(player);
85 }
86
87 /** Removes a player from this block. */
88 void removePlayer(Player player) {
89 players.remove(player);
90 }
91
92 /** Adds an npc to this block. */
93 void addNpc(Npc npc) {
94 npcs.add(npc);
95 }
96
97 /** Removes an npc from this block. */
98 void removeNpc(Npc npc) {
99 npcs.remove(npc);
100 }
101
102 /** Adds an object to this block. */
103 void addObject(GameObject object) {
104 List<GameObject> objs = getObjects().getOrDefault(object.getPosition(), new LinkedList<>());
105 if (objs.add(object))
106 getObjects().put(object.getPosition(), objs);
107 }
108
109 /** Removes an object from this block. */
111 List<GameObject> objs = getObjects().get(object.getPosition());
112 if (objs != null)
113 objs.remove(object);
114 }
115
116 /** Adds a ground item to this block. */
118 Set<GroundItem> items = getGroundItems(item.getPosition());
119 int index = 0;
120 for (GroundItem other : items) {
121 if (other.getIndex() + 1 > index) {
122 index = other.getIndex() + 1;
123 }
124 }
125 item.setIndex(index);
126 items.add(item);
127 getGroundItems().put(item.getPosition(), items);
128 }
129
130 /** Adds a ground item to this block. */
132 Set<GroundItem> items = getGroundItems(item.getPosition());
133 items.remove(item);
134 if (items.isEmpty()) {
135 getGroundItems().remove(item.getPosition());
136 } else {
137 getGroundItems().put(item.getPosition(), items);
138 }
139 }
140
141 /** @return {@code true} if this region contains this npc */
142 boolean containsNpc(Npc npc) {
143 return npcs.contains(npc);
144 }
145
146 /** @return {@code true} if this region contains this player */
147 boolean containsPlayer(Player player) {
148 return players.contains(player);
149 }
150
151 /** @return {@code true} if object is in region */
152 boolean containsObject(GameObject object) {
153 List<GameObject> objs = getObjects().get(object.getPosition());
154 return objs != null && objs.contains(object);
155 }
156
157 /** @return {@code true} if position is occupied by object */
159 List<GameObject> objs = getObjects().get(position);
160 return objs != null && !objs.isEmpty();
161 }
162
164 for (GameObject object : getGameObjects(position)) {
165 if (object.getId() == id) {
166 return object;
167 }
168 }
169 return null;
170 }
171
173 Set<Map.Entry<Position, List<GameObject>>> entrySet = getObjects().entrySet();
174 for (Map.Entry<Position, List<GameObject>> entry : entrySet) {
175 for (GameObject object : entry.getValue()) {
176 if (!(object instanceof CustomGameObject))
177 continue;
178 CustomGameObject obj = (CustomGameObject) object;
179 if (!obj.isValid()) {
180 continue;
181 }
182 System.err.println("haha we got it " + obj.getId());
183 return obj;
184 }
185 }
186 return null;
187 }
188
189 List<GameObject> getGameObjects(Position position) {
190 return getObjects().getOrDefault(position, Collections.emptyList());
191 }
192
193 void sendGameObjects(Player player) {
194 for (GameObject object : getRemovedObjects()) {
195 player.send(new SendRemoveObject(object));
196 }
197 Set<Map.Entry<Position, List<GameObject>>> entrySet = getObjects().entrySet();
198 for (Map.Entry<Position, List<GameObject>> entry : entrySet) {
199 for (GameObject object : entry.getValue()) {
200 if (!(object instanceof CustomGameObject))
201 continue;
202 CustomGameObject obj = (CustomGameObject) object;
203 if (!obj.isValid())
204 continue;
205 player.send(new SendAddObject(obj));
206 }
207 }
208 }
209
210 /**
211 * The method which handles updating when the specified {@code player}
212 * enters a new region.
213 */
214 void sendGroundItems(Player player) {
215 for (Map.Entry<Position, Set<GroundItem>> entry : getGroundItems().entrySet()) {
216 for (GroundItem groundItem : entry.getValue()) {
217 if (!groundItem.isRegistered())
218 continue;
219
220 if (!groundItem.canSee(player))
221 continue;
222
223 player.send(new SendGroundItem(groundItem));
224 }
225 }
226 }
227
228 private Map<Position, Set<GroundItem>> getGroundItems() {
229 if (groundItems == null)
230 groundItems = new ConcurrentHashMap<>();
231 return groundItems;
232 }
233
234 private Map<Position, List<GameObject>> getObjects() {
235 if (objects == null)
236 objects = new ConcurrentHashMap<>();
237 return objects;
238 }
239
240 private Deque<GameObject> getRemovedObjects() {
241 if (skipped == null)
242 skipped = new ConcurrentLinkedDeque<>();
243 return skipped;
244 }
245
246 /**
247 * Gets a single tile in this region from the specified height, x and y
248 * coordinates.
249 *
250 * @param x The x coordinate.
251 * @param y The y coordinate.
252 * @return The tile in this region for the specified attributes.
253 */
254 int getFlags(int x, int y) {
255 if (flags == null)
256 flags = new int[SIZE * SIZE];
257 return flags[x + y * SIZE];
258 }
259
260 /**
261 * Gets a single tile in this region from the specified height, x and y
262 * coordinates.
263 *
264 * @param x The x coordinate.
265 * @param y The y coordinate.
266 * @return The tile in this region for the specified attributes.
267 */
268 void setFlags(int x, int y, int flag) {
269 if (flags == null)
270 flags = new int[SIZE * SIZE];
271 flags[x + y * SIZE] |= flag;
272 }
273
274 /**
275 * Gets a single tile in this region from the specified height, x and y
276 * coordinates.
277 *
278 * @param x The x coordinate.
279 * @param y The y coordinate.
280 * @return The tile in this region for the specified attributes.
281 */
282 void unsetFlags(int x, int y, int flag) {
283 if (flags == null)
284 flags = new int[SIZE * SIZE];
285 flags[x + y * SIZE] &= ~flag;
286 }
287
288 void skip(GameObject gameObject) {
289 getRemovedObjects().add(gameObject);
290 }
291}
boolean isValid()
Validates this npc based on its current region and registered state.
Definition Entity.java:154
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.
Represents a static game object loaded from the map fs.
Represents a single tile on the game world.
Definition Position.java:14
int[] flags
The clipping flags within the region.
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.
Map< Position, List< GameObject > > getObjects()
Deque< GameObject > skipped
A list of removed objects in this region.
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.
boolean containsObject(Position position)
Map< Position, List< GameObject > > objects
A list of objects in this region.
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.
Deque< Npc > npcs
A list of npcs in this 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.
Map< Position, Set< GroundItem > > getGroundItems()
Map< Position, Set< GroundItem > > groundItems
A list of ground items in this region.
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)
Deque< Player > players
A list of players in this region.
default int getId()
Gets the object id.