set display mode 320,240,16

hide mouse
sync rate 0
sync on

`   0 = keyboard, 1 = joystick, 2 = both
`   0 = off, 1 = on
`   1 = one player, 2 = 2 players!

`   Global arrays so we can access from within functions mainly For 2 player expansion

Dim control(1)
Dim music(1)
Dim players(1)
Dim scrolltext$(20)

music(0)=0
control(0)=2
players(0)=1
scrolltext$(0)="Test text"
scrolltext$(1)="Scroll text"

`   music maestro? (can use mp3 Or Mid)

   If file exist("columns.mp3")
      music(0)=1
      load music "columns.mp3",1
   EndIf

   If file exist("columns.mid")
      music(0)=1
      load music "columns.mid",1
   EndIf

`   Create our scroll-Text fonts, prep it all, etc...

load image "font.bmp",500
paste image 500,0,0

i=165

For y=0 To 32 Step 16

   For x=0 To 295 Step 15
      get image i,x,y,x+16,y+16
      inc i
   Next x

Next y

Dim scrolltext$(20)
Dim scrollnext$(20)
Dim scrolltemp$(20)
Dim scroller_offset#(1)
Dim scroller_count(1)
Dim scroller_speed#(1)

scroller_offset#(0)=0
scroller_count(0)=0
scroller_speed#(0)=2

getscrolltext(0)

Cls 0

load bitmap "screenplay_2.bmp",7

`   title1, title2, 1player, jewels, 2player, highscore, gameover

get image 1,640,0,960,240
get image 2,0,240,320,480
get image 3,0,0,320,220
get image 4,637,240,959,256
get image 5,320,0,640,220
get image 6,320,240,640,460
get image 7,640,256,728,270

`   Swap screen

sw=screen width() : sh=screen height()
create bitmap 1,sw,sh
create bitmap 2,sw,sh

Delete bitmap 7

`   Cut out the jewels
`   Image 10 = blank, 11,12,13,14,15,16 = jewels
`   17 = explode, 18,19,20,21 = explosion

paste image 4,0,0

i=10

For x=0 To 192 Step 16
   get image i,x,0,x+16,16
   inc i
Next a

Cls 0

`   PREP the intro sequence

t=60

Dim velocity#(t)
Dim acceleration#(t)
Dim jump(t)
Dim x#(t)
Dim speed#(t)
Dim y#(t)
Dim d(t)
Dim jewel(t)

makefire(t,320,240,120)

For a=0 To t
   sprite 1000+a,-16,-16,10
   set sprite 1000+a,0,1
Next a

`   Distort Title Picture on & start intro sequence

If music(0)=1 Then loop music 1

startgame:

i=1
sinewaveon(1)
show all sprites
intro(i,t)

set current bitmap 2
Cls 0
set current bitmap 1
Cls 0
set current bitmap 0
Cls 0
set current bitmap 2
hide all sprites
sync

`   game grid is 6x13 with 16x16 sprites

If players(0)=1

   Dim grid1(5,12)
   Dim colour1(5,12)
   Dim explode1(5,12)
   Dim jewels1(0)
   Dim score1(0)
   Dim t1(12)

   x1=144 : y1#=8 : s1#=0.50 : gx1=(x1-112)/16 : gy1=(y1#+24)/16
   jewels1(0)=3 : friendly1=0 : difficulty1=5

`   get 3 random jewels To make our droplet

   i11=Rnd(difficulty1)+11
   i21=Rnd(difficulty1)+11
   i31=Rnd(difficulty1)+11

`   get the Next 3 (preview pane) too

   n11=Rnd(difficulty1)+11
   n21=Rnd(difficulty1)+11
   n31=Rnd(difficulty1)+11

   sprite 1,x1,y1#,i11
   sprite 2,x1,y1#+16,i21
   sprite 3,x1,y1#+32,i31

   show sprite 1
   show sprite 2
   show sprite 3

EndIf

If players(0)=2

   Dim grid2(5,12)
   Dim colour2(5,12)
   Dim explode2(5,12)
   Dim jewels2(0)
   Dim score2(0)
   Dim t2(12)

   x1=32 : y1#=8 : s1#=0.50 : gx1=(x1-16)/16 : gy1=(y1#+24)/16
   jewels1(0)=3 : friendly1=0 : difficulty1=2

   x2=144 : y2#=8 : s2#=0.50 : gx2=(x2-112)/16 : gy2=(y#+24)/16
   jewels2(0)=3 : friendly2=0 : difficulty2=2

`   get 3 random jewels To make our droplet

   i11=Rnd(difficulty1)+11
   i21=Rnd(difficulty1)+11
   i31=Rnd(difficulty1)+11

   i12=Rnd(difficulty2)+11
   i22=Rnd(difficulty2)+11
   i32=Rnd(difficulty2)+11

`   get the Next 3 (preview pane) too

   n11=Rnd(difficulty1)+11
   n21=Rnd(difficulty1)+11
   n31=Rnd(difficulty1)+11

   n12=Rnd(difficulty2)+11
   n22=Rnd(difficulty2)+11
   n32=Rnd(difficulty2)+11

   sprite 1,x1,y1#,i11
   sprite 2,x1,y1#+16,i21
   sprite 3,x1,y1#+32,i31

   sprite 4,x2,y2#,i12
   sprite 5,x2,y2#+16,i22
   sprite 6,x2,y2#+32,i32

   show sprite 1
   show sprite 2
   show sprite 3
   show sprite 4
   show sprite 5
   show sprite 6

EndIf

keytimer=0 : level=1

For a=0 To 5
   For b=0 To 12
      colour1(a,b)=10
      If players(0)=2 Then colour2(a,b)=10
   Next b
Next a

`   set the scene

ink rgb(175,175,0),0
set Text font "Arial"
set Text size 12
set Text opaque
slideallow1=0

If players(0)=1

   paste image 3,0,0

   For a=0 To 5
      For b=0 To 12
         paste image 10,112+(a*16),8+(b*16)
      Next b
   Next a

   paste image n11,88,8
   paste image n21,88,24
   paste image n31,88,40

Else

   paste image 5,0,0

   For a=0 To 5
      For b=0 To 12
         paste image 10,16+(a*16),8+(b*16)
      Next b
   Next a

   paste image n11,88,8
   paste image n21,88,24
   paste image n31,88,40

EndIf

copy bitmap 2,0
sync

`   --------------------------------------------------------------------------
`   da main event...
`   --------------------------------------------------------------------------

do

`   debug info
`   Text 0,222,"X: "+Str$(gx)+" Y: "+Str$(gy)+" Friendly: "+Str$(friendly)+"  "

`   temporary score display, etc.

   If players(0)=1

      Text 44,132,Str$(score1(0))
      Text 89,173,Str$(level)
      Text 89,205,Str$(jewels1(0))

   EndIf

   sprite 1,x1,y1#,i11
   sprite 2,x1,y1#+16,i21
   sprite 3,x1,y1#+32,i31

   ox1=x1 : oy1=y1#

   If players(0)=2

      sprite 4,x2,y2#,i12
      sprite 5,x2,y2#+16,i22
      sprite 6,x2,y2#+32,i32

      ox2=x2 : oy2=y2#

   EndIf

`  check For control direction

   If upkey() Or joystick up() Then direction=1
   If rightkey() Or joystick Right() Then direction=2
   If downkey() Or joystick down() Then direction=3
   If leftkey() Or joystick Left() Then direction=4

   inc keytimer

   If keytimer>=7

      If direction=2 And gx1<5 And slideallow1=0

         If grid1(gx1+1,gy1+1)=0
            inc x1,16
         EndIf

         keytimer=0

      EndIf

      If direction=4 And gx1>0 And slideallow1=0

         If grid1(gx1-1,gy1+1)=0
            dec x1,16
         EndIf

         keytimer=0

      EndIf

      If direction=3 And gy1<13 And slideallow1=0
         speed1=1
      Else
         speed1=0
      EndIf

`      rotate the pieces
      If direction=1

         itemp1=i11
         i11=i21
         i21=i31
         i31=itemp1
         keytimer=0

      EndIf

   EndIf

`   down we go
   If slideallow1=0
      If speed1=0
         inc y1#,s1#
      Else
         inc y1#,s1#*4
         speed1=0
      EndIf
   EndIf

   If players(0)=1
      gx1=(x1-112)/16 : gy1=(y1#+24)/16
   Else
      gx1=(x1-16)/16 : gy1=(y1#+24)/16
   EndIf

`   we're at the top so check To see If it's full below us
   If gy1=2
      If grid1(gx1,gy1+1)=1

         Goto gameover

      EndIf
   EndIf

`   no? good, lets test For another colour/base of the game grid

   If gy1<12
      If grid1(gx1,gy1+1)=1 Then hit1=1
   EndIf

   If gy1=12 Then hit1=1

   If hit1=1

      paste image i11,x1,(gy1*16)-24
      paste image i21,x1,(gy1*16)-8
      paste image i31,x1,(gy1*16)+8

      grid1(gx1,gy1-2)=1
      grid1(gx1,gy1-1)=1
      grid1(gx1,gy1)=1

      colour1(gx1,gy1-2)=i11
      colour1(gx1,gy1-1)=i21
      colour1(gx1,gy1)=i31

`   here is where we start checking the explode loop

      For a=1 To 3
         hide sprite a
      Next a

      Repeat

         e=explode(gx1,gy1)

         If e=1
            d=dropem()
         Else
            d=0
         EndIf

      Until d=0

      For a=1 To 3
         show sprite a
      Next a

`   get the Next set And reset our stuff

      If players(0)=2
         x1=16
         y1#=8
         x2=144
         y2#=8
      Else
         x1=144
         y1#=8
      EndIf

      i11=n11
      i21=n21
      i31=n31

      inc friendly1

      If friendly1=3
         n11=Rnd(difficulty1)+11
         n21=Rnd(difficulty1)+11
         n31=n21
         friendly1=0
      Else
         n11=Rnd(difficulty1)+11
         n21=Rnd(difficulty1)+11
         n31=Rnd(difficulty1)+11
      EndIf

      paste image n11,88,8
      paste image n21,88,24
      paste image n31,88,40

      If players(0)=1
         gx1=(x1-112)/16
         gy1=(y1#+24)/16
      Else
         gx1=(x1-16)/16
         gy1=(y1#+24)/16
         gx2=(x2-112)/16
         gy2=(y2#+24)/16
      EndIf

      j1=jewels1(0)
      inc j1,3
      jewels1(0)=j1

      hit1=0

      sco1=score1(0)
      inc sco1,25
      score1(0)=sco1

`   Gosub progress

   EndIf

`   textgrid()

   direction=0

   scroller_go()

   copy bitmap 2,0

   sync

loop



`   Scroll Text Routine

Function scroller_go()
remstart
   scroller_x#=0

   For scroll=0 To 1

      im=Asc(scrolltext$(scroll))
      inc im,100

      If im=132 Then im=191
      If im=146 Then im=192
      If im=144 Then im=193
      If im=158 Then im=194
      If im=145 Then im=195
      If im=143 Then im=196
      If im=133 Then im=197
      If im=148 Then im=205
      If im=149 Then im=206
      If im=150 Then im=207
      If im=151 Then im=208
      If im=152 Then im=209
      If im=153 Then im=210
      If im=154 Then im=211
      If im=155 Then im=212
      If im=156 Then im=213
      If im=157 Then im=214

      paste image im,scroller_offset#(0)+scroller_x#,224
      inc scroller_x#,16

   Next scroll

   sotemp#=scroller_offset#(0)
   dec sotemp#,scroller_speed#(0)
   scroller_offset#(0)=sotemp#

`   dec scroller_offset#,scroller_speed#(0)

   If sotemp#=<-16

      scroller_offset#(0)=0
      getnextcharacter(scroller_count(0))

      sctemp=scroller_count(0)
      inc sctemp
      scroller_count(0)=sctemp

`      inc scroller_count

      If sctemp=21
         getscrolltext(1)
         scroller_count(0)=0
      EndIf

   EndIf
remend
endfunction



Function explode(x,y)

`   scan array vertically looking For matching jewels And mark them all

   e=0

   For a=0 To 5
      For b=12 To 2 Step -1

         tj1=colour1(a,b)
         tj2=colour1(a,b-1)
         tj3=colour1(a,b-2)

`         we got a match sir!
         If tj1>10
            If tj1=tj2 And tj2=tj3

               grid1(a,b)=99
               grid1(a,b-1)=99
               grid1(a,b-2)=99
               e=1

               s=score1(0)
               inc s,50
               score1(0)=s

            EndIf
         EndIf

      Next b
   Next a

`   scan horizontally too

   For a=0 To 3
      For b=12 To 1 Step -1

         tj1=colour1(a,b)
         tj2=colour1(a+1,b)
         tj3=colour1(a+2,b)

         If tj1>10
            If tj1=tj2 And tj2=tj3

               grid1(a,b)=99
               grid1(a+1,b)=99
               grid1(a+2,b)=99
               e=1

               s=score1(0)
               inc s,50
               score1(0)=s

            EndIf
         EndIf

      Next b
   Next a

`   scan diagnols, bottom Left up To top Right

   For a=0 To 3
      For b=12 To 3 Step -1

         tj1=colour1(a,b)
         tj2=colour1(a+1,b-1)
         tj3=colour1(a+2,b-2)

         If tj1>10
            If tj1=tj2 And tj2=tj3

               grid1(a,b)=99
               grid1(a+1,b-1)=99
               grid1(a+2,b-2)=99
               e=1

               s=score1(0)
               inc s,100
               score1(0)=s

            EndIf
         EndIf

      Next b
   Next a

`   scan diagnols, bottom Right up To top Left

   For a=5 To 2 Step -1
      For b=12 To 3 Step -1

         tj1=colour1(a,b)
         tj2=colour1(a-1,b-1)
         tj3=colour1(a-2,b-2)

         If tj1>10
            If tj1=tj2 And tj2=tj3

               grid1(a,b)=99
               grid1(a-1,b-1)=99
               grid1(a-2,b-2)=99
               e=1

               s=score1(0)
               inc s,100
               score1(0)=s

            EndIf
         EndIf

      Next b
   Next a

endfunction e




Function dropem()

   d=0

`   2 stage process, First remove all 99 (exploded) jewels

   For a=0 To 5
      For b=0 To 12
         If grid1(a,b)=99

            colour1(a,b)=10
            grid1(a,b)=0

            If players(0)=1
               jx=(a*16)+112
               jy=(b*16)+8
            Else
               jx=(a*16)+16
               jy=(b*16)+8
               jx2=(a*16)+112
               jy2=(b*16)+8
            EndIf

`   shatter jewels loop

            ei=18

            Repeat

               paste image ei,jx,jy
               scroller_go()
               copy bitmap 2,0
               sync
               inc ei

            Until ei=22

            paste image 10,jx,jy

`            j=jewels1(0)
`            dec j
`            jewels1(0)=j

            d=1

         EndIf
      Next b
   Next a

`   now move remainder down
`   check To see If this row is blank

   For a=0 To 5

      rowcount=0

      For b=0 To 12
         rowcount=rowcount+colour1(a,b)
      Next b

      If rowcount>130

         count=0
         For b=0 To 12

            tc=colour1(a,b)
            If tc>10

               If players(0)=1
                  jx=(a*16)+112
                  jy=(b*16)+8
               Else
                  jx=(a*16)+16
                  jy=(b*16)+8
                  jx2=(a*16)+112
                  jy2=(b*16)+8
               EndIf

               t1(count)=tc
               paste image 10,jx,jy
               colour1(a,b)=10
               grid1(a,b)=0
               inc count

            EndIf

         Next b

         dec count
         rkd=count
         For b=12 To 0 Step -1

            If players(0)=1
               jx=(a*16)+112
               jy=(b*16)+8
            Else
               jx=(a*16)+16
               jy=(b*16)+8
               jx2=(a*16)+112
               jy2=(b*16)+8
            EndIf

            dal=12-b
            If t1(dal)>0
               paste image t1(rkd),jx,jy
               colour1(a,b)=t1(rkd)
               grid1(a,b)=1
               dec rkd
            EndIf

         Next b

         For b=0 To 12
            t1(b)=0
         Next b

         scroller_go()
         copy bitmap 2,0
         sync

      EndIf

   Next a

endfunction d




Function textgrid()

   x=340 : y=0

   For a=0 To 12
      For b=0 To 5
         Text x,y,Str$(colour1(b,a))+"  "
         Text x+140,y,Str$(grid1(b,a))
         inc x,20
      Next b
      inc y,20
      x=340
   Next a

endfunction




Function sinewaveon(im)

   set current bitmap 2
   paste image im,0,0

   For i=1 To 240
      get image 499+i,0,i-1,320,i
   Next i

   Cls 0

   Step#=8.0 : sx=0 : speed=6 : o#=25
   t=timer()

   Repeat

      Cls 0

      si=500

      For i=1 To 240
         paste image si,Sin(sx+i*Step#)*o#,i-1
         inc si
      Next i

      inc sx,speed

      copy bitmap 2,0

      If timer()=>t+250
         dec Step#,0.05
         dec o#,0.25
         t=timer()
      EndIf

      skip=0
      If scancode()<>0 Then skip=1
      If joystick fire a()=1 Then skip=1

      sync

   Until Step#<=3 Or skip=1

   paste image im,0,0
   copy bitmap 2,0
   sync

   Repeat
   Until scancode()=0

endfunction

`   Scroll Text routine To kick things off

Function getscrolltext(c)

   If c=0

      Read Line$

      If Line$="*"
         Restore
         Read Line$
      EndIf

      Read line2$

      If line2$="*"
         Restore
         Read line2$
      EndIf

      For a=0 To 20
         scrolltext$(a)=Mid$(Line$,1+a)
         scrollnext$(a)=Mid$(line2$,1+a)
      Next a

   Else

      Read Line$

      If Line$="*"
         Restore
         Read Line$
      EndIf

      For a=0 To 20
         scrollnext$(a)=Mid$(Line$,1+a)
      Next a

   EndIf

endfunction



`   Scroll Text routine To get the Next character And shuffle stuff

Function getnextcharacter(c)

   For a=0 To 20
      scrolltemp$(a)=scrolltext$(a)
   Next a

   For a=0 To 19
      scrolltext$(a)=scrolltemp$(1+a)
   Next a

   scrolltext$(20)=scrollnext$(c)

endfunction


`   Intro Sequence

Function intro(b,t)

   start=timer()
   set current bitmap 0

   w=320
   h=240
   xh=120

   For a=1 To 6
      If sprite exist(a) Then hide sprite a
   Next a
Redo = Timer()
   do
      if Timer() > redo+100
      paste image b,0,0

      For a=0 To t

         If jump(a)=0
            velocity#(a)=9.0+Rnd(10)
            jump(a)=1
         EndIf

         If jump(a)=1
            If velocity#(a) > 10
               velocity#(a)=velocity#(a)-acceleration#(a)
            EndIf
            y#(a)=y#(a)-velocity#(a)
         EndIf

         temp_x#=x#(a)
         temp_s#=speed#(a)

         If y#(a)>h
            y#(a)=h
         Else
            If d(a)=1
               inc temp_x#,temp_s#
            Else
               dec temp_x#,temp_s#
            EndIf
            x#(a)=temp_x#
         EndIf

         sprite 1001+a,x#(a),y#(a),jewel(a)

      Next a
      redo = timer()
      EndIf

      If timer()=>start+2500+Rnd(1000)
         makefire(t,320,240,120)
         start=timer()
      EndIf

      If joystick fire a()=1 Or scancode()>0 Then exitfunction

      sync

   loop

endfunction

Function makefire(t,w,h,xh)

   For a=0 To t

      velocity#(a)=0
      acceleration#(a)=0.5
      jump(a)=0
      x#(a)=xh+10+Rnd(xh/2)
      speed#(a)=Rnd(4)
      y#(a)=150+Rnd(40)
      d(a)=Rnd(1)
      jewel(a)=11+Rnd(6)

   Next a

endfunction

`   Game Over Routine And HighScore Table Display

gameover:

   paste image 7,116,120
   hide all sprites
   copy bitmap 2,0
   sync

   Repeat

      scroller_go()
      copy bitmap 2,0
      sync

   Until joystick fire a()=1 Or scancode()>0

`   do the mass explode thing

   ei=18

   Repeat

      For a=12 To 0 Step -1

         For b=0 To 5

            jx=(b*16)+112
            jy=(a*16)+8
            paste image ei,jx,jy

         Next b

         scroller_go()
         copy bitmap 2,0
         sync

      Next a

      inc ei

   Until ei=22

   scroller_go()
   copy bitmap 2,0
   sync

   For a=0 To 5
      For b=0 To 12
         paste image 10,112+(a*16),8+(b*16)
      Next b
   Next a

   scroller_go()
   copy bitmap 2,0
   sync

   Repeat

      scroller_go()
      copy bitmap 2,0
      sync

   Until joystick fire a()=1 Or scancode()>0

  Cls 0

  Goto startgame

`   **************************************************** PROGRESS!
`  This part isn't used yet, but could be

progress:

      If sco1>=2500 And difficulty1=2
         inc difficulty1
      EndIf

      If sco1=>5000 And difficulty1=3
         inc difficulty1
      EndIf

      If sco1=>10000 And difficulty1=4
         inc difficulty1
      EndIf

      If sco1=>10000 And s1#=0.5
         inc s1#,0.25
      EndIf

      If sco1=>50000 And s1#=0.75
         inc s1#,0.25
      EndIf

Return
