Advertisement
Dadido3

Heightmap

Oct 5th, 2014
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.16 KB | None | 0 0
  1. function Mapfill_Heightmap_Fractal(Randomness, Seed)
  2.     local Chunk_Size = 16
  3.    
  4.     -- Fill it with random start values
  5.     local Size = 2 -- SIZE OF THE RANDOM ARRAY
  6.     local Heightmap = CREATE SOME RANDOM ARRAY HERE, ATLEAST 2x2 !!!!!!!!!!!!!!!!!!!!!!
  7.    
  8.     -- Do the iterations
  9.     while Size < Chunk_Size do
  10.        
  11.         -- Resize the array
  12.         for ix = Size+1, Size*2 do
  13.             Heightmap[ix] = {}
  14.             --for iy = Size+1, Size*2 do
  15.             --  Heightmap[ix][iy] = 0
  16.             --end
  17.         end
  18.         for ix = Size, 0, -1 do
  19.             for iy = Size, 0, -1 do
  20.                 Heightmap[ix*2][iy*2] = Heightmap[ix][iy]
  21.             end
  22.         end
  23.         Size = Size * 2
  24.        
  25.         -- The diamond step
  26.         for ix = 1, Size, 2 do
  27.             for iy = 1, Size, 2 do
  28.                 Heightmap[ix][iy] = (Heightmap[ix+1][iy+1] + Heightmap[ix-1][iy+1] + Heightmap[ix+1][iy-1] + Heightmap[ix-1][iy-1]) / 4
  29.             end
  30.         end
  31.        
  32.         -- The square step
  33.         for ix = 0, Size, 2 do
  34.             for iy = 1, Size, 2 do
  35.                 Heightmap[ix][iy] = (Heightmap[ix][iy-1] + Heightmap[ix][iy+1]) / 2
  36.             end
  37.         end
  38.         for ix = 1, Size, 2 do
  39.             for iy = 0, Size, 2 do
  40.                 Heightmap[ix][iy] = (Heightmap[ix-1][iy] + Heightmap[ix+1][iy]) / 2
  41.             end
  42.         end
  43.     end
  44.    
  45.     -- Return the Heightmap
  46.    
  47.     return Heightmap
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement