Docstrings.
[python-bulletml.git] / bulletml / impl.py
1 """BulletML implementation.
2
3 http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html
4 """
5
6 from __future__ import division
7
8 import math
9
10 from bulletml import parser
11
12 # TODO(jfw): This is very non-Pythonic, it's pretty much just the
13 # BulletML reference ActionImpl translated to Python.
14
15 class Action(object):
16 """Running action implementation."""
17
18 def __init__(self, owner, parent, actions, params, rank):
19 self.actions = actions
20 self.parent = parent
21 self.repeat = 1
22 self.wait_frames = 0
23 self.speed = 0
24 self.speed_frames = 0
25 self.aim_direction = 0
26 self.direction = 0
27 self.direction_frames = 0
28 self.aiming = False
29 self.aim_mx = 0
30 self.aim_my = 0
31 self.mx = 0
32 self.my = 0
33 self.owner = owner
34 self.accel_frames = 0
35 self.previous_fire_direction = 0
36 self.previous_fire_speed = 0
37 self.params = params
38 self.rank = rank
39 self.pc = -1
40 self.finished = False
41 if parent:
42 self.copy_state(parent)
43
44 def __repr__(self):
45 return "%s(pc=%r, actions=%r)" % (
46 type(self).__name__, self.pc, self.actions)
47
48 def vanish(self):
49 """End this action and its parents."""
50 if self.parent:
51 self.parent.vanish()
52 self.pc = None
53 self.finished = True
54
55 def copy_state(self, other):
56 """Copy fire/movement state from other to self."""
57 self.direction_frames = other.direction_frames
58 self.direction = other.direction
59 self.aiming = other.aiming
60 self.speed_frames = other.speed_frames
61 self.speed = other.speed
62 self.accel_frames = other.accel_frames
63 self.mx = other.mx
64 self.my = other.my
65 self.previous_fire_direction = other.previous_fire_direction
66 self.previous_fire_speed = other.previous_fire_speed
67
68 def step(self):
69 """Advance by one frame."""
70 created = []
71
72 if self.speed_frames > 0:
73 self.speed_frames -= 1
74 self.owner.speed += self.speed
75
76 if self.direction_frames > 0:
77 self.direction_frames -= 1
78 if self.direction_frames <= 0:
79 if self.aiming:
80 self.owner.direction += self.owner.aim
81 else:
82 self.owner.direction += self.direction
83
84 if self.accel_frames > 0:
85 self.accel_frames -= 1
86 self.owner.mx += self.mx
87 self.owner.my += self.my
88
89 if self.pc is None:
90 return created
91
92 if self.wait_frames > 0:
93 self.wait_frames -= 1
94 return created
95
96 while True:
97 self.pc += 1
98
99 if self.pc >= len(self.actions):
100 self.repeat -= 1
101 if self.repeat <= 0:
102 self.pc = None
103 self.finished = True
104 if self.parent is not None:
105 self.parent.copy_state(self)
106 self.owner.replace(self, self.parent)
107 break
108 else:
109 self.pc = 0
110
111 action = self.actions[self.pc]
112
113 if isinstance(action, parser.Repeat):
114 repeat, (actions, params) = action(self.params, self.rank)
115 child = Action(self.owner, self, actions, params, self.rank)
116 child.repeat = repeat
117 self.owner.replace(self, child)
118 created.extend(child.step())
119 break
120
121 elif isinstance(action, (parser.ActionDef, parser.ActionRef)):
122 actions, params = action(self.params, self.rank)
123 child = Action(self.owner, self, actions, params, self.rank)
124 self.owner.replace(self, child)
125 created.extend(child.step())
126 break
127
128 elif isinstance(action, (parser.FireDef, parser.FireRef)):
129 direction, speed, actions = action(self.params, self.rank)
130 if direction:
131 direction, type = direction
132 if type == "aim" or type is None:
133 direction += self.owner.aim
134 elif type == "sequence":
135 direction += self.previous_fire_direction
136 elif type == "relative":
137 direction += self.owner.direction
138 else:
139 direction = self.owner.aim
140 self.previous_fire_direction = direction
141
142
143 if speed:
144 speed, type = speed
145 if type == "sequence":
146 speed += self.previous_fire_speed
147 elif type == "relative":
148 # FIXME(jfw): Reference impl uses prvFireSpeed
149 # here? That doesn't seem right at all.
150 speed += self.owner.speed
151 else:
152 speed = 1
153 self.previous_fire_speed = speed
154
155 bullet = Bullet(self.owner.x, self.owner.y, direction, speed,
156 self.owner.target, actions, self)
157 created.append(bullet)
158
159 elif isinstance(action, parser.ChangeSpeed):
160 frames, (speed, type) = action(self.params, self.rank)
161 self.speed_frames = frames
162 if type == "sequence":
163 self.speed = speed
164 elif type == "relative":
165 self.speed = speed / frames
166 else:
167 self.speed = (speed - self.owner.speed) / frames
168
169 elif isinstance(action, parser.ChangeDirection):
170 frames, (direction, type) = action(self.params, self.rank)
171 self.direction_frames = frames
172 self.aiming = False
173 if type == "sequence":
174 self.aiming = False
175 self.direction = direction
176 else:
177 if type == "absolute":
178 self.aiming = False
179 self.direction = (
180 direction - self.owner.direction) % 360
181 elif type == "relative":
182 self.aiming = False
183 self.direction = direction
184 else:
185 self.aiming = True
186 self.direction = (
187 direction
188 + self.owner.aim
189 - self.owner.direction) % 360
190
191 while self.direction > 180:
192 self.direction -= 360
193 while self.direction < -180:
194 self.direction += 360
195 self.direction /= self.direction_frames
196
197 elif isinstance(action, parser.Accel):
198 frames, horizontal, vertical = action(self.params, self.rank)
199 self.accel_frames = frames
200 if horizontal:
201 mx, type = horizontal
202 if type == "sequence":
203 self.mx = mx
204 elif type == "absolute":
205 self.mx = (mx - self.owner.mx) / frames
206 elif type == "relative":
207 self.mx = mx / frames
208 if vertical:
209 my, type = vertical
210 if type == "sequence":
211 self.my = my
212 elif type == "absolute":
213 self.my = (my - self.owner.my) / frames
214 elif type == "relative":
215 self.my = my / frames
216
217 elif isinstance(action, parser.Wait):
218 self.wait_frames = action(self.params, self.rank)
219 break
220
221 elif isinstance(action, parser.Vanish):
222 self.owner.vanish()
223 break
224
225 return created
226
227 class Bullet(object):
228 """Simple bullet implementation."""
229
230 def __init__(self, x=0, y=0, direction=0, speed=0, target=None,
231 actions=(), parent=None, rank=None):
232 self.x = self.px = x
233 self.y = self.py = y
234 self.mx = 0
235 self.my = 0
236 self.direction = direction
237 self.speed = speed
238 self.vanished = False
239 self.target = target
240 self.actions = []
241 if rank is None:
242 rank = parent.rank if parent else 0.5
243 # New bullets reset the parent hierarchy.
244 self.actions = [Action(self, None, action, params, rank)
245 for action, params in actions]
246
247 def __repr__(self):
248 return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
249 "actions=%r, target=%r, vanished=%r)") % (
250 type(self).__name__, self.x, self.y, (self.mx, self.my),
251 self.direction, self.speed, self.actions, self.target,
252 self.vanished)
253
254 @property
255 def aim(self):
256 """Angle to the target."""
257 if self.target is None:
258 return self.direction
259 else:
260 return math.degrees(
261 math.atan2(self.target.x - self.x, self.y - self.target.y))
262
263 @property
264 def finished(self):
265 """Check if this bullet is finished running."""
266 for action in self.actions:
267 if not action.finished:
268 return False
269 return self.vanished
270
271 def vanish(self):
272 """Vanish this bullet and stop all actions."""
273 self.vanished = True
274 for action in self.actions:
275 action.vanish()
276 self.actions = []
277
278 def replace(self, old, new):
279 """Replace an active action with another."""
280 try:
281 idx = self.actions.index(old)
282 except ValueError:
283 pass
284 else:
285 self.actions[idx] = new
286
287 def step(self):
288 """Advance by one frame."""
289 created = []
290
291 for action in self.actions:
292 created.extend(action.step())
293
294 direction = math.radians(self.direction)
295 self.px = self.x
296 self.py = self.y
297 self.x += self.mx + math.sin(direction) * self.speed
298 self.y += self.my - math.cos(direction) * self.speed
299
300 return created
301
302 @classmethod
303 def FromDoc(cls, doc, params=(), x=0, y=0, speed=0, direction=0,
304 target=None, rank=0.5):
305 """Construct a bullet from top-level actions in a document."""
306 actions = [act(params, rank) for act in doc.top]
307 return cls(x, y, direction, speed, target, actions, rank=rank)
308