#!/usr/bin/python2 """ Quick and dirty sketch of an NPC idea. """ import random class Knowledge: def __init__(self,filename="knowledge.txt"): pass SKILL_DEFAULTS = {"oak nation":"geography", "mithrol":"geography", "baccata":"geography", } KNOWLEDGE = { "great oak":[ ("oak nation",3,"I hear it's a magic tree in the north."), ("oak nation",5,"It's a city in the north, built around a huge magical tree."), ("oak nation",7,"Great Oak is the capital of the nation ruled by the wizard-god Veles and the Velesian race he created.") ], "baccatan liberation war":[ ("war",3,"A fast war in the east backed by Mithrol against Baccata's rulers."), ("war",5,"A revolution won by Prince Dominic in the east by clever use of religion and foreign aid."), ] } class NPC: def __init__(self,**options): self.name = options.get("name","Grignr") self.skills = options.get("skills",{}) def GetSkill(self,skill_name): ## print "Checking for skill: " + skill_name s = self.skills.get(skill_name,0) if not s: default = SKILL_DEFAULTS.get(skill_name,0) if default: s = self.GetSkill(default) return s def GetSkillRollForTopic(self,skill,topic): """How much do I know about this specific topic?""" random.seed( self.name + topic ) r = random.randint(1,6) s = self.GetSkill(skill) return r+s def FindLines(self,topic): possible = [] for l in KNOWLEDGE.get(topic,[]): skill, dc, text = l ## print "Line: " + str(text) ## print "Skill: " + str(skill) roll = self.GetSkillRollForTopic(skill,topic) ## print "I roll " + str(roll) + " vs. " + skill + " DC " + str(dc) + "." if roll >= dc: possible.append(l) return possible def TalkAbout(self,topic): lines = self.FindLines(topic) if not lines: return "[" + self.name + "] I'm sorry, but I've got nothing to say about \"" + topic + "\"." random.seed() return "[" + self.name + "] " + lines[ random.randint(0,len(lines)-1) ][2] ## Autorun if __name__ == "__main__": print "\nAlderos:" a = NPC(name="Alderos", skills={"history":5,"magic":10,"war":3,"geography":3}, ) ##print a.GetSkill("war") ##print a.GetSkillRollForTopic("magic","a") ##print a.GetSkillRollForTopic("magic","b") print a.FindLines("great oak") print a.FindLines("baccatan liberation war") print "\nBirch:" b = NPC(name="Birch", skills={"history":5,"magic":10,"war":3,"geography":3}, ) print b.FindLines("great oak") print b.FindLines("baccatan liberation war") print "\n*** Now putting it together: Get random lines of speech. ***\n" print a.TalkAbout("great oak") print b.TalkAbout("great oak") print a.TalkAbout("baccatan liberation war") print b.TalkAbout("baccatan liberation war")