fixed lua errors and code mistakes

master
orwell96 2016-05-29 22:02:51 +02:00
parent 6e34cb1955
commit c71b020927
5 changed files with 34 additions and 11 deletions

View File

@ -17,3 +17,5 @@ dofile(advtrains.modpath.."/trainhud.lua")
dofile(advtrains.modpath.."/trackplacer.lua") dofile(advtrains.modpath.."/trackplacer.lua")
dofile(advtrains.modpath.."/tracks.lua") dofile(advtrains.modpath.."/tracks.lua")
dofile(advtrains.modpath.."/wagons.lua") dofile(advtrains.modpath.."/wagons.lua")
dofile(advtrains.modpath.."/pseudoload.lua");

View File

@ -9,6 +9,9 @@ advtrains.trackdb={}
--[] may be missing if 0,0,0 --[] may be missing if 0,0,0
--load initially --load initially
--delayed since all traintypes need to be registered
minetest.after(0, function()
for tt, _ in pairs(advtrains.all_traintypes) do for tt, _ in pairs(advtrains.all_traintypes) do
local pl_fpath=minetest.get_worldpath().."/advtrains_trackdb_"..tt local pl_fpath=minetest.get_worldpath().."/advtrains_trackdb_"..tt
advtrains.trackdb[tt]={} advtrains.trackdb[tt]={}
@ -59,6 +62,8 @@ for tt, _ in pairs(advtrains.all_traintypes) do
end end
end end
--end minetest.after
end)
function advtrains.save_trackdb() function advtrains.save_trackdb()
for tt, _ in pairs(advtrains.all_traintypes) do 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) local node=minetest.get_node_or_nil(pos)
if not node then if not node then
--try raildb --try raildb
local rdp=vector.round(rdp) local rdp=vector.round(pos)
local dbe=advtrains.trackdb[traintype][rdp.y][rdp.x][rdp.z] 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 if dbe then
return true, dbe.conn1, dbe.conn2, dbe.rely1 or 0, dbe.rely2 or 0, dbe.railheight or 0 return true, dbe.conn1, dbe.conn2, dbe.rely1 or 0, dbe.rely2 or 0, dbe.railheight or 0
else 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) local conn1, conn2, rely1, rely2, railheight=advtrains.get_track_connections(node.name, node.param2)
--already in trackdb? --already in trackdb?
local rdp=vector.round(rdp) local rdp=vector.round(pos)
if not advtrains.trackdb[traintype][rdp.y][rdp.x][rdp.z] then--TODO is this necessary? 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?
advtrains.trackdb[rdp.y][rdp.x][rdp.z]={ 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, conn1=conn1, conn2=conn2,
rely1=rely1, rely2=rely2, rely1=rely1, rely2=rely2,
railheight=railheight railheight=railheight
@ -128,6 +136,9 @@ end
function advtrains.reset_trackdb_position(pos) function advtrains.reset_trackdb_position(pos)
local rdp=vector.round(pos) local rdp=vector.round(pos)
for tt, _ in pairs(advtrains.all_traintypes) do 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.trackdb[tt][rdp.y][rdp.x][rdp.z]=nil
advtrains.get_rail_info_at(pos, tt)--to restore it. advtrains.get_rail_info_at(pos, tt)--to restore it.
end end

View File

@ -74,10 +74,10 @@ function advtrains.register_tracks(tracktype, def)
after_dig_node=function(pos) after_dig_node=function(pos)
advtrains.invalidate_all_paths() advtrains.invalidate_all_paths()
advtrains.reset_trackdb_position(pos) advtrains.reset_trackdb_position(pos)
end end,
after_place_node=function(pos) after_place_node=function(pos)
advtrains.reset_trackdb_position(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", 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 {})) minetest.register_node(def.nodename_prefix.."_st_45", advtrains.merge_tables(common_def, make_overdef("st_45", 1, 5), def.straight45 or {}))

View File

@ -18,6 +18,7 @@ end
advtrains.trains={} advtrains.trains={}
advtrains.wagon_save={}
--load initially --load initially
advtrains.fpath=minetest.get_worldpath().."/advtrains" advtrains.fpath=minetest.get_worldpath().."/advtrains"
@ -169,7 +170,7 @@ function advtrains.train_step(id, train, dtime)
end 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. --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 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 --it is better to iterate luaentites only once
local found_uids={} 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 wagon.is_wagon and wagon.initialized and wagon.train_id==id then
if found_uids[wagon.unique_id] then if found_uids[wagon.unique_id] then
--duplicate found, delete it --duplicate found, delete it
wagon.object and wagon.object:remove() if wagon.object then wagon.object:remove() end
else else
found_uids[wagon.unique_id]=true found_uids[wagon.unique_id]=true
end end
@ -189,13 +190,15 @@ function advtrains.train_step(id, train, dtime)
found_uids[w_id]=nil found_uids[w_id]=nil
elseif advtrains.wagon_save[w_id] then elseif advtrains.wagon_save[w_id] then
--spawn a new and initialize it with the properties from wagon_save --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 for k,v in pairs(advtrains.wagon_save[w_id]) do
le[k]=v le[k]=v
end end
advtrains.wagon_save[w_id].name=nil
advtrains.wagon_save[w_id].object=nil
else else
--what the hell... --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.unique_id=w_id
le.train_id=id le.train_id=id
le.pos_in_trainparts=pit le.pos_in_trainparts=pit

View File

@ -72,6 +72,13 @@ function wagon:on_activate(staticdata, dtime_s)
--does this object already have an ID? --does this object already have an ID?
if not self.unique_id then if not self.unique_id then
self.unique_id=os.time()..os.clock()--should be random enough. 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 end
--is my train still here --is my train still here
if not self.train_id or not self:train() then if not self.train_id or not self:train() then