Advertisement
eea

fnoise

eea
Mar 31st, 2024 (edited)
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  1. local xseed = (math.random()-.5)*10^5
  2. local yseed = (math.random()-.5)*10^5
  3. --local spawnp = workspace:WaitForChild("SpawnLocation")
  4. local w = 100
  5. local h = 100
  6. local grid = {}
  7. local fgrid = {}
  8. local land = {}
  9. local world = Instance.new("Folder", workspace)
  10. world.Name = "World"
  11. --world:SetAttribute("w", w)
  12. --world:SetAttribute("h", h)
  13. --spawnp.Position = Vector3.xAxis*w*0.5 + Vector3.zAxis*h*0.5 + Vector3.yAxis*117.05
  14.  
  15. local water = Color3.new(0, 0, 0.5)
  16. local lamd = Color3.new(0, 0.65, 0)
  17.  
  18. function inbounds(x,y)
  19.     return x >= 1 and x <= w and y >= 1 and y <= h
  20. end
  21.  
  22. function gn(x,y)
  23.     local neiehbtgorps = {}
  24.     local land_count = 0
  25.     local water_count = 0
  26.     for py = y-1,y+1 do
  27.         for px = x-1,x+1 do
  28.             if inbounds(px,py) and ((px ~= x) or (py ~= y)) then
  29.                 table.insert(neiehbtgorps, fgrid[py][px])
  30.                 if fgrid[py][px]:GetAttribute("LandType") == 1 then
  31.                     land_count += 1
  32.                 end
  33.                 if fgrid[py][px]:GetAttribute("LandType") == 0 then
  34.                     water_count += 1
  35.                 end
  36.             end
  37.         end
  38.     end
  39.     return neiehbtgorps, {land_count, water_count}
  40. end
  41.  
  42. function find(x,y)
  43.     local n = ""
  44.     for nx = x-1,x+1 do
  45.         for ny = y-1,y+1 do
  46.             if nx >= 1 and nx <= w and ny >= 1 and ny <= h and (nx ~= x) and (ny ~= y) then
  47.                 n ..= nx..","..ny.." "
  48.             end
  49.         end
  50.     end
  51.     return n:sub(1,#n-1)
  52. end
  53.  
  54. function fnoise(x,y,z,octaves,lacunarity,persistance)
  55.     local tnoise = 0
  56.     local amplitude = 1
  57.     local detail = 1
  58.     for i = 1, octaves do
  59.         tnoise += math.noise(x*detail + xseed,y*detail + yseed,z*detail + xseed-yseed)*amplitude
  60.         amplitude *= persistance
  61.         detail *= lacunarity
  62.     end
  63.     tnoise += math.noise(x*lacunarity,y*lacunarity,z*lacunarity)*amplitude
  64.     return tnoise
  65. end
  66.  
  67. for y = 1,h do
  68.     local the_table = {}
  69.     local tt = {}
  70.     for x = 1,w do
  71.        
  72.         local tnoise = fnoise(x/23,y/23,0,6,3,0.45)
  73.        
  74.         local The_Instance = Instance.new("Part", script)
  75.         The_Instance.Material = "SmoothPlastic"
  76.         The_Instance.Position = Vector3.new(x, 7, y)
  77.         The_Instance.Size = Vector3.one
  78.         The_Instance.Anchored = true
  79.         The_Instance.Name = x..","..y
  80.         The_Instance:SetAttribute("L", The_Instance.Name)
  81.         The_Instance:SetAttribute("N", find(x,y))
  82.         if tnoise + (math.random()-.5)/50 > 0.175 then
  83.             The_Instance.Color = Color3.new(0, .65, 0)
  84.             The_Instance:SetAttribute("LandType", 1)
  85.             table.insert(land, {x, y})
  86.             the_table[x] = {1}
  87.             The_Instance:SetAttribute("B", false)
  88.         else
  89.             The_Instance.Color = Color3.new(0, 0, .5)
  90.             The_Instance:SetAttribute("LandType", 0)
  91.             the_table[x] = {0}
  92.         end
  93.         tt[x] = The_Instance
  94.     end
  95.     grid[y] = the_table;
  96.     fgrid[y] = tt;
  97.     task.wait()
  98. end
  99.  
  100. for y = 1,h do
  101.     for x = 1,w do
  102.         local p = fgrid[y][x]
  103.         local land_type = p:GetAttribute("LandType")
  104.         local n, c = gn(x, y)
  105.        
  106.         local col = p.Color
  107.         if c[1] >= 1 and land_type == 0 then
  108.             col = Color3.new(0,0,0.4)
  109.         end
  110.         if c[2] >= 3 and land_type == 1 then
  111.             col = Color3.new(0,0.55,0)
  112.             p:SetAttribute("B", true)
  113.         end
  114.         p.Color = col
  115.     end
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement