REM *** CAVE-FLY *** (c) balroq 2002

REM *** INIT STUFF
q=120
dim cs(720),sn(720)
dim px(q),py(q),pz(q),tx(q),ty(q),tz(q),i(10)
s=400
he=450 : REM HEIGHTS
su=-150 : REM SURFACE

REM *** GENERATE VERTEX
for y=-s to s step s/5
 for x=-s to s step s/5
  px(a)=x
  py(a)=y*1.5
  pz(a)=su+he/2+ran(he)
  a=a+1
 next x
next y

REM *** CALCULATE SINE TABLE
for ang=0 to 720
 cs(ang)=cos(ang*pi/360)
 sn(ang)=sin(ang*pi/360)
next ang

REM *** INIT SCREEN
open window 640,512
label loop
ang=180+cos(mm/2)*75 : REM ROTATE CAMERA ANGLE
mm=mm+.1

REM TRANSFORM POINTS
for p=0 to q
 px(p)=px(p)+24 : REM 24 = FLY SPEED
 if px(p)>s then
  px(p)=px(p)-s*2-50
  pz(p)=su+he/2+ran(he)
 fi
 newy=cs(ang)*py(p)+sn(ang)*pz(p)
 newz=cs(ang)*pz(p)-sn(ang)*py(p)
 newx=cs(ang)*px(p)+sn(ang)*newz
 newz=cs(ang)*newz-sn(ang)*px(p)+s
 tx(p)=320+newx*300/newz
 ty(p)=256+newy*300/newz
 tz(p)=newz
next p

REM SORT FACES & DRAWING ORDER
t=0
for a=110 to 120
 t=min(t,px(a))
 if t=px(a) mx=a
next a
for b=0 to 10
 i(b)=mx+b
 if i(b)>120 i(b)=i(b)-11
next b

REM DRAW ALL
for b=0 to 9
 for c=0 to 9
  ss=i(b)-c*11
  se=i(b)-(c+1)*11
  es=i(b+1)-c*11
  ee=i(b+1)-(c+1)*11
  cc=255-(tz(ss)+tz(se)+tz(ee)+tz(es))/12
  dd=cos(b/8)*cc
  setrgb 1,cc,dd,cc
  fill triangle tx(ss),ty(ss) to tx(se),ty(se) to tx(ee),ty(ee)
  fill triangle tx(ss),ty(ss) to tx(es),ty(es) to tx(ee),ty(ee)
  setrgb 1,dd,cc,dd
  fill triangle 640-tx(ss),512-ty(ss) to 640-tx(se),512-ty(se) to 640-tx(ee),512-ty(ee)
  fill triangle 640-tx(ss),512-ty(ss) to 640-tx(es),512-ty(es) to 640-tx(ee),512-ty(ee)
 next c
next b

REM SWAP BUFFER
setdispbuf cur
cur=1-cur
setdrawbuf cur

setrgb 0,0,0,0
clear window
goto loop
