annotate 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
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)
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
17
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
18 print "Testing ..."
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
19 print "==========="
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
20 test_npc(game, npc_name)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
21 print
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
22
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
23 if not interact:
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
24 return
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
25
222
2cbeeef5867c Fix npc tester (again).
Simon Cross <hodgestar@gmail.com>
parents: 196
diff changeset
26 npc = getattr(game.world.npcs, npc_name)
2cbeeef5867c Fix npc tester (again).
Simon Cross <hodgestar@gmail.com>
parents: 196
diff changeset
27 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
28
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
29 print "States:"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
30 print "-------"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
31 pprint(dsm.states.keys())
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
32 print
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
33
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
34 while True:
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
35 state = dsm.get_state()
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
36 print "%s:" % dsm.state, state.text
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
37 print "--"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
38 for i, choice in state.choices:
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
39 print "%d: %s" % (i, choice)
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
40 print "L: Leave"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
41 print "--"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
42
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
43 key = raw_input("Choice? ")
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
44 key = key.strip().upper()
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
45 if key == "L":
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
46 break
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
47 elif key.isdigit():
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
48 dsm.choice(int(key))
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
49
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
50 print "--"
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
51
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
52
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
53 def test_npc(game, npc_name):
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
54 """Test one npc."""
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
55 print "Checking", npc_name, "...",
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
56 npc = getattr(game.world.npcs, npc_name)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
57 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
58 print " Loaded %s." % (npc.dsm)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
59
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
60 my_locals = { "world": dsm.world, "state": dsm.states, "npcs": dsm.world.npcs }
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
61 my_locals.update(DsmEvent().items)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
62 for state_name, state in dsm.states.items():
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
63 print " Testing triggers for state %s" % state_name
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
64 for trigger in state.triggers:
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
65 eval(trigger._matches, {}, my_locals.copy())
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
66 eval(trigger._next_state, {}, my_locals.copy())
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
67 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
68 state.enter(my_locals)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
69 state.leave(my_locals)
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
70 print "ok"
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
71
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
72
227
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
73 def test_all(game_json):
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
74 game = GameState(game_json)
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
75
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
76 print "Testing NPCs"
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
77 print "============"
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
78 for npc_name in game.world.npcs:
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
79 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
80 print
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
81
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
82 print "Testing Sprites"
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
83 print "==============="
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
84 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
85 print "Checking", level, "...",
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
86 game.create_sprites(level)
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
87 print "ok"
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
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
90
153
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
91 def main():
227
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
100 opts, args = parser.parse_args()
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
101
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
102 if opts.test_all:
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
103 test_all(opts.game)
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
104 return
13b912f40a10 Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents: 222
diff changeset
105
153
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
106 if len(args) != 1:
249
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 246
diff changeset
107 parser.error("Must provide an npc json file")
281
9e6ff3eb63d6 Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 249
diff changeset
108 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
109
704d23022f09 Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
110 if __name__ == '__main__':
195
445a28f4b38e Fix npc tester.
Simon Cross <hodgestar@gmail.com>
parents: 153
diff changeset
111 main()