Deprecate old 4590 crossings

This is done with a task that runs once when the nodedb is loaded. A new
field of advtrains_ndb will keep a version integer to indicate migration
versions. This introduces verson 1, no version being equivalent to 0.
An LBM is also registered to replace the tracks in the world.
This commit is contained in:
Blockhead 2020-08-16 06:05:11 +10:00 committed by Gabriel Pérez-Cerezo
parent b707f20ba2
commit 595f26b450
2 changed files with 35 additions and 1 deletions

View File

@ -30,6 +30,7 @@ local ndb={}
--local variables for performance
local ndb_nodeids={}
local ndb_nodes={}
local ndb_ver
local function ndbget(x,y,z)
local ny=ndb_nodes[y]
@ -57,6 +58,16 @@ local path=minetest.get_worldpath()..DIR_DELIM.."advtrains_ndb2"
--nodeids get loaded by advtrains init.lua and passed here
function ndb.load_data(data)
ndb_nodeids = data and data.nodeids or {}
ndb_ver = data and data.ver or 0
if ndb_ver < 1 then
for k,v in pairs(ndb_nodeids) do
if v == "advtrains:dtrack_xing4590_st" then
cidDepr = k
elseif v == "advtrains:dtrack_xing90plusx_45l" then
cidNew = k
end
end
end
local file, err = io.open(path, "rb")
if not file then
atwarn("Couldn't load the node database: ", err or "Unknown Error")
@ -67,6 +78,9 @@ function ndb.load_data(data)
local hst_x=file:read(2)
local cid=file:read(2)
while hst_z and hst_y and hst_x and cid and #hst_z==2 and #hst_y==2 and #hst_x==2 and #cid==2 do
if (ndb_ver < 1 and cid == cidDepr) then
cid = cidNew
end
ndbset(bytes_to_int(hst_x), bytes_to_int(hst_y), bytes_to_int(hst_z), bytes_to_int(cid))
cnt=cnt+1
hst_z=file:read(2)
@ -77,6 +91,7 @@ function ndb.load_data(data)
atlog("nodedb: read", cnt, "nodes.")
file:close()
end
ndb_ver = 1
end
--save
@ -99,7 +114,7 @@ function ndb.save_data()
file:close()
end
os.rename(tmppath, path)
return {nodeids = ndb_nodeids}
return {nodeids = ndb_nodeids, ver = ndb_ver}
end
--function to get node. track database is not helpful here.

View File

@ -62,6 +62,25 @@ minetest.register_craft({
{'', '', 'advtrains:dtrack_placer'}
}
})
-- Deprecate any rails using the old name scheme
minetest.register_lbm({
label = "Upgrade legacy 4590 rails",
name = "advtrains_train_track:replace_legacy_4590",
nodenames = {"advtrains:dtrack_xing4590_st"},
run_at_every_load = true,
action = function(pos, node)
minetest.log("actionPos!: " .. pos.x .. "," .. pos.y .. "," .. pos.z)
minetest.log("node!: " .. node.name .. "," .. node.param1 .. "," .. node.param2)
advtrains.ndb.swap_node(pos,
{
name="advtrains:dtrack_xing90plusx_45l",
param1=node.param1,
param2=node.param2,
})
end
})
-- This will replace any items left in the inventory
minetest.register_alias("advtrains:dtrack_xing4590_placer", "advtrains:dtrack_xing90plusx_placer")
-- Diagonal
-- This set of rail crossings is named based on the angle of each intersecting