Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function getNoise() --replace modulescript
- return function(s,f)
- local final={}
- --find closest power of two
- local stepPo2=-1 --what power of two we used to get the value
- do
- local po2=0
- repeat
- stepPo2=stepPo2+1
- po2=2^stepPo2 --the actual value
- until po2+1>=s
- s=po2+1 --set the adjusted size
- end
- for i=1,s do --prepare the table
- final[i]={}
- for j=1,s do
- if (j==1 or j==s) and (i==1 or i==s) then
- final[i][j]=math.min(math.max(math.random()*(math.random()>0.5 and 0.5 or -0.5),-0.5),0.5)
- end
- end
- end
- local step=0 --set current iteration
- while step<stepPo2 do --while we haven't gotten to a step that's above our max size
- --i derived these values on pen+paper
- local offset=2^(stepPo2-step-1) --the amount of grid spaces we move each time
- local numSteps=2^step --the number of grid spaces we have to set in the square step
- local function halt()
- if step>3 and math.random()<0.3 then
- --wait()
- end
- end
- --diamond step
- for xPos=offset+1,s-1,offset*2 do
- for yPos=offset+1,s-1,offset*2 do
- final[yPos][xPos]=math.min(math.max(
- (final[yPos-offset][xPos-offset]+ --top-left
- final[yPos-offset][xPos+offset]+ --top-right
- final[yPos+offset][xPos+offset]+ --bottom-right
- final[yPos+offset][xPos-offset])/ --bottom-left
- 4+math.random()*(math.random()>0.5 and 1 or -1)*f^(step+1), --average and displacement
- -0.5),0.5)
- halt()
- end
- end
- --square step
- for xPos=1+offset,s,offset*2 do
- for yPos=1,s,offset*2 do
- local count,sum=0,0 --for the average
- if xPos-offset>=1 then --all of these checks are to make sure it is in-bounds
- count=count+1
- sum=sum+final[yPos][xPos-offset]
- end
- if xPos+offset<s then
- count=count+1
- sum=sum+final[yPos][xPos+offset]
- end
- if yPos-offset>=1 then
- count=count+1
- sum=sum+final[yPos-offset][xPos]
- end
- if yPos+offset<s then
- count=count+1
- sum=sum+final[yPos+offset][xPos]
- end
- if count>0 then --no division by zero, please
- final[yPos][xPos]=math.min(math.max(sum/count+math.random()*(math.random()>0.5 and 1 or -1)*f^(step+1),-0.5),0.5) -- average step and displacement
- end
- end
- halt()
- end
- for xPos=1,s,offset*2 do
- for yPos=1+offset,s,offset*2 do
- local count,sum=0,0 --for the average
- if xPos-offset>=1 then --all of these checks are to make sure it is in-bounds
- count=count+1
- sum=sum+final[yPos][xPos-offset]
- end
- if xPos+offset<s then
- count=count+1
- sum=sum+final[yPos][xPos+offset]
- end
- if yPos-offset>=1 then
- count=count+1
- sum=sum+final[yPos-offset][xPos]
- end
- if yPos+offset<s then
- count=count+1
- sum=sum+final[yPos+offset][xPos]
- end
- if count>0 then --no division by zero, please
- final[yPos][xPos]=math.min(math.max(sum/count+math.random()*(math.random()>0.5 and 1 or -1)*f^(step+1),-0.5),0.5) -- average step and displacement
- end
- end
- halt()
- end
- step=step+1 -- go onto the next iteration
- print(step)
- end
- return final
- end
- end
- local function gen()
- --[[for i,v in pairs(workspace:children()) do
- if (v:IsA("BasePart") or v:IsA("Hint")) and not v:IsA("Terrain") then
- v:Destroy()
- end
- end]]
- local start=tick()
- local getNoise=getNoise()
- local array=getNoise(2^8+1,0.45)
- local noiseFinish=tick()
- local amp=120
- local size=3
- local waterHeight=math.random(-20,-5)
- local sandHeight=math.random(5,8)
- local water=Instance.new("Part",workspace)
- water.FormFactor="Custom"
- water.Anchored=true
- water.Size=Vector3.new(size*#array,amp/2,size*#array)
- water.CFrame=CFrame.new(size*#array/2,waterHeight-3,size*#array/2)-Vector3.new(0,0.5,0)
- water.BrickColor=BrickColor.new("Toothpaste")
- water.Transparency=0.8
- water.CanCollide=false
- for y,row in pairs(array) do
- for x,height in pairs(row) do
- local p=Instance.new("Part",workspace)
- p.FormFactor="Custom"
- p.Size=Vector3.new(size,amp/2,size)
- p.CFrame=CFrame.new(x*size,math.floor(array[y][x]*amp),y*size)
- p.Anchored=true
- Instance.new("BlockMesh",p)
- if math.floor(array[y][x]*amp)<=waterHeight+sandHeight then
- p.BrickColor=BrickColor.new("Pastel yellow")
- p.Material="Plastic"
- else
- p.BrickColor=BrickColor.new("Earth green")
- p.Material="Grass"
- end
- end
- end
- workspace.Message.Text="Generated in "..tick()-start.." seconds. Noise generated in "..noiseFinish-start.." seconds."
- end
- game.Players.PlayerAdded:connect(function(p)
- p.Chatted:connect(function(m)
- if m:lower()=="regen" then
- gen()
- end
- end)
- end)
- gen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement