# Path: diplomacy/engine/digest.py
# def find_convoys(units, fleets):
#     """
#     Generates pairs consisting of a cluster of adjacent non-coastal
#     fleets, and the coastal territories that are reachable via convoy
#     from that cluster.  This is necessary to determine legal orders.
# 
#     """
#     fleets = [f for f in fleets if not has_land(f)]
#     index = {f: {f} for f in fleets}
# 
#     # Calculate the connected sets of fleets
#     for f1, f2 in combinations(fleets, 2):
#         # If this pair is not adjacent, ignore it
#         if f2 not in borders(f1):
#             continue
# 
#         # If the sets for each of these fleets are not already the same set,
#         # merge them and update the index
#         if index[f1] != index[f2]:
#             index[f1] |= index[f2]
#             index.update((x, index[f1]) for x in index[f2])
# 
#     groups = {frozenset(S) for S in index.values()}
# 
#     armies = {u['subregion'] for u in units if u['u_type'] == 'A'}
# 
#     convoyable = []
#     for gset in groups:
#         coasts = {
#             sr for f in gset
#             for b in borders(f)
#             for sr in territory_parts(get_territory(b))
#             if is_land(sr)
#         }
#         if coasts & armies:
#             convoyable.append((set(gset), coasts))
# 
#     return convoyable
# 
# def builds_available(units, owns):
#     builds = defaultdict(int)
#     builds.update(supplycenters(owns))
# 
#     for u in units:
#         builds[u['government']] -= 1
# 
#     return builds
# 
# Path: diplomacy/engine/utils.py
# def get_territory(sr_token):
# def territory_parts(t_token):
# def borders(sr_token):
# def unit_in(sr_token, units):
# def territory_display(t_token):
# def subregion_display(sr_token):
# def unit_display(sr_token):
# def power_token(p_str):
# def territory_token(t_str):
# def subregion_token(sr_tuple):
# def is_supply(sr_token):
# def is_land(sr_token):
# def is_sea(sr_token):
# def has_land(sr_token):
# def has_sea(sr_token):


from . import standard
from .digest import find_convoys, builds_available
from .utils import unit_in, get_territory, borders, territory_parts, has_land, is_fleet
        '': {x: (x in convoyable
                 and any(x == b for b in borders(actor)))
             for x in target}
    }


def valid_support(actor, units, owns, season):
    if season not in ('S', 'F'):
        return {}

    if not unit_in(actor, units):
        return {}

    adj = {sr for b in borders(actor)
           for sr in territory_parts(get_territory(b))}

    # support to hold
    results = {
        a: {'': False} for a in adj
        if unit_in(a, units)
    }

    # support to attack
    attackers = {
        b for a in adj
        for b in borders(a)
        if b != actor
        and unit_in(b, units)
    }
    for a in attackers:

# Path: diplomacy/engine/digest.py
# def find_convoys(units, fleets):
#     """
#     Generates pairs consisting of a cluster of adjacent non-coastal
#     fleets, and the coastal territories that are reachable via convoy
#     from that cluster.  This is necessary to determine legal orders.
# 
#     """
#     fleets = [f for f in fleets if not has_land(f)]
#     index = {f: {f} for f in fleets}
# 
#     # Calculate the connected sets of fleets
#     for f1, f2 in combinations(fleets, 2):
#         # If this pair is not adjacent, ignore it
#         if f2 not in borders(f1):
#             continue
# 
#         # If the sets for each of these fleets are not already the same set,
#         # merge them and update the index
#         if index[f1] != index[f2]:
#             index[f1] |= index[f2]
#             index.update((x, index[f1]) for x in index[f2])
# 
#     groups = {frozenset(S) for S in index.values()}
# 
#     armies = {u['subregion'] for u in units if u['u_type'] == 'A'}
# 
#     convoyable = []
#     for gset in groups:
#         coasts = {
#             sr for f in gset
#             for b in borders(f)
#             for sr in territory_parts(get_territory(b))
#             if is_land(sr)
#         }
#         if coasts & armies:
#             convoyable.append((set(gset), coasts))
# 
#     return convoyable
# 
# def builds_available(units, owns):
#     builds = defaultdict(int)
#     builds.update(supplycenters(owns))
# 
#     for u in units:
#         builds[u['government']] -= 1
# 
#     return builds
# 
# Path: diplomacy/engine/utils.py
# def get_territory(sr_token):
# def territory_parts(t_token):
# def borders(sr_token):
# def unit_in(sr_token, units):
# def territory_display(t_token):
# def subregion_display(sr_token):
# def unit_display(sr_token):
# def power_token(p_str):
# def territory_token(t_str):
# def subregion_token(sr_tuple):
# def is_supply(sr_token):
# def is_land(sr_token):
# def is_sea(sr_token):
# def has_land(sr_token):
# def has_sea(sr_token):


# Path: diplomacy/engine/check.py
from . import standard
from .digest import find_convoys, builds_available
from .utils import unit_in, get_territory, borders, territory_parts, has_land, is_fleet

        '': {x: (x in convoyable
                 and any(x == b for b in borders(actor)))
             for x in target}
    }


def valid_support(actor, units, owns, season):
    if season not in ('S', 'F'):
        return {}

    if not unit_in(actor, units):
        return {}

    adj = {sr for b in borders(actor)
           for sr in territory_parts(get_territory(b))}

    # support to hold
    results = {
        a: {'': False} for a in adj
        if unit_in(a, units)
    }

    # support to attack
    attackers = {
        b for a in adj
        for b in borders(a)
        if b != actor
        and unit_in(b, units)
    }
    for a in attackers:

        reachable = adj & set(borders(a))