World Engine Tutorial Example 1

Challenge Accepted

On this page you will find the code for the "Challenge Accepted" world. Here is a brief description of this world:

Are you ready for a challenge? In this world, each action you take will be evaluated by the AI, in order to determine how likely you are to succeed. You will begin by stating a goal you want to achieve. Next, you will describe yourself and your current situation. This is your starting point, and the AI will take this into consideration when judging the likelihood of an action being successful. Once you've described your starting point, the challenge begins! Can you reach your stated goal?

import random

data = {
    "stage": 1,
    "goal": "",
    "current_situation": "",
    "num_steps": 0,
    "goal_progress": 0,
    "goal_progress_get": None,
    "display_progress_every_n_steps": 5
}

def setup():
    if data["stage"] == 1:
        ai.perform_generation = False
        if not ui.action:
            ui.display("Please type in a goal into the action input box.")
        else:    
            data["goal"] = ui.action
            data["stage"] = 2
            ui.display("STEP 2/2: Please describe who you are and your current situation in the action input box below. This will be your starting point.")
    elif data["stage"] == 2:
        ai.perform_generation = False
        if not ui.action:
            ui.display("Please describe who you are and your current situation in the action input box.")
        else:
            data["current_situation"] = ui.action
            if data["current_situation"][-1] not in (".", "!", "?"):
                data["current_situation"] += "."
            data["stage"] = 3
            
            register_gets_and_checks()
            ai.perform_generation = True
            ai.enforce("Here is some information about the main protagonist: " + data["current_situation"])


impossible_action_gets = [
    ("The character's action defies the laws of physics or is otherwise impossible.", "bool", "action"),
    ("This action attempts to affect events beyond the character's control.", "bool", "action"),
    ("If this were a game in the form of an interactive story, this action would be considered cheating.", "bool", "action")
]
action_success_get = ("On a scale of 1 to 100 (representing percentage), estimate the chances of the character performing this action successfully.", "int", "action")
def register_gets_and_checks():
    for impossible_action_get in impossible_action_gets:
        ai.future_get(*impossible_action_get)
    ai.future_get(*action_success_get)
    data["goal_progress_get"] = ai.future_get(f'Estimate the progress made towards the following goal: "{data["goal"]}", on a scale of 1 to 100 (representing percentage). The previous percentage was: {data["goal_progress"]}. You should consider whether the character\'s action has brought the character closer to the goal, further away, or has not affected the progress towards this goal. You should then provide the appropriate integer between 0 to 100.', "int", "action")

def main():
    # for the first few steps, we add the current situation description, to get the AI started.
    if data["num_steps"] <= 10:
        ai.enforce("Here is some information about the main protagonist: " + data["current_situation"])
    
    if not ui.action:
        ai.perform_generation = False
        ui.display("Please type in an action below.")
        return

    for impossible_action_get in impossible_action_gets:
        if ai.get(*impossible_action_get):
            ai.perform_generation = False
            ui.display("This action is impossible. Please specify a different action.")
    action_success_chance = ai.get(*action_success_get)
    if random.randint(1, 100) < action_success_chance:
        ai.enforce("Make the character successfully perform the specified action.")
    else:
        ai.enforce("Make the character fail at performing the specified action.")

    data["goal_progress"] = ai.get(*data["goal_progress_get"])
        
    if data["goal_progress"] == 100:
        ui.display(f'Progress towards goal: {data["goal_progress"]}%. Congratulations, you reached your goal!')
    elif data["num_steps"] % data["display_progress_every_n_steps"] == 0:
        if data["num_steps"] == 5:
            data["display_progress_every_n_steps"] = 10
        ui.display(f'Progress towards goal: {data["goal_progress"]}%')

def pre_generate():
    data["num_steps"] += 1
    if data["stage"] <= 2:
        setup()
    else:
        main()