9df26c62a865f555458c8e0a1acfb6e16ee2bddc
[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 # The reference Noiz implementation uses
151 # prvFireSpeed here, but the standard is
152 # pretty clear -- "0 means that the direction
153 # of this fire and the direction of the bullet
154 # are the same".
155 speed += self.owner.speed
156 else:
157 speed = 1
158 self.previous_fire_speed = speed
159
160 bullet = Bullet(self.owner.x, self.owner.y, direction, speed,
161 self.owner.target, actions, self)
162 created.append(bullet)
163
164 elif isinstance(action, parser.ChangeSpeed):
165 frames, (speed, type) = action(self.params, self.rank)
166 self.speed_frames = frames
167 if type == "sequence":
168 self.speed = speed
169 elif type == "relative":
170 self.speed = speed / frames
171 else:
172 self.speed = (speed - self.owner.speed) / frames
173
174 elif isinstance(action, parser.ChangeDirection):
175 frames, (direction, type) = action(self.params, self.rank)
176 self.direction_frames = frames
177 self.aiming = False
178 if type == "sequence":
179 self.aiming = False
180 self.direction = direction
181 else:
182 if type == "absolute":
183 self.aiming = False
184 self.direction = (
185 direction - self.owner.direction) % PI_2
186 elif type == "relative":
187 self.aiming = False
188 self.direction = direction
189 else:
190 self.aiming = True
191 self.direction = (
192 direction
193 + self.owner.aim
194 - self.owner.direction) % PI_2
195
196 while self.direction > math.pi:
197 self.direction -= PI_2
198 while self.direction < -math.pi:
199 self.direction += PI_2
200 self.direction /= self.direction_frames
201
202 elif isinstance(action, parser.Accel):
203 frames, horizontal, vertical = action(self.params, self.rank)
204 self.accel_frames = frames
205 if horizontal:
206 mx, type = horizontal
207 if type == "sequence":
208 self.mx = mx
209 elif type == "absolute":
210 self.mx = (mx - self.owner.mx) / frames
211 elif type == "relative":
212 self.mx = mx / frames
213 if vertical:
214 my, type = vertical
215 if type == "sequence":
216 self.my = my
217 elif type == "absolute":
218 self.my = (my - self.owner.my) / frames
219 elif type == "relative":
220 self.my = my / frames
221
222 elif isinstance(action, parser.Wait):
223 self.wait_frames = action(self.params, self.rank)
224 break
225
226 elif isinstance(action, parser.Vanish):
227 self.owner.vanish()
228 break
229
230 return created
231
232 class Bullet(object):
233 """Simple bullet implementation."""
234
235 def __init__(self, x=0, y=0, direction=0, speed=0, target=None,
236 actions=(), parent=None, rank=None):
237 self.x = self.px = x
238 self.y = self.py = y
239 self.mx = 0
240 self.my = 0
241 self.direction = direction
242 self.speed = speed
243 self.vanished = False
244 self.target = target
245 self.actions = []
246 if rank is None:
247 rank = parent.rank if parent else 0.5
248 # New bullets reset the parent hierarchy.
249 self.actions = [Action(self, None, action, params, rank)
250 for action, params in actions]
251
252 def __repr__(self):
253 return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
254 "actions=%r, target=%r, vanished=%r)") % (
255 type(self).__name__, self.x, self.y, (self.mx, self.my),
256 self.direction, self.speed, self.actions, self.target,
257 self.vanished)
258
259 @property
260 def aim(self):
261 """Angle to the target."""
262 if self.target is None:
263 return self.direction
264 else:
265 return math.atan2(self.target.x - self.x, self.y - self.target.y)
266
267 @property
268 def finished(self):
269 """Check if this bullet is finished running."""
270 if not self.vanished:
271 return False
272 for action in self.actions:
273 if not action.finished:
274 return False
275 return True
276
277 def vanish(self):
278 """Vanish this bullet and stop all actions."""
279 self.vanished = True
280 for action in self.actions:
281 action.vanish()
282 self.actions = []
283
284 def replace(self, old, new):
285 """Replace an active action with another."""
286 try:
287 idx = self.actions.index(old)
288 except ValueError:
289 pass
290 else:
291 self.actions[idx] = new
292
293 def step(self):
294 """Advance by one frame."""
295 created = []
296
297 for action in self.actions:
298 created.extend(action.step())
299
300 self.px = self.x
301 self.py = self.y
302 self.x += self.mx + math.sin(self.direction) * self.speed
303 self.y += self.my - math.cos(self.direction) * self.speed
304
305 return created
306
307 @classmethod
308 def FromDoc(cls, doc, params=(), x=0, y=0, speed=0, direction=0,
309 target=None, rank=0.5):
310 """Construct a bullet from top-level actions in a document."""
311 actions = [act(params, rank) for act in doc.top]
312 return cls(x, y, direction, speed, target, actions, rank=rank)
313