private void MergeSegmentSegment(IMyConveyorSegmentBlock newSegmentBlock, IMyConveyorSegmentBlock oldSegmentBlock)
        {
            MyConveyorLine line1 = newSegmentBlock.ConveyorSegment.ConveyorLine;
            MyConveyorLine line2 = oldSegmentBlock.ConveyorSegment.ConveyorLine;

            // Creating a cycle - no need to merge anything
            if (line1 != line2)
            {
                line2.Merge(line1, newSegmentBlock);
            }

            UpdateLineReferences(line1, line2);

            // newSegmentBlock is a newly created block, so we have to update its line reference as well
            newSegmentBlock.ConveyorSegment.SetConveyorLine(line2);
        }
        private bool TryMergeEndpointEndpoint(IMyConveyorEndpointBlock endpointBlock1, IMyConveyorEndpointBlock endpointBlock2, ConveyorLinePosition pos1, ConveyorLinePosition pos2)
        {
            MyConveyorLine line1 = endpointBlock1.ConveyorEndpoint.GetConveyorLine(pos1);

            if (line1 == null)
            {
                return(false);
            }

            MyConveyorLine line2 = endpointBlock2.ConveyorEndpoint.GetConveyorLine(pos2);

            if (line2 == null)
            {
                return(false);
            }

            if (line1.Type != line2.Type)
            {
                return(false);
            }

            if (line1.GetEndpoint(1) == null)
            {
                line1.Reverse();
            }
            Debug.Assert(line1.GetEndpoint(1) != null);
            if (line2.GetEndpoint(0) == null)
            {
                line2.Reverse();
            }
            Debug.Assert(line2.GetEndpoint(0) != null);

            line2.Merge(line1);
            endpointBlock1.ConveyorEndpoint.SetConveyorLine(pos1, line2);
            line1.RecalculateConductivity();
            line2.RecalculateConductivity();

            return(true);
        }
        /// <summary>
        /// Tries to merge the conveyor lines of a conveyor block and segment block.
        /// Also changes the reference in the endpoint block to the correct line.
        /// </summary>
        private bool TryMergeEndpointSegment(IMyConveyorEndpointBlock endpoint, IMyConveyorSegmentBlock segmentBlock, ConveyorLinePosition endpointPosition)
        {
            MyConveyorLine endpointLine = endpoint.ConveyorEndpoint.GetConveyorLine(endpointPosition);

            if (endpointLine == null)
            {
                return(false);
            }

            // The conveyor segment cannot merge with the given endpoint
            if (!segmentBlock.ConveyorSegment.CanConnectTo(endpointPosition.GetConnectingPosition(), endpointLine.Type))
            {
                return(false);
            }

            MyConveyorLine segmentLine = segmentBlock.ConveyorSegment.ConveyorLine;

            segmentLine.Merge(endpointLine, segmentBlock);
            endpoint.ConveyorEndpoint.SetConveyorLine(endpointPosition, segmentLine);
            endpointLine.RecalculateConductivity();
            segmentLine.RecalculateConductivity();
            return(true);
        }