Beispiel #1
0
        public static void setSingleTileAsChild(TileNode t, int x, int y, bool placementAction = true)
        {
            Vector2 pos = new Vector2(x * Game1.tileSize, y * Game1.tileSize);

            bool f = checkIfICanPlaceHere(t, new Vector2(pos.X, pos.Y));

            if (f == false)
            {
                return;
            }
            else
            {
                // ModCore.CoreMonitor.Log("Adding a child!");
                System.Threading.Thread.Sleep(PathFindingCore.PathFindingLogic.delay);
                TileNode child = new TileNode(1, Vector2.Zero, t.texturePath, t.dataPath, StardustCore.IlluminateFramework.Colors.invertColor(StardustCore.IlluminateFramework.ColorsList.Cyan));
                child.seenState = (int)stateEnum.NotVisited;
                child.parent    = t;
                if (placementAction)
                {
                    child.placementAction(t.thisLocation, (int)pos.X, (int)pos.Y);
                }
                else
                {
                    child.fakePlacementAction(t.thisLocation, x, y);
                }
                //StarAI.PathFindingCore.Utilities.masterAdditionList.Add(new StarAI.PathFindingCore.PlacementNode(child, Game1.currentLocation, (int)pos.X, (int)pos.Y));
                t.children.Add(child);
            }
        }
        /// <summary>
        /// This is used to pathfind to any target that statisfies conditions. The first one hit becomes the new goal.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List <TileNode> getAnyIdealPath(object obj)
        {
            object[]        objArr = (object[])obj;
            List <TileNode> vList  = (List <TileNode>)objArr[0];

            bool utility = true;

            int                     xMin      = -1;
            int                     yMin      = -1;
            int                     xMax      = 1;
            int                     yMax      = 1;
            List <TileNode>         miniGoals = new List <TileNode>();
            List <List <TileNode> > paths     = new List <List <TileNode> >();

            //try to set children to tiles where children haven't been before
            foreach (var v in vList)
            {
                for (int x = xMin; x <= xMax; x++)
                {
                    for (int y = yMin; y <= yMax; y++)
                    {
                        if (x == 0 && y == 0)
                        {
                            continue;
                        }

                        //Include these 4 checks for just left right up down movement. Remove them to enable 8 direction path finding
                        if (x == -1 && y == -1)
                        {
                            continue;                     //upper left
                        }
                        if (x == -1 && y == 1)
                        {
                            continue;                    //bottom left
                        }
                        if (x == 1 && y == -1)
                        {
                            continue;                    //upper right
                        }
                        if (x == 1 && y == 1)
                        {
                            continue;                   //bottom right
                        }
                        Vector2 pos = new Vector2(v.tileLocation.X + x, v.tileLocation.Y + y);
                        //ModCore.CoreMonitor.Log("AHHHHHHH POSITION: " + pos.ToString(), LogLevel.Alert);
                        bool f = PathFindingCore.TileNode.checkIfICanPlaceHere(v, pos * Game1.tileSize, v.thisLocation, true, utility);
                        // ModCore.CoreMonitor.Log("OK THIS IS THE RESULT F: " + f, LogLevel.Alert);
                        if (f == true)
                        {
                            TileNode t = new TileNode(1, Vector2.Zero, Path.Combine("Tiles", "GenericUncoloredTile.xnb"), Path.Combine("Tiles", "TileData.xnb"), StardustCore.IlluminateFramework.Colors.invertColor(StardustCore.IlluminateFramework.ColorsList.RosyBrown));
                            if (placement)
                            {
                                t.placementAction(v.thisLocation, (int)pos.X * Game1.tileSize, (int)pos.Y * Game1.tileSize);
                            }
                            else
                            {
                                t.fakePlacementAction(v.thisLocation, (int)pos.X, (int)pos.Y);
                            }
                            //StardustCore.Utilities.masterAdditionList.Add(new StardustCore.DataNodes.PlacementNode( t, Game1.currentLocation, (int)pos.X * Game1.tileSize, (int)pos.Y * Game1.tileSize));
                            miniGoals.Add(t);
                            Utilities.tileExceptionList.Add(new TileExceptionMetaData(t, "Navigation"));
                        }
                    }
                }
            }
            List <TileNode> removalList = new List <TileNode>();

            Utilities.clearExceptionListWithName("Child");
            Utilities.clearExceptionListWithName("Navigation");
            TileNode tempSource = new TileNode(1, Vector2.Zero, Path.Combine("Tiles", "GenericUncoloredTile.xnb"), Path.Combine("Tiles", "TileData.xnb"), StardustCore.IlluminateFramework.Colors.invertColor(StardustCore.IlluminateFramework.ColorsList.RosyBrown));

            if (placement)
            {
                tempSource.placementAction(Game1.player.currentLocation, Game1.player.getTileX() * Game1.tileSize, Game1.player.getTileY() * Game1.tileSize);
            }
            else
            {
                tempSource.fakePlacementAction(Game1.player.currentLocation, Game1.player.getTileX(), Game1.player.getTileY());
            }

            Utilities.tileExceptionList.Add(new TileExceptionMetaData(tempSource, "Navigation"));
            //StaardustCore.Utilities.masterAdditionList.Add(new StardustCore.DataNodes.PlacementNode(tempSource, Game1.currentLocation, Game1.player.getTileX() * Game1.tileSize, Game1.player.getTileY() * Game1.tileSize));


            //have this take in a list of goals and see which goal it reaches first
            List <TileNode> path = PathFindingCore.PathFindingLogic.pathFindToSingleGoalReturnPath(tempSource, miniGoals, new List <TileNode>(), placement, utility);

            ModCore.CoreMonitor.Log("OK MY PATH IS:" + path.Count);

            Utilities.clearExceptionListWithName("Child");
            Utilities.clearExceptionListWithName("Navigation");
            if (path.Count == 0)
            {
                ModCore.CoreMonitor.Log("NOPE, no path I guess.", LogLevel.Warn);
            }
            else
            {
                ModCore.CoreMonitor.Log("There is a path", LogLevel.Alert);
                ModCore.CoreMonitor.Log("COST OF THE PATH IS: " + path.Count.ToString(), LogLevel.Alert);
            }
            if (path.Count != 0)
            {
                //ModCore.CoreMonitor.Log("PATH WAS NOT NULL", LogLevel.Warn);
                paths.Add(path);
                foreach (var someTile in path)
                {
                    removalList.Add(someTile);
                    StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(someTile);
                    if (placement)
                    {
                        someTile.thisLocation.objects.Remove(someTile.tileLocation);
                    }

                    //someTile.performRemoveAction(someTile.tileLocation, someTile.thisLocation);
                    //StardustCore.Utilities.masterRemovalList.Add(someTile);
                    //ModCore.CoreMonitor.Log("CAUGHT MY CULPERATE", LogLevel.Warn);
                }
            }

            Console.WriteLine("GOALS COUNT:" + miniGoals.Count);
            foreach (var q in removalList)
            {
                StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(q);
                if (placement)
                {
                    q.thisLocation.objects.Remove(q.tileLocation);
                }
            }
            removalList.Clear();
            int             pathCost    = 999999999;
            List <TileNode> correctPath = new List <TileNode>();

            foreach (var potentialPath in paths)
            {
                if (potentialPath.Count == 0)
                {
                    continue;
                }
                if (potentialPath.Count < pathCost)
                {
                    pathCost    = potentialPath.Count;
                    correctPath = potentialPath;
                }
            }


            return(correctPath);
        }
        public static int calculatePathCost(object obj)
        {
            object[] objArr = (object[])obj;
            TileNode v      = (TileNode)objArr[0];
            bool     placement;

            try
            {
                placement = (bool)objArr[1];
            }
            catch (Exception err)
            {
                placement = true;
            }
            foreach (var q in objArr)
            {
                ModCore.CoreMonitor.Log("OK THIS IS THE RESULT !: " + q, LogLevel.Alert);
            }
            if (v == null)
            {
                ModCore.CoreMonitor.Log("WTF MARK!!!!!!: ", LogLevel.Alert);
            }
            bool moveOn = false;

            WindowsInput.InputSimulator.SimulateKeyUp(WindowsInput.VirtualKeyCode.VK_X);
            int                     xMin      = -1;
            int                     yMin      = -1;
            int                     xMax      = 1;
            int                     yMax      = 1;
            List <TileNode>         miniGoals = new List <TileNode>();
            List <List <TileNode> > paths     = new List <List <TileNode> >();

            //try to set children to tiles where children haven't been before
            for (int x = xMin; x <= xMax; x++)
            {
                for (int y = yMin; y <= yMax; y++)
                {
                    if (x == 0 && y == 0)
                    {
                        continue;
                    }

                    //Include these 4 checks for just left right up down movement. Remove them to enable 8 direction path finding
                    if (x == -1 && y == -1)
                    {
                        continue;                     //upper left
                    }
                    if (x == -1 && y == 1)
                    {
                        continue;                    //bottom left
                    }
                    if (x == 1 && y == -1)
                    {
                        continue;                    //upper right
                    }
                    if (x == 1 && y == 1)
                    {
                        continue;                   //bottom right
                    }
                    Vector2 pos = new Vector2(v.tileLocation.X + x, v.tileLocation.Y + y);
                    //ModCore.CoreMonitor.Log("AHHHHHHH POSITION: " + pos.ToString(), LogLevel.Alert);
                    bool f = PathFindingCore.TileNode.checkIfICanPlaceHere(v, pos * Game1.tileSize, v.thisLocation, true, true);
                    ModCore.CoreMonitor.Log("OK THIS IS THE RESULT F: " + f, LogLevel.Alert);
                    if (f == true)
                    {
                        TileNode t = new TileNode(1, Vector2.Zero, Path.Combine("Tiles", "GenericUncoloredTile.xnb"), Path.Combine("Tiles", "TileData.xnb"), StardustCore.IlluminateFramework.Colors.invertColor(StardustCore.IlluminateFramework.ColorsList.RosyBrown));
                        if (placement)
                        {
                            t.placementAction(v.thisLocation, (int)pos.X * Game1.tileSize, (int)pos.Y * Game1.tileSize);
                        }
                        else
                        {
                            t.fakePlacementAction(v.thisLocation, (int)pos.X, (int)pos.Y);
                        }
                        //StardustCore.Utilities.masterAdditionList.Add(new StardustCore.DataNodes.PlacementNode(t, Game1.currentLocation, (int)pos.X * Game1.tileSize, (int)pos.Y * Game1.tileSize));
                        miniGoals.Add(t);
                        //Utilities.tileExceptionList.Add(new TileExceptionMetaData(t, "CostCalculation"));
                    }
                }
            }
            List <TileNode> removalList = new List <TileNode>();

            foreach (var nav in miniGoals)
            {
                TileNode tempSource = new TileNode(1, Vector2.Zero, Path.Combine("Tiles", "GenericUncoloredTile.xnb"), Path.Combine("Tiles", "TileData.xnb"), StardustCore.IlluminateFramework.Colors.invertColor(StardustCore.IlluminateFramework.ColorsList.RosyBrown));
                if (placement)
                {
                    tempSource.placementAction(Game1.player.currentLocation, Game1.player.getTileX() * Game1.tileSize, Game1.player.getTileY() * Game1.tileSize);
                }
                else
                {
                    tempSource.fakePlacementAction(v.thisLocation, (int)v.tileLocation.X, (int)v.tileLocation.Y);
                }

                List <TileNode> path = PathFindingCore.PathFindingLogic.pathFindToSingleGoalReturnPath(tempSource, nav, new List <TileNode>(), true, true);

                Utilities.clearExceptionListWithName(placement, "Child");

                ModCore.CoreMonitor.Log(tempSource.tileLocation.ToString() + tempSource.tileLocation.ToString());
                ModCore.CoreMonitor.Log(nav.tileLocation.ToString() + nav.tileLocation.ToString());

                if (path.Count != 0)
                {
                    ModCore.CoreMonitor.Log("PATH WAS NOT NULL", LogLevel.Warn);
                    paths.Add(path);
                    foreach (var someTile in path)
                    {
                        if (someTile == nav)
                        {
                            removalList.Add(someTile);
                        }
                        StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(someTile);
                        if (placement)
                        {
                            try
                            {
                                StardewValley.Object ob = someTile.thisLocation.objects[someTile.tileLocation];
                                ModCore.CoreMonitor.Log(ob.name);
                                if (ob.name == "Twig")
                                {
                                    ModCore.CoreMonitor.Log("Culperate 2");
                                }
                                someTile.thisLocation.objects.Remove(someTile.tileLocation);
                            }
                            catch (Exception err)
                            {
                            }
                        }
                        //someTile.performRemoveAction(someTile.tileLocation, someTile.thisLocation);
                        //StardustCore.Utilities.masterRemovalList.Add(v);
                    }
                }
            }
            foreach (var q in removalList)
            {
                StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(q);
                if (placement)
                {
                    try
                    {
                        StardewValley.Object ob = q.thisLocation.objects[q.tileLocation];
                        ModCore.CoreMonitor.Log(ob.name);
                        if (ob.name == "Twig")
                        {
                            ModCore.CoreMonitor.Log("Culperate 1");
                        }
                        q.thisLocation.objects.Remove(q.tileLocation);
                    }
                    catch (Exception err)
                    {
                    }
                }
            }
            removalList.Clear();
            int             pathCost    = 999999999;
            List <TileNode> correctPath = new List <TileNode>();

            foreach (var potentialPath in paths)
            {
                if (potentialPath.Count == 0)
                {
                    continue;
                }
                if (potentialPath.Count < pathCost)
                {
                    pathCost    = potentialPath.Count;
                    correctPath = potentialPath;
                }
            }

            foreach (var goodTile in correctPath)
            {
                StardustCore.ModCore.SerializationManager.trackedObjectList.Add(goodTile);
                //goodTile.placementAction(goodTile.thisLocation, (int)goodTile.tileLocation.X * Game1.tileSize, (int)goodTile.tileLocation.Y * Game1.tileSize);
                //StardustCore.Utilities.masterAdditionList.Add(new StardustCore.DataNodes.PlacementNode(goodTile, Game1.currentLocation, (int)goodTile.tileLocation.X * Game1.tileSize, (int)goodTile.tileLocation.Y * Game1.tileSize));
            }
            //END HERE FOR JUST CALCULATING PATH COST
            if (paths.Count == 0)
            {
                return(Int32.MaxValue);
            }
            return(correctPath.Count);
        }