Sync On:Sync Rate 60:Autocam Off
Set Camera Range .01,100
Make Object Triangle 200,0,0,0,0,.01,0,0,0,0
Global GSize as Integer
GSize=100
Dim GlobalHeights(GSize,GSize)
Global Part as Integer
 
Print "Drawing Grass..."
Grass()
Print "Global Heights..."
Establish_Globals()
 
Update_Terrain(1,1)
Update_Terrain(1,1)
Print "Terrain Done..."
Sync
 
 
Position Camera 1000,0,1000
 
Hide Object 200
Turn Object Right 200,45
 
lx=1:ly=1
Do
   Sprite 2,0,50,2
   Set Sprite Alpha 2,100
   Control Camera Using Arrowkeys 0,.05,.8
   x#=Camera Position X()
   z#=Camera Position Z()
   y#=Get Terrain Ground Height(1,x#,z#)+.08
   Position Camera x#,y#,z#
   Position Object 200, Camera Position X(),0,Camera Position Z()
   Move Object 200,-128
   nx=Object Position X(200)/64
   ny=Object Position Z(200)/64
   if nx<>lx or ny<>ly or Part>0
      Update_Terrain(nx,ny)
      lx=nx:ly=ny
   Endif
   Set Cursor 0,0
   rx#=Camera Position X()-Object Position X(1)
   rz#=Camera Position Z()-Object Position Z(1)
   Print rx#
   Print rz#
   ink Rgb(255,0,0),0
   box rx#/2,rz#/2+50,rx#/2+2,rz#/2+52
   Sync
Loop
 
End
 
Function Update_Terrain(x,y)
   if Part=0
      Print "Heightmap..."
      Create_Heightmap(x,y)
      Part=1
   Else
      Print "Make Terrain..."
      Sync
      If Object Exist(1) then Delete Object 1
      Make Object Terrain 1
      Set Terrain Heightmap 1,"Heightmap.bmp"
      Set Terrain Texture 1,3,4
      Set Terrain Tiling 1,2
      Set Terrain Scale 1,2,.25,2
      Set Terrain Split 1,20
      Build Terrain 1
      Set Object Collision Off 1
      Position Object 1,(x)*64,0,(y)*64
      Part=0
   Endif
Endfunction
 
 
 
Function Establish_Globals()
   GlobalHeights(1,1)=Rnd(50)+100
   For i=2 to GSize
      GlobalHeights(i,1)=rnd(50)+100
      adj=GlobalHeights(i-1,1)+rnd(13)-7
      GlobalHeights(i,1)=adj
   Next i
   For i=2 to GSize
      GlobalHeights(1,i)=rnd(50)+100
      adj=GlobalHeights(1,i-1)+rnd(13)-7
      GlobalHeights(1,i)=adj
   Next i
   For i = 2 to GSize
      For j= 2 to GSize
         adj=GlobalHeights(i-1,j-1)
         adj=adj+GlobalHeights(i,j-1)
         adj=adj+GlobalHeights(i-1,j)
         adj=adj/3
         nh=rnd(13)-7
         gh=adj+nh
         if gh<0 then gh=0
         if gh>200 then gh=200
         GlobalHeights(i,j)=gh
      Next j
   Next i
   Sleep 1000
Endfunction
 
Function Global_Position(n)
   Do
      if n>0 and n<GSize then Exit
      if n<1 then n=n+GSize
      if n>GSize then n=n-GSize
   Loop
Endfunction n
 
Function Splotch(h,s)
   Lock Pixels
   Ink 0,0
   Box 200,200,230+s,230+s
   c=rnd(7)+h-3
   Ink Rgb(c,c,c),0
   r=rnd(s/2)+s/4
   For i# = 0.0 to 360.0 step 2
      x=sin(i#)*r+215+s/2
      y=cos(i#)*r+215+s/2
      line 207+s/2,207+s/2,x,y
      r=r+(rnd(2)-1)
      if r>s+15 then r=s+15
      m=s-15:If m<2 then m=2
      if r<m then r=m
   Next i#
   Unlock Pixels
   Get Image 1,200,200,230+s,230+s
Endfunction
 
Function Create_Heightmap(x,y)
   If Bitmap Exist(1) Then Delete Bitmap 1
   Create Bitmap 1,512,512
   Set Current Bitmap 1
   Ink rgb(128,128,128),0
   Box 0,0,199,199
   For ny=0 to 5
      For nx=0 to 5
         xr=x+nx:yr=y+ny
         gx=Global_Position(xr)
         gy=Global_Position(yr)
         TileHeight=GlobalHeights(gx,gy)
         Seed=abs(xr*yr)
         Randomize Seed
         ox=x+(nx*32):oy=y+(ny*32)
         mx=x+((nx+1)*32):my=y+((ny+1)*32)
         Splotch(TileHeight,32)
         Sprite 1,200,200,1
         Set Sprite 1,0,1
         Set Sprite Alpha 1,200
         Paste Sprite 1,ox+rnd(32)-16,oy+rnd(32)-16
         Box ox,oy,mx,my
      Next nx
   Next ny
   For ny=0 to 5
      For nx=0 to 5
         xr=x+nx:yr=y+ny
         Seed=abs(xr*yr)
         Randomize Seed
         ox=x+(nx*32):oy=y+(ny*32)
         For i = 1 to rnd(3)+2
            sx=ox+rnd(32)-16:sy=oy+rnd(32)-16
            h=point(sx,sy)+Rnd(5)-Rnd(5)
            Splotch(h,15)
            Sprite 1,200,200,1
            Set Sprite 1,0,1
            Set Sprite Alpha 1,128
            Paste Sprite 1,sx,sy
         Next i
         xr=x+nx:yr=y+ny
         Seed=abs(xr*yr)
         Randomize Seed
         ox=x+(nx*32):oy=y+(ny*32)
         for noise=1 to rnd(50)+rnd(50)+rnd(50)+50
            noisex=rnd(32)+ox
            noisey=rnd(32)+oy
            c=point(noisex,noisey)
            c=rgbr(c)+(rnd(5)-2)
            if c<0 then c=0
            if c>255 then c=255
            ink rgb(c,c,c),0
            dot noisex,noisey
         Next noise
      Next nx
   Next ny
   if file exist("Heightmap.bmp") Then Delete File "Heightmap.bmp"
   Get Image 2,32,32,159,159,0
   Save Image "Heightmap.bmp",2
   If Sprite Exist(1) Then Delete Sprite 1
   Set Current Bitmap 0
   Delete Bitmap 1
   Set Current Bitmap 0
   Ink Rgb(255,255,255),0
   Print "Heightmap Done"
   Sync
Endfunction
 
Function Grass()
   If Bitmap Exist(1) Then Delete Bitmap 1
   Create Bitmap 1,512,512
   Set Current Bitmap 1
   Ink Rgb(0,90,0),0
   Box 0,0,511,511
   g=0
   Do
      Splotch(180,15)
      Sprite 1,200,200,1
      Size Sprite 1,rnd(3)+1,Rnd(3)+1
      Set Sprite Alpha 1,128
      Set Sprite Diffuse 1,0,255,0
      Paste Sprite 1,rnd(67)-2,rnd(67)-2
      g=g+1
      If rnd(500)+500<g Then Exit
   Loop
   Get Image 3,1,1,64,64,0
   Ink Rgb(0,90,0),0
   Box 0,0,511,511
   g=0
   Do
      Splotch(rnd(80)+130,30)
      Sprite 1,200,200,1
      Size Sprite 1,rnd(10)+10,Rnd(10)+10
      Set Sprite Alpha 1,64
      Set Sprite Diffuse 1,0,255,0
      Paste Sprite 1,rnd(75)-10,rnd(75)-10
      g=g+1
      If rnd(1000)+200<g Then Exit
   Loop
   Get Image 4,1,1,64,64,0
   Set Current Bitmap 0
   Delete Bitmap 1
   Delete Sprite 1
Endfunction