diff --git a/play.py b/play.py index 5855873..5008a0f 100644 --- a/play.py +++ b/play.py @@ -1,54 +1,34 @@ from story.story_manager import * from generator.gpt2.gpt2_generator import * - -def console_print(text, width=75): - last_newline = 0 - i = 0 - while i < len(text): - if text[i] == "\n": - last_newline = 0 - elif last_newline > width and text[i] == " ": - text = text[:i] + "\n" + text[i:] - last_newline = 0 - else: - last_newline += 1 - i += 1 - print(text) - - -def get_num_options(num): - - while True: - choice = input("Which do you choose? ") - try: - result = int(choice) - if result >= 0 and result < num: - return result - else: - print("Error invalid choice. ") - except ValueError: - print("Error invalid choice. ") - +from story.utils import * +from story.custom_story import * def select_game(): print("Which game would you like to play?") - options = ["zombies", "hospital", "peasant", "apocalypse", "classic", "knight", "necromancer", "vague"] + options = ["zombies", "hospital", "apocalypse", "classic", "knight", "necromancer", "custom"] for i, option in enumerate(options): console_print(str(i) + ") " + option + ": " + get_context(option) + "\n") choice = get_num_options(len(options)) - return options[choice] + if options[choice] == "custom": + context, prompt = make_custom_story() + + else: + game = options["choice"] + prompt = get_story_start(game) + context = get_context(game) + + return context, prompt def instructions(): text = "\nAI Dungeon 2 Instructions:" text += '\n* Enter actions starting with a verb ex. "go to the tavern" or "attack the orc."' - text += '\n* If you want to say something then enter \'say "(thing you want to say)"\'.' - text += '\n* You can also just press enter with no action if you want to let the story continue.' + text += '\n* If you want to say something then enter \'say "(thing you want to say)"\'' text += '\n* Finally if you want to end your game and start a new one just enter "restart" for any action. ' return text -def play_aidungeon_2(): +def play_aidungeon_2(): print("Initializing AI Dungeon! (This might take a few minutes)") generator = GPT2Generator() @@ -62,12 +42,10 @@ def play_aidungeon_2(): while True: print("\n\n") - game = select_game() + context, prompt = select_game() console_print(instructions()) - prompt = get_story_start(game) - context = get_context(game) - story_manager.start_new_story(prompt, context=context) + story_manager.start_new_story(prompt, context=context) print("\n") console_print(context + str(story_manager.story)) diff --git a/story/custom_story.py b/story/custom_story.py new file mode 100644 index 0000000..dd93dda --- /dev/null +++ b/story/custom_story.py @@ -0,0 +1,53 @@ +from story.utils import * +import numpy as np + +YAML_FILE = "story_data.yaml" + + +def make_custom_story(): + + with open(YAML_FILE, 'r') as stream: + data = yaml.safe_load(stream)["custom"] + + print("Pick a setting.") + settings = data["settings"].keys() + for i, setting in enumerate(settings): + console_print(str(i) + ") " + setting) + setting_key = list(settings)[get_num_options(len(settings))] + + print("Pick a character") + characters = data["settings"][setting_key]["characters"] + for i, character in enumerate(characters): + console_print(str(i) + ") " + character) + character_key = list(characters)[get_num_options(len(characters))] + + name = input("What is your name? ") + setting_description = data["settings"][setting_key]["description"] + character = data["settings"][setting_key]["characters"][character_key] + + context = "You are " + name + ", a " + character_key + " " + setting_description + \ + "You have a " + character["item1"] + " and a " + character["item2"] + "." + prompt_num = np.random.randint(0,len(character["prompts"])) + prompt = character["prompts"][prompt_num] + + return context, prompt + +if __name__=='__main__': + c, p = make_custom_story() + print(c) + print(p) + + # print("Pick a setting.") + # for i, setting in enumerate(settings): + # console_print(str(i) + ") " + setting) + # setting_choice = get_num_options(len(settings)) + # + # + # + # print("") + # options = ["zombies", "hospital", "peasant", "apocalypse", "classic", "knight", "necromancer"] + # for i, option in enumerate(options): + # console_print(str(i) + ") " + option + ": " + get_context(option) + "\n") + # + # choice = get_num_options(len(options)) + # return options[choice] \ No newline at end of file diff --git a/story/story_data.yaml b/story/story_data.yaml index 51eb906..0416584 100644 --- a/story/story_data.yaml +++ b/story/story_data.yaml @@ -5,7 +5,7 @@ prompts: peasant: "You were kicked out of your home and have set out to see if you can find work in the next town. You step onto the road and see" - apocalypse: "You walk for two hours and take a break. You've left the town you were in and are now in a more rural area. You look around you and see " + apocalypse: "You walk for two hours and take a break. You've left the town you were in and are now in a more rural area. You look around and see " zombies: "You're on top of a building. You look over the city and see" @@ -35,3 +35,66 @@ contexts: necromancer: "You are a necromancer in a world where undead spell casters rule and fight one another for power and control. " scifi: "You are a UN space marine with information crucial to the war effort that must get back to Earth. " + +custom: + settings: +# apocalyptic: +# description: "You live in a post apocalyptic world trying to survive by scavenging among the ruins of what is left behind. " +# +# zombies: +# description: "A few months ago a zombie infection broke out. You now are trying to survive on the ruins of what's left in the midst of zombie hordes. " +# +# modern: +# description: "" + +# context is "you are [name] a [character] [description]. You have a [item1] and [item2]. + + fantasy: + description: "living in the kingdom of Larion. " + characters: + + noble: + prompts: ["You are awakened by one of your servants who tells you that your keep is under attack. You look out the window and see"] + + item1: "pouch of gold" + item2: "small dagger" + + knight: + prompts: ["You enter the forest where you believe the ogre that has been terrorizing your home has been hiding. You step inside and"] + + item1: "steel longsword" + item2: "wooden shield" + + squire: + prompts: ["You follow Sir Theo as he enters the forest, he turns to you and says"] + + item1: "spear" + item2: "pack of supplies" + + wizard: + prompts: ["You finish your long journey and finally arrive at the ruin you've been looking for. You look around and see"] + + item1: "staff" + item2: "spellbook" + + ranger: + prompt: ["You spot the deer and are ready to finish your hunt when suddenly"] + + item1: "hunting bow" + item2: "quiver of arrows" + + peasant: + prompt: ["You wake up and begin working in the fields. You see"] + + item1: "pitchfork" + item2: "nothing else" + + rogue: + prompt: ["You successfully sneak into the merchant's house. You look around and see"] + + item1: "long steel dagger" + item2: "length of rope" + + + + diff --git a/story/utils.py b/story/utils.py index d956257..cf5cb07 100644 --- a/story/utils.py +++ b/story/utils.py @@ -7,6 +7,34 @@ YAML_FILE = "story/story_data.yaml" from profanityfilter import ProfanityFilter pf = ProfanityFilter() +def console_print(text, width=75): + last_newline = 0 + i = 0 + while i < len(text): + if text[i] == "\n": + last_newline = 0 + elif last_newline > width and text[i] == " ": + text = text[:i] + "\n" + text[i:] + last_newline = 0 + else: + last_newline += 1 + i += 1 + print(text) + + +def get_num_options(num): + + while True: + choice = input("Enter the number of your choice: ") + try: + result = int(choice) + if result >= 0 and result < num: + return result + else: + print("Error invalid choice. ") + except ValueError: + print("Error invalid choice. ") + def get_context(key): with open(YAML_FILE, 'r') as stream: data_loaded = yaml.safe_load(stream) @@ -35,7 +63,8 @@ def get_action_verbs(key): def player_died(text): - dead_phrases = ["you died", "you are dead", "You died", "You are dead", "You're dead", "you're dead", "you have died", "You have died"] + dead_phrases = ["you die", "You die", "you died", "you are dead", "You died", "You are dead", "You're dead", + "you're dead", "you have died", "You have died"] for phrase in dead_phrases: if phrase in text: return True @@ -43,7 +72,7 @@ def player_died(text): def player_won(text): - won_phrases = ["live happily ever after"] + won_phrases = ["live happily ever after", "you live forever"] for phrase in won_phrases: if phrase in text: return True