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 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 void RemoveConveyorBlock(IMyConveyorEndpointBlock block) { if (block is MyShipConnector) { m_connectors.Remove(block as MyShipConnector); } if (IsClosing) { return; } if (OnBeforeRemoveEndpointBlock != null) { OnBeforeRemoveEndpointBlock(block); } for (int i = 0; i < block.ConveyorEndpoint.GetLineCount(); ++i) { MyConveyorLine line = block.ConveyorEndpoint.GetConveyorLine(i); line.DisconnectEndpoint(block.ConveyorEndpoint); if (line.IsDegenerate) { m_lines.Remove(line); } } }
private void UpdateLineReferences(MyConveyorLine oldLine, MyConveyorLine newLine) { for (int i = 0; i < 2; ++i) { if (oldLine.GetEndpoint(i) != null) { oldLine.GetEndpoint(i).SetConveyorLine(oldLine.GetEndpointPosition(i), newLine); } } foreach (var position in oldLine) { var block = m_grid.GetCubeBlock(position); if (block == null) { continue; } var segmentBlock = block.FatBlock as IMyConveyorSegmentBlock; Debug.Assert(segmentBlock != null, "Conveyor line was going through a non-segment block"); if (segmentBlock == null) { continue; } segmentBlock.ConveyorSegment.SetConveyorLine(newLine); } oldLine.RecalculateConductivity(); newLine.RecalculateConductivity(); }
public void RemoveSegmentBlock(IMyConveyorSegmentBlock segmentBlock) { if (IsClosing) { return; } if (OnBeforeRemoveSegmentBlock != null) { OnBeforeRemoveSegmentBlock(segmentBlock); } MyConveyorLine oldLine = segmentBlock.ConveyorSegment.ConveyorLine; MyConveyorLine newLine = segmentBlock.ConveyorSegment.ConveyorLine.RemovePortion(segmentBlock.ConveyorSegment.ConnectingPosition1.NeighbourGridPosition, segmentBlock.ConveyorSegment.ConnectingPosition2.NeighbourGridPosition); // Old line or new line can be empty after splitting. If the new line would be empty, Split(...) will return null // We have to unregister only old line if (oldLine.IsDegenerate) { m_lines.Remove(oldLine); } if (newLine == null) { return; } UpdateLineReferences(newLine, newLine); // The new line will always need to be registered Debug.Assert(!newLine.IsDegenerate); m_lines.Add(newLine); }
public void InitializeConveyorSegment() { MyConveyorLine.BlockLinePositionInformation[] blockLinePositions = MyConveyorLine.GetBlockLinePositions(this); if (blockLinePositions.Length != 0) { ConveyorLinePosition connectingPosition = this.PositionToGridCoords(blockLinePositions[0].Position).GetConnectingPosition(); this.m_segment.Init(this, connectingPosition, this.PositionToGridCoords(blockLinePositions[1].Position).GetConnectingPosition(), blockLinePositions[0].LineType, MyObjectBuilder_ConveyorLine.LineConductivity.FULL); } }
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(); }
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); } }
public void SetConveyorLine(ConveyorLinePosition position, MyConveyorLine newLine) { ConveyorLinePosition[] positions = GetLinePositions(); for (int i = 0; i < positions.Length; ++i) { var gridPosition = PositionToGridCoords(positions[i]); if (gridPosition.Equals(position)) { m_conveyorLines[i] = newLine; return; } } return; }
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); }
public MyMultilineConveyorEndpoint(MyCubeBlock myBlock) { ProfilerShort.Begin("MyMultilineConveyorEndpoint(...)"); m_block = myBlock; MyConveyorLine.BlockLinePositionInformation[] positionInfo = MyConveyorLine.GetBlockLinePositions(myBlock); m_conveyorLines = new MyConveyorLine[positionInfo.Length]; MyGridConveyorSystem conveyorSystem = myBlock.CubeGrid.GridSystems.ConveyorSystem; int i = 0; foreach (var position in positionInfo) { var gridPosition = PositionToGridCoords(position.Position); MyConveyorLine line = conveyorSystem.GetDeserializingLine(gridPosition); if (line == null) { line = new MyConveyorLine(); line.Init(gridPosition, gridPosition.GetConnectingPosition(), myBlock.CubeGrid, position.LineType, position.LineConductivity); line.InitEndpoints(this, null); } else { if (line.GetEndpointPosition(0).Equals(gridPosition)) { line.SetEndpoint(0, this); } else if (line.GetEndpointPosition(1).Equals(gridPosition)) { line.SetEndpoint(1, this); } } m_conveyorLines[i] = line; i++; } myBlock.SlimBlock.ComponentStack.IsFunctionalChanged += UpdateLineFunctionality; myBlock.CubeGrid.GridSystems.ConveyorSystem.ResourceSink.IsPoweredChanged += UpdateLineFunctionality; m_pathfindingData = new MyPathfindingData(this); 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); }
private void UpdateLineReferences(MyConveyorLine oldLine, MyConveyorLine newLine) { for (int i = 0; i < 2; ++i) { if (oldLine.GetEndpoint(i) != null) { oldLine.GetEndpoint(i).SetConveyorLine(oldLine.GetEndpointPosition(i), newLine); } } foreach (var position in oldLine) { var block = m_grid.GetCubeBlock(position); if (block == null) continue; var segmentBlock = block.FatBlock as IMyConveyorSegmentBlock; Debug.Assert(segmentBlock != null, "Conveyor line was going through a non-segment block"); if (segmentBlock == null) continue; segmentBlock.ConveyorSegment.SetConveyorLine(newLine); } oldLine.RecalculateConductivity(); newLine.RecalculateConductivity(); }
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(); }
public static void DebugDraw(this IMyConveyorEndpoint endpoint) { if (!MyDebugDrawSettings.DEBUG_DRAW_CONVEYORS) { return; } Vector3 centerPos = new Vector3(); for (int i = 0; i < endpoint.GetLineCount(); ++i) { var position = endpoint.GetPosition(i); Vector3 pos = new Vector3(position.LocalGridPosition) + 0.5f * new Vector3(position.VectorDirection); centerPos += pos; } centerPos = centerPos * endpoint.CubeBlock.CubeGrid.GridSize / (float)endpoint.GetLineCount(); centerPos = Vector3.Transform(centerPos, endpoint.CubeBlock.CubeGrid.WorldMatrix); for (int i = 0; i < endpoint.GetLineCount(); ++i) { var position = endpoint.GetPosition(i); MyConveyorLine line = endpoint.GetConveyorLine(i); Vector3 pos = (new Vector3(position.LocalGridPosition) + 0.5f * new Vector3(position.VectorDirection)) * endpoint.CubeBlock.CubeGrid.GridSize; Vector3 pos2 = (new Vector3(position.LocalGridPosition) + 0.4f * new Vector3(position.VectorDirection)) * endpoint.CubeBlock.CubeGrid.GridSize; pos = Vector3.Transform(pos, endpoint.CubeBlock.CubeGrid.WorldMatrix); pos2 = Vector3.Transform(pos2, endpoint.CubeBlock.CubeGrid.WorldMatrix); Vector3 dir = Vector3.TransformNormal(position.VectorDirection * endpoint.CubeBlock.CubeGrid.GridSize * 0.5f, endpoint.CubeBlock.CubeGrid.WorldMatrix); Color color = line.IsFunctional ? Color.Orange : Color.DarkRed; color = line.IsWorking ? Color.GreenYellow : color; EndpointDebugShape shape = EndpointDebugShape.SHAPE_SPHERE; float dirMultiplier = 1.0f; float radius = 0.05f; if (line.GetEndpoint(0) == null || line.GetEndpoint(1) == null) { if (line.Type == Common.ObjectBuilders.MyObjectBuilder_ConveyorLine.LineType.SMALL_LINE) { dirMultiplier = 0.2f; radius = 0.015f; shape = EndpointDebugShape.SHAPE_SPHERE; } else { dirMultiplier = 0.1f; radius = 0.015f; shape = EndpointDebugShape.SHAPE_CAPSULE; } } else { if (line.Type == Common.ObjectBuilders.MyObjectBuilder_ConveyorLine.LineType.SMALL_LINE) { dirMultiplier = 1.0f; radius = 0.05f; shape = EndpointDebugShape.SHAPE_SPHERE; } else { dirMultiplier = 0.2f; radius = 0.05f; shape = EndpointDebugShape.SHAPE_CAPSULE; } } MyRenderProxy.DebugDrawLine3D(pos, pos + dir * dirMultiplier, color, color, true); if (shape == EndpointDebugShape.SHAPE_SPHERE) { MyRenderProxy.DebugDrawSphere(pos, radius * endpoint.CubeBlock.CubeGrid.GridSize, color.ToVector3(), 1.0f, false); } else if (shape == EndpointDebugShape.SHAPE_CAPSULE) { MyRenderProxy.DebugDrawCapsule(pos - dir * dirMultiplier, pos + dir * dirMultiplier, radius * endpoint.CubeBlock.CubeGrid.GridSize, color, false); } if (MyDebugDrawSettings.DEBUG_DRAW_CONVEYORS_LINE_IDS) { MyRenderProxy.DebugDrawText3D(pos2, line.GetHashCode().ToString(), color, 0.6f, false); } MyRenderProxy.DebugDrawLine3D(pos, centerPos, color, color, false); } }
public void SetConveyorLine(MyConveyorLine newLine) { ConveyorLine = newLine; }
public void SetConveyorLine(MyConveyorLine newLine) { ConveyorLine = newLine; }