Reputation: 720
I'm working on a text-based RPG in Python, but I'm stuck on NPCs. I need something that's kind of like a tree. For example:
Bot: hello there. 1. Hi, what's your name? 2. Hi, Where do you live? > 2 Player: Where do you live? Bot: That's a creepy question. Why do you ask? 1. I was just curious. 2. Never mind. 3. Hide yo kids, hide yo wife > 1 Player: I was just curious. Bot: Okay. 1. Do you like cheese? 2. Bye. > 1 Player: Do you like cheese? Bot: Yes! I LOVE cheese.
Each choice you make would branch off to other choices, A.K.A. if the user had answered '1' for the first question the bot would answer "My name is Bob. What's yours?"
My game is designed in a way where a level editor would not be an impossible prospect (every place you can visit is stored as a key in a dictionary, then a tuple containing everything in it [I have a shop class, an enemy class, a 'portal' class, and soon an NPC class]). So I'd like this created in a way so I can hold it all in a variable that my NPC class has stored (not a bunch of 'if' statements)
Just to clarify, I am NOT asking for someone to write code for me, I am just unsure about how to approach the problem.
Tree-like structure that begins with one string with a number of strings that 'branch off' from it
Each one of those strings has more 'branches'
The difference from this and a tuple of tuples is that there needs to be a string where they branch off rather than an immediate split.
Upvotes: 4
Views: 3137
Reputation: 56654
You want a directed graph, where bot statements are nodes and player responses are arcs.
You may also want arcs to have some built-in intelligence - to enable/disable themselves based on player state (ie if the player does not have the blue key then "unlock the blue door" is not an available option).
Sample code to follow.
Upvotes: 0
Reputation: 13289
This is closely related to a decision tree but the player is the one making the choices, so it's not quite one in an AI sense. You can define a data structure yourself that has these properties, though
class TreeNode:
def __init__(self,parent,children,overview_text,direction_text):
#... set the variables ...
# question_text is a string
def print_question(self):
print overview_text
for child in children:
print direction_text
# add some accessor methods
def add_child(self,child):
...
Your NPCs then could keep track of their state via a particular TreeNode - call that the current_question- and the entire conversation is in a tree of TreeNodes - call that a dialogue.
class NPC:
def __init__(self,dialogue):
#... set the variables ...
self.current_question = None
def converse(self):
#.... do some stuff....
self.current_question = users_choice;
This way you can break off the conversation as needed.
Note that TreeNode might be a misnome because you might want conversations to represent a graph instead of just a tree. Lots of RPGs will have conversation paths that can loop to repeat information, for example. The data structure above can't do this because the overview_text (conceptually part of the node) and the direction_text (conceptually part of the edge) are treated as one.
Upvotes: 4