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