Example #1
0
def setup_behavior_tree():
    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    surrender_plan = Sequence(name='Surrender Condition')
    decorator_inverter = Inverter()
    if_lost_check = Check(if_lost_the_game)
    decorator_inverter.child_node = if_lost_check
    surrender_action = Action(surrender)
    surrender_plan.child_nodes = [decorator_inverter, surrender_action]

    offensive_plan = Sequence(name='Offensive Strategy')
    can_dominate_check = Check(can_dominate_enemy_planet)
    decorator_always_false = AlwaysFalse()
    attack_action = Action(attack_most_valuable_enemy_planet)
    decorator_always_false.child_node = attack_action
    offensive_plan.child_nodes = [can_dominate_check, decorator_always_false]

    defensive_plan = Sequence(name='Defensive Strategy')
    is_attacked_check = Check(enemy_has_fleet_attacking)
    decorator_always_false = AlwaysFalse()
    defense_action = Action(send_defensive_fleet)
    decorator_always_false.child_node = defense_action
    defensive_plan.child_nodes = [is_attacked_check, decorator_always_false]

    spread_plan = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_most_valuable_neutral_planet)
    spread_plan.child_nodes = [neutral_planet_check, spread_action]

    root.child_nodes = [surrender_plan,offensive_plan, defensive_plan, spread_plan]

    logging.info('\n' + root.tree_to_string())
    return root
Example #2
0
def setup_behavior_tree():
    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    # Checks
    largest_fleet_check = Check(have_largest_fleet)
    enemy_planet_check = Check(if_enemy_planet_available)
    neutral_planet_check = Check(if_neutral_planet_available)

    # Spread to weakest neutral
    spread_orig = Sequence(name='Spread Original')
    # spread_orig = Selector(name='Spread Original')
    spread_orig_action = Action(spread_to_weakest_neutral_planet)
    spread_orig.child_nodes = [spread_orig_action]

    # Spread vanilla
    spread_vanilla_plan = Sequence(name='Spread Vanilla')
    # spread_vanilla_plan = Selector(name='Spread Vanilla')
    spread_vanilla_action = Action(spread_vanilla)
    spread_vanilla_plan.child_nodes = [spread_vanilla_action]

    # Spread to high growth rate
    spread_hgr_plan = Sequence(name='Spread High Growth Rate')
    # spread_hgr_plan = Selector(name='Spread High Growth Rate')
    spread_hgr_action = Action(spread_to_highest_growth_rate)
    spread_hgr_plan.child_nodes = [spread_hgr_action]

    # Spread default
    spread_def_plan = Sequence(name='Spread Default')
    # spread_def_plan = Selector(name='Spread Default')
    spread_def_action = Action(spread_default)
    spread_def_plan.child_nodes = [spread_def_action]

    # Attack Vanilla
    attack_plan = Sequence(name='Attack Vanilla')
    # attack_plan = Selector(name='Attack Vanilla')
    attack_action = Action(attack_vanilla)
    attack_plan.child_nodes = [attack_action]

    # Attack high growth rate
    attack_hgr_plan = Sequence(name='Attack High Growth Rate')
    # attack_hgr_plan = Selector(name='Attack High Growth Rate')
    attack_hgr_action = Action(attack_high_growth)
    attack_hgr_plan.child_nodes = [attack_hgr_action]

    # Attack-full
    attack_full = Sequence(name='Attack Full Sequence')
    attack_full.child_nodes = [largest_fleet_check, enemy_planet_check, attack_plan, attack_hgr_plan]

    # Spread-full
    spread_full = Sequence(name='Spread Full Sequence')
    spread_full.child_nodes = [neutral_planet_check, spread_def_plan, spread_hgr_plan, spread_vanilla_plan, spread_orig]

    root.child_nodes = [attack_full, spread_full, attack_action.copy(),
                        attack_hgr_action.copy(), spread_vanilla_action.copy(),
                        spread_orig_action.copy(), spread_def_action.copy(),
                        spread_hgr_action.copy()]
    logging.info('\n' + root.tree_to_string())
    return root
def setup_behavior_tree():
    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    attack = Action(attack_weakest_enemy_planet)
    offensive_plan.child_nodes = [largest_fleet_check, attack]

    spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_weakest_neutral_planet)
    spread_sequence.child_nodes = [neutral_planet_check, spread_action]

    reinforcements = Sequence(name='Send Reinforcements')
    invaded_check = Check(invaded_maybe)
    send_backup = Action(send_aid_to_invaded_planet)
    reinforcements.child_nodes = [invaded_check, send_backup]

    multi_plan = Sequence(name='Multi Attack')
    have_planets = Check(min_planets)
    multi_action = Action(multi_attack)

    multi_plan.child_nodes = [largest_fleet_check, have_planets, multi_action]

    mul_spread = Sequence(name='Multi Spread')
    have_planets = Check(min_planets)
    is_neutral = Check(if_neutral_planet_available)
    multi_spread_action = Action(multi_spread)

    mul_spread.child_nodes = [have_planets, is_neutral, multi_spread_action]

    strong_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    strongest_attack = Action(strong_attack)
    strong_plan.child_nodes = [largest_fleet_check, strongest_attack]

    finish_plan = Sequence(name='Finish Plan')
    largest_fleet_check = Check(have_largest_fleet)
    have_planets = Check(min_planets)
    finish_action = Action(barrage)
    finish_plan.child_nodes = [largest_fleet_check, have_planets, finish_action]

    pinpoint_plan = Sequence(name='Pinpoint')
    largest_fleet_check = Check(have_largest_fleet)
    have_planets = Check(min_planets)
    pinpoint_action = Action(pinpoint)
    pinpoint_plan.child_nodes = [largest_fleet_check, have_planets, pinpoint_action]

    root.child_nodes = [offensive_plan, spread_sequence, reinforcements, mul_spread, multi_plan, finish_plan, attack.copy()]

    logging.info('\n' + root.tree_to_string())
    return root
def setup_behavior_tree():
    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')


    defensive_plan = Sequence(name="Defensive Plan")
    defend_being_attacked = Check(enemy_attacking)
    defend_planets = Action(defend_planet)
    defend_losing = Check(losing_enemy)
    defend_abandon = Action(abandon_planet)
    defensive_plan.child_nodes = [defend_being_attacked,defend_planets,defend_losing,defend_abandon]

    '''offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    attack = Action(attack_weakest_enemy_planet)
    offensive_plan.child_nodes = [largest_fleet_check, attack]'''

    greedy_a_behavior = Sequence(name='Greedy Attack')
    greedy_a_check = Check(outnumebering_enemy)
    greedy_a_attack = Action(attack_enemy)
    greedy_a_behavior.child_nodes = [greedy_a_check,greedy_a_attack]

    inter_a_behavior = Sequence(name="Intermediate Attack")
    inter_a_check = Check(winning_few_enemy)
    inter_a_attack = Action(attack_enemy)
    inter_a_behavior.child_nodes = [inter_a_check,inter_a_attack]

    caut_a_behavior = Sequence(name="Cautious Attack")
    caut_a_check = Check(losing_enemy)
    caut_a_attack = Action(attack_enemy)
    caut_a_behavior.child_nodes = [caut_a_check, caut_a_attack]

    offensive_plan = Selector(name='Offensive Strategy')
    offensive_plan.child_nodes = [greedy_a_behavior,inter_a_behavior,caut_a_behavior]


    spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_planets)
    spread_sequence.child_nodes = [neutral_planet_check, spread_action]

    '''spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_weakest_neutral_planet)
    spread_sequence.child_nodes = [neutral_planet_check, spread_action]'''

    root.child_nodes = [defensive_plan,offensive_plan,spread_sequence]

    logging.info('\n' + root.tree_to_string())
    return root
Example #5
0
def setup_behavior_tree():

    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    attack = Action(attack_weakest_enemy_planet)
    offensive_plan.child_nodes = [largest_fleet_check, attack]

    spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_weakest_neutral_planet)
    spread_sequence.child_nodes = [neutral_planet_check, spread_action]

    root.child_nodes = [offensive_plan, spread_sequence, attack.copy()]

    logging.info('\n' + root.tree_to_string())
    return root
def new_tree():
    root = Selector()
 
    #plan for the first few turns of the game, expand to easy neutral planets then switch to aggressive expansion
    startingMoves = Sequence()
    gameStart = Check(homeBase)
    fleetEngage = Check(readyShips)
    createEmpire = Action(expandToNeutrals)
    startingMoves.child_nodes = [gameStart, fleetEngage, createEmpire]

    #once we have a stable base, we can start spreading out and attacking enemy planets
    expandEmpire = Sequence()
    muster = Check(checkForces)
    crusade = Action(assult)
    expandEmpire.child_nodes = [muster, crusade]

    root.child_nodes = [startingMoves, expandEmpire]

    logging.info('\n' + root.tree_to_string())
    return root
def setup_behavior_tree():
    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    exploit_plan = Sequence(name='Exploit Weakest Strategy')
    production_check = Check(if_better_production)
    weak_planet_check = Check(if_weak_enemy_planet)
    exploit = Action(attack_good_enemy_planet)
    exploit_plan.child_nodes = [production_check, weak_planet_check, exploit]
    
    explore_plan = Sequence(name='Explore Strategy')
    explore_check = Check(if_unexplored)
    explore = Action(spread_unexplored_planet)
    explore_plan.child_nodes = [explore_check, explore]
    
    default_attack = Action(attack_weakest_enemy_planet)

    root.child_nodes = [exploit_plan, explore_plan, default_attack]

    logging.info('\n' + root.tree_to_string())
    return root
Example #8
0
def setup_behavior_tree():
    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')
    
    other_attack = Action(attack_same_planet_as_enemy)
    b_spread = Action(best_spread)
    
    offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    attack = Action(attack_weakest_enemy_planet)
    offensive_plan.child_nodes = [largest_fleet_check, attack]

    spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_weakest_closest_neutral_planet)
    spread_sequence.child_nodes = [neutral_planet_check, spread_action]

    root.child_nodes = [b_spread, other_attack, offensive_plan]

    logging.info('\n' + root.tree_to_string())
    return root
def setup_behavior_tree():

    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    #attack_beneficial
    beneficial_plan = Sequence(name='beneficial Strategy')
    #check_neutrals = Check(benefit_check)
    benefit = Action(attack_beneficial)
    beneficial_plan.child_nodes = [benefit]  #[check_neutrals, benefit]

    speedy_spread_spree_plan = Sequence(name='speedy quick Strategy')
    check_neutrals = Check(quicky_check)
    quick = Action(attack_quickest)
    speedy_spread_spree_plan.child_nodes = [check_neutrals, quick]

    fat_spread_plan = Sequence(name='Fat Spread Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    #checky_check = Check(checky_check)
    spread = Action(spread_to_fattest_neutral_planet)  #_weakest_enemy_planet
    fat_spread_plan.child_nodes = [largest_fleet_check, spread]

    offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    attack = Action(aggressive_attack)  #_weakest_enemy_planet
    offensive_plan.child_nodes = [largest_fleet_check, attack]

    #spread_sequence = Sequence(name='Spread Strategy')
    #neutral_planet_check = Check(if_neutral_planet_available)
    #spread_action = Action(spread_to_weakest_neutral_planet)
    #spread_sequence.child_nodes = [neutral_planet_check, spread_action]

    root.child_nodes = [
        speedy_spread_spree_plan, offensive_plan, beneficial_plan,
        attack.copy()
    ]
    #root.child_nodes = [offensive_plan, spread_sequence, attack.copy()]

    logging.info('\n' + root.tree_to_string())
    return root
Example #10
0
def setup_behavior_tree():
    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    grab_what_enemy_wants = Sequence(name='Getting what enemy wants')
    neutral_planet_check = Check(if_neutral_planet_available)
    is_grabbing = Check(if_a_good_neutral_available)
    take_good_neutral = Action(if_a_good_neutral_available_now)
    grab_what_enemy_wants.child_nodes = [neutral_planet_check, is_grabbing, take_good_neutral]


    offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    #largest_growth_rate_check = Check(have_largest_growth_rate)
    attack = Action(attack_weakest_enemy_planet)
    offensive_plan.child_nodes = [attack]

    """
    send_three_to_neutral = Sequence(name='send 3 neutral')
    above_30 = Check(above_30_not_sending)
    send_one = Action(send_to_closest_neutral_if_backup)
    send_backup = Action(send_affensive_help)
    send_three_to_neutral.child_nodes = [above_30, send_one, send_backup, send_backup]
    """

    spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_selector = Selector(name='Spread Selector')
    enough = Check(if_dont_have_enough_neutral)
    not_close_enough = Check(if_enemy_too_far)
    spread_selector.child_nodes = [enough, not_close_enough]
    worth = Check(worth_attacking_neutral)
    spread_action = Action(spread_to_best_neutral)
    spread_sequence.child_nodes = [neutral_planet_check, spread_selector, worth, spread_action]

    root.child_nodes = [grab_what_enemy_wants, spread_sequence, offensive_plan]

    logging.info('\n' + root.tree_to_string())
    return root
Example #11
0
def setup_behavior_tree():

    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    #attack plan
    invert_att = Inverter()
    succeed_att = Succeeder()
    offensive_plan = Sequence(name='Offensive Strategy')
    enough_planets_check = Check(has_enough_planets)
    attack = Action(attack_weakest_enemy_planet)
    invert_att.child_node = succeed_att
    succeed_att.child_node = offensive_plan
    offensive_plan.child_nodes = [enough_planets_check, attack]

    #defense plan
    invert_def = Inverter()
    succeed_def = Succeeder()
    defense = Action(defend_weakest_ally)
    succeed_def.child_node = defense
    invert_def.child_node = succeed_def

    #reinforce plan
    #invert_reinforce = Inverter()
    #succeed_reinforce = Succeeder()
    #reinforce = Action(reinforce_new_planet)
    #succeed_reinforce.child_node = reinforce
    #invert_reinforce.child_node = succeed_reinforce

    #spread plan
    spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_weakest_neutral_planet)
    spread_sequence.child_nodes = [neutral_planet_check, spread_action]

    root.child_nodes = [invert_att, invert_def, spread_sequence, attack.copy()]

    logging.info('\n' + root.tree_to_string())
    return root
Example #12
0
def generate_tree(root):
    num_children = random.randint(2, 5)
    while True:

        is_composite = random.randint(0, 100) < 10
        if is_composite:
            new_class = ["-", Selector] if random.randint(
                0, 2) == 0 else ["_", Sequence]
        else:
            new_class = random.choice(list(node_dict.items()))

        if new_class[1] == Selector:

            new_node = Selector(name="Selector")
            new_node.child_nodes = []
            new_root = generate_tree(new_node)
            new_root.parent_node = root
            root.child_nodes.append(new_root)

        elif new_class[1] == Sequence:

            new_node = Sequence(name="Sequence")
            new_node.child_nodes = []
            new_root = generate_tree(new_node)
            new_root.parent_node = root
            root.child_nodes.append(new_root)

        elif new_class[0].isupper():
            new_node = Check(new_class[1])
            new_node.parent_node = root
            root.child_nodes.append(new_node)

        elif new_class[0].islower():
            new_node = Action(new_class[1])
            new_node.parent_node = root
            root.child_nodes.append(new_node)

        if num_children > 0:
            num_children -= 1
        if num_children <= 0:
            break
    return root
Example #13
0
def setup_behavior_tree():

    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')
    
    # sequence = Sequence(name)
    # check(s) = Check(name_of_check) - takes boolean
    # action = Action(name_of_behavior) - do something
    # sequence.child_nodes = [check(s), action(s)]

    #spread_sequence = Sequence(name='Spread Strategy')
    #neutral_planet_check = Check(if_neutral_planet_available)
    #spread_action = Action(spread_to_weakest_neutral_planet)
    #spread_sequence.child_nodes = [neutral_planet_check, spread_action]
    
    spread_production_sequence = Sequence(name='Spread Production Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_production_sequence_action = Action(spread_production)
    spread_production_sequence.child_nodes = [neutral_planet_check, spread_production_sequence_action]
    
    defend_sequence = Sequence(name='Defend Strategy')
    enemy_attacks_check = Check(enemy_attacks)
    defend_action = Action(defend2)
    defend_sequence.child_nodes = [enemy_attacks_check, defend_action]
    
    offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    attack = Action(attack_weakest_enemy_planet)
    offensive_plan.child_nodes = [largest_fleet_check, attack]

    # 
    root.child_nodes = [spread_production_sequence, offensive_plan, attack.copy()]
    
    #root.child_nodes = [spread_sequence, spread_production_sequence, offensive_plan, attack.copy()]

    logging.info('\n' + root.tree_to_string())
    return root
Example #14
0
def setup_behavior_tree():

    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')
    """
    offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    attack = Action(attack_weakest_enemy_planet)
    offensive_plan.child_nodes = [largest_fleet_check, attack]

    spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_weakest_neutral_planet)
    spread_sequence.child_nodes = [neutral_planet_check, spread_action]

    counter_action = Action(counter_fleet)

    regrow_sequence = Sequence(name='Regrow Strategy')
    lone_ally_check = Check(lone_ally)
    take_high_growth_action = Action(take_high_growth)
    regrow_sequence.child_nodes = [lone_ally_check, take_high_growth_action]

    checkmate_sequence = Sequence(name='Checkmate Strategy')
    lone_enemy_check = Check(lone_enemy)
    rush_first_target_action = Action(rush_first_target)
    checkmate_sequence.child_nodes = [lone_enemy_check, rush_first_target_action]
    """

    early_game_strategy = Sequence(name='Early Game Strategy')
    lone_ally_check = Check(lone_ally)
    grow_from_one_action = Action(grow_from_one)
    early_game_strategy.child_nodes = [lone_ally_check, grow_from_one_action]

    production_action = Action(production)

    aggressive_strategy = Selector(name='Aggressive Strategy')
    attack_sequence = Sequence(name='Attack Sequence')
    largest_fleet_check = Check(have_largest_fleet)
    attack_action = Action(attack)
    attack_sequence.child_nodes = [largest_fleet_check, attack_action]
    spread_action = Action(spread)
    aggressive_strategy.child_nodes = [attack_sequence, spread_action]

    defend_action = Action(defend)

    root.child_nodes = [
        early_game_strategy, aggressive_strategy, defend_action
    ]

    logging.info('\n' + root.tree_to_string())
    return root
Example #15
0
def setup_behavior_tree():

    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_weakest_neutral_planet)
    spread_sequence.child_nodes = [neutral_planet_check, spread_action]

    spread_enemy = Sequence(name='Spread Attack')
    enemy_check = Check(spread_check)
    spread_go = Action(start_spread)
    spread_enemy.child_nodes = [enemy_check, spread_go]

    defense = Sequence(name='defense')
    defensive_check = Check(defense_check)
    defense_go = Action(defense_action)
    defense.child_nodes = [defensive_check, defense_go]

    root.child_nodes = [spread_sequence, spread_enemy, defense]

    logging.info('\n' + root.tree_to_string())
    return root
Example #16
0
def setup_behavior_tree():
    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    grab_what_enemy_wants = Sequence(name='Getting what enemy wants')
    neutral_planet_check = Check(if_neutral_planet_available)
    is_grabbing = Check(if_a_good_neutral_available)
    take_good_neutral = Action(if_a_good_neutral_available_now)
    grab_what_enemy_wants.child_nodes = [
        neutral_planet_check, is_grabbing, take_good_neutral
    ]

    offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_largest_fleet)
    #largest_growth_rate_check = Check(have_largest_growth_rate)
    attack = Action(attack_weakest_enemy_planet)
    offensive_plan.child_nodes = [attack]
    """
    send_three_to_neutral = Sequence(name='send 3 neutral')
    above_30 = Check(above_30_not_sending)
    send_one = Action(send_to_closest_neutral_if_backup)
    send_backup = Action(send_affensive_help)
    send_three_to_neutral.child_nodes = [above_30, send_one, send_backup, send_backup]
    """

    spread_sequence = Sequence(name='Spread Strategy')
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_selector = Selector(name='Spread Selector')
    enough = Check(if_dont_have_enough_neutral)
    not_close_enough = Check(if_enemy_too_far)
    spread_selector.child_nodes = [enough, not_close_enough]
    worth = Check(worth_attacking_neutral)
    spread_action = Action(spread_to_best_neutral)
    spread_sequence.child_nodes = [
        neutral_planet_check, spread_selector, worth, spread_action
    ]

    root.child_nodes = [grab_what_enemy_wants, spread_sequence, offensive_plan]

    logging.info('\n' + root.tree_to_string())
    return root
Example #17
0
def parse_tree(tree_string):

    root = Selector(name="Root")

    #Current node has the current selector or sequence that we are processing
    current_node = root

    for char in tree_string:
        if char == "-":
            #Selector
            new_node = Selector(name="Selector")
            new_node.parent_node = current_node
            new_node.child_nodes = []
            current_node.child_nodes.append(new_node)

        elif char == "_":
            #Sequence
            new_node = Sequence(name="Sequence")
            new_node.parent_node = current_node
            new_node.child_nodes = []
            current_node.child_nodes.append(new_node)

        elif char == "[":
            #Start children
            current_node = current_node.child_nodes[-1]

        elif char == "]":
            #End children
            current_node = current_node.parent_node

        elif char.isupper():
            #Check
            new_node = Check(node_dict[char])
            new_node.parent_node = current_node
            current_node.child_nodes.append(new_node)

        elif char.islower():                   
            #Action
            new_node = Action(node_dict[char])
            new_node.parent_node = current_node
            current_node.child_nodes.append(new_node)  
    logging.info('\n Decoded tree as: ' + root.tree_to_string())

    return root   
Example #18
0
def parse_tree(tree_string):

    root = Selector(name="Root")

    #Current node has the current selector or sequence that we are processing
    current_node = root

    for char in tree_string[1,-1]:
        if char == "-":
            #Selector
            new_node = Selector(name="Selector")
            new_node.parent = current_node
            current_node.child_nodes.append(new_node)

        elif char == "_":
            #Sequence
            new_node = Selector(name="Sequence")
            new_node.parent = current_node
            current_node.child_nodes.append(new_node)

        elif char == "[":
            #Start children
            current_node = current_node.child_nodes[-1]

        elif char == "]":
            #End children
            current_node = current_node.parent_node

        elif char.isupper():
            #Check
            new_node = Check(node_dict(char))
            new_node.parent_node = current_node
            current_node.child_nodes.append(new_node)

        elif char.islower():                   
            #Action
            new_node = Action(node_dict(char))
            new_node.parent_node = current_node
            current_node.child_nodes.append(new_node)  

    return root   
Example #19
0
def setup_behavior_tree():

    # Top-down construction of behavior tree
    root = Selector(name='High Level Ordering of Strategies')

    # defense_planet = Sequence(name='Save My Planet')
    # check_attack = Check(if_planet_got_attack)
    # protect = Action(protect_planet)
    # defense_planet.child_nodes = [check_attack, protect]

    # silent = Sequence(name="do nothing")
    # check_ships = Check(check_ship_left)
    # wait = Action(do_no_op)
    # silent.child_nodes = [check_ships, wait]

    defensive_plan = Sequence(name='Deffensive Strategy')
    check_owned_ship = Check(have_more_conquest)
    protect_action = Action(defendPlanet)
    defensive_plan.child_nodes = [check_owned_ship, protect_action]
    # defense = Selector(name='defense')
    # defense.child_nodes = [denfensive_plan]

    populate_plan = Sequence(name='no-op')
    conserve_check = Check(save_fleet)
    nothing = Action(do_no_op)
    populate_plan.child_nodes = [conserve_check, nothing]

    growth_plan = Sequence(name='Eco Strategy')
    eco_check = Check(need_econ)
    grow = Action(spread_ally)
    growth_plan.child_nodes = [eco_check, grow]

    offensive_plan = Sequence(name='Offensive Strategy')
    largest_fleet_check = Check(have_less_conquest)
    attack = Action(attack_weakest_enemy_planet)
    offensive_plan.child_nodes = [largest_fleet_check, attack]

    spread_sequence = Sequence(name='Spread Strategy')
    conquest_check = Check(have_less_conquest)
    neutral_planet_check = Check(if_neutral_planet_available)
    spread_action = Action(spread_to_weakest_neutral_planet)
    spread_sequence.child_nodes = [
        conquest_check, neutral_planet_check, spread_action
    ]

    steal_plan = Sequence(name='Thief')
    steal_check = Check(steal_fleet)
    big_check = Check(have_largest_fleet)
    steal = Action(steal_planet)
    steal_plan.child_nodes = [big_check, steal_check, steal]
    '''
    defensive_plan = Sequence(name='Defend Strategy')
    enemy_planet_attack_check = Check(if_enemy_planet_attack)
    largest_fleet_check = Check(have_largest_fleet)
    visiting_check = Check(is_busy_reinforce) # Returns true if already did this.
    '''

    #steal and growth plan could cause a lost
    #root.child_nodes = [spread_sequence, offensive_plan, defensive_plan, steal_plan]
    root.child_nodes = [
        defensive_plan, offensive_plan, spread_sequence, steal_plan
    ]

    logging.info('\n' + root.tree_to_string())
    return root