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

Procedural Terrain Generation from Tables

Post by KedyZ »

Can someone help me to code a Procedural Terrain Generation from Tables?
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Procedural Terrain Generation from Tables

Post by darkfrei »

KedyZ wrote: Sun Sep 25, 2022 11:10 am Can someone help me to code a Procedural Terrain Generation from Tables?
Can you show the example of source information and how the result must looking for?
: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 »

So you have an empty table named terrain. There are variables g and a. a has air.png, and g has ground.png. then it will put the variables in the tables like this: terrain = {a,a,a,a
g,g,g,a
g,a,a,g
g,g,g,g} when it will generate the table it will somehow draw the terrain.
KedyZ
Prole
Posts: 11
Joined: Sun Sep 25, 2022 11:06 am

Re: Procedural Terrain Generation from Tables

Post by KedyZ »

Also it won't be the same terrain, it will generate not the same.
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Procedural Terrain Generation from Tables

Post by darkfrei »

KedyZ wrote: Sun Sep 25, 2022 2:26 pm So you have an empty table named terrain. There are variables g and a. a has air.png, and g has ground.png. then it will put the variables in the tables like this: terrain = {a,a,a,a
g,g,g,a
g,a,a,g
g,g,g,g} when it will generate the table it will somehow draw the terrain.
The simplest way to do it is (not tested):

Code: Select all

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
map = {
	{a,a,a,a},
	{g,g,a,g},
	{g,a,a,g},
	{g,g,g,g},
}

Code: Select all

function love.draw()
	for y, xs in ipairs (map) do
		for x, tile in ipairs (xs) do
			love.graphics.draw(tile.image, (x-1)*tileSize, (y-1)*tileSize)
		end
	end
end
For procedural generation just use the noise:
https://love2d.org/wiki/love.math.noise

Code: Select all

local w, h = 4, 4
map = {}
for y = 1, h do
	map[y] = {}
	for x = 1, w do
		local value = love.math.noise( x*10, y*10 ) -- x10 makes more thick randomness
		if value > 0.8 then -- 80% air and 20% ground
			map[y][x] = g -- ground
		else
			map[y][x] = a -- air
		end
	end
end
Last edited by darkfrei on Mon Sep 26, 2022 1:31 pm, edited 1 time in total.
: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 »

procedual generation just shows me nothing
KedyZ
Prole
Posts: 11
Joined: Sun Sep 25, 2022 11:06 am

Re: Procedural Terrain Generation from Tables

Post by KedyZ »

nevermind but it shows the same every time and if i want to put it in function.load it shows me that i need to put an end at line 1
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Procedural Terrain Generation from Tables

Post by darkfrei »

KedyZ wrote: Mon Sep 26, 2022 11:36 am procedual generation just shows me nothing
Don't forget to define a and g.

Code: Select all

-- License CC0 (Creative Commons license) (c) darkfrei, 2022

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 w, h = 25, 18
	map = {}
	for y = 1, h do
		map[y] = {}
		for x = 1, w do
			local value = love.math.noise( 0.08*x, 0.2*y)
			if value > 0.8 then -- 80% air and 20% ground
				map[y][x] = g -- ground
			else
				map[y][x] = a -- air
			end
		end
	end
end

function love.update(dt)

end

function love.draw()
	for y, xs in ipairs (map) do
		for x, tile in ipairs (xs) do
			love.graphics.draw(tile.image, (x-1)*tileSize, (y-1)*tileSize)
		end
	end
end
tile-generation-01.png
tile-generation-01.png (38.11 KiB) Viewed 2598 times
tile-generation-01.love
(1.76 KiB) Downloaded 75 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 »

still the same when you open it twice
KedyZ
Prole
Posts: 11
Joined: Sun Sep 25, 2022 11:06 am

Re: Procedural Terrain Generation from Tables

Post by KedyZ »

do i need to put a random number generator somewhere?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 14 guests