Merge nocrash branch into master and merging it with the unified functions so that restoring works how it should

Also fix minor bugs and an occasional crash with couples
master
orwell96 2017-04-29 19:44:43 +02:00
commit 0c7144bcc4
12 changed files with 829 additions and 758 deletions

Binary file not shown.

View File

@ -92,12 +92,15 @@ advtrains.register_tracks("default", {
return {
after_place_node=apn_func,
after_dig_node=function(pos)
return advtrains.pcall(function()
advtrains.invalidate_all_paths()
advtrains.ndb.clear(pos)
local pts=minetest.pos_to_string(pos)
atc.controllers[pts]=nil
end)
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())
return
@ -135,6 +138,7 @@ advtrains.register_tracks("default", {
atc.send_command(pos)
end
end
end)
end,
advtrains = {
on_train_enter = function(pos, train_id)

View File

@ -31,6 +31,7 @@ minetest.register_entity("advtrains:discouple", {
end,
get_staticdata=function() return "DISCOUPLE" end,
on_punch=function(self, player)
return advtrains.pcall(function()
--only if player owns at least one wagon next to this
local own=player:get_player_name()
if self.wagon.owner and self.wagon.owner==own and not self.wagon.lock_couples then
@ -54,8 +55,10 @@ minetest.register_entity("advtrains:discouple", {
else
minetest.chat_send_player(own, attrans("You need to own at least one neighboring wagon to destroy this couple."))
end
end)
end,
on_step=function(self, dtime)
return advtrains.pcall(function()
local t=os.clock()
if not self.wagon then
self.object:remove()
@ -77,6 +80,7 @@ minetest.register_entity("advtrains:discouple", {
self.updatepct_timer=2
end
atprintbm("discouple_step", t)
end)
end,
})
@ -99,19 +103,21 @@ minetest.register_entity("advtrains:couple", {
is_couple=true,
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")
self.object:remove()
return
end
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 id1, id2=self.train_id_1, self.train_id_2
if self.train1_is_backpos and not self.train2_is_backpos then
advtrains.do_connect_trains(id1, id2, clicker)
--case 2 (second train is front)
@ -128,8 +134,10 @@ minetest.register_entity("advtrains:couple", {
end
atprint("Coupled trains", id1, id2)
self.object:remove()
end)
end,
on_step=function(self, dtime)
return advtrains.pcall(function()
local t=os.clock()
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]
@ -167,5 +175,6 @@ minetest.register_entity("advtrains:couple", {
end
end
atprintbm("couple step", t)
end)
end,
})

View File

@ -9,6 +9,29 @@ end
advtrains = {trains={}, wagon_save={}, player_to_train_mapping={}}
--pcall
local no_action=false
function advtrains.pcall(fun)
if no_action then return end
local succ, err, return1, return2, return3, return4=pcall(fun)
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
for aoi, le in pairs(minetest.luaentities) do
if le.is_wagon then
le.object:remove()
end
end
minetest.after(1, advtrains.load)
else
return err, return1, return2, return3, return4
end
end
advtrains.modpath = minetest.get_modpath("advtrains")
function advtrains.print_concat_table(a)
@ -94,6 +117,7 @@ dofile(advtrains.modpath.."/craft_items.lua")
--load/save
advtrains.fpath=minetest.get_worldpath().."/advtrains"
function advtrains.avt_load()
local file, err = io.open(advtrains.fpath, "r")
if not file then
@ -197,11 +221,12 @@ end
--## MAIN LOOP ##--
--Calls all subsequent main tasks of both advtrains and atlatc
local init_load
local init_load=false
local save_interval=20
local save_timer=save_interval
minetest.register_globalstep(function(dtime_mt)
return advtrains.pcall(function()
--call load once. see advtrains.load() comment
if not init_load then
advtrains.load()
@ -232,8 +257,10 @@ 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.
--## MAIN LOAD ROUTINE ##
-- Causes the loading of everything
@ -247,6 +274,8 @@ function advtrains.load()
advtrains_itm_init()
end
init_load=true
no_action=false
atprint("[load_all]Loaded advtrains save files")
end
--## MAIN SAVE ROUTINE ##
@ -261,5 +290,6 @@ function advtrains.save()
if atlatc then
atlatc.save()
end
atprint("[save_all]Saved advtrains save files")
end
minetest.register_on_shutdown(advtrains.save)

View File

@ -203,6 +203,7 @@ minetest.register_abm({
nodenames = {"group:save_in_nodedb"},
run_at_every_load = true,
action = 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.
@ -227,14 +228,17 @@ minetest.register_abm({
atprint("nodedb: ", pos, "not in database")
ndb.update(pos, node)
end
end)
end,
interval=10,
chance=1,
})
minetest.register_on_dignode(function(pos, oldnode, digger)
return advtrains.pcall(function()
ndb.clear(pos)
end)
end)
function ndb.get_nodes()
return ndb_nodes

View File

@ -187,6 +187,7 @@ function tp.register_track_placer(nnprefix, imgprefix, dispname)
wield_image = imgprefix.."_placer.png",
groups={},
on_place = function(itemstack, placer, pointed_thing)
return advtrains.pcall(function()
local name = placer:get_player_name()
if not name then
return itemstack
@ -207,6 +208,7 @@ function tp.register_track_placer(nnprefix, imgprefix, dispname)
end
end
return itemstack
end)
end,
})
end
@ -220,6 +222,7 @@ 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
@ -260,8 +263,10 @@ 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
@ -292,6 +297,7 @@ minetest.register_craftitem("advtrains:trackworker",{
else
atprint(name, dump(tp.tracks))
end
end)
end,
})

View File

@ -37,8 +37,7 @@ advtrains.train_roll_force=0.5--per second, not divided by number of wagons, acc
advtrains.train_emerg_force=10--for emergency brakes(when going off track)
advtrains.mainloop_trainlogic(function(dtime)
advtrains.mainloop_trainlogic=function(dtime)
--build a table of all players indexed by pts. used by damage and door system.
advtrains.playersbypts={}
for _, player in pairs(minetest.get_connected_players()) do
@ -67,6 +66,7 @@ advtrains.mainloop_trainlogic(function(dtime)
end
minetest.register_on_joinplayer(function(player)
return advtrains.pcall(function()
local pname=player:get_player_name()
local id=advtrains.player_to_train_mapping[pname]
if id then
@ -85,8 +85,10 @@ minetest.register_on_joinplayer(function(player)
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
@ -101,6 +103,7 @@ minetest.register_on_dieplayer(function(player)
end
end
end)
end)
--[[
train step structure:
@ -797,6 +800,10 @@ end
function advtrains.do_connect_trains(first_id, second_id, player)
local first, second=advtrains.trains[first_id], advtrains.trains[second_id]
if not first or not second or not first.index or not second.index or not first.end_index or not second.end_index then
return false
end
if first.couple_lock_back or second.couple_lock_front then
-- trains are ordered correctly!
if player then
@ -817,6 +824,7 @@ function advtrains.do_connect_trains(first_id, second_id, player)
advtrains.update_trainpart_properties(first_id)
advtrains.trains[first_id].velocity=new_velocity
advtrains.trains[first_id].tarvelocity=0
return true
end
function advtrains.invert_train(train_id)

View File

@ -50,6 +50,7 @@ function wagon:on_activate(sd_uid, dtime_s)
end
function wagon:get_staticdata()
return advtrains.pcall(function()
if not self:ensure_init() then return end
atprint("[wagon "..((self.unique_id and self.unique_id~="" and self.unique_id) or "no-id").."]: saving to wagon_save")
--serialize inventory, if it has one
@ -63,6 +64,7 @@ function wagon:get_staticdata()
advtrains.wagon_save[self.unique_id].name=nil
advtrains.wagon_save[self.unique_id].object=nil
return self.unique_id
end)
end
--returns: uid of wagon
function wagon:init_new_instance(train_id, properties)
@ -147,6 +149,7 @@ 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
if not puncher or not puncher:is_player() then
return
@ -177,6 +180,7 @@ function wagon:on_punch(puncher, time_from_last_punch, tool_capabilities, direct
inv:add_item("main", item)
end
end
end)
end
function wagon:destroy()
--some rules:
@ -211,6 +215,7 @@ end
function wagon:on_step(dtime)
return advtrains.pcall(function()
if not self:ensure_init() then return end
local t=os.clock()
@ -465,6 +470,7 @@ function wagon:on_step(dtime)
self.old_acceleration_vector=accelerationvec
self.old_yaw=yaw
atprintbm("wagon step", t)
end)
end
function advtrains.get_real_path_index(train, pit)
@ -485,6 +491,7 @@ function advtrains.get_real_path_index(train, pit)
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
@ -564,6 +571,7 @@ function wagon:on_rightclick(clicker)
self:show_get_on_form(pname)
end
end
end)
end
function wagon:get_on(clicker, seatno)
@ -708,6 +716,7 @@ function wagon:show_wagon_properties(pname)
minetest.show_formspec(pname, "advtrains_prop_"..self.unique_id, form)
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
@ -780,6 +789,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
end
end)
end)
function wagon:seating_from_key_helper(pname, fields, no)
local sgr=self.seats[no].group
for _,access in ipairs(self.seat_groups[sgr].access_to) do
@ -841,6 +851,7 @@ function advtrains.register_wagon(sysname, prototype, desc, inv_img)
stack_max = 1,
on_place = function(itemstack, placer, pointed_thing)
return advtrains.pcall(function()
if not pointed_thing.type == "node" then
return
end
@ -876,6 +887,7 @@ function advtrains.register_wagon(sysname, prototype, desc, inv_img)
end
return itemstack
end)
end,
})
end

View File

@ -136,7 +136,7 @@ function advtrains_itm_mainloop(dtime)
end
timer=2
end
end)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname=="itrainmap" and fields.quit then
itm_pdata[player:get_player_name()]=nil

View File

@ -62,10 +62,10 @@ function atlatc.load()
end
file:close()
end
end
-- run init code of all environments
atlatc.run_initcode()
end
atlatc.save = function()
--versions:
@ -107,4 +107,4 @@ function atlatc.mainloop_stepcode(dtime)
timer=0
atlatc.run_stepcode()
end
end)
end

View File

@ -22,7 +22,6 @@ function iq.add(t, pos, evtdata)
end
function iq.mainloop(dtime)
if run then
timer=timer + math.min(dtime, 0.2)
for i=1,#queue do
local qe=queue[i]
@ -41,7 +40,6 @@ function iq.mainloop(dtime)
end
end
end
end)