/*public static void Apply(HarmonyInstance harmony)
         * {
         *  var fix = typeof(BuildingDecorationDetour).GetMethod("LoadPaths");
         *  harmony.Patch(OriginalMethod, new HarmonyMethod(fix), null, null);
         *
         * }*/

        /*public static void Revert(HarmonyInstance harmony)
         * {
         *  harmony.Unpatch(OriginalMethod, HarmonyPatchType.Prefix);
         * }*/

        /*private static MethodInfo OriginalMethod => typeof(BuildingDecoration).GetMethod("LoadPaths");*/

        #endregion DETOUR

        private static HashSet <ConnectionPoint> ReleaseCollidingSegments()
        {
            // We obtain a list of nodes adjacent to the deleted segment to know where to reconnect
            HashSet <ConnectionPoint> borderNodes = new HashSet <ConnectionPoint>();

            if (ToolControllerDetour.CollidingSegmentsCache2 == null)
            {
                return(null);
            }
            foreach (ushort segment in ToolControllerDetour.CollidingSegmentsCache2)
            {
                //Debug.Log("Releasing segment " + segment);
                NetSegment netSegment = NetAccess.GetSegment(segment);

                // We keep untouchable segments
                if ((netSegment.m_flags & NetSegment.Flags.Untouchable) != NetSegment.Flags.None)
                {
                    continue;
                }

                bool inverted = ((netSegment.m_flags & NetSegment.Flags.Invert) != NetSegment.Flags.None);

                borderNodes.Add(new ConnectionPoint(netSegment.m_startNode, netSegment.m_startDirection, netSegment.Info, inverted));
                borderNodes.Add(new ConnectionPoint(netSegment.m_endNode, netSegment.m_endDirection, netSegment.Info, !inverted));
                NetAccess.ReleaseSegment(segment, true);
            }

            borderNodes.RemoveWhere(n => !NetAccess.ExistsNode(n.Node));

            ToolControllerDetour.CollidingSegmentsCache2 = null;

            //Debug.Log("Border nodes (1): " + borderNodes.Count);

            return(borderNodes);
        }
예제 #2
0
        /* If node distance is too short, we travel one segment up from the border node and set the new node as the one to connect to */
        private static void RepairShortSegment(ref Vector3 direction, ref ushort node)
        {
            //Debug.Log("Repairing short segment...");

            NetNode netNode = NetAccess.GetNode(node);

            // If there is more than one segment we cannot safely delete it (we don't even know from which segment we should pick)
            if (netNode.CountSegments() != 1)
            {
                return;
            }

            ushort     segmentId  = NetAccess.GetFirstSegment(netNode);
            NetSegment netSegment = NetAccess.GetSegment(segmentId);

            if (node == netSegment.m_startNode)
            {
                direction = netSegment.m_endDirection;
                node      = netSegment.m_endNode;
            }
            else
            {
                direction = netSegment.m_startDirection;
                node      = netSegment.m_startNode;
            }

            NetAccess.ReleaseSegment(segmentId, true);
        }
        // Solved by ReleaseQuestionableSegments

        /*private static void CheckSplitSegmentAngle(ref NetTool.ControlPoint point, FastList<ushort> createdSegments, HashSet<ConnectionPoint> borderNodes)
         * {
         *  if(point.m_segment != 0)
         *  {
         *      if (createdSegments.Contains(point.m_segment))
         *          return;
         *
         *      if (point.m_node != 0)
         *          return;
         *
         *      Debug.Log("CheckSplitSegmentAngle: Snapping detected");
         *      NetSegment netSegment = NetAccess.GetSegment(point.m_segment);
         *      netSegment.GetClosestPositionAndDirection(point.m_position, out Vector3 pos, out Vector3 dir);
         *      float angle = Vector3.Angle(point.m_direction, dir);
         *      if(angle < 5 || 180 - angle < 5)
         *      {
         *          Debug.Log("CheckSplitSegmentAngle: Releasing (" + angle + " deg)");
         *          bool inverted = ((netSegment.m_flags & NetSegment.Flags.Invert) != NetSegment.Flags.None);
         *          ConnectionPoint p1 = new ConnectionPoint(netSegment.m_startNode, netSegment.m_startDirection, netSegment.Info, inverted);
         *          ConnectionPoint p2 = new ConnectionPoint(netSegment.m_endNode, netSegment.m_endDirection, netSegment.Info, !inverted);
         *          NetAccess.ReleaseSegment(point.m_segment, true);
         *          if(NetAccess.ExistsNode(p1.Node) && NetAccess.ExistsNode(p2.Node))
         *          {
         *              borderNodes.Add(p1);
         *              borderNodes.Add(p2);
         *          }
         *      }
         *  }
         * }*/

        /* Sometimes the intersection end snaps to an existing road. But it can happen that the intersection road and the road it snaps to are (more or
         * less) parallel. Then we are left with a piece of old road overlapping the new road because the old segment for some reason doesn't show up as
         * colliding. We have to find it and release it. I think that it shouldn't happen more than once per intersection tho. */
        private static void ReleaseQuestionableSegments(FastList <ushort> newNodes, FastList <ushort> newSegments)
        {
            foreach (ushort node in newNodes)
            {
                NetNode netNode      = NetAccess.GetNode(node);
                ushort  foundSegment = 0;
                for (int i = 0; i < 8; i++)
                {
                    ushort segment = netNode.GetSegment(i);
                    if (segment != 0 && newSegments.Contains(segment))
                    {
                        if (foundSegment != 0)
                        {
                            goto continueOuterLoop;
                        }
                        else
                        {
                            foundSegment = segment;
                        }
                    }
                }

                Vector3 direction = NetAccess.GetSegment(foundSegment).GetDirection(node);
                for (int i = 0; i < 8; i++)
                {
                    ushort segment = netNode.GetSegment(i);
                    if (segment != 0 && segment != foundSegment)
                    {
                        float angle = Vector3.Angle(direction, NetAccess.GetSegment(segment).GetDirection(node));
                        if (angle < 10)
                        {
                            //Debug.Log("Releasing questionable segment " + segment);
                            NetAccess.ReleaseSegment(segment);
                            goto breakOuterLoop;
                        }
                    }
                }

                continueOuterLoop :;
            }
            breakOuterLoop :;
        }