2013-01-27 16:26:42 +13:00
|
|
|
spawning = {}
|
2013-01-30 15:28:57 +13:00
|
|
|
local registered_spawns = {}
|
|
|
|
local filepath = minetest.get_worldpath()..'/spawning'
|
|
|
|
|
|
|
|
--Load spawns
|
|
|
|
local input = io.open(filepath..".spawns", "r")
|
|
|
|
if input then
|
|
|
|
while true do
|
|
|
|
local nodename = input:read("*l")
|
|
|
|
if not nodename then break end
|
|
|
|
local parms = {}
|
|
|
|
--Catch config.
|
|
|
|
i, flags = nodename:match("^(%S*) (.*)")
|
|
|
|
repeat
|
|
|
|
v, p = flags:match("^(%S*) (.*)")
|
|
|
|
if p then
|
|
|
|
flags = p
|
|
|
|
end
|
|
|
|
if v then
|
|
|
|
table.insert(parms,v)
|
|
|
|
else
|
|
|
|
v = flags:match("^(%S*)")
|
|
|
|
table.insert(parms,v)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
until false
|
|
|
|
registered_spawns[i] = {
|
|
|
|
pos={x=parms[1],y=parms[2],z=parms[3]}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
io.close(input)
|
|
|
|
end
|
|
|
|
|
|
|
|
function spawning.save_spawns()
|
|
|
|
local output = io.open(filepath..".spawns", "w")
|
|
|
|
for i,v in pairs(registered_spawns) do
|
|
|
|
output:write(i.." "..v.pos.x.." "..v.pos.y.." "..v.pos.z.."\n")
|
|
|
|
end
|
|
|
|
io.close(output)
|
|
|
|
end
|
2013-01-27 16:26:42 +13:00
|
|
|
|
2013-01-31 13:33:06 +13:00
|
|
|
--Set spawn pos
|
2013-01-30 15:28:57 +13:00
|
|
|
function spawning.set_spawn(place, pos)
|
|
|
|
local spawn = registered_spawns[place]
|
2013-01-31 13:33:06 +13:00
|
|
|
if not spawn then spawning.register_spawn(place, {}) end
|
|
|
|
|
2013-01-30 15:28:57 +13:00
|
|
|
registered_spawns[place].pos = pos
|
|
|
|
|
|
|
|
--Save spawns.
|
|
|
|
spawning.save_spawns()
|
|
|
|
end
|
|
|
|
|
2015-06-23 00:45:39 +02:00
|
|
|
--Remove spawn pos
|
|
|
|
function spawning.unset_spawn(place)
|
|
|
|
registered_spawns[place] = nil
|
|
|
|
spawning.save_spawns()
|
|
|
|
end
|
|
|
|
|
2013-01-31 13:33:06 +13:00
|
|
|
function spawning.is_spawn(place)
|
|
|
|
local spawn = registered_spawns[place]
|
|
|
|
if not spawn then return false else return true end
|
|
|
|
end
|
|
|
|
|
2013-01-30 15:28:57 +13:00
|
|
|
function spawning.spawn(player, place)
|
2013-01-31 20:41:15 +13:00
|
|
|
if type(player) == "table" then place = player[2] player = player[1] end
|
2013-01-30 15:28:57 +13:00
|
|
|
local spawn = registered_spawns[place]
|
2013-05-29 20:53:20 +12:00
|
|
|
local pos = spawn.pos
|
2013-01-30 15:28:57 +13:00
|
|
|
if spawn then
|
2013-01-31 17:16:18 +13:00
|
|
|
player:setpos(pos)
|
2013-01-27 16:26:42 +13:00
|
|
|
end
|
2013-05-29 20:53:20 +12:00
|
|
|
for i,v in pairs(minetest.env:find_nodes_in_area({x=pos.x-20,y=pos.y-20,z=pos.z-20}, {x=pos.x+20,y=pos.y+20,z=pos.z+20}, "default:lava_source")) do
|
|
|
|
minetest.env:remove_node(v)
|
|
|
|
end
|
2013-01-27 16:26:42 +13:00
|
|
|
end
|
2013-01-30 15:28:57 +13:00
|
|
|
|
|
|
|
function spawning.register_spawn(name, spawndef)
|
|
|
|
local pos
|
|
|
|
--Save spawnpoint position if it is already assigned.
|
|
|
|
if registered_spawns[name] then pos = registered_spawns[name].pos end
|
|
|
|
|
|
|
|
-- Apply defaults and add to registered_* table.
|
|
|
|
setmetatable(spawndef, {__index = spawning.spawndef_default})
|
|
|
|
registered_spawns[name] = spawndef
|
|
|
|
|
|
|
|
--Restore position if it was already assigned.
|
|
|
|
if pos then registered_spawns[name].pos = pos end
|
|
|
|
|
|
|
|
--Save spawns.
|
|
|
|
spawning.save_spawns()
|
|
|
|
end
|
|
|
|
|
|
|
|
spawning.spawndef_default = {
|
|
|
|
pos = {x=0, y=0, z=0},
|
|
|
|
mode = "static",
|
|
|
|
}
|