aboutsummaryrefslogtreecommitdiff
path: root/advtrains/trainlogic.lua
diff options
context:
space:
mode:
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>2019-12-01 12:09:04 +0100
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>2019-12-01 12:09:04 +0100
commit0518fbbf3ae388631cb21f238068f2a6b3785972 (patch)
tree0a70cd4c3a480d32af005f7e1b67cd0e1c30976f /advtrains/trainlogic.lua
parentb71c72b4ab4d50c8f3a3a6ccbe15427548e1d2ff (diff)
parent2157a7ef365902162336540d89d91049c5d3a394 (diff)
downloadadvtrains-0518fbbf3ae388631cb21f238068f2a6b3785972.tar.gz
advtrains-0518fbbf3ae388631cb21f238068f2a6b3785972.tar.bz2
advtrains-0518fbbf3ae388631cb21f238068f2a6b3785972.zip
Merge branch 'master' of https://git.bananach.space/advtrains
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