annotate systems/scriptingsystem.py @ 164:ed24962cdf5e

Small fixes.
author KarstenBock@gmx.net
date Sat, 19 Nov 2011 19:28:38 +0100
parents d224bbce512a
children 565ffdd98d68
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
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
14 from collections import deque
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
15 from copy import deepcopy
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
16
65
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
17 from parpg.bGrease import System
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
18
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
19 class Script(object):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
20 """Script object"""
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
21
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
22 def __init__(self, actions, system):
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
23 """Constructor"""
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
24 assert(isinstance(actions, deque))
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
25 self.actions = actions
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
26 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
27 self.system = system
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
28 self.reset()
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
29
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
30 def reset(self):
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
31 """Resets the state of the script"""
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
32 self.running_actions = deepcopy(self.actions)
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
33 self.running = False
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
34 self.finished = False
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
35 self.time = 0
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
36 self.wait = 0
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
37 self.cur_action = None
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
38
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
39 def update(self, time):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
40 """Advance the script"""
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
41 if not self.running:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
42 return
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
43 if self.cur_action and not self.cur_action.executed:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
44 return
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
45 self.time += time
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
46 if self.wait <= self.time:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
47 self.time = 0
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
48 try:
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
49 action_data = self.running_actions.popleft()
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
50 action = self.system.actions[action_data[0]]
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
51 action_params = eval(action_data[1],
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
52 self.system.funcs,
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
53 self.system.vals
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
54 )
164
ed24962cdf5e Small fixes.
KarstenBock@gmx.net
parents: 161
diff changeset
55 if not (isinstance(action_params, list)
ed24962cdf5e Small fixes.
KarstenBock@gmx.net
parents: 161
diff changeset
56 or isinstance(action_params, tuple)):
ed24962cdf5e Small fixes.
KarstenBock@gmx.net
parents: 161
diff changeset
57 action_params = [action_params]
ed24962cdf5e Small fixes.
KarstenBock@gmx.net
parents: 161
diff changeset
58 self.cur_action = action(self.system.world, *action_params)
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
59 self.wait = action_data[2]
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
60 if len(action_data) >= 4:
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
61 vals = (
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
62 eval(action_data[4], self.system.funcs, self.system.vals)
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
63 if len(action_data) > 4
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
64 else ()
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
65 )
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
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
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
71 else:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
72 self.cur_action.execute()
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
73 except IndexError:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
74 self.finished = True
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
75 self.running = False
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
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
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
83
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
84 def __init__(self, commands, actions):
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
85 """Constructor"""
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
86 self.funcs = {}
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
87 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
88 self.commands = commands
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
89 self.actions = actions
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
90 self.game_state = None
161
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
91 self.reset()
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
92
161
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
93 def reset(self):
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
94 """Resets the script and condition collections"""
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
95 self.scripts = {}
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
96 self.conditions = []
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
97
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
98
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
99 def step(self, dt):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
100 """Execute a time step for the system. Must be defined
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
101 by all system classes.
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
102
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
103 :param dt: Time since last step invocation
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
104 :type dt: float
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
105 """
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
106 self.vals.clear()
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
107 self.vals.update(
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
108 self.game_state.getObjectDictOfMap(
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
109 self.game_state.current_map_name)
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
110 )
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
111 self.funcs.clear()
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
112 self.funcs.update(self.game_state.funcs)
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
113 for condition_data in self.conditions:
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
114 condition = condition_data[0]
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
115 script_name = condition_data[1]
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
116 if not self.scripts.has_key(script_name):
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
117 return
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
118 script = self.scripts[script_name]
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
119 if eval(condition, self.funcs, self.vals) and not script.running:
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
120 script.running = True
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
121 for script in self.scripts.itervalues():
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
122 assert(isinstance(script, Script))
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
123 if script.finished:
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
124 script.reset()
157
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
125 elif script.running:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
126 script.update(dt)
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
127
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
128 def setScript(self, name, actions):
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
129 """Sets a script.
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
130 @param name: The name of the script
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
131 @param actions: What the script does
161
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
132 @type actions: deque or iterable
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
133 """
161
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
134 if not(isinstance(actions, deque)):
d224bbce512a Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 160
diff changeset
135 actions = deque(actions)
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
136 self.scripts[name] = Script(actions,
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
137 self
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
138 )
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
139
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
140 def addCondition(self, condition, script_name):
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
141 """Adds a condition.
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
142 @param condition: Condition which will be evaluated
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
143 @param script_name: Name of the script that will be executed if the
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
144 condition evaluates to True.
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
145 """
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
146 self.conditions.append((condition, script_name))
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
147
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
148
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
149 def runScript(self, name):
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
150 """Runs a script with the given name
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
151 @param name: The name of the script"""
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
152 if self.scripts.has_key(name):
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
153 self.scripts[name].running = True
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
154
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 159
diff changeset
155