From b13c7259763aa5c150e25c894a621a59b7bd37d4 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Wed, 10 Jun 2020 20:56:35 +0200 Subject: Workaround to handle split points (dt. aufgefahrene Weichen), fixing H#77 --- advtrains/helpers.lua | 6 +++++- advtrains/init.lua | 2 +- advtrains/path.lua | 16 ++++++++++++++-- advtrains/trainlogic.lua | 24 ++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index e04991e..782a5c5 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -291,8 +291,9 @@ function advtrains.conn_matches_to(conn, other_conns) return false end - +-- Going from the rail at pos (does not need to be rounded) along connection with id conn_idx, if there is a matching rail, return it and the matching connid -- returns: , , , +-- parameter this_conns_p is connection table of this rail and is optional, is determined by get_rail_info_at if not provided. function advtrains.get_adjacent_rail(this_posnr, this_conns_p, conn_idx, drives_on) local this_pos = advtrains.round_vector_floor_y(this_posnr) local this_conns = this_conns_p @@ -332,6 +333,9 @@ function advtrains.get_adjacent_rail(this_posnr, this_conns_p, conn_idx, drives_ return nil end +-- when a train enters a rail on connid 'conn', which connid will it go out? +-- nconns: number of connections in connection table +-- returns: connid_out local connlku={[2]={2,1}, [3]={2,1,1}, [4]={2,1,4,3}} function advtrains.get_matching_conn(conn, nconns) return connlku[nconns][conn] diff --git a/advtrains/init.lua b/advtrains/init.lua index 99e81ed..bdb0bf9 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -392,7 +392,7 @@ advtrains.avt_save = function(remove_players_from_wagons) "trainparts", "recently_collided_with_env", "atc_brake_target", "atc_wait_finish", "atc_command", "atc_delay", "door_open", "text_outside", "text_inside", "line", "routingcode", - "il_sections", "speed_restriction", "is_shunt", + "il_sections", "speed_restriction", "is_shunt", "points_split", }) --then save it tmp_trains[id]=v diff --git a/advtrains/path.lua b/advtrains/path.lua index ff034c9..b132e92 100644 --- a/advtrains/path.lua +++ b/advtrains/path.lua @@ -178,9 +178,15 @@ function advtrains.path_get(train, index) if adj_pos then advtrains.occ.set_item(train.id, adj_pos, pef) + -- If we have split points, notify accordingly + local mconnid = advtrains.get_matching_conn(adj_connid, #next_conns) + if #next_conns==3 and adj_connid==1 and train.points_split and train.points_split[advtrains.encode_pos(adj_pos)] then + --atdebug(id,"has split points restored at",adj_pos) + mconnid = 3 + end + adj_pos.y = adj_pos.y + nextrail_y train.path_cp[pef] = adj_connid - local mconnid = advtrains.get_matching_conn(adj_connid, #next_conns) train.path_cn[pef] = mconnid train.path_dir[pef] = advtrains.conn_angle_median(next_conns[adj_connid].c, next_conns[mconnid].c) train.path_trk_f = pef @@ -211,9 +217,15 @@ function advtrains.path_get(train, index) if adj_pos then advtrains.occ.set_item(train.id, adj_pos, peb) + -- If we have split points, notify accordingly + local mconnid = advtrains.get_matching_conn(adj_connid, #next_conns) + if #next_conns==3 and adj_connid==1 and train.points_split and train.points_split[advtrains.encode_pos(adj_pos)] then + atdebug(id,"has split points restored at",adj_pos) + mconnid = 3 + end + adj_pos.y = adj_pos.y + nextrail_y train.path_cn[peb] = adj_connid - local mconnid = advtrains.get_matching_conn(adj_connid, #next_conns) train.path_cp[peb] = mconnid train.path_dir[peb] = advtrains.conn_angle_median(next_conns[mconnid].c, next_conns[adj_connid].c) train.path_trk_b = peb diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 6198c18..1ae96bd 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -627,6 +627,16 @@ local function tnc_call_enter_callback(pos, train_id, train, index) -- call other registered callbacks run_callbacks_enter_node(pos, train_id, train, index) + + -- check for split points + if mregnode and mregnode.at_conns and #mregnode.at_conns == 3 and train.path_cp[index] == 3 then + -- train came from connection 3 of a switch, so it split points. + if not train.points_split then + train.points_split = {} + end + train.points_split[advtrains.encode_pos(pos)] = true + --atdebug(train_id,"split points at",pos) + end end local function tnc_call_leave_callback(pos, train_id, train, index) --atdebug("tnc leave",pos,train_id) @@ -638,6 +648,20 @@ local function tnc_call_leave_callback(pos, train_id, train, index) -- call other registered callbacks run_callbacks_leave_node(pos, train_id, train, index) + + -- split points do not matter anymore. clear them + if train.points_split then + if train.points_split[advtrains.encode_pos(pos)] then + train.points_split[advtrains.encode_pos(pos)] = nil + --atdebug(train_id,"has passed split points at",pos) + end + -- any entries left? + for _,_ in pairs(train.points_split) do + return + end + train.points_split = nil + end + -- WARNING possibly unreachable place! end function advtrains.tnc_call_approach_callback(pos, train_id, train, index, lzbdata) -- cgit v1.2.3