RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
RegionBlock.java
1package com.osroyale.game.world.region;
2
3import com.osroyale.game.world.entity.mob.npc.Npc;
4import com.osroyale.game.world.entity.mob.player.Player;
5import com.osroyale.game.world.items.ground.GroundItem;
6import com.osroyale.game.world.object.CustomGameObject;
7import com.osroyale.game.world.object.GameObject;
8import com.osroyale.game.world.position.Position;
9import com.osroyale.net.packet.out.SendAddObject;
10import com.osroyale.net.packet.out.SendGroundItem;
11import com.osroyale.net.packet.out.SendRemoveObject;
12
13import java.util.*;
14import java.util.concurrent.ConcurrentHashMap;
15import java.util.concurrent.ConcurrentLinkedDeque;
16
17import static com.osroyale.game.world.region.Region.SIZE;
18
57
58public class RegionBlock {
59
61 private int[] flags;
62
64 private Deque<Player> players = new ConcurrentLinkedDeque<>();
65
67 private Deque<Npc> npcs = new ConcurrentLinkedDeque<>();
68
70 private Map<Position, List<GameObject>> objects;
71
73 private Deque<GameObject> skipped;
74
76 private Map<Position, Set<GroundItem>> groundItems;
77
79 public Collection<Player> getPlayers() {
80 return players;
81 }
82
84 public Collection<Npc> getNpcs() {
85 return npcs;
86 }
87
95 public Set<GroundItem> getGroundItems(Position position) {
96 return getGroundItems().getOrDefault(position, ConcurrentHashMap.newKeySet());
97 }
98
107 GroundItem getGroundItem(int id, Position position) {
108 for (GroundItem item : getGroundItems(position)) {
109 if (item.isRegistered() && item.item.matchesId(id)) {
110 return item;
111 }
112 }
113 return null;
114 }
115
117 void addPlayer(Player player) {
118 players.add(player);
119 }
120
122 void removePlayer(Player player) {
123 players.remove(player);
124 }
125
127 void addNpc(Npc npc) {
128 npcs.add(npc);
129 }
130
132 void removeNpc(Npc npc) {
133 npcs.remove(npc);
134 }
135
137 void addObject(GameObject object) {
138 List<GameObject> objs = getObjects().getOrDefault(object.getPosition(), new LinkedList<>());
139 if (objs.add(object))
140 getObjects().put(object.getPosition(), objs);
141 }
142
144 void removeObject(GameObject object) {
145 List<GameObject> objs = getObjects().get(object.getPosition());
146 if (objs != null)
147 objs.remove(object);
148 }
149
151 void addGroundItem(GroundItem item) {
152 Set<GroundItem> items = getGroundItems(item.getPosition());
153 int index = 0;
154 for (GroundItem other : items) {
155 if (other.getIndex() + 1 > index) {
156 index = other.getIndex() + 1;
157 }
158 }
159 item.setIndex(index);
160 items.add(item);
161 getGroundItems().put(item.getPosition(), items);
162 }
163
165 void removeGroundItem(GroundItem item) {
166 Set<GroundItem> items = getGroundItems(item.getPosition());
167 items.remove(item);
168 if (items.isEmpty()) {
169 getGroundItems().remove(item.getPosition());
170 } else {
171 getGroundItems().put(item.getPosition(), items);
172 }
173 }
174
176 boolean containsNpc(Npc npc) {
177 return npcs.contains(npc);
178 }
179
181 boolean containsPlayer(Player player) {
182 return players.contains(player);
183 }
184
186 boolean containsObject(GameObject object) {
187 List<GameObject> objs = getObjects().get(object.getPosition());
188 return objs != null && objs.contains(object);
189 }
190
192 boolean containsObject(Position position) {
193 List<GameObject> objs = getObjects().get(position);
194 return objs != null && !objs.isEmpty();
195 }
196
197 GameObject getGameObject(int id, Position position) {
198 for (GameObject object : getGameObjects(position)) {
199 if (object.getId() == id) {
200 return object;
201 }
202 }
203 return null;
204 }
205
206 GameObject getCustomObject(int id, Position position) {
207 Set<Map.Entry<Position, List<GameObject>>> entrySet = getObjects().entrySet();
208 for (Map.Entry<Position, List<GameObject>> entry : entrySet) {
209 for (GameObject object : entry.getValue()) {
210 if (!(object instanceof CustomGameObject))
211 continue;
212 CustomGameObject obj = (CustomGameObject) object;
213 if (!obj.isValid()) {
214 continue;
215 }
216 System.err.println("haha we got it " + obj.getId());
217 return obj;
218 }
219 }
220 return null;
221 }
222
223 List<GameObject> getGameObjects(Position position) {
224 return getObjects().getOrDefault(position, Collections.emptyList());
225 }
226
227 void sendGameObjects(Player player) {
228 for (GameObject object : getRemovedObjects()) {
229 player.send(new SendRemoveObject(object));
230 }
231 Set<Map.Entry<Position, List<GameObject>>> entrySet = getObjects().entrySet();
232 for (Map.Entry<Position, List<GameObject>> entry : entrySet) {
233 for (GameObject object : entry.getValue()) {
234 if (!(object instanceof CustomGameObject))
235 continue;
236 CustomGameObject obj = (CustomGameObject) object;
237 if (!obj.isValid())
238 continue;
239 player.send(new SendAddObject(obj));
240 }
241 }
242 }
243
248 void sendGroundItems(Player player) {
249 for (Map.Entry<Position, Set<GroundItem>> entry : getGroundItems().entrySet()) {
250 for (GroundItem groundItem : entry.getValue()) {
251 if (!groundItem.isRegistered())
252 continue;
253
254 if (!groundItem.canSee(player))
255 continue;
256
257 player.send(new SendGroundItem(groundItem));
258 }
259 }
260 }
261
262 private Map<Position, Set<GroundItem>> getGroundItems() {
263 if (groundItems == null)
264 groundItems = new ConcurrentHashMap<>();
265 return groundItems;
266 }
267
268 private Map<Position, List<GameObject>> getObjects() {
269 if (objects == null)
270 objects = new ConcurrentHashMap<>();
271 return objects;
272 }
273
274 private Deque<GameObject> getRemovedObjects() {
275 if (skipped == null)
276 skipped = new ConcurrentLinkedDeque<>();
277 return skipped;
278 }
279
288 int getFlags(int x, int y) {
289 if (flags == null)
290 flags = new int[SIZE * SIZE];
291 return flags[x + y * SIZE];
292 }
293
302 void setFlags(int x, int y, int flag) {
303 if (flags == null)
304 flags = new int[SIZE * SIZE];
305 flags[x + y * SIZE] |= flag;
306 }
307
316 void unsetFlags(int x, int y, int flag) {
317 if (flags == null)
318 flags = new int[SIZE * SIZE];
319 flags[x + y * SIZE] &= ~flag;
320 }
321
322 void skip(GameObject gameObject) {
323 getRemovedObjects().add(gameObject);
324 }
325}
Set< GroundItem > getGroundItems(Position position)