From c71b0209270daa2df717c449c67507d9db0dc0a9 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Sun, 29 May 2016 22:02:51 +0200 Subject: [PATCH] fixed lua errors and code mistakes --- init.lua | 2 ++ pseudoload.lua | 21 ++++++++++++++++----- tracks.lua | 4 ++-- trainlogic.lua | 11 +++++++---- wagons.lua | 7 +++++++ 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/init.lua b/init.lua index bc3b366..48a42d3 100644 --- a/init.lua +++ b/init.lua @@ -17,3 +17,5 @@ dofile(advtrains.modpath.."/trainhud.lua") dofile(advtrains.modpath.."/trackplacer.lua") dofile(advtrains.modpath.."/tracks.lua") dofile(advtrains.modpath.."/wagons.lua") + +dofile(advtrains.modpath.."/pseudoload.lua"); \ No newline at end of file diff --git a/pseudoload.lua b/pseudoload.lua index 5b2e4a4..47318ae 100644 --- a/pseudoload.lua +++ b/pseudoload.lua @@ -9,6 +9,9 @@ advtrains.trackdb={} --[] may be missing if 0,0,0 --load initially +--delayed since all traintypes need to be registered +minetest.after(0, function() + for tt, _ in pairs(advtrains.all_traintypes) do local pl_fpath=minetest.get_worldpath().."/advtrains_trackdb_"..tt advtrains.trackdb[tt]={} @@ -59,6 +62,8 @@ for tt, _ in pairs(advtrains.all_traintypes) do end end +--end minetest.after +end) function advtrains.save_trackdb() for tt, _ in pairs(advtrains.all_traintypes) do @@ -99,8 +104,8 @@ function advtrains.get_rail_info_at(pos, traintype) local node=minetest.get_node_or_nil(pos) if not node then --try raildb - local rdp=vector.round(rdp) - local dbe=advtrains.trackdb[traintype][rdp.y][rdp.x][rdp.z] + local rdp=vector.round(pos) + local dbe=(advtrains.trackdb[traintype] and advtrains.trackdb[traintype][rdp.y] and advtrains.trackdb[traintype][rdp.y][rdp.x] and advtrains.trackdb[traintype][rdp.y][rdp.x][rdp.z]) if dbe then return true, dbe.conn1, dbe.conn2, dbe.rely1 or 0, dbe.rely2 or 0, dbe.railheight or 0 else @@ -114,9 +119,12 @@ function advtrains.get_rail_info_at(pos, traintype) local conn1, conn2, rely1, rely2, railheight=advtrains.get_track_connections(node.name, node.param2) --already in trackdb? - local rdp=vector.round(rdp) - if not advtrains.trackdb[traintype][rdp.y][rdp.x][rdp.z] then--TODO is this necessary? - advtrains.trackdb[rdp.y][rdp.x][rdp.z]={ + local rdp=vector.round(pos) + if not (advtrains.trackdb[traintype] and advtrains.trackdb[traintype][rdp.y] and advtrains.trackdb[traintype][rdp.y][rdp.x] and advtrains.trackdb[traintype][rdp.y][rdp.x][rdp.z]) then--TODO is this necessary? + if not advtrains.trackdb[traintype] then advtrains.trackdb[traintype]={} end + if not advtrains.trackdb[traintype][rdp.y] then advtrains.trackdb[traintype][rdp.y]={} end + if not advtrains.trackdb[traintype][rdp.y][rdp.x] then advtrains.trackdb[traintype][rdp.y][rdp.x]={} end + advtrains.trackdb[traintype][rdp.y][rdp.x][rdp.z]={ conn1=conn1, conn2=conn2, rely1=rely1, rely2=rely2, railheight=railheight @@ -128,6 +136,9 @@ end function advtrains.reset_trackdb_position(pos) local rdp=vector.round(pos) for tt, _ in pairs(advtrains.all_traintypes) do + if not advtrains.trackdb[tt] then advtrains.trackdb[tt]={} end + if not advtrains.trackdb[tt][rdp.y] then advtrains.trackdb[tt][rdp.y]={} end + if not advtrains.trackdb[tt][rdp.y][rdp.x] then advtrains.trackdb[tt][rdp.y][rdp.x]={} end advtrains.trackdb[tt][rdp.y][rdp.x][rdp.z]=nil advtrains.get_rail_info_at(pos, tt)--to restore it. end diff --git a/tracks.lua b/tracks.lua index 54c7100..f9e4b2b 100644 --- a/tracks.lua +++ b/tracks.lua @@ -74,10 +74,10 @@ function advtrains.register_tracks(tracktype, def) after_dig_node=function(pos) advtrains.invalidate_all_paths() advtrains.reset_trackdb_position(pos) - end + end, after_place_node=function(pos) advtrains.reset_trackdb_position(pos) - end + end, } minetest.register_node(def.nodename_prefix.."_st", advtrains.merge_tables(common_def, make_overdef("st", 0, 4), def.straight or {})) minetest.register_node(def.nodename_prefix.."_st_45", advtrains.merge_tables(common_def, make_overdef("st_45", 1, 5), def.straight45 or {})) diff --git a/trainlogic.lua b/trainlogic.lua index 87c5d33..785f6f8 100644 --- a/trainlogic.lua +++ b/trainlogic.lua @@ -18,6 +18,7 @@ end advtrains.trains={} +advtrains.wagon_save={} --load initially advtrains.fpath=minetest.get_worldpath().."/advtrains" @@ -169,7 +170,7 @@ function advtrains.train_step(id, train, dtime) end --check for any trainpart entities if they have been unloaded. do this only if both front and end positions are loaded, to ensure train entities will be placed inside loaded area, and only every second. - train.check_trainpartload=train.check_trainpartload-dtime + train.check_trainpartload=(train.check_trainpartload or 0)-dtime if train.check_trainpartload<=0 and posfront and posback and minetest.get_node_or_nil(posfront) and minetest.get_node_or_nil(posback) then --it is better to iterate luaentites only once local found_uids={} @@ -177,7 +178,7 @@ function advtrains.train_step(id, train, dtime) if wagon.is_wagon and wagon.initialized and wagon.train_id==id then if found_uids[wagon.unique_id] then --duplicate found, delete it - wagon.object and wagon.object:remove() + if wagon.object then wagon.object:remove() end else found_uids[wagon.unique_id]=true end @@ -189,13 +190,15 @@ function advtrains.train_step(id, train, dtime) found_uids[w_id]=nil elseif advtrains.wagon_save[w_id] then --spawn a new and initialize it with the properties from wagon_save - local le=minetest.env:add_entity(posfront, "advtrains:"..sysname):get_luaentity() + local le=minetest.env:add_entity(posfront, advtrains.wagon_save[w_id].name):get_luaentity() for k,v in pairs(advtrains.wagon_save[w_id]) do le[k]=v end + advtrains.wagon_save[w_id].name=nil + advtrains.wagon_save[w_id].object=nil else --what the hell... - local le=minetest.env:add_entity(posfront, "advtrains:"..sysname):get_luaentity() + local le=minetest.env:add_entity(posfront, advtrains.wagon_save[w_id].name):get_luaentity() le.unique_id=w_id le.train_id=id le.pos_in_trainparts=pit diff --git a/wagons.lua b/wagons.lua index ea54e19..b58c574 100644 --- a/wagons.lua +++ b/wagons.lua @@ -72,6 +72,13 @@ function wagon:on_activate(staticdata, dtime_s) --does this object already have an ID? if not self.unique_id then self.unique_id=os.time()..os.clock()--should be random enough. + else + for _,wagon in pairs(minetest.luaentities) do + if wagon.is_wagon and wagon.initialized and wagon.unique_id==self.unique_id then--i am a duplicate! + self.object:remove() + return + end + end end --is my train still here if not self.train_id or not self:train() then