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
|
' Hardwire
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import "types.bmx"
Import "particle.bmx"
Import "sounds.bmx"
Type Pit
Const WIDTH=10
Const HEIGHT=14
Function X:Int(p:Int)
Return 230+p*32
End Function
Function Y:Int(p:Int)
Return 96+p*32
End Function
End Type
Type TWire
Const CROSS:Int=0
Const LEFT_RIGHT:Int=1
Const TOP_BOTTOM:Int=2
Const TOP_LEFT:Int=3
Const TOP_RIGHT:Int=4
Const BOTTOM_LEFT:Int=5
Const BOTTOM_RIGHT:Int=6
Const DIR_NONE:Int=0
Const DIR_UP:Int=1
Const DIR_RIGHT:Int=2
Const DIR_DOWN:Int=3
Const DIR_LEFT:Int=4
Global rotright:Int[]
Global rotleft:Int[]
Global img:TImage[]
Global dir:Int[][]
Global dx:Int[]
Global dy:Int[]
Global dirname:String[]
Global typename:String[]
Field t:Int
Function Init()
rotright=[0,2,1,4,6,3,5]
rotleft=[0,2,1,5,3,6,4]
img=[GameGFX.cross,GameGFX.left_right,GameGFX.top_bottom,GameGFX.top_left,GameGFX.top_right,GameGFX.bottom_left,GameGFX.bottom_right]
' CROSS LEFT_RIGHT TOP_BOTTOM TOP_LEFT TOP_RIGHT BOTTOM_LEFT BOTTOM_RIGHT
dir=[[DIR_UP,DIR_RIGHT,DIR_DOWN,DIR_LEFT], [DIR_NONE,DIR_RIGHT,DIR_NONE,DIR_LEFT], [DIR_UP,DIR_NONE,DIR_DOWN,DIR_NONE], [DIR_NONE,DIR_UP,DIR_LEFT,DIR_NONE], [DIR_NONE,DIR_NONE,DIR_RIGHT,DIR_UP], [DIR_LEFT,DIR_DOWN,DIR_NONE,DIR_NONE], [DIR_RIGHT,DIR_NONE,DIR_NONE,DIR_DOWN]]
dx=[0,0,1,0,-1]
dy=[0,-1,0,1,0]
dirname=["NONE","UP","RIGHT","DOWN","LEFT"]
typename=["CROSS","LEFT_RIGHT","TOP_BOTTOM","TOP_LEFT","TOP_RIGHT","BOTTOM_LEFT","BOTTOM_RIGHT"]
End Function
Method New()
t=Rand(0,6)
End Method
Function Create:TWire(t:Int[])
Local o:TWire=New TWire
o.t=t[Rand(0,t.length-1)]
Return o
End Function
Method Image:TImage()
Return img[t]
End Method
Method RotateLeft()
t=rotleft[t]
End Method
Method RotateRight()
t=rotright[t]
End Method
Method Traverse:Int(d:Int)
If d=DIR_NONE
Return DIR_NONE
EndIf
Return dir[t][d-1]
End Method
Function DirX:Int(d:Int)
Return dx[d]
End Function
Function DirY:Int(d:Int)
Return dy[d]
End Function
End Type
Type TPiece Abstract
Field x:Int
Field y:Int
Field ox:Int
Field oy:Int
Field map:TWire[4,4]
Field rot:Int
Field offx:Int[4]
Field offy:Int[4]
Function Create:TPiece()
Local o:TPiece
Select Rand(0,5)
Case 0
o=TPiece(New TPiece_S_Left)
Case 1
o=TPiece(New TPiece_S_Right)
Case 2
o=TPiece(New TPiece_L_Left)
Case 3
o=TPiece(New TPiece_L_Right)
Case 4
o=TPiece(New TPiece_Square)
Case 5
o=TPiece(New TPiece_Bar)
End Select
o.BaseInit()
Return o
End Function
Method Init() Abstract
Method BaseInit()
Init()
rot=0
ox=offx[rot]
oy=offy[rot]
End Method
Method Draw()
For Local px:Int=0 Until 4
For Local py:Int=0 Until 4
If map[px,py]
Local gx:Int=Pit.X(x-ox+px)
Local gy:Int=Pit.Y(y-oy+py)
SetColor(255,255,255)
DrawImage(GameGFX.tile,gx,gy)
SetColor(128,128,128)
DrawImage(map[px,py].Image(),gx,gy)
EndIf
Next
Next
End Method
Method RotateLeft()
If rot=0
rot=3
Else
rot:-1
EndIf
ox=offx[rot]
oy=offy[rot]
Local m:TWire[4,4]
For Local px:Int=0 Until 4
For Local py:Int=0 Until 4
If map[px,py]
map[px,py].RotateLeft()
EndIf
m[py,3-px]=map[px,py]
Next
Next
map=m
End Method
Method RotateRight()
rot=(rot+1) Mod 4
ox=offx[rot]
oy=offy[rot]
Local m:TWire[4,4]
For Local px:Int=0 Until 4
For Local py:Int=0 Until 4
If map[px,py]
map[px,py].RotateRight()
EndIf
m[3-py,px]=map[px,py]
Next
Next
map=m
End Method
Method Collides:Int(gm:TGameMap)
For Local px:Int=0 Until 4
For Local py:Int=0 Until 4
If map[px,py]
Local gx:Int=x-ox+px
Local gy:Int=y-oy+py
If gx<0 Or gx>=Pit.WIDTH Or gy>=Pit.HEIGHT
Return True
EndIf
If gy>=0 And gm.map[gx,gy]
Return True
EndIf
EndIf
Next
Next
Return False
End Method
Method AddToMap(gm:TGameMap)
For Local px:Int=0 Until 4
For Local py:Int=0 Until 4
If map[px,py]
Local gx:Int=x-ox+px
Local gy:Int=y-oy+py
If gy<0
gm.overflow=True
Else
gm.map[gx,gy]=map[px,py]
Particles.AddDust(Pit.X(gx)+16,Pit.Y(gy)+32)
EndIf
EndIf
Next
Next
Sound.Click()
gm.CheckWires()
End Method
End Type
Type TPiece_S_Right Extends TPiece
Method Init()
map[1,0]=TWire.Create([TWire.CROSS,TWire.BOTTOM_RIGHT])
map[2,0]=TWire.Create([TWire.CROSS,TWire.TOP_LEFT,TWire.BOTTOM_LEFT,TWire.LEFT_RIGHT])
map[0,1]=TWire.Create([TWire.CROSS,TWire.TOP_RIGHT,TWire.BOTTOM_RIGHT,TWire.LEFT_RIGHT])
map[1,1]=TWire.Create([TWire.CROSS,TWire.TOP_LEFT])
offx=[1,2,2,0]
offy=[1,1,2,2]
End Method
End Type
Type TPiece_S_Left Extends TPiece
Method Init()
map[0,0]=TWire.Create([TWire.CROSS,TWire.TOP_RIGHT,TWire.BOTTOM_RIGHT,TWire.LEFT_RIGHT])
map[1,0]=TWire.Create([TWire.CROSS,TWire.BOTTOM_LEFT])
map[1,1]=TWire.Create([TWire.CROSS,TWire.TOP_RIGHT])
map[2,1]=TWire.Create([TWire.CROSS,TWire.TOP_LEFT,TWire.BOTTOM_LEFT,TWire.LEFT_RIGHT])
offx=[1,2,2,0]
offy=[1,1,2,2]
End Method
End Type
Type TPiece_L_Right Extends TPiece
Method Init()
map[0,0]=TWire.Create([TWire.CROSS,TWire.BOTTOM_RIGHT])
map[1,0]=TWire.Create([TWire.CROSS,TWire.TOP_LEFT,TWire.BOTTOM_LEFT,TWire.LEFT_RIGHT])
map[0,1]=TWire.Create([TWire.CROSS,TWire.TOP_BOTTOM])
map[0,2]=TWire.Create([TWire.CROSS,TWire.TOP_LEFT,TWire.TOP_RIGHT,TWire.TOP_BOTTOM])
offx=[0,2,3,1]
offy=[1,0,2,3]
End Method
End Type
Type TPiece_L_Left Extends TPiece
Method Init()
map[0,0]=TWire.Create([TWire.CROSS,TWire.TOP_RIGHT,TWire.BOTTOM_RIGHT,TWire.LEFT_RIGHT])
map[1,0]=TWire.Create([TWire.CROSS,TWire.BOTTOM_LEFT])
map[1,1]=TWire.Create([TWire.CROSS,TWire.TOP_BOTTOM])
map[1,2]=TWire.Create([TWire.CROSS,TWire.TOP_LEFT,TWire.TOP_RIGHT,TWire.TOP_BOTTOM])
offx=[1,2,2,1]
offy=[1,1,2,2]
End Method
End Type
Type TPiece_Square Extends TPiece
Method Init()
map[0,0]=TWire.Create([TWire.CROSS])
map[1,0]=TWire.Create([TWire.CROSS])
map[0,1]=TWire.Create([TWire.CROSS])
map[1,1]=TWire.Create([TWire.CROSS])
offx=[0,2,2,0]
offy=[1,1,3,3]
End Method
End Type
Type TPiece_Bar Extends TPiece
Method Init()
map[1,0]=TWire.Create([TWire.CROSS,TWire.BOTTOM_LEFT,TWire.BOTTOM_RIGHT,TWire.TOP_BOTTOM])
map[1,1]=TWire.Create([TWire.CROSS,TWire.TOP_BOTTOM])
map[1,2]=TWire.Create([TWire.CROSS,TWire.TOP_BOTTOM])
map[1,3]=TWire.Create([TWire.CROSS,TWire.TOP_LEFT,TWire.TOP_RIGHT,TWire.TOP_BOTTOM])
offx=[1,1,2,1]
offy=[1,1,1,2]
End Method
End Type
Type TFallingBlock
Field w:TWire
Field x:Int
Field y:Double
Field yi:Double
Function Create:TFallingBlock(x:Int, y:Double)
Local o:TFallingBlock=New TFallingBlock
o.w=New TWire
o.x=x
o.y=y
o.yi=0
Return o
End Function
Method Update()
y:+yi
yi=Max(4.0,yi+0.1)
End Method
Method Draw()
SetColor(255,255,255)
DrawImage(GameGFX.tile,Pit.X(x),y)
SetColor(128,128,128)
DrawImage(w.Image(),Pit.X(x),y)
End Method
End Type
Type TWireListEnt
Field w:TWire
Field x:Int
Field y:Int
Function Create:TWireListEnt(x:Int, y:Int, w:TWire)
Local o:TWireListEnt=New TWireListEnt
o.x=x
o.y=y
o.w=w
Return o
End Function
End Type
Type TWireList
Field list:TList
Field col:Int
Method New()
list=CreateList()
col=128
End Method
Method Add(x:Int, y:Int, w:TWire)
list.AddLast(TWireListEnt.Create(x,y,w))
End Method
Method Draw:Int()
col=Max(255,col+5)
SetColor(col,col,col)
For Local e:TWireListEnt=EachIn list
DrawImage(e.w.Image(),Pit.X(e.x),Pit.Y(e.y))
Next
If col<255
Return True
Else
For Local e:TWireListEnt=EachIn list
Particles.AddImage(e.w.Image(),Pit.X(e.x),Pit.Y(e.y))
Next
Return False
EndIf
End Method
End Type
Type TGameMap
Field map:TWire[Pit.WIDTH,Pit.HEIGHT]
Field trode_col:Int
Field trode_coli:Int
Field overflow:Int
Field top:Int[Pit.WIDTH]
Field drop:TList
Field cx:Int
Field cy:Int
Field path:TList
Method New()
trode_col=0
trode_coli=1
overflow=False
drop=CreateList()
path=CreateList()
CalcTop()
cx=0
cy=Pit.HEIGHT-2
End Method
Method CalcTop()
For Local x:Int=0 Until Pit.WIDTH
top[x]=-1
For Local y:Int=Pit.Height-1 To 0 Step -1
If Not map[x,y]
top[x]=y
Exit
EndIf
Next
Next
End Method
Method AddRow(y:Int=-32)
If BlockInteract() Then Return
For Local x:Int=0 Until Pit.WIDTH
drop.AddLast(TFallingBlock.Create(x,y))
Next
End Method
Method BlockInteract:Int()
Return overflow Or path.Count()
End Method
Method FindPath:Int(l:TWireList, x:Int, y:Int, dir:Int)
If y<0
Return False
EndIf
If x<0 Or x=Pit.WIDTH Or y=Pit.HEIGHT
Return True
EndIf
Local w:TWire=map[x,y]
If Not w
Return False
EndIf
dir=w.Traverse(dir)
If dir=TWire.DIR_NONE
Return False
EndIf
l.Add(x,y,w)
Return FindPath(l,x+TWire.DirX(dir),y+TWire.DirY(dir),dir)
End Method
Method FindLoop:Int(l:TWireList, ox:Int, oy:Int, x:Int, y:Int, dir:Int)
If y<0 Or x<0 Or x=Pit.WIDTH Or y=Pit.HEIGHT
Return False
EndIf
Local w:TWire=map[x,y]
If Not w
Return False
EndIf
dir=w.Traverse(dir)
If dir=TWire.DIR_NONE
Return False
EndIf
l.Add(x,y,w)
x:+TWire.DirX(dir)
y:+TWire.DirY(dir)
If x=ox And y=oy And dir=TWire.DIR_LEFT
Return True
EndIf
Return FindLoop(l,ox,oy,x,y,dir)
End Method
Method CheckWires()
For Local y:Int=0 Until Pit.HEIGHT
Local l:TWireList=New TWireList
If FindPath(l,0,y,TWire.DIR_RIGHT)
path.AddLast(l)
EndIf
l=New TWireList
If FindPath(l,Pit.WIDTH-1,y,TWire.DIR_LEFT)
path.AddLast(l)
EndIf
Next
For Local x:Int=0 Until Pit.WIDTH
Local l:TWireList=New TWireList
If FindPath(l,x,Pit.HEIGHT-1,TWire.DIR_UP)
path.AddLast(l)
EndIf
Next
For Local x:Int=0 Until Pit.WIDTH
For Local y:Int=0 Until Pit.HEIGHT
If map[x,y]
Local l:TWireList=New TWireList
Select map[x,y].t
Case TWire.CROSS, TWire.LEFT_RIGHT, TWire.TOP_LEFT, TWire.BOTTOM_LEFT
If FindLoop(l,x,y,x,y,TWire.DIR_LEFT)
path.AddLast(l)
EndIf
End Select
EndIf
Next
Next
End Method
Method CursorLeft()
If BlockInteract() Then Return
cx=Max(0,cx-1)
End Method
Method CursorRight()
If BlockInteract() Then Return
cx=Min(Pit.WIDTH-2,cx+1)
End Method
Method CursorUp()
If BlockInteract() Then Return
cy=Max(0,cy-1)
End Method
Method CursorDown()
If BlockInteract() Then Return
cy=Min(Pit.HEIGHT-2,cy+1)
End Method
Method Rotate()
If BlockInteract() Then Return
Local done:Int=False
For Local x:Int=0 To 1
For Local y:Int=0 To 1
If map[cx+x,cy+y]
map[cx+x,cy+y].RotateRight()
done=True
EndIf
Next
Next
If done
Sound.Click()
EndIf
CheckWires()
End Method
Method Draw:Int()
Local score:Int=0
SetColor(255,255,255)
DrawImage(GameGFX.pit_bottomleft,Pit.X(-1),Pit.Y(Pit.HEIGHT))
DrawImage(GameGFX.pit_bottomright,Pit.X(Pit.WIDTH),Pit.Y(Pit.HEIGHT))
DrawImage(GameGFX.pit_top,Pit.X(-1),Pit.Y(0))
DrawImage(GameGFX.pit_top,Pit.X(Pit.WIDTH),Pit.Y(0))
SetColor(trode_col,trode_col,0)
DrawImage(GameGFX.trode_left,Pit.X(-1),Pit.Y(0))
DrawImage(GameGFX.trode_right,Pit.X(Pit.WIDTH),Pit.Y(0))
For Local f:Int=0 Until Pit.WIDTH
SetColor(255,255,255)
DrawImage(GameGFX.pit_bottom,Pit.X(f),Pit.Y(Pit.HEIGHT))
SetColor(trode_col,trode_col,0)
DrawImage(GameGFX.trode_bottom,Pit.X(f),Pit.Y(Pit.HEIGHT))
Next
For Local f:Int=1 Until Pit.HEIGHT
SetColor(255,255,255)
DrawImage(GameGFX.pit_side,Pit.X(-1),Pit.Y(f))
DrawImage(GameGFX.pit_side,Pit.X(Pit.WIDTH),Pit.Y(f))
SetColor(trode_col,trode_col,0)
DrawImage(GameGFX.trode_left,Pit.X(-1),Pit.Y(f))
DrawImage(GameGFX.trode_right,Pit.X(Pit.WIDTH),Pit.Y(f))
Next
For Local x:Int=0 Until Pit.WIDTH
For Local y:Int=0 Until Pit.HEIGHT
If map[x,y]
SetColor(255,255,255)
DrawImage(GameGFX.tile,Pit.X(x),Pit.Y(y))
SetColor(128,128,128)
DrawImage(map[x,y].Image(),Pit.X(x),Pit.Y(y))
EndIf
Next
Next
If BlockInteract()
For Local b:TFallingBlock=EachIn drop
b.Draw()
Next
Local l:TEasyLink=TEasyLink.Create(path)
Local check:Int=False
While l.Value()
Local b:TWireList=TWireList(l.Value())
If b.Draw()
l.MoveNext()
Else
If Not overflow
score:+b.list.Count()
For Local e:TWireListEnt=EachIn b.list
For Local y:Int=e.y To 1 Step -1
map[e.x,y]=map[e.x,y-1]
Next
map[e.x,0]=Null
Particles.AddDust(Pit.X(e.x)+16,Pit.Y(e.y)+32)
check=True
Next
EndIf
l.Remove()
EndIf
Wend
If check
Sound.Path()
CalcTop()
CheckWires()
EndIf
Else
Local hit:Int=False
Local l:TEasyLink=TEasyLink.Create(drop)
While l.Value()
Local b:TFallingBlock=TFallingBlock(l.Value())
Local t:Int=Pit.Y(top[b.x])'-ImageHeight(GameGFX.tile)
b.Update()
If b.y>t
If top[b.x]=-1
overflow=True
b.y=t
l.MoveNext()
Else
hit=True
map[b.x,top[b.x]]=b.w
Particles.AddDust(Pit.X(b.x)+16,Pit.Y(top[b.x])+32)
l.Remove()
EndIf
Else
b.Draw()
l.MoveNext()
EndIf
Wend
If hit And Not overflow
Sound.Click()
CheckWires()
CalcTop()
EndIf
EndIf
trode_col:+trode_coli
If (trode_col=255 And trode_coli>0) Or (trode_col=200 And trode_coli<0)
trode_coli=-trode_coli
EndIf
Return score
End Method
End Type
|