REM ***********************************************
REM Title: random terrain
REM Author: Phaelax
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
 
sync on
backdrop on
color backdrop 0
hide mouse
randomize timer()
 
tx = 50
tz = 50
tsx# = 1000.0/tx
tsz# = 1000.0/tz
 
 
dim heights#(tx,tz)
 
`load image "grass.jpg", 1
 
make matrix 1, 1000,1000,tx,tz
`prepare matrix texture 1,1,1,1
 
position camera 0,300,0
point camera 500,100,500
 
 
 
do
 
 
   gosub camera_stuff
 
 
   if spacekey() and flag=0
      flag = 1
      x1 = rnd(1000)
      y1 = rnd(1000)
      x2 = rnd(1000)
      y2 = rnd(1000)
      gosub _calc_matrix
   endif
 
   if returnkey() and flag2=0
      flag2 = 1
      gosub _smooth_matrix
   endif
 
   if shiftkey() and flag3=0
      flag3=1
      gosub _randomize_matrix
   endif
 
   if spacekey() = 0 then flag = 0
   if returnkey() = 0 then flag2 = 0
   if shiftkey() = 0 then flag3 = 0
 
 
   sync
loop
 
 
_randomize_matrix:
   for z = 0 to tz
      for x = 0 to tx
         h# = rnd(100)
         set matrix height 1,x,z,h#
         heights#(x,z) = h#
      next x
   next z
   update matrix 1
RETURN
 
_calc_matrix:
 
   h0# = 20
   h1# = 15
 
   for z = 0 to tz
      for x = 0 to tx
         px# = x*tsx#
         pz# = z*tsz#
         if point_line(px#,pz#,x1,y1,x2,y2) >= 0
            h# = get matrix height(1,x,z)+h0#
            set matrix height 1, x, z, h#
         else
            h# = get matrix height(1,x,z)-h1#
            set matrix height 1, x, z, h#
         endif
         heights#(x,z) = h#
      next x
   next z
 
   update matrix 1
RETURN
 
 
 
_smooth_matrix:
 
   for z = 0 to tz
      for x = 0 to tx
         count = 0
         h1# = 0
         h2# = 0
         h3# = 0
         h4# = 0
         h5# = 0
         h6# = 0
         h7# = 0
         h8# = 0
         if z < tz
            if x > 0 then h1# = heights#(x-1,z+1) : inc count
            h2# = heights#(x,z+1) : inc count
            if x < tx then h3# = heights#(x+1,z+1) : inc count
         endif
 
         if x > 0 then h4# = heights#(x-1,z) : inc count
         if x < tx then h5# = heights#(x+1,z) : inc count
 
         if z > 0
            if x > 0 then h6# = heights#(x-1,z-1) : inc count
            h7# = heights#(x,z-1) : inc count
            if x < tx then h8# = heights#(x+1,z-1) : inc count
         endif
 
         avg# = (h1#+h2#+h3#+h4#+h5#+h6#+h7#+h8#) / count
 
         set matrix height 1,x,z,avg#
      next x
   next z
 
   update matrix 1
 
   for z = 0 to tz
      for x = 0 to tx
         heights#(x,z) = get matrix height(1,x,z)
      next x
   next z
 
RETURN
 
 
 
 
camera_stuff:
  oldcx#=cx#
  oldcz#=cz#
  speed# = 5
  if upkey()=1
    cx#=newxvalue(cx#,a#,speed#)
    cz#=newzvalue(cz#,a#,speed#)
  endif
  if downkey()=1
    cx#=newxvalue(cx#,a#,-speed#)
    cz#=newzvalue(cz#,a#,-speed#)
  endif
  if leftkey()=1
    cx#=newxvalue(cx#,wrapvalue(a#-90.0),speed#)
    cz#=newzvalue(cz#,wrapvalue(a#-90.0),speed#)
  endif
  if rightkey()=1
    cx#=newxvalue(cx#,wrapvalue(a#+90.0),speed#)
    cz#=newzvalue(cz#,wrapvalue(a#+90.0),speed#)
  endif
 
   if shiftkey() then inc cy#, 2
   if controlkey() then dec cy#, 2
 
 
 
  a#=wrapvalue(a#+(mousemovex()/3.0))
  cxa#=cxa#+(mousemovey()/3.0)
  if cxa#<-90.0 then cxa#=-90.0
  if cxa#>90.0 then cxa#=90.0
  cy# = get ground height(1,cx#,cz#)
  position camera cx#,cy#+100,cz#
  rotate camera wrapvalue(cxa#),a#,0
RETURN
 
 
function point_line(px#,py#, x1#,y1#,x2#,y2#)
   dp# = (x2# - x1#) * (py# - y1#) - (px# - x1#) * (y2# - y1#)
endfunction dp#