Mercurial > parpg-source
annotate systems/scriptingsystem.py @ 171:565ffdd98d68
Added math functions to the scripting system functions.
author | Beliar <KarstenBock@gmx.net> |
---|---|
date | Sun, 26 Feb 2012 01:24:33 +0100 |
parents | ed24962cdf5e |
children | 3abd31885f0f |
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], | |
53 self.system.funcs, | |
54 self.system.vals | |
55 ) | |
164 | 56 if not (isinstance(action_params, list) |
57 or isinstance(action_params, tuple)): | |
58 action_params = [action_params] | |
59 self.cur_action = action(self.system.world, *action_params) | |
160 | 60 self.wait = action_data[2] |
61 if len(action_data) >= 4: | |
62 vals = ( | |
63 eval(action_data[4], self.system.funcs, self.system.vals) | |
64 if len(action_data) > 4 | |
65 else () | |
66 ) | |
67 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
|
68 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
|
69 *vals, |
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
70 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
|
71 ) |
157 | 72 else: |
73 self.cur_action.execute() | |
74 except IndexError: | |
75 self.finished = True | |
76 self.running = False | |
77 | |
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
78 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
79 class ScriptingSystem(System): |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
80 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
81 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
|
82 their behavior. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
83 """ |
157 | 84 |
160 | 85 def __init__(self, commands, actions): |
157 | 86 """Constructor""" |
171
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
87 self.funcs = {} |
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
88 self.common_funcs = { |
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
89 "sqrt":math.sqrt, |
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
90 "log":math.log, |
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
91 } |
157 | 92 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
|
93 self.commands = commands |
160 | 94 self.actions = actions |
95 self.game_state = None | |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
96 self.reset() |
157 | 97 |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
98 def reset(self): |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
99 """Resets the script and condition collections""" |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
100 self.scripts = {} |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
101 self.conditions = [] |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
102 |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
103 |
157 | 104 def step(self, dt): |
105 """Execute a time step for the system. Must be defined | |
106 by all system classes. | |
107 | |
108 :param dt: Time since last step invocation | |
109 :type dt: float | |
110 """ | |
160 | 111 self.vals.clear() |
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) | |
171
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
118 self.funcs.update(self.common_funcs) |
160 | 119 for condition_data in self.conditions: |
120 condition = condition_data[0] | |
121 script_name = condition_data[1] | |
122 if not self.scripts.has_key(script_name): | |
123 return | |
124 script = self.scripts[script_name] | |
125 if eval(condition, self.funcs, self.vals) and not script.running: | |
126 script.running = True | |
127 for script in self.scripts.itervalues(): | |
157 | 128 assert(isinstance(script, Script)) |
129 if script.finished: | |
160 | 130 script.reset() |
157 | 131 elif script.running: |
132 script.update(dt) | |
160 | 133 |
134 def setScript(self, name, actions): | |
135 """Sets a script. | |
136 @param name: The name of the script | |
137 @param actions: What the script does | |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
138 @type actions: deque or iterable |
160 | 139 """ |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
140 if not(isinstance(actions, deque)): |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
141 actions = deque(actions) |
160 | 142 self.scripts[name] = Script(actions, |
143 self | |
144 ) | |
145 | |
146 def addCondition(self, condition, script_name): | |
147 """Adds a condition. | |
148 @param condition: Condition which will be evaluated | |
149 @param script_name: Name of the script that will be executed if the | |
150 condition evaluates to True. | |
151 """ | |
152 self.conditions.append((condition, script_name)) | |
153 | |
154 | |
155 def runScript(self, name): | |
156 """Runs a script with the given name | |
157 @param name: The name of the script""" | |
158 if self.scripts.has_key(name): | |
159 self.scripts[name].running = True | |
160 | |
171
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
161 |