Implementation.
[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, errors
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 = 0
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 if parent:
41 self.copy_state(parent)
42
43 def vanish(self):
44 if self.parent:
45 self.parent.vanish()
46 self.repeat = 0
47 self.pc = None
48
49 def copy_state(self, other):
50 self.direction_frames = other.direction_frames
51 self.direction = other.direction
52 self.aiming = other.aiming
53 self.speed_frames = other.speed_frames
54 self.speed = other.speed
55 self.accel_frames = other.accel_frames
56 self.mx = other.mx
57 self.my = other.my
58 self.previous_fire_direction = other.previous_fire_direction
59 self.previous_fire_speed = other.previous_fire_speed
60
61 def step(self):
62 created = []
63
64 if self.speed_frames > 0:
65 self.speed_frames -= 1
66 self.owner.speed += self.speed
67 if self.direction_frames > 0:
68 self.direction_frames -= 1
69 if self.direction_frames <= 0:
70 if self.aiming:
71 self.owner.direction += self.owner.aim
72 else:
73 self.owner.direction += self.direction
74 if self.accel_frames > 0:
75 self.accel_frames -= 1
76 self.owner.mx += self.mx
77 self.owner.my += self.my
78
79 if self.pc is None:
80 return created
81
82 if self.wait_frames > 0:
83 self.wait_frames -= 1
84 return created
85
86 while True:
87 self.pc += 1
88 if self.pc >= len(self.actions):
89 self.repeat -= 1
90 if self.repeat <= 0:
91 if self.parent is not None:
92 self.parent.copy_state(self)
93 self.owner.replace(self, self.parent)
94 break
95 else:
96 self.pc = 0
97
98 action = self.actions[self.pc]
99
100 if isinstance(action, parser.Repeat):
101 repeat, (actions, params) = action(self.params, self.rank)
102 child = Action(self.owner, self, actions, params, self.rank)
103 child.repeat = repeat
104 self.owner.replace(self, child)
105 created.extend(child.step())
106 break
107
108 elif isinstance(action, (parser.ActionDef, parser.ActionRef)):
109 action, params = action(self.params, self.rank)
110 child = Action(self.owner, self, actions, params, self.rank)
111 self.owner.replace(self, child)
112 created.extend(child.step())
113 break
114
115 elif isinstance(action, (parser.FireDef, parser.FireRef)):
116 direction, speed, actions = action(self.params, self.rank)
117 if direction:
118 direction, type = direction
119 if type == "aim":
120 direction += self.owner.aim
121 elif type == "sequence":
122 direction += self.previous_fire_direction
123 elif type == "relative":
124 direction += self.owner.direction
125 else:
126 direction = self.owner.aim
127 self.previous_fire_direction = direction
128
129
130 if speed:
131 speed, type = speed
132 if type == "sequence":
133 speed += self.previous_fire_speed
134 elif type == "relative":
135 # FIXME(jfw): Reference impl uses prvFireSpeed
136 # here? That doesn't seem right at all.
137 speed += self.owner.speed
138 else:
139 speed = 1
140 self.previous_fire_speed = speed
141
142 bullet = Bullet(self.owner.x, self.owner.y, direction, speed,
143 self.owner.target, actions, self)
144 created.append(bullet)
145
146 elif isinstance(action, parser.ChangeSpeed):
147 frames, (speed, type) = action(self.params, self.rank)
148 self.speed_frames = frames
149 if type == "sequence":
150 self.speed = speed
151 elif type == "relative":
152 self.speed = speed / frames
153 else:
154 self.speed = (speed - self.owner.speed) / frames
155
156 elif isinstance(action, parser.ChangeDirection):
157 frames, (direction, type) = action(self.params, self.rank)
158 self.direction_frames = frames
159 self.aiming = False
160 if type == "sequence":
161 self.aiming = False
162 self.direction = direction
163 else:
164 if type == "absolute":
165 self.aiming = False
166 self.direction = (
167 direction - self.owner.direction) % 360
168 elif type == "relative":
169 self.aiming = False
170 self.direction = direction
171 else:
172 self.aiming = True
173 self.direction = (
174 direction
175 + self.owner.aim
176 - self.owner.direction) % 360
177
178 while self.direction > 180:
179 self.direction -= 360
180 while self.direction < -180:
181 self.direction += 360
182 self.direction /= self.direction_frames
183
184 elif isinstance(action, parser.Accel):
185 frames, horizontal, vertical = action(self.params, self.rank)
186 self.accel_frames = frames
187 if horizontal:
188 mx, type = horizontal
189 if type == "sequence":
190 self.mx = mx
191 elif type == "absolute":
192 self.mx = (mx - bullet.mx) / frames
193 elif type == "relative":
194 self.mx = mx / frames
195 if vertical:
196 my, type = vertical
197 if type == "sequence":
198 self.my = my
199 elif type == "absolute":
200 self.my = (my - bullet.my) / frames
201 elif type == "relative":
202 self.my = my / frames
203
204 elif isinstance(action, parser.Wait):
205 self.wait_frames = action(self.params, self.rank)
206 break
207
208 elif isinstance(action, parser.Vanish):
209 self.owner.vanish()
210 break
211
212 return created
213
214 class Bullet(object):
215 """Simple bullet implementation."""
216
217 def __init__(self, x=0, y=0, direction=0, speed=0, target=None,
218 actions=(), parent=None):
219 self.x = self.px = x
220 self.y = self.py = y
221 self.mx = 0
222 self.my = 0
223 self.direction = direction
224 self.speed = speed
225 self.vanished = False
226 self.target = target
227 self.actions = []
228 if actions and not parent:
229 raise errors.Error
230 for action, params in actions:
231 self.actions.append(
232 Action(self, parent, action, params, parent.rank))
233
234 def __repr__(self):
235 return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
236 "actions=%r, target=%r, vanished=%r)") % (
237 type(self).__name__, self.x, self.y, (self.mx, self.my),
238 self.direction, self.speed, self.actions, self.target,
239 self.vanished)
240
241 @property
242 def aim(self):
243 """Angle to the target."""
244 if self.target is None:
245 return self.direction
246 else:
247 return math.atan2(self.target.x - self.x, self.target.y - self.y)
248
249 def vanish(self):
250 """Vanish this bullet and stop all actions."""
251 self.vanished = True
252 for action in self.actions:
253 action.vanish()
254 self.actions = []
255
256 def replace(self, old, new):
257 try:
258 idx = self.actions.index(old)
259 except ValueError:
260 pass
261 else:
262 self.actions[idx] = new
263
264 def step(self):
265 created = []
266
267 for action in self.actions:
268 created.extend(action.step())
269
270 direction = math.degrees(self.direction)
271 self.x += self.mx + math.sin(direction) * self.speed
272 self.y += self.my + math.cos(direction) * self.speed
273
274 return created