Page 1 of 2

Procedural Terrain Generation from Tables

Posted: Sun Sep 25, 2022 11:10 am
by KedyZ
Can someone help me to code a Procedural Terrain Generation from Tables?

Re: Procedural Terrain Generation from Tables

Posted: Sun Sep 25, 2022 1:33 pm
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?

Re: Procedural Terrain Generation from Tables

Posted: Sun Sep 25, 2022 2:26 pm
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.

Re: Procedural Terrain Generation from Tables

Posted: Sun Sep 25, 2022 2:28 pm
by KedyZ
Also it won't be the same terrain, it will generate not the same.

Re: Procedural Terrain Generation from Tables

Posted: Mon Sep 26, 2022 7:16 am
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

Re: Procedural Terrain Generation from Tables

Posted: Mon Sep 26, 2022 11:36 am
by KedyZ
procedual generation just shows me nothing

Re: Procedural Terrain Generation from Tables

Posted: Mon Sep 26, 2022 12:34 pm
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

Re: Procedural Terrain Generation from Tables

Posted: Mon Sep 26, 2022 12:34 pm
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 2775 times
tile-generation-01.love
(1.76 KiB) Downloaded 78 times

Re: Procedural Terrain Generation from Tables

Posted: Mon Sep 26, 2022 12:37 pm
by KedyZ
still the same when you open it twice

Re: Procedural Terrain Generation from Tables

Posted: Mon Sep 26, 2022 12:38 pm
by KedyZ
do i need to put a random number generator somewhere?