Now got it to work.
I had to change the code to this:
-- decelerate the train if needed
if twrap.limit and twrap.train.speed ~= 0 and shouldBrakeTrain(twrap.train, twrap.limit) then
local decel = calcDeceleration(twrap.train)
if twrap.train.speed > 0 then
if (twrap.train.speed - decel) > 0 then
twrap.train.speed = math.max(twrap.train.speed - decel, twrap.limit)
else
twrap.train.speed = twrap.limit
end
else
if (twrap.train.speed + decel) < 0 then
twrap.train.speed = math.min(twrap.train.speed + decel, -twrap.limit)
else
twrap.train.speed = -twrap.limit
end
end
end
And to this:
function calcDeceleration(train)
local abs_speed = math.abs(train.speed)
local factor = 1
if isTrainArriving(train) then
factor = 0.75
end
factor = factor * 10.0 -- line added to decellerate much quicker
if abs_speed > 0.6 then
factor = 0.005*factor
elseif abs_speed > 0.3 then
factor = 0.0075*factor
else
factor = 0.01*factor
end
return math.min(1, factor)
end
I will fork your mod and enter those changes there since not everybody will like this change. This leads to unrealistically hard braking trains.