Пример #1
0
 public PathFinder.Node FindPath(
     PathFinder.Point start,
     PathFinder.Point end,
     int depth = 2147483647)
 {
     return(this.FindPathReversed(end, start, depth));
 }
Пример #2
0
    public int Heuristic(PathFinder.Point a, PathFinder.Point b)
    {
        int num  = a.x - b.x;
        int num1 = a.y - b.y;

        return(num * num + num1 * num1);
    }
Пример #3
0
 public Node(PathFinder.Point point, int cost, int heuristic, PathFinder.Node next = null)
 {
     this.point     = point;
     this.cost      = cost;
     this.heuristic = heuristic;
     this.next      = next;
 }
Пример #4
0
 public int Heuristic(PathFinder.Point a)
 {
     if (this.costmap[a.y, a.x] != 2147483647)
     {
         return(0);
     }
     return(1);
 }
Пример #5
0
    public int Heuristic(PathFinder.Point a, List <PathFinder.Point> b)
    {
        int num = int.MaxValue;

        for (int index = 0; index < b.Count; ++index)
        {
            num = Mathf.Min(num, this.Heuristic(a, b[index]));
        }
        return(num);
    }
Пример #6
0
    public int Heuristic(PathFinder.Point a, List <PathFinder.Point> b)
    {
        int num = 2147483647;

        for (int i = 0; i < b.Count; i++)
        {
            num = Mathf.Min(num, this.Heuristic(a, b[i]));
        }
        return(num);
    }
Пример #7
0
    public PathFinder.Point GetPathFinderPoint(int res)
    {
        Vector3 position = base.transform.position;
        float   num      = TerrainMeta.NormalizeX(position.x);
        float   num2     = TerrainMeta.NormalizeZ(position.z);

        PathFinder.Point result = default(PathFinder.Point);
        result.x = Mathf.Clamp((int)(num * (float)res), 0, res - 1);
        result.y = Mathf.Clamp((int)(num2 * (float)res), 0, res - 1);
        return(result);
    }
Пример #8
0
    private PathFinder.Node FindPathReversed(List <PathFinder.Point> startList, List <PathFinder.Point> endList, int depth = 2147483647)
    {
        if (this.visited != null)
        {
            Array.Clear(this.visited, 0, this.visited.Length);
        }
        else
        {
            this.visited = new bool[this.costmap.GetLength(0), this.costmap.GetLength(1)];
        }
        int num     = 0;
        int length  = this.costmap.GetLength(0) - 1;
        int num1    = 0;
        int length1 = this.costmap.GetLength(1) - 1;
        IntrusiveMinHeap <PathFinder.Node> intrusiveMinHeap = new IntrusiveMinHeap <PathFinder.Node>();

        foreach (PathFinder.Point point in startList)
        {
            int num2 = this.costmap[point.y, point.x];
            int num3 = this.Heuristic(point, endList);
            intrusiveMinHeap.Add(new PathFinder.Node(point, num2, num3, null));
            this.visited[point.y, point.x] = true;
        }
        while (!intrusiveMinHeap.Empty)
        {
            int num4 = depth;
            depth = num4 - 1;
            if (num4 <= 0)
            {
                break;
            }
            PathFinder.Node node = intrusiveMinHeap.Pop();
            if (node.heuristic == 0)
            {
                return(node);
            }
            for (int i = 0; i < (int)this.neighbors.Length; i++)
            {
                PathFinder.Point point1 = node.point + this.neighbors[i];
                if (point1.x >= num && point1.x <= length && point1.y >= num1 && point1.y <= length1 && !this.visited[point1.y, point1.x])
                {
                    this.visited[point1.y, point1.x] = true;
                    int num5 = this.costmap[point1.y, point1.x];
                    if (num5 != 2147483647)
                    {
                        int num6 = node.cost + num5;
                        int num7 = this.Heuristic(point1, endList);
                        intrusiveMinHeap.Add(new PathFinder.Node(point1, num6, num7, node));
                    }
                }
            }
        }
        return(null);
    }
Пример #9
0
    public PathFinder.Point GetPoint(int res)
    {
        Vector3 vector3 = base.transform.position;
        float   single  = TerrainMeta.NormalizeX(vector3.x);
        float   single1 = TerrainMeta.NormalizeZ(vector3.z);

        PathFinder.Point point = new PathFinder.Point()
        {
            x = Mathf.Clamp((int)(single * (float)res), 0, res - 1),
            y = Mathf.Clamp((int)(single1 * (float)res), 0, res - 1)
        };
        return(point);
    }
Пример #10
0
    private PathFinder.Node FindPathReversed(
        List <PathFinder.Point> startList,
        List <PathFinder.Point> endList,
        int depth = 2147483647)
    {
        if (this.visited == null)
        {
            this.visited = new bool[this.costmap.GetLength(0), this.costmap.GetLength(1)];
        }
        else
        {
            Array.Clear((Array)this.visited, 0, this.visited.Length);
        }
        int num1 = 0;
        int num2 = this.costmap.GetLength(0) - 1;
        int num3 = 0;
        int num4 = this.costmap.GetLength(1) - 1;
        IntrusiveMinHeap <PathFinder.Node> intrusiveMinHeap = (IntrusiveMinHeap <PathFinder.Node>)null;

        foreach (PathFinder.Point start in startList)
        {
            int cost      = this.costmap[start.y, start.x];
            int heuristic = this.Heuristic(start, endList);
            ((IntrusiveMinHeap <PathFinder.Node>) ref intrusiveMinHeap).Add(new PathFinder.Node(start, cost, heuristic, (PathFinder.Node)null));
            this.visited[start.y, start.x] = true;
        }
        while (!((IntrusiveMinHeap <PathFinder.Node>) ref intrusiveMinHeap).get_Empty() && depth-- > 0)
        {
            PathFinder.Node next = ((IntrusiveMinHeap <PathFinder.Node>) ref intrusiveMinHeap).Pop();
            if (next.heuristic == 0)
            {
                return(next);
            }
            for (int index = 0; index < this.neighbors.Length; ++index)
            {
                PathFinder.Point point = next.point + this.neighbors[index];
                if (point.x >= num1 && point.x <= num2 && (point.y >= num3 && point.y <= num4) && !this.visited[point.y, point.x])
                {
                    this.visited[point.y, point.x] = true;
                    int num5 = this.costmap[point.y, point.x];
                    if (num5 != int.MaxValue)
                    {
                        int cost      = next.cost + num5;
                        int heuristic = this.Heuristic(point, endList);
                        ((IntrusiveMinHeap <PathFinder.Node>) ref intrusiveMinHeap).Add(new PathFinder.Node(point, cost, heuristic, next));
                    }
                }
            }
        }
        return((PathFinder.Node)null);
    }
Пример #11
0
    public PathFinder.Node FindClosestWalkable(PathFinder.Point start, int depth = 2147483647)
    {
        if (this.visited != null)
        {
            Array.Clear(this.visited, 0, this.visited.Length);
        }
        else
        {
            this.visited = new bool[this.costmap.GetLength(0), this.costmap.GetLength(1)];
        }
        int num     = 0;
        int length  = this.costmap.GetLength(0) - 1;
        int num1    = 0;
        int length1 = this.costmap.GetLength(1) - 1;
        IntrusiveMinHeap <PathFinder.Node> intrusiveMinHeap = new IntrusiveMinHeap <PathFinder.Node>();
        int num2 = 1;
        int num3 = this.Heuristic(start);

        intrusiveMinHeap.Add(new PathFinder.Node(start, num2, num3, null));
        this.visited[start.y, start.x] = true;
        while (!intrusiveMinHeap.Empty)
        {
            int num4 = depth;
            depth = num4 - 1;
            if (num4 <= 0)
            {
                break;
            }
            PathFinder.Node node = intrusiveMinHeap.Pop();
            if (node.heuristic == 0)
            {
                return(node);
            }
            for (int i = 0; i < (int)this.neighbors.Length; i++)
            {
                PathFinder.Point point = node.point + this.neighbors[i];
                if (point.x >= num && point.x <= length && point.y >= num1 && point.y <= length1 && !this.visited[point.y, point.x])
                {
                    this.visited[point.y, point.x] = true;
                    int num5 = node.cost + 1;
                    int num6 = this.Heuristic(point);
                    intrusiveMinHeap.Add(new PathFinder.Node(point, num5, num6, node));
                }
            }
        }
        return(null);
    }
Пример #12
0
    public PathFinder.Node FindClosestWalkable(PathFinder.Point start, int depth = 2147483647)
    {
        if (this.visited == null)
        {
            this.visited = new bool[this.costmap.GetLength(0), this.costmap.GetLength(1)];
        }
        else
        {
            Array.Clear((Array)this.visited, 0, this.visited.Length);
        }
        int num1 = 0;
        int num2 = this.costmap.GetLength(0) - 1;
        int num3 = 0;
        int num4 = this.costmap.GetLength(1) - 1;
        IntrusiveMinHeap <PathFinder.Node> intrusiveMinHeap = (IntrusiveMinHeap <PathFinder.Node>)null;
        int cost1      = 1;
        int heuristic1 = this.Heuristic(start);

        ((IntrusiveMinHeap <PathFinder.Node>) ref intrusiveMinHeap).Add(new PathFinder.Node(start, cost1, heuristic1, (PathFinder.Node)null));
        this.visited[start.y, start.x] = true;
        while (!((IntrusiveMinHeap <PathFinder.Node>) ref intrusiveMinHeap).get_Empty() && depth-- > 0)
        {
            PathFinder.Node next = ((IntrusiveMinHeap <PathFinder.Node>) ref intrusiveMinHeap).Pop();
            if (next.heuristic == 0)
            {
                return(next);
            }
            for (int index = 0; index < this.neighbors.Length; ++index)
            {
                PathFinder.Point point = next.point + this.neighbors[index];
                if (point.x >= num1 && point.x <= num2 && (point.y >= num3 && point.y <= num4) && !this.visited[point.y, point.x])
                {
                    this.visited[point.y, point.x] = true;
                    int cost2      = next.cost + 1;
                    int heuristic2 = this.Heuristic(point);
                    ((IntrusiveMinHeap <PathFinder.Node>) ref intrusiveMinHeap).Add(new PathFinder.Node(point, cost2, heuristic2, next));
                }
            }
        }
        return((PathFinder.Node)null);
    }
Пример #13
0
 public RingNode(int pos_x, int pos_y, int dir_x, int dir_y, int stepcount)
 {
     position  = new PathFinder.Point(pos_x, pos_y);
     direction = new PathFinder.Point(dir_x, dir_y);
     attempts  = stepcount;
 }
Пример #14
0
 public int Heuristic(PathFinder.Point a)
 {
     return(this.costmap[a.y, a.x] != int.MaxValue ? 0 : 1);
 }
Пример #15
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)
    {
        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);
    }
    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);
    }
Пример #18
0
    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;
                int   num1     = SeedRandom.Range(ref seed, 100, 500);
                float slope    = heightMap.GetSlope(normX, normZ);
                int   topology = topologyMap.GetTopology(normX, normZ, radius);
                int   num2     = 2295686;
                int   num3     = 49152;
                costmap[index1, index2] = (double)slope > 20.0 || (topology & num2) != 0 ? int.MaxValue : ((topology & num3) == 0 ? 1 + (int)((double)slope * (double)slope * 10.0) + num1 : 2500);
            }
        }
        PathFinder pathFinder = new PathFinder(costmap, true);
        List <GenerateRoadLayout.PathSegment> pathSegmentList = new List <GenerateRoadLayout.PathSegment>();
        List <GenerateRoadLayout.PathNode>    source1         = new List <GenerateRoadLayout.PathNode>();
        List <GenerateRoadLayout.PathNode>    source2         = new List <GenerateRoadLayout.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.Road))
            {
                PathFinder.Point point           = target.GetPoint(res);
                PathFinder.Node  closestWalkable = pathFinder.FindClosestWalkable(point, 100000);
                if (closestWalkable != null)
                {
                    GenerateRoadLayout.PathNode pathNode = new GenerateRoadLayout.PathNode();
                    pathNode.monument = monumentInfo;
                    pathNode.target   = target;
                    pathNode.node     = closestWalkable;
                    if (flag)
                    {
                        source1.Add(pathNode);
                    }
                    else
                    {
                        source2.Add(pathNode);
                    }
                }
            }
        }
        while (source2.Count != 0)
        {
            startList.Clear();
            endList.Clear();
            startList.AddRange(source1.Select <GenerateRoadLayout.PathNode, PathFinder.Point>((Func <GenerateRoadLayout.PathNode, PathFinder.Point>)(x => x.node.point)));
            startList.AddRange((IEnumerable <PathFinder.Point>)pointList);
            endList.AddRange(source2.Select <GenerateRoadLayout.PathNode, PathFinder.Point>((Func <GenerateRoadLayout.PathNode, PathFinder.Point>)(x => x.node.point)));
            PathFinder.Node pathUndirected = pathFinder.FindPathUndirected(startList, endList, 100000);
            if (pathUndirected == null)
            {
                GenerateRoadLayout.PathNode copy = source2[0];
                source1.AddRange(source2.Where <GenerateRoadLayout.PathNode>((Func <GenerateRoadLayout.PathNode, bool>)(x => Object.op_Equality((Object)x.monument, (Object)copy.monument))));
                source2.RemoveAll((Predicate <GenerateRoadLayout.PathNode>)(x => Object.op_Equality((Object)x.monument, (Object)copy.monument)));
            }
            else
            {
                GenerateRoadLayout.PathSegment segment = new GenerateRoadLayout.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);
                GenerateRoadLayout.PathNode copy = source2.Find((Predicate <GenerateRoadLayout.PathNode>)(x =>
                {
                    if (!(x.node.point == segment.start.point))
                    {
                        return(x.node.point == segment.end.point);
                    }
                    return(true);
                }));
                source1.AddRange(source2.Where <GenerateRoadLayout.PathNode>((Func <GenerateRoadLayout.PathNode, bool>)(x => Object.op_Equality((Object)x.monument, (Object)copy.monument))));
                source2.RemoveAll((Predicate <GenerateRoadLayout.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;
                }
            }
        }
        foreach (GenerateRoadLayout.PathNode pathNode in source1)
        {
            GenerateRoadLayout.PathNode    target      = pathNode;
            GenerateRoadLayout.PathSegment pathSegment = pathSegmentList.Find((Predicate <GenerateRoadLayout.PathSegment>)(x =>
            {
                if (!(x.start.point == target.node.point))
                {
                    return(x.end.point == target.node.point);
                }
                return(true);
            }));
            if (pathSegment != null)
            {
                if (pathSegment.start.point == target.node.point)
                {
                    PathFinder.Node node1 = target.node;
                    PathFinder.Node node2 = pathFinder.Reverse(target.node);
                    PathFinder.Node start = pathSegment.start;
                    node1.next         = start;
                    pathSegment.start  = node2;
                    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> vector3List = new List <Vector3>();

        foreach (GenerateRoadLayout.PathSegment pathSegment in pathSegmentList)
        {
            bool flag1 = false;
            bool flag2 = false;
            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;
                if (pathSegment.start == node && Object.op_Inequality((Object)pathSegment.origin, (Object)null))
                {
                    flag1 = true;
                    normX = TerrainMeta.NormalizeX((float)((Component)pathSegment.origin).get_transform().get_position().x);
                    normZ = TerrainMeta.NormalizeZ((float)((Component)pathSegment.origin).get_transform().get_position().z);
                }
                else if (pathSegment.end == node && Object.op_Inequality((Object)pathSegment.target, (Object)null))
                {
                    flag2 = true;
                    normX = TerrainMeta.NormalizeX((float)((Component)pathSegment.target).get_transform().get_position().x);
                    normZ = TerrainMeta.NormalizeZ((float)((Component)pathSegment.target).get_transform().get_position().z);
                }
                float num1 = TerrainMeta.DenormalizeX(normX);
                float num2 = TerrainMeta.DenormalizeZ(normZ);
                float num3 = Mathf.Max(heightMap.GetHeight(normX, normZ), 1f);
                vector3List.Add(new Vector3(num1, num3, num2));
            }
            if (vector3List.Count != 0)
            {
                if (vector3List.Count >= 2)
                {
                    pathListList.Add(new PathList("Road " + (object)pathListList.Count, vector3List.ToArray())
                    {
                        Width         = 10f,
                        InnerPadding  = 1f,
                        OuterPadding  = 1f,
                        InnerFade     = 1f,
                        OuterFade     = 8f,
                        RandomScale   = 0.75f,
                        MeshOffset    = -0.0f,
                        TerrainOffset = -0.5f,
                        Topology      = 2048,
                        Splat         = 128,
                        Start         = flag1,
                        End           = flag2
                    });
                }
                vector3List.Clear();
            }
        }
        foreach (PathList pathList in pathListList)
        {
            pathList.Path.Smoothen(2);
        }
        TerrainMeta.Path.Roads.AddRange((IEnumerable <PathList>)pathListList);
    }
Пример #19
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);
        }
    }
Пример #20
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);
    }