Example #1
0
        public List <DrivingNode> Go(Point startPoint, Point endPoint, Reusables reusables)
        {
            reusables.Clear();
            this.startPoint = startPoint;
            this.endPoint   = endPoint;
            foundTarget     = false;
            Point     workingPoint;
            AStarNode workingNode;

            reusables.toCheckList.Add(GetOneD(startPoint), new AStarNode(GetOneD(startPoint), GetOneD(startPoint), 0, 0, 255, true));

            for (int i = 0; i < maximumToCheck; i++)
            {
                if (NextRoundChecks(reusables, out workingPoint, out workingNode))
                {
                    AddNewPoints(reusables, workingPoint, workingNode);
                }
                else
                {
                    break;
                }
            }

            if (foundTarget)
            {
                //Debugging(checkedNodeList, toCheckList);
                return(GetDrivingNodeList(reusables, startPoint));
            }
            else
            {
                return(new List <DrivingNode>());
            }
        }
Example #2
0
        static IEnumerable <XNode> RemoveRegionsAndResolveReusables(Reusables r)
        {
            var e = r.Element;

            using (r.Monitor.OpenDebug($"Processing {e.ToStringPath()}."))
            {
                if (e.Name == "Reuse")
                {
                    return(r.Apply(e));
                }
                var children = e.Nodes()
                               .SelectMany(n => n is XElement c ? RemoveRegionsAndResolveReusables(new Reusables(r, c)) : new[] { n });
                if (e.Name == "Region")
                {
                    return(children);
                }
                if (e.Name == "Reusable")
                {
                    bool replace   = (bool?)e.Attribute("Replace") ?? false;
                    bool @override = (bool?)e.Attribute("Override") ?? false;
                    r.Parent.Add((string)e.AttributeRequired("Name"), children.ToList(), replace, @override);
                    return(Enumerable.Empty <XElement>());
                }
                var attr = e.Attributes().Select(a => new XAttribute(a).SetLineColumnInfo(a));
                return(new[] { new XElement(e.Name, attr, children).SetLineColumnInfo(e) });
            }
        }
Example #3
0
 public Reusables(Reusables directParent, XElement e)
 {
     Element = e;
     Monitor = directParent.Monitor;
     while (directParent.Element.Name == "Region")
     {
         directParent = directParent.Parent;
     }
     Parent = directParent;
 }
Example #4
0
        private void AddNewPoints(Reusables reusables, Point workingPoint, AStarNode workingNode)
        {
            for (byte i = 0; i < 8; i += 2)
            {
                Point newPoint = workingPoint + AngleStuff.directionPoint[i];

                if (workingPoint == Point.Zero)
                {
                    string BUG = "";
                    Debug.WriteLine("BUGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG We hit an empty point on an astar");
                }
                else
                {
                    if (WorldController.world.tileGrid[newPoint.X, newPoint.Y].IsRoad() && !WorldController.world.tileGrid[newPoint.X, newPoint.Y].tileLogistic.IsDirectionBlock(i))
                    {
                        if (!IsInLists(newPoint, reusables))
                        {
                            int count     = 1;
                            int tileScore = WorldController.world.tileGrid[newPoint.X, newPoint.Y].tileLogistic.pathFindingScore;
                            int roadId    = WorldController.world.tileGrid[newPoint.X, newPoint.Y].tileLogistic.roadId;

                            TileLogistic workingLogistic = WorldController.world.tileGrid[workingPoint.X, workingPoint.Y].tileLogistic;
                            TileLogistic newLogistic     = WorldController.world.tileGrid[newPoint.X, newPoint.Y].tileLogistic;


                            while (newLogistic.roadId == workingLogistic.roadId && newLogistic.roadId < 12 && !EndOrDirectionBlockOrStop(newPoint, newLogistic, i))
                            {
                                if (!IsInLists(newPoint, reusables))
                                {
                                    count++;
                                    newPoint    = newPoint + AngleStuff.directionPoint[i];
                                    newLogistic = WorldController.world.tileGrid[newPoint.X, newPoint.Y].tileLogistic;
                                }
                                else
                                {
                                    break;
                                }
                            }

                            while (count > -1 && reusables.toCheckList.ContainsKey(GetOneD(newPoint)))
                            {
                                newPoint = newPoint + AngleStuff.directionPoint[AngleStuff.RotateDirection(i, 4)];
                                count--;
                            }

                            if (count > 0 && !reusables.toCheckList.ContainsKey(GetOneD(newPoint)))
                            {
                                int id = GetOneD(newPoint);
                                reusables.toCheckList.Add(id, new AStarNode(id, GetOneD(workingPoint), CalcNScore(tileScore * count, workingNode), CalcHScore(newPoint), i, true));
                            }
                        }
                    }
                }
            }
        }
Example #5
0
        private bool NextRoundChecks(Reusables reusables, out Point workingPoint, out AStarNode workingNode)
        {
            if (reusables.toCheckList.Count > 0)
            {
                workingNode = reusables.toCheckList.ElementAt(0).Value;

                foreach (AStarNode nodeCheck in reusables.toCheckList.Values)
                {
                    if (nodeCheck.GetTotalScore() < workingNode.GetTotalScore())
                    {
                        workingNode = nodeCheck;
                    }
                }
                reusables.toCheckList.Remove(workingNode.currentPoint);

                if (reusables.checkedNodeList.ContainsKey(workingNode.currentPoint))
                {
                    AStarNode existing = reusables.checkedNodeList[workingNode.currentPoint];
                    if (workingNode.GetTotalScore() < existing.GetTotalScore())     //Its a better version
                    {
                        reusables.checkedNodeList.Remove(workingNode.currentPoint);
                        reusables.checkedNodeList.Add(workingNode.currentPoint, workingNode);
                    }
                    else  //Using recursive lets get the next best one
                    {
                        NextRoundChecks(reusables, out workingPoint, out workingNode);
                    }
                }
                else
                {
                    reusables.checkedNodeList.Add(workingNode.currentPoint, workingNode);
                }

                workingPoint = GetTwoD(workingNode.currentPoint);

                if (workingNode.isRoad)
                {
                    endPoint  = workingNode.GetCurrent2D();
                    foundRoad = true;
                    return(false);
                }
                else if (workingPoint == endPoint)
                {
                    foundTarget = true;
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            workingPoint = Point.Zero;
            workingNode  = new AStarNode(0, 0, 0, 0, 0, false);
            return(false);
        }
Example #6
0
 private bool IsInLists(Point newPoint, Reusables reusables)
 {
     if (reusables.checkedNodeList.ContainsKey(GetOneD(newPoint)))
     {
         return(true);
     }
     if (reusables.toCheckList.ContainsKey(GetOneD(newPoint)))
     {
         return(true);
     }
     return(false);
 }
Example #7
0
        private void AddNewPoints(Reusables reusables, Point workingPoint, AStarNode workingNode)
        {
            //Diagonals
            for (byte i = 1; i < 8; i += 2)
            {
                Point newPoint = workingPoint + AngleStuff.directionPoint[i];

                if (WorldController.world.tileGrid[newPoint.X, newPoint.Y].IsRoad() || WorldController.world.tileGrid[newPoint.X, newPoint.Y].IsOpen())
                {
                    Point diagonal1 = workingPoint + AngleStuff.directionPoint[i - 1];
                    Point diagonal2 = workingPoint + AngleStuff.directionPoint[i + 1];

                    if (WorldController.world.tileGrid[diagonal1.X, diagonal1.Y].IsDrivable() && WorldController.world.tileGrid[diagonal2.X, diagonal2.Y].IsDrivable())
                    {
                        if (!WorldController.world.tileGrid[newPoint.X, newPoint.Y].IsParkedAndLockedVehicle(currentVehicle))
                        {
                            if (!IsInLists(newPoint, reusables))
                            {
                                int  tileScore       = WorldController.world.tileGrid[newPoint.X, newPoint.Y].tileLogistic.pathFindingScore;
                                bool isRoad          = WorldController.world.tileGrid[newPoint.X, newPoint.Y].tileLogistic.IsRoad();
                                int  id              = GetOneD(newPoint);
                                bool directionChange = workingNode.directionFrom != i;

                                reusables.toCheckList.Add(id, new AStarNode(id, GetOneD(workingPoint), CalcNScoreWithDiagonal(tileScore, workingNode, directionChange), CalcHScore(newPoint), i, isRoad));
                            }
                        }
                    }
                }
            }


            //Straights
            for (byte i = 0; i < 8; i += 2)
            {
                Point newPoint = workingPoint + AngleStuff.directionPoint[i];

                if (WorldController.world.tileGrid[newPoint.X, newPoint.Y].IsDrivable())
                {
                    if (!WorldController.world.tileGrid[newPoint.X, newPoint.Y].IsParkedAndLockedVehicle(currentVehicle))
                    {
                        if (!IsInLists(newPoint, reusables))
                        {
                            int  tileScore       = WorldController.world.tileGrid[newPoint.X, newPoint.Y].tileLogistic.pathFindingScore;
                            bool isRoad          = WorldController.world.tileGrid[newPoint.X, newPoint.Y].tileLogistic.IsRoad();
                            int  id              = GetOneD(newPoint);
                            bool directionChange = workingNode.directionFrom != i;
                            //int directionChange = directionChangeCost * (Directions.GetDirectionDifference(workingNode.directionFrom, i));
                            reusables.toCheckList.Add(id, new AStarNode(id, GetOneD(workingPoint), CalcNScore(tileScore, workingNode, directionChange), CalcHScore(newPoint), i, isRoad));
                        }
                    }
                }
            }
        }
Example #8
0
    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(this);
            Debug.LogError("Tried to create more than one singleton!");
        }

        hexPathHighLights = new List <GameObject>();
        for (int i = 0; i < 128; i++)
        {
            hexPathHighLights.Add(Instantiate(hexPathHighLightPreFab));
            hexPathHighLights[i].SetActive(false);
        }
    }
Example #9
0
        //we need to order it our next tile to drive to is the end off the list
        //as we want to pull them off the end
        public List <DrivingNode> GetDrivingNodeList(Reusables reusables, bool reversed, Point startPoint, Point endPoint, bool keepDestination)
        {
            List <DrivingNode> drivingNodeList = new List <DrivingNode>();

            AStarNode currentNode  = reusables.checkedNodeList[GetOneD(endPoint)];
            Point     currentPoint = currentNode.GetCurrent2D();


            while (currentPoint != startPoint)
            {
                currentPoint = currentNode.GetCurrent2D();
                drivingNodeList.Add(new DrivingNode(currentPoint.X, currentPoint.Y));
                currentPoint = currentNode.GetPrevious2D();
                currentNode  = reusables.checkedNodeList[GetOneD(currentPoint)];
            }

            drivingNodeList.Add(new DrivingNode(currentPoint.X, currentPoint.Y));

            if (drivingNodeList.Count > 0)
            {
                if (reversed)
                {
                    //We need to pop the first from the list
                    drivingNodeList.RemoveAt(0);
                }
                else
                {
                    drivingNodeList.RemoveAt(drivingNodeList.Count - 1);
                }
            }

            if (keepDestination)
            {
                drivingNodeList.Add(new DrivingNode(startPoint.X, startPoint.Y));
            }

            return(AddDirectionTurns(drivingNodeList, startPoint));
        }
Example #10
0
        //we need to order it our next tile to drive to is the end off the list
        //as we want to pull them off the end
        public List <DrivingNode> GetDrivingNodeList(Reusables reusables, Point startPoint)
        {
            List <DrivingNode> drivingNodeList = new List <DrivingNode>();

            AStarNode currentNode  = reusables.checkedNodeList[GetOneD(endPoint)];
            Point     currentPoint = currentNode.GetCurrent2D();

            while (currentPoint != startPoint)
            {
                currentPoint = currentNode.GetCurrent2D();
                Point reachPoint = currentNode.GetPrevious2D();

                int direction = currentNode.GetMirrorDirection();

                do
                {
                    drivingNodeList.Add(new DrivingNode(currentPoint.X, currentPoint.Y));
                    currentPoint = AngleStuff.AddPointToDirection(currentPoint, direction);
                }while (currentPoint != reachPoint);
                //probably a better way to do this, but if we reached our reach point we do need to add it one more

                //   drivingNodeList.Add(new DrivingNode(currentPoint.X, currentPoint.Y));



                currentNode = reusables.checkedNodeList[GetOneD(currentPoint)];
            }

            if (drivingNodeList.Count > 0)
            {
                //We need to pop the last from the list
                //   drivingNodeList.RemoveAt(drivingNodeList.Count - 1);
            }

            return(AddDirectionTurns(drivingNodeList, startPoint));
        }
Example #11
0
        public List <DrivingNode> Go(Point startPoint, Point endPointZ, bool reversed, out SearchResult searchResult, out Point midPoint, Vehicle currentVehicle, bool keepDestination, Reusables reusables)
        {
            reusables.Clear();
            //We were not using this before.  Does it affect anything.  Will it block vehilces in?
            this.currentVehicle = currentVehicle;
            this.startPoint     = startPoint;
            endPoint            = endPointZ;
            foundTarget         = false;
            foundRoad           = false;
            Point     workingPoint;
            AStarNode workingNode;

            reusables.toCheckList.Add(GetOneD(startPoint), new AStarNode(GetOneD(startPoint), GetOneD(startPoint), 0, 0, 0, false));

            for (int i = 0; i < maximumToCheck; i++)
            {
                if (NextRoundChecks(reusables, out workingPoint, out workingNode))
                {
                    AddNewPoints(reusables, workingPoint, workingNode);
                }
                else
                {
                    break;
                }
            }

            //         Debugging(checkedNodeList, toCheckList);
            if (foundTarget)
            {
                searchResult = SearchResult.EndFound;
                midPoint     = GetMidPoint(reversed);
                //Debugging(checkedNodeList, toCheckList);
                return(GetDrivingNodeList(reusables, reversed, startPoint, endPoint, keepDestination));
            }
            else if (foundRoad)
            {
                searchResult = SearchResult.RoadFound;
                midPoint     = GetMidPoint(reversed);
                //Debugging(checkedNodeList, toCheckList);
                return(GetDrivingNodeList(reusables, reversed, startPoint, endPoint, keepDestination));
            }
            else
            {
                searchResult = SearchResult.None;
                midPoint     = GetMidPoint(reversed);
                //Debugging(checkedNodeList, toCheckList);
                return(new List <DrivingNode>());
            }
        }
Example #12
0
 public virtual void Start(Reusables reusables)
 {
 }
Example #13
0
 public JobWorker()
 {
     jobQueue  = new ConcurrentQueue <Job>();
     reusables = new Reusables();
 }