aboutsummaryrefslogtreecommitdiff
path: root/advtrains
diff options
context:
space:
mode:
Diffstat (limited to 'advtrains')
-rw-r--r--advtrains/advtrains/init.lua14
-rw-r--r--advtrains/advtrains/nodedb.lua57
2 files changed, 55 insertions, 16 deletions
diff --git a/advtrains/advtrains/init.lua b/advtrains/advtrains/init.lua
index 3fc16ed..2bdf39c 100644
--- a/advtrains/advtrains/init.lua
+++ b/advtrains/advtrains/init.lua
@@ -14,9 +14,11 @@ local no_action=false
function advtrains.pcall(fun)
if no_action then return end
- local succ, err, return1, return2, return3, return4=pcall(fun)
+ local succ, return1, return2, return3, return4=xpcall(fun, function(err)
+ atwarn("Lua Error occured: ", err)
+ atwarn(debug.traceback())
+ end)
if not succ then
- atwarn("Lua Error occured: ", err)
atwarn("Restoring saved state in 1 second...")
no_action=true
--read last save state and continue, as if server was restarted
@@ -25,9 +27,13 @@ function advtrains.pcall(fun)
le.object:remove()
end
end
- minetest.after(1, advtrains.load)
+ minetest.after(1, function()
+ advtrains.load()
+ atwarn("Reload successful!")
+ advtrains.ndb.restore_all()
+ end)
else
- return err, return1, return2, return3, return4
+ return return1, return2, return3, return4
end
end
diff --git a/advtrains/advtrains/nodedb.lua b/advtrains/advtrains/nodedb.lua
index 55adea8..5a193bd 100644
--- a/advtrains/advtrains/nodedb.lua
+++ b/advtrains/advtrains/nodedb.lua
@@ -197,13 +197,16 @@ function advtrains.get_rail_info_at(pos, drives_on)
return true, conn1, conn2, rely1, rely2, railheight
end
-
-minetest.register_abm({
- name = "advtrains:nodedb_on_load_update",
- nodenames = {"group:save_in_nodedb"},
- run_at_every_load = true,
- action = function(pos, node)
+ndb.run_lbm = function(pos, nodep)
return advtrains.pcall(function()
+ local node=nodep
+ if not node then
+ node=minetest.get_node_or_nil(pos)
+ if not node then
+ --this is defintely not loaded
+ return nil
+ end
+ end
local cid=ndbget(pos.x, pos.y, pos.z)
if cid then
--if in database, detect changes and apply.
@@ -215,11 +218,17 @@ minetest.register_abm({
ndb.update(pos, node)
else
if (nodeid~=node.name or param2~=node.param2) then
- atprint("nodedb: lbm replaced", pos, "with nodeid", nodeid, "param2", param2, "cid is", cid)
- minetest.swap_node(pos, {name=nodeid, param2 = param2})
- local ndef=minetest.registered_nodes[nodeid]
- if ndef and ndef.on_updated_from_nodedb then
- ndef.on_updated_from_nodedb(pos, node)
+ local ori_ndef=minetest.registered_nodes[node.name]
+ if ori_ndef and ori_ndef.groups.save_in_nodedb then --check if this node has been worldedited, and don't replace then
+ atprint("nodedb: lbm replaced", pos, "with nodeid", nodeid, "param2", param2, "cid is", cid)
+ minetest.swap_node(pos, {name=nodeid, param2 = param2})
+ local ndef=minetest.registered_nodes[nodeid]
+ if ndef and ndef.on_updated_from_nodedb then
+ ndef.on_updated_from_nodedb(pos, node)
+ end
+ return true
+ else
+ ndb.clear(pos)
end
end
end
@@ -228,11 +237,35 @@ minetest.register_abm({
atprint("nodedb: ", pos, "not in database")
ndb.update(pos, node)
end
+ return false
end)
- end,
+ end
+
+
+minetest.register_abm({
+ name = "advtrains:nodedb_on_load_update",
+ nodenames = {"group:save_in_nodedb"},
+ run_at_every_load = true,
+ action = ndb.run_lbm,
interval=10,
chance=1,
})
+
+--used when restoring stuff after a crash
+ndb.restore_all = function()
+ atwarn("Updating the map from the nodedb, this may take a while")
+ local cnt=0
+ for y, ny in pairs(ndb_nodes) do
+ for x, nx in pairs(ny) do
+ for z, _ in pairs(nx) do
+ if ndb.run_lbm({x=x, y=y, z=z}) then
+ cnt=cnt+1
+ end
+ end
+ end
+ end
+ atwarn("Updated",cnt,"nodes")
+end
minetest.register_on_dignode(function(pos, oldnode, digger)
return advtrains.pcall(function()