process TUG given PORT
   define TUG.ICON as a pointer variable
   define PORT as an integer variable
   define SHIP as pointer variable
   define .TOWSPEED to mean 2500.0     '' tenths of a mile per day
   define .FREESPEED to mean 5000.0

   let TUG.ICON = TUG

   until .END.OF.SIMULATION,
   do
      until TUG.QUEUE(TUG) is empty,
      do
         let TUG.STATUS(TUG) = .BUSY

           '' pick the ship in queue that is closed
         for each SHIP in TUG.QUEUE(TUG),
            with SHIP.LOCATION(SHIP) eq TUG.LOCATION(TUG)
            find the first case
         if found
            remove this SHIP from TUG.QUEUE(TUG)
         else
            remove the first SHIP from TUG.QUEUE(TUG)
         always

           '' go to the ship, then tow it
         if SHIP.LOCATION(SHIP) = .IN.HARBOR
               '' move the tug to the port entrance
            if TUG.LOCATION(TUG) ne .IN.HARBOR
               let VXFORM.V = .PORT.TRANSFORMS + PORT -1
               display TUG.ICON with "ltug"
               call MOVE.TO(TUG, PORT.X.ENTRANCE(PORT), PORT.Y.ENTRANCE(PORT),
                 .FREESPEED, .TRUE)
            always

               '' get the ship
            let TUG.SHIP.IN.TOW(TUG) = SHIP
            remove this SHIP from PORT.HARBOR(PORT)
            call HARBOR.UPDATE given PORT

               '' tow the ship to the berth that it has picked out
            let VXFORM.V = .PORT.TRANSFORMS + PORT -1
            display TUG.ICON with "rtugp"    '' right tug pull
            call MOVE.TO(TUG, BERTH.X.LOCATION(SHIP.BERTH(SHIP)), 
              BERTH.Y.LOCATION(SHIP.BERTH(SHIP)), .TOWSPEED, .TRUE)
            let SHIP.LOCATION(SHIP) = .AT.DOCK

            let VXFORM.V = .PORT.TRANSFORMS + PORT -1
            display TUG.ICON with "rtug"
         else    '' at dock
               '' move to the berth to get the ship
            let VXFORM.V = .PORT.TRANSFORMS + PORT -1
            if BERTH.X.LOCATION(SHIP.BERTH(SHIP)) gt LOCATION.X(TUG)
                display TUG.ICON with "rtug"
            always
            if BERTH.X.LOCATION(SHIP.BERTH(SHIP)) lt LOCATION.X(TUG)
                display TUG.ICON with "ltug"
            always
            call MOVE.TO(TUG, BERTH.X.LOCATION(SHIP.BERTH(SHIP)), 
              BERTH.Y.LOCATION(SHIP.BERTH(SHIP)), .FREESPEED, .TRUE)

               '' grab the ship
            let TUG.SHIP.IN.TOW(TUG) = SHIP

               '' tow the ship to the port entrance
            let VXFORM.V = .PORT.TRANSFORMS + PORT -1
            display TUG.ICON with "ltugp"
            call MOVE.TO(TUG, PORT.X.ENTRANCE(PORT), PORT.Y.ENTRANCE(PORT),
              .TOWSPEED, .TRUE)
            let SHIP.LOCATION(SHIP) = .IN.HARBOR

            let VXFORM.V = .PORT.TRANSFORMS + PORT -1
            display TUG.ICON with "ltug"
         always

         let TUG.SHIP.IN.TOW(TUG) = 0
         let TUG.LOCATION(TUG) = SHIP.LOCATION(SHIP)

         reactivate this SHIP now
      loop
      let TUG.STATUS(TUG) = .IDLE
      suspend   ''Awaiting more SHIPs to serve
   loop
   suspend   ''Awaiting end of simulation

end   ''process TUG
