Mercurial > skaapsteker
annotate scripts/npc-test @ 361:75dc259b41f5
Haiku data.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 14:22:04 +0200 |
parents | 78220c989e6a |
children | fbae2da0c3c0 |
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 | 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 | 26 npc = getattr(game.world.npcs, npc_name) |
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 |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
60 def test_switch_to(npc_name): |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
61 assert npc_name in game.world.npcs |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
62 |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
63 my_locals = { |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
64 "world": dsm.world, |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
65 "state": dsm.states, |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
66 "npcs": dsm.world.npcs, |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
67 "switch_to": test_switch_to, |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
68 } |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
69 my_locals.update(DsmEvent().items) |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
70 |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
71 for state_name, state in dsm.states.items(): |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
72 print " Testing triggers for state %s" % state_name |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
73 for trigger in state.triggers: |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
74 eval(trigger._matches, {}, my_locals.copy()) |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
75 eval(trigger._next_state, {}, my_locals.copy()) |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
76 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
|
77 state.enter(my_locals) |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
78 state.leave(my_locals) |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
79 print "ok" |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
80 |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
81 |
227
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
82 def test_all(game_json): |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
83 game = GameState(game_json) |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
84 |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
85 print "Testing NPCs" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
86 print "============" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
87 for npc_name in game.world.npcs: |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
88 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
|
89 print |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
90 |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
91 print "Testing Sprites" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
92 print "===============" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
93 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
|
94 print "Checking", level, "...", |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
95 game.create_sprites(level) |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
96 print "ok" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
97 print |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
98 |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
99 |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
100 def main(): |
227
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
101 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
|
102 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
109 opts, args = parser.parse_args() |
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 if opts.test_all: |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
112 test_all(opts.game) |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
113 return |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
114 |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
115 if len(args) != 1: |
249 | 116 parser.error("Must provide an npc json file") |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
117 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
|
118 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
119 if __name__ == '__main__': |
195 | 120 main() |