64 private Deque<Player> players =
new ConcurrentLinkedDeque<>();
67 private Deque<Npc> npcs =
new ConcurrentLinkedDeque<>();
70 private Map<Position, List<GameObject>> objects;
73 private Deque<GameObject> skipped;
76 private Map<Position, Set<GroundItem>> groundItems;
96 return getGroundItems().getOrDefault(position, ConcurrentHashMap.newKeySet());
109 if (item.isRegistered() && item.item.matchesId(
id)) {
117 void addPlayer(Player player) {
122 void removePlayer(Player player) {
123 players.remove(player);
127 void addNpc(Npc npc) {
132 void removeNpc(Npc npc) {
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);
144 void removeObject(GameObject
object) {
145 List<GameObject> objs = getObjects().get(
object.getPosition());
151 void addGroundItem(GroundItem item) {
154 for (GroundItem other : items) {
155 if (other.getIndex() + 1 > index) {
156 index = other.getIndex() + 1;
159 item.setIndex(index);
165 void removeGroundItem(GroundItem item) {
168 if (items.isEmpty()) {
176 boolean containsNpc(Npc npc) {
177 return npcs.contains(npc);
181 boolean containsPlayer(Player player) {
182 return players.contains(player);
186 boolean containsObject(GameObject
object) {
187 List<GameObject> objs = getObjects().get(
object.getPosition());
188 return objs !=
null && objs.contains(
object);
192 boolean containsObject(Position position) {
193 List<GameObject> objs = getObjects().get(position);
194 return objs !=
null && !objs.isEmpty();
197 GameObject getGameObject(
int id, Position position) {
198 for (GameObject
object : getGameObjects(position)) {
199 if (
object.getId() ==
id) {
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))
212 CustomGameObject obj = (CustomGameObject)
object;
213 if (!obj.isValid()) {
216 System.err.println(
"haha we got it " + obj.getId());
223 List<GameObject> getGameObjects(Position position) {
224 return getObjects().getOrDefault(position, Collections.emptyList());
227 void sendGameObjects(Player player) {
228 for (GameObject
object : getRemovedObjects()) {
229 player.send(
new SendRemoveObject(
object));
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))
236 CustomGameObject obj = (CustomGameObject)
object;
239 player.send(
new SendAddObject(obj));
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())
254 if (!groundItem.canSee(player))
257 player.send(
new SendGroundItem(groundItem));
263 if (groundItems ==
null)
264 groundItems =
new ConcurrentHashMap<>();
268 private Map<Position, List<GameObject>> getObjects() {
270 objects =
new ConcurrentHashMap<>();
274 private Deque<GameObject> getRemovedObjects() {
276 skipped =
new ConcurrentLinkedDeque<>();
288 int getFlags(
int x,
int y) {
290 flags =
new int[SIZE * SIZE];
291 return flags[x + y * SIZE];
302 void setFlags(
int x,
int y,
int flag) {
304 flags =
new int[SIZE * SIZE];
305 flags[x + y * SIZE] |= flag;
316 void unsetFlags(
int x,
int y,
int flag) {
318 flags =
new int[SIZE * SIZE];
319 flags[x + y * SIZE] &= ~flag;
322 void skip(GameObject gameObject) {
323 getRemovedObjects().add(gameObject);