diff --git a/maps/mountain_fortress_v3/icw/functions.lua b/maps/mountain_fortress_v3/icw/functions.lua index 621763a8..a943d9ce 100644 --- a/maps/mountain_fortress_v3/icw/functions.lua +++ b/maps/mountain_fortress_v3/icw/functions.lua @@ -830,6 +830,59 @@ local function move_room_to_train(icw, train, wagon) end end +local function get_connected_rolling_stock(entity, direction, carriages) + --thanks Boskid + local first_stock, second_stock + for k, v in pairs(carriages) do + if v == entity then + first_stock = carriages[k - 1] + second_stock = carriages[k + 1] + break + end + end + if not first_stock then + first_stock, second_stock = second_stock, nil + end + if not first_stock then + return nil + end + + local angle = math.atan2(-(entity.position.x - first_stock.position.x), entity.position.y - first_stock.position.y) / (2 * math.pi) - entity.orientation + if direction == defines.rail_direction.back then + angle = angle + 0.5 + end + while angle < -0.5 do + angle = angle + 1 + end + while angle > 0.5 do + angle = angle - 1 + end + local connected_stock + if angle > -0.25 and angle < 0.25 then + connected_stock = first_stock + else + connected_stock = second_stock + end + if not connected_stock then + return nil + end + + angle = math.atan2(-(connected_stock.position.x - entity.position.x), connected_stock.position.y - entity.position.y) / (2 * math.pi) - connected_stock.orientation + while angle < -0.5 do + angle = angle + 1 + end + while angle > 0.5 do + angle = angle - 1 + end + local joint_of_connected_stock + if angle > -0.25 and angle < 0.25 then + joint_of_connected_stock = defines.rail_direction.front + else + joint_of_connected_stock = defines.rail_direction.back + end + return connected_stock, joint_of_connected_stock +end + function Public.construct_train(icw, locomotive, carriages) for i, carriage in pairs(carriages) do if carriage == locomotive then