Page 1 of 1

Get Nodes around a specified position

PostPosted: Sat Jan 07, 2012 14:20
by sapier
When using "get_objects_inside_radius" i recognized there was no function doing this for nodes too.

running a 3d for loop around nodes using getnode is feasable but seems to be way to time consuming, at least if done in lua.

here's a patch adding "get_nodes_inside_radius".


This feature can be used in at least 3 mods i know about:

farming -> detect water nearby
animals -> plasmaballs removing terrain nodes
minereal detector -> no need to parse surrounding in lua

wheat pumpkin etc (probably) -> detect water too

Patch

PostPosted: Sat Jan 07, 2012 15:31
by robin
Good idea!

Can you create a small mod to test the function?

I'm also just curious about the following: Is there any way to proof, that the native functions works faster than the lua implementation? I've heard it in some threads, but never saw evidence for it.

PostPosted: Sat Jan 07, 2012 21:32
by sfan5
Should be merged! +3

PostPosted: Sat Jan 07, 2012 23:02
by Jordach
+1000

it depends on the script for the speed.

PostPosted: Sat Jan 07, 2012 23:59
by randomproof
This is just pushing all the nodes at once. You would still need to loop through them in LUA. A better way to do this might be to ask the server how many of a certain node or in the radius. Another thing to consider is how you are defining a radius, cube or sphere?

PostPosted: Sun Jan 08, 2012 04:07
by sapier
Yes this still forces you to loop through the list but you won't have to go up the way to c++ and back to lua for each node.

I tought about adding a function testing presence of a specific node(s) for farming too. This will most likely add another speed increase.

But it will limit the functions use in following use cases

1) If you're interested in different nodes you'll have to call it for each node
e.g. mineral detector
2) If you're interested in all nodes except of two/three/four you can't really use the function
e.g. plasmaballs in animals which only don't harm cobble and stone by now
3) If you don't know wich nodes do interesst you on calling
e.g. if you decide which nodes you're interested by some node parameters (that may be set by other mods too)

Probably a solution migt be changing to:
get_nodes_inside_radius(pos,radius,nodename)
If nodename is nil -> return all nodes
else -> return only nodes with this name.


It's called get_nodes_inside_RADIUS ... if it's usefull to have other shapes than circle which dont have a radius there at least should be a name change to lets say get_objects_inside_AREA.

I personaly do not support cube shape because of:
1) you would have to add this for objects too to make it symmetrical (everyone looks for it at objects too if present here)
2) Even as minetests basic shape is cube most people do think in distances and cube shape would have realy strange effects if using larger "radius" values
3) If adding cube shape why not add cylinder tourus pyramid and cone too ;-)

This is MY PERSONAL opinion ;-)

PostPosted: Mon Jan 09, 2012 15:40
by randomproof
I see now the function "current_pos.getDistanceFrom(pos)" so yeah you are searching a sphere shape. It would be nice to have that as a LUA function. I'm sure some other mods could use that function.

Also, an interesting thing about LUA to C++ functions it that the number of arguments and the number of returned items are not fixed.
So you could call:
"nodes = get_nodes_inside_radius(pos, radius)"
and just get all the nodes in a table

or call:
"num_of_nodes = get_nodes_inside_radius(pos, radius, nodename)"
and get a number returned

or call:
"num_of_nodes1, num_of_nodes2 = get_nodes_inside_radius(pos, radius, nodename, nodename)"
and get two numbers returned. You could have as many different nodenames as you want.

PostPosted: Tue Jan 10, 2012 15:09
by jn
A minor nitpick: Indenting the individual "for"s of a XYZ loop doesn't necessarily help readability; see player.cpp:378.
(https://github.com/celeron55/minetest/blob/master/src/player.cpp#L378)

PostPosted: Sat Aug 25, 2012 21:53
by Neuromancer
Was get_nodes_inside_radius ever added to Minetest? What is the best way to do this short of installing sapier's patch? Do we still have to loop through all the nodes?

PostPosted: Sat Aug 25, 2012 22:01
by PilzAdam
Neuromancer wrote:Was get_nodes_inside_radius ever added to Minetest? What is the best way to do this short of installing sapier's patch? Do we still have to loop through all the nodes?

https://github.com/celeron55/minetest/blob/master/doc/lua_api.txt#L1024

PostPosted: Sun Aug 26, 2012 01:02
by Neuromancer
Thanks Pilz. I'm using this to find places to create a waterfall sound. To do it in version .13 I just used the brute force lua for loops method.

I'm thinking I'm going to need find_nodes_in_area because if there is just 1 flowing water node it doesn't make sense to have a small waterfall sound. So I will look for at least 10 default:water_flowing nodes in the area. How wide out do you think I can go (min & max pos) without affecting performance? +/- 10 for all positions x,y, & z? 15, 20?