summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/BackgroundLoaderQueue.java
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-02-12 22:33:32 -0800
committerFuwn <[email protected]>2026-02-12 22:33:32 -0800
commitc7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch)
treedf9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/scape/BackgroundLoaderQueue.java
downloadworldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz
worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip
Initial commit
Diffstat (limited to 'NET/worlds/scape/BackgroundLoaderQueue.java')
-rw-r--r--NET/worlds/scape/BackgroundLoaderQueue.java158
1 files changed, 158 insertions, 0 deletions
diff --git a/NET/worlds/scape/BackgroundLoaderQueue.java b/NET/worlds/scape/BackgroundLoaderQueue.java
new file mode 100644
index 0000000..97eacd9
--- /dev/null
+++ b/NET/worlds/scape/BackgroundLoaderQueue.java
@@ -0,0 +1,158 @@
+package NET.worlds.scape;
+
+import NET.worlds.console.Main;
+import NET.worlds.console.MainCallback;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+class BackgroundLoaderQueue implements MainCallback {
+ private Room activeRoom;
+ private int itemsInQueue;
+ private BackgroundLoaderVector inAllRooms = new BackgroundLoaderVector();
+ private BackgroundLoaderVector inUnknownRoom = new BackgroundLoaderVector();
+ private Hashtable rooms = new Hashtable();
+ private boolean mainRan;
+
+ synchronized void activeRoomChanged(Room room) {
+ this.activeRoom = room;
+ }
+
+ public boolean hasHighPriorityItems() {
+ return this.inAllRooms.size() > 0;
+ }
+
+ synchronized void add(BackgroundLoaderElement e) {
+ if (!this.addHelper(e, false)) {
+ if (e.inAllRooms()) {
+ this.inAllRooms.insertElementAt(e, 0);
+ } else {
+ this.inUnknownRoom.addElement(e);
+ }
+ }
+
+ this.itemsInQueue++;
+ this.notify();
+ }
+
+ private boolean addHelper(BackgroundLoaderElement e, boolean isMainThread) {
+ Room r = e.getRoom(isMainThread);
+ if (r != null && r.isActive()) {
+ BackgroundLoaderVector v = (BackgroundLoaderVector)this.rooms.get(r);
+ if (v == null) {
+ this.rooms.put(r, v = new BackgroundLoaderVector());
+ }
+
+ v.addElement(e);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ private BackgroundLoaderElement getHelper(Room r) {
+ BackgroundLoaderElement e = null;
+ BackgroundLoaderVector v = (BackgroundLoaderVector)this.rooms.get(r);
+ if (v != null) {
+ e = v.removeFirst();
+ if (v.isEmpty()) {
+ this.rooms.remove(r);
+ }
+ }
+
+ return e;
+ }
+
+ synchronized BackgroundLoaderElement getItem() {
+ BackgroundLoaderElement e = null;
+
+ while (true) {
+ while (this.itemsInQueue == 0) {
+ try {
+ this.wait();
+ } catch (InterruptedException var3) {
+ }
+ }
+
+ if (this.inAllRooms.size() != 0) {
+ e = this.inAllRooms.removeFirst();
+ break;
+ }
+
+ if (this.activeRoom != null && (e = this.getHelper(this.activeRoom)) != null) {
+ break;
+ }
+
+ this.mainRan = false;
+ Main.register(this);
+
+ while (!this.mainRan) {
+ try {
+ this.wait();
+ } catch (InterruptedException var4) {
+ }
+ }
+
+ if (this.inAllRooms.size() == 0 && (this.activeRoom == null || this.rooms.get(this.activeRoom) == null)) {
+ if ((e = this.getClosestObject()) == null) {
+ if (this.inUnknownRoom.size() != 0) {
+ e = this.inUnknownRoom.removeFirst();
+ } else {
+ Enumeration k = this.rooms.keys();
+
+ assert k.hasMoreElements();
+
+ e = this.getHelper((Room)k.nextElement());
+
+ assert e != null;
+ }
+ }
+ break;
+ }
+ }
+
+ assert e != null;
+
+ this.itemsInQueue--;
+
+ assert this.itemsInQueue >= 0;
+
+ return e;
+ }
+
+ private BackgroundLoaderElement getClosestObject() {
+ synchronized (Pilot.visibleRooms) {
+ float minDist = Float.MAX_VALUE;
+ int minEle = -1;
+ int count = Pilot.visibleRoomInfo.size();
+
+ for (int i = 0; i < count; i++) {
+ RoomSubscribeInfo s = Pilot.visibleRoomInfo.elementAt(i);
+ if (s.d < minDist && this.rooms.get(Pilot.visibleRooms.elementAt(i)) != null) {
+ minDist = s.d;
+ minEle = i;
+ }
+ }
+
+ return minEle == -1 ? null : this.getHelper(Pilot.visibleRooms.elementAt(minEle));
+ }
+ }
+
+ @Override
+ public synchronized void mainCallback() {
+ int count = this.inUnknownRoom.size();
+ int i = 0;
+
+ while (count-- != 0) {
+ BackgroundLoaderElement e = (BackgroundLoaderElement)this.inUnknownRoom.get(i);
+ if (this.addHelper(e, true)) {
+ this.inUnknownRoom.removeElementAt(i);
+ } else {
+ i++;
+ }
+ }
+
+ this.mainRan = true;
+ Main.unregister(this);
+ this.notify();
+ }
+}