def SetAction(self, verb, action, idobject = None): if type(verb) is str: verb = [verb] if type(idobject) is str or idobject is None: idobject = [idobject] for i in verb: for j in idobject: try: if type(verbs.objects[verbs.directory.index(i)]) is str: i = verbs.objects[verbs.directory.index(i)] except: game.Log(self.name + " > Verb '" + i + "' does not exist -- it will be automatically created. Be warned, though, that it won't have a default response.") verbs.New(verb) try: # Delete the previous action, if set matches = Indices(self.verbs, i) foundmatch = False for k in matches: if self.idobjects[k] == j: foundmatch = k if foundmatch is not False: # Match found -- delete it del self.actions[foundmatch] del self.verbs[foundmatch] del self.idobjects[foundmatch] except: # No matches, we're in the clear pass self.verbs.append(i) self.actions.append(action) self.idobjects.append(j)
def Location(self, location = None): if location is not None: success = 0 try: del locations.Get(self.location).contents[locations.Get(self.location).contents.index(self.name)] except: pass try: del Get(self.location).contents[Get(self.location).contents.index(self.name)] except: pass try: locations.Get(location).contents.append(self.name) self.location = location success = 1 except: pass try: if Get(location).isContainer or Get(location).isSurface: Get(location).contents.append(self.name) self.location = location success = 1 except: pass if self.name == "me" and location == "startroom": # Yeah, we'll just go ahead and ignore that success = 1 if success == 0: game.Log("Can't move item '" + self.name + "', location '" + location + "' does not exist.") return self.location else: return self.location
def Get(word): word = word.lower() # Usage: object objects object object objectdirectory objects object. Seriously, I need to think of more distinguishing variable names. try: try: if type(objects[directory.index(word)]) is str: return Get(objects[directory.index(word)]) # Wow, that's a painful-looking line of code else: try: return objects[directory.index(word)] except: return None except: try: return objects[directory.index(word)] except: return None except: game.Log("Verb '"+word+"' does not exist.")
def StartGame(): global story import dialogue dialogue.Init() # Loads the dialogue module -- needs game.softdata to be complete first, so this is called by the client exec Game().storyscripts in globals() story = StoryScripts() if not game.Settings().Started(): intro = Game().Intro() + "\n<center><b><big>" + Game().Name() + "</big></b>" if Game().Author() != "Unknown": intro += "<br/>By " + Game().Author() intro += "</center>\n" + scripts.Go(Game().StartRoom()) + "\n(For instructions on how to play, type 'help' and hit enter)" Game().Started(True) Game().Response(intro) if Game().Response().find("{") is not -1: try: Game().Response(SplitCommand(Game().Response())) except: game.Log("Error with splitting a command.")
def Init(): try: xmldoc = parse(game.softdata["gamepath"] + game.softdata["storypath"] + "/" + game.softdata["storyfile"] + ".dialogue.xml") # parse an XML file by name for i in xmldoc.getElementsByTagName('conversation'): if i.tagName == "conversation": ProcessOptions(i) xmldoc.unlink() except expat.ExpatError: print "\n'dialogue.xml' contains an XML formatting error\n" raise except IOError: game.Log("Failed to read file '" + game.softdata["gamepath"] + game.softdata["storyfile"] + ".dialogue.xml'. Disregard this if you don't need it.") except: print "\nSomething went wrong:\n" raise
def RemoveAction(self, verb, idobject = None): if type(verbs.objects[verbs.directory.index(verb)]) is str: verb = verbs.objects[verbs.directory.index(verb)] try: matches = Indices(self.verbs, verb) foundmatch = False for i in matches: if self.idobjects[i] == idobject: foundmatch = i if foundmatch is not False: # Match found -- delete it del self.actions[foundmatch] del self.verbs[foundmatch] del self.idobjects[foundmatch] return True except: # No matches game.Log("Failed to remove action") return False
def Get(word, adj = None): word = word.lower() try: try: matches = Indices(directory, word) nearmatches = [] for i in matches: if type(objects[i]) is not str: return objects[i] else: nearmatches.append(Get(objects[i])) if adj: # Hmm, we don't have any direct hits. Let's check to see if there's an adjective. for i in nearmatches: if i.adjective is adj: return i return nearmatches[0] # There's no way of knowing what object this was supposed to be. I give up. Just take whatever. except: try: return objects[directory.index(word)] except: return None except: game.Log("Item '"+word+"' does not exist.")
def SplitCommand(command): # Takes a command contained in curly braces and processes it, replacing the curly-braced code with the returned result. silent = False split1 = command.partition("{") split2 = split1[2].partition("}") command = split2[0] if command[0] == "~": silent = True command = command[1:] try: exec "response = (" + command + ")" except: response = "(Oops, it seems we had an error. You probably shouldn't try doing that again.)" game.Log("Broken command: (" + command + ")") if response is not None and not silent: combine = str(split1[0]) + str(response) + str(split2[2]) else: combine = str(split1[0]) + str(split2[2]) if combine.find("{") is -1: return combine else: return SplitCommand(combine)
def ProcessInput(input): # Takes the user's input, chews it up, and tries to execute it as intelligently as possible. output = "" input = input.lower() Game().Track(input) for i in ["the", "a", "an"]: input = input.replace(" "+i+" ", " ") for i in ["?", ".", ",", "!", "\"", "\'"]: # Currently disallows multiple commands input = input.replace(i, " ") if Game().capturemode is False: words = input.split() try: Game().current["verb"] = words[0] except: Game().current["verb"] = None if (RequiresIDO(Game().current["verb"], words) or FindIndex(directobjectlist, words)) and len(words) > 3: # Do we need an indirect object? # This isn't a particularly intelligent way of finding the indirect object, but it seems to work fairly well. index = FindIndex(directobjectlist, words) if(index > 2): # Makes sure that the trigger word isn't directly after the verb # Yay, we found one of the trigger words! try: Game().current["idobject"] = words[len(words)-1] Game().current["idobjectadj"] = words[len(words)-2] Game().current["object"] = words[index-2] Game().current["objectadj"] = words[index-3] except: Game().current["idobject"] = None Game().current["object"] = words[len(words)-1] Game().current["objectadj"] = words[len(words)-2] game.Log("Missing an indirect object") else: Game().current["idobject"] = None Game().current["object"] = words[len(words)-1] Game().current["objectadj"] = words[len(words)-2] # Just guessing -- if this isn't it, we'll try harder later.. game.Log("Missing an indirect object") else: # We don't need an indirect object, let's just try to grab the object from the end of the command. if len(words) > 1: Game().current["object"] = words[len(words)-1] Game().current["objectadj"] = words[len(words)-2] # Just guessing -- if this isn't it, we'll try harder later.. else: Game().current["object"] = None Game().current["idobject"] = None if Game().current["object"] in ["it", "them", "him", "her"]: Game().current["object"] = Game().Salience() output += "(" + (Item(Game().Current("object")).Grammar("the") + " " + Item(Game().Current("object")).Description("short") + ")").strip() + "\n" else: if items.Disambig("object") is not None: return items.Disambig("object") if items.Disambig("idobject") is not None: return items.Disambig("idobject") try: Game().current["object"] = Item(Game().Current("object")).name # Quick alternate name disambiguation, should make things easier further in Game().Salience(Game().Current("object")) except: pass try: Game().current["idobject"] = Item(Game().current["idobject"]).name # Quick alternate name disambiguation, should make things easier further in except: pass try: if (Game().Current("verb") in verbignorelist) or ("sys_" in Game().Current("verb")): return Verb(Game().Current("verb")).DefaultResponse() # We're working with files, so let's not bother with all this game logic else: Game().LastCommand(input); # Now we have a verb, noun, and possibly an indirect object. Here, we can finally process it. if Verb(Game().Current("verb")) and Item(Game().current["object"]): # We have both a verb and an object! Let's check to see if the object is in the player's location. if Item(Game().current["object"]).Location() == Item("me").Location() or Item(Game().current["object"]).Location() == "me": # Awesome, it is! Let's try to push the verb to the object. output += Item(Game().current["object"]).Do(Verb(Game().current["verb"]).name, Game().current["idobject"]) else: # Well, the object exists, but it's not here. Let's check to see if it's contained somewhere in the room if scripts.PlayerHas(Game().current["object"], True): output += Item(Game().current["object"]).Do(Verb(Game().current["verb"]).name, Game().current["idobject"]) elif Item(Game().current["object"]).Location() in locations.Get(Item("me").Location()).contents: output += "You can't reach " output += Item(Game().current["object"]).Grammar("the") + " " + Item(Game().current["object"]).Description("short") + "." else: output += "You don't see " output += Item(Game().current["object"]).Grammar("a") + " " output += Item(Game().current["object"]).Description("short")+" here." else: # Either the verb or the object doesn't exist. Let's just fall back on the verb. Have I ever mentioned that I love the command "raise"? It's so simple. raise except: try: if Verb(Game().Current("verb")) and Game().Current("object"): if Game().Current("object") in directionlist: # It's okay, ma'am. We know this object. output += Verb(Game().Current("verb")).DefaultResponse() else: # There's an object, but it doesn't exist. return "I'm not sure what you mean by '" + Game().Current("object") + "'." elif Verb(Game().current["verb"]): # Hmm, we only seem to have a verb. We'll help if we can. if Verb(Game().current["verb"]).Clarify() is not None: return Verb(Game().current["verb"]).Clarify() else: output += Verb(Game().current["verb"]).DefaultResponse() else: raise except: # I have no idea whatsoever. if Game().Current("verb"): return "I don't understand the word '" + Game().current["verb"] + "'." else: # There's nothing here... return "Excuse me?" if output: # We go a little easier on the player by not adding a turn (and thus processing daemons) if we couldn't understand them Game().AddMove() daemonoutput = "" for i in Game().daemons.keys(): Game().daemons[i].countdown -= 1 if Game().daemons[i].countdown == 0 or Game().daemons[i].countdown == -1: # The -1 is for Daemons set to execute immediately daemonoutput += "\n" + Game().daemons[i].script if Game().daemons[i].repeat == 0: del Game().daemons[i] else: Game().daemons[i].repeat -= 1 Game().daemons[i].countdown = Game().daemons[i].delay+1 return output + daemonoutput else: return "I don't understand." else: Game().capturemode = False return Parse(Game().captureprocess.replace("%input%", input))
def Disambig(objecttype): # Wow, this is an eyesore. This is invoked when the parser can't narrow down the object being referred to by the player to a single item. # Type can be "object" or "idobject" if objecttype == "object" or objecttype == "idobject": try: ambig = Indices(directory, game.Settings().current[objecttype]) if len(ambig) > 1: # Hmm, we have conflicting indirect objects. Let's try to figure out which one's being referred to. possibilities = [] for i in ambig: # Finds objects that are currently in the room if type(objects[i]) is str: if Get(objects[i]).Location() == Get("me").Location() or Get(objects[i]).Location() == "me": possibilities.append({"id": i, "name": Get(objects[i]).name, "adjective": Get(objects[i]).Adjective()}) elif Get(objects[i]).Location() in locations.Get(Get("me").Location()).contents: possibilities.append({"id": i, "name": Get(objects[i]).name, "adjective": Get(objects[i]).Adjective()}) else: if objects[i].Location() == Get("me").Location() or objects[i].Location() == "me": # Rules out anything not in the room possibilities.append({"id": i, "name": objects[i].name, "adjective": objects[i].Adjective()}) elif objects[i].Location() in locations.Get(Get("me").Location()).contents: possibilities.append({"id": i, "name": objects[i].name, "adjective": objects[i].Adjective()}) if len(possibilities) == 0: return "You don't see that here." + daemonoutput # Okay, we have some possibilities. Let's see if any adjectives line up. for i in range(len(possibilities)): if str(game.Settings().current[objecttype+"adj"]) == possibilities[i]["adjective"]: game.Settings().current[objecttype] = possibilities[i]["name"] raise if len(possibilities) == 1: game.Settings().current[objecttype] = possibilities[0]["name"] raise else: captureprompt = "Which " + game.Settings().current[objecttype] + ": " if len(possibilities) == 2: captureprompt += possibilities[0]["adjective"] + " or " + possibilities[1]["adjective"] + "?" else: for i in range(len(possibilities)): if i < len(possibilities)-2: captureprompt += possibilities[i]["adjective"] + ", " captureprompt += possibilities[(len(possibilities)-2)]["adjective"] + " or " + possibilities[(len(possibilities)-1)]["adjective"] + "?" # We'll need to rebuild the input a bit to fit in the new adjective if objecttype == "object": captureprocess = game.Settings().current["verb"] + " %input% " + game.Settings().current["object"] if game.Settings().current["idobject"]: if game.Settings().current["idobjectadj"]: captureprocess += " " + game.Settings().current["idobjectadj"] + " " + game.Settings().current["idobject"] else: captureprocess += " " + game.Settings().current["idobject"] return scripts.Capture(captureprompt, captureprocess) if objecttype == "idobject": captureprocess = game.Settings().current["verb"] if game.Settings().current["objectadj"]: captureprocess += " " + game.Settings().current["objectadj"] captureprocess += " " + game.Settings().current["object"] + " to %input% " + game.Settings().current["idobject"] return scripts.Capture(captureprompt, captureprocess) else: # No more conflicts. return None except: return None else: game.Log("Incorrect object type: '" + type + "'") return None
def error(self, msg): game.Log(game.LOG_LV_ERROR, msg, self.mod)
def warn(self, msg): game.Log(game.LOG_LV_WARNING, msg, self.mod)
def info(self, msg): game.Log(game.LOG_LV_INFO, msg, self.mod)
def debug(self, msg): game.Log(game.LOG_LV_DEBUG, msg, self.mod)