-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.lua
73 lines (69 loc) · 2.63 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
random_spawn = {}
function random_spawn(player)
local elevation = 15
local radius = 1000
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)
else
find_ground(new_spawn, player)
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 == -30 then
random_spawn(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)
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
end
end
until finished == true or i < -30
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)
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)
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
end
end
until node.name == 'air' or i >= 30
end
end
minetest.register_node('random_spawn:node', {
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)
random_spawn(player)
end
})