Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new_struct events not firing in file parser #60

Closed
Rakaneth opened this issue Dec 28, 2018 · 3 comments
Closed

new_struct events not firing in file parser #60

Rakaneth opened this issue Dec 28, 2018 · 3 comments
Assignees
Labels

Comments

@Rakaneth
Copy link

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
Copy link
Author

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 HexDecimal added the bug label Dec 28, 2018
@HexDecimal HexDecimal self-assigned this Dec 28, 2018
@HexDecimal
Copy link
Collaborator

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

@Rakaneth
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants