diff options
author | orwell96 <orwell@bleipb.de> | 2018-07-21 16:43:37 +0200 |
---|---|---|
committer | orwell96 <orwell@bleipb.de> | 2018-07-21 16:43:37 +0200 |
commit | a6a042fdc40cf2d2f29062e8a068b074619faefd (patch) | |
tree | ccea896045d0e327e9d24f961bd95da317bafd39 /advtrains | |
parent | a386f4ce5b9a21c4ad9abd2ebfc77ba1835d7ec9 (diff) | |
download | advtrains-a6a042fdc40cf2d2f29062e8a068b074619faefd.tar.gz advtrains-a6a042fdc40cf2d2f29062e8a068b074619faefd.tar.bz2 advtrains-a6a042fdc40cf2d2f29062e8a068b074619faefd.zip |
Teleport player to their train every 2 seconds instead of just on_joinplayer
Solves problem of wagon being unloaded while player sitting inside it because of network lag
Diffstat (limited to 'advtrains')
-rw-r--r-- | advtrains/trainlogic.lua | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 741d5fb..f2ed831 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -81,28 +81,31 @@ advtrains.mainloop_trainlogic=function(dtime) endstep() end -minetest.register_on_joinplayer(function(player) +function advtrains.tp_player_to_train(player) + local pname = player:get_player_name() + local id=advtrains.player_to_train_mapping[pname] + if id then + local train=advtrains.trains[id] + if not train then advtrains.player_to_train_mapping[pname]=nil return end + --set the player to the train position. + --minetest will emerge the area and load the objects, which then will call reattach_all(). + --because player is in mapping, it will not be subject to dying. + player:setpos(train.last_pos_prev) + end +end +minetest.register_on_joinplayer(function() return advtrains.pcall(function() - local pname=player:get_player_name() - local id=advtrains.player_to_train_mapping[pname] - if id then - local train=advtrains.trains[id] - if not train then advtrains.player_to_train_mapping[pname]=nil return end - --set the player to the train position. - --minetest will emerge the area and load the objects, which then will call reattach_all(). - --because player is in mapping, it will not be subject to dying. - player:setpos(train.last_pos_prev) - --independent of this, cause all wagons of the train which are loaded to reattach their players - --needed because already loaded wagons won't call reattach_all() - for _,wagon in pairs(minetest.luaentities) do - if wagon.is_wagon and wagon.initialized and wagon.train_id==id then - wagon:reattach_all() - end + --independent of this, cause all wagons of the train which are loaded to reattach their players + --needed because already loaded wagons won't call reattach_all() + for _,wagon in pairs(minetest.luaentities) do + if wagon.is_wagon and wagon.initialized and wagon.train_id==id then + wagon:reattach_all() end end end) end) + minetest.register_on_dieplayer(function(player) return advtrains.pcall(function() local pname=player:get_player_name() @@ -427,6 +430,12 @@ function advtrains.train_step_a(id, train, dtime) train.check_trainpartload=(train.check_trainpartload or 0)-dtime local node_range=(math.max((minetest.settings:get("active_block_range") or 0),1)*16) if train.check_trainpartload<=0 then + -- teleport players to their train + for _, player in pairs(minetest.get_connected_players()) do + advtrains.tp_player_to_train(player) + end + + local ori_pos=train_pos --see 3a. --atprint("[train "..id.."] at "..minetest.pos_to_string(vector.round(ori_pos))) |