Page 1 of 1

How to generate ore in minetest?

PostPosted: Thu Oct 04, 2012 17:36
by ownsyouall
I've made my own ore using the lua api and im trying to add it into the game. I'm very confused on how to do this though. I took a look at how minetest_game dose it via (Code from minetest_game/mods/default/mapgen.lua)
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local function generate_ore(name, wherein, minp, maxp, seed, chunks_per_volume, chunk_size, ore_per_chunk, height_min, height_max)
if maxp.y < height_min or minp.y > height_max then
return
end
local y_min = math.max(minp.y, height_min)
local y_max = math.min(maxp.y, height_max)
local volume = (maxp.x-minp.x+1)*(y_max-y_min+1)*(maxp.z-minp.z+1)
local pr = PseudoRandom(seed)
local num_chunks = math.floor(chunks_per_volume * volume)
local inverse_chance = math.floor(chunk_size*chunk_size*chunk_size / ore_per_chunk)
--print("generate_ore num_chunks: "..dump(num_chunks))
for i=1,num_chunks do
local y0 = pr:next(y_min, y_max-chunk_size+1)
if y0 >= height_min and y0 <= height_max then
local x0 = pr:next(minp.x, maxp.x-chunk_size+1)
local z0 = pr:next(minp.z, maxp.z-chunk_size+1)
local p0 = {x=x0, y=y0, z=z0}
for x1=0,chunk_size-1 do
for y1=0,chunk_size-1 do
for z1=0,chunk_size-1 do
if pr:next(1,inverse_chance) == 1 then
local x2 = x0+x1
local y2 = y0+y1
local z2 = z0+z1
local p2 = {x=x2, y=y2, z=z2}
if minetest.env:get_node(p2).name == wherein then
minetest.env:set_node(p2, {name=name})
end
end
end
end
end
end
end
--print("generate_ore done")
end

generate_ore("default:stone_with_coal", "default:stone", minp, maxp, seed+0, 1/8/8/8, 3, 8, -31000, 64)
generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+1, 1/12/12/12, 2, 3, -15, 2)
generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+2, 1/9/9/9, 3, 5, -63, -16)
generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+3, 1/7/7/7, 3, 5, -31000, -64)
generate_ore("default:mese", "default:stone", minp, maxp, seed+4, 1/16/16/16, 2, 3, -127, -64)


But the variables of chunks_per_volume, chunk_size and ore_per_chunk don't make any sense to me. I've looked around in the website and in irc but no one seems to know. So what exactly dose chunks_per_volume, chunk_size and ore_per_chunk do? Is there an easier way to make it so ores generate while still being able to set how rare they are and where they spawn height wise?

PostPosted: Thu Oct 04, 2012 18:03
by PilzAdam
ore: the mineral you want to add

chunk: a chunk is a group of ores (its a cube)

ore_per_chunk: how many nodes in the chunk should be turned from wherein to the ore: If you have coal that is generated into stone then not all stone in the chunk is turned into coal

chunk_size: the length of the sides of the cubes

chunks_per_volume: This is difficult; it says how many chunks should be at a volume of the wherein material; e.g. if you want 2 chunks in a volume of 100 m^3 then you set ores_per_chunk = 2/100

I hope this helps. Feel free to ask what you dont understand.

PostPosted: Thu Oct 04, 2012 21:29
by ownsyouall
Ok thanks a bunch but im not clear on ore_per_chunk. So say i have a chunk_size of 10 by 10 by 10 so 100 blocks in it total. If i set ore_per_chunk to 1 would that mean 1% of those blocks in that chunk would be my ore?

PostPosted: Fri Oct 05, 2012 12:52
by PilzAdam
ownsyouall wrote:Ok thanks a bunch but im not clear on ore_per_chunk. So say i have a chunk_size of 10 by 10 by 10 so 100 blocks in it total. If i set ore_per_chunk to 1 would that mean 1% of those blocks in that chunk would be my ore?

No: 10 by 10 by 10 => volume = 1000
if ore_per_chunk = 1 then it would be 0.1%
if ore_per_chunk = 10 then it would be 1%

E.g.: In my clouds mod in my fork of minetest_game the generate_cloud function based on generate_ore. I have set ore_per_chunk to 1/4 of the chunk size, so 1/4 of all air nodes in the chunk are turned to cloud.