Esempio n. 1
0
    public override void Process(uint seed)
    {
        if (World.Networked || World.Size < MinWorldSize)
        {
            return;
        }
        int[,] array = TerrainPath.CreateRoadCostmap(ref seed);
        PathFinder      pathFinder = new PathFinder(array);
        int             length     = array.GetLength(0);
        int             num        = length / 4;
        int             num2       = 4;
        int             stepcount  = num / num2;
        int             num3       = length / 2;
        int             pos_x      = num;
        int             pos_x2     = length - num;
        int             pos_y      = num;
        int             pos_y2     = length - num;
        int             num4       = 0;
        int             dir_x      = -num2;
        int             dir_x2     = num2;
        int             dir_y      = -num2;
        int             dir_y2     = num2;
        List <RingNode> list       = ((World.Size >= 5000) ? new List <RingNode>
        {
            new RingNode(num3, pos_y2, num4, dir_y, stepcount),
            new RingNode(pos_x2, pos_y2, dir_x, dir_y, stepcount),
            new RingNode(pos_x2, num3, dir_x, num4, stepcount),
            new RingNode(pos_x2, pos_y, dir_x, dir_y2, stepcount),
            new RingNode(num3, pos_y, num4, dir_y2, stepcount),
            new RingNode(pos_x, pos_y, dir_x2, dir_y2, stepcount),
            new RingNode(pos_x, num3, dir_x2, num4, stepcount),
            new RingNode(pos_x, pos_y2, dir_x2, dir_y, stepcount)
        } : new List <RingNode>
        {
            new RingNode(pos_x2, pos_y2, dir_x, dir_y, stepcount),
            new RingNode(pos_x2, pos_y, dir_x, dir_y2, stepcount),
            new RingNode(pos_x, pos_y, dir_x2, dir_y2, stepcount),
            new RingNode(pos_x, pos_y2, dir_x2, dir_y, stepcount)
        });

        for (int i = 0; i < list.Count; i++)
        {
            RingNode ringNode = list[i];
            RingNode next     = list[(i + 1) % list.Count];
            RingNode prev     = list[(i - 1 + list.Count) % list.Count];
            ringNode.next = next;
            ringNode.prev = prev;
            while (!pathFinder.IsWalkable(ringNode.position))
            {
                if (ringNode.attempts <= 0)
                {
                    return;
                }
                ringNode.position += ringNode.direction;
                ringNode.attempts--;
            }
        }
        foreach (RingNode item in list)
        {
            item.path = pathFinder.FindPath(item.position, item.next.position, 250000);
        }
        bool flag = false;

        while (!flag)
        {
            flag = true;
            PathFinder.Point point = new PathFinder.Point(0, 0);
            foreach (RingNode item2 in list)
            {
                point += item2.position;
            }
            point /= list.Count;
            float    num5      = float.MinValue;
            RingNode ringNode2 = null;
            foreach (RingNode item3 in list)
            {
                if (item3.path == null)
                {
                    float num6 = new Vector2(item3.position.x - point.x, item3.position.y - point.y).magnitude;
                    if (item3.prev.path == null)
                    {
                        num6 *= 1.5f;
                    }
                    if (num6 > num5)
                    {
                        num5      = num6;
                        ringNode2 = item3;
                    }
                }
            }
            if (ringNode2 == null)
            {
                continue;
            }
            do
            {
                if (ringNode2.attempts <= 0)
                {
                    return;
                }
                ringNode2.position += ringNode2.direction;
                ringNode2.attempts--;
            }while (!pathFinder.IsWalkable(ringNode2.position));
            ringNode2.path      = pathFinder.FindPath(ringNode2.position, ringNode2.next.position, 250000);
            ringNode2.prev.path = pathFinder.FindPath(ringNode2.prev.position, ringNode2.position, 250000);
            flag = false;
        }
        if (!flag)
        {
            return;
        }
        for (int j = 0; j < list.Count; j++)
        {
            RingNode ringNode3 = list[j];
            RingNode ringNode4 = list[(j + 1) % list.Count];
            for (PathFinder.Node node = ringNode3.path; node != null; node = node.next)
            {
                for (PathFinder.Node node2 = ringNode4.path; node2 != null; node2 = node2.next)
                {
                    if (Mathf.Abs(node.point.x - node2.point.x) <= 1 && Mathf.Abs(node.point.y - node2.point.y) <= 1)
                    {
                        node.next      = null;
                        ringNode4.path = node2;
                        break;
                    }
                }
            }
        }
        PathFinder.Node node3 = null;
        PathFinder.Node node4 = null;
        foreach (RingNode item4 in list)
        {
            if (node3 == null)
            {
                node3 = item4.path;
                node4 = item4.path;
            }
            else
            {
                node4.next = item4.path;
            }
            while (node4.next != null)
            {
                node4 = node4.next;
            }
        }
        node4.next = new PathFinder.Node(node3.point, node3.cost, node3.heuristic);
        List <Vector3> list2 = new List <Vector3>();

        for (PathFinder.Node node5 = node3; node5 != null; node5 = node5.next)
        {
            float normX = ((float)node5.point.x + 0.5f) / (float)length;
            float normZ = ((float)node5.point.y + 0.5f) / (float)length;
            float x     = TerrainMeta.DenormalizeX(normX);
            float z     = TerrainMeta.DenormalizeZ(normZ);
            float y     = Mathf.Max(TerrainMeta.HeightMap.GetHeight(normX, normZ), 1f);
            list2.Add(new Vector3(x, y, z));
        }
        if (list2.Count >= 2)
        {
            int      count    = TerrainMeta.Path.Roads.Count;
            PathList pathList = new PathList("Road " + count, list2.ToArray());
            pathList.Width            = 12f;
            pathList.InnerPadding     = 1f;
            pathList.OuterPadding     = 1f;
            pathList.InnerFade        = 1f;
            pathList.OuterFade        = 8f;
            pathList.RandomScale      = 0.75f;
            pathList.MeshOffset       = 0f;
            pathList.TerrainOffset    = -0.125f;
            pathList.Topology         = 2048;
            pathList.Splat            = 128;
            pathList.Start            = false;
            pathList.End              = false;
            pathList.ProcgenStartNode = node3;
            pathList.ProcgenEndNode   = node4;
            pathList.Path.Smoothen(4);
            pathList.Path.RecalculateTangents();
            pathList.AdjustPlacementMap(24f);
            TerrainMeta.Path.Roads.Add(pathList);
        }
    }
Esempio n. 2
0
    public override void Process(uint seed)
    {
        if (World.Networked)
        {
            TerrainMeta.Path.Roads.Clear();
            TerrainMeta.Path.Roads.AddRange(World.GetPaths("Road"));
            return;
        }
        List <PathList> list = new List <PathList>();

        int[,] array = TerrainPath.CreateRoadCostmap(ref seed);
        PathFinder              pathFinder = new PathFinder(array);
        int                     length     = array.GetLength(0);
        List <PathSegment>      list2      = new List <PathSegment>();
        List <PathNode>         list3      = new List <PathNode>();
        List <PathNode>         list4      = new List <PathNode>();
        List <PathNode>         list5      = new List <PathNode>();
        List <PathFinder.Point> list6      = new List <PathFinder.Point>();
        List <PathFinder.Point> list7      = new List <PathFinder.Point>();
        List <PathFinder.Point> list8      = new List <PathFinder.Point>();

        foreach (PathList road in TerrainMeta.Path.Roads)
        {
            if (road.ProcgenStartNode == null || road.ProcgenEndNode == null)
            {
                continue;
            }
            int num = 1;
            for (PathFinder.Node node4 = road.ProcgenStartNode; node4 != null; node4 = node4.next)
            {
                if (num % 8 == 0)
                {
                    list6.Add(node4.point);
                }
                num++;
            }
        }
        foreach (MonumentInfo monument in TerrainMeta.Path.Monuments)
        {
            if (monument.Type == MonumentType.Roadside)
            {
                continue;
            }
            TerrainPathConnect[] componentsInChildren = monument.GetComponentsInChildren <TerrainPathConnect>(true);
            foreach (TerrainPathConnect terrainPathConnect in componentsInChildren)
            {
                if (terrainPathConnect.Type == RoadType)
                {
                    PathFinder.Point pathFinderPoint = terrainPathConnect.GetPathFinderPoint(length);
                    PathFinder.Node  node5           = pathFinder.FindClosestWalkable(pathFinderPoint, 100000);
                    if (node5 != null)
                    {
                        PathNode pathNode = new PathNode();
                        pathNode.monument = monument;
                        pathNode.target   = terrainPathConnect;
                        pathNode.node     = node5;
                        list4.Add(pathNode);
                    }
                }
            }
        }
        while (list4.Count != 0 || list5.Count != 0)
        {
            if (list4.Count == 0)
            {
                PathNode node3 = list5[0];
                list4.AddRange(list5.Where((PathNode x) => x.monument == node3.monument));
                list5.RemoveAll((PathNode x) => x.monument == node3.monument);
                pathFinder.PushPoint      = node3.monument.GetPathFinderPoint(length);
                pathFinder.PushRadius     = node3.monument.GetPathFinderRadius(length);
                pathFinder.PushDistance   = 40;
                pathFinder.PushMultiplier = 1;
            }
            list8.Clear();
            list8.AddRange(list4.Select((PathNode x) => x.node.point));
            list7.Clear();
            list7.AddRange(list3.Select((PathNode x) => x.node.point));
            list7.AddRange(list5.Select((PathNode x) => x.node.point));
            list7.AddRange(list6);
            PathFinder.Node node6 = pathFinder.FindPathUndirected(list7, list8, 100000);
            if (node6 == null)
            {
                PathNode node2 = list4[0];
                list5.AddRange(list4.Where((PathNode x) => x.monument == node2.monument));
                list4.RemoveAll((PathNode x) => x.monument == node2.monument);
                list5.Remove(node2);
                list3.Add(node2);
                continue;
            }
            PathSegment segment = new PathSegment();
            for (PathFinder.Node node7 = node6; node7 != null; node7 = node7.next)
            {
                if (node7 == node6)
                {
                    segment.start = node7;
                }
                if (node7.next == null)
                {
                    segment.end = node7;
                }
            }
            list2.Add(segment);
            PathNode node = list4.Find((PathNode x) => x.node.point == segment.start.point || x.node.point == segment.end.point);
            list5.AddRange(list4.Where((PathNode x) => x.monument == node.monument));
            list4.RemoveAll((PathNode x) => x.monument == node.monument);
            list5.Remove(node);
            list3.Add(node);
            PathNode pathNode2 = list5.Find((PathNode x) => x.node.point == segment.start.point || x.node.point == segment.end.point);
            if (pathNode2 != null)
            {
                list5.Remove(pathNode2);
                list3.Add(pathNode2);
            }
            int num2 = 1;
            for (PathFinder.Node node8 = node6; node8 != null; node8 = node8.next)
            {
                if (num2 % 8 == 0)
                {
                    list6.Add(node8.point);
                }
                num2++;
            }
        }
        foreach (PathNode target in list3)
        {
            PathSegment pathSegment = list2.Find((PathSegment x) => x.start.point == target.node.point || x.end.point == target.node.point);
            if (pathSegment != null)
            {
                if (pathSegment.start.point == target.node.point)
                {
                    PathFinder.Node node9 = target.node;
                    PathFinder.Node start = pathFinder.Reverse(target.node);
                    node9.next         = pathSegment.start;
                    pathSegment.start  = start;
                    pathSegment.origin = target.target;
                }
                else if (pathSegment.end.point == target.node.point)
                {
                    pathSegment.end.next = target.node;
                    pathSegment.end      = pathFinder.FindEnd(target.node);
                    pathSegment.target   = target.target;
                }
            }
        }
        List <Vector3> list9 = new List <Vector3>();

        foreach (PathSegment item in list2)
        {
            bool start2 = false;
            bool end    = false;
            for (PathFinder.Node node10 = item.start; node10 != null; node10 = node10.next)
            {
                float normX = ((float)node10.point.x + 0.5f) / (float)length;
                float normZ = ((float)node10.point.y + 0.5f) / (float)length;
                if (item.start == node10 && item.origin != null)
                {
                    start2 = true;
                    normX  = TerrainMeta.NormalizeX(item.origin.transform.position.x);
                    normZ  = TerrainMeta.NormalizeZ(item.origin.transform.position.z);
                }
                else if (item.end == node10 && item.target != null)
                {
                    end   = true;
                    normX = TerrainMeta.NormalizeX(item.target.transform.position.x);
                    normZ = TerrainMeta.NormalizeZ(item.target.transform.position.z);
                }
                float x2 = TerrainMeta.DenormalizeX(normX);
                float z  = TerrainMeta.DenormalizeZ(normZ);
                float y  = Mathf.Max(TerrainMeta.HeightMap.GetHeight(normX, normZ), 1f);
                list9.Add(new Vector3(x2, y, z));
            }
            if (list9.Count != 0)
            {
                if (list9.Count >= 2)
                {
                    int      number   = TerrainMeta.Path.Roads.Count + list.Count;
                    PathList pathList = CreateSegment(number, list9.ToArray());
                    pathList.Start            = start2;
                    pathList.End              = end;
                    pathList.ProcgenStartNode = item.start;
                    pathList.ProcgenEndNode   = item.end;
                    list.Add(pathList);
                }
                list9.Clear();
            }
        }
        foreach (PathList item2 in list)
        {
            item2.Path.Smoothen(4);
            item2.Path.RecalculateTangents();
            item2.AdjustPlacementMap(item2.Width * 2f);
        }
        TerrainMeta.Path.Roads.AddRange(list);
    }
    public override void Process(uint seed)
    {
        if (World.Networked)
        {
            TerrainMeta.Path.Powerlines.Clear();
            TerrainMeta.Path.Powerlines.AddRange(World.GetPaths("Powerline"));
            return;
        }
        List <PathList>     list      = new List <PathList>();
        List <MonumentInfo> monuments = TerrainMeta.Path.Monuments;

        int[,] array = TerrainPath.CreatePowerlineCostmap(ref seed);
        PathFinder              pathFinder = new PathFinder(array);
        int                     length     = array.GetLength(0);
        List <PathSegment>      list2      = new List <PathSegment>();
        List <PathNode>         list3      = new List <PathNode>();
        List <PathNode>         list4      = new List <PathNode>();
        List <PathFinder.Point> list5      = new List <PathFinder.Point>();
        List <PathFinder.Point> list6      = new List <PathFinder.Point>();
        List <PathFinder.Point> list7      = new List <PathFinder.Point>();

        foreach (PathList road in TerrainMeta.Path.Roads)
        {
            if (road.ProcgenStartNode == null || road.ProcgenEndNode == null || !road.IsExtraWide)
            {
                continue;
            }
            int num = 1;
            for (PathFinder.Node node = road.ProcgenStartNode; node != null; node = node.next)
            {
                if (num % 8 == 0)
                {
                    list5.Add(node.point);
                }
                num++;
            }
        }
        foreach (MonumentInfo item in monuments)
        {
            TerrainPathConnect[] componentsInChildren = item.GetComponentsInChildren <TerrainPathConnect>(true);
            foreach (TerrainPathConnect terrainPathConnect in componentsInChildren)
            {
                if (terrainPathConnect.Type == InfrastructureType.Power)
                {
                    PathFinder.Point pathFinderPoint = terrainPathConnect.GetPathFinderPoint(length);
                    PathFinder.Node  node2           = pathFinder.FindClosestWalkable(pathFinderPoint, 100000);
                    if (node2 != null)
                    {
                        PathNode pathNode = new PathNode();
                        pathNode.monument = item;
                        pathNode.node     = node2;
                        list4.Add(pathNode);
                    }
                }
            }
        }
        while (list4.Count != 0)
        {
            list7.Clear();
            list7.AddRange(list4.Select((PathNode x) => x.node.point));
            list6.Clear();
            list6.AddRange(list3.Select((PathNode x) => x.node.point));
            list6.AddRange(list5);
            PathFinder.Node node3 = pathFinder.FindPathUndirected(list6, list7, 100000);
            if (node3 == null)
            {
                PathNode copy2 = list4[0];
                list3.AddRange(list4.Where((PathNode x) => x.monument == copy2.monument));
                list4.RemoveAll((PathNode x) => x.monument == copy2.monument);
                continue;
            }
            PathSegment segment = new PathSegment();
            for (PathFinder.Node node4 = node3; node4 != null; node4 = node4.next)
            {
                if (node4 == node3)
                {
                    segment.start = node4;
                }
                if (node4.next == null)
                {
                    segment.end = node4;
                }
            }
            list2.Add(segment);
            PathNode copy = list4.Find((PathNode x) => x.node.point == segment.start.point || x.node.point == segment.end.point);
            list3.AddRange(list4.Where((PathNode x) => x.monument == copy.monument));
            list4.RemoveAll((PathNode x) => x.monument == copy.monument);
            int num2 = 1;
            for (PathFinder.Node node5 = node3; node5 != null; node5 = node5.next)
            {
                if (num2 % 8 == 0)
                {
                    list5.Add(node5.point);
                }
                num2++;
            }
        }
        List <Vector3> list8 = new List <Vector3>();

        foreach (PathSegment item2 in list2)
        {
            for (PathFinder.Node node6 = item2.start; node6 != null; node6 = node6.next)
            {
                float num3   = ((float)node6.point.x + 0.5f) / (float)length;
                float num4   = ((float)node6.point.y + 0.5f) / (float)length;
                float height = TerrainMeta.HeightMap.GetHeight01(num3, num4);
                list8.Add(TerrainMeta.Denormalize(new Vector3(num3, height, num4)));
            }
            if (list8.Count != 0)
            {
                if (list8.Count >= 8)
                {
                    int      num5     = TerrainMeta.Path.Powerlines.Count + list.Count;
                    PathList pathList = new PathList("Powerline " + num5, list8.ToArray());
                    pathList.Start            = true;
                    pathList.End              = true;
                    pathList.ProcgenStartNode = item2.start;
                    pathList.ProcgenEndNode   = item2.end;
                    list.Add(pathList);
                }
                list8.Clear();
            }
        }
        foreach (PathList item3 in list)
        {
            item3.Path.RecalculateTangents();
        }
        TerrainMeta.Path.Powerlines.AddRange(list);
    }