libtcod / python-tcod

A high-performance Python port of libtcod. Includes the libtcodpy module for backwards compatibility with older projects.
BSD 2-Clause "Simplified" License
410 stars 36 forks source link

new_struct events not firing in file parser #60

Closed Rakaneth closed 5 years ago

Rakaneth commented 5 years ago

I'm using Python 3.7.2 and trying to use the file parser (I am porting from a C++ version of the same game and already have data files written in that format). I have defined my custom listener:

class CreatureParser:
    def __init__(self):
        self.temp = CreatureTemplate()

    def new_struct(self, struct, name):
        if tcod.struct_get_name(struct) == 'creature':
            print(f'New creature: {name}')
            self.temp = CreatureTemplate()
        return True

    def new_flag(self, name):
        return True

    def new_property(self, name, typ, value):
        stat_names = ['str', 'stam', 'spd', 'skl', 'sag', 'smt']
        if name == 'name':
            self.temp.name = value
        elif name == 'type':
            self.temp.type = value
        elif name == 'desc':
            self.temp.desc = value
        elif name == 'unarmed':
            self.temp.unarmed = value
        elif name == 'glyph':
            self.temp.glyph = value
        elif name in stat_names:
            self.temp.stats[name] = value
        elif name == 'col':
            self.temp.color = value
        elif name == 'tags':
            self.temp.tags = value
        elif name == 'startItems':
            self.temp.start_tems = value
        return True

    def end_struct(self, struct, name):
        CREATURE_TEMPLATES[name] = self.temp
        return True

    def error(self, msg):
        print(f'Error parsing creature file: {msg}')
        return False

My data file looks like this:

creature "wolf" {
    name="wolf"
    type="wolf"
    desc="A large, grey wolf."
    unarmed="fangs"
    glyph='W'
    col="127,101,63"
    stats {
        spd=15
        str=15
        vision=10
    }
    tags=["animal", "wolf"]
}

creature "zombie" {
    name="zombie"
    type="undead"
    desc="Once a man, now a shambling zombie."
    unarmed="hands"
    glyph='U'
    stats {
        str=20
        skl=5
        vision=4
    }
    tags=["undead"]
}

The new_struct function is not getting called, though everything else is.

Rakaneth commented 5 years ago

I fixed this in my local copy by updating line 2523 in libtcodpy.py:

def _pycall_parser_new_struct(struct, name):
    return _parser_listener.end_struct(struct, _unpack_char_p(name))

I updated this to:

def _pycall_parser_new_struct(struct, name):
    return _parser_listener.new_struct(struct, _unpack_char_p(name))
HexDecimal commented 5 years ago

Sorry for that. The parser rarely gets used from Python. I'll deploy a fix today.

Rakaneth commented 5 years ago

Indeed - like I said, I've got data files from a C++ version of the game and it's about as much effort writing the parsers as it is to convert the data files to something suitable in Python.