So, I'm trying to establish a limiting code that ensures that (relx+x) and (relz+z) doesn't fall off the matrix boundaries. There may be an ARRAY INDEX EXISTS, but there is nothing to determine matrix in-bounds as far as I know, and the current code doesn't seem to adequately check if it is in-bounds.
Basically, I'm grabbing the nearest 5 points and averaging them together against the current, causing the surface to level out.
Previously in the code:
//Matrix Initial Generation
MAKE MATRIX 1,2000,2000,100,100
POSITION MATRIX 1,-1000,0,-1000
RANDOMIZE MATRIX 1,terrain_height
//Matrix Data
PREPARE MATRIX TEXTURE 1,2,4,4
FOR x=0 TO 99
For z=0 TO 99
t=RND(15)+1
SET MATRIX TILE 1,x,z,t
NEXT z
NEXT x
//Center Matrix Data
FOR x=43 TO 55
FOR z=45 TO 53
SET MATRIX HEIGHT 1,x,z,0
NEXT z
NEXT x
UPDATE MATRIX 1
IF SPACEKEY()=1 AND SHIFTKEY()=1
//Average Ground Normals
FOR x=1 TO 100
FOR z=1 TO 100
avg=0
it=0
FOR relx=-2 TO 2
FOR relz=-2 TO 2
IF 2<(relx+x)<98 AND 2<(relz+z)<98
INC avg,GET MATRIX HEIGHT(1,(relx+x),(relz+z))
INC it
ENDIF
NEXT relz
NEXT relx
avg=avg/it
SET MATRIX HEIGHT 1,x,z,((GET MATRIX HEIGHT(1,x,z))+avg)/2
NEXT z
NEXT x
//Normalize Inner
FOR x=44 TO 56
FOR z=46 TO 54
SET MATRIX HEIGHT 1,x,z,0
NEXT z
NEXT x
//Update
UPDATE MATRIX 1
ENDIF