## How to get ground level (pos.y) at x and z

LC Creations
Member
Posts: 83
Joined: Mon Feb 18, 2019 02:53

### How to get ground level (pos.y) at x and z

Let's say, there is a player in a cave at pos 0,-327,0. But above me, is a very high moutian. I want to know how I can find the ground level directly above where the player stands inside that cave.

DrFrankenstone
Member
Posts: 157
Joined: Tue May 24, 2016 05:36
GitHub: treer
Location: Melbourne, Australia
Contact:

### Re: How to get ground level (pos.y) at x and z

You've posted variants of this question about 3 times now, adding more comments or specifics to a single post would be better.

Unless such a feature has been recently added, I'm not aware of any good ways, but there are ways...

First use minetest.get_spawn_level(x, z) as it's fast and works on all maps. Unfortunately this will return nil if (x,z) is water or higher that 16, or for various other reasons, so when it returns nil you have to fall back on a second method.

The second method can be either
• Use minetest.find_nodes_in_area_under_air() to search the landscape for a node with air above - this is slow and caves/overhangs may cause it to return the wrong answer. Results can be corrected for by using minetest.get_node_light to check the air has a midday light value of 15 - daylight / sky above.
• look up which mapgen+seed the world is using then calculate the height using the same algorithm the mapgen used. This is complicated and needs an implementation for each mapgen, but Pyrollo has done a lot of heavy lifting here and created a mgutils library as part of the grunds mod. It provides mgutils.get_level_at_point() and looks like it supports v7, v6, and Valleys. This method will not take into account whether players have modified the landscape.
Depending on your speed/accuracy/height requirements, you might skip get_spawn_level(), as the height values it returns can differ compared to the second method by a node or two, depending on the mapgen in use.

### Who is online

Users browsing this forum: No registered users and 1 guest