comparison scripts/npc-test @ 281:9e6ff3eb63d6

Enable testing of individual NPCs.
author Simon Cross <hodgestar@gmail.com>
date Fri, 08 Apr 2011 20:11:45 +0200
parents 30ae3c681507
children 78220c989e6a
comparison
equal deleted inserted replaced
280:849e59e919af 281:9e6ff3eb63d6
10 10
11 from skaapsteker.dialogue import DSM, DsmEvent 11 from skaapsteker.dialogue import DSM, DsmEvent
12 from skaapsteker.gamestate import GameState 12 from skaapsteker.gamestate import GameState
13 13
14 14
15 def run(npc_name, game_json): 15 def run(npc_name, game_json, interact):
16 game = GameState(game_json) 16 game = GameState(game_json)
17
18 print "Testing ..."
19 print "==========="
20 test_npc(game, npc_name)
21 print
22
23 if not interact:
24 return
25
17 npc = getattr(game.world.npcs, npc_name) 26 npc = getattr(game.world.npcs, npc_name)
18 dsm = DSM(npc_name, game.world, npc.dsm, npc.state) 27 dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
19 28
20 print "States:" 29 print "States:"
21 print "-------" 30 print "-------"
39 dsm.choice(int(key)) 48 dsm.choice(int(key))
40 49
41 print "--" 50 print "--"
42 51
43 52
53 def test_npc(game, npc_name):
54 """Test one npc."""
55 print "Checking", npc_name, "...",
56 npc = getattr(game.world.npcs, npc_name)
57 dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
58 print " Loaded %s." % (npc.dsm)
59
60 my_locals = { "world": dsm.world, "state": dsm.states, "npcs": dsm.world.npcs }
61 my_locals.update(DsmEvent().items)
62 for state_name, state in dsm.states.items():
63 print " Testing triggers for state %s" % state_name
64 for trigger in state.triggers:
65 eval(trigger._matches, {}, my_locals.copy())
66 eval(trigger._next_state, {}, my_locals.copy())
67 print " Test on_entry and on_exit for state %s" % state_name
68 state.enter(my_locals)
69 state.leave(my_locals)
70 print "ok"
71
72
44 def test_all(game_json): 73 def test_all(game_json):
45 game = GameState(game_json) 74 game = GameState(game_json)
46 75
47 print "Testing NPCs" 76 print "Testing NPCs"
48 print "============" 77 print "============"
49 for npc_name in game.world.npcs: 78 for npc_name in game.world.npcs:
50 print "Checking", npc_name, "...", 79 test_npc(game, npc_name)
51 npc = getattr(game.world.npcs, npc_name)
52 dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
53 print " Loaded %s." % (npc.dsm)
54
55 my_locals = { "world": dsm.world, "state": dsm.states, "npcs": dsm.world.npcs }
56 my_locals.update(DsmEvent().items)
57 for state_name, state in dsm.states.items():
58 print " Testing triggers for state %s" % state_name
59 for trigger in state.triggers:
60 eval(trigger._matches, {}, my_locals.copy())
61 eval(trigger._next_state, {}, my_locals.copy())
62 print " Test on_entry and on_exit for state %s" % state_name
63 state.enter(my_locals)
64 state.leave(my_locals)
65 print "ok"
66 print 80 print
67 81
68 print "Testing Sprites" 82 print "Testing Sprites"
69 print "===============" 83 print "==============="
70 for level in game.world.levels: 84 for level in game.world.levels:
76 90
77 def main(): 91 def main():
78 parser = optparse.OptionParser(usage="%prog [options] <npc name>") 92 parser = optparse.OptionParser(usage="%prog [options] <npc name>")
79 parser.add_option("--all", action="store_true", default=False, 93 parser.add_option("--all", action="store_true", default=False,
80 dest="test_all", help="test all NPCs in game") 94 dest="test_all", help="test all NPCs in game")
95 parser.add_option("--interact", "-i", action="store_true", default=False,
96 dest="interact", help="enable interactive session with npc after testing")
81 parser.add_option("--game", default="game.json", 97 parser.add_option("--game", default="game.json",
82 dest="game", help="game .json file to use") 98 dest="game", help="game .json file to use")
83 99
84 opts, args = parser.parse_args() 100 opts, args = parser.parse_args()
85 101
87 test_all(opts.game) 103 test_all(opts.game)
88 return 104 return
89 105
90 if len(args) != 1: 106 if len(args) != 1:
91 parser.error("Must provide an npc json file") 107 parser.error("Must provide an npc json file")
92 run(args[0], opts.game) 108 run(args[0], opts.game, opts.interact)
93 109
94 if __name__ == '__main__': 110 if __name__ == '__main__':
95 main() 111 main()