Page 1 of 1

LVM and mapgen help request

PostPosted: Sun Apr 20, 2014 18:12
by stu
I am looking for some help with map generation and the lua voxel manipulator.
Hopefully you can get the idea of what I am trying to do from the code.
Basically replacing each grass node with one of 4 random variants.

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
minetest.register_on_generated(function(minp, maxp, seed)
   local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
   local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
   local data = vm:get_data()
   for i in area:iterp(emin, emax) do
      if data[i] == minetest.get_content_id("default:grass") then
         local n = tostring(math.random(1, 4))
         data[i] = minetest.get_content_id("default:grass_"..n)
      end
   end
   vm:set_data(data)
   vm:set_lighting({day=0, night=0})
   vm:calc_lighting()
   vm:write_to_map(data)
end)


This code does do what I want but is incredibly slow.
Is there a better way to achieve the same effect?

Edit: Note this is not minetest_game, the grass node here is a solid (drawtype normal) node.

Re: LVM and mapgen help request

PostPosted: Sun Apr 20, 2014 18:31
by Krock
try this:
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 area = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
   local data = vm:get_data()
   local c_grass = minetest.get_content_id("default:grass")
   for i in area:iterp(minp, maxp) do
      if data[i] == c_grass then

Re: LVM and mapgen help request

PostPosted: Sun Apr 20, 2014 18:50
by stu
Krock wrote:try this:
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 area = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
   local data = vm:get_data()
   local c_grass = minetest.get_content_id("default:grass")
   for i in area:iterp(minp, maxp) do
      if data[i] == c_grass then


That is certainly a lot faster, thanks.

Edit: Although this change does make a massive improvement, it is still not quite as fast as I'd like,
maybe I am just expecting too much.

Note that I took the get_content_id calls out of the loop entirely.