Remove pcall wrappers completely, add command to disable advtrains mainloop

master
orwell96 2020-04-27 16:02:28 +02:00
parent b59b0d587d
commit 5c2534cc35
8 changed files with 145 additions and 178 deletions

View File

@ -108,53 +108,49 @@ advtrains.atc_function = function(def, preset, suffix, rotation)
return {
after_place_node=apn_func,
after_dig_node=function(pos)
return advtrains.pcall(function()
advtrains.invalidate_all_paths(pos)
advtrains.ndb.clear(pos)
local pts=minetest.pos_to_string(pos)
atc.controllers[pts]=nil
end)
advtrains.invalidate_all_paths(pos)
advtrains.ndb.clear(pos)
local pts=minetest.pos_to_string(pos)
atc.controllers[pts]=nil
end,
on_receive_fields = function(pos, formname, fields, player)
return advtrains.pcall(function()
if advtrains.is_protected(pos, player:get_player_name()) then
minetest.record_protection_violation(pos, player:get_player_name())
if advtrains.is_protected(pos, player:get_player_name()) then
minetest.record_protection_violation(pos, player:get_player_name())
return
end
local meta=minetest.get_meta(pos)
if meta then
if not fields.save then
--[[--maybe only the dropdown changed
if fields.mode then
meta:set_string("mode", idxtrans[fields.mode])
if fields.mode=="digiline" then
meta:set_string("infotext", attrans("ATC controller, mode @1\nChannel: @2", fields.mode, meta:get_string("command")) )
else
meta:set_string("infotext", attrans("ATC controller, mode @1\nCommand: @2", fields.mode, meta:get_string("command")) )
end
meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
end]]--
return
end
local meta=minetest.get_meta(pos)
if meta then
if not fields.save then
--maybe only the dropdown changed
if fields.mode then
meta:set_string("mode", idxtrans[fields.mode])
if fields.mode=="digiline" then
meta:set_string("infotext", attrans("ATC controller, mode @1\nChannel: @2", (fields.mode or "?"), meta:get_string("command")) )
else
meta:set_string("infotext", attrans("ATC controller, mode @1\nCommand: @2", (fields.mode or "?"), meta:get_string("command")) )
end
meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
end
return
end
meta:set_string("mode", idxtrans[fields.mode])
meta:set_string("command", fields.command)
meta:set_string("command_on", fields.command_on)
meta:set_string("channel", fields.channel)
if fields.mode=="digiline" then
meta:set_string("infotext", attrans("ATC controller, mode @1\nChannel: @2", (fields.mode or "?"), meta:get_string("command")) )
else
meta:set_string("infotext", attrans("ATC controller, mode @1\nCommand: @2", (fields.mode or "?"), meta:get_string("command")) )
end
meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
local pts=minetest.pos_to_string(pos)
local _, conns=advtrains.get_rail_info_at(pos, advtrains.all_tracktypes)
atc.controllers[pts]={command=fields.command}
if #advtrains.occ.get_trains_at(pos) > 0 then
atc.send_command(pos)
end
--meta:set_string("mode", idxtrans[fields.mode])
meta:set_string("command", fields.command)
--meta:set_string("command_on", fields.command_on)
meta:set_string("channel", fields.channel)
--if fields.mode=="digiline" then
-- meta:set_string("infotext", attrans("ATC controller, mode @1\nChannel: @2", fields.mode, meta:get_string("command")) )
--else
meta:set_string("infotext", attrans("ATC controller, mode @1\nCommand: @2", "-", meta:get_string("command")) )
--end
meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
local pts=minetest.pos_to_string(pos)
local _, conns=advtrains.get_rail_info_at(pos, advtrains.all_tracktypes)
atc.controllers[pts]={command=fields.command}
if #advtrains.occ.get_trains_at(pos) > 0 then
atc.send_command(pos)
end
end)
end
end,
advtrains = {
on_train_enter = function(pos, train_id)

View File

@ -13,76 +13,74 @@ minetest.register_tool("advtrains:copytool", {
-- The front of the train is used as the start of the new train and it proceeds backwards from
-- the direction of travel.
on_place = function(itemstack, placer, pointed_thing)
return advtrains.pcall(function()
if ((not pointed_thing.type == "node") or (not placer.get_player_name)) then
return
end
local pname = placer:get_player_name()
local node=minetest.get_node_or_nil(pointed_thing.under)
if not node then atprint("[advtrains]Ignore at placer position") return itemstack end
local nodename=node.name
if(not advtrains.is_track_and_drives_on(nodename, {default=true})) then
atprint("no track here, not placing.")
return itemstack
end
if not minetest.check_player_privs(placer, {train_operator = true }) then
minetest.chat_send_player(pname, "You don't have the train_operator privilege.")
return itemstack
end
if not minetest.check_player_privs(placer, {train_admin = true }) and minetest.is_protected(pointed_thing.under, placer:get_player_name()) then
return itemstack
end
local tconns=advtrains.get_track_connections(node.name, node.param2)
local yaw = placer:get_look_horizontal()
local plconnid = advtrains.yawToClosestConn(yaw, tconns)
local prevpos = advtrains.get_adjacent_rail(pointed_thing.under, tconns, plconnid, {default=true})
if not prevpos then
minetest.chat_send_player(pname, "The track you are trying to place the wagon on is not long enough!")
return
end
local meta = itemstack:get_meta()
if not meta then
minetest.chat_send_player(pname, attrans("The clipboard couldn't access the metadata. Paste failed."))
if ((not pointed_thing.type == "node") or (not placer.get_player_name)) then
return
end
local clipboard = meta:get_string("clipboard")
if (clipboard == "") then
minetest.chat_send_player(pname, "The clipboard is empty.");
return
end
clipboard = minetest.deserialize(clipboard)
if (clipboard.wagons == nil) then
minetest.chat_send_player(pname, "The clipboard is empty.");
return
end
end
local pname = placer:get_player_name()
local wagons = {}
local n = 1
for _, wagonProto in pairs(clipboard.wagons) do
local wagon = advtrains.create_wagon(wagonProto.type, pname)
advtrains.wagons[wagon].wagon_flipped = wagonProto.wagon_flipped
wagons[n] = wagon
n = n + 1
end
local id=advtrains.create_new_train_at(pointed_thing.under, plconnid, 0, wagons)
local train = advtrains.trains[id]
train.off_track = train.end_index<train.path_trk_b
if (train.off_track) then
minetest.chat_send_player(pname, "Back of train would end up off track, cancelling.")
advtrains.remove_train(id)
return
end
train.text_outside = clipboard.text_outside
train.text_inside = clipboard.text_inside
train.routingcode = clipboard.routingcode
train.line = clipboard.line
local node=minetest.get_node_or_nil(pointed_thing.under)
if not node then atprint("[advtrains]Ignore at placer position") return itemstack end
local nodename=node.name
if(not advtrains.is_track_and_drives_on(nodename, {default=true})) then
atprint("no track here, not placing.")
return itemstack
end
if not minetest.check_player_privs(placer, {train_operator = true }) then
minetest.chat_send_player(pname, "You don't have the train_operator privilege.")
return itemstack
end
if not minetest.check_player_privs(placer, {train_admin = true }) and minetest.is_protected(pointed_thing.under, placer:get_player_name()) then
return itemstack
end
local tconns=advtrains.get_track_connections(node.name, node.param2)
local yaw = placer:get_look_horizontal()
local plconnid = advtrains.yawToClosestConn(yaw, tconns)
local prevpos = advtrains.get_adjacent_rail(pointed_thing.under, tconns, plconnid, {default=true})
if not prevpos then
minetest.chat_send_player(pname, "The track you are trying to place the wagon on is not long enough!")
return
end)
end
local meta = itemstack:get_meta()
if not meta then
minetest.chat_send_player(pname, attrans("The clipboard couldn't access the metadata. Paste failed."))
return
end
local clipboard = meta:get_string("clipboard")
if (clipboard == "") then
minetest.chat_send_player(pname, "The clipboard is empty.");
return
end
clipboard = minetest.deserialize(clipboard)
if (clipboard.wagons == nil) then
minetest.chat_send_player(pname, "The clipboard is empty.");
return
end
local wagons = {}
local n = 1
for _, wagonProto in pairs(clipboard.wagons) do
local wagon = advtrains.create_wagon(wagonProto.type, pname)
advtrains.wagons[wagon].wagon_flipped = wagonProto.wagon_flipped
wagons[n] = wagon
n = n + 1
end
local id=advtrains.create_new_train_at(pointed_thing.under, plconnid, 0, wagons)
local train = advtrains.trains[id]
train.off_track = train.end_index<train.path_trk_b
if (train.off_track) then
minetest.chat_send_player(pname, "Back of train would end up off track, cancelling.")
advtrains.remove_train(id)
return
end
train.text_outside = clipboard.text_outside
train.text_inside = clipboard.text_inside
train.routingcode = clipboard.routingcode
train.line = clipboard.line
return
end,
-- Copy: Take the pointed-at train and put it on the clipboard
on_use = function(itemstack, user, pointed_thing)
@ -182,4 +180,4 @@ minetest.register_tool("advtrains:copytool", {
minetest.chat_send_player(user:get_player_name(), attrans("Train copied!"))
return itemstack
end
})
})

View File

@ -32,7 +32,6 @@ minetest.register_entity("advtrains:discouple", {
end,
get_staticdata=function() return "DISCOUPLE" end,
on_punch=function(self, player)
return advtrains.pcall(function()
local pname = player:get_player_name()
if pname and pname~="" and self.wagon then
if advtrains.safe_decouple_wagon(self.wagon.id, pname) then
@ -41,10 +40,8 @@ minetest.register_entity("advtrains:discouple", {
minetest.add_entity(self.object:getpos(), "advtrains:lockmarker")
end
end
end)
end,
on_step=function(self, dtime)
return advtrains.pcall(function()
if not self.wagon then
self.object:remove()
return
@ -58,7 +55,6 @@ minetest.register_entity("advtrains:discouple", {
self.object:remove()
return
end
end)
end,
})
@ -79,7 +75,6 @@ minetest.register_entity("advtrains:couple", {
is_couple=true,
static_save = false,
on_activate=function(self, staticdata)
return advtrains.pcall(function()
if staticdata=="COUPLE" then
--couple entities have no right to exist further...
atprint("Couple loaded from staticdata, destroying")
@ -87,11 +82,9 @@ minetest.register_entity("advtrains:couple", {
return
end
self.object:set_armor_groups({immmortal=1})
end)
end,
get_staticdata=function(self) return "COUPLE" end,
on_rightclick=function(self, clicker)
return advtrains.pcall(function()
if not self.train_id_1 or not self.train_id_2 then return end
local pname=clicker
@ -102,15 +95,12 @@ minetest.register_entity("advtrains:couple", {
else
lockmarker(self.object)
end
end)
end,
on_step=function(self, dtime)
return advtrains.pcall(function()
if advtrains.wagon_outside_range(self.object:getpos()) then
self.object:remove()
return
end
advtrains.atprint_context_tid=self.train_id_1
if not self.train_id_1 or not self.train_id_2 then atprint("Couple: train ids not set!") self.object:remove() return end
local train1=advtrains.trains[self.train_id_1]
@ -161,8 +151,6 @@ minetest.register_entity("advtrains:couple", {
end
atprintbm("couple step", t)
advtrains.atprint_context_tid=nil
end)
end,
})
minetest.register_entity("advtrains:lockmarker", {
@ -175,7 +163,6 @@ minetest.register_entity("advtrains:lockmarker", {
is_lockmarker=true,
static_save = false,
on_activate=function(self, staticdata)
return advtrains.pcall(function()
if staticdata=="COUPLE" then
--couple entities have no right to exist further...
atprint("Couple loaded from staticdata, destroying")
@ -184,7 +171,6 @@ minetest.register_entity("advtrains:lockmarker", {
end
self.object:set_armor_groups({immmortal=1})
self.life=5
end)
end,
get_staticdata=function(self) return "COUPLE" end,
on_step=function(self, dtime)

View File

@ -61,26 +61,6 @@ local function reload_saves()
end)
end
function advtrains.pcall(fun)
if no_action then return end
local succ, return1, return2, return3, return4=xpcall(fun, function(err)
atwarn("Lua Error occured: ", err)
atwarn(debug.traceback())
if advtrains.atprint_context_tid then
advtrains.path_print(advtrains.trains[advtrains.atprint_context_tid], atdebug)
atwarn(advtrains.trains[advtrains.atprint_context_tid].debug)
end
end)
if not succ then
error("pcall")
reload_saves()
else
return return1, return2, return3, return4
end
end
advtrains.modpath = minetest.get_modpath("advtrains")
--Advtrains dump (special treatment of pos and sigd)
@ -569,7 +549,11 @@ advtrains.mainloop_runcnt=0
local t = 0
minetest.register_globalstep(function(dtime_mt)
return advtrains.pcall(function()
if no_action then
-- the advtrains globalstep is skipped by command. Return immediately
return
end
advtrains.mainloop_runcnt=advtrains.mainloop_runcnt+1
--atprint("Running the main loop, runcnt",advtrains.mainloop_runcnt)
--call load once. see advtrains.load() comment
@ -615,7 +599,6 @@ minetest.register_globalstep(function(dtime_mt)
save_timer=save_interval
atprintbm("saving", t)
end
end)
end)
--if something goes wrong in these functions, there is no help. no pcall here.
@ -644,6 +627,11 @@ function advtrains.save(remove_players_from_wagons)
atwarn("Instructed to save() but load() was never called!")
return
end
if no_action then
atlog("[save] Saving requested externally, but Advtrains step is disabled. Not saving any data as state may be inconsistent.")
return
end
advtrains.avt_save(remove_players_from_wagons) --saving advtrains. includes ndb at advtrains.ndb.save_data()
if atlatc then
atlatc.save()
@ -664,11 +652,9 @@ minetest.register_chatcommand("at_empty_seats",
description = "Detach all players, especially the offline ones, from all trains. Use only when no one serious is on a train.", -- Full description
privs = {train_operator=true, server=true}, -- Require the "privs" privilege to run
func = function(name, param)
return advtrains.pcall(function()
atwarn("Data is being saved. While saving, advtrains will remove the players from trains. Save files will be reloaded afterwards!")
advtrains.save(true)
reload_saves()
end)
end,
})
-- This chat command solves another problem: Trains getting randomly stuck.
@ -678,13 +664,36 @@ minetest.register_chatcommand("at_reroute",
description = "Delete all train routes, force them to recalculate",
privs = {train_operator=true}, -- Only train operator is required, since this is relatively safe.
func = function(name, param)
return advtrains.pcall(function()
advtrains.invalidate_all_paths()
return true, "Successfully invalidated train routes"
end)
end,
})
minetest.register_chatcommand("at_disable_step",
{
params = "<yes/no>",
description = "Disable the advtrains globalstep temporarily",
privs = {server=true},
func = function(name, param)
if minetest.is_yes(param) then
-- disable everything, and turn off saving
no_action = true;
atwarn("The advtrains globalstep has been disabled. Trains are not moving, and no data is saved! Run '/at_disable_step no' to enable again!")
return true, "Disabled advtrains successfully"
elseif no_action then
atwarn("Re-enabling advtrains globalstep...")
reload_saves()
return true
else
return false, "Advtrains is already running normally!"
end
end,
})
advtrains.is_no_action = function()
return no_action
end
local tot=(os.clock()-lot)*1000
minetest.log("action", "[advtrains] Loaded in "..tot.."ms")

View File

@ -282,7 +282,6 @@ function advtrains.get_rail_info_at(pos, drives_on)
end
ndb.run_lbm = function(pos, node)
return advtrains.pcall(function()
local cid=ndbget(pos.x, pos.y, pos.z)
if cid then
--if in database, detect changes and apply.
@ -310,7 +309,6 @@ ndb.run_lbm = function(pos, node)
ndb.update(pos, node)
end
return false
end)
end
@ -358,9 +356,7 @@ ndb.restore_all = function()
end
minetest.register_on_dignode(function(pos, oldnode, digger)
return advtrains.pcall(function()
ndb.clear(pos)
end)
end)
function ndb.get_nodes()
@ -381,14 +377,12 @@ minetest.register_chatcommand("at_sync_ndb",
description = "Write node db back to map and find ghost nodes", -- Full description
privs = {train_operator=true},
func = function(name, param)
return advtrains.pcall(function()
if os.time() < ptime+30 then
return false, "Please wait at least 30s from the previous execution of /at_restore_ndb!"
end
local text = ndb.restore_all()
ptime=os.time()
return true, text
end)
end,
})

View File

@ -275,8 +275,7 @@ function tp.register_track_placer(nnprefix, imgprefix, dispname, def)
groups={advtrains_trackplacer=1, digtron_on_place=1},
liquids_pointable = def.liquids_pointable,
on_place = function(itemstack, placer, pointed_thing)
return advtrains.pcall(function()
local name = placer:get_player_name()
local name = placer:get_player_name()
if not name then
return itemstack, false
end
@ -303,7 +302,6 @@ function tp.register_track_placer(nnprefix, imgprefix, dispname, def)
end
end
return itemstack, true
end)
end,
})
end
@ -317,7 +315,6 @@ minetest.register_craftitem("advtrains:trackworker",{
wield_image = "advtrains_trackworker.png",
stack_max = 1,
on_place = function(itemstack, placer, pointed_thing)
return advtrains.pcall(function()
local name = placer:get_player_name()
if not name then
return
@ -382,10 +379,8 @@ minetest.register_craftitem("advtrains:trackworker",{
advtrains.ndb.swap_node(pos, {name=nnprefix.."_"..suffix..modext[modpos+1], param2=node.param2})
end
end
end)
end,
on_use=function(itemstack, user, pointed_thing)
return advtrains.pcall(function()
local name = user:get_player_name()
if not name then
return
@ -430,7 +425,6 @@ minetest.register_craftitem("advtrains:trackworker",{
else
atprint(name, dump(tp.tracks))
end
end)
end,
})

View File

@ -127,7 +127,6 @@ function advtrains.tp_player_to_train(player)
end
end
minetest.register_on_joinplayer(function(player)
return advtrains.pcall(function()
advtrains.hud[player:get_player_name()] = nil
advtrains.hhud[player:get_player_name()] = nil
--independent of this, cause all wagons of the train which are loaded to reattach their players
@ -137,12 +136,10 @@ minetest.register_on_joinplayer(function(player)
wagon:reattach_all()
end
end
end)
end)
minetest.register_on_dieplayer(function(player)
return advtrains.pcall(function()
local pname=player:get_player_name()
local id=advtrains.player_to_train_mapping[pname]
if id then
@ -156,7 +153,6 @@ minetest.register_on_dieplayer(function(player)
end
end
end
end)
end)
--[[
@ -421,7 +417,7 @@ function advtrains.train_step_b(id, train, dtime)
v_target_apply(v_targets, VLEVER_EMERG, braketar)
end
else
v_target_apply(v_targets, VLEVER_BRAKE, braketar)
v_target_apply(v_targets, VLEVER_ROLL, braketar)
end
end
end

View File

@ -166,7 +166,6 @@ end
-- Remove the wagon
function wagon:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
return advtrains.pcall(function()
if not self:ensure_init() then return end
local data = advtrains.wagons[self.id]
@ -223,7 +222,6 @@ function wagon:on_punch(puncher, time_from_last_punch, tool_capabilities, direct
for _,item in ipairs(self.drops or {self.name}) do
inv:add_item("main", item)
end
end)
end
function wagon:destroy()
--some rules:
@ -268,9 +266,13 @@ function wagon:is_driver_stand(seat)
end
function wagon:on_step(dtime)
return advtrains.pcall(function()
if not self:ensure_init() then return end
if advtrains.is_no_action() then
self.object:remove()
return
end
local t=os.clock()
local pos = self.object:getpos()
local data = advtrains.wagons[self.id]
@ -595,11 +597,9 @@ function wagon:on_step(dtime)
self.old_acceleration_vector=accelerationvec
self.old_yaw=yaw
atprintbm("wagon step", t)
end)
end
function wagon:on_rightclick(clicker)
return advtrains.pcall(function()
if not self:ensure_init() then return end
if not clicker or not clicker:is_player() then
return
@ -687,7 +687,6 @@ function wagon:on_rightclick(clicker)
self:show_get_on_form(pname)
end
end
end)
end
function wagon:get_on(clicker, seatno)
@ -1101,7 +1100,6 @@ function wagon:handle_bordcom_fields(pname, formname, fields)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
return advtrains.pcall(function()
local uid=string.match(formname, "^advtrains_geton_(.+)$")
if uid then
for _,wagon in pairs(minetest.luaentities) do
@ -1185,7 +1183,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
end
end
end)
end)
function wagon:seating_from_key_helper(pname, fields, no)
local data = advtrains.wagons[self.id]
@ -1389,7 +1386,6 @@ function advtrains.register_wagon(sysname_p, prototype, desc, inv_img, nincreati
groups = { not_in_creative_inventory = nincreative and 1 or 0},
on_place = function(itemstack, placer, pointed_thing)
return advtrains.pcall(function()
if not pointed_thing.type == "node" then
return
end
@ -1427,8 +1423,6 @@ function advtrains.register_wagon(sysname_p, prototype, desc, inv_img, nincreati
itemstack:take_item()
end
return itemstack
end)
end,
})
end