annotate src/parpg/systems/scriptingsystem.py @ 192:191f89a22303

Further work on the scripting system.
author KarstenBock@gmx.net
date Sun, 13 Nov 2011 17:19:14 +0100
parents c97e48257f51
children 728eabc921f2
rev   line source
34
5ac50245e42c 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
5ac50245e42c 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
5ac50245e42c 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
5ac50245e42c 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.
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
5 #
5ac50245e42c 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,
5ac50245e42c 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
5ac50245e42c 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
5ac50245e42c 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.
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
10 #
5ac50245e42c 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
5ac50245e42c 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/>.
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
13
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
14 from collections import deque
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
15 from copy import deepcopy
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
16
92
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
17 from parpg.bGrease import System
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
18
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
19 class Script(object):
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
20 """Script object"""
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
21
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
22 def __init__(self, actions, system):
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
23 """Constructor"""
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
24 assert(isinstance(actions, deque))
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
25 self.actions = actions
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
26 assert(isinstance(system, ScriptingSystem))
191
c97e48257f51 script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents: 189
diff changeset
27 self.system = system
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
28 self.reset()
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
29
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
30 def reset(self):
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
31 """Resets the state of the script"""
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
32 self.running_actions = deepcopy(self.actions)
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
33 self.running = False
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
34 self.finished = False
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
35 self.time = 0
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
36 self.wait = 0
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
37 self.cur_action = None
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
38
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
39 def update(self, time):
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
40 """Advance the script"""
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
41 if not self.running:
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
42 return
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
43 if self.cur_action and not self.cur_action.executed:
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
44 return
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
45 self.time += time
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
46 if self.wait <= self.time:
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
47 self.time = 0
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
48 try:
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
49 action_data = self.running_actions.popleft()
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
50 action = self.system.actions[action_data[0]]
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
51 action_params = eval(action_data[1],
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
52 self.system.funcs,
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
53 self.system.vals
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
54 )
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
55 self.cur_action = action(self.system.world, action_params)
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
56 self.wait = action_data[2]
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
57 if len(action_data) >= 4:
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
58 vals = (
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
59 eval(action_data[4], self.system.funcs, self.system.vals)
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
60 if len(action_data) > 4
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
61 else ()
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
62 )
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
63 command = action_data[3]
191
c97e48257f51 script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents: 189
diff changeset
64 self.system.commands[command](
c97e48257f51 script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents: 189
diff changeset
65 *vals,
c97e48257f51 script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents: 189
diff changeset
66 action=self.cur_action
c97e48257f51 script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents: 189
diff changeset
67 )
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
68 else:
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
69 self.cur_action.execute()
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
70 except IndexError:
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
71 self.finished = True
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
72 self.running = False
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
73
34
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
74
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
75 class ScriptingSystem(System):
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
76 """
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
77 System responsible for managing scripts attached to entities to define
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
78 their behavior.
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
79 """
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
80
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
81 def __init__(self, commands, actions):
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
82 """Constructor"""
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
83 self.funcs = {}
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
84 self.vals = {}
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
85 self.scripts = {}
191
c97e48257f51 script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents: 189
diff changeset
86 self.commands = commands
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
87 self.conditions = []
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
88 self.actions = actions
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
89 self.game_state = None
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
90
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
91 def step(self, dt):
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
92 """Execute a time step for the system. Must be defined
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
93 by all system classes.
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
94
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
95 :param dt: Time since last step invocation
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
96 :type dt: float
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
97 """
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
98 self.vals.clear()
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
99 self.vals.update(
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
100 self.game_state.getObjectDictOfMap(
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
101 self.game_state.current_map_name)
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
102 )
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
103 self.funcs.clear()
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
104 self.funcs.update(self.game_state.funcs)
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
105 for condition_data in self.conditions:
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
106 condition = condition_data[0]
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
107 script_name = condition_data[1]
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
108 if not self.scripts.has_key(script_name):
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
109 return
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
110 script = self.scripts[script_name]
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
111 if eval(condition, self.funcs, self.vals) and not script.running:
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
112 script.running = True
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
113 for script in self.scripts.itervalues():
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
114 assert(isinstance(script, Script))
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
115 if script.finished:
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
116 script.reset()
189
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
117 elif script.running:
61d158ce6bc3 Implemented simple script system.
KarstenBock@gmx.net
parents: 92
diff changeset
118 script.update(dt)
192
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
119
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
120 def setScript(self, name, actions):
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
121 """Sets a script.
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
122 @param name: The name of the script
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
123 @param actions: What the script does
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
124 """
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
125 self.scripts[name] = Script(actions,
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
126 self
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
127 )
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
128
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
129 def addCondition(self, condition, script_name):
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
130 """Adds a condition.
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
131 @param condition: Condition which will be evaluated
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
132 @param script_name: Name of the script that will be executed if the
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
133 condition evaluates to True.
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
134 """
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
135 self.conditions.append((condition, script_name))
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
136
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
137
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
138 def runScript(self, name):
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
139 """Runs a script with the given name
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
140 @param name: The name of the script"""
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
141 if self.scripts.has_key(name):
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
142 self.scripts[name].running = True
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
143
191f89a22303 Further work on the scripting system.
KarstenBock@gmx.net
parents: 191
diff changeset
144