Esempio n. 1
0
def getDict():
    """Return a dictionary mapping flag abbreviations to flag classes"""
    import BZFlag.Flag.List
    return Util.getSubclassDict(BZFlag.Flag.List, FlagBase, 'abbreviation')
Esempio n. 2
0
def getMessageDict(module):
    """Return a dictionary mapping message IDs to message classes,
       given a module containing Message subclasses.
       """
    return Util.getSubclassDict(module, Message, 'messageId')
Esempio n. 3
0
def Text(name,
         size       = Scale.WorldSize,
         wallHeight = Scale.WallHeight,
         ):
    """
    Load a world from a text file. This is the default world format.

    Positional arguments:
        name : File or URI containing the world

    Keyword arguments:
        size       : Default distance along each side of the generated square world
        wallHeight : Height of the world's walls
    """
    import re
    
    f = Util.autoFile(name)
    w = World()
    section = None
    sectionDict = Util.getSubclassDict(WorldObjects, WorldObjects.WorldObject,
                                       'textName', 'textSectionDict')

    # We won't actually add the objects to the world until later,
    # since we need to start the world out with walls and a game style
    # block, but we might get the information needed for those at any
    # point in the file.
    w.erase()
    blocks = []
        
    for line in f:
        # If this is a kludge used by map editors to store extra
        # attributes. Don't process any comments on the line.
        if not line.startswith("#!"):
            line = re.sub("#.*", "", line)
        line = line.strip()
        if line:
            if section:
                # We're inside a section. Accumulate lines until 'end'
                sectionLines.append(line)
                if line == 'end':
                    # Done with this section, process it.

                    if section == 'world':
                        # World information
                        for line in sectionLines:
                            tokens = re.split("\s+", line)
                            keyword = tokens[0].lower()
                            args = tokens[1:]
                            if keyword == 'size':
                                size = int(args[0])

                    else:
                        # Assume all other sections are world objects
                        try:
                            cls = sectionDict[section]
                        except KeyError:
                            raise Errors.ProtocolError(
                                "World file contains unknown section type '%s'" % section)
                        inst = cls()
                        inst.textRead(sectionLines)
                        blocks.append(inst)
                        
                    section = None
            elif not line.startswith("#!"):
                # We're beginning a section
                section = line.lower()
                if section.find(" ") >= 0:
                    raise Errors.ProtocolError("Unexpected whitespace within section name")
                sectionLines = []

    if section:
        raise Errors.ProtocolError("World file has unterminated '%s' section" % section)

    # Now store all the blocks in our world
    w.storeSkeletonHeader(size, wallHeight)
    for block in blocks:
        w.storeBlock(block)
    w.storeSkeletonFooter()
    w.postprocess()
    return w