From c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 12 Feb 2026 22:33:32 -0800 Subject: Initial commit --- NET/worlds/scape/BackgroundLoaderQueue.java | 158 ++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 NET/worlds/scape/BackgroundLoaderQueue.java (limited to 'NET/worlds/scape/BackgroundLoaderQueue.java') 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(); + } +} -- cgit v1.2.3