Mercurial > skaapsteker
view scripts/npc-test @ 234:a661b6621ec4
Add testing of triggers and entry / exit functions.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Thu, 07 Apr 2011 11:05:54 +0200 |
parents | 13b912f40a10 |
children | 8dee25fd070d |
line wrap: on
line source
#!/usr/bin/env python "Skaapsteker npc tester" import os.path import sys import optparse from pprint import pprint sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) from skaapsteker.dialogue import DSM, DsmEvent from skaapsteker.gamestate import GameState def run(npc_name, game_json): game = GameState(game_json) npc = getattr(game.world.npcs, npc_name) dsm = DSM(npc_name, game.world, npc.dsm, npc.state) print "States:" print "-------" pprint(dsm.states.keys()) print while True: state = dsm.get_state() print "%s:" % dsm.state, state.text print "--" for i, choice in state.choices: print "%d: %s" % (i, choice) print "L: Leave" print "--" key = raw_input("Choice? ") key = key.strip().upper() if key == "L": break elif key.isdigit(): dsm.choice(int(key)) print "--" def test_all(game_json): game = GameState(game_json) print "Testing NPCs" print "============" for npc_name in game.world.npcs: print "Checking", npc_name, "...", npc = getattr(game.world.npcs, npc_name) dsm = DSM(npc_name, game.world, npc.dsm, npc.state) print " Loaded %s." % (npc.dsm) my_locals = { "world": dsm.world, "state": dsm.states, "npcs": dsm.world.npcs } my_locals.update(DsmEvent().items) for state_name, state in dsm.states.items(): print " Testing triggers for state %s" % state_name for trigger in state.triggers: eval(trigger._matches, {}, my_locals.copy()) eval(trigger._next_state, {}, my_locals.copy()) print " Test on_entry and on_exit for state %s" % state_name state.enter(my_locals) state.leave(my_locals) print "ok" print print "Testing Sprites" print "===============" for level in game.world.levels: print "Checking", level, "...", game.create_sprites(level) print "ok" print def main(): parser = optparse.OptionParser(usage="%prog [options] <npc name>") parser.add_option("--all", action="store_true", default=False, dest="test_all", help="test all NPCs in game") parser.add_option("--game", default="game.json", dest="game", help="game .json file to use") opts, args = parser.parse_args() if opts.test_all: test_all(opts.game) return if len(args) != 1: p.error("Must provide an npc json file") run(args[0], game) if __name__ == '__main__': main()