public override void Process(uint seed)
    {
        List <PathList>     pathListList = new List <PathList>();
        TerrainHeightMap    heightMap    = TerrainMeta.HeightMap;
        TerrainTopologyMap  topologyMap  = TerrainMeta.TopologyMap;
        List <MonumentInfo> monuments    = TerrainMeta.Path.Monuments;

        if (monuments.Count == 0)
        {
            return;
        }
        int res = Mathf.NextPowerOfTwo((int)((double)World.Size / 10.0));

        int[,] costmap = new int[res, res];
        float radius = 5f;

        for (int index1 = 0; index1 < res; ++index1)
        {
            float normZ = ((float)index1 + 0.5f) / (float)res;
            for (int index2 = 0; index2 < res; ++index2)
            {
                float normX    = ((float)index2 + 0.5f) / (float)res;
                float slope    = heightMap.GetSlope(normX, normZ);
                int   topology = topologyMap.GetTopology(normX, normZ, radius);
                int   num1     = 2295174;
                int   num2     = 55296;
                int   num3     = 512;
                costmap[index1, index2] = (topology & num1) == 0 ? ((topology & num2) == 0 ? ((topology & num3) == 0 ? 1 + (int)((double)slope * (double)slope * 10.0) : 1000) : 2500) : int.MaxValue;
            }
        }
        PathFinder pathFinder = new PathFinder(costmap, true);
        List <GeneratePowerlineLayout.PathSegment> pathSegmentList = new List <GeneratePowerlineLayout.PathSegment>();
        List <GeneratePowerlineLayout.PathNode>    source1         = new List <GeneratePowerlineLayout.PathNode>();
        List <GeneratePowerlineLayout.PathNode>    source2         = new List <GeneratePowerlineLayout.PathNode>();
        List <PathFinder.Point> pointList = new List <PathFinder.Point>();
        List <PathFinder.Point> startList = new List <PathFinder.Point>();
        List <PathFinder.Point> endList   = new List <PathFinder.Point>();

        foreach (MonumentInfo monumentInfo in monuments)
        {
            bool flag = source1.Count == 0;
            foreach (TerrainPathConnect target in monumentInfo.GetTargets(InfrastructureType.Power))
            {
                PathFinder.Point point           = target.GetPoint(res);
                PathFinder.Node  closestWalkable = pathFinder.FindClosestWalkable(point, 100000);
                if (closestWalkable != null)
                {
                    GeneratePowerlineLayout.PathNode pathNode = new GeneratePowerlineLayout.PathNode();
                    pathNode.monument = monumentInfo;
                    pathNode.node     = closestWalkable;
                    if (flag)
                    {
                        source1.Add(pathNode);
                    }
                    else
                    {
                        source2.Add(pathNode);
                    }
                }
            }
        }
        while (source2.Count != 0)
        {
            startList.Clear();
            endList.Clear();
            startList.AddRange(source1.Select <GeneratePowerlineLayout.PathNode, PathFinder.Point>((Func <GeneratePowerlineLayout.PathNode, PathFinder.Point>)(x => x.node.point)));
            startList.AddRange((IEnumerable <PathFinder.Point>)pointList);
            endList.AddRange(source2.Select <GeneratePowerlineLayout.PathNode, PathFinder.Point>((Func <GeneratePowerlineLayout.PathNode, PathFinder.Point>)(x => x.node.point)));
            PathFinder.Node pathUndirected = pathFinder.FindPathUndirected(startList, endList, 100000);
            if (pathUndirected == null)
            {
                GeneratePowerlineLayout.PathNode copy = source2[0];
                source1.AddRange(source2.Where <GeneratePowerlineLayout.PathNode>((Func <GeneratePowerlineLayout.PathNode, bool>)(x => Object.op_Equality((Object)x.monument, (Object)copy.monument))));
                source2.RemoveAll((Predicate <GeneratePowerlineLayout.PathNode>)(x => Object.op_Equality((Object)x.monument, (Object)copy.monument)));
            }
            else
            {
                GeneratePowerlineLayout.PathSegment segment = new GeneratePowerlineLayout.PathSegment();
                for (PathFinder.Node node = pathUndirected; node != null; node = node.next)
                {
                    if (node == pathUndirected)
                    {
                        segment.start = node;
                    }
                    if (node.next == null)
                    {
                        segment.end = node;
                    }
                }
                pathSegmentList.Add(segment);
                GeneratePowerlineLayout.PathNode copy = source2.Find((Predicate <GeneratePowerlineLayout.PathNode>)(x =>
                {
                    if (!(x.node.point == segment.start.point))
                    {
                        return(x.node.point == segment.end.point);
                    }
                    return(true);
                }));
                source1.AddRange(source2.Where <GeneratePowerlineLayout.PathNode>((Func <GeneratePowerlineLayout.PathNode, bool>)(x => Object.op_Equality((Object)x.monument, (Object)copy.monument))));
                source2.RemoveAll((Predicate <GeneratePowerlineLayout.PathNode>)(x => Object.op_Equality((Object)x.monument, (Object)copy.monument)));
                int num = 1;
                for (PathFinder.Node node = pathUndirected; node != null; node = node.next)
                {
                    if (num % 8 == 0)
                    {
                        pointList.Add(node.point);
                    }
                    ++num;
                }
            }
        }
        List <Vector3> vector3List = new List <Vector3>();

        foreach (GeneratePowerlineLayout.PathSegment pathSegment in pathSegmentList)
        {
            for (PathFinder.Node node = pathSegment.start; node != null; node = node.next)
            {
                float normX    = ((float)node.point.x + 0.5f) / (float)res;
                float normZ    = ((float)node.point.y + 0.5f) / (float)res;
                float height01 = heightMap.GetHeight01(normX, normZ);
                vector3List.Add(TerrainMeta.Denormalize(new Vector3(normX, height01, normZ)));
            }
            if (vector3List.Count != 0)
            {
                if (vector3List.Count >= 8)
                {
                    pathListList.Add(new PathList("Powerline " + (object)pathListList.Count, vector3List.ToArray())
                    {
                        Start = true,
                        End   = true
                    });
                }
                vector3List.Clear();
            }
        }
        TerrainMeta.Path.Powerlines.AddRange((IEnumerable <PathList>)pathListList);
    }
Пример #2
0
    public override void Process(uint seed)
    {
        List <PathList>     pathLists   = new List <PathList>();
        TerrainHeightMap    heightMap   = TerrainMeta.HeightMap;
        TerrainTopologyMap  topologyMap = TerrainMeta.TopologyMap;
        List <MonumentInfo> monuments   = TerrainMeta.Path.Monuments;

        if (monuments.Count == 0)
        {
            return;
        }
        int num = Mathf.NextPowerOfTwo((int)((float)((float)World.Size) / 10f));

        int[,] numArray = new int[num, num];
        float single = 5f;

        for (int i = 0; i < num; i++)
        {
            float single1 = ((float)i + 0.5f) / (float)num;
            for (int j = 0; j < num; j++)
            {
                float single2  = ((float)j + 0.5f) / (float)num;
                float slope    = heightMap.GetSlope(single2, single1);
                int   topology = topologyMap.GetTopology(single2, single1, single);
                int   num1     = 2295174;
                int   num2     = 55296;
                int   num3     = 512;
                if ((topology & num1) != 0)
                {
                    numArray[i, j] = 2147483647;
                }
                else if ((topology & num2) != 0)
                {
                    numArray[i, j] = 2500;
                }
                else if ((topology & num3) == 0)
                {
                    numArray[i, j] = 1 + (int)(slope * slope * 10f);
                }
                else
                {
                    numArray[i, j] = 1000;
                }
            }
        }
        PathFinder pathFinder = new PathFinder(numArray, true);
        List <GeneratePowerlineLayout.PathSegment> pathSegments = new List <GeneratePowerlineLayout.PathSegment>();
        List <GeneratePowerlineLayout.PathNode>    pathNodes    = new List <GeneratePowerlineLayout.PathNode>();
        List <GeneratePowerlineLayout.PathNode>    pathNodes1   = new List <GeneratePowerlineLayout.PathNode>();
        List <PathFinder.Point> points  = new List <PathFinder.Point>();
        List <PathFinder.Point> points1 = new List <PathFinder.Point>();
        List <PathFinder.Point> points2 = new List <PathFinder.Point>();

        foreach (MonumentInfo monument in monuments)
        {
            bool count = pathNodes.Count == 0;
            foreach (TerrainPathConnect target in monument.GetTargets(InfrastructureType.Power))
            {
                PathFinder.Point point = target.GetPoint(num);
                PathFinder.Node  node  = pathFinder.FindClosestWalkable(point, 100000);
                if (node == null)
                {
                    continue;
                }
                GeneratePowerlineLayout.PathNode pathNode = new GeneratePowerlineLayout.PathNode()
                {
                    monument = monument,
                    node     = node
                };
                if (!count)
                {
                    pathNodes1.Add(pathNode);
                }
                else
                {
                    pathNodes.Add(pathNode);
                }
            }
        }
        while (pathNodes1.Count != 0)
        {
            points1.Clear();
            points2.Clear();
            points1.AddRange(
                from x in pathNodes
                select x.node.point);
            points1.AddRange(points);
            points2.AddRange(
                from x in pathNodes1
                select x.node.point);
            PathFinder.Node node1 = pathFinder.FindPathUndirected(points1, points2, 100000);
            if (node1 != null)
            {
                GeneratePowerlineLayout.PathSegment pathSegment = new GeneratePowerlineLayout.PathSegment();
                for (PathFinder.Node k = node1; k != null; k = k.next)
                {
                    if (k == node1)
                    {
                        pathSegment.start = k;
                    }
                    if (k.next == null)
                    {
                        pathSegment.end = k;
                    }
                }
                pathSegments.Add(pathSegment);
                GeneratePowerlineLayout.PathNode pathNode1 = pathNodes1.Find((GeneratePowerlineLayout.PathNode x) => {
                    if (x.node.point == pathSegment.start.point)
                    {
                        return(true);
                    }
                    return(x.node.point == pathSegment.end.point);
                });
                pathNodes.AddRange(
                    from x in pathNodes1
                    where x.monument == pathNode1.monument
                    select x);
                pathNodes1.RemoveAll((GeneratePowerlineLayout.PathNode x) => x.monument == pathNode1.monument);
                int num4 = 1;
                for (PathFinder.Node l = node1; l != null; l = l.next)
                {
                    if (num4 % 8 == 0)
                    {
                        points.Add(l.point);
                    }
                    num4++;
                }
            }
            else
            {
                GeneratePowerlineLayout.PathNode item = pathNodes1[0];
                pathNodes.AddRange(
                    from x in pathNodes1
                    where x.monument == item.monument
                    select x);
                pathNodes1.RemoveAll((GeneratePowerlineLayout.PathNode x) => x.monument == item.monument);
            }
        }
        List <Vector3> vector3s = new List <Vector3>();

        foreach (GeneratePowerlineLayout.PathSegment pathSegment1 in pathSegments)
        {
            for (PathFinder.Node m = pathSegment1.start; m != null; m = m.next)
            {
                float single3  = ((float)m.point.x + 0.5f) / (float)num;
                float single4  = ((float)m.point.y + 0.5f) / (float)num;
                float height01 = heightMap.GetHeight01(single3, single4);
                vector3s.Add(TerrainMeta.Denormalize(new Vector3(single3, height01, single4)));
            }
            if (vector3s.Count == 0)
            {
                continue;
            }
            if (vector3s.Count >= 8)
            {
                PathList pathList = new PathList(string.Concat("Powerline ", pathLists.Count), vector3s.ToArray())
                {
                    Start = true,
                    End   = true
                };
                pathLists.Add(pathList);
            }
            vector3s.Clear();
        }
        TerrainMeta.Path.Powerlines.AddRange(pathLists);
    }