2012-10-12 13:50:02 +02:00
|
|
|
# This file is part of A Robot's Conundrum.
|
2012-08-11 02:22:29 +02:00
|
|
|
#
|
2012-10-12 13:50:02 +02:00
|
|
|
# A Robot's Conundrum is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU General Public License as published by the Free
|
|
|
|
# Software Foundation, either version 3 of the License, or (at your option) any
|
|
|
|
# later version.
|
2012-08-11 02:22:29 +02:00
|
|
|
#
|
2012-10-12 13:50:02 +02:00
|
|
|
# A Robot's Conundrum is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2012-08-11 02:22:29 +02:00
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
2012-10-12 13:50:02 +02:00
|
|
|
# A Robot's Conundrum. If not, see <http://www.gnu.org/licenses/>.
|
2012-08-11 02:22:29 +02:00
|
|
|
#
|
|
|
|
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
|
|
|
#
|
|
|
|
# level1.py
|
|
|
|
# --------------------
|
|
|
|
# date created : Fri Aug 10 2012
|
|
|
|
# copyright : (C) 2012 Niels G. W. Serup
|
2012-10-12 13:50:02 +02:00
|
|
|
# maintained by : Niels G. W. Serup <ngws@metanohi.name>
|
2012-08-11 02:22:29 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
The fourth level.
|
|
|
|
"""
|
|
|
|
|
2012-08-13 16:20:45 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2012-08-11 02:22:29 +02:00
|
|
|
import os
|
|
|
|
import pygame
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import itertools
|
|
|
|
|
|
|
|
import level
|
|
|
|
import player
|
|
|
|
import tile
|
|
|
|
import block
|
|
|
|
import lever
|
2012-08-13 16:20:45 +02:00
|
|
|
from mirror import Mirror
|
|
|
|
from laser import Laser
|
2012-08-11 02:22:29 +02:00
|
|
|
import misc
|
|
|
|
import worldobject
|
|
|
|
|
|
|
|
import logic.lasermirror as lm
|
2012-08-12 17:01:58 +02:00
|
|
|
from logic.direction import *
|
2012-08-11 02:22:29 +02:00
|
|
|
|
|
|
|
class Level4(level.Level):
|
|
|
|
def __init__(self, game, graphics_dir, paused=False):
|
|
|
|
level.Level.__init__(self, game, graphics_dir,
|
2012-08-21 18:36:51 +02:00
|
|
|
size=(64 * 17, 48 * 21), paused=paused)
|
2012-08-11 02:22:29 +02:00
|
|
|
|
2012-08-21 18:36:51 +02:00
|
|
|
self.dimensions = 17, 17
|
2012-08-11 02:22:29 +02:00
|
|
|
|
2012-08-21 18:36:51 +02:00
|
|
|
not_ok_pos = set(itertools.product(range(7, 10), range(1, 4)))
|
|
|
|
for i, j in filter(lambda p: p not in not_ok_pos, self._positions()):
|
|
|
|
self.tiles.append(
|
|
|
|
tile.Tile(self, i * 64, (j + 4) * 48, self.imgs[
|
|
|
|
('indoor%d' % random.randint(1, 6))
|
|
|
|
if random.randrange(6) != 0 else 'ground1']))
|
2012-08-11 02:22:29 +02:00
|
|
|
|
|
|
|
self.draw_background()
|
2012-08-12 18:28:09 +02:00
|
|
|
|
2012-08-11 21:47:38 +02:00
|
|
|
self.playfield = lm.generate_simple_playfield(16)
|
|
|
|
|
2012-08-13 21:42:46 +02:00
|
|
|
self.target_blocks = []
|
2012-08-11 21:47:38 +02:00
|
|
|
for (x, y), t in self.playfield.items():
|
2012-08-13 00:05:44 +02:00
|
|
|
x1, y1 = 64 * x, 48 * (y + 4)
|
2012-08-12 17:01:58 +02:00
|
|
|
if isinstance(t, lm.Source):
|
2012-08-13 21:42:46 +02:00
|
|
|
self.objects.append(
|
|
|
|
block.LaserSource(self, x1, y1, t.direction))
|
2012-08-12 17:01:58 +02:00
|
|
|
continue
|
2012-08-11 21:47:38 +02:00
|
|
|
def mir(b, x1, y1):
|
|
|
|
def f(x, y):
|
|
|
|
def g(setting):
|
|
|
|
self.playfield[(x, y)] = lm.MirrorLeft \
|
|
|
|
if self.playfield[(x, y)] is lm.MirrorRight \
|
|
|
|
else lm.MirrorRight
|
|
|
|
self.generate_lasers()
|
|
|
|
return g
|
2012-08-13 16:20:45 +02:00
|
|
|
return Mirror(self, x, y, b, links=[f(x, y)])
|
2012-08-13 21:42:46 +02:00
|
|
|
def targ():
|
|
|
|
b = block.LaserTarget(self, x, y)
|
|
|
|
self.target_blocks.append(b)
|
|
|
|
return b
|
2012-08-11 02:22:29 +02:00
|
|
|
self.objects.append({
|
2012-08-13 21:42:46 +02:00
|
|
|
lm.MirrorLeft: lambda: mir(True, x1, y1),
|
|
|
|
lm.MirrorRight: lambda: mir(False, x1, y1),
|
|
|
|
lm.Lever: lambda: lever.Lever(
|
2012-08-11 21:47:38 +02:00
|
|
|
self, x1, y1, [lambda setting: self.generate_lasers],
|
|
|
|
toggling=True,
|
|
|
|
anim='lever_leftright' if x in (0, 15)
|
2012-08-11 02:22:29 +02:00
|
|
|
else 'lever_updown'),
|
2012-08-13 21:42:46 +02:00
|
|
|
lm.Target: targ,
|
|
|
|
lm.Blocker: lambda: block.Block(self, x1, y1,
|
|
|
|
self.imgs['block1'],
|
|
|
|
movable=False)
|
|
|
|
}[t]())
|
2012-08-13 16:20:45 +02:00
|
|
|
mirrors = list(filter(lambda obj: isinstance(obj, Mirror),
|
2012-08-11 02:22:29 +02:00
|
|
|
self.objects))
|
|
|
|
levers = list(filter(lambda obj: isinstance(obj, lever.Lever),
|
|
|
|
self.objects))
|
|
|
|
random.shuffle(levers)
|
|
|
|
for l in levers:
|
|
|
|
m = min(mirrors, key=lambda m: misc.manhattan_dist(
|
|
|
|
(m.x, m.y), (l.x, l.y)))
|
|
|
|
mirrors.remove(m)
|
2012-08-11 14:30:46 +02:00
|
|
|
l.links.insert(0, (lambda m: lambda setting: m.rotate())(m))
|
|
|
|
|
2012-08-21 18:36:51 +02:00
|
|
|
top = self.dimensions[0]
|
2012-08-13 21:42:46 +02:00
|
|
|
for i in range(top):
|
2012-08-13 00:05:44 +02:00
|
|
|
if not i % 3:
|
2012-08-13 21:42:46 +02:00
|
|
|
self.objects.append(block.Block(
|
|
|
|
self, i * 64, 48 * 3,
|
|
|
|
self.imgs['wall'],
|
|
|
|
width=1 if i == top - 1 else 3,
|
|
|
|
blit_area=(0, 0, 96, 192) if i == top - 1 else None))
|
2012-08-13 00:05:44 +02:00
|
|
|
self.objects.append(block.InvisBlock(self, i * 64,
|
|
|
|
self.size[1]))
|
2012-08-21 18:36:51 +02:00
|
|
|
for i in range(self.dimensions[1]):
|
2012-08-23 17:31:15 +02:00
|
|
|
self.objects.append(block.InvisBlock(self, - 64, (i + 4) * 48))
|
|
|
|
self.objects.append(block.InvisBlock(self, self.size[0], (i + 4) * 48))
|
2012-08-13 00:05:44 +02:00
|
|
|
|
2012-08-11 14:30:46 +02:00
|
|
|
self.generate_lasers()
|
2012-08-11 02:22:29 +02:00
|
|
|
|
2012-08-21 18:36:51 +02:00
|
|
|
self.bottom_objects.append(worldobject.WithBackground(
|
|
|
|
self, self.imgs['elevator_top'], 64 * 7, 48 * 5))
|
|
|
|
self.elevator = worldobject.WithBackground(
|
|
|
|
self, self.imgs['elevator'], 64 * 7, 48 * 6, 48 * 3,
|
|
|
|
(0, 0, 256, 192 - 24 - 48 * 2))
|
|
|
|
self.bottom_objects.append(self.elevator)
|
|
|
|
self.bottom_objects.append(worldobject.WithBackground(
|
|
|
|
self, self.imgs['elevator_bottom'], 64 * 7, 48 * 7))
|
|
|
|
|
|
|
|
self.player.set_pos(64 * 8, 48 * 5)
|
2012-08-11 02:22:29 +02:00
|
|
|
self.player.set_init_pos()
|
2012-08-21 18:36:51 +02:00
|
|
|
self._old_player_update = self.player.update
|
|
|
|
self.player.update = lambda e, t, dt: self._old_player_update([], t, dt)
|
|
|
|
|
|
|
|
self._start_time = pygame.time.get_ticks()
|
2012-08-11 02:22:29 +02:00
|
|
|
|
2012-08-13 21:42:46 +02:00
|
|
|
def on_completion(self):
|
|
|
|
# End game here.
|
|
|
|
print('Well done.')
|
|
|
|
|
2012-08-11 02:22:29 +02:00
|
|
|
def restart(self):
|
|
|
|
for obj in self.objects:
|
|
|
|
obj.reset_pos()
|
|
|
|
|
2012-08-11 14:30:46 +02:00
|
|
|
def generate_lasers(self):
|
2012-08-13 21:42:46 +02:00
|
|
|
lasers = lm.generate_lasers(self.playfield)
|
|
|
|
self.lasers_orig = list(itertools.chain(*lasers))
|
|
|
|
self.lasers = []
|
|
|
|
for laser in lasers:
|
|
|
|
laser = iter(laser)
|
|
|
|
self.lasers.append(Laser(self, next(laser), first_laser=True))
|
|
|
|
self.lasers.extend(Laser(self, line) for line in laser)
|
|
|
|
for b in self.target_blocks:
|
|
|
|
b.new_playfield_update()
|
|
|
|
if all(b.glows for b in self.target_blocks):
|
|
|
|
self.on_completion()
|
2012-08-12 18:28:09 +02:00
|
|
|
|
2012-08-13 21:42:46 +02:00
|
|
|
def update(self, e, t, dt):
|
2012-08-21 18:36:51 +02:00
|
|
|
self.update2(e, t, dt)
|
|
|
|
start_offset = (t - self._start_time) / 25
|
|
|
|
if start_offset >= 96:
|
|
|
|
start_offset = 96
|
|
|
|
self.player.y = 48 * 7 - start_offset
|
|
|
|
self.elevator.z_px = 48 * 3 - start_offset
|
|
|
|
self.elevator.blit_area = (0, 0, 256, 192 - 24 - 48 * 2 + start_offset)
|
|
|
|
if start_offset == 96:
|
|
|
|
self.update = self.update2
|
|
|
|
self.player.update = self._old_player_update
|
|
|
|
|
|
|
|
def update2(self, e, t, dt):
|
2012-08-13 21:42:46 +02:00
|
|
|
level.Level.update(self, e, t, dt)
|
|
|
|
for laser in self.lasers:
|
|
|
|
laser.update(e, t, dt)
|
|
|
|
|
2012-08-11 14:30:46 +02:00
|
|
|
def draw(self, window):
|
|
|
|
self._blit_background(window)
|
|
|
|
|
2012-08-21 18:36:51 +02:00
|
|
|
for obj in self._sorted_objs(self.bottom_objects):
|
|
|
|
try:
|
|
|
|
obj.draw(window)
|
|
|
|
except IndexError:
|
|
|
|
print("Skipping frames ...")
|
|
|
|
|
2012-08-12 20:43:14 +02:00
|
|
|
objs = self._sorted_objs(self.objects + self.lasers)
|
2012-08-13 16:20:45 +02:00
|
|
|
objs = self._after_sort(itertools.groupby(
|
|
|
|
objs, lambda obj: obj.y + obj.z))
|
2012-08-11 21:47:38 +02:00
|
|
|
for obj in objs:
|
2012-08-11 14:30:46 +02:00
|
|
|
obj.draw(window)
|
|
|
|
|
|
|
|
self.darkness.draw(window)
|
2012-08-12 20:43:14 +02:00
|
|
|
|
|
|
|
def _after_sort(self, objss):
|
2012-08-13 00:05:44 +02:00
|
|
|
n_objs = []
|
|
|
|
for c, objs in objss:
|
|
|
|
n_objs.extend(self._after_sort_line(list(objs)))
|
|
|
|
return n_objs
|
2012-08-12 20:43:14 +02:00
|
|
|
|
|
|
|
def _after_sort_line(self, objs):
|
2012-08-13 21:42:46 +02:00
|
|
|
is_special = lambda obj: type(obj) in \
|
|
|
|
(Mirror, Laser, block.LaserSource, block.LaserTarget)
|
2012-08-13 16:20:45 +02:00
|
|
|
specials, nonspecials = (filter(is_special, objs),
|
|
|
|
filter(lambda obj: not is_special(obj), objs))
|
2012-08-12 20:43:14 +02:00
|
|
|
return nonspecials + self._sort_line_specials(specials)
|
|
|
|
|
|
|
|
def _sort_line_specials(self, objs):
|
2012-08-13 16:20:45 +02:00
|
|
|
mirrors = filter(lambda obj: isinstance(obj, Mirror), objs)
|
|
|
|
lasers = filter(lambda obj: isinstance(obj, Laser), objs)
|
2012-08-13 21:42:46 +02:00
|
|
|
sources = filter(lambda obj: isinstance(obj, block.LaserSource), objs)
|
|
|
|
targets = filter(lambda obj: isinstance(obj, block.LaserTarget), objs)
|
2012-08-13 16:20:45 +02:00
|
|
|
|
|
|
|
lasers_back = set(lasers)
|
|
|
|
sep_ords = []
|
2012-08-13 21:42:46 +02:00
|
|
|
for obj in itertools.chain(mirrors, targets):
|
2012-08-13 16:20:45 +02:00
|
|
|
before, after = [], []
|
2012-08-13 21:42:46 +02:00
|
|
|
for laser in _hit_lasers(obj, lasers):
|
2012-08-13 16:20:45 +02:00
|
|
|
lasers_back.discard(laser)
|
2012-08-13 21:42:46 +02:00
|
|
|
if _obj_is_behind_laser(obj, laser):
|
2012-08-13 16:20:45 +02:00
|
|
|
after.append(laser)
|
|
|
|
else:
|
|
|
|
before.append(laser)
|
2012-08-13 21:42:46 +02:00
|
|
|
sep_ords.append(before + [obj] + after)
|
|
|
|
points = set((obj.x0, obj.y0) for obj in itertools.chain(mirrors, targets))
|
|
|
|
for laser in filter(lambda laser: (laser.x0, laser.y0) in points
|
|
|
|
and (laser.x1, laser.y1) in points, lasers):
|
2012-08-13 16:20:45 +02:00
|
|
|
# print(laser)
|
|
|
|
xs, ys = filter(lambda sep_ord: laser in sep_ord, sep_ords)
|
|
|
|
sep_ords.remove(xs)
|
|
|
|
sep_ords.remove(ys)
|
|
|
|
|
|
|
|
xs, ys, nobjs = iter(xs), iter(ys), []
|
|
|
|
while True:
|
|
|
|
x = next(xs)
|
|
|
|
if x is laser:
|
|
|
|
break
|
|
|
|
nobjs.append(x)
|
|
|
|
while True:
|
|
|
|
x = next(ys)
|
|
|
|
if x is laser:
|
|
|
|
break
|
|
|
|
nobjs.append(x)
|
|
|
|
|
|
|
|
nobjs.append(laser)
|
|
|
|
nobjs.extend(xs)
|
|
|
|
nobjs.extend(ys)
|
|
|
|
sep_ords.append(nobjs)
|
|
|
|
|
2012-08-13 21:42:46 +02:00
|
|
|
objs = list(itertools.chain(*sep_ords)) + list(lasers_back) \
|
|
|
|
+ sources
|
2012-08-13 16:20:45 +02:00
|
|
|
return objs
|
|
|
|
|
2012-08-13 21:42:46 +02:00
|
|
|
def _hit_lasers(obj, lasers):
|
|
|
|
p = (obj.x0, obj.y0)
|
2012-08-13 16:20:45 +02:00
|
|
|
return filter(lambda laser: (laser.x0, laser.y0) == p
|
|
|
|
or (laser.x1, laser.y1) == p, lasers)
|
|
|
|
|
2012-08-13 21:42:46 +02:00
|
|
|
def _obj_is_behind_laser(obj, laser):
|
|
|
|
return (_mirror_is_behind_laser
|
|
|
|
if isinstance(obj, Mirror)
|
|
|
|
else _target_is_behind_laser)(obj, laser)
|
|
|
|
|
2012-08-13 16:20:45 +02:00
|
|
|
def _mirror_is_behind_laser(mirror, laser):
|
|
|
|
return \
|
|
|
|
laser.y0 == laser.y1 \
|
|
|
|
and (
|
|
|
|
(mirror.left_up
|
|
|
|
and (
|
|
|
|
(laser.x0 == mirror.x0 and laser.x1 > mirror.x0)
|
|
|
|
or
|
|
|
|
(laser.x1 == mirror.x0 and laser.x0 > mirror.x0)
|
|
|
|
)
|
|
|
|
) \
|
|
|
|
or \
|
|
|
|
(not mirror.left_up
|
|
|
|
and (
|
|
|
|
(laser.x0 == mirror.x0 and laser.x1 < mirror.x0)
|
|
|
|
or
|
|
|
|
(laser.x1 == mirror.x0 and laser.x0 < mirror.x0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2012-08-13 21:42:46 +02:00
|
|
|
|
|
|
|
def _target_is_behind_laser(target, laser):
|
|
|
|
return laser.x0 != laser.x1
|