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