Mercurial > skaapsteker
annotate skaapsteker/dialogue.py @ 251:432f6997d306
More hooking up of interacting with NPCs.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Fri, 08 Apr 2011 00:40:34 +0200 |
parents | 8dee25fd070d |
children | 71f15f6e9274 |
rev | line source |
---|---|
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
1 import json |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
2 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
3 from . import data |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
4 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
5 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
6 class DSM(object): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
7 """Dialogue State Machine! |
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 Parameters |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
10 ---------- |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
11 json_filename : str |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
12 Path to file under data/ that contains JSON description |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
13 of state machine. |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
14 world : object |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
15 Something to allow states to introspect the game state with. |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
16 """ |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
17 |
217
fcc5eca8eaca
Add explicit starting state to avoid it magically appearing later.
Simon Cross <hodgestar@gmail.com>
parents:
216
diff
changeset
|
18 def __init__(self, name, world, json_filename, state): |
fcc5eca8eaca
Add explicit starting state to avoid it magically appearing later.
Simon Cross <hodgestar@gmail.com>
parents:
216
diff
changeset
|
19 self.state = state |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
20 self.world = world |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
21 self.states = AttrDict() |
190 | 22 src = json.loads(data.load(json_filename).read()) |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
23 for state, state_src in src.iteritems(): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
24 pseudo_path = [json_filename, state] |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
25 self.states[state] = DsmState(state, state_src, pseudo_path) |
217
fcc5eca8eaca
Add explicit starting state to avoid it magically appearing later.
Simon Cross <hodgestar@gmail.com>
parents:
216
diff
changeset
|
26 assert self.state in self.states, "DSM must have start state %r" % (self.state,) |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
27 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
28 def get_state(self): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
29 return self.states[self.state] |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
30 |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
246
diff
changeset
|
31 def has_text(self): |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
246
diff
changeset
|
32 return bool(self.states[self.state].text) |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
246
diff
changeset
|
33 |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
34 def event(self, ev): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
35 my_locals = { |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
36 "state": self.states, |
193
897eec397cbb
Fix state checks for hattori, ichiro, kaneda and kumiko. In the process provide easier access to other npcs.
Simon Cross <hodgestar@gmail.com>
parents:
190
diff
changeset
|
37 "world": self.world, |
897eec397cbb
Fix state checks for hattori, ichiro, kaneda and kumiko. In the process provide easier access to other npcs.
Simon Cross <hodgestar@gmail.com>
parents:
190
diff
changeset
|
38 "npcs": self.world.npcs, |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
39 } |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
40 my_locals.update(ev.items) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
41 state = self.states[self.state] |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
42 next_state = state.event(my_locals) |
246
8dee25fd070d
Fix some npc-test and dialogue bugs.
Simon Cross <hodgestar@gmail.com>
parents:
233
diff
changeset
|
43 if next_state is not None and next_state.name in self.states: |
216
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
44 self.states[self.state].leave(my_locals) |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
45 self.state = next_state.name |
217
fcc5eca8eaca
Add explicit starting state to avoid it magically appearing later.
Simon Cross <hodgestar@gmail.com>
parents:
216
diff
changeset
|
46 # TODO: update self.world to reflect new state? |
216
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
47 self.states[self.state].enter(my_locals) |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
48 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
49 def choice(self, i): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
50 self.event(DsmEvent(choice=i)) |
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 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
53 class AttrDict(dict): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
54 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
55 def __getattr__(self, name): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
56 if name not in self: |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
57 raise AttributeError("No attribute %r" % (name,)) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
58 return self[name] |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
59 |
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 class DsmEvent(object): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
62 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
63 def __init__(self, choice=None): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
64 self.items = { |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
65 "choice": choice, |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
66 } |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
67 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
68 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
69 class DsmState(object): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
70 """State within a DSM. |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
71 """ |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
72 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
73 def __init__(self, name, state_src, base_path): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
74 self.name = name |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
75 self.text = state_src.get("text", None) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
76 self.choices = [] |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
77 self.triggers = [] |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
78 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
79 choices = state_src.get("choices", []) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
80 for i, choice in enumerate(choices): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
81 pseudo_path = base_path + ["choice-%d" % i] |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
82 self.choices.append((i, choice["text"])) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
83 next_state_code = choice.get("next", None) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
84 if next_state_code is not None: |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
85 self.triggers.append( |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
86 Trigger("choice == %d" % i, next_state_code, pseudo_path)) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
87 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
88 events = state_src.get("events", []) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
89 for i, event in enumerate(events): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
90 pseudo_path = base_path + ["event-%d" % i] |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
91 self.triggers.append(Trigger(event["matches"], event["next"], |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
92 pseudo_path)) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
93 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
94 auto_next = state_src.get("auto_next", None) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
95 if auto_next is not None: |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
96 pseudo_path = base_path + ["auto_next"] |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
97 self.triggers.append(Trigger("""True""", auto_next, pseudo_path)) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
98 |
216
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
99 on_entry = state_src.get("on_entry", None) |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
100 if on_entry is not None: |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
101 self.on_entry = compile(on_entry, |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
102 "<%s>" % ":".join(base_path + ["on_entry"]), |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
103 "exec") |
233
8ea658969f47
Fix on_entry and on_exit handling.
Simon Cross <hodgestar@gmail.com>
parents:
217
diff
changeset
|
104 else: |
8ea658969f47
Fix on_entry and on_exit handling.
Simon Cross <hodgestar@gmail.com>
parents:
217
diff
changeset
|
105 self.on_entry = None |
216
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
106 |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
107 on_exit = state_src.get("on_exit", None) |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
108 if on_exit is not None: |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
109 self.on_exit = compile(on_exit, |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
110 "<%s>" % ":".join(base_path + ["on_exit"]), |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
111 "exec") |
233
8ea658969f47
Fix on_entry and on_exit handling.
Simon Cross <hodgestar@gmail.com>
parents:
217
diff
changeset
|
112 else: |
8ea658969f47
Fix on_entry and on_exit handling.
Simon Cross <hodgestar@gmail.com>
parents:
217
diff
changeset
|
113 self.on_exit = None |
216
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
114 |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
115 def __repr__(self): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
116 return "<%r name=%r>" % (self.__class__.__name__, self.name) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
117 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
118 def event(self, my_locals): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
119 for trigger in self.triggers: |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
120 next_state = trigger.fire(my_locals) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
121 if next_state is not None: |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
122 return next_state |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
123 |
216
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
124 def enter(self, my_locals): |
233
8ea658969f47
Fix on_entry and on_exit handling.
Simon Cross <hodgestar@gmail.com>
parents:
217
diff
changeset
|
125 if self.on_entry is not None: |
8ea658969f47
Fix on_entry and on_exit handling.
Simon Cross <hodgestar@gmail.com>
parents:
217
diff
changeset
|
126 exec(self.on_entry, {}, my_locals.copy()) |
216
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
127 |
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
128 def leave(self, my_locals): |
233
8ea658969f47
Fix on_entry and on_exit handling.
Simon Cross <hodgestar@gmail.com>
parents:
217
diff
changeset
|
129 if self.on_exit is not None: |
8ea658969f47
Fix on_entry and on_exit handling.
Simon Cross <hodgestar@gmail.com>
parents:
217
diff
changeset
|
130 exec(self.on_exit, {}, my_locals.copy()) |
216
023eea4ad4a5
Add support for trigger world changes when entering and exiting dialog states.
Simon Cross <hodgestar@gmail.com>
parents:
196
diff
changeset
|
131 |
153
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
132 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
133 class Trigger(object): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
134 """Matches DSM events and triggers state transitions. |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
135 """ |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
136 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
137 def __init__(self, matches_code, next_state_code, pseudo_path): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
138 self._matches = compile(matches_code, |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
139 "<%s>" % ":".join(pseudo_path + ["match"]), |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
140 "eval") |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
141 self._next_state = compile(next_state_code, |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
142 "<%s>" % ":".join(pseudo_path + ["next"]), |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
143 "eval") |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
144 |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
145 def fire(self, my_locals): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
146 if eval(self._matches, {}, my_locals.copy()): |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
147 return eval(self._next_state, {}, my_locals.copy()) |
704d23022f09
Start of dialogue tree / NPC state machine support.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
148 return None |