local function n(n,t,H,B)local S=-BASE_HEIGHT local Z=t if Z<BASE_HEIGHT*WATER then local t=CFrame.new(n*4+2,((Z+BASE_HEIGHT*WATER)*2)/2,H*4+2)local B=Vector3.new(4,(BASE_HEIGHT*WATER-Z)*2,4)workspace.Terrain:FillBlock(t,B,Enum.Material.Water)end local I=CFrame.new(n*4+2,((S+Z)*2)/2,H*4+2)local O=Vector3.new(4,(Z-S)*2,4)workspace.Terrain:FillBlock(I,O,B)end function makeChunk(t,H)local B=Vector3.new(t*CHUNK_SCALE,0,H*CHUNK_SCALE)chunks[t][H]=true for B=0,CHUNK_SCALE-1,1 do for S=0,CHUNK_SCALE-1,1 do local Z=0 local I=t*CHUNK_SCALE+B local O=H*CHUNK_SCALE+S local G=math.sqrt(I*I+O*O)if G>RADIUS then Z=math.noise(I/X_SCALE,O/Z_SCALE,GENERATION_SEED)if G-RADIUS<=RADIUS*R_SCALE then local n=(G-RADIUS)/(RADIUS*R_SCALE)Z=Z*n end end local i=Z*BASE_HEIGHT if G<RADIUS then n(I,i,O,Enum.Material.Cobblestone)else if i<BASE_HEIGHT*WATER then n(I,i,O,Enum.Material.Mud)elseif i<-BASE_HEIGHT/3 then if rnd:NextNumber()>DELTA then n(I,i,O,Enum.Material.Sand)else n(I,i,O,Enum.Material.Grass)end elseif i>BASE_HEIGHT/2 then if rnd:NextNumber()>DELTA then n(I,i,O,Enum.Material.Ice)else n(I,i,O,Enum.Material.Cobblestone)end else if rnd:NextNumber()>DELTA/2 then if rnd:NextNumber()>DELTA then n(I,i,O,Enum.Material.LeafyGrass)if G>RADIUS then spawnTree(Vector3.new(I*4,i,O*4))end else n(I,i,O,Enum.Material.Grass)end else n(I,i,O,Enum.Material.Slate)end end end end end end function checkSurroundings(n)local t,H=math.floor((n.X/4)/CHUNK_SCALE),math.floor((n.Z/4)/CHUNK_SCALE)local B=math.max(1,RENDER_DISTANCE/CHUNK_SCALE)for n=-B,B,1 do for B=-B,B,1 do local S=t+n local Z=H+B if not chunkExists(S,Z)then makeChunk(S,Z)end end end end while true do for n,t in pairs(Players:GetPlayers())do if t.Character then local n=t.Character:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if n then checkSurroundings(n.Position)end end end wait(1)game.Workspace.Finish.Value=1 end

local function mountLayer(x, heightY, z, material)
	local beginY = -BASE_HEIGHT
	local endY = heightY
	-- так что вот тут надо добавить условие на воду!!!!
	-- сперва добавим воду если надо,  а потом уже дно...
	if endY < BASE_HEIGHT*WATER then
		local cframe = CFrame.new(x * 4 + 2, (endY + BASE_HEIGHT*WATER) *2 / 2 , z * 4 + 2) -- отрисовка новой позиции
		local size = Vector3.new(4, (BASE_HEIGHT*WATER - endY) *2 , 4) -- а попросту, заполнение от низа до полученного значения
		workspace.Terrain:FillBlock(cframe, size, Enum.Material.Water)
	end

	local cframe = CFrame.new(x * 4 + 2, (beginY + endY)*2 /2 , z * 4 + 2) -- отрисовка новой позиции
	local size = Vector3.new(4, (endY - beginY)*2 , 4) -- а попросту, заполнение от низа до полученного значения
	workspace.Terrain:FillBlock(cframe, size, material)
end

function makeChunk(chunkX, chunkZ)
	local rootPosition = Vector3.new(chunkX * CHUNK_SCALE, 0, chunkZ * CHUNK_SCALE)
	chunks[chunkX][chunkZ] = true -- Acknowledge the chunk's existance.
	for x = 0, CHUNK_SCALE - 1 do
		for z = 0, CHUNK_SCALE - 1 do
			local noise=0
			local cx = (chunkX * CHUNK_SCALE) + x
			local cz = (chunkZ * CHUNK_SCALE) + z
			local r=math.sqrt(cx*cx + cz*cz) -- растояние от центра
			--- пример ровного места
			if  r > RADIUS then
				noise = math.noise(cx / X_SCALE, cz / Z_SCALE, GENERATION_SEED)
 				if r-RADIUS <= (RADIUS*R_SCALE) then
					local k=(r-RADIUS)/(RADIUS*R_SCALE)
					noise = noise * k
				end	
			end
			-- расчёт вертикали
			local cy = noise * BASE_HEIGHT			
			-- присвоение материал
			if  r < RADIUS then
				mountLayer(cx, cy, cz, Enum.Material.Cobblestone)
			else
				if cy<BASE_HEIGHT*WATER then
					mountLayer(cx, cy, cz, Enum.Material.Mud)
				elseif cy<-BASE_HEIGHT/3 then
					if rnd:NextNumber()>DELTA then -- if math.random()>DELTA then
						mountLayer(cx, cy, cz, Enum.Material.Sand)		
					else
						mountLayer(cx, cy, cz, Enum.Material.Grass)
					end 				
				elseif cy>BASE_HEIGHT/2 then
					if rnd:NextNumber()>DELTA then --if math.random()>DELTA then
						mountLayer(cx, cy, cz, Enum.Material.Ice)
					else
						mountLayer(cx, cy, cz, Enum.Material.Cobblestone)
					end 
				else
					if rnd:NextNumber()>DELTA/2 then --if math.random()>DELTA/2 then
						if rnd:NextNumber()>DELTA then
							mountLayer(cx, cy, cz, Enum.Material.LeafyGrass)
							-- высадим дерево?
							if  r > RADIUS then
								 spawnTree(Vector3.new(cx*4,cy,cz*4))
							end
						else
							mountLayer(cx, cy, cz, Enum.Material.Grass)
						end
					else
						mountLayer(cx, cy, cz, Enum.Material.Slate)
					end
				end
			end			
		end
	end
end
 
function checkSurroundings(location)
	local chunkX, chunkZ = math.floor(location.X / 4 / CHUNK_SCALE), math.floor(location.Z / 4 / CHUNK_SCALE)
	local range = math.max(1, RENDER_DISTANCE / CHUNK_SCALE)
	for x = -range, range do
		for z = -range, range do
			local cx = chunkX + x
			local cz = chunkZ + z
			if not chunkExists(cx, cz) then
				makeChunk(cx, cz)
			end
		end
	end
end
 
while true do
	for _, player in pairs(Players:GetPlayers()) do
		if player.Character then
			local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				checkSurroundings(humanoidRootPart.Position)
			end
		end
	end
	wait(1)
	game.Workspace.Finish.Value=1 -- отрисовано
end
