minetest-mod-spawnrand/init.lua

97 lines
3.3 KiB
Lua
Raw Normal View History

spawnrand = {}
2017-05-27 10:13:44 -07:00
function spawnrand(player)
2017-05-27 11:00:27 -07:00
local elevation = 15
local radius = 500
2017-05-27 10:13:44 -07:00
local posx = math.random(-radius, radius)
local posz = math.random(-radius, radius)
2017-05-27 11:47:01 -07:00
local new_spawn = {x = -174 + posx, y = elevation, z = 178 + posz}
2017-05-27 10:13:44 -07:00
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(.1, find_ground, new_spawn, player)
2017-05-27 11:00:27 -07:00
else
find_ground(new_spawn, player)
2017-05-27 10:13:44 -07:00
end
end
function find_ground(pos, player)
local node = minetest.get_node({x = pos.x, y = pos.y, z = pos.z})
if node.name == 'air' or node.name == 'ignore' then --Theoretically above ground
local i = 1
local finished = false
repeat
local node = minetest.get_node({x = pos.x, y = pos.y - i, z = pos.z})
i = i-1
if i == -20 then
spawnrand(player)
2017-05-27 10:13:44 -07:00
end
if node.name ~= 'air' and node.name ~= 'ignore' then
2017-05-27 12:57:56 -07:00
local protection = minetest.is_protected({x = pos.x, y = pos.y - i + 1, z = pos.z}, player)
2017-05-27 11:00:27 -07:00
if protection then
spawnrand(player)
2017-05-27 12:57:56 -07:00
2017-05-27 11:47:01 -07:00
else
player:setpos({x = pos.x, y = pos.y - i + 2, z = pos.z})
minetest.set_node({x = pos.x, y = pos.y + i +1, z = pos.z}, {name = 'default:torch', param2 = 1})
finished = true
2017-05-27 11:00:27 -07:00
end
2017-05-27 10:13:44 -07:00
end
until finished == true or i < -20
2017-05-27 10:13:44 -07:00
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 == 20 then
spawnrand(player)
2017-05-27 10:13:44 -07:00
end
if node.name == 'air' then
2017-05-27 11:47:01 -07:00
local protection = minetest.is_protected({x = pos.x, y = pos.y + i, z = pos.z}, player)
2017-05-27 11:00:27 -07:00
if protection then
spawnrand(player)
2017-05-27 11:47:01 -07:00
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 = 25
2017-05-27 11:00:27 -07:00
end
2017-05-27 10:13:44 -07:00
end
2017-05-27 12:57:56 -07:00
until node.name == 'air' or i >= 30
2017-05-27 10:13:44 -07:00
end
end
-- api and usage in game
minetest.register_node('spawnrand:node', {
2017-05-27 10:13:44 -07:00
description = 'tests the spawn function',
inventory_image = 'default_gold_lump.png',
tiles = {'default_gold_lump.png'},
groups = {oddly_breakable_by_hand = 1},
paramtype = 'light',
light_source = 14,
on_punch = function(pos, node, player)
spawnrand(player)
2017-05-27 10:13:44 -07:00
end
})
-- newspam in new player join
minetest.register_on_newplayer(function(player)
spawnrand(player)
end)
-- newspam in payer dead, but doe snot remain with same position.. take care of bed but not of home yet
minetest.register_on_respawnplayer(function(player)
local name = player:get_player_name()
if beds.spawn then
local pos = beds.spawn[name]
if pos then
minetest.log("action", name.." already has bed, so no spawnrand, respawns at "..minetest.pos_to_string(pos))
player:setpos(pos)
return true
end
else
minetest.log("action", name.." does not has bed, again i will send you far away to you last position")
spawnrand(player)
end
end)