--trainlogic.lua
--controls train entities stuff about connecting/disconnecting/colliding trains and other things
local benchmark=false
local bm={}
local bmlt=0
local bmsteps=0
local bmstepint=200
atprintbm=function(action, ta)
if not benchmark then return end
local t=(os.clock()-ta)*1000
if not bm[action] then
bm[action]=t
else
bm[action]=bm[action]+t
end
bmlt=bmlt+t
end
function endstep()
if not benchmark then return end
bmsteps=bmsteps-1
if bmsteps<=0 then
bmsteps=bmstepint
for key, value in pairs(bm) do
minetest.chat_send_all(key.." "..(value/bmstepint).." ms avg.")
end
minetest.chat_send_all("Total time consumed by all advtrains actions per step: "..(bmlt/bmstepint).." ms avg.")
bm={}
bmlt=0
end
end
--acceleration for lever modes (trainhud.lua), per wagon
local t_accel_all={
[0] = -10,
[1] = -3,
[2] = -0.5,
[4] = 0.5,
}
--acceleration per engine
local t_accel_eng={
[0] = 0,
[1] = 0,
[2] = 0,
[4] = 1.5,
}
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
if not advtrains.player_to_train_mapping[player:get_player_name()] then
--players in train are not subject to damage
local ptspos=minetest.pos_to_string(vector.round(player:getpos()))
advtrains.playersbypts[ptspos]=player
end
end
--regular train step
--[[ structure:
1. make trains calculate their occupation windows when needed (a)
2. when occupation tells us so, restore the occupation tables (a)
4. make trains move and update their new occupation windows and write changes
to occupation tables (b)
5. make trains do other stuff (c)
]]--
local t=os.clock()
for k,v in pairs(advtrains.trains) do
advtrains.atprint_context_tid=k
advtrains.train_ensure_init(k, v)
end
for k,v in pairs(advtrains.trains) do
advtrains.atprint_context_tid=k
advtrains.train_step_b(k, v, dtime)
end
for k,v in pairs(advtrains.trains) do
advtrains.atprint_context_tid=k
advtrains.train_step_c(k, v, dtime)
end
advtrains.atprint_context_tid=nil
atprintbm("trainsteps", t)
endstep()
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()
end
end
|