+ newlist = self.getlist(facet)
+ newlist.append(value)
+ self.set(facet, newlist)
+
+ def new_event(self, action, when=None):
+ """Create, attach and enqueue an event element."""
+
+ # if when isn't specified, that means now
+ if not when: when = self.universe.get_time()
+
+ # events are elements themselves
+ event = Element("event:" + self.key + ":" + counter)
+
+ def send(self, message, eol="$(eol)", raw=False, flush=False, add_prompt=True, just_prompt=False):
+ """Convenience method to pass messages to an owner."""
+ if self.owner: self.owner.send(message, eol, raw, flush, add_prompt, just_prompt)
+
+ def can_run(self, command):
+ """Check if the user can run this command object."""
+
+ # has to be in the commands category
+ if command not in self.universe.categories["command"].values(): result = False
+
+ # avatars of administrators can run any command
+ elif self.owner and self.owner.account.getboolean("administrator"): result = True
+
+ # everyone can run non-administrative commands
+ elif not command.getboolean("administrative"): result = True
+
+ # otherwise the command cannot be run by this actor
+ else: result = False
+
+ # pass back the result
+ return result
+
+ def update_location(self):
+ """Make sure the location's contents contain this element."""
+ location = self.get("location")
+ if location in self.universe.contents:
+ self.universe.contents[location].contents[self.key] = self
+ def clean_contents(self):
+ """Make sure the element's contents aren't bogus."""
+ for element in self.contents.values():
+ if element.get("location") != self.key:
+ del self.contents[element.key]
+ def go_to(self, location):
+ """Relocate the element to a specific location."""
+ current = self.get("location")
+ if current and self.key in self.universe.contents[current].contents:
+ del universe.contents[current].contents[self.key]
+ if location in self.universe.contents: self.set("location", location)
+ self.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"))
+ self.echo_to_location("You suddenly realize that " + self.get("name") + " is here.")
+ def move_direction(self, direction):
+ """Relocate the element in a specified direction."""
+ self.echo_to_location(self.get("name") + " exits " + self.universe.categories["internal"]["directions"].getdict(direction)["exit"] + ".")
+ self.send("You exit " + self.universe.categories["internal"]["directions"].getdict(direction)["exit"] + ".", add_prompt=False)
+ self.go_to(self.universe.contents[self.get("location")].link_neighbor(direction))
+ self.echo_to_location(self.get("name") + " arrives from " + self.universe.categories["internal"]["directions"].getdict(direction)["enter"] + ".")
+ def look_at(self, key):
+ """Show an element to another element."""
+ if self.owner:
+ element = self.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 self.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)"
+ elif element is not self:
+ message += "$(grn)" + element.get("impression") + "$(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(",")]
+ directions = self.universe.categories["internal"]["directions"]
+ offsets = dict([(x, directions.getdict(x)["vector"]) for x in directions.facets()])
+ 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 self.universe.contents: portals[portal] = neighbor
+ for facet in self.facets():
+ if facet.startswith("link_"):
+ neighbor = self.get(facet)
+ if neighbor in self.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 self.universe.contents[self.get("location")].contents.values():
+ if element is not self: element.send(message)