From cee62d7e175b73ae4ea08a0bebccaac1e0df043f Mon Sep 17 00:00:00 2001 From: ywang Date: Tue, 3 Dec 2019 22:46:36 +0100 Subject: Add function to calculate distance until override The function will be used to improve the train movement algorithm. The changes related to spaces in blank lines is caused by a weird feature in the text editor. --- advtrains/lzb.lua | 77 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 15 deletions(-) (limited to 'advtrains') diff --git a/advtrains/lzb.lua b/advtrains/lzb.lua index 6cbf4ab..461e863 100644 --- a/advtrains/lzb.lua +++ b/advtrains/lzb.lua @@ -47,19 +47,19 @@ end local function look_ahead(id, train) - + local acc = advtrains.get_acceleration(train, 1) local vel = train.velocity local brakedst = ( -(vel*vel) / (2*acc) ) * params.DST_FACTOR - + local brake_i = advtrains.path_get_index_by_offset(train, train.index, brakedst + params.BRAKE_SPACE) --local aware_i = advtrains.path_get_index_by_offset(train, brake_i, AWARE_ZONE) - + local lzb = train.lzb local trav = lzb.trav - + --train.debug = lspd - + while trav <= brake_i do trav = trav + 1 local pos = advtrains.path_get(train, trav) @@ -74,12 +74,12 @@ local function look_ahead(id, train) -- run callbacks -- Note: those callbacks are defined in trainlogic.lua for consistency with the other node callbacks advtrains.tnc_call_approach_callback(pos, id, train, trav, lzb.data) - + end end - + lzb.trav = trav - + end --[[ @@ -92,7 +92,7 @@ s = v0 * ------- + - * | ------- | = ----------- local function apply_control(id, train) local lzb = train.lzb - + local i = 1 while i<=#lzb.oncoming do if lzb.oncoming[i].idx < train.index then @@ -100,20 +100,20 @@ local function apply_control(id, train) if ent.fun then ent.fun(ent.pos, id, train, ent.idx, ent.spd, lzb.data) end - + table.remove(lzb.oncoming, i) else i = i + 1 end end - + for i, it in ipairs(lzb.oncoming) do local a = advtrains.get_acceleration(train, 1) --should be negative local v0 = train.velocity local v1 = it.spd if v1 and v1 <= v0 then local s = (v1*v1 - v0*v0) / (2*a) - + local st = s + params.ADD_SLOW if v0 > 3 then st = s + params.ADD_FAST @@ -121,9 +121,9 @@ local function apply_control(id, train) if v0<=0 then st = s + params.ADD_STAND end - + local i = advtrains.path_get_index_by_offset(train, it.idx, -st) - + --train.debug = dump({v0f=v0*f, aff=a*f*f,v0=v0, v1=v1, f=f, a=a, s=s, st=st, i=i, idx=train.index}) if i <= train.index then -- Gotcha! Braking... @@ -131,7 +131,7 @@ local function apply_control(id, train) --train.debug = train.debug .. "BRAKE!!!" return end - + i = advtrains.path_get_index_by_offset(train, i, -params.ZONE_ROLL) if i <= train.index and v0>1 then -- roll control @@ -149,6 +149,53 @@ local function apply_control(id, train) train.ctrl.lzb = nil end +-- Get the distance between the train and the LZB control point +-- If not sure, use 3 as the parameter for lever level. +function advtrains.lzb_get_distance_until_override(id, train, lever) + if lever == 4 then return nil end -- acceleration can not be forced by LZB + local lzb = train.lzb + local i = 1 + local ret = nil -- the value to return + -- Remove LZB entries that are no longer valid + while i <= #lzb.oncoming do + if lzb.oncoming[i].idx < train.index then + local ent = lzb.oncoming[i] + if ent.fun then + ent.fun(ent.pos, id, train, ent.idx, ent.spd, lzb.data) + end + table.remove(lzb.oncoming, i) + else + i = i + 1 + end + end + -- Now run through all the LZB entries and find the one that is nearest to the train + for _, it in ipairs(lzb.oncoming) do + local a = advtrains.get_acceleration(train, lever) + local v0 = train.velocity + local v1 = it.spd + if v1 and v1 <= v0 then + if a !~ 0 then local s = (v1*v1-v0*)/2/a else s = 0 end + local st + if v0 > 3 then st = s + params.ADD_FAST + elseif v0 <= 0 then st = s + params.ADD_STAND + else st = s + params.ADD_SLOW + end + i = advtrains.path_get_index_by_offset(train, it.idx, -st) + if lever == 2 then + i = advtrains.path_get_index_by_offset(train, it.idx, -params.ZONE_ROLL) + end + if lever == 3 then + i = advtrains.path_get_index_by_offset(train, id.idx, -params.ZONE_HOLD) + end + if not ret then ret = i - train.index end + if (i - train.index) < ret then ret = i - train.index end + end + end + -- In extreme cases, there might be no LZB at all. + -- In such a case, return nil because the distance to LZB is infinite. + return ret +end + local function invalidate(train) train.lzb = { trav = atfloor(train.index), -- cgit v1.2.3 From 202c746896d9cc4f0b6b655a933dda776aa24912 Mon Sep 17 00:00:00 2001 From: ywang Date: Wed, 4 Dec 2019 00:14:12 +0100 Subject: Update trainlogic to use new LZB function --- advtrains/trainlogic.lua | 105 ++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 52 deletions(-) (limited to 'advtrains') diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 580a7d4..12e1b93 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -410,12 +410,9 @@ function advtrains.train_step_b(id, train, dtime) --end local tmp_lever - - for _, lev in pairs(train.ctrl) do - -- use the most restrictive of all control overrides - tmp_lever = math.min(tmp_lever or 4, lev) - end - + + tmp_lever = train.ctrl.user or train.ctrl.atc + if not tmp_lever then -- if there was no control at all, default to 3 tmp_lever = 3 @@ -424,57 +421,61 @@ function advtrains.train_step_b(id, train, dtime) if tarvel_cap and trainvelocity>tarvel_cap then tmp_lever = 0 end - - train.lever = tmp_lever - - --- 3a. actually calculate new velocity --- - if tmp_lever~=3 then - local accel = advtrains.get_acceleration(train, tmp_lever) - local vdiff = accel*dtime - - -- This should only be executed when we are accelerating - -- I suspect that this causes the braking bugs - if tmp_lever == 4 then - - -- ATC control exception: don't cross tarvelocity if - -- atc provided a target_vel - if train.tarvelocity then - local tvdiff = train.tarvelocity - trainvelocity - if tvdiff~=0 and math.abs(vdiff) > math.abs(tvdiff) then - --applying this change would cross tarvelocity - --atdebug("In Tvdiff condition, clipping",vdiff,"to",tvdiff) - --atdebug("vel=",trainvelocity,"tvel=",train.tarvelocity) - vdiff=tvdiff - end - end - if tarvel_cap and trainvelocity<=tarvel_cap and trainvelocity+vdiff>tarvel_cap then - vdiff = tarvel_cap - train.velocity - end - local mspeed = (train.max_speed or 10) - if trainvelocity+vdiff > mspeed then - vdiff = mspeed - trainvelocity - end - end - - if trainvelocity+vdiff < 0 then - vdiff = - trainvelocity - end + train.lever = tmp_lever - train.acceleration=vdiff - train.velocity=train.velocity+vdiff - --if train.ctrl.user then - -- train.tarvelocity = train.velocity - --end + --- 4a. Get the correct lever based on LZB --- + tmp_lever = tmp_lever + 1 + local lzblever = tmp_lever + local s, s1, s2, v0, v1, v2, t1, t2, a1, a2 + repeat + tmp_lever = lzblever + lzblever = tmp_lever - 1 + if lzblever < 0 then lzblever = 0 end + s1 = advtrains.lzb_get_distance_until_override(id, train, lzblever) + until (s1 >= 0) or (s1 == nil) -- also jump out if there is no LZB restriction + --- 4b. Calculations --- + a1 = advtrains.get_acceleration(train, tmp_lever) + a2 = advtrains.get_acceleration(train, lzblever) + v0 = train.velocity + if s1 == nil then -- No LZB limit - continue as normal + v2 = v0 + a1 * dtime + if train.tarvelocity then v2 = math.min(v2, train.tarvelocity) end + v2 = math.min(v2, (train.max_speed or 10)) + s = (v2*v2 - v0*v0)/2/a1 + a2 = a1 + lzblever = tmp_lever else - train.acceleration = 0 + if (-v0*v0)/2/a1 < s1 then -- train stops in front of LZB control + v2 = 0 + s = (-v0*v0)/2/a1 + else -- Train continues and further control seems to be taken + v1 = math.sqrt(2*s1*a1 + v0*v0) + if train.tarvelocity then v1 = math.min(v1, train.tarvelocity) end + v1 = math.min(v1, (train.max_speed or 10)) + t1 = (v1-v0)/a1 + t2 = dtime - t1 + if t2 > 0 then -- if the train can reach s2 + v2 = a2*t2+v1 + if v2 < 0 then v2 = 0 end -- Force velocity to be at least 0 + s2 = (v2*v2-v1*v1)/2/a2 + s = s1 + s2 + else -- the train might not reach s2 due to some limits + v2 = v1 + s = (v1*v1 - v0*v0)/2/a1 + end + end end - - --- 4. move train --- - + --- 4c. move train and change train properties --- local pdist = train.path_dist[math.floor(train.index)] or 1 - local distance = (train.velocity*dtime) / pdist - + local distance = s / pdist + if train.lever > lzblever then train.ctrl.lzb = lzblever + else train.ctrl.lzb = nil + end + train.lever = lzblever + train.velocity = v2 + train.acceleration = a2 + --debugging code --train.debug = atdump(train.ctrl).."step_dist: "..math.floor(distance*1000) -- cgit v1.2.3 From b11b92580391d6a24c8726c376210ca239b97d42 Mon Sep 17 00:00:00 2001 From: ywang Date: Wed, 4 Dec 2019 10:19:30 +0100 Subject: Fix problems related to zero Note: this fix only makes sure that the mod does not throw errors, but there are currently weird behaviors related to trainlogic. --- advtrains/lzb.lua | 10 +-- advtrains/trainlogic.lua | 205 +++++++++++++++++++++++------------------------ 2 files changed, 107 insertions(+), 108 deletions(-) (limited to 'advtrains') diff --git a/advtrains/lzb.lua b/advtrains/lzb.lua index 461e863..a34c2de 100644 --- a/advtrains/lzb.lua +++ b/advtrains/lzb.lua @@ -152,10 +152,12 @@ end -- Get the distance between the train and the LZB control point -- If not sure, use 3 as the parameter for lever level. function advtrains.lzb_get_distance_until_override(id, train, lever) - if lever == 4 then return nil end -- acceleration can not be forced by LZB + if lever == 4 then return nil end local lzb = train.lzb local i = 1 local ret = nil -- the value to return + local a = advtrains.get_acceleration(train, lever) -- Acceleration + local v0 = train.velocity -- Remove LZB entries that are no longer valid while i <= #lzb.oncoming do if lzb.oncoming[i].idx < train.index then @@ -170,12 +172,10 @@ function advtrains.lzb_get_distance_until_override(id, train, lever) end -- Now run through all the LZB entries and find the one that is nearest to the train for _, it in ipairs(lzb.oncoming) do - local a = advtrains.get_acceleration(train, lever) - local v0 = train.velocity local v1 = it.spd if v1 and v1 <= v0 then - if a !~ 0 then local s = (v1*v1-v0*)/2/a else s = 0 end - local st + local s, st + if a ~= 0 then s = (v1*v1-v0*v0)/2/a else s = 0 end if v0 > 3 then st = s + params.ADD_FAST elseif v0 <= 0 then st = s + params.ADD_STAND else st = s + params.ADD_SLOW diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 12e1b93..b7a4528 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -37,6 +37,7 @@ local t_accel_all={ [0] = -10, [1] = -3, [2] = -0.5, + [3] = 0, [4] = 0.5, } --acceleration per engine @@ -44,6 +45,7 @@ local t_accel_eng={ [0] = 0, [1] = 0, [2] = 0, + [3] = 0, [4] = 1.5, } @@ -59,7 +61,7 @@ advtrains.mainloop_trainlogic=function(dtime) advtrains.playersbypts[ptspos]=player end end - + if tp_player_tmr<=0 then -- teleport players to their train every 2 seconds for _, player in pairs(minetest.get_connected_players()) do @@ -78,28 +80,28 @@ advtrains.mainloop_trainlogic=function(dtime) 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 - + advtrains.lock_path_inval = true - + 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.lock_path_inval = false - + advtrains.atprint_context_tid=nil - + atprintbm("trainsteps", t) endstep() end @@ -236,7 +238,7 @@ function advtrains.train_ensure_init(id, train) atwarn(debug.traceback()) return nil end - + train.dirty = true if train.no_step then return nil end @@ -245,12 +247,12 @@ function advtrains.train_ensure_init(id, train) assertdef(train, "acceleration", 0) assertdef(train, "id", id) assertdef(train, "ctrl", {}) - - + + if not train.drives_on or not train.max_speed then advtrains.update_trainpart_properties(id) end - + --restore path if not train.path then if not train.last_pos then @@ -272,9 +274,9 @@ function advtrains.train_ensure_init(id, train) Result: log flood. ]] end - + local result = advtrains.path_create(train, train.last_pos, train.last_connid or 1, train.last_frac or 0) - + if result==false then atlog("Train",id,": Restoring path failed, node at",train.last_pos,"is gone! Train will be disabled. You can try to place a rail at this position and restart the server.") train.no_step = true @@ -288,31 +290,31 @@ function advtrains.train_ensure_init(id, train) end -- by now, we should have a working initial path train.wait_for_path = false - + advtrains.update_trainpart_properties(id) recalc_end_index(train) - + --atdebug("Train",id,": Successfully restored path at",train.last_pos," connid",train.last_connid," frac",train.last_frac) - + -- run on_new_path callbacks run_callbacks_new_path(id, train) end - + train.dirty = false -- TODO einbauen! return true end function advtrains.train_step_b(id, train, dtime) if train.no_step or train.wait_for_path or not train.path then return end - + -- in this code, we check variables such as path_trk_? and path_dist. We need to ensure that the path is known for the whole 'Train' zone advtrains.path_get(train, atfloor(train.index + 2)) advtrains.path_get(train, atfloor(train.end_index - 1)) - + --- 3. handle velocity influences --- local train_moves=(train.velocity~=0) local tarvel_cap = train.speed_restriction - + if train.recently_collided_with_env then tarvel_cap=0 if not train_moves then @@ -322,26 +324,26 @@ function advtrains.train_step_b(id, train, dtime) if train.locomotives_in_train==0 then tarvel_cap=0 end - + --- 3a. this can be useful for debugs/warnings and is used for check_trainpartload --- local t_info, train_pos=sid(id), advtrains.path_get(train, atfloor(train.index)) if train_pos then t_info=t_info.." @"..minetest.pos_to_string(train_pos) --atprint("train_pos:",train_pos) end - + --apply off-track handling: local front_off_track = train.index>train.path_trk_f local back_off_track=train.end_indextrainvelocity then train.ctrl.atc=4 @@ -404,20 +406,13 @@ function advtrains.train_step_b(id, train, dtime) end end end - + --if tarvel_cap and train.tarvelocity and tarvel_captarvel_cap then tmp_lever = 0 end @@ -425,8 +420,8 @@ function advtrains.train_step_b(id, train, dtime) train.lever = tmp_lever --- 4a. Get the correct lever based on LZB --- - tmp_lever = tmp_lever + 1 local lzblever = tmp_lever + tmp_lever = tmp_lever + 1 local s, s1, s2, v0, v1, v2, t1, t2, a1, a2 repeat tmp_lever = lzblever @@ -442,27 +437,31 @@ function advtrains.train_step_b(id, train, dtime) v2 = v0 + a1 * dtime if train.tarvelocity then v2 = math.min(v2, train.tarvelocity) end v2 = math.min(v2, (train.max_speed or 10)) - s = (v2*v2 - v0*v0)/2/a1 + if a1 == 0 then -- if there is no acceleration, simply s = v * t + s = v0 * dtime + else + s = (v2*v2 - v0*v0)/2/a1 + end a2 = a1 lzblever = tmp_lever else - if (-v0*v0)/2/a1 < s1 then -- train stops in front of LZB control + if (a1 ~= 0) and ((-v0*v0)/2/a1 < s1) then -- train stops in front of LZB control v2 = 0 s = (-v0*v0)/2/a1 else -- Train continues and further control seems to be taken v1 = math.sqrt(2*s1*a1 + v0*v0) if train.tarvelocity then v1 = math.min(v1, train.tarvelocity) end v1 = math.min(v1, (train.max_speed or 10)) - t1 = (v1-v0)/a1 + if a1 == 0 then t1 = s1/v1 else t1 = (v1-v0)/a1 end t2 = dtime - t1 if t2 > 0 then -- if the train can reach s2 v2 = a2*t2+v1 if v2 < 0 then v2 = 0 end -- Force velocity to be at least 0 - s2 = (v2*v2-v1*v1)/2/a2 + if a2 == 0 then s2 = v2 * t2 else s2 = (v2*v2-v1*v1)/2/a2 end s = s1 + s2 else -- the train might not reach s2 due to some limits v2 = v1 - s = (v1*v1 - v0*v0)/2/a1 + if a1 == 0 then s = v2 * dtime else s = (v1*v1 - v0*v0)/2/a1 end end end end @@ -478,47 +477,47 @@ function advtrains.train_step_b(id, train, dtime) --debugging code --train.debug = atdump(train.ctrl).."step_dist: "..math.floor(distance*1000) - + train.index=train.index+distance - + recalc_end_index(train) end function advtrains.train_step_c(id, train, dtime) if train.no_step or train.wait_for_path or not train.path then return end - + -- all location/extent-critical actions have been done. -- calculate the new occupation window run_callbacks_update(id, train) - + -- Return if something(TM) damaged the path if train.no_step or train.wait_for_path or not train.path then return end - + advtrains.path_clear_unused(train) - + advtrains.path_setrestore(train) - + -- less important stuff - + train.check_trainpartload=(train.check_trainpartload or 0)-dtime if train.check_trainpartload<=0 then advtrains.spawn_wagons(id) train.check_trainpartload=2 end - + --- 8. check for collisions with other trains and damage players --- - + local train_moves=(train.velocity~=0) - + --- Check whether this train can be coupled to another, and set couple entities accordingly if not train.was_standing and not train_moves then advtrains.train_check_couples(train) end train.was_standing = not train_moves - + if train_moves then - + local collided = false local coll_grace=1 local collindex = advtrains.path_get_index_by_offset(train, train.index, -coll_grace) @@ -574,7 +573,7 @@ function advtrains.train_step_c(id, train, dtime) if is_loaded_area then local objs = minetest.get_objects_inside_radius(rcollpos, 2) for _,obj in ipairs(objs) do - if not obj:is_player() and obj:get_armor_groups().fleshy and obj:get_armor_groups().fleshy > 0 + if not obj:is_player() and obj:get_armor_groups().fleshy and obj:get_armor_groups().fleshy > 0 and obj:get_luaentity() and obj:get_luaentity().name~="signs:text" then obj:punch(obj, 1, { full_punch_interval = 1.0, damage_groups = {fleshy = 1000}, }, nil) end @@ -637,7 +636,7 @@ local function tnc_call_leave_callback(pos, train_id, train, index) if mregnode and mregnode.advtrains and mregnode.advtrains.on_train_leave then mregnode.advtrains.on_train_leave(pos, train_id, train, index) end - + -- call other registered callbacks run_callbacks_leave_node(pos, train_id, train, index) end @@ -649,7 +648,7 @@ function advtrains.tnc_call_approach_callback(pos, train_id, train, index, lzbda if mregnode and mregnode.advtrains and mregnode.advtrains.on_train_approach then mregnode.advtrains.on_train_approach(pos, train_id, train, index, lzbdata) end - + -- call other registered callbacks run_callbacks_approach_node(pos, train_id, train, index, lzbdata) end @@ -708,68 +707,68 @@ end) function advtrains.create_new_train_at(pos, connid, ioff, trainparts) local new_id=advtrains.random_id() while advtrains.trains[new_id] do new_id=advtrains.random_id() end--ensure uniqueness - + local t={} t.id = new_id - + t.last_pos=pos t.last_connid=connid t.last_frac=ioff - + --t.tarvelocity=0 t.velocity=0 t.trainparts=trainparts - + advtrains.trains[new_id] = t --atdebug("Created new train:",t) - + if not advtrains.train_ensure_init(new_id, advtrains.trains[new_id]) then atwarn("create_new_train_at",pos,connid,"failed! This might lead to temporary bugs.") return end - + run_callbacks_create(new_id, advtrains.trains[new_id]) - + return new_id end function advtrains.remove_train(id) local train = advtrains.trains[id] - + if not advtrains.train_ensure_init(id, train) then atwarn("remove_train",id,"failed! This might lead to temporary bugs.") return end - + run_callbacks_remove(id, train) - + advtrains.path_invalidate(train) advtrains.couple_invalidate(train) - + local tp = train.trainparts --atdebug("Removing train",id,"leftover trainparts:",tp) - + advtrains.trains[id] = nil - + return tp - + end function advtrains.add_wagon_to_train(wagon_id, train_id, index) local train=advtrains.trains[train_id] - + if not advtrains.train_ensure_init(train_id, train) then atwarn("Train",train_id,"is not initialized! Operation aborted!") return end - + if index then table.insert(train.trainparts, index, wagon_id) else table.insert(train.trainparts, wagon_id) end - + advtrains.update_trainpart_properties(train_id) recalc_end_index(train) run_callbacks_update(train_id, train) @@ -784,14 +783,14 @@ function advtrains.update_trainpart_properties(train_id, invert_flipstate) --FIX: deep-copy the table!!! train.max_speed=20 train.extent_h = 0; - + local rel_pos=0 local count_l=0 local shift_dcpl_lock=false for i, w_id in ipairs(train.trainparts) do - + local data = advtrains.wagons[w_id] - + -- 1st: update wagon data (pos_in_train a.s.o) if data then local wagon = advtrains.wagon_prototypes[data.type or data.entity_name] @@ -812,7 +811,7 @@ function advtrains.update_trainpart_properties(train_id, invert_flipstate) shift_dcpl_lock, data.dcpl_lock = data.dcpl_lock, shift_dcpl_lock end rel_pos=rel_pos+wagon.wagon_span - + if wagon.drives_on then for k,_ in pairs(train.drives_on) do if not wagon.drives_on[k] then @@ -834,7 +833,7 @@ local ablkrng = minetest.settings:get("active_block_range")*16 -- Called from train_step_*(), not required to check init. function advtrains.spawn_wagons(train_id) local train = advtrains.trains[train_id] - + for i = 1, #train.trainparts do local w_id = train.trainparts[i] local data = advtrains.wagons[w_id] @@ -847,14 +846,14 @@ function advtrains.spawn_wagons(train_id) -- eventually need to spawn new object. check if position is loaded. local index = advtrains.path_get_index_by_offset(train, train.index, -data.pos_in_train) local pos = advtrains.path_get(train, atfloor(index)) - + local spawn = false for _,p in pairs(minetest.get_connected_players()) do if vector.distance(p:get_pos(),pos)<=ablkrng then spawn = true end end - + if spawn then local wt = advtrains.get_wagon_prototype(data) local wagon = minetest.add_entity(pos, wt):get_luaentity() @@ -897,11 +896,11 @@ function advtrains.split_train_at_index(train, index) advtrains.update_trainpart_properties(train_id) recalc_end_index(train) run_callbacks_update(train_id, train) - + --create subtrain local newtrain_id=advtrains.create_new_train_at(pos, connid, frac, tp) local newtrain=advtrains.trains[newtrain_id] - + newtrain.velocity=train.velocity return newtrain_id -- return new train ID, so new train can be manipulated @@ -938,7 +937,7 @@ local function createcouple(pos, train1, t1_is_front, train2, t2_is_front) else train2.cpl_back = obj end - + end function advtrains.train_check_couples(train) @@ -1029,7 +1028,7 @@ end function advtrains.do_connect_trains(first_id, second_id, vel) local first, second=advtrains.trains[first_id], advtrains.trains[second_id] - + if not advtrains.train_ensure_init(first_id, first) then atwarn("Train",first_id,"is not initialized! Operation aborted!") return @@ -1038,18 +1037,18 @@ function advtrains.do_connect_trains(first_id, second_id, vel) atwarn("Train",second_id,"is not initialized! Operation aborted!") return end - + local first_wagoncnt=#first.trainparts local second_wagoncnt=#second.trainparts - + for _,v in ipairs(second.trainparts) do table.insert(first.trainparts, v) end - + advtrains.remove_train(second_id) - + first.velocity= vel or 0 - + advtrains.update_trainpart_properties(first_id) advtrains.couple_invalidate(first) return true @@ -1057,14 +1056,14 @@ end function advtrains.invert_train(train_id) local train=advtrains.trains[train_id] - + if not advtrains.train_ensure_init(train_id, train) then atwarn("Train",train_id,"is not initialized! Operation aborted!") return end - + advtrains.path_setrestore(train, true) - + -- rotate some other stuff if train.door_open then train.door_open = - train.door_open @@ -1072,20 +1071,20 @@ function advtrains.invert_train(train_id) if train.atc_command then train.atc_arrow = not train.atc_arrow end - + advtrains.path_invalidate(train, true) advtrains.couple_invalidate(train) - + local old_trainparts=train.trainparts train.trainparts={} for k,v in ipairs(old_trainparts) do table.insert(train.trainparts, 1, v)--notice insertion at first place end advtrains.update_trainpart_properties(train_id, true) - + -- recalculate path advtrains.train_ensure_init(train_id, train) - + -- If interlocking present, check whether this train is in a section and then set as shunt move after reversion if advtrains.interlocking and train.il_sections and #train.il_sections > 0 then train.is_shunt = true @@ -1114,7 +1113,7 @@ function advtrains.invalidate_all_paths(pos) else tab = advtrains.trains end - + for id, _ in pairs(tab) do advtrains.invalidate_path(id) end @@ -1146,7 +1145,7 @@ local nonblocknodes={ "default:fence_junglewood", "default:torch", "bones:bones", - + "default:sign_wall", "signs:sign_wall", "signs:sign_wall_blue", @@ -1160,8 +1159,8 @@ local nonblocknodes={ "signs:sign_wall_yellow", "signs:sign_post", "signs:sign_hanging", - - + + } minetest.after(0, function() for _,name in ipairs(nonblocknodes) do -- cgit v1.2.3 From 9abe2af7aafc55377dde73b4fff278043c7274f6 Mon Sep 17 00:00:00 2001 From: ywang Date: Thu, 5 Dec 2019 22:23:13 +0100 Subject: More accurate train logic, but still buggy --- advtrains/helpers.lua | 29 ++++---- advtrains/lzb.lua | 172 +++++++++++++++++++++++++---------------------- advtrains/trainlogic.lua | 78 ++++++++------------- 3 files changed, 138 insertions(+), 141 deletions(-) (limited to 'advtrains') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index e04991e..a2eabb5 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -79,7 +79,7 @@ function advtrains.yawToDirection(yaw, conn1, conn2) local yaw2 = advtrains.dir_to_angle(conn2) local adiff1 = advtrains.minAngleDiffRad(yaw, yaw1) local adiff2 = advtrains.minAngleDiffRad(yaw, yaw2) - + if math.abs(adiff2)=1 do conn_y = conn_y - 1 adj_pos.y = adj_pos.y + 1 end - + local nextnode_ok, nextconns, nextrail_y=advtrains.get_rail_info_at(adj_pos, drives_on) if not nextnode_ok then adj_pos.y = adj_pos.y - 1 @@ -398,6 +398,13 @@ function advtrains.decode_pos(pts) return vector.new(dec(strx), dec(stry), dec(strz)) end +function advtrains.solve_quadratic_equation(a, b, c) + if not (a and b and c) then return nil end + local delta = (b*b - 4*a*c) + if delta < 0 then return nil end + return {((-b+math.sqrt(delta))/2/a),((-b-math.sqrt(delta))/2/a)} +end + --[[ Benchmarking code local tdt = {} local tlt = {} @@ -435,8 +442,6 @@ end atdebug("pts",os.clock()-t1,"s") --Results: ---2018-11-29 16:57:08: ACTION[Main]: [advtrains]endec 1.786451 s ---2018-11-29 16:57:10: ACTION[Main]: [advtrains]pts 2.566377 s +--2018-11-29 16:57:08: ACTION[Main]: [advtrains]endec 1.786451 s +--2018-11-29 16:57:10: ACTION[Main]: [advtrains]pts 2.566377 s ]] - - diff --git a/advtrains/lzb.lua b/advtrains/lzb.lua index a34c2de..3961438 100644 --- a/advtrains/lzb.lua +++ b/advtrains/lzb.lua @@ -83,80 +83,104 @@ local function look_ahead(id, train) end --[[ -Distance needed to accelerate from v0 to v1 with constant acceleration a: - - v1 - v0 a / v1 - v0 \ 2 v1^2 - v0^2 -s = v0 * ------- + - * | ------- | = ----------- - a 2 \ a / 2*a +The .i element is the index at which LZB overrides the train control with the +lever specified by the index value. The .v element is the speed at which the +train control is taken over by LZB with the lever specified by the index. The .t +element calculates the time needed for the train to reach the point where the +control is taken over by LZB with the lever specified by the index. Unintialized +.v and .t values indicate that the train has passed the point with the +corresponding index. Note that thhe 0th item contains the data related to the +LZB point itself, and not related to the emergency brake. ]] - -local function apply_control(id, train) - local lzb = train.lzb - - local i = 1 - while i<=#lzb.oncoming do - if lzb.oncoming[i].idx < train.index then - local ent = lzb.oncoming[i] - if ent.fun then - ent.fun(ent.pos, id, train, ent.idx, ent.spd, lzb.data) - end - - table.remove(lzb.oncoming, i) +function advtrains.lzb_map_entry(train, lzb) + local ret = {[0]={},[1]={},[2]={},[3]={}} + if (not train) or (not lzb) then return ret end + local ti = train.index + local v0 = train.velocity + local v1 = lzb.spd + local a = advtrains.get_acceleration(train, train.lever) + local s = (v1*v1-v0*v0)/2/advtrains.get_acceleration(train, 1) + if v0 > 3 then s = s + params.ADD_FAST + elseif v0 <=0 then s = s + params.ADD_STAND + else s = s + params.ADD_SLOW + end + ret[0].i = lzb.idx + ret[1].i = advtrains.path_get_index_by_offset(train, ret[0].i, -s) + ret[2].i = advtrains.path_get_index_by_offset(train, ret[1].i, -params.ZONE_ROLL) + ret[3].i = advtrains.path_get_index_by_offset(train, ret[2].i, -params.ZONE_HOLD) + if a == 0 then ret[3].t = (ret[3].i)/v0 + else + ret[3].t = advtrains.solve_quadratic_equation(a/2, v0, (ti-ret[3].i)) + if not ret[3].t then ret[3].t = 0 else - i = i + 1 + if ret[3].t[1]<0 then + if ret[3].t[2]<0 then ret[3].t = ret[3].t[2] + else ret[3].t = math.abs(math.max(ret[3].t[1], ret[3].t[2])) + end + else + if ret[3].t[2]<0 then ret[3].t = ret[3].t[1] + else ret[3].t = math.min(ret[3].t[1], ret[3].t[2]) + end + end end end - - for i, it in ipairs(lzb.oncoming) do - local a = advtrains.get_acceleration(train, 1) --should be negative - local v0 = train.velocity - local v1 = it.spd - if v1 and v1 <= v0 then - local s = (v1*v1 - v0*v0) / (2*a) - - local st = s + params.ADD_SLOW - if v0 > 3 then - st = s + params.ADD_FAST - end - if v0<=0 then - st = s + params.ADD_STAND - end - - local i = advtrains.path_get_index_by_offset(train, it.idx, -st) - - --train.debug = dump({v0f=v0*f, aff=a*f*f,v0=v0, v1=v1, f=f, a=a, s=s, st=st, i=i, idx=train.index}) - if i <= train.index then - -- Gotcha! Braking... - train.ctrl.lzb = 1 - --train.debug = train.debug .. "BRAKE!!!" - return - end - - i = advtrains.path_get_index_by_offset(train, i, -params.ZONE_ROLL) - if i <= train.index and v0>1 then - -- roll control - train.ctrl.lzb = 2 - return + ret[3].v = (v0 + a*ret[3].t) + if ret[3].v <= lzb.spd then ret[3].v = lzb.spd end -- Avoid devision by zero + if ret[3].v > (train.max_speed or 10) then ret[3].v = train.max_speed or 0 end + ret[2].v = ret[3].v + ret[2].t = (ret[3].i-ret[2].i)/ret[3].v + ret[1].t = advtrains.solve_quadratic_equation(advtrains.get_acceleration(train,2),ret[2].v,(ret[2].i-ret[1].i)) + if not ret[1].t then ret[1].t = 0 + else + if ret[1].t[1]<0 then + if ret[1].t[2]<0 then ret[1].t = ret[1].t[2] + else ret[1].t = math.abs(math.max(ret[1].t[1], ret[1].t[2])) end - i = advtrains.path_get_index_by_offset(train, i, -params.ZONE_HOLD) - if i <= train.index and v0>1 then - -- hold speed - train.ctrl.lzb = 3 - return + else + if ret[1].t[2]<0 then ret[1].t = ret[1].t[1] + else ret[1].t = math.min(math.max(ret[1].t[1], ret[1].t[2])) end end end - train.ctrl.lzb = nil + ret[1].v = (ret[2].v + advtrains.get_acceleration(train,2)*ret[1].t) + if ret[1].v <= lzb.spd then ret[1].v = lzb.spd end + ret[0].v = lzb.spd + ret[0].t = (ret[0].v-ret[1].v)/advtrains.get_acceleration(train,1) + return ret +end + +--[[ +advtrains.lzb_get_limit_by_entry - get the limit +Returns a table contraining the speed and the acceleration limits +]] +function advtrains.lzb_get_limit_by_entry(train, lzb) + local ret = {} + local lzbmap = advtrains.lzb_map_entry(train, lzb) + if not (lzbmap[3].i and lzbmap[2].i and lzbmap[1].i and lzbmap[0].i) then + return {} + elseif (lzbmap[3].i > train.index) then return {} + elseif (lzbmap[2].i > train.index) then ret.lever = 3 + elseif (lzbmap[1].i > train.index) then ret.lever = 2 + else ret.lever = 1 + end + if ret.lever == 3 then ret.velocity = lzbmap[3].v + else + local s = train.index - lzbmap[ret.lever].i + local a = advtrains.get_acceleration(train, ret.lever) + local v0 = lzbmap[ret.lever].v + ret.velocity = math.sqrt(2*a*s - v0*v0) + end + if ret.velocity < train.velocity -1 then ret.lever = ret.lever - 1 end + return ret end --- Get the distance between the train and the LZB control point --- If not sure, use 3 as the parameter for lever level. -function advtrains.lzb_get_distance_until_override(id, train, lever) +-- Get next LZB restriction with the lowest speed restriction +function advtrains.lzb_get_next(train) if lever == 4 then return nil end local lzb = train.lzb local i = 1 - local ret = nil -- the value to return - local a = advtrains.get_acceleration(train, lever) -- Acceleration + local ret + local a = advtrains.get_acceleration(train, 3) -- Acceleration local v0 = train.velocity -- Remove LZB entries that are no longer valid while i <= #lzb.oncoming do @@ -170,29 +194,20 @@ function advtrains.lzb_get_distance_until_override(id, train, lever) i = i + 1 end end - -- Now run through all the LZB entries and find the one that is nearest to the train + -- Now run through all the LZB entries and find the one with the lowest + -- speed requirement for _, it in ipairs(lzb.oncoming) do local v1 = it.spd if v1 and v1 <= v0 then - local s, st - if a ~= 0 then s = (v1*v1-v0*v0)/2/a else s = 0 end - if v0 > 3 then st = s + params.ADD_FAST - elseif v0 <= 0 then st = s + params.ADD_STAND - else st = s + params.ADD_SLOW - end - i = advtrains.path_get_index_by_offset(train, it.idx, -st) - if lever == 2 then - i = advtrains.path_get_index_by_offset(train, it.idx, -params.ZONE_ROLL) - end - if lever == 3 then - i = advtrains.path_get_index_by_offset(train, id.idx, -params.ZONE_HOLD) + local curlimit = advtrains.lzb_get_limit_by_entry(train, it) + local retlimit = advtrains.lzb_get_limit_by_entry(train, ret) + if not ret then ret = it + elseif not curlimit.velocity then + elseif retlimit.velocity > curlimit.velocity then + ret = it end - if not ret then ret = i - train.index end - if (i - train.index) < ret then ret = i - train.index end end end - -- In extreme cases, there might be no LZB at all. - -- In such a case, return nil because the distance to LZB is infinite. return ret end @@ -234,5 +249,4 @@ advtrains.te_register_on_update(function(id, train) return end look_ahead(id, train) - apply_control(id, train) end, true) diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index b7a4528..30d2d6a 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -419,61 +419,39 @@ function advtrains.train_step_b(id, train, dtime) train.lever = tmp_lever - --- 4a. Get the correct lever based on LZB --- - local lzblever = tmp_lever - tmp_lever = tmp_lever + 1 - local s, s1, s2, v0, v1, v2, t1, t2, a1, a2 - repeat - tmp_lever = lzblever - lzblever = tmp_lever - 1 - if lzblever < 0 then lzblever = 0 end - s1 = advtrains.lzb_get_distance_until_override(id, train, lzblever) - until (s1 >= 0) or (s1 == nil) -- also jump out if there is no LZB restriction - --- 4b. Calculations --- - a1 = advtrains.get_acceleration(train, tmp_lever) - a2 = advtrains.get_acceleration(train, lzblever) - v0 = train.velocity - if s1 == nil then -- No LZB limit - continue as normal - v2 = v0 + a1 * dtime - if train.tarvelocity then v2 = math.min(v2, train.tarvelocity) end - v2 = math.min(v2, (train.max_speed or 10)) - if a1 == 0 then -- if there is no acceleration, simply s = v * t - s = v0 * dtime - else - s = (v2*v2 - v0*v0)/2/a1 + --- 4a. Calculate movement --- + local lzbnxt = advtrains.lzb_get_next(train) + local lzblimit = advtrains.lzb_get_limit_by_entry(train, lzbnxt) + local lzbmap = advtrains.lzb_map_entry(train, lzbnxt) + local a = advtrains.get_acceleration(train, tmp_lever) + local v0 = train.velocity + local v1 = a*dtime+v0 + v1 = math.min(v1, (train.max_speed or 10)) + local s + if a == 0 then s = v1*dtime + else s = (v1*v1 - v0*v0)/2/a + end + if lzblimit.velocity and lzblimit.velocity < train.velocity then + tmp_lever = lzblimit.lever + while (lzbmap[tmp_lever].t > dtime) do + tmp_lever = tmp_lever - 1 end - a2 = a1 - lzblever = tmp_lever - else - if (a1 ~= 0) and ((-v0*v0)/2/a1 < s1) then -- train stops in front of LZB control - v2 = 0 - s = (-v0*v0)/2/a1 - else -- Train continues and further control seems to be taken - v1 = math.sqrt(2*s1*a1 + v0*v0) - if train.tarvelocity then v1 = math.min(v1, train.tarvelocity) end - v1 = math.min(v1, (train.max_speed or 10)) - if a1 == 0 then t1 = s1/v1 else t1 = (v1-v0)/a1 end - t2 = dtime - t1 - if t2 > 0 then -- if the train can reach s2 - v2 = a2*t2+v1 - if v2 < 0 then v2 = 0 end -- Force velocity to be at least 0 - if a2 == 0 then s2 = v2 * t2 else s2 = (v2*v2-v1*v1)/2/a2 end - s = s1 + s2 - else -- the train might not reach s2 due to some limits - v2 = v1 - if a1 == 0 then s = v2 * dtime else s = (v1*v1 - v0*v0)/2/a1 end - end + a = advtrains.get_acceleration(train, tmp_lever) + v0 = lzbmap[tmp_lever].v + t = dtime - lzbmap[tmp_lever].t + v1 = a*t+v0 + v1 = math.min(v1, (train.max_speed or 10)) + s = lzbmap[tmp_lever].i - train.index + if a == 0 then s = s + v1*t + else s = s + (v1*v1-v0*v0)/2/a end end - --- 4c. move train and change train properties --- + --- 4b. Move train and update train properties --- local pdist = train.path_dist[math.floor(train.index)] or 1 local distance = s / pdist - if train.lever > lzblever then train.ctrl.lzb = lzblever - else train.ctrl.lzb = nil - end - train.lever = lzblever - train.velocity = v2 - train.acceleration = a2 + train.lever = tmp_lever + train.velocity = v1 + train.acceleration = a --debugging code --train.debug = atdump(train.ctrl).."step_dist: "..math.floor(distance*1000) -- cgit v1.2.3 From 45f5406e7ebf22f0c2594c8e8a4e734cc23479b7 Mon Sep 17 00:00:00 2001 From: ywang Date: Thu, 5 Dec 2019 22:38:18 +0100 Subject: Fix minor bugs --- advtrains/trainlogic.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'advtrains') diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 30d2d6a..5f340ac 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -427,25 +427,33 @@ function advtrains.train_step_b(id, train, dtime) local v0 = train.velocity local v1 = a*dtime+v0 v1 = math.min(v1, (train.max_speed or 10)) + v1 = math.max(v1, 0) local s if a == 0 then s = v1*dtime else s = (v1*v1 - v0*v0)/2/a end + train.ctrl.lzb = nil if lzblimit.velocity and lzblimit.velocity < train.velocity then tmp_lever = lzblimit.lever while (lzbmap[tmp_lever].t > dtime) do tmp_lever = tmp_lever - 1 end + train.ctrl.lzb = tmp_lever a = advtrains.get_acceleration(train, tmp_lever) v0 = lzbmap[tmp_lever].v t = dtime - lzbmap[tmp_lever].t v1 = a*t+v0 v1 = math.min(v1, (train.max_speed or 10)) + v1 = math.max(v1, 0) s = lzbmap[tmp_lever].i - train.index if a == 0 then s = s + v1*t else s = s + (v1*v1-v0*v0)/2/a end end + -- FIX: calculate the average acceleration, as if it is static, to avoid + -- weird wagon positions + -- Since v0 might have been changed, we should use train.velocity instead. + a = (v1 - train.velocity)/dtime --- 4b. Move train and update train properties --- local pdist = train.path_dist[math.floor(train.index)] or 1 local distance = s / pdist -- cgit v1.2.3 From a404f83ebb6aa26b16e56debc2d6d86f52df680e Mon Sep 17 00:00:00 2001 From: ywang Date: Sat, 7 Dec 2019 16:48:19 +0100 Subject: Improved quadratic function solver; added wrapper for math.sqrt() --- advtrains/helpers.lua | 11 ++++++++++- advtrains/lzb.lua | 4 ++-- advtrains/trainlogic.lua | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) (limited to 'advtrains') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index a2eabb5..9afe72d 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -398,13 +398,22 @@ function advtrains.decode_pos(pts) return vector.new(dec(strx), dec(stry), dec(strz)) end +-- Solve quadratic equations (i.e. a*x^2 + b*x + c = 0) function advtrains.solve_quadratic_equation(a, b, c) if not (a and b and c) then return nil end + if a == 0 then return {-c/b, -c/b} end -- avoid division by zero local delta = (b*b - 4*a*c) - if delta < 0 then return nil end + if delta < 0 then return {-b/2/a,-b/2/a} end -- ignore imaginary part return {((-b+math.sqrt(delta))/2/a),((-b-math.sqrt(delta))/2/a)} end +-- safe square root +-- Negative return values indicate imaginary numbers. +function advtrains.safesqrt(a) + if a >= 0 then return math.sqrt(a) end + return 0 - math.sqrt(-a) +end + --[[ Benchmarking code local tdt = {} local tlt = {} diff --git a/advtrains/lzb.lua b/advtrains/lzb.lua index 3961438..30d7972 100644 --- a/advtrains/lzb.lua +++ b/advtrains/lzb.lua @@ -94,7 +94,7 @@ LZB point itself, and not related to the emergency brake. ]] function advtrains.lzb_map_entry(train, lzb) local ret = {[0]={},[1]={},[2]={},[3]={}} - if (not train) or (not lzb) then return ret end + if not (train and lzb) then return ret end local ti = train.index local v0 = train.velocity local v1 = lzb.spd @@ -168,7 +168,7 @@ function advtrains.lzb_get_limit_by_entry(train, lzb) local s = train.index - lzbmap[ret.lever].i local a = advtrains.get_acceleration(train, ret.lever) local v0 = lzbmap[ret.lever].v - ret.velocity = math.sqrt(2*a*s - v0*v0) + ret.velocity = math.abs(advtrains.safesqrt(2*a*s - v0*v0)) end if ret.velocity < train.velocity -1 then ret.lever = ret.lever - 1 end return ret diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 5f340ac..441a11a 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -433,7 +433,7 @@ function advtrains.train_step_b(id, train, dtime) else s = (v1*v1 - v0*v0)/2/a end train.ctrl.lzb = nil - if lzblimit.velocity and lzblimit.velocity < train.velocity then + if lzblimit.velocity and lzblimit.lever < train.lever then tmp_lever = lzblimit.lever while (lzbmap[tmp_lever].t > dtime) do tmp_lever = tmp_lever - 1 @@ -456,7 +456,7 @@ function advtrains.train_step_b(id, train, dtime) a = (v1 - train.velocity)/dtime --- 4b. Move train and update train properties --- local pdist = train.path_dist[math.floor(train.index)] or 1 - local distance = s / pdist + local distance = pdist == 0 and s or s / pdist train.lever = tmp_lever train.velocity = v1 train.acceleration = a -- cgit v1.2.3 From a2da32b536646085fdbf0a4d0714d49171093726 Mon Sep 17 00:00:00 2001 From: ywang Date: Mon, 16 Dec 2019 19:53:03 +0100 Subject: Stable code --- advtrains/helpers.lua | 16 ------ advtrains/lzb.lua | 125 ++++++++++++----------------------------------- advtrains/trainlogic.lua | 27 ++-------- 3 files changed, 35 insertions(+), 133 deletions(-) (limited to 'advtrains') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index 9afe72d..c398025 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -398,22 +398,6 @@ function advtrains.decode_pos(pts) return vector.new(dec(strx), dec(stry), dec(strz)) end --- Solve quadratic equations (i.e. a*x^2 + b*x + c = 0) -function advtrains.solve_quadratic_equation(a, b, c) - if not (a and b and c) then return nil end - if a == 0 then return {-c/b, -c/b} end -- avoid division by zero - local delta = (b*b - 4*a*c) - if delta < 0 then return {-b/2/a,-b/2/a} end -- ignore imaginary part - return {((-b+math.sqrt(delta))/2/a),((-b-math.sqrt(delta))/2/a)} -end - --- safe square root --- Negative return values indicate imaginary numbers. -function advtrains.safesqrt(a) - if a >= 0 then return math.sqrt(a) end - return 0 - math.sqrt(-a) -end - --[[ Benchmarking code local tdt = {} local tlt = {} diff --git a/advtrains/lzb.lua b/advtrains/lzb.lua index 30d7972..2574c51 100644 --- a/advtrains/lzb.lua +++ b/advtrains/lzb.lua @@ -82,100 +82,36 @@ local function look_ahead(id, train) end ---[[ -The .i element is the index at which LZB overrides the train control with the -lever specified by the index value. The .v element is the speed at which the -train control is taken over by LZB with the lever specified by the index. The .t -element calculates the time needed for the train to reach the point where the -control is taken over by LZB with the lever specified by the index. Unintialized -.v and .t values indicate that the train has passed the point with the -corresponding index. Note that thhe 0th item contains the data related to the -LZB point itself, and not related to the emergency brake. +--[[ Distance needed to accelerate for t (time) starting from v0 with acc. a: + at² +s = v0 * t + --- + 2 ]] -function advtrains.lzb_map_entry(train, lzb) - local ret = {[0]={},[1]={},[2]={},[3]={}} - if not (train and lzb) then return ret end - local ti = train.index +function advtrains.lzb_get_limit_by_entry(train, lzb, dtime) + if not (type(lzb)=="table") then return nil end + local getacc = advtrains.get_acceleration local v0 = train.velocity local v1 = lzb.spd - local a = advtrains.get_acceleration(train, train.lever) - local s = (v1*v1-v0*v0)/2/advtrains.get_acceleration(train, 1) - if v0 > 3 then s = s + params.ADD_FAST - elseif v0 <=0 then s = s + params.ADD_STAND - else s = s + params.ADD_SLOW - end - ret[0].i = lzb.idx - ret[1].i = advtrains.path_get_index_by_offset(train, ret[0].i, -s) - ret[2].i = advtrains.path_get_index_by_offset(train, ret[1].i, -params.ZONE_ROLL) - ret[3].i = advtrains.path_get_index_by_offset(train, ret[2].i, -params.ZONE_HOLD) - if a == 0 then ret[3].t = (ret[3].i)/v0 - else - ret[3].t = advtrains.solve_quadratic_equation(a/2, v0, (ti-ret[3].i)) - if not ret[3].t then ret[3].t = 0 - else - if ret[3].t[1]<0 then - if ret[3].t[2]<0 then ret[3].t = ret[3].t[2] - else ret[3].t = math.abs(math.max(ret[3].t[1], ret[3].t[2])) - end - else - if ret[3].t[2]<0 then ret[3].t = ret[3].t[1] - else ret[3].t = math.min(ret[3].t[1], ret[3].t[2]) - end - end - end - end - ret[3].v = (v0 + a*ret[3].t) - if ret[3].v <= lzb.spd then ret[3].v = lzb.spd end -- Avoid devision by zero - if ret[3].v > (train.max_speed or 10) then ret[3].v = train.max_speed or 0 end - ret[2].v = ret[3].v - ret[2].t = (ret[3].i-ret[2].i)/ret[3].v - ret[1].t = advtrains.solve_quadratic_equation(advtrains.get_acceleration(train,2),ret[2].v,(ret[2].i-ret[1].i)) - if not ret[1].t then ret[1].t = 0 - else - if ret[1].t[1]<0 then - if ret[1].t[2]<0 then ret[1].t = ret[1].t[2] - else ret[1].t = math.abs(math.max(ret[1].t[1], ret[1].t[2])) - end - else - if ret[1].t[2]<0 then ret[1].t = ret[1].t[1] - else ret[1].t = math.min(math.max(ret[1].t[1], ret[1].t[2])) - end - end - end - ret[1].v = (ret[2].v + advtrains.get_acceleration(train,2)*ret[1].t) - if ret[1].v <= lzb.spd then ret[1].v = lzb.spd end - ret[0].v = lzb.spd - ret[0].t = (ret[0].v-ret[1].v)/advtrains.get_acceleration(train,1) - return ret -end - ---[[ -advtrains.lzb_get_limit_by_entry - get the limit -Returns a table contraining the speed and the acceleration limits -]] -function advtrains.lzb_get_limit_by_entry(train, lzb) - local ret = {} - local lzbmap = advtrains.lzb_map_entry(train, lzb) - if not (lzbmap[3].i and lzbmap[2].i and lzbmap[1].i and lzbmap[0].i) then - return {} - elseif (lzbmap[3].i > train.index) then return {} - elseif (lzbmap[2].i > train.index) then ret.lever = 3 - elseif (lzbmap[1].i > train.index) then ret.lever = 2 - else ret.lever = 1 - end - if ret.lever == 3 then ret.velocity = lzbmap[3].v - else - local s = train.index - lzbmap[ret.lever].i - local a = advtrains.get_acceleration(train, ret.lever) - local v0 = lzbmap[ret.lever].v - ret.velocity = math.abs(advtrains.safesqrt(2*a*s - v0*v0)) - end - if ret.velocity < train.velocity -1 then ret.lever = ret.lever - 1 end - return ret + local s = (v1*v1-v0*v0)/2/getacc(train,1) + local t = dtime or 0.2 + local i = advtrains.path_get_index_by_offset(train, lzb.idx, -s) + if v0 > 3 then i = advtrains.path_get_index_by_offset(train, i, -params.ADD_FAST) + elseif v0 <= 0 then i = advtrains.path_get_index_by_offset(train, i, -params.ADD_STAND) + else i = advtrains.path_get_index_by_offset(train, i, -params.ADD_SLOW) end + i = advtrains.path_get_index_by_offset(train, i, -params.ZONE_ROLL) + i = advtrains.path_get_index_by_offset(train, i, -params.ZONE_HOLD) + if train.index + v0*t + getacc(train,4)*t*t/2 <= i then return 4 end + if train.index + v0*t <= i then return 3 end + i = advtrains.path_get_index_by_offset(train, i, params.ZONE_HOLD) + if train.index + v0*t + getacc(train,2)*t*t/2 <= i then return 2 end + i = advtrains.path_get_index_by_offset(train, i, params.ZONE_ROLL) + if train.index + v0*t + getacc(train,1)*t*t/2 <= i then return 1 end + return 0 end -- Get next LZB restriction with the lowest speed restriction -function advtrains.lzb_get_next(train) +-- The return values include the LZB entry and the speed limit +function advtrains.lzb_get_next(train,dtime) if lever == 4 then return nil end local lzb = train.lzb local i = 1 @@ -199,16 +135,17 @@ function advtrains.lzb_get_next(train) for _, it in ipairs(lzb.oncoming) do local v1 = it.spd if v1 and v1 <= v0 then - local curlimit = advtrains.lzb_get_limit_by_entry(train, it) - local retlimit = advtrains.lzb_get_limit_by_entry(train, ret) if not ret then ret = it - elseif not curlimit.velocity then - elseif retlimit.velocity > curlimit.velocity then - ret = it + else + local retlimit = advtrains.lzb_get_limit_by_entry(train,ret,dtime) + local curlimit = advtrains.lzb_get_limit_by_entry(train,ret,dtime) + if retlimit and retlimit > curlimit then ret=it + elseif retlimit == curlimit and it.idx < ret.idx then ret=it + end end end end - return ret + return ret,advtrains.lzb_get_limit_by_entry(train, ret,dtime) end local function invalidate(train) diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 441a11a..7999984 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -420,9 +420,9 @@ function advtrains.train_step_b(id, train, dtime) train.lever = tmp_lever --- 4a. Calculate movement --- - local lzbnxt = advtrains.lzb_get_next(train) - local lzblimit = advtrains.lzb_get_limit_by_entry(train, lzbnxt) - local lzbmap = advtrains.lzb_map_entry(train, lzbnxt) + local lzbnxt,lzblever = advtrains.lzb_get_next(train,dtime) + if lzblever and lzblever < tmp_lever then tmp_lever = lzblever train.ctrl.lzb = true + else train.ctrl.lzb = false end local a = advtrains.get_acceleration(train, tmp_lever) local v0 = train.velocity local v1 = a*dtime+v0 @@ -432,28 +432,9 @@ function advtrains.train_step_b(id, train, dtime) if a == 0 then s = v1*dtime else s = (v1*v1 - v0*v0)/2/a end - train.ctrl.lzb = nil - if lzblimit.velocity and lzblimit.lever < train.lever then - tmp_lever = lzblimit.lever - while (lzbmap[tmp_lever].t > dtime) do - tmp_lever = tmp_lever - 1 - end - train.ctrl.lzb = tmp_lever - a = advtrains.get_acceleration(train, tmp_lever) - v0 = lzbmap[tmp_lever].v - t = dtime - lzbmap[tmp_lever].t - v1 = a*t+v0 - v1 = math.min(v1, (train.max_speed or 10)) - v1 = math.max(v1, 0) - s = lzbmap[tmp_lever].i - train.index - if a == 0 then s = s + v1*t - else s = s + (v1*v1-v0*v0)/2/a - end - end -- FIX: calculate the average acceleration, as if it is static, to avoid -- weird wagon positions - -- Since v0 might have been changed, we should use train.velocity instead. - a = (v1 - train.velocity)/dtime + a = (v1 - v0)/dtime --- 4b. Move train and update train properties --- local pdist = train.path_dist[math.floor(train.index)] or 1 local distance = pdist == 0 and s or s / pdist -- cgit v1.2.3 From f4e3592b5bb6a1a7ad7ccc4fcceec9185fa9ea8a Mon Sep 17 00:00:00 2001 From: ywang Date: Mon, 16 Dec 2019 20:16:09 +0100 Subject: Remove dtime limiting --- advtrains/init.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'advtrains') diff --git a/advtrains/init.lua b/advtrains/init.lua index c5604fd..d3866ce 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -459,6 +459,7 @@ minetest.register_globalstep(function(dtime_mt) advtrains.load() end + --[[ local dtime if GENERATE_ATRICIFIAL_LAG then dtime = 0.2 @@ -476,6 +477,8 @@ minetest.register_globalstep(function(dtime_mt) end end + ]] + local dtime = dtime_mt advtrains.mainloop_trainlogic(dtime) if advtrains_itm_mainloop then advtrains_itm_mainloop(dtime) -- cgit v1.2.3 From 6bc8fa44df56d55baa0fa4e244f1f5660559cad7 Mon Sep 17 00:00:00 2001 From: ywang Date: Tue, 17 Dec 2019 20:11:45 +0100 Subject: Consider speed limit; minor improvments --- advtrains/trainlogic.lua | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'advtrains') diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 7999984..a28fadc 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -413,8 +413,12 @@ function advtrains.train_step_b(id, train, dtime) local tmp_lever = (train.ctrl.user or train.ctrl.atc) or 3 - if tarvel_cap and trainvelocity>tarvel_cap then - tmp_lever = 0 + if tarvel_cap then + if trainvelocity > tarvel_cap then + tmp_lever = 0 + elseif trainvelocity == tarvel_cap then + tmp_lever = 3 + end end train.lever = tmp_lever @@ -428,9 +432,13 @@ function advtrains.train_step_b(id, train, dtime) local v1 = a*dtime+v0 v1 = math.min(v1, (train.max_speed or 10)) v1 = math.max(v1, 0) + if tarvel_cap then v1 = math.min(v1, tarvel_cap) end local s if a == 0 then s = v1*dtime - else s = (v1*v1 - v0*v0)/2/a + else + s = (v1*v1 - v0*v0)/2/a + local acctime = (v1-v0)/a + if acctime < dtime then s = s + v1*(dtime - acctime) end end -- FIX: calculate the average acceleration, as if it is static, to avoid -- weird wagon positions -- cgit v1.2.3 From dae5a07eec6588e44d84c2126f9e483460c25571 Mon Sep 17 00:00:00 2001 From: ywang Date: Wed, 18 Dec 2019 15:04:30 +0100 Subject: HUD improvments --- advtrains/trainhud.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'advtrains') diff --git a/advtrains/trainhud.lua b/advtrains/trainhud.lua index d8b25ee..65a4982 100644 --- a/advtrains/trainhud.lua +++ b/advtrains/trainhud.lua @@ -9,7 +9,7 @@ advtrains.hhud[player:get_player_name()] = nil end) local mletter={[1]="F", [-1]="R", [0]="N"} -local doorstr={[-1]="|<>| >|<", [0]=">|< >|<", [1]=">|< |<>|"} +local doorstr={[-1]="<|> >|<", [0]=">|< >|<", [1]=">|< <|>"} function advtrains.on_control_change(pc, train, flip) local maxspeed = train.max_speed or 10 @@ -168,27 +168,27 @@ function advtrains.hud_train_format(train, flip) local vel = advtrains.abs_ceil(train.velocity) local vel_kmh=advtrains.abs_ceil(advtrains.ms_to_kmh(train.velocity)) - local levers = "B - o +" + local levers = " BB B - o + " local tlev=train.lever if train.velocity==0 and not train.active_control then tlev=1 end - if tlev == 0 then levers = ">BB< - o +" end - if tlev == 1 then levers = ">B< - o +" end - if tlev == 2 then levers = "B >-< o +" end - if tlev == 3 then levers = "B - >o< +" end - if tlev == 4 then levers = "B - o >+<" end + if tlev == 0 then levers = ">BB< B - o + " end + if tlev == 1 then levers = " BB >B< - o + " end + if tlev == 2 then levers = " BB B >-< o + " end + if tlev == 3 then levers = " BB B - >o< + " end + if tlev == 4 then levers = " BB B - o >+<" end local topLine, firstLine local secondLine if train.tarvelocity or train.atc_command then - local b=" " + local b=" " local tvels="" if train.tarvelocity then local tvel = advtrains.abs_ceil(train.tarvelocity) tvels = "|"..string.rep("+", tvel)..string.rep("_", max-tvel) end if train.atc_brake_target then - b="-B-" + b="-B" end local ad = "" if train.atc_delay then -- cgit v1.2.3 From b83dc400f0fffb291c66cb2dd74b8f9810aafd28 Mon Sep 17 00:00:00 2001 From: ywang Date: Wed, 18 Dec 2019 16:04:51 +0100 Subject: Fix failure to recognize braking --- advtrains/atc.lua | 10 +++++----- advtrains/trainlogic.lua | 26 +++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) (limited to 'advtrains') diff --git a/advtrains/atc.lua b/advtrains/atc.lua index fa9f097..c08acb3 100644 --- a/advtrains/atc.lua +++ b/advtrains/atc.lua @@ -186,18 +186,18 @@ end local matchptn={ ["SM"]=function(id, train) train.tarvelocity=train.max_speed + train.atc_brake_target=nil return 2 end, ["S([0-9]+)"]=function(id, train, match) train.tarvelocity=tonumber(match) + train.atc_brake_target=nil return #match+1 end, ["B([0-9]+)"]=function(id, train, match) - if train.velocity>tonumber(match) then - train.atc_brake_target=tonumber(match) - if not train.tarvelocity or train.tarvelocity>train.atc_brake_target then - train.tarvelocity=train.atc_brake_target - end + train.atc_brake_target=tonumber(match) + if not train.tarvelocity or train.tarvelocity>train.atc_brake_target then + train.tarvelocity=train.atc_brake_target end return #match+1 end, diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index a28fadc..8e1ac93 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -368,13 +368,16 @@ function advtrains.train_step_b(id, train, dtime) braketar = 0 emerg = true end + --[[ if braketar and braketar>=trainvelocity then train.atc_brake_target=nil braketar = nil end + ]] --if train.tarvelocity and train.velocity==train.tarvelocity then -- train.tarvelocity = nil --end + --[[ if train.atc_wait_finish then if not train.atc_brake_target and (not train.tarvelocity or train.velocity==train.tarvelocity) then train.atc_wait_finish=nil @@ -389,8 +392,10 @@ function advtrains.train_step_b(id, train, dtime) elseif train.atc_delay then train.atc_delay = nil end + ]] train.ctrl.atc = nil + if train.tarvelocity then train.ctrl.atc = 3 end if train.tarvelocity and train.tarvelocity>trainvelocity then train.ctrl.atc=4 end @@ -405,23 +410,37 @@ function advtrains.train_step_b(id, train, dtime) train.ctrl.atc=2 end end + if train.atc_wait_finish then + if (not train.ctrl.atc) or (train.ctrl.atc<3 and train.tarvelocity>=train.velocity) or (train.ctrl.atc==3) or (train.ctrl.atc>3 and train.velocity>=train.tarvelocity) then + train.atc_wait_finish=nil + end + end + if train.atc_command then + if (not train.atc_delay or train.atc_delay<=0) and not train.atc_wait_finish then + advtrains.atc.execute_atc_command(id, train) + else + train.atc_delay=train.atc_delay-dtime + end + elseif train.atc_delay then + train.atc_delay = nil + end end --if tarvel_cap and train.tarvelocity and tarvel_cap tarvel_cap then tmp_lever = 0 elseif trainvelocity == tarvel_cap then - tmp_lever = 3 + tmp_lever = math.min(3, tmp_lever) end end - train.lever = tmp_lever + --train.lever = tmp_lever --- 4a. Calculate movement --- local lzbnxt,lzblever = advtrains.lzb_get_next(train,dtime) @@ -433,6 +452,7 @@ function advtrains.train_step_b(id, train, dtime) v1 = math.min(v1, (train.max_speed or 10)) v1 = math.max(v1, 0) if tarvel_cap then v1 = math.min(v1, tarvel_cap) end + if tmp_lever == 4 and train.tarvelocity then v1=math.min(train.tarvelocity,v1) end local s if a == 0 then s = v1*dtime else -- cgit v1.2.3 From 1b24fb775736799d9cdb05b2bc4e43e0f783f94d Mon Sep 17 00:00:00 2001 From: ywang Date: Sun, 12 Jan 2020 15:36:17 +0100 Subject: Image optimizations --- advtrains/textures/advtrains_across.png | Bin 302 -> 139 bytes advtrains/textures/advtrains_across_anim.png | Bin 524 -> 302 bytes advtrains/textures/advtrains_boiler.png | Bin 413 -> 270 bytes advtrains/textures/advtrains_chimney.png | Bin 309 -> 157 bytes advtrains/textures/advtrains_couple.png | Bin 339 -> 165 bytes advtrains/textures/advtrains_cpl_lock.png | Bin 209 -> 119 bytes advtrains/textures/advtrains_cpl_unlock.png | Bin 213 -> 118 bytes advtrains/textures/advtrains_discouple.png | Bin 293 -> 152 bytes advtrains/textures/advtrains_driver_cab.png | Bin 352 -> 213 bytes advtrains/textures/advtrains_dtrack_atc_placer.png | Bin 1259 -> 681 bytes .../textures/advtrains_dtrack_bumper_placer.png | Bin 2213 -> 1067 bytes .../textures/advtrains_dtrack_detector_placer.png | Bin 1253 -> 625 bytes advtrains/textures/advtrains_dtrack_load_placer.png | Bin 1248 -> 678 bytes advtrains/textures/advtrains_dtrack_placer.png | Bin 1097 -> 591 bytes advtrains/textures/advtrains_dtrack_rail.png | Bin 4582 -> 3782 bytes advtrains/textures/advtrains_dtrack_shared.png | Bin 7141 -> 6660 bytes advtrains/textures/advtrains_dtrack_shared_atc.png | Bin 7215 -> 6747 bytes .../advtrains_dtrack_shared_detector_off.png | Bin 7180 -> 6679 bytes .../advtrains_dtrack_shared_detector_on.png | Bin 7181 -> 6684 bytes advtrains/textures/advtrains_dtrack_shared_load.png | Bin 7339 -> 6776 bytes .../textures/advtrains_dtrack_shared_unload.png | Bin 7338 -> 6776 bytes advtrains/textures/advtrains_dtrack_slopeplacer.png | Bin 2415 -> 2093 bytes .../textures/advtrains_dtrack_unload_placer.png | Bin 1260 -> 678 bytes advtrains/textures/advtrains_platform.png | Bin 193 -> 160 bytes advtrains/textures/advtrains_retrosignal.png | Bin 8496 -> 4357 bytes advtrains/textures/advtrains_retrosignal_inv.png | Bin 2242 -> 1707 bytes advtrains/textures/advtrains_signal_inv.png | Bin 856 -> 798 bytes advtrains/textures/advtrains_signal_off.png | Bin 5882 -> 1795 bytes advtrains/textures/advtrains_signal_on.png | Bin 5884 -> 1797 bytes advtrains/textures/advtrains_signal_wall_off.png | Bin 3056 -> 2577 bytes advtrains/textures/advtrains_signal_wall_on.png | Bin 3043 -> 2850 bytes advtrains/textures/advtrains_track_cr.png | Bin 33370 -> 33264 bytes advtrains/textures/advtrains_track_cr_45.png | Bin 33938 -> 33832 bytes advtrains/textures/advtrains_track_placer.png | Bin 32349 -> 32040 bytes advtrains/textures/advtrains_track_st.png | Bin 20405 -> 20220 bytes advtrains/textures/advtrains_track_st_45.png | Bin 39977 -> 39629 bytes advtrains/textures/advtrains_track_swlcr.png | Bin 33378 -> 33209 bytes advtrains/textures/advtrains_track_swlcr_45.png | Bin 45772 -> 45552 bytes advtrains/textures/advtrains_track_swlst.png | Bin 32321 -> 32166 bytes advtrains/textures/advtrains_track_swlst_45.png | Bin 46408 -> 46221 bytes advtrains/textures/advtrains_track_swrcr.png | Bin 33670 -> 33527 bytes advtrains/textures/advtrains_track_swrcr_45.png | Bin 46865 -> 46626 bytes advtrains/textures/advtrains_track_swrst.png | Bin 32654 -> 32519 bytes advtrains/textures/advtrains_track_swrst_45.png | Bin 47636 -> 47375 bytes advtrains/textures/advtrains_trackworker.png | Bin 328 -> 270 bytes advtrains/textures/advtrains_wagon_placeholder.png | Bin 723 -> 665 bytes advtrains/textures/advtrains_wheel.png | Bin 582 -> 297 bytes advtrains/textures/drwho_screwdriver.png | Bin 328 -> 270 bytes 48 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 advtrains/textures/advtrains_across.png mode change 100755 => 100644 advtrains/textures/advtrains_across_anim.png mode change 100755 => 100644 advtrains/textures/advtrains_boiler.png mode change 100755 => 100644 advtrains/textures/advtrains_chimney.png mode change 100755 => 100644 advtrains/textures/advtrains_couple.png mode change 100755 => 100644 advtrains/textures/advtrains_discouple.png mode change 100755 => 100644 advtrains/textures/advtrains_driver_cab.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_atc_placer.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_bumper_placer.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_detector_placer.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_load_placer.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_placer.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_rail.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_shared.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_shared_atc.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_shared_detector_off.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_shared_detector_on.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_shared_load.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_shared_unload.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_slopeplacer.png mode change 100755 => 100644 advtrains/textures/advtrains_dtrack_unload_placer.png mode change 100755 => 100644 advtrains/textures/advtrains_platform.png mode change 100755 => 100644 advtrains/textures/advtrains_retrosignal.png mode change 100755 => 100644 advtrains/textures/advtrains_retrosignal_inv.png mode change 100755 => 100644 advtrains/textures/advtrains_signal_inv.png mode change 100755 => 100644 advtrains/textures/advtrains_signal_off.png mode change 100755 => 100644 advtrains/textures/advtrains_signal_on.png mode change 100755 => 100644 advtrains/textures/advtrains_signal_wall_off.png mode change 100755 => 100644 advtrains/textures/advtrains_signal_wall_on.png mode change 100755 => 100644 advtrains/textures/advtrains_track_cr.png mode change 100755 => 100644 advtrains/textures/advtrains_track_cr_45.png mode change 100755 => 100644 advtrains/textures/advtrains_track_placer.png mode change 100755 => 100644 advtrains/textures/advtrains_track_st.png mode change 100755 => 100644 advtrains/textures/advtrains_track_st_45.png mode change 100755 => 100644 advtrains/textures/advtrains_track_swlcr.png mode change 100755 => 100644 advtrains/textures/advtrains_track_swlcr_45.png mode change 100755 => 100644 advtrains/textures/advtrains_track_swlst.png mode change 100755 => 100644 advtrains/textures/advtrains_track_swlst_45.png mode change 100755 => 100644 advtrains/textures/advtrains_track_swrcr.png mode change 100755 => 100644 advtrains/textures/advtrains_track_swrcr_45.png mode change 100755 => 100644 advtrains/textures/advtrains_track_swrst.png mode change 100755 => 100644 advtrains/textures/advtrains_track_swrst_45.png mode change 100755 => 100644 advtrains/textures/advtrains_trackworker.png mode change 100755 => 100644 advtrains/textures/advtrains_wheel.png mode change 100755 => 100644 advtrains/textures/drwho_screwdriver.png (limited to 'advtrains') diff --git a/advtrains/textures/advtrains_across.png b/advtrains/textures/advtrains_across.png old mode 100755 new mode 100644 index da65a61..a979224 Binary files a/advtrains/textures/advtrains_across.png and b/advtrains/textures/advtrains_across.png differ diff --git a/advtrains/textures/advtrains_across_anim.png b/advtrains/textures/advtrains_across_anim.png old mode 100755 new mode 100644 index 584d023..2df350a Binary files a/advtrains/textures/advtrains_across_anim.png and b/advtrains/textures/advtrains_across_anim.png differ diff --git a/advtrains/textures/advtrains_boiler.png b/advtrains/textures/advtrains_boiler.png old mode 100755 new mode 100644 index 8c7ff0d..5fc7742 Binary files a/advtrains/textures/advtrains_boiler.png and b/advtrains/textures/advtrains_boiler.png differ diff --git a/advtrains/textures/advtrains_chimney.png b/advtrains/textures/advtrains_chimney.png old mode 100755 new mode 100644 index 285a1a6..c94f722 Binary files a/advtrains/textures/advtrains_chimney.png and b/advtrains/textures/advtrains_chimney.png differ diff --git a/advtrains/textures/advtrains_couple.png b/advtrains/textures/advtrains_couple.png old mode 100755 new mode 100644 index eda3704..542e032 Binary files a/advtrains/textures/advtrains_couple.png and b/advtrains/textures/advtrains_couple.png differ diff --git a/advtrains/textures/advtrains_cpl_lock.png b/advtrains/textures/advtrains_cpl_lock.png index a25aaf4..f2f14d1 100644 Binary files a/advtrains/textures/advtrains_cpl_lock.png and b/advtrains/textures/advtrains_cpl_lock.png differ diff --git a/advtrains/textures/advtrains_cpl_unlock.png b/advtrains/textures/advtrains_cpl_unlock.png index f58d00a..3699091 100644 Binary files a/advtrains/textures/advtrains_cpl_unlock.png and b/advtrains/textures/advtrains_cpl_unlock.png differ diff --git a/advtrains/textures/advtrains_discouple.png b/advtrains/textures/advtrains_discouple.png old mode 100755 new mode 100644 index 5c064c3..ce6d210 Binary files a/advtrains/textures/advtrains_discouple.png and b/advtrains/textures/advtrains_discouple.png differ diff --git a/advtrains/textures/advtrains_driver_cab.png b/advtrains/textures/advtrains_driver_cab.png old mode 100755 new mode 100644 index 331bcc9..838e7e2 Binary files a/advtrains/textures/advtrains_driver_cab.png and b/advtrains/textures/advtrains_driver_cab.png differ diff --git a/advtrains/textures/advtrains_dtrack_atc_placer.png b/advtrains/textures/advtrains_dtrack_atc_placer.png old mode 100755 new mode 100644 index 31c2b30..e7cbe56 Binary files a/advtrains/textures/advtrains_dtrack_atc_placer.png and b/advtrains/textures/advtrains_dtrack_atc_placer.png differ diff --git a/advtrains/textures/advtrains_dtrack_bumper_placer.png b/advtrains/textures/advtrains_dtrack_bumper_placer.png old mode 100755 new mode 100644 index 27191fe..7bc9aeb Binary files a/advtrains/textures/advtrains_dtrack_bumper_placer.png and b/advtrains/textures/advtrains_dtrack_bumper_placer.png differ diff --git a/advtrains/textures/advtrains_dtrack_detector_placer.png b/advtrains/textures/advtrains_dtrack_detector_placer.png old mode 100755 new mode 100644 index e6c6ad6..c3b4fd9 Binary files a/advtrains/textures/advtrains_dtrack_detector_placer.png and b/advtrains/textures/advtrains_dtrack_detector_placer.png differ diff --git a/advtrains/textures/advtrains_dtrack_load_placer.png b/advtrains/textures/advtrains_dtrack_load_placer.png old mode 100755 new mode 100644 index 427c011..27db319 Binary files a/advtrains/textures/advtrains_dtrack_load_placer.png and b/advtrains/textures/advtrains_dtrack_load_placer.png differ diff --git a/advtrains/textures/advtrains_dtrack_placer.png b/advtrains/textures/advtrains_dtrack_placer.png old mode 100755 new mode 100644 index 7bef8a9..a5181eb Binary files a/advtrains/textures/advtrains_dtrack_placer.png and b/advtrains/textures/advtrains_dtrack_placer.png differ diff --git a/advtrains/textures/advtrains_dtrack_rail.png b/advtrains/textures/advtrains_dtrack_rail.png old mode 100755 new mode 100644 index bd0c217..75776be Binary files a/advtrains/textures/advtrains_dtrack_rail.png and b/advtrains/textures/advtrains_dtrack_rail.png differ diff --git a/advtrains/textures/advtrains_dtrack_shared.png b/advtrains/textures/advtrains_dtrack_shared.png old mode 100755 new mode 100644 index 736c7db..b968ba6 Binary files a/advtrains/textures/advtrains_dtrack_shared.png and b/advtrains/textures/advtrains_dtrack_shared.png differ diff --git a/advtrains/textures/advtrains_dtrack_shared_atc.png b/advtrains/textures/advtrains_dtrack_shared_atc.png old mode 100755 new mode 100644 index 1f83c37..6502d3b Binary files a/advtrains/textures/advtrains_dtrack_shared_atc.png and b/advtrains/textures/advtrains_dtrack_shared_atc.png differ diff --git a/advtrains/textures/advtrains_dtrack_shared_detector_off.png b/advtrains/textures/advtrains_dtrack_shared_detector_off.png old mode 100755 new mode 100644 index 724d907..564bbbb Binary files a/advtrains/textures/advtrains_dtrack_shared_detector_off.png and b/advtrains/textures/advtrains_dtrack_shared_detector_off.png differ diff --git a/advtrains/textures/advtrains_dtrack_shared_detector_on.png b/advtrains/textures/advtrains_dtrack_shared_detector_on.png old mode 100755 new mode 100644 index 7bb29d6..e844a2c Binary files a/advtrains/textures/advtrains_dtrack_shared_detector_on.png and b/advtrains/textures/advtrains_dtrack_shared_detector_on.png differ diff --git a/advtrains/textures/advtrains_dtrack_shared_load.png b/advtrains/textures/advtrains_dtrack_shared_load.png old mode 100755 new mode 100644 index 5fd0d7a..6a2396f Binary files a/advtrains/textures/advtrains_dtrack_shared_load.png and b/advtrains/textures/advtrains_dtrack_shared_load.png differ diff --git a/advtrains/textures/advtrains_dtrack_shared_unload.png b/advtrains/textures/advtrains_dtrack_shared_unload.png old mode 100755 new mode 100644 index e9fc5bd..18948e2 Binary files a/advtrains/textures/advtrains_dtrack_shared_unload.png and b/advtrains/textures/advtrains_dtrack_shared_unload.png differ diff --git a/advtrains/textures/advtrains_dtrack_slopeplacer.png b/advtrains/textures/advtrains_dtrack_slopeplacer.png old mode 100755 new mode 100644 index 1d456b0..2ed74c2 Binary files a/advtrains/textures/advtrains_dtrack_slopeplacer.png and b/advtrains/textures/advtrains_dtrack_slopeplacer.png differ diff --git a/advtrains/textures/advtrains_dtrack_unload_placer.png b/advtrains/textures/advtrains_dtrack_unload_placer.png old mode 100755 new mode 100644 index 486861e..4f13569 Binary files a/advtrains/textures/advtrains_dtrack_unload_placer.png and b/advtrains/textures/advtrains_dtrack_unload_placer.png differ diff --git a/advtrains/textures/advtrains_platform.png b/advtrains/textures/advtrains_platform.png old mode 100755 new mode 100644 index 5ba9663..8637e12 Binary files a/advtrains/textures/advtrains_platform.png and b/advtrains/textures/advtrains_platform.png differ diff --git a/advtrains/textures/advtrains_retrosignal.png b/advtrains/textures/advtrains_retrosignal.png old mode 100755 new mode 100644 index 141198d..4d1325f Binary files a/advtrains/textures/advtrains_retrosignal.png and b/advtrains/textures/advtrains_retrosignal.png differ diff --git a/advtrains/textures/advtrains_retrosignal_inv.png b/advtrains/textures/advtrains_retrosignal_inv.png old mode 100755 new mode 100644 index 1036594..767d505 Binary files a/advtrains/textures/advtrains_retrosignal_inv.png and b/advtrains/textures/advtrains_retrosignal_inv.png differ diff --git a/advtrains/textures/advtrains_signal_inv.png b/advtrains/textures/advtrains_signal_inv.png old mode 100755 new mode 100644 index ed64ed9..6638360 Binary files a/advtrains/textures/advtrains_signal_inv.png and b/advtrains/textures/advtrains_signal_inv.png differ diff --git a/advtrains/textures/advtrains_signal_off.png b/advtrains/textures/advtrains_signal_off.png old mode 100755 new mode 100644 index 8046e52..1ff5564 Binary files a/advtrains/textures/advtrains_signal_off.png and b/advtrains/textures/advtrains_signal_off.png differ diff --git a/advtrains/textures/advtrains_signal_on.png b/advtrains/textures/advtrains_signal_on.png old mode 100755 new mode 100644 index 5228bb3..8b9fe0d Binary files a/advtrains/textures/advtrains_signal_on.png and b/advtrains/textures/advtrains_signal_on.png differ diff --git a/advtrains/textures/advtrains_signal_wall_off.png b/advtrains/textures/advtrains_signal_wall_off.png old mode 100755 new mode 100644 index 3e7b1e1..3c78b4f Binary files a/advtrains/textures/advtrains_signal_wall_off.png and b/advtrains/textures/advtrains_signal_wall_off.png differ diff --git a/advtrains/textures/advtrains_signal_wall_on.png b/advtrains/textures/advtrains_signal_wall_on.png old mode 100755 new mode 100644 index b628c7e..271204d Binary files a/advtrains/textures/advtrains_signal_wall_on.png and b/advtrains/textures/advtrains_signal_wall_on.png differ diff --git a/advtrains/textures/advtrains_track_cr.png b/advtrains/textures/advtrains_track_cr.png old mode 100755 new mode 100644 index 40f0cc5..2d02abe Binary files a/advtrains/textures/advtrains_track_cr.png and b/advtrains/textures/advtrains_track_cr.png differ diff --git a/advtrains/textures/advtrains_track_cr_45.png b/advtrains/textures/advtrains_track_cr_45.png old mode 100755 new mode 100644 index 54966b3..42b7898 Binary files a/advtrains/textures/advtrains_track_cr_45.png and b/advtrains/textures/advtrains_track_cr_45.png differ diff --git a/advtrains/textures/advtrains_track_placer.png b/advtrains/textures/advtrains_track_placer.png old mode 100755 new mode 100644 index 03e17ed..9e47d90 Binary files a/advtrains/textures/advtrains_track_placer.png and b/advtrains/textures/advtrains_track_placer.png differ diff --git a/advtrains/textures/advtrains_track_st.png b/advtrains/textures/advtrains_track_st.png old mode 100755 new mode 100644 index 5ad7e4f..4d1a1a0 Binary files a/advtrains/textures/advtrains_track_st.png and b/advtrains/textures/advtrains_track_st.png differ diff --git a/advtrains/textures/advtrains_track_st_45.png b/advtrains/textures/advtrains_track_st_45.png old mode 100755 new mode 100644 index 63b4c96..81bb285 Binary files a/advtrains/textures/advtrains_track_st_45.png and b/advtrains/textures/advtrains_track_st_45.png differ diff --git a/advtrains/textures/advtrains_track_swlcr.png b/advtrains/textures/advtrains_track_swlcr.png old mode 100755 new mode 100644 index d9b5c0b..116a331 Binary files a/advtrains/textures/advtrains_track_swlcr.png and b/advtrains/textures/advtrains_track_swlcr.png differ diff --git a/advtrains/textures/advtrains_track_swlcr_45.png b/advtrains/textures/advtrains_track_swlcr_45.png old mode 100755 new mode 100644 index f098fc9..df85f86 Binary files a/advtrains/textures/advtrains_track_swlcr_45.png and b/advtrains/textures/advtrains_track_swlcr_45.png differ diff --git a/advtrains/textures/advtrains_track_swlst.png b/advtrains/textures/advtrains_track_swlst.png old mode 100755 new mode 100644 index 314bd2d..0ca2160 Binary files a/advtrains/textures/advtrains_track_swlst.png and b/advtrains/textures/advtrains_track_swlst.png differ diff --git a/advtrains/textures/advtrains_track_swlst_45.png b/advtrains/textures/advtrains_track_swlst_45.png old mode 100755 new mode 100644 index 765d0ec..28b2b37 Binary files a/advtrains/textures/advtrains_track_swlst_45.png and b/advtrains/textures/advtrains_track_swlst_45.png differ diff --git a/advtrains/textures/advtrains_track_swrcr.png b/advtrains/textures/advtrains_track_swrcr.png old mode 100755 new mode 100644 index f74e1bc..59451b8 Binary files a/advtrains/textures/advtrains_track_swrcr.png and b/advtrains/textures/advtrains_track_swrcr.png differ diff --git a/advtrains/textures/advtrains_track_swrcr_45.png b/advtrains/textures/advtrains_track_swrcr_45.png old mode 100755 new mode 100644 index fa432aa..f4c46c8 Binary files a/advtrains/textures/advtrains_track_swrcr_45.png and b/advtrains/textures/advtrains_track_swrcr_45.png differ diff --git a/advtrains/textures/advtrains_track_swrst.png b/advtrains/textures/advtrains_track_swrst.png old mode 100755 new mode 100644 index 06ea29e..290df45 Binary files a/advtrains/textures/advtrains_track_swrst.png and b/advtrains/textures/advtrains_track_swrst.png differ diff --git a/advtrains/textures/advtrains_track_swrst_45.png b/advtrains/textures/advtrains_track_swrst_45.png old mode 100755 new mode 100644 index be477b7..3832455 Binary files a/advtrains/textures/advtrains_track_swrst_45.png and b/advtrains/textures/advtrains_track_swrst_45.png differ diff --git a/advtrains/textures/advtrains_trackworker.png b/advtrains/textures/advtrains_trackworker.png old mode 100755 new mode 100644 index b50bcae..ad31643 Binary files a/advtrains/textures/advtrains_trackworker.png and b/advtrains/textures/advtrains_trackworker.png differ diff --git a/advtrains/textures/advtrains_wagon_placeholder.png b/advtrains/textures/advtrains_wagon_placeholder.png index 383c181..9771fa0 100644 Binary files a/advtrains/textures/advtrains_wagon_placeholder.png and b/advtrains/textures/advtrains_wagon_placeholder.png differ diff --git a/advtrains/textures/advtrains_wheel.png b/advtrains/textures/advtrains_wheel.png old mode 100755 new mode 100644 index fb72879..3ba16a8 Binary files a/advtrains/textures/advtrains_wheel.png and b/advtrains/textures/advtrains_wheel.png differ diff --git a/advtrains/textures/drwho_screwdriver.png b/advtrains/textures/drwho_screwdriver.png old mode 100755 new mode 100644 index b50bcae..ad31643 Binary files a/advtrains/textures/drwho_screwdriver.png and b/advtrains/textures/drwho_screwdriver.png differ -- cgit v1.2.3 From 2dd280b93146fad9edca072a1ccdeb2b7b979691 Mon Sep 17 00:00:00 2001 From: ywang Date: Sun, 12 Jan 2020 17:40:22 +0100 Subject: Fix bug with Point Speed Restriction Rail --- advtrains/lzb.lua | 23 ++++++++++++++++------- advtrains/trainlogic.lua | 1 + 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'advtrains') diff --git a/advtrains/lzb.lua b/advtrains/lzb.lua index 2574c51..794ad42 100644 --- a/advtrains/lzb.lua +++ b/advtrains/lzb.lua @@ -87,20 +87,29 @@ end s = v0 * t + --- 2 ]] +function advtrains.lzb_get_limit_zone(train, lzb, lever, vel) + local lvr = lever or train.lever + local v0 = vel or train.velocity + local v1 = lzb.spd + local s = (v1*v1-v0*v0)/2/advtrains.get_acceleration(train, 1) + if v0 > 3 then s = s + params.ADD_FAST + elseif v0 <= 0 then s = s + params.ADD_STAND + else s = s + params.ADD_SLOW + end + if lvr >= 2 then s = s + params.ZONE_HOLD end + if lvr >= 3 then s = s + params.ZONE_ROLL end + return advtrains.path_get_index_by_offset(train, lzb.idx, -s) +end + function advtrains.lzb_get_limit_by_entry(train, lzb, dtime) if not (type(lzb)=="table") then return nil end local getacc = advtrains.get_acceleration local v0 = train.velocity local v1 = lzb.spd - local s = (v1*v1-v0*v0)/2/getacc(train,1) local t = dtime or 0.2 - local i = advtrains.path_get_index_by_offset(train, lzb.idx, -s) - if v0 > 3 then i = advtrains.path_get_index_by_offset(train, i, -params.ADD_FAST) - elseif v0 <= 0 then i = advtrains.path_get_index_by_offset(train, i, -params.ADD_STAND) - else i = advtrains.path_get_index_by_offset(train, i, -params.ADD_SLOW) end - i = advtrains.path_get_index_by_offset(train, i, -params.ZONE_ROLL) - i = advtrains.path_get_index_by_offset(train, i, -params.ZONE_HOLD) + local i = advtrains.lzb_get_limit_zone(train, lzb, 4, v0 + getacc(train,4)) if train.index + v0*t + getacc(train,4)*t*t/2 <= i then return 4 end + i = advtrains.lzb_get_limit_zone(train, lzb, 3, v0) if train.index + v0*t <= i then return 3 end i = advtrains.path_get_index_by_offset(train, i, params.ZONE_HOLD) if train.index + v0*t + getacc(train,2)*t*t/2 <= i then return 2 end diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 8e1ac93..886abe8 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -457,6 +457,7 @@ function advtrains.train_step_b(id, train, dtime) if a == 0 then s = v1*dtime else s = (v1*v1 - v0*v0)/2/a + s = math.max(s, 0) local acctime = (v1-v0)/a if acctime < dtime then s = s + v1*(dtime - acctime) end end -- cgit v1.2.3 From 7033271a1073e1735dc558b4b4770e7a45564b44 Mon Sep 17 00:00:00 2001 From: ywang Date: Sun, 12 Jan 2020 18:11:18 +0100 Subject: Fix problem related to long distance between train and LZB point --- advtrains/lzb.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'advtrains') diff --git a/advtrains/lzb.lua b/advtrains/lzb.lua index 794ad42..da3b4b6 100644 --- a/advtrains/lzb.lua +++ b/advtrains/lzb.lua @@ -96,8 +96,10 @@ function advtrains.lzb_get_limit_zone(train, lzb, lever, vel) elseif v0 <= 0 then s = s + params.ADD_STAND else s = s + params.ADD_SLOW end - if lvr >= 2 then s = s + params.ZONE_HOLD end - if lvr >= 3 then s = s + params.ZONE_ROLL end + if v0 >= params.ZONE_VSLOW then + if lvr >= 2 then s = s + params.ZONE_HOLD end + if lvr >= 3 then s = s + params.ZONE_ROLL end + end return advtrains.path_get_index_by_offset(train, lzb.idx, -s) end -- cgit v1.2.3 From 4083f4270a674d56e067538c6876106f94faa60c Mon Sep 17 00:00:00 2001 From: ywang Date: Sat, 25 Jan 2020 21:25:02 +0100 Subject: explicitly pass train_id to atc.send_command --- advtrains/atc.lua | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'advtrains') diff --git a/advtrains/atc.lua b/advtrains/atc.lua index c08acb3..cc8f0b7 100644 --- a/advtrains/atc.lua +++ b/advtrains/atc.lua @@ -42,31 +42,31 @@ function atc.send_command(pos, par_tid) atlog("ATC controller at",pts,": This controller had an arrowconn of", atc.controllers[pts].arrowconn, "set. Since this field is now deprecated, it was removed.") atc.controllers[pts].arrowconn = nil end - + local train = advtrains.trains[train_id] local index = advtrains.path_lookup(train, pos) - + local iconnid = 1 if index then iconnid = train.path_cn[index] else atwarn("ATC rail at", pos, ": Rail not on train's path! Can't determine arrow direction. Assuming +!") end - - local command = atc.controllers[pts].command + + local command = atc.controllers[pts].command command = eval_conditional(command, iconnid==1, train.velocity) if not command then command="" end command=string.match(command, "^%s*(.*)$") - + if command == "" then atprint("Sending ATC Command to", train_id, ": Not modifying, conditional evaluated empty.") return true end - + atc.train_set_command(train, command, iconnid==1) atprint("Sending ATC Command to", train_id, ":", command, "iconnid=",iconnid) return true - + else atwarn("ATC rail at", pos, ": Sending command failed: The train",train_id,"does not exist. This seems to be a bug.") end @@ -123,7 +123,7 @@ advtrains.atc_function = function(def, preset, suffix, rotation) end local meta=minetest.get_meta(pos) if meta then - if not fields.save then + if not fields.save then --maybe only the dropdown changed if fields.mode then meta:set_string("mode", idxtrans[fields.mode]) @@ -146,7 +146,7 @@ advtrains.atc_function = function(def, preset, suffix, rotation) meta:set_string("infotext", attrans("ATC controller, mode @1\nCommand: @2", fields.mode, meta:get_string("command")) ) end meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta)) - + local pts=minetest.pos_to_string(pos) local _, conns=advtrains.get_rail_info_at(pos, advtrains.all_tracktypes) atc.controllers[pts]={command=fields.command} @@ -158,7 +158,7 @@ advtrains.atc_function = function(def, preset, suffix, rotation) end, advtrains = { on_train_enter = function(pos, train_id) - atc.send_command(pos) + atc.send_command(pos, train_id) end, }, } @@ -244,7 +244,7 @@ eval_conditional = function(command, arrow, speed) if cond=="-" then cond_applies=not arrow end - else + else cond, compare, rest=string.match(command, "^I([<>]=?)([0-9]+)(.+)$") if cond and compare then is_cond=true @@ -261,7 +261,7 @@ eval_conditional = function(command, arrow, speed) cond_applies=speed>=tonumber(compare) end end - end + end if is_cond then atprint("Evaluating if statement: "..command) atprint("Cond: "..(cond or "nil")) @@ -300,30 +300,30 @@ end function atc.execute_atc_command(id, train) --strip whitespaces local command=string.match(train.atc_command, "^%s*(.*)$") - - + + if string.match(command, "^%s*$") then train.atc_command=nil return end train.atc_command = eval_conditional(command, train.atc_arrow, train.velocity) - + if not train.atc_command then return end command=string.match(train.atc_command, "^%s*(.*)$") - + if string.match(command, "^%s*$") then train.atc_command=nil return end - + for pattern, func in pairs(matchptn) do local match=string.match(command, "^"..pattern) if match then local patlen=func(id, train, match) - + atprint("Executing: "..string.sub(command, 1, patlen)) - + train.atc_command=string.sub(command, patlen+1) if train.atc_delay<=0 and not train.atc_wait_finish then --continue (recursive, cmds shouldn't get too long, and it's a end-recursion.) -- cgit v1.2.3 From 40124d97f4e3279eb76d7d77bac15bf61373f1ac Mon Sep 17 00:00:00 2001 From: ywang Date: Tue, 7 Apr 2020 19:40:35 +0200 Subject: hostfix for trains ignoring ATC tracks --- advtrains/init.lua | 74 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 35 deletions(-) (limited to 'advtrains') diff --git a/advtrains/init.lua b/advtrains/init.lua index d3866ce..c3298a2 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -41,7 +41,7 @@ end function advtrains.pcall(fun) if no_action then return end - + local succ, return1, return2, return3, return4=xpcall(fun, function(err) atwarn("Lua Error occured: ", err) atwarn(debug.traceback()) @@ -140,11 +140,11 @@ if minetest.settings:get_bool("advtrains_enable_debugging") then if not context then return end local text=advtrains.print_concat_table({t, ...}) advtrains.drb_record(context, text) - + --atlog("@@",advtrains.atprint_context_tid,t,...) end dofile(advtrains.modpath.."/debugringbuffer.lua") - + end function assertt(var, typ) @@ -156,7 +156,7 @@ end dofile(advtrains.modpath.."/helpers.lua"); --dofile(advtrains.modpath.."/debugitems.lua"); -advtrains.meseconrules = +advtrains.meseconrules = {{x=0, y=0, z=-1}, {x=1, y=0, z=0}, {x=-1, y=0, z=0}, @@ -222,7 +222,7 @@ end function advtrains.avt_load() -- check for new, split advtrains save file - + local version = advtrains.read_component("version") local tbl if version and version == 3 then @@ -242,7 +242,7 @@ function advtrains.avt_load() wagon_save = true, ptmap = true, atc = true, - ndb = true, + ndb = true, lines = true, version = 2, } @@ -253,7 +253,7 @@ function advtrains.avt_load() tbl[i] = advtrains.read_component(i) end tbl["interlocking"] = il_save - else + else local file, err = io.open(advtrains.fpath, "r") if not file then minetest.log("warning", " Failed to read advtrains save data from file "..advtrains.fpath..": "..(err or "Unknown Error")) @@ -352,19 +352,19 @@ advtrains.save_component = function (tbl, name) file:write(datastr) file:close() os.rename(temppath, advtrains.fpath.."_"..name) - + end advtrains.avt_save = function(remove_players_from_wagons) --atprint("saving") - + if remove_players_from_wagons then for w_id, data in pairs(advtrains.wagons) do data.seatp={} end advtrains.player_to_train_mapping={} end - + local tmp_trains={} for id, train in pairs(advtrains.trains) do --first, deep_copy the train @@ -383,7 +383,7 @@ advtrains.avt_save = function(remove_players_from_wagons) advtrains.remove_train(id) end end - + for id, wdata in pairs(advtrains.wagons) do local _,proto = advtrains.get_wagon_prototype(wdata) if proto.has_inventory then @@ -397,7 +397,7 @@ advtrains.avt_save = function(remove_players_from_wagons) -- TODO temp wdata.dcpl_lock = nil end - + --versions: -- 1 - Initial new save format. -- 2 - version as of tss branch 11-2018+ @@ -426,11 +426,11 @@ advtrains.avt_save = function(remove_players_from_wagons) for i,k in pairs(save_tbl) do advtrains.save_component(k,i) end - + for i,k in pairs(il_save) do advtrains.save_component(k,"interlocking_"..i) end - + if DUMP_DEBUG_SAVE then local file, err = io.open(advtrains.fpath.."_DUMP", "w") if err then @@ -458,7 +458,7 @@ minetest.register_globalstep(function(dtime_mt) if not init_load then advtrains.load() end - + --[[ local dtime if GENERATE_ATRICIFIAL_LAG then @@ -466,7 +466,7 @@ minetest.register_globalstep(function(dtime_mt) if os.clock()0) do + local dtime = dtime_mt + if dtime > 0.2 then dtime = 0.2 end + advtrains.mainloop_trainlogic(dtime) + if advtrains_itm_mainloop then + advtrains_itm_mainloop(dtime) + end + if atlatc then + atlatc.mainloop_stepcode(dtime) + atlatc.interrupt.mainloop(dtime) + end + if advtrains.lines then + advtrains.lines.step(dtime) + end + dtime_mt = dtime_mt - dtime end - + --trigger a save when necessary - save_timer=save_timer-dtime + save_timer=save_timer-dtime_mt if save_timer<=0 then local t=os.clock() --save @@ -534,7 +539,7 @@ function advtrains.save(remove_players_from_wagons) atlatc.save() end atprint("[save_all]Saved advtrains save files") - + --TODO very simple yet hacky workaround for the "green signals" bug advtrains.invalidate_all_paths() end @@ -559,8 +564,8 @@ minetest.register_chatcommand("at_empty_seats", -- This chat command solves another problem: Trains getting randomly stuck. minetest.register_chatcommand("at_reroute", { - params = "", - description = "Delete all train routes, force them to recalculate", + params = "", + description = "Delete all train routes, force them to recalculate", privs = {train_operator=true}, -- Only train operator is required, since this is relatively safe. func = function(name, param) return advtrains.pcall(function() @@ -573,4 +578,3 @@ minetest.register_chatcommand("at_reroute", local tot=(os.clock()-lot)*1000 minetest.log("action", "[advtrains] Loaded in "..tot.."ms") - -- cgit v1.2.3