def battle_from_instruction(split_instruction): arguments = text_to_split_section(split_instruction[1], ";", False) num_args = len(arguments) battle_party = [] party_member_names = arguments[0].split( ",") #Shouldn't have escape sequence, using builtin method for party_member_name in party_member_names: character = characters[section_strip(party_member_name)] battle_party.append(character) enemy_party = [] enemy_names = arguments[1].split( ",") #Also shouldn't have escape sequence, can use builtin method for enemy_name in enemy_names: enemy_name = enemy_name.lower( ) #Change to match format in util/variables/instances template_enemy = enemies[section_strip(enemy_name)] enemy_party.append(template_enemy.copy()) #Make sure to copy! location = section_strip(arguments[2]) intro = section_strip(arguments[3]) primary_enemy_name = section_strip(arguments[4]) if num_args == 5: full_battle(battle_party, enemy_party, location, intro, primary_enemy_name) elif num_args == 6: lose_message = section_strip(arguments[5]) #Stored for clarity full_battle(battle_party, enemy_party, location, intro, primary_enemy_name, lose_message)
def add_item_from_instruct(split_instruction): arguments = text_to_split_section(split_instruction[1], ";", False) #item_name, [description, [price, [damageAmt]]] num_args = len(arguments) if num_args != 4: raise ValueError("ADD_ITEM must have 4 values") add_item(arguments[0], arguments[1], arguments[2], arguments[4])
def replace_colors(instruction, start_line_num, strip_trailing_newline=True): line_num = start_line_num new_text = "" cur_val = "" is_first_of_line = False ignore_newline = False for c in instruction: if c == '{': new_text += cur_val cur_val = "" #Will always remove braces elif c == '}': split_section = text_to_split_section(cur_val, ":") if split_section[0] == "COLOR": if cur_val in ColorToColor: new_text += ColorToColor[cur_val] else: raise ValueError("Line " + str(line_num) + ": Unknown color \"" + cur_val + "\"") else: raise ValueError("Line " + str(line_num) + ": Non-color instruction \"{" + cur_val + "}\"") cur_val = "" elif c == '\n': line_num += 1 if not ignore_newline: cur_val += c elif c != '#': cur_val += c ignore_newline = (c == '}') or (c == '#' and is_first_of_line) is_first_of_line = c == '\n' #Update this for the next iteration new_text += cur_val new_text = remove_trailing_newline( new_text) if strip_trailing_newline else new_text return new_text
def handle_instruct(instruction, variables): """Returns a dictionary with information about instructions for caller. "Return": True if caller should exit, False if not. "Return depth": How many calls back to go, negative if infinite. "Target node": A reference to the node to jump to if applicable, None if not.""" instruct_status = { "Return": False, "Return all": False, "Target node": None, } split_instruction = text_to_split_section(instruction, ":", False) # Simple instructions if split_instruction[0] == "COLOR": color_instruct(split_instruction) elif split_instruction[0] == "TIME": time_instruct(split_instruction) elif split_instruction[0] == "CLEAR": clearConsole() elif split_instruction[0] == "LOADING_EFFECT": loading_effect() # Jump instructions elif split_instruction[0] == "EXIT_ALL": instruct_status["Return all"] = True instruct_status["Return"] = True elif split_instruction[0] == "EXIT_NODE": instruct_status["Return"] = True elif split_instruction[0] == "ENTER": instruct_status["Target node"] = get_node_from_instruct( split_instruction, 1) # Inventory, variables, characters elif split_instruction[0] == "ADD_ITEM": add_item_from_instruct(split_instruction) elif split_instruction[0] == "CHANGE": change_variable(split_instruction, variables) elif split_instruction[0] == "CHARACTER": handle_character(split_instruction) # Battle elif split_instruction[0] == "BATTLE": battle_from_instruction(split_instruction) return instruct_status
def convert_escape(line_num, section, variables, node_dict, prefix): err_text = check_escape(section, variables, node_dict, prefix, True) if err_text != "": raise ValueError("Line " + str(line_num) + ": " + err_text) split_section = text_to_split_section(section, ":", True) value = section #Default case if split_section[0] == "COLOR": color_name = split_section[1] value = ColorToColor[color_name] elif split_section[0] == "ENTER" or split_section[0] == "RETURN_TO": split_section[1] = convert_name_to_global(split_section[1], prefix) value = split_section_to_text(split_section, ":", True) elif split_section[ 0] == "CHANGE": #Currently the same as ENTER and RETURN_TO instruction split_section[1] = convert_name_to_global(split_section[1], prefix) value = split_section_to_text(split_section, ":", True) return value