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 Take(item): if item == "all": itemlist = [] taken = 0 output = "" for i in locations.Get(items.Get("me").Location()).contents: if items.Get(i).canTake is True and i != "me": if items.Get(i).Adjective() is not None: itemlist.append( items.Get(i).Adjective().capitalize() + " " + items.Get(i).description["short"] + ": " + items.Get(i).Do("get")) else: itemlist.append( items.Get(i).description["short"].capitalize() + ": " + items.Get(i).Do("get")) if len(itemlist) > 0: for i in itemlist: output += i + "\n" return output else: return "There's nothing here to take." elif game.Settings().Current("object") in [ "up", "u", "down", "d", "in", "out" ]: Go(game.Settings().Current("object")) elif items.Get(item).Location() == "me": return "You're already carrying it." elif items.Get(item).canTake is True and PlayerHas(item, True): items.Get(item).Location("me") items.Get(item).Description("initial", None) return "Taken." else: # Okay, the item isn't in the room, it isn't "all", and we're not already carrying it. Let's check the other objects in the room to see if they have the object. return "You can't take that."
def Touch(what=None): if what is None: if locations.Get( items.Get("me").Location()).description["touch"] is not None: return locations.Get( items.Get("me").Location()).description["touch"] else: return "You don't feel anything unusual." else: try: if items.Get(what).Location() == items.Get( "me").Location() or items.Get(what).Location() == "me": if items.Get(what).description["touch"] is None: return "You don't feel anything unusual." else: return items.Get(what).description["touch"] except: return "You don't feel anything unusual."
def Listen(what=None): if what is None: if locations.Get( items.Get("me").Location()).description["sound"] is not None: return locations.Get( items.Get("me").Location()).description["sound"] else: return "You don't hear anything unusual." else: try: if items.Get(what).Location() == items.Get( "me").Location() or items.Get(what).Location() == "me": if items.Get(what).description["sound"] is None: return "It's quiet." else: return items.Get(what).description["sound"] except: return "You don't hear anything unusual."
def Look(what=None, justcontents=False): import re output = "" if what is None: return locations.Get(items.Get("me").Location()).Describe() else: if not justcontents: if items.Get(what).description["long"] is None: output = "There doesn't seem to be anything interesting about " + items.Get( what).Grammar("it") + "." else: output = items.Get(what).description["long"] if items.Get(what).contents and items.Get(what).isContainer and ( items.Get(what).isOpen is not True): output += " It's closed." elif items.Get(what).contents and items.Get( what).isContainer and items.Get(what).isOpen and items.Get( what).announceContents and what != "me": contents = "" initial = "" for i in items.Get(what).contents: if i is not "all" and not items.Get(i).Description( "initial") and not items.Get(i).isSilent: contents += " " + items.Get(i).Grammar( "a") + " " + items.Get(i).Description("short") + ", " if items.Get(i).Description("initial"): initial += "\n" + items.Get(i).Description("initial") if contents: output += " It contains" + Listify(contents) + "." if initial: output += initial if items.Get(what).contents and items.Get( what).isSurface and items.Get(what).announceContents: contents = "" initial = "" for i in items.Get(what).contents: if not items.Get(i).Description("initial") and not items.Get( i).isSilent: contents += " " + items.Get(i).Grammar("a") + " " if items.Get(i).Adjective() is not None: contents += items.Get(i).Adjective() + " " contents += items.Get(i).Description("short") + ", " if items.Get(i).Description("initial"): initial += "\n" + items.Get(i).Description("initial") if contents: output += " Here, you find" + Listify(contents) + "." if initial: output += initial p = re.compile(" a ") for i in p.finditer(output): if output[i.end()] in ["a", "e", "i", "o", "u"]: return output[:(i.start() + 1)] + "an " + output[i.end():] return output.replace(" .", ".")
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 Location(location): return locations.Get(location) def Verb(verb): return verbs.Get(verb)
def Go(direction=None): output = "" if direction is None and game.Settings().Current("object"): direction = game.Settings().Current("object") if direction == "enter": direction = "i" if direction == "exit": direction = "o" if direction in ["forward", "forwards"]: direction = "f" if direction in ["back", "backwards"]: direction = "b" direction = direction[0] elif direction is None and game.Settings().Current("verb") == "climb": direction = "u" elif direction is None: direction = "f" if direction in [ "n", "s", "e", "w", "u", "d", "i", "o", "l", "r", "f", "b" ]: if direction == "f": if game.Settings().Facing() in ["n", "u", "d"]: output += "\n(north)" elif game.Settings().Facing() == "s": output += "\n(south)" elif game.Settings().Facing() == "e": output += "\n(east)" elif game.Settings().Facing() == "w": output += "\n(west)" direction = game.Settings().Facing() if direction == "b": if game.Settings().Facing() == "n": direction = "s" output += "\n(south)" elif game.Settings().Facing() == "s": direction = "n" output += "\n(north)" elif game.Settings().Facing() == "e": direction = "w" output += "\n(west)" elif game.Settings().Facing() == "w": direction = "e" output += "\n(east)" elif game.Settings().Facing() == "u": direction = "d" output += "\n(down)" elif game.Settings().Facing() == "d": direction = "u" output += "\n(up)" elif game.Settings().Facing() == "i": direction = "o" output += "\n(outt)" elif game.Settings().Facing() == "o": direction = "i" output += "\n(in)" else: direction = "s" output += "\n(south)" if direction == "l": if game.Settings().Facing() == "n": direction = "w" output += "\n(west)" elif game.Settings().Facing() == "s": direction = "e" output += "\n(east)" elif game.Settings().Facing() == "e": direction = "n" output += "\n(north)" elif game.Settings().Facing() == "w": direction = "s" output += "\n(south)" else: direction = "w" output += "\n(west)" if direction == "r": if game.Settings().Facing() == "n": direction = "e" output += "\n(east)" elif game.Settings().Facing() == "s": direction = "w" output += "\n(west)" elif game.Settings().Facing() == "e": direction = "s" output += "\n(south)" elif game.Settings().Facing() == "w": direction = "n" output += "\n(north)" else: direction = "e" output += "\n(east)" currentlocation = items.Get("me").Location() try: # Check to see if everything is blocked return locations.Get(currentlocation).blockedconnections["a"] except: # So far so good try: # Check to see if that direction is blocked return locations.Get( currentlocation).blockedconnections[direction] except: # Coast is clear! try: if locations.Get(currentlocation).Description( "exit") is not None: output += "\n" + locations.Get( currentlocation).Description("exit") items.Get("me").Location( locations.Get(currentlocation).connections[direction]) for i in locations.Get(currentlocation).contents: if items.Get(i).followPlayer is True: items.Get(i).Location(items.Get("me").Location()) if items.Get(i).Description("follow") is not None: output += "\n" + items.Get(i).Description( "follow") output += "\n" + Look() if locations.Get(items.Get("me").Location()).Description( "firstenter") is not None: output += "\n" + locations.Get( items.Get("me").Location()).Description( "firstenter") locations.Get(items.Get("me").Location()).Description( "firstenter", "") if locations.Get(items.Get("me").Location()).Description( "enter") is not None: output += "\n" + locations.Get( items.Get("me").Location()).Description("enter") game.Settings().Facing(direction) return output except: # raise return "You can't go in that direction." else: try: currentlocation = items.Get("me").Location() items.Get("me").Location(locations.Get(direction).name) if currentlocation: if locations.Get(currentlocation).Description( "exit") is not None: output += locations.Get(currentlocation).Description( "exit") + "\n" for i in locations.Get(currentlocation).contents: if items.Get(i).followPlayer is True: items.Get(i).Location(items.Get("me").Location()) if items.Get(i).Description("follow") is not None: output += "\n" + items.Get(i).Description("follow") output += "\n" + Look() if locations.Get(items.Get("me").Location()).Description( "firstenter") is not None: output += locations.Get(items.Get( "me").Location()).Description("firstenter") + "\n" locations.Get(items.Get("me").Location()).Description( "firstenter", "") if locations.Get(items.Get("me").Location()).Description( "enter") is not None: output += locations.Get( items.Get("me").Location()).Description("enter") + "\n" game.Settings().Facing("n") return output except: return "You can't go in that direction."
def Location(location): return locations.Get(location)
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 Location(location): return locations.Get(location) def Game(): return game.Settings()