Mercurial > skaapsteker
view scripts/npc-test @ 431:b753ea36909e
Fix the bug Anna found.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 18:46:28 +0200 |
parents | 78220c989e6a |
children | fbae2da0c3c0 |
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, interact): game = GameState(game_json) print "Testing ..." print "===========" test_npc(game, npc_name) print if not interact: return 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_npc(game, npc_name): """Test one npc.""" 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) def test_switch_to(npc_name): assert npc_name in game.world.npcs my_locals = { "world": dsm.world, "state": dsm.states, "npcs": dsm.world.npcs, "switch_to": test_switch_to, } 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" def test_all(game_json): game = GameState(game_json) print "Testing NPCs" print "============" for npc_name in game.world.npcs: test_npc(game, npc_name) 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("--interact", "-i", action="store_true", default=False, dest="interact", help="enable interactive session with npc after testing") 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: parser.error("Must provide an npc json file") run(args[0], opts.game, opts.interact) if __name__ == '__main__': main()