`Globals; Can be changed
Global CameraSpeed = 1
Global MatrixSide = 200
Global NumSquares = 20
Global HeightConstant = 20
Global MaxAddHeight = 10
Global SmoothnessConstant = 20
 
`No point changing these
Global MatrixX As Float
Global MatrixZ As Float
 
`Screen Stuff
Sync on
Sync Rate 60
 
Fog on
Fog Distance MatrixSide*.8
Fog Color 0,0,200
color backdrop rgb(0,0,200)
 
 
 
 
PrepareMatrixes()
 
Position Camera (MatrixSide*3)/2,HeightConstant*5,(MatrixSide*3)/2
 
 
 
 
Do
 
`Control Camera Using Arrowkeys 0,1,1
If upkey() = 1 Then Pitch camera up CameraSpeed
If downkey() = 1 Then Pitch camera down CameraSpeed
If leftkey() = 1 Then Turn Camera left CameraSpeed
If rightkey() = 1 Then Turn Camera right CameraSpeed
 
If shiftkey() = 1 Then Move Camera CameraSpeed
If controlkey() = 1 Then Move Camera -CameraSpeed
 
 
CheckMatrixes()
 
Text 0,0,str$(screen fps())
`Text 0,20,str$(get ground height(5,Camera Position X(), Camera Position Z()))
`Text 0,40,str$(Camera Position X())
`Text 150,40,str$(Camera Position Z())
 
Sync
Loop
 
 
 
 
Function PrepareMatrixes()
 
`Grass Texture
cls
box 0,0,100,100,rgb(100,200,100),rgb(100,200,100),rgb(100,200,100),rgb(100,200,100)
Get Image 1,0,0,100,100
 
For x = 0 To 2
	For z = 0 To 2
		Make Matrix x*3+z+1, MatrixSide, MatrixSide, NumSquares, Numsquares
		Prepare Matrix Texture x*3+z+1, 1, 1, 1
	Next z
Next x
 
UpdateMatrixes()			
 
endfunction
 
 
 
 
 
 
 
Function CheckMatrixes()
 
CameraX = Camera Position X()
CameraZ = Camera Position Z()
 
If MatrixX + MatrixSide >= CameraX
	MatrixX = MatrixX - MatrixSide	
	Update = 1
endif
 
If MatrixX + (MatrixSide*2) <= CameraX
	MatrixX = MatrixX + MatrixSide	
	Update = 1
endif
 
If MatrixZ + MatrixSide >= CameraZ
	MatrixZ = MatrixZ	- MatrixSide
	update = 1
endif
 
If MatrixZ + (MatrixSide*2) <= CameraZ
	MatrixZ = MatrixZ + MatrixSide
	update = 1
endif
 
 
If update = 1 Then Updatematrixes()
 
Endfunction
 
 
 
 
 
 
 
Function Updatematrixes()
 
NumberX as float
NumberZ as float
smoothness as float
height as float
 
For x = 0 To 2
	For z = 0 To 2
 
		For TileX = 0 To NumSquares
			FOr TileZ = 0 To NumSquares
            NumberX = wrapvalue(((MatrixX/MatrixSide)+x)*(Numsquares)+ TileX)
            NumberZ = wrapvalue(((MatrixZ/MatrixSide)+z)*(Numsquares)+ TileZ)
				smoothness = SmoothnessConstant
				Height = cos((NumberX)+(NumberZ))*MaxAddHeight         				
				Set Matrix Height x*3+z+1, TileX, TileZ, HeightConstant*((sin(NumberX*smoothness)+sin(NumberZ*smoothness))+Height)
 
			next tileZ
		next tilex
	   Position Matrix x*3+z+1, (x*MatrixSide)+MatrixX, 0, (z*MatrixSide) + MatrixZ
	Next z
Next x
 
endfunction