Mercurial > parpg-source
annotate 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 |
rev | line source |
---|---|
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
1 # This program is free software: you can redistribute it and/or modify |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
2 # it under the terms of the GNU General Public License as published by |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
3 # the Free Software Foundation, either version 3 of the License, or |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
4 # (at your option) any later version. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
5 # |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
6 # This program is distributed in the hope that it will be useful, |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
9 # GNU General Public License for more details. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
10 # |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
11 # You should have received a copy of the GNU General Public License |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
12 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
13 |
160 | 14 from collections import deque |
15 from copy import deepcopy | |
171
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
16 import math |
160 | 17 |
65
e856b604b650
Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents:
41
diff
changeset
|
18 from parpg.bGrease import System |
157 | 19 |
20 class Script(object): | |
21 """Script object""" | |
22 | |
160 | 23 def __init__(self, actions, system): |
157 | 24 """Constructor""" |
25 assert(isinstance(actions, deque)) | |
26 self.actions = actions | |
160 | 27 assert(isinstance(system, ScriptingSystem)) |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
28 self.system = system |
160 | 29 self.reset() |
30 | |
31 def reset(self): | |
32 """Resets the state of the script""" | |
33 self.running_actions = deepcopy(self.actions) | |
157 | 34 self.running = False |
35 self.finished = False | |
36 self.time = 0 | |
37 self.wait = 0 | |
38 self.cur_action = None | |
160 | 39 |
157 | 40 def update(self, time): |
41 """Advance the script""" | |
42 if not self.running: | |
43 return | |
44 if self.cur_action and not self.cur_action.executed: | |
45 return | |
46 self.time += time | |
47 if self.wait <= self.time: | |
48 self.time = 0 | |
49 try: | |
160 | 50 action_data = self.running_actions.popleft() |
51 action = self.system.actions[action_data[0]] | |
52 action_params = eval(action_data[1], | |
173
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
53 *self.system.getEnvironment() |
160 | 54 ) |
164 | 55 if not (isinstance(action_params, list) |
56 or isinstance(action_params, tuple)): | |
57 action_params = [action_params] | |
58 self.cur_action = action(self.system.world, *action_params) | |
160 | 59 self.wait = action_data[2] |
60 if len(action_data) >= 4: | |
61 vals = ( | |
173
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
62 eval(action_data[4], *self.system.getEnvironment()) |
160 | 63 if len(action_data) > 4 |
64 else () | |
65 ) | |
66 command = action_data[3] | |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
67 self.system.commands[command]( |
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
68 *vals, |
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
69 action=self.cur_action |
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
70 ) |
157 | 71 else: |
72 self.cur_action.execute() | |
73 except IndexError: | |
74 self.finished = True | |
75 self.running = False | |
76 | |
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
77 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
78 class ScriptingSystem(System): |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
79 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
80 System responsible for managing scripts attached to entities to define |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
81 their behavior. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
82 """ |
157 | 83 |
160 | 84 def __init__(self, commands, actions): |
157 | 85 """Constructor""" |
173
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
86 self.__funcs = {} |
171
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
87 self.common_funcs = { |
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
88 "sqrt":math.sqrt, |
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
89 "log":math.log, |
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
90 } |
173
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
91 self.__vals = {} |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
92 self.commands = commands |
160 | 93 self.actions = actions |
94 self.game_state = None | |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
95 self.reset() |
157 | 96 |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
97 def reset(self): |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
98 """Resets the script and condition collections""" |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
99 self.scripts = {} |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
100 self.conditions = [] |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
101 |
173
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
102 def getEnvironment(self): |
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
103 """Returns the globals and locals used by the scripting system""" |
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
104 return self.__vals, self.__funcs |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
105 |
173
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
106 |
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
107 def updateEnvironment(self): |
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
108 self.__vals.clear() |
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
109 self.__vals.update(self.game_state.getObjectDictOfMap(self.game_state.current_map_name)) |
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
110 self.__funcs.clear() |
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
111 self.__funcs.update(self.game_state.funcs) |
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
112 self.__funcs.update(self.common_funcs) |
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
113 |
157 | 114 def step(self, dt): |
115 """Execute a time step for the system. Must be defined | |
116 by all system classes. | |
117 | |
118 :param dt: Time since last step invocation | |
119 :type dt: float | |
120 """ | |
173
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
121 self.updateEnvironment() |
160 | 122 for condition_data in self.conditions: |
123 condition = condition_data[0] | |
124 script_name = condition_data[1] | |
125 if not self.scripts.has_key(script_name): | |
126 return | |
127 script = self.scripts[script_name] | |
173
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
128 if eval(condition, *self.getEnvironment()) and not script.running: |
160 | 129 script.running = True |
130 for script in self.scripts.itervalues(): | |
157 | 131 assert(isinstance(script, Script)) |
132 if script.finished: | |
160 | 133 script.reset() |
157 | 134 elif script.running: |
135 script.update(dt) | |
160 | 136 |
137 def setScript(self, name, actions): | |
138 """Sets a script. | |
139 @param name: The name of the script | |
140 @param actions: What the script does | |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
141 @type actions: deque or iterable |
160 | 142 """ |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
143 if not(isinstance(actions, deque)): |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
144 actions = deque(actions) |
160 | 145 self.scripts[name] = Script(actions, |
146 self | |
147 ) | |
148 | |
149 def addCondition(self, condition, script_name): | |
150 """Adds a condition. | |
151 @param condition: Condition which will be evaluated | |
152 @param script_name: Name of the script that will be executed if the | |
153 condition evaluates to True. | |
154 """ | |
155 self.conditions.append((condition, script_name)) | |
156 | |
157 | |
158 def runScript(self, name): | |
159 """Runs a script with the given name | |
160 @param name: The name of the script""" | |
161 if self.scripts.has_key(name): | |
162 self.scripts[name].running = True | |
163 | |
171
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
164 |