summaryrefslogtreecommitdiff
path: root/bubbob/ext5/__init__.py
blob: ef73f14c6381c0b565067d0e1be667240a3f8899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from __future__ import generators
import os, random
import images, gamesrv
from images import ActiveSprite
import boards
from boards import CELL, HALFCELL, bget
from monsters import Monster
from player import Dragon, BubPlayer
from bubbles import Bubble
import bonuses

LocalDir = os.path.basename(os.path.dirname(__file__))

localmap = {
    ('lem-walk', 1,0) :  ('image1.ppm', (  0,  0, 32, 32)),
    ('lem-walk', 1,1) :  ('image1.ppm', ( 32,  0, 32, 32)),
    ('lem-walk', 1,2) :  ('image1.ppm', ( 64,  0, 32, 32)),
    ('lem-walk', 1,3) :  ('image1.ppm', ( 96,  0, 32, 32)),
    ('lem-walk', 1,4) :  ('image1.ppm', (128,  0, 32, 32)),
    ('lem-walk', 1,5) :  ('image1.ppm', (160,  0, 32, 32)),
    ('lem-walk', 1,6) :  ('image1.ppm', (192,  0, 32, 32)),
    ('lem-walk', 1,7) :  ('image1.ppm', (224,  0, 32, 32)),
    ('lem-fall', 1,0) :  ('image1.ppm', (256,  0, 32, 32)),
    ('lem-fall', 1,1) :  ('image1.ppm', (288,  0, 32, 32)),
    ('lem-fall', 1,2) :  ('image1.ppm', (320,  0, 32, 32)),
    ('lem-fall', 1,3) :  ('image1.ppm', (352,  0, 32, 32)),

    ('lem-fall',-1,3) :  ('image2.ppm', (  0,  0, 32, 32)),
    ('lem-fall',-1,2) :  ('image2.ppm', ( 32,  0, 32, 32)),
    ('lem-fall',-1,1) :  ('image2.ppm', ( 64,  0, 32, 32)),
    ('lem-fall',-1,0) :  ('image2.ppm', ( 96,  0, 32, 32)),
    ('lem-walk',-1,7) :  ('image2.ppm', (128,  0, 32, 32)),
    ('lem-walk',-1,6) :  ('image2.ppm', (160,  0, 32, 32)),
    ('lem-walk',-1,5) :  ('image2.ppm', (192,  0, 32, 32)),
    ('lem-walk',-1,4) :  ('image2.ppm', (224,  0, 32, 32)),
    ('lem-walk',-1,3) :  ('image2.ppm', (256,  0, 32, 32)),
    ('lem-walk',-1,2) :  ('image2.ppm', (288,  0, 32, 32)),
    ('lem-walk',-1,1) :  ('image2.ppm', (320,  0, 32, 32)),
    ('lem-walk',-1,0) :  ('image2.ppm', (352,  0, 32, 32)),

    ('lem-jail',   0) :  ('image4.ppm', (  0,  0, 32, 32)),
    ('lem-jail',   1) :  ('image4.ppm', (  0, 32, 32, 32)),
    ('lem-jail',   2) :  ('image4.ppm', (  0, 64, 32, 32)),
    }
for n in range(16):
    localmap[('lem-crash', n)] = ('image3.ppm', (32*n, 0, 32, 32))

music = gamesrv.getmusic(os.path.join(LocalDir, 'music.wav'))
snd_ouch = gamesrv.getsample(os.path.join(LocalDir, 'ouch.wav'))


class Lemmy:
    right     = [('lem-walk', 1,n) for n in range(8)]
    left      = [('lem-walk',-1,n) for n in range(8)]
    jailed    = [('lem-jail',   n) for n in range(3)]


class Lemming(Monster):

    def __init__(self, lemmings, x, y, dir):
        Monster.__init__(self, Lemmy, x, y, dir, in_list=lemmings.lemlist)
        self.lemmings = lemmings

    def argh(self, *args, **kwds):
        self.untouchable()
        self.gen = [self.jumpout()]

    def resetimages(self):
        pass

    def touched(self, dragon):
        if 20 >= abs(self.x - dragon.x) >= 14:
            if self.x < dragon.x:
                self.dir = -1
            else:
                self.dir = 1

    def in_bubble(self, bubble):
        self.move(bubble.x, bubble.y)
        Monster.in_bubble(self, bubble)
        return -1

    def bubbling(self, bubble):
        dx = random.randrange(-3, 4)
        dy = random.randrange(-4, 2)
        counter = 0
        while not hasattr(bubble, 'poplist'):
            if self.y < -CELL and bubble.y > CELL:  # bubble wrapped
                self.leaveboard(bubble)
                return
            self.move(bubble.x+dx, bubble.y+dy)
            yield None
        if bubble.poplist is None and bubble.y <= -2*CELL+1:
            self.leaveboard(bubble)
            return
        self.setimages(None)
        self.gen = [self.jumpout()]

    def jumpout(self):
        # jumping out of the bubble
        self.seticon(images.sprget(self.mdef.jailed[1]))
        dxy = [(random.random()-0.5) * 9.0,
               (random.random()+0.5) * (-5.0)]
        for n in self.parabolic(dxy):
            yield n
            if dxy[1] >= 2.0:
                break
        if dxy[0] < 0:
            self.dir = -1
        else:
            self.dir = 1
        self.touchable = 1
        self.gen.append(self.falling())

    def falling(self):
        self.setimages(None)
        n = 0
        lemmap = self.lemmings.lemmap
        while not self.onground():
            yield None
            self.move(self.x, (self.y + 4) & ~3,
                      lemmap['lem-fall', self.dir, n&3])
            n += 1
            if self.y >= boards.bheight:
                self.kill()
                return
            yield None
        if n <= 33:
            self.gen.append(self.walking())
        else:
            self.play(snd_ouch)
            self.untouchable()
            self.to_front()
            self.gen = [self.die([('lem-crash', n) for n in range(16)], 2)]

    def walking(self):
        self.setimages(None)
        n = 0
        lemmap = self.lemmings.lemmap
        y0 = self.y // 16
        while self.y == y0*16:
            yield None
            nx = self.x + self.dir*2
            x0 = (nx+15) // 16
            if bget(x0, y0+1) == ' ':
                if bget(x0, y0+2) == ' ':
                    y0 += 1  # fall
            elif bget(x0, y0) != ' ':
                self.dir = -self.dir
                self.resetimages()
                continue
            else:  # climb
                y0 -= 1
                n2 = 0
                while self.y > y0*16:
                    self.step(0, -2)
                    if n2:
                        n2 -= 1
                    else:
                        self.seticon(lemmap['lem-walk', self.dir, n&7])
                        n += 1
                        n2 = 2
                    yield None
            self.move(nx, self.y, lemmap['lem-walk', self.dir, n&7])
            n += 1
            yield None
            yield None
        self.gen.append(self.falling())

    def onground(self):
        if self.y & 15:
            return 0
        x0 = (self.x+15) // 16
        y0 = self.y // 16 + 2
        return bget(x0, y0) != ' ' == bget(x0, y0-1)

    def leaveboard(self, bubble):
        if hasattr(bubble, 'd'):
            bubble.play(images.Snd.Extra)
            score = self.lemmings.score
            bubber = bubble.d.bubber
            score[bubber] = score.get(bubber, 0) + 1
            bonuses.points(bubble.x, bubble.y, bubble.d, 500)
        self.kill()

    default_mode = walking


class Lemmings:
    
    def bgen(self, limittime = 60.1): # 0:60
        self.score = {}
        for t in boards.initsubgame(music, self.displaypoints):
            yield t
        self.lemmap = {}
        for key in localmap:
            self.lemmap[key] = images.sprget(key)

        tc = boards.TimeCounter(limittime)
        self.lemlist = []
        self.lemtotal = 0
        for t in self.frame():
            t = boards.normal_frame()
            yield t
            tc.update(t)
            if tc.time == 0.0:
                break

        tc.restore()
        for s in self.lemlist[:]:
            if s.alive:
                s.kill()
        for s in images.ActiveSprites[:]:
            if isinstance(s, Bubble):
                s.pop()
        for t in boards.result_ranking(self.score.copy(), self.lemtotal):
            yield t

    def displaypoints(self, bubber):
        return self.score.get(bubber, 0)

    def frame(self):
        windline = '>>' + '^'*(curboard.width-4) + '<<'
        curboard.winds = [windline] * curboard.height

        countholes = 0
        ymax = curboard.height-1
        for x in range(2, curboard.width-2):
            if bget(x, ymax) == ' ':
                countholes += 1

        xrange = []
        try:
            for delta in range(2, curboard.width):
                for x in [delta, curboard.width-delta-1]:
                    if x in xrange: raise StopIteration
                    xrange.append(x)
        except StopIteration:
            pass

        for x in xrange:
            if countholes > curboard.width//6 and bget(x, ymax) == ' ':
                curboard.putwall(x, ymax)
                curboard.reorder_walls()
                countholes -= 1
            for y in range(0, ymax):
                if bget(x, y) == ' ':
                    break
                curboard.killwall(x, y)
            yield None

        testing = {}
        def addlemming():
            for x, y in testing.items():
                if bget(x, y) != ' ' == bget(x, y-1):
                    if x <= curboard.width//2:
                        dir = 1
                    else:
                        dir = -1
                    s = Lemming(self, x*CELL-HALFCELL, (y-2)*CELL, dir)
                    self.lemtotal += 1
                if y < ymax:
                    testing[x] = y+1
                else:
                    del testing[x]
        for x in xrange:
            testing[x] = 1
            addlemming()
            yield None
        while testing:
            addlemming()
            yield None

        while self.lemlist:
            yield None

# This game is suitable for at least min_players players
min_players = 1

def run():
    global curboard
    from boards import curboard
    boards.replace_boardgen(Lemmings().bgen())

def setup():
    for key, (filename, rect) in localmap.items():
        filename = os.path.join(LocalDir, filename)
        images.sprmap[key] = (filename, rect)
setup()