Mercurial > skaapsteker
annotate scripts/npc-test @ 596:a1ca84c797fb
Drop trailing colon
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 10 Apr 2011 19:44:20 +0200 |
parents | 5a8a7f17597d |
children |
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) |
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 | 27 npc = getattr(game.world.npcs, npc_name) |
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() |
483
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
37 text = state.text |
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
38 if text is not None: |
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
39 text = text.encode('utf-8') |
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
40 print "%s:" % dsm.state, text |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
41 print "--" |
521
5a8a7f17597d
Add support for 'if': '...' to dialogue choices.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
42 for i, choice in dsm.choices(): |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
43 print "%d: %s" % (i, choice) |
483
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
44 if state.auto_next: |
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
45 print "N: Next" |
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
46 else: |
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
47 print "L: Leave" |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
48 print "--" |
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 key = raw_input("Choice? ") |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
51 key = key.strip().upper() |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
52 if key == "L": |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
53 break |
483
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
54 if key == "N": |
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
476
diff
changeset
|
55 dsm.auto_next() |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
56 elif key.isdigit(): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
57 dsm.choice(int(key)) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
58 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
59 print "--" |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
60 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
61 |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
62 def test_npc(game, npc_name): |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
63 """Test one npc.""" |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
64 print "Checking", npc_name, "...", |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
65 npc = getattr(game.world.npcs, npc_name) |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
66 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
|
67 print " Loaded %s." % (npc.dsm) |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
68 |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
69 def test_switch_to(npc_name): |
476
1c05b6c2b971
Start of drop item support.
Simon Cross <hodgestar@gmail.com>
parents:
458
diff
changeset
|
70 assert npc_name in game.world.npcs, "Unknown NPC %r" % (npc_name,) |
1c05b6c2b971
Start of drop item support.
Simon Cross <hodgestar@gmail.com>
parents:
458
diff
changeset
|
71 |
1c05b6c2b971
Start of drop item support.
Simon Cross <hodgestar@gmail.com>
parents:
458
diff
changeset
|
72 def test_drop_item(item_name): |
1c05b6c2b971
Start of drop item support.
Simon Cross <hodgestar@gmail.com>
parents:
458
diff
changeset
|
73 assert item_name in game.world.items, "Unknown item %r" % (item_name,) |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
74 |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
75 my_locals = { |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
76 "world": dsm.world, |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
77 "state": dsm.states, |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
78 "npcs": dsm.world.npcs, |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
79 "switch_to": test_switch_to, |
476
1c05b6c2b971
Start of drop item support.
Simon Cross <hodgestar@gmail.com>
parents:
458
diff
changeset
|
80 "drop_item": test_drop_item, |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
81 } |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
82 my_locals.update(DsmEvent().items) |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
281
diff
changeset
|
83 |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
84 for state_name, state in dsm.states.items(): |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
85 print " Testing triggers for state %s" % state_name |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
86 for trigger in state.triggers: |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
87 eval(trigger._matches, {}, my_locals.copy()) |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
88 eval(trigger._next_state, {}, my_locals.copy()) |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
89 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
|
90 state.enter(my_locals) |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
91 state.leave(my_locals) |
521
5a8a7f17597d
Add support for 'if': '...' to dialogue choices.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
92 list(state.choices(my_locals)) |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
93 print "ok" |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
94 |
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
95 |
227
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
96 def test_all(game_json): |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
97 game = GameState(game_json) |
458
fbae2da0c3c0
Fix npc-test to track changes to GameState.
Simon Cross <hodgestar@gmail.com>
parents:
302
diff
changeset
|
98 game.new_game() |
227
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 print "Testing NPCs" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
101 print "============" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
102 for npc_name in game.world.npcs: |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
103 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
|
104 print |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
105 |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
106 print "Testing Sprites" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
107 print "===============" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
108 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
|
109 print "Checking", level, "...", |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
110 game.create_sprites(level) |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
111 print "ok" |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
112 print |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
113 |
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 def main(): |
227
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
124 opts, args = parser.parse_args() |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
125 |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
126 if opts.test_all: |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
127 test_all(opts.game) |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
128 return |
13b912f40a10
Add --all option for testing NPCs and sprite creation.
Simon Cross <hodgestar@gmail.com>
parents:
222
diff
changeset
|
129 |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
130 if len(args) != 1: |
249 | 131 parser.error("Must provide an npc json file") |
281
9e6ff3eb63d6
Enable testing of individual NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
132 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
|
133 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
134 if __name__ == '__main__': |
195 | 135 main() |