Beispiel #1
0
    public static IntersectionPoint GetRandomOtherIntersectionPoint(IntersectionPoint current)
    {
        List <IntersectionPoint> others = new List <IntersectionPoint>(Spawners);

        others.Remove(current);
        return(others[Mathf.FloorToInt((Random.value * others.Count))]);
    }
Beispiel #2
0
    private void UseTestRoute()
    {
        int i = 0;

        foreach (var node in TestRoad.GSDSpline.mNodes)
        {
            var inter = new PathMapBuilder.IntersectionPoint();
            inter.Node1      = node;
            inter.bIsSpawner = node.bIsEndPoint;
            if (i > 0)
            {
                var con = new Connection();
                con.Nodes.Add(node);
                con.Nodes.Add(TestRoad.GSDSpline.mNodes[i - 1]);
                con.Spline = TestRoad.GSDSpline;
                con.StartEndPoint.Add(Route[i - 1]);
                con.StartEndPoint.Add(inter);
                inter.Roads.Add(con);
                Route[i - 1].Roads.Add(con);
            }
            Route.Add(inter);
            i++;
        }
        if (RoadManager.DebubMode)
        {
            Debug.Log("Route set: " + Route.Count);
        }
        ConvertRouteToPath();
    }
    public void Fill(List <PathMapBuilder.IntersectionPoint> list, PathMapBuilder.IntersectionPoint start)
    {
        foreach (var node in list)
        {
            Table.Add(node, new DijkstraTableData()
            {
                Distance = float.MaxValue, PrevIntersectionNode = null
            });
        }

        Table[start].Distance = 0;
    }
Beispiel #4
0
    public static DijkstraTable CalculatePathTable(IntersectionPoint start)
    {
        List <IntersectionPoint> Checked   = new List <IntersectionPoint>();
        List <IntersectionPoint> Unchecked = new List <IntersectionPoint>(IntersectionPoints);

        IntersectionPoint current = start;

        DijkstraTable dijkstraTable = new DijkstraTable();

        dijkstraTable.Fill(IntersectionPoints, start);
        while (Unchecked.Count > 0)
        {
            //Remove from unchecked
            var test = Unchecked.Remove(current);
            if (!test && DebubMode)
            {
                Debug.LogError("List remove unsuccesfull!");
            }

            //Add to checked
            Checked.Add(current);

            List <DijkstraSingleConnectionData> connectedIntersectionPoints = new List <DijkstraSingleConnectionData>();

            //Find all connected Intersections and save distance data
            foreach (Connection road in current.Roads)
            {
                if (Unchecked.Contains(road.StartEndPoint[0]))
                {
                    connectedIntersectionPoints.Add(new DijkstraSingleConnectionData
                    {
                        Distance = road.GetLength(), TargetIntersectionNode = road.StartEndPoint[0]
                    });
                }
                if (Unchecked.Contains(road.StartEndPoint[1]))
                {
                    connectedIntersectionPoints.Add(new DijkstraSingleConnectionData
                    {
                        Distance = road.GetLength(),
                        TargetIntersectionNode = road.StartEndPoint[1]
                    });
                }
            }

            //compare new distance with old dist, replace if shorter
            foreach (var dijkstraSingleConnectionData in connectedIntersectionPoints)
            {
                float newDistance = dijkstraTable.Table[current].Distance + dijkstraSingleConnectionData.Distance;
                float oldDistance = dijkstraTable.Table[dijkstraSingleConnectionData.TargetIntersectionNode].Distance;

                if (newDistance < oldDistance)
                {
                    dijkstraTable.Table[dijkstraSingleConnectionData.TargetIntersectionNode].Distance             = newDistance;
                    dijkstraTable.Table[dijkstraSingleConnectionData.TargetIntersectionNode].PrevIntersectionNode =
                        current;
                }
            }

            //find next point to be checked
            PathMapBuilder.IntersectionPoint nextIntersectionPoint = current;
            float distance = float.MaxValue;

            List <PathMapBuilder.IntersectionPoint> ignoreIntersectionPoints = Checked;
            foreach (var dijkstraTableData in dijkstraTable.Table)
            {
                if (!ignoreIntersectionPoints.Contains(dijkstraTableData.Key))
                {
                    if (dijkstraTableData.Value.Distance < distance)
                    {
                        nextIntersectionPoint = dijkstraTableData.Key;
                        distance = dijkstraTableData.Value.Distance;
                    }
                }
            }
            current = nextIntersectionPoint;
        }
        return(dijkstraTable);
    }