local function visitDockedShips(ship, callback, visited, depth)
  local signature = ship:getSignature()
  if not visited[signature] then
    visited[signature] = true
    callback(ship, depth)
    for candidate in mn.getShipList() do
      if ship:isDocked(candidate) then
        visitDockedShips(candidate, callback, visited, depth + 1)
      end
    end
  end
end

return function(callback)
  return function(ship)
    if ship and ship:isValid() then
      if ship:isDocked() then
        visitDockedShips(ship, callback, {}, 0)
      else
        callback(ship, 0)
      end
    end
  end
end

