Example #1
0
    def load(self, filename):
        start_room = None
        first_room = None
        new_room = None
        new_command = None
        cur_room_name = None

        file_stream = file(filename)

        for line in file_stream:
            line = string.translate(string.strip(line), \
                                    string.maketrans('\\', '\n'))

            if line.find("=") != -1:
                keyword, params = line.split("=", 1)
            else:
                keyword = line
                params = None

            if keyword == "TITLE":
                self.title = params[:]

            elif keyword == "START_ROOM":
                start_room = params[:]

            elif keyword == "INVENTORY_LIMIT":
                self.player.item_limit = string.atoi(params)

            elif keyword == "ROOM":
                new_room = Room()
                new_room.name = params[:]
                self.rooms[new_room.name] = new_room
                if first_room == None:
                    first_room = new_room

                cur_room_name = new_room.name

                new_command = None

            elif keyword == "LOCAL":
                cur_room_name = params[:]

                new_room = None
                new_command = None

            elif keyword == "GLOBAL":
                cur_room_name = None

                new_room = None
                new_command = None

            elif keyword == "COMMAND":
                new_command = Command()
                self.commands.append(new_command)

                if cur_room_name != None:
                    new_command.location = cur_room_name[:]

                if params[0] == "+":
                    new_command.condition = params[1:]
                elif params[0] == "-":
                    new_command.condition = params[:]
                else:
                    pos = params.find(":")
                    if pos != -1:
                        if params[pos + 1] == "+":
                            new_command.condition = params[pos + 2:]
                        else:
                            new_command.condition = params[pos + 1:]

                        new_command.commands = params[:pos].split(",")
                    else:
                        new_command.commands = params.split(",")

            elif keyword == "ACTION":
                # If there is no current command, make one.
                if new_command == None:
                    new_command = Command()
                    self.commands.append(new_command)

                    if cur_room_name != None:
                        new_command.location = cur_room_name[:]

                for action in params.split(";"):
                    new_command.add_action(action)
                    #new_command.actions.append(action)

            elif keyword == "DESC":
                if new_room != None:
                    new_room.desc = params[:]

            elif keyword == "ALT_DESC":
                if new_room != None:
                   new_room.desc_alt = params[:]

            elif keyword == "DESC_CONTROL":
                if new_room != None:
                   new_room.desc_ctrl = params[:]

            elif keyword == "CONTENTS":
                if new_room != None:
                   new_room.items = params.split(",")

            elif keyword == "NORTH":
                if new_room != None:
                   new_room.init_neighbor("N", params[:])

            elif keyword == "SOUTH":
                if new_room != None:
                   new_room.init_neighbor("S", params[:])

            elif keyword == "EAST":
                if new_room != None:
                   new_room.init_neighbor("E", params[:])

            elif keyword == "WEST":
                if new_room != None:
                   new_room.init_neighbor("W", params[:])

            elif keyword == "UP":
                if new_room != None:
                   new_room.init_neighbor("U", params[:])

            elif keyword == "DOWN":
                if new_room != None:
                   new_room.init_neighbor("D", params[:])

        if self.rooms.has_key(start_room):
            self.player.current_room = self.rooms[start_room]
        elif first_room != None:
            self.player.current_room = first_room
        else:
            self.player.current_room = Room()
            self.player.current_room.name = "limbo"
            self.player.current_room.desc = \
                "This adventure has no rooms.  You are in limbo!"

        file_stream.close()