Ejemplo n.º 1
0
    def load(self, worldobject):
        self.source = worldobject

        if not app.version_at_least(self.source.META["app-version"]):
            raise error.VersionError(app.VERSION, self.source.META["app-version"])

        data = self.source.WORLD
        data = data.replace("\t", "")

        # Filter out comments
        data = core_regex["comment"].sub("", data)

        # Parse globals
        globals, objects = data.split("---")
        for match in core_regex["var"].finditer(globals):
            var, value = match.groups()
            self.vars[var] = value

        objects_with_params = list()
        # Parse and create objects
        # Stage 1: create objects
        for type, id, name, description, params in core_regex["constructor"].findall(objects):
            if not type in primitives.registry:
                # FIXME
                print "Error: Invalid type specified for object."

            newobject = primitives.get(type)(name, description)

            macro = parse_macros(newobject, description)
            if macro:
                newobject.get_description = macro

            if params:
                objects_with_params.append((newobject, params))

            self.objects[id] = newobject

            # Stage 2: set up objects that have additional parameters
        for object, params in objects_with_params:
            for param in constructor_param_regex["param"].findall(params):
                if isinstance(object, primitives.get("ROOM")):
                    match = constructor_param_regex["exit"].match(param)
                    if match:
                        direction, destinationid = match.groups()
                        object.add_exit(direction, self.get_object(destinationid))

                match = constructor_param_regex["var"].match(param)
                if match:
                    var, value = match.groups()

                    # We will use magic to match an (objectid) specifier, a list of (objectid)s, or a python expression.
                    objectmatch = core_regex["object"].match(value)
                    if objectmatch:
                        object.vars[var] = self.get_object(objectmatch.groups()[0])
                    elif core_regex["object_list_pattern"].match(value):
                        object.vars[var] = [
                            self.get_object(objectmatch.groups()[0])
                            for objectmatch in core_regex["object"].finditer(value)
                        ]
                    else:
                        # Otherwise, evaluate a python expression.
                        object.vars[var] = eval(value, vars(self.source))

                match = constructor_param_regex["message"].match(param)
                if match:
                    name, message = match.groups()

                    macro = parse_macros(object, message)
                    if macro:
                        object.messages[name] = macro
                    else:
                        object.messages[name] = message

                match = constructor_param_regex["object"].match(param)
                if match:
                    objectid = match.groups()[0]
                    self.get_object(objectid).move_to(object)

                    # Allow objects to set themselves up after they have been initialized.
        for object in self.objects.itervalues():
            if hasattr(object, "post_init"):
                object.post_init()

        self.protagonist = self.get_object(self.vars["PROTAGONIST"])
        self.protagonist.move_to(self.get_object(self.vars["START"]))
Ejemplo n.º 2
0
 def create(*args):
     self.objects[id] = primitives.get(type)(*args)
     return self.objects[id]