rand on new player, rename original random_spawn to spawnrand

* added minetest.register_on_joinplayer callback to randomized new player
* renaming to plan to use two kind of spawn mods.. this is for randown
* rename random_spawn to spawnrand
* reduce time wait to .1 second if no ground valid
* reduce time to search from 30 times cicle to 20
master
root 2021-10-15 23:38:24 -04:00
parent 08e113f867
commit 9c23c69a01
1 changed files with 22 additions and 14 deletions

View File

@ -1,15 +1,15 @@
random_spawn = {}
spawnrand = {}
function random_spawn(player)
function spawnrand(player)
local elevation = 15
local radius = 1000
local radius = 500
local posx = math.random(-radius, radius)
local posz = math.random(-radius, radius)
local new_spawn = {x = -174 + posx, y = elevation, z = 178 + posz}
local node = minetest.get_node_or_nil(new_spawn)
if not node or node.name == 'ignore' then
minetest.emerge_area({x = new_spawn.x, y = new_spawn.y+30, z = new_spawn.z}, {x = new_spawn.x, y = new_spawn.y-30, z = new_spawn.z})
minetest.after(.5, find_ground, new_spawn, player)
minetest.after(.1, find_ground, new_spawn, player)
else
find_ground(new_spawn, player)
end
@ -23,13 +23,13 @@ function find_ground(pos, player)
repeat
local node = minetest.get_node({x = pos.x, y = pos.y - i, z = pos.z})
i = i-1
if i == -30 then
random_spawn(player)
if i == -20 then
spawnrand(player)
end
if node.name ~= 'air' and node.name ~= 'ignore' then
local protection = minetest.is_protected({x = pos.x, y = pos.y - i + 1, z = pos.z}, player)
if protection then
random_spawn(player)
spawnrand(player)
else
player:setpos({x = pos.x, y = pos.y - i + 2, z = pos.z})
@ -37,30 +37,31 @@ function find_ground(pos, player)
finished = true
end
end
until finished == true or i < -30
until finished == true or i < -20
elseif node.name ~= 'air' or node.name ~= 'ignore' then --Theoretically below ground
local i = 1
repeat
local node = minetest.get_node({x = pos.x, y = pos.y + i, z = pos.z})
i = i + 1
if i == 30 then
random_spawn(player)
if i == 20 then
spawnrand(player)
end
if node.name == 'air' then
local protection = minetest.is_protected({x = pos.x, y = pos.y + i, z = pos.z}, player)
if protection then
random_spawn(player)
spawnrand(player)
else
player:setpos({x = pos.x, y = pos.y + i, z = pos.z})
minetest.set_node({x = pos.x, y = pos.y + i -1, z = pos.z}, {name = 'default:torch', param2 = 1})
i = 35
i = 25
end
end
until node.name == 'air' or i >= 30
end
end
minetest.register_node('random_spawn:node', {
-- api and usage in game
minetest.register_node('spawnrand:node', {
description = 'tests the spawn function',
inventory_image = 'default_gold_lump.png',
tiles = {'default_gold_lump.png'},
@ -68,6 +69,13 @@ minetest.register_node('random_spawn:node', {
paramtype = 'light',
light_source = 14,
on_punch = function(pos, node, player)
random_spawn(player)
spawnrand(player)
end
})
-- newspam in new player join
minetest.register_on_newplayer(function(player)
spawnrand(player)
end)