Update talking_npc.py

updating talking_npc to use the current Menu system
This commit is contained in:
grungies1138 2016-10-04 08:34:53 -05:00 committed by Griatch
parent 3ff9eac86c
commit eb4be28d88

View file

@ -1,27 +1,20 @@
""" """
Evennia Talkative NPC Evennia Talkative NPC
Contribution - Griatch 2011 Contribution - Griatch 2011
This is a simple NPC object capable of holding a simple menu-driven This is a simple NPC object capable of holding a simple menu-driven
conversation. Create it by creating an object of typeclass conversation. Create it by creating an object of typeclass
contrib.talking_npc.TalkingNPC, For example using @create: contrib.talking_npc.TalkingNPC, For example using @create:
@create/drop John : contrib.talking_npc.TalkingNPC
@create/drop John : contrib.talking_npc.TalkingNPC
Walk up to it and give the talk command to strike up a conversation. Walk up to it and give the talk command to strike up a conversation.
If there are many talkative npcs in the same room you will get to If there are many talkative npcs in the same room you will get to
choose which one's talk command to call (Evennia handles this choose which one's talk command to call (Evennia handles this
automatically). automatically).
Note that this is only a prototype class, showcasing the uses of the Note that this is only a prototype class, showcasing the uses of the
menusystem module. It is NOT a full mob implementation. menusystem module. It is NOT a full mob implementation.
""" """
from evennia import DefaultObject, CmdSet, default_cmds from evennia import DefaultObject, CmdSet, default_cmds
from evennia.contrib import menusystem from evennia.utils.evmenu import EvMenu
# #
@ -31,10 +24,8 @@ from evennia.contrib import menusystem
class CmdTalk(default_cmds.MuxCommand): class CmdTalk(default_cmds.MuxCommand):
""" """
talks to an npc talks to an npc
Usage: Usage:
talk talk
This command is only available if a talkative non-player-character (NPC) This command is only available if a talkative non-player-character (NPC)
is actually present. It will strike up a conversation with that NPC is actually present. It will strike up a conversation with that NPC
and give you options on what to talk about. and give you options on what to talk about.
@ -51,19 +42,63 @@ class CmdTalk(default_cmds.MuxCommand):
self.caller.msg("(You walk up and talk to %s.)" % self.obj.key) self.caller.msg("(You walk up and talk to %s.)" % self.obj.key)
# conversation is a dictionary of keys, each pointing to
# a dictionary defining the keyword arguments to the MenuNode
# constructor.
conversation = obj.db.conversation
if not conversation:
self.caller.msg("%s says: 'Sorry, I don't have time to talk right now.'" % (self.obj.key))
return
# build all nodes by loading them from the conversation tree. # Initiate the menu
menu = menusystem.MenuTree(self.caller) EvMenu(self.caller, "typeclasses.talking_npc",
for key, kwargs in conversation.items(): startnode="menu_start_node")
menu.add(menusystem.MenuNode(key, **kwargs))
menu.start()
def menu_start_node(caller):
text = "Hello there, how can I help you?"
options = ({"desc": "Hey, do you know what this 'Evennia' thing is all about?",
"goto": "info1"},
{"desc": "What's your name, little NPC?",
"goto": "info2"})
return text, options
def info1(caller):
text = "Oh, Evennia is where you are right now! Don't you feel the power?"
options = ({"desc": "Sure, *I* do, not sure how you do though. You are just an NPC.",
"goto": "info3"},
{"desc": "Sure I do. What's yer name, NPC?"},
{"desc": "Ok, bye for now then."},
{"goto": "END"})
return text, options
def info2(caller):
text = "My name is not really important ... I'm just an NPC after all."
options = ({"desc": "I didn't really want to know it anyhow.",
"goto": "info3"},
{"desc": "Okay then, so what's this 'Evennia' thing about?",
"goto": "info1"})
return text, options
def info3(caller):
text = "Well ... I'm sort of busy so, have to go. NPC business. Important stuff. You wouldn't understand."
options = ({"desc": "Oookay ... I won't keep you. Bye.",
"goto": "END"},
{"desc": "Wait, why don't you tell me your name first?",
"goto": "info2"})
return text, options
def END(caller):
text = "Goodbye, then."
options = ()
return text, options
class TalkingCmdSet(CmdSet): class TalkingCmdSet(CmdSet):
@ -74,49 +109,15 @@ class TalkingCmdSet(CmdSet):
"populates the cmdset" "populates the cmdset"
self.add(CmdTalk()) self.add(CmdTalk())
#
# Discussion tree. See contrib.menusystem.MenuNode for the keywords.
# (This could be in a separate module too)
#
CONV = {"START": {"text": "Hello there, how can I help you?",
"links": ["info1", "info2"],
"linktexts": ["Hey, do you know what this 'Evennia' thing is all about?",
"What's your name, little NPC?"],
"keywords": None,
"callback": None},
"info1": {"text": "Oh, Evennia is where you are right now! Don't you feel the power?",
"links": ["info3", "info2", "END"],
"linktexts":["Sure, *I* do, not sure how you do though. You are just an NPC.",
"Sure I do. What's yer name, NPC?",
"Ok, bye for now then."],
"keywords": None,
"callback": None},
"info2": {"text": "My name is not really important ... I'm just an NPC after all.",
"links": ["info3", "info1"],
"linktexts": ["I didn't really want to know it anyhow.",
"Okay then, so what's this 'Evennia' thing about?"],
"keywords": None,
"callback": None},
"info3": {"text": "Well ... I'm sort of busy so, have to go. NPC business. Important stuff. You wouldn't understand.",
"links": ["END", "info2"],
"linktexts": ["Oookay ... I won't keep you. Bye.",
"Wait, why don't you tell me your name first?"],
"keywords": None,
"callback": None},
}
class TalkingNPC(DefaultObject): class TalkingNPC(DefaultObject):
""" """
This implements a simple Object using the talk command and using the This implements a simple Object using the talk command and using the
conversation defined above. . conversation defined above.
""" """
def at_object_creation(self): def at_object_creation(self):
"This is called when object is first created." "This is called when object is first created."
# store the conversation.
self.db.conversation = CONV
self.db.desc = "This is a talkative NPC." self.db.desc = "This is a talkative NPC."
# assign the talk command to npc # assign the talk command to npc
self.cmdset.add_default(TalkingCmdSet, permanent=True) self.cmdset.add_default(TalkingCmdSet, permanent=True)