sc2simulator/setup/treeSelection.py
def selectUnitList(treeMod, race, rules, mapData, numFails=0):
    """pick units that meet all criteria"""
    units = []
    newRules = copyRules(rules)
    motherShipPicked = False
    available = set()
    tree = treeMod.getLastTree()
    ignoredTypes = {12, 31, 58, 85, 113, 128, 151, 501, 687, 892}
    for u in tree._leaves[race].values():
        for p in u.produces:
            if any([ignoreType == p.mType for ignoreType in ignoredTypes]):
                continue
            available.add(p)
    while len(units) < rules.unitsMax: # create units, but no more than the specified maximum
        u = pickRandomUnit(available, newRules, motherShipPicked)
        if u == None: # didn't find a new unit to add (criteria not met) => verify that this (finished) unit list meets conditions
            if rules.unitsMin < len(units): break # enough units were created; minimum num units condition met
            elif numFails < c.MAX_UNIT_GRP_TRIES: # repick units to better meet conditions
                print("Criteria failed %d times; unit selection has restarted"%(
                    numFails + 1))
                return selectUnitList(available, rules, mapData, numFails+1)
            else: # attempted to generate unit + conditions too many times
                print("FAILED to select units after %d attempts."%(numFails))
                return []
        else: # picked a unit successfully; attempt to pick more!
            if u.name == "Mothership": motherShipPicked = True # ensure that at most only one mothership can be picked
            units.append( u )
    if rules.defense:
        remaining = int(rules.defense)
        available = [treeMod.getUnitByID(code)[0] \
            for code in codeMap[race]["defense"]]
        while remaining > 0:
            newU = random.choice(available)
            units.append(newU)
            remaining -= 1
    if rules.detectors:
        remaining = int(rules.detectors)
        available = [treeMod.getUnitByID(code)[0] \
            for code in codeMap[race]["detection"]]
        while remaining > 0:
            newU = random.choice(available)
            units.append(newU)
            remaining -= 1
    return units
pick units that meet all criteria
pick units that meet all criteria
pick units that meet all criteria
def selectUnitList(treeMod, race, rules, mapData, numFails=0):
    units = []
    newRules = copyRules(rules)
    motherShipPicked = False
    available = set()
    tree = treeMod.getLastTree()
    ignoredTypes = {12, 31, 58, 85, 113, 128, 151, 501, 687, 892}
    for u in tree._leaves[race].values():
        for p in u.produces:
            if any([ignoreType == p.mType for ignoreType in ignoredTypes]):
                continue
            available.add(p)
    while len(units) < rules.unitsMax: 
        u = pickRandomUnit(available, newRules, motherShipPicked)
        if u == None: 
            if rules.unitsMin < len(units): break 
            elif numFails < c.MAX_UNIT_GRP_TRIES: 
                print("Criteria failed %d times; unit selection has restarted"%(
                    numFails + 1))
                return selectUnitList(available, rules, mapData, numFails+1)
            else: 
                print("FAILED to select units after %d attempts."%(numFails))
                return []
        else: 
            if u.name == "Mothership": motherShipPicked = True 
            units.append( u )
    if rules.defense:
        remaining = int(rules.defense)
        available = [treeMod.getUnitByID(code)[0] \
            for code in codeMap[race]["defense"]]
        while remaining > 0:
            newU = random.choice(available)
            units.append(newU)
            remaining -= 1
    if rules.detectors:
        remaining = int(rules.detectors)
        available = [treeMod.getUnitByID(code)[0] \
            for code in codeMap[race]["detection"]]
        while remaining > 0:
            newU = random.choice(available)
            units.append(newU)
            remaining -= 1
    return units
def selectUnitList ( treeMod , race , rules , mapData , numFails = 0 ) : units = [ ] newRules = copyRules ( rules ) motherShipPicked = False available = set ( ) tree = treeMod . getLastTree ( ) ignoredTypes = { 12 , 31 , 58 , 85 , 113 , 128 , 151 , 501 , 687 , 892 } for u in tree . _leaves [ race ] . values ( ) : for p in u . produces : if any ( [ ignoreType == p . mType for ignoreType in ignoredTypes ] ) : continue available . add ( p ) while len ( units ) < rules . unitsMax : u = pickRandomUnit ( available , newRules , motherShipPicked ) if u == None : if rules . unitsMin < len ( units ) : break elif numFails < c . MAX_UNIT_GRP_TRIES : print ( "Criteria failed %d times; unit selection has restarted" % ( numFails + 1 ) ) return selectUnitList ( available , rules , mapData , numFails + 1 ) else : print ( "FAILED to select units after %d attempts." % ( numFails ) ) return [ ] else : if u . name == "Mothership" : motherShipPicked = True units . append ( u ) if rules . defense : remaining = int ( rules . defense ) available = [ treeMod . getUnitByID ( code ) [ 0 ] for code in codeMap [ race ] [ "defense" ] ] while remaining > 0 : newU = random . choice ( available ) units . append ( newU ) remaining -= 1 if rules . detectors : remaining = int ( rules . detectors ) available = [ treeMod . getUnitByID ( code ) [ 0 ] for code in codeMap [ race ] [ "detection" ] ] while remaining > 0 : newU = random . choice ( available ) units . append ( newU ) remaining -= 1 return units
pick units that meet all criteria
pick units that meet all criteria
"""pick units that meet all criteria""" # create units, but no more than the specified maximum # didn't find a new unit to add (criteria not met) => verify that this (finished) unit list meets conditions # enough units were created; minimum num units condition met # repick units to better meet conditions # attempted to generate unit + conditions too many times # picked a unit successfully; attempt to pick more! # ensure that at most only one mothership can be picked
treeMod
race
rules
mapData
numFails