Page 2 of 2

Re: Procedural Terrain Generation from Tables

Posted: Mon Sep 26, 2022 12:54 pm
by darkfrei
KedyZ wrote: Mon Sep 26, 2022 12:38 pm do i need to put a random number generator somewhere?
Just create some random values:

Code: Select all

local seedX = love.math.random(1024)
local seedY = love.math.random(1024)
and then add it to the noise position:

Code: Select all

local value = love.math.noise( 0.08*x+seedX, 0.2*y+seedY)
Update:
Also with other scale:

Code: Select all

function love.load()
	tileSize = 32 -- for image 32x32 pixels
	a = {image = love.graphics.newImage("air.png")} -- image 32x32 pixels
	g = {image = love.graphics.newImage("ground.png")} -- image 32x32 pixels
	local seedX = love.math.random(1024)
	local seedY = love.math.random(1024)

	local w, h = 25*4, 19*4-1
	map = {}
	for y = 1, h do
		map[y] = {}
		for x = 1, w do
			local value = love.math.noise( 0.04*x+seedX, 0.14*y+seedY)
			if value > 0.7 then -- 70% air and 30% ground
				map[y][x] = g -- ground
			else
				map[y][x] = a -- air
			end
		end
	end
end

function love.draw()
	local scale = 0.25
	for y, xs in ipairs (map) do
		for x, tile in ipairs (xs) do
			love.graphics.draw(tile.image, (x-1)*tileSize*scale, (y-1)*tileSize*scale, 0, scale, scale)
		end
	end
end
tile-generation-02.png
tile-generation-02.png (23.6 KiB) Viewed 2551 times
tile-generation-02.love
(1.61 KiB) Downloaded 80 times

Re: Procedural Terrain Generation from Tables

Posted: Mon Sep 26, 2022 5:31 pm
by milon
Here's an old/barebones/crappy example that you can look at too:
viewtopic.php?f=14&t=84984

I haven't touched the code in ages, but it's a more complex procedural (overworld) terrain generator than a basic space/wall generator.

Re: Procedural Terrain Generation from Tables

Posted: Tue Sep 27, 2022 1:00 pm
by KedyZ
and is it possible to make grass at the top?

Re: Procedural Terrain Generation from Tables

Posted: Tue Sep 27, 2022 3:46 pm
by darkfrei
KedyZ wrote: Tue Sep 27, 2022 1:00 pm and is it possible to make grass at the top?
Very easy! Just check that the the tile above was air:

Code: Select all

function love.load()
	tileSize = 32 -- for image 32x32 pixels
	a = {image = love.graphics.newImage("air.png")} -- image 32x32 pixels
	g = {image = love.graphics.newImage("ground.png")} -- image 32x32 pixels
	gg = {image = love.graphics.newImage("ground-grass.png")} -- image 32x32 pixels
	local seedX = love.math.random(1024)
	local seedY = love.math.random(1024)

	local w, h = 25, 19
	map = {}
	for y = 1, h do
		map[y] = {}
		for x = 1, w do
			local value = love.math.noise( 0.04*x+seedX, 0.14*y+seedY)
			if value > 0.7 then -- 70% air and 30% ground
				if map[y-1] and map[y-1][x] and map[y-1][x] == a then
					-- one tile above was an air
					map[y][x] = gg -- ground grass
				else
					map[y][x] = g -- ground
				end
			else
				map[y][x] = a -- air
			end
		end
	end
end

function love.draw()
	local scale = 1
	for y, xs in ipairs (map) do
		for x, tile in ipairs (xs) do
			love.graphics.draw(tile.image, (x-1)*tileSize*scale, (y-1)*tileSize*scale, 0, scale, scale)
		end
	end
end
tile-generation-03.png
tile-generation-03.png (22.31 KiB) Viewed 2503 times
tile-generation-03.love
(2.22 KiB) Downloaded 82 times

Re: Procedural Terrain Generation from Tables

Posted: Tue Oct 11, 2022 11:41 am
by KedyZ
i tried to add ores to the generation but i dont see where can i put it so it generates

Re: Procedural Terrain Generation from Tables

Posted: Tue Oct 11, 2022 3:46 pm
by Nikki
you could add ore like so:

Code: Select all

			if value > 0.7 then -- 70% air and 30% ground
				if map[y-1] and map[y-1][x] and map[y-1][x] == a then
					-- one tile above was an air
					map[y][x] = gg -- ground grass
				else
					map[y][x] = g -- ground
					if value >= 0.6 and value <= 0.65 then
						map[y][x] = ore
					end
				end
			else
				map[y][x] = a -- air
			end

Re: Procedural Terrain Generation from Tables

Posted: Wed Oct 12, 2022 10:56 am
by KedyZ
thx

Re: Procedural Terrain Generation from Tables

Posted: Fri Oct 14, 2022 8:50 am
by darkfrei
You can make the ore with simple random:

Code: Select all

	for y = 1, h do
		map[y] = {}
		for x = 1, w do
			local value = love.math.noise( 0.04*x+seedX, 0.14*y+seedY)
			if value > 0.7 then -- 70% air and 30% ground
				if map[y-1] and map[y-1][x] and map[y-1][x] == a then
					map[y][x] = gg -- ground with grass
				elseif math.random (6) == 1 then
					map[y][x] = go -- ground with ore
				else
					map[y][x] = g -- ground
				end
			else
				map[y][x] = a -- air
			end
		end
	end
tile-generation-04.png
tile-generation-04.png (24.61 KiB) Viewed 2295 times