rem Waves - A wave simulation
rem copyright Marc Gale (Xalthorn)

gosub initialise
gosub mainloop
exit

rem ##################################
label mainloop
 repeat
  setdispbuf curbuf 
  curbuf=1-curbuf
  setdrawbuf curbuf
  gosub drawfluid
  if ran(20)>18 then gosub drops fi
  gosub fluctuate
 until (1=2)
return

rem ##################################
label fluctuate
 for x=0 to 10
  for y=0 to 10
   land(x,y)=land(x,y)+landd(x,y)
   if land(x,y)>mid then
    landd(x,y)=landd(x,y)-0.4
   else
    if land(x,y)<mid then
     landd(x,y)=landd(x,y)+0.4
    fi
   fi

   if landd(x,y)>20 then landd(x,y)=20 fi
   if landd(x,y)<-20 then landd(x,y)=-20 fi
  next y
 next x
return

rem ##################################
label drops
  dropx=int(ran(10))
  dropy=int(ran(10))
  for x=dropx to dropx+1
   for y=dropy to dropy+1
    land(x,y)=land(x,y)-2
   next y
  next x
return

rem ##################################
label drawfluid
 setrgb 1,50,80,50
 setrgb 2,50,80,100
 setrgb 3,50,80,100
 gtriangle 0,512 to 0,0 to 640,0
 setrgb 3,50,80,50
 gtriangle 0,512 to 640,0 to 640,512

 for y=10 to 1 step -1
  for x=1 to 10
   x1=x*30+y*30-40
   y1=x*15-y*15+250
   if mod(x+y,2)=0 then
    setrgb 1,150,150,150
   else
    setrgb 1,50,50,100
   fi

   col1=land(x-1,y-1)+100
   col2=land(x-1,y)+100
   col3=land(x,y)+100
   col4=land(x,y-1)+100

   rem top
   setrgb 1,0,0,col1
   setrgb 2,0,0,col2
   setrgb 3,0,0,col3
   gtriangle x1,y1-15-land(x-1,y-1) to x1+30,y1-30-land(x-1,y) to x1+60,y1-15-land(x,y)

   setrgb 2,0,0,col3
   setrgb 3,0,0,col4
   gtriangle x1,y1-15-land(x-1,y-1) to x1+60,y1-15-land(x,y) to x1+30,y1+15-land(x,y-1)

   if y=1 then
    rem bottom left edge
    setrgb 1,0,0,0
    setrgb 2,0,0,col1
    setrgb 3,0,0,col4
    gtriangle x1,y1+30 to x1,y1-15-land(x-1,y-1) to x1+30,y1-land(x,y-1)
    setrgb 2,0,0,col4
    setrgb 3,0,0,0
    gtriangle x1,y1+30 to x1+30,y1-land(x,y-1) to x1+30,y1+45
   fi

   if x=10 then
   rem bottom right edge
   setrgb 1,0,0,0
   setrgb 2,0,0,col4
   setrgb 3,0,0,col3
   gtriangle x1+30,y1+45 to x1+30,y1-land(x,y-1) to x1+60,y1-15-land(x,y)
   setrgb 2,0,0,col3
   setrgb 3,0,0,0
   gtriangle x1+30,y1+45 to x1+60,y1-15-land(x,y) to x1+60,y1+30
   fi
  next x
 next y
return

rem ##################################
label initialise
 curbuf=0
 open window 640,512

 mid=10
 dim land(10,10)
 dim landd(10,10)
 for x=0 to 10
  for y=0 to 10
   land(x,y)=mid
   landd(x,y)=0
  next y
 next x
return
