+ def append(self, facet, value):
+ """Append value tp a list."""
+ if type(value) is long: value = str(value)
+ elif not type(value) is str: value = repr(value)
+ newlist = self.getlist(facet)
+ newlist.append(value)
+ self.set(facet, newlist)
+ def send(self, message, eol="$(eol)"):
+ """Convenience method to pass messages to an owner."""
+ if self.owner: self.owner.send(message, eol)
+ def go_to(self, location):
+ """Relocate the element to a specific location."""
+ current = self.get("location")
+ if current and current in universe.contents[current].contents:
+ del universe.contents[current].contents[self.key]
+ if location in universe.contents: self.set("location", location)
+ universe.contents[location].contents[self.key] = self
+ self.look_at(location)
+ def go_home(self):
+ """Relocate the element to its default location."""
+ self.go_to(self.get("default_location"))
+ def move_direction(self, direction):
+ """Relocate the element in a specified direction."""
+ self.go_to(universe.contents[self.get("location")].link_neighbor(direction))
+ def look_at(self, key):
+ """Show an element to another element."""
+ if self.owner:
+ element = universe.contents[key]
+ message = ""
+ name = element.get("name")
+ if name: message += "$(cyn)" + name + "$(nrm)$(eol)"
+ description = element.get("description")
+ if description: message += description + "$(eol)"
+ portal_list = element.portals().keys()
+ if portal_list:
+ portal_list.sort()
+ message += "$(cyn)[ Exits: " + ", ".join(portal_list) + " ]$(nrm)$(eol)"
+ for element in universe.contents[self.get("location")].contents.values():
+ if element.getboolean("is_actor") and element is not self:
+ message += "$(yel)" + element.get("name") + " is here.$(nrm)$(eol)"
+ self.send(message)
+ def portals(self):
+ """Map the portal directions for a room to neighbors."""
+ portals = {}
+ if match("""^location:-?\d+,-?\d+,-?\d+$""", self.key):
+ coordinates = [(int(x)) for x in self.key.split(":")[1].split(",")]
+ offsets = {
+ "down": (0,0,-1),
+ "east": (1,0,0),
+ "north": (0,1,0),
+ "south": (0,-1,0),
+ "up": (0,0,1),
+ "west": (-1,0,0)
+ }
+ for portal in self.getlist("gridlinks"):
+ adjacent = map(lambda c,o: c+o, coordinates, offsets[portal])
+ neighbor = "location:" + ",".join([(str(x)) for x in adjacent])
+ if neighbor in universe.contents: portals[portal] = neighbor
+ for facet in self.facets():
+ if facet.startswith("link_"):
+ neighbor = self.get(facet)
+ if neighbor in universe.contents:
+ portal = facet.split("_")[1]
+ portals[portal] = neighbor
+ return portals
+ def link_neighbor(self, direction):
+ """Return the element linked in a given direction."""
+ portals = self.portals()
+ if direction in portals: return portals[direction]
+ def echo_to_location(self, message):
+ """Show a message to other elements in the current location."""
+ for element in universe.contents[self.get("location")].contents.values():
+ if element is not self: element.send(message)