Procedural Terrain Generation from Tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Procedural Terrain Generation from Tables

Post 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 2325 times
tile-generation-02.love
(1.61 KiB) Downloaded 79 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Procedural Terrain Generation from Tables

Post 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.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
KedyZ
Prole
Posts: 11
Joined: Sun Sep 25, 2022 11:06 am

Re: Procedural Terrain Generation from Tables

Post by KedyZ »

and is it possible to make grass at the top?
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Procedural Terrain Generation from Tables

Post 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 2277 times
tile-generation-03.love
(2.22 KiB) Downloaded 82 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
KedyZ
Prole
Posts: 11
Joined: Sun Sep 25, 2022 11:06 am

Re: Procedural Terrain Generation from Tables

Post by KedyZ »

i tried to add ores to the generation but i dont see where can i put it so it generates
User avatar
Nikki
Citizen
Posts: 83
Joined: Wed Jan 25, 2017 5:42 pm

Re: Procedural Terrain Generation from Tables

Post 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
KedyZ
Prole
Posts: 11
Joined: Sun Sep 25, 2022 11:06 am

Re: Procedural Terrain Generation from Tables

Post by KedyZ »

thx
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Procedural Terrain Generation from Tables

Post 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 2069 times
Attachments
tile-generation-04.love
(2.64 KiB) Downloaded 70 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 54 guests