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(); } }