areas/legacy.lua

148 lines
3.5 KiB
Lua
Raw Normal View History

2013-09-03 01:16:14 +02:00
-- This file contains functions to convert from
-- the old areas format and other compatability code.
minetest.register_chatcommand("legacy_load_areas", {
params = "<version>",
2013-09-03 07:33:08 +02:00
description = "Loads, converts, and saves the areas from"
.." a legacy save file.",
2014-07-13 01:10:26 +02:00
privs = {areas=true, server=true},
2013-09-03 01:16:14 +02:00
func = function(name, param)
minetest.chat_send_player(name, "Converting areas...")
local version = tonumber(param)
if version == 0 then
err = areas:node_ownership_load()
if err then
minetest.chat_send_player(name, "Error loading legacy file: "..err)
return
end
else
minetest.chat_send_player(name, "Invalid version number. (0 allowed)")
2013-09-03 01:16:14 +02:00
return
end
minetest.chat_send_player(name, "Legacy file loaded.")
for k, area in pairs(areas.areas) do
-- New position format
area.pos1 = {x=area.x1, y=area.y1, z=area.z1}
area.pos2 = {x=area.x2, y=area.y2, z=area.z2}
2013-09-03 01:16:14 +02:00
area.x1, area.y1, area.z1,
area.x2, area.y2, area.z2 =
2013-09-03 01:16:14 +02:00
nil, nil, nil, nil, nil, nil
-- Area positions sorting
2014-07-12 22:37:54 +02:00
areas:sortPos(area.pos1, area.pos2)
2013-09-03 01:16:14 +02:00
-- Add name
area.name = "unnamed"
-- Remove ID
area.id = nil
2013-09-03 01:16:14 +02:00
end
minetest.chat_send_player(name, "Table format updated.")
areas:save()
2013-09-03 07:33:08 +02:00
minetest.chat_send_player(name, "Converted areas saved. Done.")
2014-07-12 22:37:54 +02:00
end
})
2013-09-03 01:16:14 +02:00
function areas:node_ownership_load()
2013-09-03 01:16:14 +02:00
local filename = minetest.get_worldpath().."/owners.tbl"
tables, err = loadfile(filename)
if err then
return err
end
tables = tables()
for idx = 1, #tables do
local tolinkv, tolinki = {}, {}
for i, v in pairs(tables[idx]) do
if type(v) == "table" and tables[v[1]] then
table.insert(tolinkv, {i, tables[v[1]]})
end
if type(i) == "table" and tables[i[1]] then
table.insert(tolinki, {i, tables[i[1]]})
end
end
-- link values, first due to possible changes of indices
for _, v in ipairs(tolinkv) do
tables[idx][v[1]] = v[2]
end
-- link indices
for _, v in ipairs(tolinki) do
tables[idx][v[2]], tables[idx][v[1]] = tables[idx][v[1]], nil
end
end
self.areas = tables[1]
end
-- Returns the name of the first player that owns an area
function areas.getNodeOwnerName(pos)
for id, area in pairs(areas:getAreasAtPos(pos)) do
return area.owner
2013-09-03 01:16:14 +02:00
end
return false
end
-- Checks if a node is owned by you
function areas.isNodeOwner(pos, name)
2014-07-13 01:10:26 +02:00
if minetest.check_player_privs(name, areas.adminPrivs) then
2013-09-03 01:16:14 +02:00
return true
end
for id, area in pairs(areas:getAreasAtPos(pos)) do
if name == area.owner then
return true
2013-09-03 01:16:14 +02:00
end
end
return false
end
2013-11-15 00:26:57 +01:00
-- Checks if an area has an owner
function areas.hasOwner(pos)
for id, area in pairs(areas:getAreasAtPos(pos)) do
return true
2013-11-15 00:26:57 +01:00
end
return false
end
2013-09-03 01:16:14 +02:00
IsPlayerNodeOwner = areas.isNodeOwner
GetNodeOwnerName = areas.getNodeOwnerName
HasOwner = areas.hasOwner
-- This is entirely untested and may break in strange and new ways.
2013-09-03 01:16:14 +02:00
if areas.legacy_table then
owner_defs = {}
setmetatable(owner_defs, {
__index = function(table, key)
local a = rawget(areas.areas, key)
if a then
a.x1 = a.pos1.x
a.y1 = a.pos1.y
a.z1 = a.pos1.z
a.x2 = a.pos2.x
a.y2 = a.pos2.y
a.z2 = a.pos2.z
a.pos1, a.pos2 = nil, nil
a.id = key
2013-09-03 01:16:14 +02:00
end
return a
end,
__newindex = function(table, key, value)
2013-12-16 23:18:15 +01:00
local a = value
a.pos1, a.pos2 = {}, {}
a.pos1.x = a.x1
a.pos1.y = a.y1
a.pos1.z = a.z1
a.pos2.x = a.x2
a.pos2.y = a.y2
a.pos2.z = a.z2
a.x1, a.y1, a.z1, a.x2, a.y2, a.z2 =
nil, nil, nil, nil, nil, nil
a.name = a.name or "unnamed"
a.id = nil
return rawset(areas.areas, key, a)
2013-09-03 01:16:14 +02:00
end
})
end