Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawn level: Add 'get_spawn_level(x, z)' API #7104

Closed
wants to merge 1 commit into from

Conversation

Projects
None yet
5 participants
@paramat
Copy link
Member

commented Mar 7, 2018

Spawn level: Add 'get_spawn_level(x, z)' API

Returns a suitable player spawn y co-ordinate for unmodified terrain.
///////////////////

This returns the value calculated by the existing getSpawnLevelAtPoint() functions in each mapgen that are used to get the spawn y co-ordinate for players for a particular (x, z).
Allows mods to know where to place a player when teleporting to ungenerated terrain.
Allows customised spawning behaviour.

"Returns a player spawn y co-ordinate for the provided (x, z) co-ordinates,
or nil for an unsuitable spawn point.
For most mapgens a 'suitable spawn point' is one with y between
water_level and water_level + 16, and in mgv7 well away from rivers,
so nil will be returned for many (x, z) co-ordinates.
The spawn level returned is for a player spawn in unmodified terrain.
The spawn level is intentionally above terrain level to cope with full-node
biome 'dust' nodes."

Tested with this mod:

local timer = 0

	minetest.register_globalstep(function(dtime)
		timer = timer + dtime
		if timer > 2 then
			timer = 0
			for _, player in ipairs(minetest.get_connected_players()) do
				local ppos = player:getpos()
				ppos.x = math.floor(ppos.x)
				ppos.z = math.floor(ppos.z)
				local spawn_y = minetest.get_spawn_level(ppos.x, ppos.z)
				print(spawn_y)
				minetest.set_node({x = ppos.x, y = spawn_y, z = ppos.z},
					{name = "default:stone"})
			end
		end
	end)

Enable fly and fly slightly above the terrain to avoid being inside the placed stone, the stone will appear 2 nodes above ground level, this matches the level the engine calculates and is intentional to cope with full-node biome dust nodes so that a player does not spawn waist deep in a snowblock.

@paramat paramat force-pushed the paramat:spawnlevelapi branch from 4f2f78d to 7dcf10b Mar 8, 2018

@paramat paramat removed the WIP label Mar 8, 2018

@paramat

This comment has been minimized.

Copy link
Member Author

commented Mar 8, 2018

Tested, added docs, ready for review.

@Megaf

This comment has been minimized.

Copy link
Contributor

commented Mar 8, 2018

Nice feature to have. I can see several mods making good use of this.
@paramat Maybe you could make a forum post briefly talking about this once it's merged?

@rubenwardy

This comment has been minimized.

Copy link
Member

commented Mar 8, 2018

get_mapgen_height() is probably a better name

{
NO_MAP_LOCK_REQUIRED;

int x = luaL_checkinteger(L, 1);

This comment has been minimized.

Copy link
@nerzhul

nerzhul Mar 8, 2018

Member

it's s16 not int

This comment has been minimized.

Copy link
@SmallJoker

SmallJoker Mar 8, 2018

Member

Does not matter due implicit conversion in either way.

This comment has been minimized.

Copy link
@paramat

paramat Mar 8, 2018

Author Member

I was copying common practice in APIs, don't mind either way, will use s16.

This comment has been minimized.

Copy link
@paramat

paramat Mar 9, 2018

Author Member

Yes much better as it matches the argument for the function, thanks.

This comment has been minimized.

Copy link
@paramat

paramat Mar 9, 2018

Author Member

Done.


int x = luaL_checkinteger(L, 1);
int z = luaL_checkinteger(L, 2);
v2s16 pos2d = v2s16(x, z);

This comment has been minimized.

Copy link
@nerzhul

nerzhul Mar 8, 2018

Member

unnecessary temporary variable

This comment has been minimized.

Copy link
@paramat

paramat Mar 8, 2018

Author Member

Ok will do, considered it, did this for clarity.

This comment has been minimized.

Copy link
@paramat

paramat Mar 9, 2018

Author Member

Sorry i see what you mean now, totally agree =)

This comment has been minimized.

Copy link
@paramat

paramat Mar 9, 2018

Author Member

Done.

@paramat

This comment has been minimized.

Copy link
Member Author

commented Mar 8, 2018

rubenwardy, 'mapgen height' is unsuitable because this returns the value calculated by 'getSpawnLevelAtPoint()' in each mapgen. These functions return a spawn level that is slightly above the terrain to cope with full-node biome dust nodes, and they only return a spawn level if the level is within a certain range. So the value is very much to do with spawning not terrain height.

@paramat paramat added the WIP label Mar 8, 2018

@rubenwardy

This comment has been minimized.

Copy link
Member

commented Mar 8, 2018

Ah might be worth noting that it could be a bit above the surface:

Returned heights are not necessary on the surface, but may be a few nodes above

@paramat

This comment has been minimized.

Copy link
Member Author

commented Mar 8, 2018

Yes ok.

@paramat paramat force-pushed the paramat:spawnlevelapi branch from 7dcf10b to 7786a83 Mar 9, 2018

@paramat

This comment has been minimized.

Copy link
Member Author

commented Mar 9, 2018

Updated and retested.

@paramat paramat removed the WIP label Mar 9, 2018

Spawn level: Add 'get_spawn_level(x, z)' API
Returns a suitable player spawn y co-ordinate for unmodified terrain.

@paramat paramat force-pushed the paramat:spawnlevelapi branch from 7786a83 to cedfc01 Mar 9, 2018

Both done.

@paramat

This comment has been minimized.

Copy link
Member Author

commented Mar 10, 2018

@paramat paramat closed this Mar 10, 2018

@paramat paramat deleted the paramat:spawnlevelapi branch Mar 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.