rem Breakit level designer
rem 
rem A game utility by Marc Gale (Xalthorn)

gosub initialise
gosub main
exit

rem ##########################################
label main
 repeat
  gosub editor
 until (1=2)
return

rem ##########################################
label editor
 repeat
  gosub setupscreen
  cbx=1:cby=1
  show=0
  repeat
   gosub flipscreen
   gosub drawframe
   gosub drawcurbrick
   gosub processinput
  until (show=1)
  gosub showdata
 until (1=2)
return

rem ##########################################
label brickrun
   if map(x,y)<>bricktype then
    if brickcount>0 then
     t$=str$(bricktype)+","+str$(brickcount)+","
     text textx,texty,t$
     textx=textx+len(t$)*10
     if textx>560 then
      textx=40:texty=texty+20
     fi
    fi

    bricktype=map(x,y)
    brickcount=1
   else
    brickcount=brickcount+1
   fi
return

rem ##########################################
label showdata
 setdispbuf cb
 setdrawbuf cb
 clear window

 bricktype=-1
 brickchangeh=0
 for y=1 to 35
  for x=1 to 22
   if map(x,y)<>bricktype then
    bricktype=map(x,y)
    brickchangeh=brickchangeh+1
   fi
  next x
 next y
 
 bricktype=-1
 brickchangev=0
 for x=1 to 22
  for y=1 to 35
   if map(x,y)<>bricktype then
    bricktype=map(x,y)
    brickchangev=brickchangev+1
   fi
  next y
 next x

 x=20:y=20
 r1=0:g1=0:b1=150
 r2=150:g2=150:b2=150
 if brickchangev>brickchangeh then
  t$="Horizontal coding : type 1"
  runtype=1
 else
  t$="Vertical coding : type 2"
  runtype=2
 fi
 gosub ttext

 setrgb 1,255,255,255
 textx=40:texty=40
 bricktype=0:brickcount=0

 if runtype=1 then
  for y=1 to 35
   for x=1 to 22
    gosub brickrun
   next x
  next y
 else
  for x=1 to 22
   for y=1 to 35
    gosub brickrun
   next y
  next x
 fi

 if bricktype>0 then
  t$=str$(bricktype)+","+str$(brickcount)
  text textx,texty,t$
  textx=textx+len(t$)*10
 fi
 text textx,texty,"-1,-1"

 repeat until(and(peek("port1"),8)=0)
 repeat until(and(peek("port1"),8)>0)
 clear window
return

rem ##########################################
label clearmap
 for by=1 to 35
  for bx=1 to 22
   map(bx,by)=0
   gosub drawblock
   cbx=bx:cby=by:gosub undrawframe
  next bx
 next by
 cbx=1:cby=1
return

rem ##########################################
label drawsides
  setrgb 1,0,0,100
  setrgb 2,0,0,100
  setrgb 3,100,100,255

' bars
  gtriangle 0,20 to 0,512 to 10,512
  gtriangle 20,20 to 20,512 to 10,512
  gtriangle 480,20 to 480,512 to 490,512
  gtriangle 500,20 to 500,512 to 490,512
  setrgb 2,100,100,255
  gtriangle 0,20 to 10,20 to 10,512
  gtriangle 20,20 to 10,20 to 10,512
  gtriangle 480,20 to 490,20 to 490,512
  gtriangle 500,20 to 490,20 to 490,512
return

rem ##########################################
label collide
 if x>0 and x<23 and y>0 and y<36 then
  if map(x,y)>0 then
   hit=1
   if map(x,y)<13 then
    if mod(map(x,y),2)=0 then
     map(x,y)=map(x,y)-1
     bx=x:by=y
     gosub drawblock
     setdrawbuf 1-cb
     gosub drawblock
     setdrawbuf cb
     score=score+10
    else
     if int(ran(10))=1 gosub bonus
     map(x,y)=0
     bricks=bricks-1
     bx=x:by=y
     gosub undrawblock
     score=score+10
    fi                 : rem end of mod
   else
    bx=x:by=y
    gosub drawblock    : rem redraw solid block
   fi                  : rem end of <13
  fi
 fi
return

rem ##########################################
label processinput
 if moved>0 moved=moved-1
 if moved>0 return

 joy=peek("port1")
 if and(joy,128)>0 and cbx>1 then
  gosub undrawframe
  cbx=cbx-1
  moved=movewait
 fi
  
 if and(joy,32)>0 and cbx<22 then
  gosub undrawframe
  cbx=cbx+1
  moved=movewait
 fi

 if and(joy,16)>0 and cby>1 then
  gosub undrawframe
  cby=cby-1
  moved=movewait
 fi
  
 if and(joy,64)>0 and cby<35 then
  gosub undrawframe
  cby=cby+1
  moved=movewait
 fi

 if and(joy,4096)>0 then
  gosub clearmap
 fi

 if and(joy,16384)>0 then
  bx=cbx:by=cby:map(bx,by)=tilet:gosub drawblock
 fi

 if and(joy,32768)>0 then
  bx=cbx:by=cby:map(bx,by)=0:gosub drawblock
 fi

 if and(joy,2048)>0 and tilet<13 then
  tilet=tilet+1
  moved=movewait
 fi

 if and(joy,1024)>0 and tilet>1 then
  tilet=tilet-1
  moved=movewait
 fi

 if and(joy,8)>0 show=1
return

rem ##########################################
label ttext
 setrgb 1,r1,g1,b1
 for yy=y-1 to y+1
  for xx=x-1 to x+1
   text xx,yy,t$
  next xx
 next yy
 setrgb 1,r2,g2,b2
 text x,y,t$
return

rem ##########################################
label pbox
 setrgb 1,r1,g1,b1
 for yy=y-2 to y+2
  for xx=x-2 to x+2
   line xx,yy to xx+w1,yy
   line to xx+w1,yy+h1
   line to xx+w2,yy+h1
   line to xx+w2,yy+h2
   line to xx,yy+h2
   line to xx,yy 
  next xx
 next yy

 setrgb 1,r2,g2,b2
 line x,y to x+w1,y
 line to x+w1,y+h1
 line to x+w2,y+h1
 line to x+w2,y+h2
 line to x,y+h2
 line to x,y 
return

rem ##########################################
label setupscreen
 for a=1 to 2
  gosub flipscreen
  clear window

' right hand information column
  setrgb 1,50,50,150
  setrgb 1,50,50,100
  fill rectangle 500,0 to 640,512

' Brick display area
  x=505:y=20:w1=65:w2=127:h1=15:h2=45
  r1=0:g1=0:b1=0
  r2=200:g2=200:b2=255
  gosub pbox
  t$="Brick"
  x=510:y=35
  r1=0:g1=0:b1=0
  r2=255:g2=255:b2=255
  gosub ttext

  r1=0:g1=0:b1=0
  r2=200:g2=200:b2=200
  t$="L1 - Previous"
  x=505:y=100
  gosub ttext
  t$="R1 - Next"
  x=505:y=120
  gosub ttext
  t$=" X - Place"
  x=505:y=140
  r2=150:g2=150:b2=255
  gosub ttext
  t$="Sq - Remove"
  x=505:y=160
  r2=255:g2=150:b2=255
  gosub ttext
  t$="Tr - Wipe all"
  x=505:y=180
  r2=150:g2=255:b2=150
  gosub ttext

  r2=255:g2=255:b2=255
  t$="Start toggles"
  x=505:y=220
  gosub ttext
  t$="between data"
  x=505:y=240
  gosub ttext
  t$="and design."
  x=505:y=260
  gosub ttext

  setrgb 1,0,0,100
  setrgb 2,0,0,100
  setrgb 3,100,100,255

' left corner box
  gtriangle 0,0 to 20,0 to 10,10
  gtriangle 0,0 to 0,20 to 10,10
  gtriangle 0,20 to 20,20 to 10,10
  gtriangle 20,0 to 20,20 to 10,10
' right corner box
  gtriangle 480,0 to 500,0 to 490,10
  gtriangle 480,0 to 480,20 to 490,10
  gtriangle 480,20 to 500,20 to 490,10
  gtriangle 500,0 to 500,20 to 490,10

' bars
  gtriangle 0,20 to 0,512 to 10,512
  gtriangle 20,20 to 20,512 to 10,512
  gtriangle 480,20 to 480,512 to 490,512
  gtriangle 500,20 to 500,512 to 490,512
  gtriangle 20,0 to 480,0 to 480,10
  gtriangle 20,20 to 480,20 to 480,10
  setrgb 2,100,100,255
  gtriangle 0,20 to 10,20 to 10,512
  gtriangle 20,20 to 10,20 to 10,512
  gtriangle 480,20 to 490,20 to 490,512
  gtriangle 500,20 to 490,20 to 490,512
  gtriangle 20,0 to 20,10 to 480,10
  gtriangle 20,20 to 20,10 to 480,10

  r=100:g=100:b=100
  for cby=1 to 35
   for cbx=1 to 22
    bx=cbx:by=cby:gosub drawblock
    gosub undrawframe
   next cbx
  next cby

 next a
return

rem ##########################################
label drawframe
 x=cbx*20+10:y=cby*10+20
 setrgb 1,ran(255),ran(255),ran(255)
 setdrawbuf cb
 rectangle x,y to x+20,y+10
 setdrawbuf 1-cb
 rectangle x,y to x+20,y+10
return

rem ##########################################
label undrawframe
 x=cbx*20+10:y=cby*10+20
 setrgb 1,50,50,50
 setdrawbuf cb
 rectangle x,y to x+20,y+10
 setdrawbuf 1-cb
 rectangle x,y to x+20,y+10
return

rem ##########################################
label drawblock
 x=bx*20+10:y=by*10+20
 brick=map(bx,by)
 r=brickcol(brick,1)
 g=brickcol(brick,2)
 b=brickcol(brick,3)

 for z=0 to 1
  setdrawbuf z
  setrgb 1,r,g,b
  fill rectangle x,y to x+18,y+8
  setrgb 1,r*1.5,g*1.5,b*1.5
  line x,y to x+18,y
  line to x+18,y+8
  setrgb 1,r*0.5,g*0.5,b*0.5
  line x,y to x,y+8
  line to x+18,y+8
  if brick>0 setrgb 1,255,255,255
  fill rectangle x+16,y+1 to x+17,y+2
 next z
return

rem ##########################################
label drawcurbrick
 y=50
 brick=tilet
 r=brickcol(brick,1)
 g=brickcol(brick,2)
 b=brickcol(brick,3)

 for a=0 to 1
  for x=520 to 600 step 20
   setdrawbuf a
   setrgb 1,r,g,b
   fill rectangle x,y to x+18,y+8
   setrgb 1,r*1.5,g*1.5,b*1.5
   line x,y to x+18,y
   line to x+18,y+8
   setrgb 1,r*0.5,g*0.5,b*0.5
   line x,y to x,y+8
   line to x+18,y+8
   if brick>0 setrgb 1,255,255,255
   fill rectangle x+16,y+1 to x+17,y+2
  next x
 next a
return

rem ##########################################
label flipscreen
 setdispbuf cb
 cb=1-cb
 setdrawbuf cb
return

rem ##########################################
label initialise
 open window 640,512
 dim map(22,35)

 numbricks=13
 dim brickcol(numbricks,3)
 restore brickcolours
 for a=1 to numbricks
  read brickcol(a,1),brickcol(a,2),brickcol(a,3)
 next a

 movewait=5
 tilet=1
return

label brickcolours
data 100,0,0,200,0,0,100,100,0,200,200,0,0,100,0,0,200,0
data 0,0,100,0,0,200,100,0,100,200,0,200,0,100,100
data 0,200,200,100,100,100




