aboutsummaryrefslogtreecommitdiff
path: root/advtrains/trainlogic.lua
diff options
context:
space:
mode:
Diffstat (limited to 'advtrains/trainlogic.lua')
-rw-r--r--advtrains/trainlogic.lua92
1 files changed, 31 insertions, 61 deletions
diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua
index 86f60c1..580a7d4 100644
--- a/advtrains/trainlogic.lua
+++ b/advtrains/trainlogic.lua
@@ -703,40 +703,6 @@ advtrains.te_register_on_remove(function(id, train)
--atdebug(id,"tnc remove",train.index,train.end_index)
end)
--- Calculates the indices where the window borders of the occupation windows are.
--- TODO adapt this code to new system, probably into a callback (probably only the brake distance code is needed)
-local function calc_occwindows(id, train)
- local end_index = advtrains.path_get_index_by_offset(train, train.index, -train.trainlen)
- train.end_index = end_index
- local cpl_b = end_index - COUPLE_ZONE
- local safety_b = advtrains.path_get_index_by_offset(train, cpl_b, -SAFETY_ZONE)
- local cpl_f = end_index + COUPLE_ZONE
- local safety_f = advtrains.path_get_index_by_offset(train, cpl_f, SAFETY_ZONE)
-
- -- calculate brake distance
- local acc_all = t_accel_all[1]
- local acc_eng = t_accel_eng[1]
- local nwagons = #train.trainparts
- local acc = acc_all + (acc_eng*train.locomotives_in_train)/nwagons
- local vel = train.velocity
- local brakedst = (vel*vel) / (2*acc)
-
- local brake_i = math.max(advtrains.path_get_index_by_offset(train, train.index, brakedst + BRAKE_SPACE), safety_f)
- local aware_i = advtrains.path_get_index_by_offset(train, brake_i, AWARE_ZONE)
-
- return {
- safety_b,
- cpl_b,
- end_index,
- train.index,
- cpl_f,
- safety_f,
- brake_i,
- aware_i,
- }
-end
-
-
--returns new id
function advtrains.create_new_train_at(pos, connid, ioff, trainparts)
local new_id=advtrains.random_id()
@@ -985,21 +951,23 @@ function advtrains.train_check_couples(train)
if not train.cpl_front then
-- recheck front couple
local front_trains, pos = advtrains.occ.get_occupations(train, atround(train.index) + CPL_CHK_DST)
- for tid, idx in pairs(front_trains) do
- local other_train = advtrains.trains[tid]
- if not advtrains.train_ensure_init(tid, other_train) then
- atwarn("Train",tid,"is not initialized! Couldn't check couples!")
- return
- end
- --atdebug(train.id,"front: ",idx,"on",tid,atround(other_train.index),atround(other_train.end_index))
- if other_train.velocity == 0 then
- if idx>=other_train.index and idx<=other_train.index + CPL_ZONE then
- createcouple(pos, train, true, other_train, true)
- break
+ if minetest.get_node_or_nil(pos) then -- if the position is loaded...
+ for tid, idx in pairs(front_trains) do
+ local other_train = advtrains.trains[tid]
+ if not advtrains.train_ensure_init(tid, other_train) then
+ atwarn("Train",tid,"is not initialized! Couldn't check couples!")
+ return
end
- if idx<=other_train.end_index and idx>=other_train.end_index - CPL_ZONE then
- createcouple(pos, train, true, other_train, false)
- break
+ --atdebug(train.id,"front: ",idx,"on",tid,atround(other_train.index),atround(other_train.end_index))
+ if other_train.velocity == 0 then
+ if idx>=other_train.index and idx<=other_train.index + CPL_ZONE then
+ createcouple(pos, train, true, other_train, true)
+ break
+ end
+ if idx<=other_train.end_index and idx>=other_train.end_index - CPL_ZONE then
+ createcouple(pos, train, true, other_train, false)
+ break
+ end
end
end
end
@@ -1013,20 +981,22 @@ function advtrains.train_check_couples(train)
if not train.cpl_back then
-- recheck back couple
local back_trains, pos = advtrains.occ.get_occupations(train, atround(train.end_index) - CPL_CHK_DST)
- for tid, idx in pairs(back_trains) do
- local other_train = advtrains.trains[tid]
- if not advtrains.train_ensure_init(tid, other_train) then
- atwarn("Train",tid,"is not initialized! Couldn't check couples!")
- return
- end
- if other_train.velocity == 0 then
- if idx>=other_train.index and idx<=other_train.index + CPL_ZONE then
- createcouple(pos, train, false, other_train, true)
- break
+ if minetest.get_node_or_nil(pos) then -- if the position is loaded...
+ for tid, idx in pairs(back_trains) do
+ local other_train = advtrains.trains[tid]
+ if not advtrains.train_ensure_init(tid, other_train) then
+ atwarn("Train",tid,"is not initialized! Couldn't check couples!")
+ return
end
- if idx<=other_train.end_index and idx>=other_train.end_index - CPL_ZONE then
- createcouple(pos, train, false, other_train, false)
- break
+ if other_train.velocity == 0 then
+ if idx>=other_train.index and idx<=other_train.index + CPL_ZONE then
+ createcouple(pos, train, false, other_train, true)
+ break
+ end
+ if idx<=other_train.end_index and idx>=other_train.end_index - CPL_ZONE then
+ createcouple(pos, train, false, other_train, false)
+ break
+ end
end
end
end