InterpreterShip: fix bug that caused ships with no input docks to not be serviced
[fleet.git] / src / edu / berkeley / fleet / interpreter / InterpreterShip.java
index 43fa15b..811c02b 100644 (file)
@@ -26,7 +26,34 @@ abstract class InterpreterShip extends FleetTwoShip {
     public abstract void service();
 
     public final void _service() {
+
+        // service all the docks
         for(InterpreterDock p : docks.values()) p.service();
+
+        // flushing logic (must come between dock servicing and subclass)
+        boolean someflushing = false;
+        boolean allflushing = true;
+        boolean someempty = false;
+        for(InterpreterDock d : docks.values()) {
+            if (!d.isInputDock()) continue;
+            if (d.flushing) someflushing = true;
+            else allflushing = false;
+            if (!d.flushing && !d.dataReadyForShip) someempty = true;
+        }
+        if (allflushing && someflushing) {
+            for(InterpreterDock d : docks.values())
+                if (d.isInputDock())
+                    d.flushing = false;
+            return;
+        } else if (someflushing && !someempty) {
+            for(InterpreterDock d : docks.values())
+                if (d.isInputDock() && !d.flushing && d.dataReadyForShip) {
+                    System.out.println("FLUSH AT " + this);
+                    d.dataReadyForShip = false;
+                }
+        }
+
+        // now pass control to the subclass
         service();
     }