diff options
Diffstat (limited to 'advtrains/advtrains/trainlogic.lua')
-rw-r--r-- | advtrains/advtrains/trainlogic.lua | 91 |
1 files changed, 37 insertions, 54 deletions
diff --git a/advtrains/advtrains/trainlogic.lua b/advtrains/advtrains/trainlogic.lua index 0caea0b..7dad80c 100644 --- a/advtrains/advtrains/trainlogic.lua +++ b/advtrains/advtrains/trainlogic.lua @@ -36,27 +36,8 @@ advtrains.train_brake_force=3--per second, not divided by number of wagons advtrains.train_roll_force=0.5--per second, not divided by number of wagons, acceleration when rolling without brake advtrains.train_emerg_force=10--for emergency brakes(when going off track) -advtrains.save_interval=10 -advtrains.save_timer=advtrains.save_interval -minetest.register_globalstep(function(dtime_mt) -return advtrains.pcall(function() - - --limit dtime: if trains move too far in one step, automation may cause stuck and wrongly braking trains - local dtime=dtime_mt - if dtime>0.2 then - atprint("Limiting dtime to 0.2!") - dtime=0.2 - end - - advtrains.save_timer=advtrains.save_timer-dtime - if advtrains.save_timer<=0 then - local t=os.clock() - --save - advtrains.save() - advtrains.save_timer=advtrains.save_interval - atprintbm("saving", t) - end +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 @@ -82,49 +63,46 @@ return advtrains.pcall(function() atprintbm("trainsteps", t) endstep() -end) -end) +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 - 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() + 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 end end - 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 - local train=advtrains.trains[id] - if not train then advtrains.player_to_train_mapping[pname]=nil return end - for _,wagon in pairs(minetest.luaentities) do - if wagon.is_wagon and wagon.initialized and wagon.train_id==id then - --when player dies, detach him from the train - --call get_off_plr on every wagon since we don't know which one he's on. - wagon:get_off_plr(pname) + 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 + for _,wagon in pairs(minetest.luaentities) do + if wagon.is_wagon and wagon.initialized and wagon.train_id==id then + --when player dies, detach him from the train + --call get_off_plr on every wagon since we don't know which one he's on. + wagon:get_off_plr(pname) + end end end - end -end) + end) end) --[[ train step structure: @@ -822,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 @@ -842,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) |