Register_on_generated local variables

User avatar
Kenney
Member
 
Posts: 131
Joined: Fri Jan 02, 2015 12:12
In-game: Kenney

Register_on_generated local variables

by Kenney » Sat May 23, 2015 22:35

Very strange, but I'm guessing it's related to the 'local' variables. The first part of the code works (dirt_grass), the second (rock) however does nothing. Anyone able to tell what I'm doing wrong?

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)
   for z = minp.z, maxp.z, 1 do
   for x = minp.x, maxp.x, 1 do
   for y = minp.y, maxp.y, 1 do
      local p = {x = x, y = y, z = z}
      local ran = math.random(1, 3)
      
      if minetest.get_node(p).name == 'voxus:dirt_grass' then
         minetest.set_node(p, {name='voxus:dirt_grass'})
      end
      
      if minetest.get_node(p).name == 'voxus:rock' and ran == 1 then
         local p_above = p
         p_above.y = p.y + 1
         
         local p_below = p
         p_below.y = p.y - 1
      
         if minetest.get_node(p_below).name == 'air' then
            minetest.set_node(p_below, {name='voxus:trunk'})
         end
         
         if minetest.get_node(p_above).name == 'air' then
            minetest.set_node(p_above, {name='voxus:mushroom_tall'})
         end
      end
   end
   end
   end
end)
Games:
Voxus | UFO Race
 

Sokomine
Member
 
Posts: 2980
Joined: Sun Sep 09, 2012 17:31

Re: Register_on_generated local variables

by Sokomine » Sat May 23, 2015 22:49

Kenney wrote:The first part of the code works (dirt_grass), the second (rock) however does nothing.

How can you tell that? You seem to place the same node that was there already? At least in the dirt_grass part.

Kenney wrote:Anyone able to tell what I'm doing wrong?

Are you aware of voxelmanip? It's a lot faster than repeated get/set_node calls. There are also diffrent methods for spawning tree-like things. They're called "decorations" in MT. Pretty useful for mapgens :-)
A list of my mods can be found here.
 

User avatar
Kenney
Member
 
Posts: 131
Joined: Fri Jan 02, 2015 12:12
In-game: Kenney

Re: Register_on_generated local variables

by Kenney » Sat May 23, 2015 22:55

Dirt_grass has an on_construct handler which actually replaces it with a random other block, on_construct doesn't seem to fire when the map is being generated so I hacked that code in there to get it working.

Not aware of voxelmanip or how it works, decorations also do not seem to work properly and don't allow stuff like placing mushrooms in dark places.
Games:
Voxus | UFO Race
 

Sokomine
Member
 
Posts: 2980
Joined: Sun Sep 09, 2012 17:31

Re: Register_on_generated local variables

by Sokomine » Sat May 23, 2015 23:25

Kenney wrote:Dirt_grass has an on_construct handler which actually replaces it with a random other block, on_construct doesn't seem to fire when the map is being generated so I hacked that code in there to get it working.

Why not put one of the random blocks there directly?

Kenney wrote:Not aware of voxelmanip or how it works, decorations also do not seem to work properly and don't allow stuff like placing mushrooms in dark places.

You really ought to look that up. It'll help you a lot and speed your game up considerably. Especially voxelmanip may be helpful for you. It's way faster than set/get_node. It does operate on content_id rather than on nodenames, handles param2 diffrently and does not directly handle metadata at all, but - did I say that already? :-) It's fast. As far as mushrooms go, there's a mod somewhere that places them. But in general, you ought to take a look at the Ethereal subgame. That one uses decorations to create an intresting landscape.
A list of my mods can be found here.
 

prestidigitator
Member
 
Posts: 632
Joined: Thu Feb 21, 2013 23:54

Re: Register_on_generated local variables

by prestidigitator » Sun May 24, 2015 01:28

For one thing your positional logic probably isn't doing what you expect it to do. Positions are table objects, stored by reference not value. So you can't just assign one variable referring to a table to another to get an independent copy. This code:

Kenney wrote:
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 p_above = p
p_above.y = p.y + 1
         
local p_below = p
p_below.y = p.y - 1


Will result in three variables all referring to the same position, which will have the same coordinates as the original position (since you first added then subtracted the same value from y). To copy a position, you can do one of the following:

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 p2 = vector.new(p1);
-- OR
local p2 = { x = p1.x, y = p1.y, z = p1.z };
-- OR
local p2 = vector.new(p1.x, p1.y, p1.z);  -- EDIT: Fixed typo.  Had "p2.y" for last argument.


The second and third options give you a convenient place to create a new vector with different coordinates, since you can add or subtract right in the constructor. With the first option, you'd first make the copy and then modify the coordinate (e.g. p2.y = p2.y + 1).
Last edited by prestidigitator on Sun May 24, 2015 10:13, edited 2 times in total.
 

paramat
Member
 
Posts: 2662
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat

Re: Register_on_generated local variables

by paramat » Sun May 24, 2015 04:35

The lua voxel manipulator processes nodes in bulk instead of individual calls to the map per node. Using it my lua mapgens have gone from a minute per chunk to a second per chunk generation time. Used in 'on generated' it processes a mapchunk at a time by using the already-created 'mapgen object voxelmanip'. See my mods for examples of use. Also https://forum.minetest.net/viewtopic.php?f=18&t=6396
 

User avatar
Kenney
Member
 
Posts: 131
Joined: Fri Jan 02, 2015 12:12
In-game: Kenney

Re: Register_on_generated local variables

by Kenney » Sun May 24, 2015 20:14

Thanks guys, decided to switch over to voxelmanip which is really just as easy to handle (see code below).

One question though; how would I go about getting the amount of light at the current node? I want mushrooms to only spawn in dark areas.

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 a = VoxelArea:new{
      MinEdge={x=emin.x, y=emin.y, z=emin.z},
      MaxEdge={x=emax.x, y=emax.y, z=emax.z},
   }
   
   local data = vm:get_data()
   local sidelen = maxp.x - minp.x + 1
   
   for z = minp.z, maxp.z do
   for y = minp.y, maxp.y do
   for x = minp.x, maxp.x do
      local vi = a:index(x, y, z)
      local vi_above = a:index(x, y + 1, z)
      local vi_below = a:index(x, y - 1, z)
      
      local node_current = minetest.get_name_from_content_id(data[vi])
      local node_above   = minetest.get_name_from_content_id(data[vi_above])
      local node_below   = minetest.get_name_from_content_id(data[vi_below])
      
      -- Mushrooms
      if node_current == 'voxus:rock' and node_above == 'air' then
         data[vi_above] = minetest.get_content_id("voxus:mushroom_beige")
      end
   end
   end
   end
   
   vm:set_data(data)
   vm:write_to_map(data)
   
   vm:get_light_data()
end)
Games:
Voxus | UFO Race
 

paramat
Member
 
Posts: 2662
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat

Re: Register_on_generated local variables

by paramat » Sun May 24, 2015 21:31

See lua-api.txt, you can 'get' a luavoxelmanip of param1 values, which are the light values, it works just the same as the vm of content ids, process it then write it.
 

User avatar
Kenney
Member
 
Posts: 131
Joined: Fri Jan 02, 2015 12:12
In-game: Kenney

Re: Register_on_generated local variables

by Kenney » Sun May 24, 2015 21:39

paramat wrote:See lua-api.txt, you can 'get' a luavoxelmanip of param1 values, which are the light values, it works just the same as the vm of content ids, process it then write it.

Ah fantastic, that was easy :) Got everything working now, thanks for the help everyone!
Games:
Voxus | UFO Race
 


Return to Minetest Problems

Who is online

Users browsing this forum: No registered users and 19 guests