public void Init(MyCubeBlock myBlock, ConveyorLinePosition a, ConveyorLinePosition b, MyObjectBuilder_ConveyorLine.LineType type, MyObjectBuilder_ConveyorLine.LineConductivity conductivity = MyObjectBuilder_ConveyorLine.LineConductivity.FULL)
        {
            CubeBlock = myBlock;
            ConnectingPosition1 = a;
            ConnectingPosition2 = b;

            // Neighbour grid position of one of the connecting positions is inside this block
            var linePosition = (myBlock as IMyConveyorSegmentBlock).ConveyorSegment.ConnectingPosition1.NeighbourGridPosition;

            ConveyorLine = myBlock.CubeGrid.GridSystems.ConveyorSystem.GetDeserializingLine(linePosition);
            if (ConveyorLine == null)
            {
                ConveyorLine = new MyConveyorLine();
                if (IsCorner)
                    ConveyorLine.Init(a, b, myBlock.CubeGrid, type, conductivity, CalculateCornerPosition());
                else
                    ConveyorLine.Init(a, b, myBlock.CubeGrid, type, conductivity, (Vector3I?)null);
            }
            else
            {
                Debug.Assert(ConveyorLine.Type == type, "Conveyor line type mismatch on segment deserialization");
            }

            myBlock.SlimBlock.ComponentStack.IsFunctionalChanged += CubeBlock_IsFunctionalChanged;
        }
 public bool CanConnectTo(ConveyorLinePosition connectingPosition, MyObjectBuilder_ConveyorLine.LineType type)
 {
     if (type == ConveyorLine.Type &&
            (connectingPosition.Equals(ConnectingPosition1.GetConnectingPosition()) ||
             connectingPosition.Equals(ConnectingPosition2.GetConnectingPosition()))
        ) return true;
     return false;
 }
        private ConveyorLinePosition PositionToGridCoords(ConveyorLinePosition position)
        {
            ConveyorLinePosition retval = new ConveyorLinePosition();

            Matrix matrix = new Matrix();
            this.Orientation.GetMatrix(out matrix);
            Vector3 transformedPosition = Vector3.Transform(new Vector3(position.LocalGridPosition), matrix);

            retval.LocalGridPosition = Vector3I.Round(transformedPosition) + this.Position;
            retval.Direction = this.Orientation.TransformDirection(position.Direction);

            return retval;
        }
Beispiel #4
0
        public void InitializeConveyorSegment()
        {
            MyConveyorLine.BlockLinePositionInformation[] positionInfo = MyConveyorLine.GetBlockLinePositions(this);

            Debug.Assert(positionInfo.Length == 2, "Dummies not correctly defined for conveyor frame");

            if (positionInfo.Length > 0)
            {
                ConveyorLinePosition position1 = PositionToGridCoords(positionInfo[0].Position).GetConnectingPosition();
                ConveyorLinePosition position2 = PositionToGridCoords(positionInfo[1].Position).GetConnectingPosition();
                Debug.Assert(positionInfo[0].LineType == positionInfo[1].LineType, "Inconsistent conveyor line type in conveyor segment block model");

                m_segment.Init(this, position1, position2, positionInfo[0].LineType);
            }
        }
        private void AddSegmentBlockInternal(IMyConveyorSegmentBlock segmentBlock, ConveyorLinePosition connectingPosition)
        {
            var otherBlock = m_grid.GetCubeBlock(connectingPosition.LocalGridPosition);

            if (otherBlock != null)
            {
                if (m_deserializedLines != null && m_deserializedLines.Contains(segmentBlock.ConveyorSegment.ConveyorLine))
                {
                    return;
                }

                var otherConveyorBlock = otherBlock.FatBlock as IMyConveyorEndpointBlock;
                var otherSegmentBlock  = otherBlock.FatBlock as IMyConveyorSegmentBlock;

                if (otherSegmentBlock != null)
                {
                    var oldLine = segmentBlock.ConveyorSegment.ConveyorLine;
                    if (m_lines.Contains(oldLine))
                    {
                        m_lines.Remove(oldLine);
                    }

                    if (otherSegmentBlock.ConveyorSegment.CanConnectTo(connectingPosition, segmentBlock.ConveyorSegment.ConveyorLine.Type))
                    {
                        MergeSegmentSegment(segmentBlock, otherSegmentBlock);
                    }
                }
                if (otherConveyorBlock != null)
                {
                    var oldLine = otherConveyorBlock.ConveyorEndpoint.GetConveyorLine(connectingPosition);
                    if (TryMergeEndpointSegment(otherConveyorBlock, segmentBlock, connectingPosition))
                    {
                        m_lines.Remove(oldLine);
                    }
                }
            }
        }
        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;
        }
        private void AddSegmentBlockInternal(IMyConveyorSegmentBlock segmentBlock, ConveyorLinePosition connectingPosition)
        {
            var otherBlock = m_grid.GetCubeBlock(connectingPosition.LocalGridPosition);
            if (otherBlock != null)
            {
                if (m_deserializedLines != null && m_deserializedLines.Contains(segmentBlock.ConveyorSegment.ConveyorLine))
                    return;

                var otherConveyorBlock = otherBlock.FatBlock as IMyConveyorEndpointBlock;
                var otherSegmentBlock = otherBlock.FatBlock as IMyConveyorSegmentBlock;

                if (otherSegmentBlock != null)
                {
                    var oldLine = segmentBlock.ConveyorSegment.ConveyorLine;
                    if (m_lines.Contains(oldLine))
                    {
                        m_lines.Remove(oldLine);
                    }

                    if (otherSegmentBlock.ConveyorSegment.CanConnectTo(connectingPosition, segmentBlock.ConveyorSegment.ConveyorLine.Type))
                        MergeSegmentSegment(segmentBlock, otherSegmentBlock);
                }
                if (otherConveyorBlock != null)
                {
                    var oldLine = otherConveyorBlock.ConveyorEndpoint.GetConveyorLine(connectingPosition);
                    if (TryMergeEndpointSegment(otherConveyorBlock, segmentBlock, connectingPosition))
                    {
                        m_lines.Remove(oldLine);
                    }
                }
            }
        }
        public MyConveyorLine GetDeserializingLine(ConveyorLinePosition position)
        {
            if (m_lineEndpoints == null) return null;

            MyConveyorLine retval;
            m_lineEndpoints.TryGetValue(position, out retval);
            return retval;
        }
        public void BeforeBlockDeserialization(List<MyObjectBuilder_ConveyorLine> lines)
        {
            ProfilerShort.Begin("ConveyorSystem.BeforeBlockDeserialization()");
            if (lines == null)
            {
                ProfilerShort.End();
                return;
            }

            m_lineEndpoints = new Dictionary<ConveyorLinePosition, MyConveyorLine>(lines.Count * 2);
            m_linePoints = new Dictionary<Vector3I, MyConveyorLine>(lines.Count * 4);
            m_deserializedLines = new HashSet<MyConveyorLine>();

            foreach (var lineBuilder in lines)
            {
                MyConveyorLine line = new MyConveyorLine();
                line.Init(lineBuilder, m_grid);
                if (!line.CheckSectionConsistency()) continue;

                ConveyorLinePosition start = new ConveyorLinePosition(lineBuilder.StartPosition, lineBuilder.StartDirection);
                ConveyorLinePosition end = new ConveyorLinePosition(lineBuilder.EndPosition, lineBuilder.EndDirection);

                try
                {
                    m_lineEndpoints.Add(start, line);
                    m_lineEndpoints.Add(end, line);

                    foreach (var position in line)
                    {
                        m_linePoints.Add(position, line);
                    }

                    m_deserializedLines.Add(line);
                    m_lines.Add(line);
                }
                catch (ArgumentException)
                {
                    // Something was wrong in the conveyor line serialization. Display an assert, but don't crash.
                    Debug.Assert(false, "Problem with deserializing lines. Recalculating all lines from scratch...");
                    // Reset the deserialization structures and rebuild the conveyor lines anew
                    m_lineEndpoints = null;
                    m_deserializedLines = null;
                    m_linePoints = null;
                    m_lines.Clear();
                    break;
                }
            }
            ProfilerShort.End();
        }
        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);
        }