Mercurial > parpg-source
comparison systems/scriptingsystem.py @ 173:3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
author | Beliar <KarstenBock@gmx.net> |
---|---|
date | Thu, 01 Mar 2012 14:49:09 +0100 |
parents | 565ffdd98d68 |
children | 230d316cc43b |
comparison
equal
deleted
inserted
replaced
172:5d47ad053aef | 173:3abd31885f0f |
---|---|
48 self.time = 0 | 48 self.time = 0 |
49 try: | 49 try: |
50 action_data = self.running_actions.popleft() | 50 action_data = self.running_actions.popleft() |
51 action = self.system.actions[action_data[0]] | 51 action = self.system.actions[action_data[0]] |
52 action_params = eval(action_data[1], | 52 action_params = eval(action_data[1], |
53 self.system.funcs, | 53 *self.system.getEnvironment() |
54 self.system.vals | |
55 ) | 54 ) |
56 if not (isinstance(action_params, list) | 55 if not (isinstance(action_params, list) |
57 or isinstance(action_params, tuple)): | 56 or isinstance(action_params, tuple)): |
58 action_params = [action_params] | 57 action_params = [action_params] |
59 self.cur_action = action(self.system.world, *action_params) | 58 self.cur_action = action(self.system.world, *action_params) |
60 self.wait = action_data[2] | 59 self.wait = action_data[2] |
61 if len(action_data) >= 4: | 60 if len(action_data) >= 4: |
62 vals = ( | 61 vals = ( |
63 eval(action_data[4], self.system.funcs, self.system.vals) | 62 eval(action_data[4], *self.system.getEnvironment()) |
64 if len(action_data) > 4 | 63 if len(action_data) > 4 |
65 else () | 64 else () |
66 ) | 65 ) |
67 command = action_data[3] | 66 command = action_data[3] |
68 self.system.commands[command]( | 67 self.system.commands[command]( |
82 their behavior. | 81 their behavior. |
83 """ | 82 """ |
84 | 83 |
85 def __init__(self, commands, actions): | 84 def __init__(self, commands, actions): |
86 """Constructor""" | 85 """Constructor""" |
87 self.funcs = {} | 86 self.__funcs = {} |
88 self.common_funcs = { | 87 self.common_funcs = { |
89 "sqrt":math.sqrt, | 88 "sqrt":math.sqrt, |
90 "log":math.log, | 89 "log":math.log, |
91 } | 90 } |
92 self.vals = {} | 91 self.__vals = {} |
93 self.commands = commands | 92 self.commands = commands |
94 self.actions = actions | 93 self.actions = actions |
95 self.game_state = None | 94 self.game_state = None |
96 self.reset() | 95 self.reset() |
97 | 96 |
98 def reset(self): | 97 def reset(self): |
99 """Resets the script and condition collections""" | 98 """Resets the script and condition collections""" |
100 self.scripts = {} | 99 self.scripts = {} |
101 self.conditions = [] | 100 self.conditions = [] |
102 | 101 |
102 def getEnvironment(self): | |
103 """Returns the globals and locals used by the scripting system""" | |
104 return self.__vals, self.__funcs | |
103 | 105 |
106 | |
107 def updateEnvironment(self): | |
108 self.__vals.clear() | |
109 self.__vals.update(self.game_state.getObjectDictOfMap(self.game_state.current_map_name)) | |
110 self.__funcs.clear() | |
111 self.__funcs.update(self.game_state.funcs) | |
112 self.__funcs.update(self.common_funcs) | |
113 | |
104 def step(self, dt): | 114 def step(self, dt): |
105 """Execute a time step for the system. Must be defined | 115 """Execute a time step for the system. Must be defined |
106 by all system classes. | 116 by all system classes. |
107 | 117 |
108 :param dt: Time since last step invocation | 118 :param dt: Time since last step invocation |
109 :type dt: float | 119 :type dt: float |
110 """ | 120 """ |
111 self.vals.clear() | 121 self.updateEnvironment() |
112 self.vals.update( | |
113 self.game_state.getObjectDictOfMap( | |
114 self.game_state.current_map_name) | |
115 ) | |
116 self.funcs.clear() | |
117 self.funcs.update(self.game_state.funcs) | |
118 self.funcs.update(self.common_funcs) | |
119 for condition_data in self.conditions: | 122 for condition_data in self.conditions: |
120 condition = condition_data[0] | 123 condition = condition_data[0] |
121 script_name = condition_data[1] | 124 script_name = condition_data[1] |
122 if not self.scripts.has_key(script_name): | 125 if not self.scripts.has_key(script_name): |
123 return | 126 return |
124 script = self.scripts[script_name] | 127 script = self.scripts[script_name] |
125 if eval(condition, self.funcs, self.vals) and not script.running: | 128 if eval(condition, *self.getEnvironment()) and not script.running: |
126 script.running = True | 129 script.running = True |
127 for script in self.scripts.itervalues(): | 130 for script in self.scripts.itervalues(): |
128 assert(isinstance(script, Script)) | 131 assert(isinstance(script, Script)) |
129 if script.finished: | 132 if script.finished: |
130 script.reset() | 133 script.reset() |