annotate scripts/npc-test @ 458:fbae2da0c3c0

Fix npc-test to track changes to GameState.
author Simon Cross <hodgestar@gmail.com>
date Sat, 09 Apr 2011 20:49:52 +0200
parents 78220c989e6a
children 1c05b6c2b971
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
153
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2 "Skaapsteker npc tester"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
3
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
4 import os.path
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
5 import sys
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
6 import optparse
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
7 from pprint import pprint
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
8
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
9 sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
10
196
a4c4e2f34162 Remove DummyWorld since npc-test now uses the real world.
Simon Cross <hodgestar@gmail.com>
parents: 195
diff changeset
11 from skaapsteker.dialogue import DSM, DsmEvent
195
445a28f4b38e Fix npc tester.
Simon Cross <hodgestar@gmail.com>
parents: 153
diff changeset
12 from skaapsteker.gamestate import GameState
153
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
13
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
14
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
15 def run(npc_name, game_json, interact):
227
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
16 game = GameState(game_json)
458
fbae2da0c3c0 Fix npc-test to track changes to GameState.
Simon Cross <hodgestar@gmail.com>
parents: 302
diff changeset
17 game.new_game()
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
18
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
19 print "Testing ..."
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
20 print "==========="
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
21 test_npc(game, npc_name)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
22 print
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
23
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
24 if not interact:
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
25 return
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
26
222
2cbeeef5867c Fix npc tester (again).
Simon Cross <hodgestar@gmail.com>
parents: 196
diff changeset
27 npc = getattr(game.world.npcs, npc_name)
2cbeeef5867c Fix npc tester (again).
Simon Cross <hodgestar@gmail.com>
parents: 196
diff changeset
28 dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
153
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
29
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
30 print "States:"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
31 print "-------"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
32 pprint(dsm.states.keys())
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
33 print
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
34
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
35 while True:
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
36 state = dsm.get_state()
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
37 print "%s:" % dsm.state, state.text
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
38 print "--"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
39 for i, choice in state.choices:
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
40 print "%d: %s" % (i, choice)
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
41 print "L: Leave"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
42 print "--"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
43
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
44 key = raw_input("Choice? ")
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
45 key = key.strip().upper()
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
46 if key == "L":
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
47 break
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
48 elif key.isdigit():
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
49 dsm.choice(int(key))
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
50
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
51 print "--"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
52
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
53
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
54 def test_npc(game, npc_name):
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
55 """Test one npc."""
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
56 print "Checking", npc_name, "...",
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
57 npc = getattr(game.world.npcs, npc_name)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
58 dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
59 print " Loaded %s." % (npc.dsm)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
60
302
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
61 def test_switch_to(npc_name):
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
62 assert npc_name in game.world.npcs
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
63
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
64 my_locals = {
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
65 "world": dsm.world,
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
66 "state": dsm.states,
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
67 "npcs": dsm.world.npcs,
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
68 "switch_to": test_switch_to,
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
69 }
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
70 my_locals.update(DsmEvent().items)
302
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 281
diff changeset
71
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
72 for state_name, state in dsm.states.items():
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
73 print " Testing triggers for state %s" % state_name
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
74 for trigger in state.triggers:
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
75 eval(trigger._matches, {}, my_locals.copy())
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
76 eval(trigger._next_state, {}, my_locals.copy())
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
77 print " Test on_entry and on_exit for state %s" % state_name
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
78 state.enter(my_locals)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
79 state.leave(my_locals)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
80 print "ok"
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
81
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
82
227
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
83 def test_all(game_json):
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
84 game = GameState(game_json)
458
fbae2da0c3c0 Fix npc-test to track changes to GameState.
Simon Cross <hodgestar@gmail.com>
parents: 302
diff changeset
85 game.new_game()
227
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
86
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
87 print "Testing NPCs"
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
88 print "============"
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
89 for npc_name in game.world.npcs:
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
90 test_npc(game, npc_name)
227
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
91 print
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
92
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
93 print "Testing Sprites"
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
94 print "==============="
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
95 for level in game.world.levels:
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
96 print "Checking", level, "...",
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
97 game.create_sprites(level)
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
98 print "ok"
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
99 print
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
100
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
101
153
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
102 def main():
227
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
103 parser = optparse.OptionParser(usage="%prog [options] <npc name>")
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
104 parser.add_option("--all", action="store_true", default=False,
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
105 dest="test_all", help="test all NPCs in game")
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
106 parser.add_option("--interact", "-i", action="store_true", default=False,
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
107 dest="interact", help="enable interactive session with npc after testing")
227
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
108 parser.add_option("--game", default="game.json",
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
109 dest="game", help="game .json file to use")
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
110
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
111 opts, args = parser.parse_args()
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
112
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
113 if opts.test_all:
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
114 test_all(opts.game)
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
115 return
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
116
153
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
117 if len(args) != 1:
249
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 246
diff changeset
118 parser.error("Must provide an npc json file")
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
119 run(args[0], opts.game, opts.interact)
153
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
120
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
121 if __name__ == '__main__':
195
445a28f4b38e Fix npc tester.
Simon Cross <hodgestar@gmail.com>
parents: 153
diff changeset
122 main()