summaryrefslogtreecommitdiff
path: root/bubbob/boards.py
blob: dced6f5315a0ad71c0238d43a9ad8010f081d5a7 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
from __future__ import generators
import random, os, sys, math
import gamesrv
import images

CELL = 16    # this constant is inlined at some places, don't change
HALFCELL = CELL//2
FRAME_TIME = 0.025
#DEFAULT_LEVEL_FILE = 'levels/scratch.py'

BOARD_BKGND = 1    # 0 = black, 1 = darker larger wall tiles


class Copyable:
    pass   # see bonuses.py, class Clock


class Board(Copyable):
    letter    = 0
    fire      = 0
    lightning = 0
    water     = 0
    top       = 0
    
    WIND_DELTA = HALFCELL

    def __init__(self, num):
        # the subclasses should define 'walls', 'winds', 'monsters'
        self.walls = walls = [line for line in self.walls.split('\n') if line]
        self.winds = winds = [line for line in self.winds.split('\n') if line]
        self.num = num
        self.width = len(walls[0])
        self.height = len(walls)
        for line in walls:
            assert len(line) == self.width, "some wall lines are longer than others"
        for line in winds:
            assert len(line) == self.width, "some wind lines are longer than others"
        #assert walls[0] ==  walls[-1], "first and last lines must be identical"
        assert len(winds) == self.height, "wall and wind heights differ"
        self.walls_by_pos = {}
        self.sprites = {}
        if self.top:
            testline = self.walls[0]
        else:
            testline = self.walls[-1]
        self.holes = testline.find('  ') >= 0
        self.playingboard = 0
        self.bonuslevel = not self.monsters or (gamesrv.game.finalboard is not None and self.num >= gamesrv.game.finalboard)
        self.cleaning_gen_state = 0

    def set_musics(self, prefix=[]):
        if (self.num+1) % 20 < 10:
            gamesrv.set_musics(prefix + [images.music_intro], [images.music_game],
                               reset=0)
        else:
            gamesrv.set_musics(prefix + [], [images.music_game2], reset=0)

    def writesprites(self, name, xyicolist):
        sprlist = self.sprites.setdefault(name, [])
        xyicolist = xyicolist[:]
        xyicolist.reverse()
        for s in sprlist[:]:
            if xyicolist:
                s.move(*xyicolist.pop())
            else:
                s.kill()
                sprlist.remove(s)
        while xyicolist:
            x, y, ico = xyicolist.pop()
            sprlist.append(gamesrv.Sprite(ico, x, y))

    def enter(self, complete=1, inplace=0, fastreenter=False):
        global curboard
        if inplace:
            print "Re -",
        print "Entering board", self.num+1
        self.set_musics()
        # add board walls
        l = self.sprites.setdefault('walls', [])
        bl = self.sprites.setdefault('borderwalls', [])
        if inplace:
            deltay = 0
        else:
            deltay = bheight
        wnx = wny = 1
        while haspat((self.num, wnx, 0)):
            wnx += 1
        while haspat((self.num, 0, wny)):
            wny += 1
        self.wnx = wnx
        self.wny = wny

        if haspat((self.num, 'l')):
            lefticon = patget((self.num, 'l'))
            if haspat((self.num, 'r')):
                righticon = patget((self.num, 'r'))
            else:
                righticon = lefticon
            xrange = range(2, self.width-2)
        else:
            xrange = range(self.width)
            lefticon = righticon = None

        if BOARD_BKGND == 1:
            gl = self.sprites.setdefault('background', [])
            xmax = (self.width-2)*CELL
            ymax = self.height*CELL
            y = -HALFCELL
            ystep = 0
            firstextra = 1
            while y < ymax:
                x = 2*CELL+HALFCELL
                xstep = 0
                while x < xmax:
                    bitmap, rect = loadpattern((self.num, xstep, ystep),
                                               images.KEYCOL)
                    bitmap, rect = images.makebkgndpattern(bitmap, rect)
                    if firstextra:
                        # special position where a bit of black might show up
                        x -= rect[2]
                        xstep = (xstep-1) % wnx
                        firstextra = 0
                        continue
                    bkgndicon = bitmap.geticon(*rect)
                    w = gamesrv.Sprite(bkgndicon, x, y + deltay)
                    gl.append(w)
                    x += rect[2]
                    xstep = (xstep+1) % wnx
                y += rect[3]
                ystep = (ystep+1) % wny
        else:
            gl = []

        if lefticon is not None:
            for y in range(0, self.height, lefticon.h // CELL):
                bl.append(gamesrv.Sprite(lefticon, 0, y*CELL + deltay))

        for y in range(self.height):
            for x in xrange:
                c = self.walls[y][x]
                if c == '#':
                    wallicon = patget((self.num, x%wnx, y%wny), images.KEYCOL)
                    w = gamesrv.Sprite(wallicon, x*CELL, y*CELL + deltay)
                    l.append(w)
                    self.walls_by_pos[y,x] = w

        if not l:
            # self.sprites['walls'] must not be empty, for putwall
            wallicon = patget((self.num, 0, 0), images.KEYCOL)
            w = gamesrv.Sprite(wallicon, 0, -wallicon.h)
            l.append(w)

        if righticon is not None:
            for y in range(0, self.height, lefticon.h // CELL):
                bl.append(gamesrv.Sprite(righticon, (self.width-2)*CELL, y*CELL + deltay))

        while deltay:
            dy = -min(deltay, 8)
            for w in gl:
                w.step(0, dy)
            for w in l:
                w.step(0, dy)
            for w in bl:
                w.step(0, dy)
            deltay += dy
            yield 1

        if inplace:
            for w in images.ActiveSprites:
                w.to_front()

        curboard = self
        if gamesrv.game:
            gamesrv.game.updateboard()
        if not complete:
            return
        # add players
        from player import BubPlayer, scoreboard
        if not inplace:
            random.shuffle(BubPlayer.PlayerList)
        scoreboard(1, inplace=inplace)
        if not fastreenter:
            random.shuffle(BubPlayer.PlayerList)
            playing = []
            for p in BubPlayer.PlayerList:
                if p.isplaying():
                    p.enterboard(playing)
                    p.zarkon()
                    playing.append(p)
            for d in BubPlayer.DragonList:
                d.enter_new_board()
        else:
            # kill stuff left over from leave(inplace=1) (Big Clock bonus only)
            import bonuses
            keepme = bonuses.Points
            dragons = {}
            playing = []
            for p in BubPlayer.PlayerList:
                if p.isplaying():
                    for d in p.dragons:
                        if hasattr(d, 'dcap'):
                            d.dcap['shield'] = 90
                        dragons[d] = True
                    playing.append(p)
            for s in images.ActiveSprites[:]:
                if isinstance(s, keepme) or s in dragons:
                    pass
                else:
                    s.kill()
        # add monsters
        if not self.bonuslevel:
            import monsters
            f_monsters = gamesrv.game.f_monsters
            if f_monsters < 0.1:
                f_monsters = max(1.0, min(2.0, (len(playing)-2)/2.2+1.0))
            for mdef in self.monsters:
                if not fastreenter:
                    yield 2
                cls = getattr(monsters, mdef.__class__.__name__)
                dir = mdef.dir
                i = random.random()
                while i < f_monsters:
                    cls(mdef, dir=dir)
                    dir = -dir
                    i += 1.0
        self.playingboard = 1

    def putwall(self, x, y, w=None):
        wallicon = patget((self.num, x%self.wnx, y%self.wny), images.KEYCOL)
        if w is None:
            w = gamesrv.Sprite(wallicon, 0, bheight)
            l = self.sprites['walls']
            w.to_back(l[-1])
            l.append(w)
        self.walls_by_pos[y,x] = w
        if y >= 0:
            line = self.walls[y]
            self.walls[y] = line[:x] + '#' + line[x+1:]

    def killwall(self, x, y, kill=1):
        w = self.walls_by_pos[y,x]
        if kill:
            l = self.sprites['walls']
            if len(l) > 1:
                l.remove(w)
                w.kill()
            else:
                # self.sprites['walls'] must never be empty
                # or putwall will crash!
                w.move(0, -bheight)
        del self.walls_by_pos[y,x]
        line = self.walls[y]
        self.walls[y] = line[:x] + ' ' + line[x+1:]
        return w

    def reorder_walls(self):
        walls_by_pos = self.walls_by_pos
        items = [(yx, w1.ico) for yx, w1 in walls_by_pos.items()]
        if not items:
            return   # otherwise self.sprites['walls'] would be emptied
        items.sort()
        l = self.sprites['walls']
        while len(l) > len(items):
            l.pop().kill()
        assert len(items) == len(l)
        for ((y,x), ico), w2 in zip(items, l):
            w2.move(x*CELL, y*CELL, ico)
            walls_by_pos[y,x] = w2

    def leave(self, inplace=0):
        global curboard
        if not gamesrv.has_loop_music():
            gamesrv.fadeout(1.5)
        from player import BubPlayer
        for p in BubPlayer.PlayerList:
            if p.isplaying():
                p.savecaps()
        if BubPlayer.LeaveBonus:
            for t in BubPlayer.LeaveBonus:
                yield t
            BubPlayer.LeaveBonus = None
        curboard = None
        if inplace:
            i = -1
        else:
            while images.ActiveSprites:
                s = random.choice(images.ActiveSprites)
                s.kill()
                yield 0.9
            i = 0
        sprites = []
        for l in self.sprites.values():
            sprites += l
        self.sprites.clear()
        self.walls_by_pos.clear()
        random.shuffle(sprites)
        for s in sprites:
            s.kill()
            if i:
                i -= 1
            else:
                yield 0.32
                i = 3
        if not inplace:
            for p in BubPlayer.PlayerList:
                if p.isplaying():
                    p.zarkoff()
            yield 4

    def clean_gen_state(self):
        self.cleaning_gen_state = 1
        while len(BoardGen) > 1:
            #yield force_singlegen()
            #if 'flood' in self.sprites:
            #    for s in self.sprites['flood']:
            #        s.kill()
            #    del self.sprites['flood']
            yield normal_frame()
        self.cleaning_gen_state = 0

def bget(x, y):
    if 0 <= x < curboard.width:
        if y < 0 or y >= curboard.height:
            y = 0
        return curboard.walls[y][x]
    else:
        return '#'

def wget(x, y):
    delta = curboard.WIND_DELTA
    x = (x + delta) // 16
    y = (y + delta) // 16
    if 0 <= x < curboard.width:
        if y < 0:
            y = 0
        elif y >= curboard.height:
            y = -1
        return curboard.winds[y][x]
    elif x < 0:
        return '>'
    else:
        return '<'

def onground(x, y):
    if y & 15:
        return 0
    x0 = (x+5) // 16
    x1 = (x+16) // 16
    x2 = (x+27) // 16
    y0 = y // 16 + 2

    if x0 < 0 or x2 >= curboard.width:
        return 0
    y1 = y0 - 1
    if not (0 < y0 < curboard.height):
        if y0 != curboard.height:
            y1 = 0
        y0 = 0
    y0 = curboard.walls[y0]
    y1 = curboard.walls[y1]
    return (' ' == y1[x0] == y1[x1] == y1[x2] and
            not (' ' == y0[x0] == y0[x1] == y0[x2]))
    #return (' ' == bget(x0,y0-1) == bget(x1,y0-1) == bget(x2,y0-1) and
    #        not (' ' == bget(x0,y0) == bget(x1,y0) == bget(x2,y0)))
    #return (bget(x1,y0-1)==' ' and
    #        ((bget(x1,y0)=='#') or
    #         (bget(x0,y0)=='#' and bget(x0,y0-1)==' ') or
    #         (bget(x2,y0)=='#' and bget(x2,y0-1)==' ')))

def onground_nobottom(x, y):
    return onground(x, y) and y+32 < bheight

def underground(x, y):
    if y % CELL:
        return 0
    x0 = (x+5) // CELL
    x1 = (x+CELL) // CELL
    x2 = (x+2*CELL-5) // CELL
    y0 = y // CELL

    if x0 < 0 or x2 >= curboard.width:
        return 0
    y1 = y0 - 1
    if not (0 < y0 < curboard.height):
        if y0 != curboard.height:
            y1 = 0
        y0 = 0
    y0 = curboard.walls[y0]
    y1 = curboard.walls[y1]
    return (' ' == y0[x0] == y0[x1] == y0[x2] and
            not (' ' == y1[x0] == y1[x1] == y1[x2]))

def x2bounds(x):
    if x < 32:
        return 32
    elif x > bwidth - 64:
        return bwidth - 64
    else:
        return x

def vertical_warp(nx, ny):
    if ny >= bheight:
        ny -= bheightmod
    elif ny < -32:
        ny += bheightmod
    return nx, ny

def vertical_warp_sprite(spr):
    if spr.y >= bheight:
        spr.step(0, -bheightmod)
    elif spr.y < -32:
        spr.step(0, bheightmod)

##def vertical_warp(nx, ny):
##    if ny >= bheight:
##        ny -= bheightmod
##    elif ny < -32:
##        ny += bheightmod
##    else:
##        return (nx, ny), 0
##    from player import BubPlayer
##    if BubPlayer.Moebius:
##        nx = bwidth - 2*CELL - nx
##        return (nx, ny), 1
##    else:
##        return (nx, ny), 0


MODULES = ['boards', 'bonuses', 'bubbles', 'images',
           'mnstrmap', 'monsters', 'player', 'ranking',
           'binboards', 'macbinary', 'boarddef',
           'ext1', 'ext2', 'ext3', 'ext4', 'ext5', 'ext6', 'ext7']

def loadmodules(force=0):
    levelfilename = gamesrv.game.levelfile
    modulefiles = {None: levelfilename}
    for m in MODULES:
        if os.path.isfile(m+'.py'):
            modulefiles[m] = m+'.py'
        elif os.path.isfile(os.path.join(m, '__init__.py')):
            modulefiles[m] = os.path.join(m, '__init__.py')
    mtimes = {}
    for m, mfile in modulefiles.items():
        mtimes[m] = os.stat(mfile).st_mtime
    reload = force or (mtimes != getattr(sys, 'ST_MTIMES', None))
    import player
    playerlist = player.BubPlayer.PlayerList
    if reload:
        delete = hasattr(sys, 'ST_MTIMES')
        sys.ST_MTIMES = mtimes
        if delete:
            print "Reloading modules."
            for m, mfile in modulefiles.items():
                if m is not None and m in sys.modules:
                    del sys.modules[m]

    # Clear
    clearallsprites()

    import player
    for p in playerlist:
        player.upgrade(p)
    for n in range(len(playerlist), images.MAX):
        playerlist.append(player.BubPlayer(n))
    player.BubPlayer.PlayerList = playerlist
    if reload:
        import boards
        from images import haspat, loadpattern
        boards.haspat = haspat
        boards.loadpattern = loadpattern
        del boards.BoardList[:]
        if levelfilename.lower().endswith('.py'):
            levels = {}
            print 'Source level file:', levelfilename
            execfile(levelfilename, levels)
            if 'GenerateLevels' in levels:
                levels = levels['GenerateLevels']()
                if isinstance(levels, list):
                    levels = dict(zip(range(len(levels)), levels))
        else:
            import binboards
            levels = binboards.load(levelfilename)
        boards.register(levels)
    return reload

def clearallsprites():
    gamesrv.clearsprites()
    import images
    del images.ActiveSprites[:]
    images.SpritesByLoc.clear()

def wait_for_one_player():
    from player import BubPlayer
    clearallsprites()
    nimages = None
    while not [p for p in BubPlayer.PlayerList if p.isplaying()]:
        yield 3
        if not nimages:
            desc = getattr(gamesrv.game, 'FnDesc', '?')
            host, port = getattr(gamesrv.game, 'address', ('?', '?'))
            images.writestrlines([
                "Welcome to",
                desc.upper(),
                "at %s:%s" % (host.lower(), port),
                None,
                "Click on your Favorite Color's Dragon",
                "Choose four keys: Right, Left, Jump, Shoot",
                "and Let's Go!",
                None,
                "Click again for more than one player",
                "on the same machine.",
                ])
            
            from mnstrmap import PlayerBubbles
            nimages = [PlayerBubbles.bubble[0],
                       PlayerBubbles.bubble[1],
                       PlayerBubbles.bubble[1],
                       PlayerBubbles.bubble[0],
                       PlayerBubbles.bubble[2],
                       PlayerBubbles.bubble[2]]
            screenwidth = bwidth + 9*CELL
            screenheight = bheight

            def welcomebubbling(self):
                fx = self.x
                dx = (random.random() - 0.5) * 1.9
                for y in range(self.y-3, -self.ico.h, -3):
                    fx += dx
                    self.move(int(fx), y)
                    yield None
                    if y == self.ypop:
                        from mnstrmap import PlayerBubbles
                        self.setimages(None)
                        self.gen.append(self.die(PlayerBubbles.explosion))
                self.kill()

            yield 10
            gamesrv.set_musics([], [images.music_game2], reset=0)
        
        if ((not images.ActiveSprites or random.random() < 0.05678) and
            gamesrv.clients):
            # make sure the extension's images are loaded too
            # NB. this is also needed for import auto-detection
            import ext1; import ext2; import ext3
            import ext4; import ext5; import ext6
            import ext7
            
            ico = images.sprget(nimages[0])
            s = images.ActiveSprite(ico,
                                    random.randrange(0, screenwidth-ico.w),
                                    screenheight)
            s.ypop = random.randrange(-ico.h, screenheight)
            s.gen = [welcomebubbling(s)]
            s.setimages(s.cyclic(nimages, speed=1))
            if random.random() > 0.4321:
                try:
                    key, (filename, (x, y, w, h)) = random.choice(
                        images.sprmap.items())
                except:
                    w = h = 0
                if w == h == 32:
                    s2 = images.ActiveSprite(images.sprget(key), -32, 0)
                    s2.gen = [s2.following(s, (s.ico.w-32)//2, (s.ico.h-32)//2)]
                    s.ypop = None
        images.action(images.ActiveSprites[:])

def patget(n, keycol=None):
    bitmap, rect = loadpattern(n, keycol)
    return bitmap.geticon(*rect)

def get_lives():
    return gamesrv.game.limitlives

def do_nothing():
    while True:
        yield 5

BoardList = []
curboard = None
BoardGen = [do_nothing()]

def next_board(num=0, complete=1, fastreenter=False):
    yield force_singlegen()
    set_frametime(1.0)
    brd = curboard
    inplace = 0
    if brd:
        inplace = brd.bonuslevel or fastreenter
        num = brd.num
        if not inplace:
            num += gamesrv.game.stepboard
            if num >= len(BoardList):
                num = len(BoardList)-1
            if (gamesrv.game.finalboard is not None and num > gamesrv.game.finalboard):
                num = gamesrv.game.finalboard
        for t in brd.leave(inplace=inplace):
            yield t

    # reset global board state
    from player import BubPlayer, reset_global_board_state
    reset_global_board_state()
    if not inplace:
        del BubPlayer.MonsterList[:]
        # wait for at least one player
        for t in wait_for_one_player():
            yield t
        # reload modules if changed
        if loadmodules():
            import boards
            boards.BoardGen = [boards.next_board(num)]
            return

    if num < 0:
        num = 0
    elif num >= len(BoardList):
        num = len(BoardList)-1
    brd = BoardList[num](num)
    for t in brd.enter(complete, inplace=inplace, fastreenter=fastreenter):
        yield t

    if brd.bonuslevel:
        gen = bonus_play
    else:
        gen = normal_play
    BoardGen[0] = gen()

def set_frametime(ft, privtime=100):
    from player import BubPlayer
    BubPlayer.BaseFrametime = ft
    BubPlayer.PlayersPrivateTime = privtime
    images.loadsounds(1.0 / ft)

def extra_boardgen(gen, at_end=0):
    if curboard.playingboard:
        if at_end or not BoardGen:
            BoardGen.append(gen)
        else:
            BoardGen.insert(1, gen)

def replace_boardgen(gen, force=0):
    if curboard.playingboard or force:
        curboard.playingboard = 0
        BoardGen[0] = gen

def force_singlegen():
    del BoardGen[1:]
    return 0

def has_singlegen():
    return len(BoardGen) <= 1

def display_hat(p, d):
    if p.team == -1 or getattr(d,'isdying',0) or hasattr(d,'no_hat'):
        return
    try:
        bottom_up = d.bottom_up()
    except AttributeError:
        bottom_up = 0
    try:
        image = ('hat', p.team, d.dir, d.hatangle)
    except AttributeError:
        image = ('hat', p.team)
    if bottom_up:
        image = 'vflip', image
        y = d.y
    else:
        y = d.y - 16
    ico = images.sprget(image)
    if (getattr(d,'hatsprite',None) is None or
        not d.hatsprite.alive):
        d.hatsprite = images.ActiveSprite(ico, d.x, y)
    else:
        d.hatsprite.to_front()
        d.hatsprite.move(d.x, y, ico)
    d.hatsprite.gen = [d.hatsprite.die([None])]

def normal_frame():
    from player import BubPlayer
    BubPlayer.FrameCounter += 1

    # main generator dispatch loop
    images.action(images.ActiveSprites[:])
    
    frametime = 10
    for p in BubPlayer.PlayerList:
        if p.isplaying():
            frametime = BubPlayer.BaseFrametime
            p.zarkon()
            for d in p.dragons:
                d.to_front()
                display_hat(p, d)
                d.prefix(p.pn)
    if not (BubPlayer.FrameCounter & 31):
        gamesrv.compactsprites()
        reset = getattr(BubPlayer, 'MultiplyerReset', 0)
        if reset and BubPlayer.FrameCounter >= reset:
            BubPlayer.MultiplyerReset = 0
            set_frametime(1.0)
    return frametime

def normal_play():
    from player import BubPlayer
    import bonuses
    framecounter = 0
    bonus_callback = bonuses.start_normal_play()
    while BubPlayer.MonsterList:
        bonus_callback()
        yield normal_frame()
        if not BubPlayer.DragonList:
            continue
        framecounter += 1
        BASE = 500
        if not (framecounter % BASE):
            if framecounter == 4*BASE:
                from monsters import Monster
                from mnstrmap import BigImages
                ico = images.sprget(BigImages.hurryup[1])
                s = images.ActiveSprite(ico, (bwidth-ico.w)//2, (bheight-ico.h)//2)
                s.setimages(s.die(BigImages.hurryup * 12, 2))
                images.Snd.Hurry.play()
                mlist = [s for s in images.ActiveSprites
                         if (isinstance(s, Monster) and s.regular() and
                             not s.angry)]
                if mlist:
                    s = random.choice(mlist)
                    s.angry = [s.genangry()]
                    s.resetimages()
            if framecounter >= 6*BASE:
                mlist = [s for s in images.ActiveSprites
                         if isinstance(s, Monster) and s.regular() and s.angry]
                if mlist:
                    images.Snd.Hell.play()
                    gamesrv.set_musics([], [])
                    s = random.choice(mlist)
                    s.become_ghost()
                    framecounter = -200
                else:
                    framecounter = 2*BASE
            if framecounter == 0:
                curboard.set_musics()
    replace_boardgen(last_monster_killed(), 1)

##def normal_play():
##    # TESTING!!
##    from player import BubPlayer
##    for p in BubPlayer.PlayerList:
##        if not p.icons:
##            p.loadicons(p.icons, images.sprget)
##    results = {BubPlayer.PlayerList[0]: 100,
##               BubPlayer.PlayerList[1]: 200,
##               BubPlayer.PlayerList[2]: 300,
##               BubPlayer.PlayerList[3]: 400,
##               BubPlayer.PlayerList[4]: 100,
##               BubPlayer.PlayerList[5]: 200,
##               BubPlayer.PlayerList[6]: 300,
##               BubPlayer.PlayerList[7]: 400,
##               BubPlayer.PlayerList[8]:1000,
##               BubPlayer.PlayerList[9]:1000,
##               }
##    maximum = None
##    for t in result_ranking(results, maximum):
##        yield t

def last_monster_killed(end_delay=390, music=None):
    from player import BubPlayer
    for t in exit_board(music=music):
        yield t
    if curboard.bonuslevel:
        curboard.playingboard = 1
        for t in bonus_play():
            yield t
            end_delay -= 1
            if end_delay <= 0:
                replace_boardgen(next_board(), 1)
                break
    else:
        for i in range(end_delay):
            yield normal_frame()
        replace_boardgen(next_board(), 1)

##def bonus_play():
##    from player import BubPlayer
##    import bubbles
##    while BubPlayer.LimitScoreColor is None:
##        yield normal_frame()
##        players = [(p.points, p.pn) for p in BubPlayer.PlayerList
##                   if p.isplaying()]
##        if players:
##            players.sort()
##            points, BubPlayer.LimitScoreColor = players[-1]
##            BubPlayer.LimitScore = ((points + limit) // 100000) * 100000
##    for p in BubPlayer.PlayerList:
##        if p.isplaying():
##            p.givepoints(0)  # check LimitScore and update scoreboard()
##    while not (BubPlayer.BubblesBecome or BubPlayer.MegaBonus):
##        if random.random() < 0.06:
##            bubbles.newbonusbubble()
##        yield normal_frame()
##    # special board end
##    import monsters
##    monsters.argh_em_all()
##    replace_boardgen(last_monster_killed(), 1)

class TimeCounter(Copyable):
    def __init__(self, limittime, blink=0):
        from player import BubPlayer
        self.saved_time = BubPlayer.LimitTime
        self.time = limittime / FRAME_TIME
        self.prev = None
        self.blink = blink
    def update(self, t):
        from player import BubPlayer, scoreboard
        self.time -= t
        if self.time < 0.0:
            self.time = 0.0
        BubPlayer.LimitTime = self.time * FRAME_TIME
        next = int(BubPlayer.LimitTime)
        if self.blink and BubPlayer.LimitTime - next >= 0.5:
            BubPlayer.LimitTime = next = None
        if self.prev != next:
            scoreboard(compresslimittime=1)
            self.prev = next
    def restore(self):
        from player import BubPlayer
        BubPlayer.LimitTime = self.saved_time

def bonus_play():
    from player import BubPlayer
    import bubbles
    BubPlayer.MegaBonus = None
    BubPlayer.BubblesBecome = None
    Time0 = 5.0 / FRAME_TIME  # when to slow down time
    tc = TimeCounter(BubPlayer.LimitTime or 180.9)   # 3:00
    prev = None
    while not (BubPlayer.BubblesBecome or BubPlayer.MegaBonus):
        if random.random() < 0.099:
            bubbles.newbonusbubble()
        t = normal_frame()
        tc.update(t)
        if tc.time < Time0:
            if tc.time <= 0.5:
                tc.time = 0.5
                BubPlayer.LimitTime = 0.0
            t *= math.sqrt(Time0 / tc.time)
        yield t
        if tc.time == 0.5:
            gamesrv.game.End = 'gameover'
            gamesrv.game.updateboard()
            replace_boardgen(game_over(), 1)
            return
    # special board end
    import monsters
    monsters.argh_em_all()
    replace_boardgen(last_monster_killed(), 1)

def game_over():
    yield force_singlegen()
    from player import scoreboard
    import ranking
    images.Snd.Extralife.play()
    gamesrv.set_musics([], [images.music_potion])
    scoreboard()
    for t in ranking.game_over():
        yield t

def game_reset():
    import time
    from player import BubPlayer
    t1 = time.time()
    while 1:
        yield 0
        if BubPlayer.LimitTime and BubPlayer.LimitTime >= 1.0:
            # someone else ticking the clock, try again later
            return
        if abs(time.time() - t1) > 2.0:
            break
    # anyone playing ?
    if not gamesrv.game.End:
        return  # yes -> cancel game_reset()
    # let's tick the clock !
    tc = TimeCounter(60.9, blink=1)   # 1:00
    t1 = time.time()
    while tc.time:
        yield 0
        # anyone playing now ?
        if not gamesrv.game.End:
            tc.restore()
            return  # yes -> cancel game_reset()
        t = time.time()  # use real time
        deltat = (t-t1)/FRAME_TIME
        if deltat < 1.0:
            deltat = 1.0
        elif deltat > 100.0:
            deltat = 100.0
        tc.update(deltat)
        t1 = t
    gamesrv.game.reset()

##def wasting_play():
##    from player import BubPlayer, scoreboard
##    import bubbles
##    curboard.wastingplay = {}
##    for p in BubPlayer.PlayerList:
##        if p.isplaying():
##            p.letters = {}
##            p.bonbons = p.points // 50000
##    scoreboard()
    
##    while len(BubPlayer.DragonList) > 1:
##        if random.random() < 0.03:
##            bubbles.newbubble(1)
##        yield normal_frame()
##    for d in BubPlayer.DragonList:
##        curboard.wastingplay[d.bubber] = len(curboard.wastingplay)
##    for i in range(50):
##        yield normal_frame()

##    total = len(curboard.wastingplay)
##    results = [(total-n, p) for p, n in curboard.wastingplay.items()]
##    results.sort()
##    results = [(p, str(n)) for n, p in results]
##    for t in display_ranking(results):
##        yield t
##    # never ending

def skiplevels(blink, skip):
    # (not used any more)
    saved = BoardGen[:]
    while skip:
        skip -= 1
        BoardGen[:] = saved
        for i in range(10):  # frozen pause
            yield 3
            if blink:
                blink.step(-bwidth, 0)
                yield 3.33
                blink.step(bwidth, 0)
        blink = None
        for t in next_board(complete=(skip==0)):
            yield t

def exit_board(delay=8, music=None, repeatmusic=[]):
    from bubbles import Bubble
    from bonuses import RandomBonus, end_normal_play
    from player import BubPlayer
    from monsters import Monster
    end_normal_play()
    curboard.playingboard = 0
    actives = images.ActiveSprites[:]
    for s in actives:
        if ((isinstance(s, Monster) and s.still_playing())
            or isinstance(s, RandomBonus)):
            s.kill()
    music = music or []
    if BubPlayer.MegaBonus:
        music[:1] = [images.music_modern]
    if music or repeatmusic:
        gamesrv.set_musics(music, repeatmusic)
    for i in range(delay):
        yield normal_frame()
    bubble_outcome = BubPlayer.BubblesBecome or Bubble.pop
    for s in actives:
        if isinstance(s, Bubble):
            bubble_outcome(s)
            yield normal_frame()
    if BubPlayer.MegaBonus:
        BubPlayer.MegaBonus()

def potion_fill(blist, big=0):
    from player import BubPlayer
    from bonuses import Bonus
    #timeleft = 1680.0
    for t in exit_board(0, music=[images.music_potion]):
        #timeleft -= t
        yield t
    notes = all_notes = []
    y = 1
    while y < 11 or (y < height-2 and (len(all_notes) < 10 or big)):
        for x in range(2, width-3, 2):
            if ' ' == bget(x,y) == bget(x+1,y) == bget(x,y+1) == bget(x+1,y+1):
                b = Bonus(x*CELL, y*CELL, falling=0, *blist[((x+y)//2)%len(blist)])
                b.timeout = (444,666)[big]
                all_notes.append(b)
        for i in range(2):
            t = normal_frame()
            #timeleft -= t
            yield t
        y += 2
    while notes: #and timeleft > 0.0:
        notes = [b for b in notes if b.alive]
        t = normal_frame()
        #timeleft -= t
        yield t
    for i in range(10):
        t = normal_frame()
        #timeleft -= t
        yield t
    results = {}
    for b in all_notes:
        for d in b.taken_by:
            bubber = d.bubber
            results[bubber] = results.get(bubber, 0) + 1
    for t in result_ranking(results, len(all_notes)):
        yield t
    #fadeouttime = 3.33
    #fullsoundframes = bonusframes - 10 - int(fadeouttime / FRAME_TIME)
    #for i in range(fullsoundframes):
    #    yield normal_frame()
    #gamesrv.fadeout(fadeouttime)
    #for i in range(fullsoundframes, 490):
    #    yield normal_frame()

def result_ranking(results, maximum=None, timeleft=200):
    import ranking
    results = ranking.ranking_picture(results, maximum, timeleft is not None)
    if curboard.bonuslevel and timeleft is not None:
        play_again = bonus_play()
    else:
        play_again = None
    for t in ranking.display(results, timeleft, play_again):
        yield t
    if gamesrv.game.End != 'gameover':
        gamesrv.set_musics([], [])
        replace_boardgen(next_board(), 1)

def extra_water_flood():
    from mnstrmap import Flood
    from monsters import Monster
    waves_icons = [images.sprget(n) for n in Flood.waves]
    fill_icon = images.sprget(Flood.fill)
    bspr = []
    if 'flood' in curboard.sprites:
        return    # only one flooding at a time
    curboard.sprites['flood'] = bspr
    waves_sprites = [gamesrv.Sprite(waves_icons[0], x, bheight-CELL)
                     for x in range(0, bwidth, CELL)]
    bspr += waves_sprites
    fill_by_line = []
    poplist = [None]
    while waves_sprites[0].y > 0:
        yield 0
        waves_icons.insert(0, waves_icons.pop())
        for s in waves_sprites:
            s.seticon(waves_icons[0])
        yield 0
        sprites = [gamesrv.Sprite(fill_icon, s.x, s.y) for s in waves_sprites]
        bspr += sprites
        fill_by_line.append(sprites)
        for s in waves_sprites:
            s.step(0, -16)
        for s in images.touching(0, waves_sprites[0].y, bwidth, bheight):
            if isinstance(s, Monster):
                s.argh(poplist)
    while 1:
        for i in range(2):
            yield 0
            waves_icons.insert(0, waves_icons.pop())
            for s in waves_sprites:
                s.seticon(waves_icons[0])
        if not fill_by_line:
            break
        for s in fill_by_line.pop():
            s.kill()
        for s in waves_sprites:
            s.step(0, 16)
    for s in waves_sprites:
        s.kill()
    del curboard.sprites['flood']

def extra_aquarium():
    from mnstrmap import Flood
    from player import BubPlayer
    for i in range(200):
        if 'flood' not in curboard.sprites:  # only one flooding at a time
            break
        yield 0
        if curboard.cleaning_gen_state:
            return
    else:
        return
    curboard.sprites['flood'] = []
    gl = curboard.sprites.setdefault('background', [])
    curboard.holes = True     # so that random PlainBubbles show up anyway
    walls = curboard.sprites['walls']
    seen = {}

    def newsprite(ico, x, y):
        s = gamesrv.Sprite(ico, x, y)
        s.to_back(walls[0])
        gl.append(s)
        return s

    def fishplayers(ymin):
        for d in BubPlayer.DragonList:
            if d not in seen and d.y >= ymin:
                seen[d] = True
                d.become_fish()
                d.bubber.emotic(d, 4)

    waves_icons = [images.sprget(n) for n in Flood.waves]
    fill_icon = images.sprget(Flood.fill)
    waves_sprites = [newsprite(waves_icons[0], x, bheight-CELL)
                     for x in range(2*CELL + HALFCELL, bwidth - 2*CELL, CELL)]
    while waves_sprites[0].y > -fill_icon.h:
        fishplayers(waves_sprites[0].y)
        yield 0
        waves_icons.append(waves_icons.pop(0))
        for s in waves_sprites:
            s.seticon(waves_icons[0])
        fishplayers(waves_sprites[0].y)
        yield 0
        for s in waves_sprites:
            newsprite(fill_icon, s.x, s.y)
        for s in waves_sprites:
            s.step(0, -16)
    for s in waves_sprites:
        s.kill()
    BubPlayer.SuperFish = True
    fishplayers(-sys.maxint)

def extra_walls_falling():
    walls_by_pos = curboard.walls_by_pos
    moves = 1
    while moves and not curboard.cleaning_gen_state:
        moves = 0
        for y in range(height-3, -1, -1):
            for x in range(2, width-2):
                if ((y,x) in walls_by_pos and
                    (y+1,x) not in walls_by_pos and
                    (y+2,x) not in walls_by_pos):
                    y0 = y
                    while (y0-1,x) in walls_by_pos:
                        y0 -= 1
                    w = curboard.killwall(x, y0, 0)
                    curboard.putwall(x, y+1, w)
                    moves = 1
        curboard.reorder_walls()
        for y in range(6):
            yield 0

def single_blocks_falling(xylist):
    walls_by_pos = curboard.walls_by_pos
    while xylist:
        newlist = []
        for x, y in xylist:
            if ((y,x) in walls_by_pos and (y+1,x) not in walls_by_pos and
                y < curboard.height-1):
                newlist.append((x, y+1))
        for x, y in newlist:
            w = curboard.killwall(x, y-1, 0)
            curboard.putwall(x, y, w)
        xylist = newlist
        curboard.reorder_walls()
        for i in range(7):
            yield 0

def extra_display_repulse(cx, cy, dlimit=5000, dfactor=1000):
    offsets = {}
    for s in gamesrv.sprites_by_n.values():
        x, y = s.getdisplaypos()
        if x is not None:
            dx = x - cx
            dy = y - cy
            d = dx*dx + dy*dy + 100
            if d <= dlimit:
                dx = (dx*dfactor)//d
                dy = (dy*dfactor)//d
                offsets[s] = dx, dy
                s.setdisplaypos(int(x+dx), int(y+dy))
    yield 0
    yield 0
    while offsets:
        prevoffsets = offsets
        offsets = {}
        for s, (dx, dy) in prevoffsets.items():
            if s.alive:
                if dx < 0:
                    dx += max(1, (-dx)//5)
                elif dx:
                    dx -= max(1, dx//5)
                if dy < 0:
                    dy += max(1, (-dy)//5)
                elif dy:
                    dy -= max(1, dy//5)
                if dx or dy:
                    offsets[s] = dx, dy
                s.setdisplaypos(int(s.x+dx), int(s.y+dy))
        yield 0

def extra_bkgnd_black(cx, cy):
    gl = curboard.sprites.get('background')
    dist = 0
    while gl:
        dist += 17
        dist2 = dist * dist
        gl2 = []
        for s in gl:
            if (s.x-cx)*(s.x-cx) + (s.y-cy)*(s.y-cy) < dist2:
                s.kill()
            else:
                gl2.append(s)
        gl[:] = gl2
        yield 0

def extra_light_off(timeout, icocache={}):
    for i in range(timeout):
        if curboard.cleaning_gen_state:
            break
        dragons = {}
        import player
        playerlist = player.BubPlayer.PlayerList
        for bubber in playerlist:
            for dragon in bubber.dragons:
                dragons[dragon] = True
        for s in gamesrv.sprites_by_n.values():
            try:
                ico = icocache[s.ico, s in dragons]
            except KeyError:
                ico = images.make_darker(s.ico, s in dragons)
                icocache[s.ico, s in dragons] = ico
            s.setdisplayicon(ico)
        yield 0
    for s in gamesrv.sprites_by_n.values():
        s.setdisplayicon(s.ico)

def extra_swap_up_down(N=27):
    # unregister all walls
    walls = curboard.walls_by_pos.items()
    walls.sort()
    if not walls:
        return
    curboard.walls_by_pos.clear()
    emptyline = '##' + ' '*(width-4) + '##'
    curboard.walls = [emptyline] * height
    l = curboard.sprites['walls']
    wallicon = l[0].ico
    wallpool = l[:]
    l[:] = [gamesrv.Sprite(wallicon, 0, -wallicon.h)]
    
    # force the top half of the walls on front
    #for (y,x), w in walls:
    #    if y*2 < height:
    
    # show the walls swapping up/down
    ycenter = ((height-1)*CELL) // 2
    for i in range(N):
        alpha = math.cos((math.pi*(i+1))/N)
        ymap = {}
        for y in range(height):
            ymap[y] = int(alpha*(y*CELL-ycenter)) + ycenter
        for (y,x), w in walls:
            if y in ymap:
                w.move(x*CELL, ymap[y])
        yield 0
        if i == (N+1)//2:
            # reorder the wall sprites in the middle of the swap
            walls = [((-y,x), w) for (y,x), w in walls]
            walls.sort()
            for i in range(len(walls)):
                (y,x), w = walls[i]
                walls[i] = (y,x), wallpool[i]
            walls = [((-y,x), w) for (y,x), w in walls]
            walls.sort()
            # reverse all dragons!
            from player import BubPlayer
            for dragon in BubPlayer.DragonList:
                dragon.dcap['gravity'] *= -1.0
    
    # freeze the walls in their new position
    i = 0
    for (y,x), w in walls:
        y = height-1 - y
        if 0 <= y < height and (y,x) not in curboard.walls_by_pos:
            w = wallpool[i]
            i += 1
            curboard.putwall(x, y, w)
    l[:0] = wallpool[:i]
    for w in wallpool[i:]:
        w.kill()
    curboard.reorder_walls()

def extra_catch_all_monsters(dragons=[], everything=False):
    from monsters import Monster
    from bubbles import BigBubbleCatcher
    from bonuses import Bonus, BonusMaker
    if not dragons:
        from player import BubPlayer
        dragons = BubPlayer.DragonList
    i = 0
    give_up = 33
    for s in images.ActiveSprites[:]:
        if curboard.cleaning_gen_state:
            break
        while not dragons:
            give_up -= 1
            if give_up == 0:
                return
            yield 0
        if not s.alive or not s.touchable:
            continue
        if isinstance(s, Bonus):
            ok = s.bubblable
        elif isinstance(s, BonusMaker):
            ok = everything
        else:
            ok = isinstance(s, Monster)
        if ok:
            dragon = dragons[i%len(dragons)]
            BigBubbleCatcher(dragon, s, 542 + 17*i)
            i += 1
            yield 0
            yield 0

def extra_make_random_level(cx=None, cy=None, repeat_delay=200):
    from bonuses import DustStar
    # generate any random level
    localdir = os.path.dirname(__file__)
    filename = os.path.join(localdir, 'levels', 'RandomLevels.py')
    d = {}
    execfile(filename, d)
    Level = d['GenerateSingleLevel'](curboard.width, curboard.height)
    lvl = Level(curboard.num)
    walllist = []
    if cx is None: cx = bwidth // 2
    if cy is None: cy = bheight // 2
    for y in range(curboard.height):
        dy = cy - (y*16+8)
        dy2 = dy*dy*0.75
        for x in range(2, curboard.width-2):
            dx = cx - (x*16+8)
            d2 = dx*dx + dy2
            walllist.append((d2, x, y))
    walllist.sort()

    # dynamically replace the current level's walls with the new level's
    dist = 0
    speedf = 15.0
    added = 0
    for d2, x, y in walllist:
        while d2 > dist*dist:
            if added:
                curboard.reorder_walls()
                added = 0
            yield 0
            dist += 4.1
            speedf *= 0.99
        if curboard.walls[y][x] == ' ':
            if lvl.walls[y][x] == ' ':
                continue
            else:
                curboard.putwall(x, y)
                added = 1
                big = 1
        else:
            if lvl.walls[y][x] == ' ':
                curboard.killwall(x, y)
                big = 1
            else:
                big = 0
        sx = x*16
        sy = y*16
        DustStar(sx - 8*big,
                 sy - 8*big,
                 speedf * (sx-cx) / dist,
                 speedf * (sy-cy) / dist,
                 big=big)
    # patch the winds too
    curboard.winds = lvl.winds
    curboard.reorder_walls()
    yield 0
    # wait a bit and restart
    if repeat_delay < 1000:
        for i in range(repeat_delay):
            yield 0
            if curboard.cleaning_gen_state:
                return
        extra_boardgen(extra_make_random_level(
            repeat_delay = repeat_delay * 3 // 2))

def extra_bubbles(timeout):
    from bubbles import newforcedbubble
    falloff = 0.25
    L = math.log(0.965)     # same speed falloff rate as in throwing_bubble()
    cx = (bwidth - CELL) // 2
    cy = (bheight - CELL) // 2
    for i in range(timeout):
        if curboard.cleaning_gen_state:
            return
        if random.random() < falloff:
            bubble = newforcedbubble()
            if bubble:
                tx = random.randrange(CELL, bwidth - 2*CELL) - cx
                ty = random.randrange(CELL, bheight - 2*CELL) - cy
                if ty == 0:
                    ty = 1
                dist = math.sqrt(tx * tx + ty * ty)
                acos = tx / dist
                asin = ty / dist
                hspeed = 4 - dist * L
                bubble.thrown_bubble(cx, cy, hspeed, (acos, asin))
        falloff *= 0.998
        yield 0


def initsubgame(music, displaypoints):
    from player import BubPlayer, scoreboard
    for t in exit_board(0, repeatmusic=[music]):
        yield t
    BubPlayer.DisplayPoints = displaypoints
    scoreboard()
    for t in curboard.clean_gen_state():
        yield t

def register(dict):
    global width, height, bwidth, bheight, bheightmod
    items = dict.items()
    items.sort()
    for name, board in items:
        try:
            if not issubclass(board, Board) or board is Board:
                continue
        except TypeError:
            continue
        BoardList.append(board)
    # check sizes
    assert BoardList, "board file does not define any board"
    B = BoardList[0]
    try:
        test = B(-1)
        width = test.width
        height = test.height
        for B in BoardList[1:]:
            test = B(-1)
            assert test.width == width, "some boards have a different width"
            assert test.height == height, "some boards have a different height"
    except Exception, e:
        print 'Caught "%s" in level "%s":' % (e, B.__name__)
        raise e
    bwidth = width*CELL
    bheight = height*CELL
    bheightmod = (height+2)*CELL

##def define_boards(filename):
##    global curboard, boards, width, height, bwidth, bheight, bheightmod
##    curboard = None
##    boards = []
##    def board((wallfile, wallrect), shape):
##        lines = shape.strip().split('\n')
##        bmp = gamesrv.getbitmap(wallfile)
##        wallicon = bmp.geticon(*wallrect)
##        boards.append(Board(lines, wallicon))
##    d = {'board': board}
##    execfile(filename, d)
##    assert boards, "board file does not define any board"
##    width = boards[0].width
##    height = boards[0].height
##    for b in boards[1:]:
##        assert b.width == width, "some boards have a different width"
##        assert b.height == height, "some boards have a different height"
##    bwidth = width*CELL
##    bheight = height*CELL
##    bheightmod = len(boards[0].lines)*CELL


#try:
#    import psyco
#except ImportError:
#    pass
#else:
#    psyco.bind(normal_frame)