diff --git a/StarAI/StarAI/StarAI/Commands.cs b/StarAI/StarAI/StarAI/Commands.cs index 255bf7c8..2e2e5b4c 100644 --- a/StarAI/StarAI/StarAI/Commands.cs +++ b/StarAI/StarAI/StarAI/Commands.cs @@ -187,20 +187,14 @@ namespace StarAI { ModCore.CoreMonitor.Log("TASK IS Finished PATHFINDING", LogLevel.Warn); - ModCore.obj[0] = PathFindingLogic.source; - ModCore.obj[1] = PathFindingLogic.currentGoal; - ModCore.obj[2] = PathFindingLogic.queue; - ModCore.fun = new Task(new Action(PathFindingLogic.pathFindToSingleGoal), ModCore.obj); - return; + ModCore.fun = new Task(new Action(PathFindingLogic.pathFindToAllGoals)); + // return; } if (ModCore.fun.Status == TaskStatus.Created) { ModCore.CoreMonitor.Log("CREATE AND RUN A TASK!!! PATHFINDING!"); - ModCore.obj[0] = PathFindingLogic.source; - ModCore.obj[1] = PathFindingLogic.currentGoal; - ModCore.obj[2] = PathFindingLogic.queue; - ModCore.fun = new Task(new Action(PathFindingLogic.pathFindToSingleGoal), ModCore.obj); + ModCore.fun = new Task(new Action(PathFindingLogic.pathFindToAllGoals)); ModCore.fun.Start(); return; @@ -210,10 +204,7 @@ namespace StarAI { ModCore.CoreMonitor.Log(ModCore.fun.Exception.ToString()); ModCore.CoreMonitor.Log("CREATE AND RUN A TASK!!! PATHFINDING!"); - ModCore.obj[0] = PathFindingLogic.source; - ModCore.obj[1] = PathFindingLogic.currentGoal; - ModCore.obj[2] = PathFindingLogic.queue; - ModCore.fun = new Task(new Action(PathFindingLogic.pathFindToSingleGoal), ModCore.obj); + ModCore.fun = new Task(new Action(PathFindingLogic.pathFindToAllGoals)); } } diff --git a/StarAI/StarAI/StarAI/ModCore.cs b/StarAI/StarAI/StarAI/ModCore.cs index a7348327..6a300974 100644 --- a/StarAI/StarAI/StarAI/ModCore.cs +++ b/StarAI/StarAI/StarAI/ModCore.cs @@ -24,7 +24,7 @@ namespace StarAI public static List warpGoals = new List(); public static object[] obj = new object[3]; - public static Task fun = new Task(new Action(PathFindingLogic.pathFindToSingleGoal), obj); + public static Task fun = new Task(new Action(PathFindingLogic.pathFindToAllGoals)); public override void Entry(IModHelper helper) { obj[0] = PathFindingLogic.source; @@ -40,11 +40,11 @@ namespace StarAI PathFindingCore.Utilities.initializeTileExceptionList(); //throw new NotImplementedException(); StardewModdingAPI.Events.LocationEvents.CurrentLocationChanged += LocationEvents_CurrentLocationChanged; - StardewModdingAPI.Events.GameEvents.SecondUpdateTick += GameEvents_SecondUpdateTick; StardewModdingAPI.Events.ControlEvents.KeyPressed += ControlEvents_KeyPressed; StardewModdingAPI.Events.SaveEvents.AfterLoad += SaveEvents_AfterLoad; + StardustCore.ModCore.SerializationManager.acceptedTypes.Add("StarAI.PathFindingCore.TileNode", new StardustCore.Serialization.SerializerDataNode(new StardustCore.Serialization.SerializerDataNode.SerializingFunction(TileNode.Serialize), new StardustCore.Serialization.SerializerDataNode.ParsingFunction(TileNode.ParseIntoInventory), new StardustCore.Serialization.SerializerDataNode.WorldParsingFunction(TileNode.SerializeFromWorld), new StardustCore.Serialization.SerializerDataNode.SerializingToContainerFunction(TileNode.Serialize))); } @@ -55,7 +55,10 @@ namespace StarAI public void loadExceptionTiles() { - + if (!Directory.Exists(Path.Combine(CoreHelper.DirectoryPath, PathFindingCore.Utilities.folderForExceptionTiles))) + { + Directory.CreateDirectory(Path.Combine(CoreHelper.DirectoryPath, PathFindingCore.Utilities.folderForExceptionTiles)); + } // Process the list of files found in the directory. string[] fileEntries = Directory.GetFiles(Path.Combine(CoreHelper.DirectoryPath,PathFindingCore.Utilities.folderForExceptionTiles)); foreach (string fileName in fileEntries) @@ -180,35 +183,7 @@ namespace StarAI - private void GameEvents_SecondUpdateTick(object sender, EventArgs e) - { - if (Game1.player == null) return; - if (Game1.hasLoadedGame == false) return; - // ModCore.CoreMonitor.Log(Game1.player.currentLocation.isTileLocationOpen(new xTile.Dimensions.Location((int)(Game1.player.getTileX() + 1)*Game1.tileSize, (int)(Game1.player.getTileY())*Game1.tileSize)).ToString()); - //CoreMonitor.Log(Convert.ToString(warpGoals.Count)); - if (warpGoals.Count == 0) return; - if (fun.Status == TaskStatus.Running) - { - //CoreMonitor.Log("TASK IS RUNNING", LogLevel.Alert); - return; - } - if (fun.Status == TaskStatus.RanToCompletion) - { - - //CoreMonitor.Log("TASK IS Finished", LogLevel.Warn); - fun = new Task(new Action(PathFindingCore.Utilities.calculateMovement)); - return; - } - - if (fun.Status == TaskStatus.Created) - { - //CoreMonitor.Log("CREATE AND RUN A TASK!!!"); - fun.Start(); - return; - } - - } - + private void LocationEvents_CurrentLocationChanged(object sender, StardewModdingAPI.Events.EventArgsCurrentLocationChanged e) { CoreMonitor.Log("LOCATION CHANGED!"); diff --git a/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs b/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs index 74e51c11..7adcee05 100644 --- a/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs +++ b/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs @@ -7,20 +7,65 @@ using System.Threading.Tasks; using StarAI; using StardewModdingAPI; using StardewValley; +using WindowsInput; namespace StarAI.PathFindingCore { class PathFindingLogic { public static TileNode source; - public static List goals=new List(); - public static List queue=new List(); + public static List goals = new List(); + public static List queue = new List(); public static int totalPathCost; public static TileNode currentGoal; public static int delay; - public static List path=new List(); + public static List path = new List(); public static int index = 0; + + + public static void pathFindToAllGoals() + { + List cleanseGoals = new List(); + foreach (var v in goals) + { + Commands.pathfind("path to all goals", new string[]{ + "addStart", + "currentPosition" + }); + queue = new List(); + currentGoal = v; + pathFindToSingleGoal(source, v, queue); //v is a goal in my goal list, queue is my queue to work with,and I always set my start to where I am at. + List removalList = new List(); + foreach(var tile in path) + { + removalList.Add(tile); + } + foreach (var tile in removalList) + { + StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(tile); + (tile as TileNode).thisLocation.objects.Remove(tile.tileLocation); + } + cleanseGoals.Add(v); + } + foreach(var v in cleanseGoals) + { + goals.Remove(v); + } + + + } + + + public static void pathFindToSingleGoal(TileNode Source, TileNode Goal, List Queue) + { + object[] obj = new object[3]; + obj[0] = Source; + obj[1] = Goal; + obj[2] = Queue; + pathFindToSingleGoal(obj); + } + public static void pathFindToSingleGoal(object data) { ModCore.CoreMonitor.Log("LET'S GO!!!!", LogLevel.Error); @@ -132,13 +177,13 @@ namespace StarAI.PathFindingCore } - if (currentNode.tileLocation != currentGoal.tileLocation) + if (currentNode.tileLocation != Goal.tileLocation) { ModCore.CoreMonitor.Log("NO PATH FOUND", LogLevel.Error); return; } - if (currentNode.tileLocation == currentGoal.tileLocation) + if (currentNode.tileLocation == Goal.tileLocation) { ModCore.CoreMonitor.Log("SWEET BEANS!!!!!!", LogLevel.Error); queue.Clear(); @@ -177,8 +222,136 @@ namespace StarAI.PathFindingCore path.Add(currentNode); } } + List removalList = new List(); + foreach(var v in StardustCore.ModCore.SerializationManager.trackedObjectList) + { + if(v is TileNode) + { + if (path.Contains(v) || goals.Contains(v)) + { + continue; + } + else + { + removalList.Add((TileNode)v); + } + } + } + foreach(var v in removalList) + { + StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(v); + (v as TileNode).thisLocation.objects.Remove(v.tileLocation); + } + calculateMovement(path); + // goals.Remove(Goal); + } + public static void calculateMovement(List path) + { + path.Reverse(); + bool xTargetReached = false; + bool yTargetReached = false; + while (path.Count > 0) + { + TileNode w = path[0]; + ModCore.CoreMonitor.Log("Goto: " + w.tileLocation.ToString()); + ModCore.CoreMonitor.Log("My position now: " + Game1.player.getTileLocation()); + //ModCore.CoreMonitor.Log("My Point position now: " + Game1.player.getTileLocationPoint()); + if (Game1.player.getTileX() == w.tileLocation.X && Game1.player.getTileY() == w.tileLocation.Y) + { + path.Remove(w); + w.thisLocation.objects.Remove(w.tileLocation); + xTargetReached = false; + yTargetReached = false; + StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(w); + ModCore.CoreMonitor.Log("LOOOP", LogLevel.Debug); + // return; + continue; + } + else + { + Vector2 center = Utilities.parseCenterFromTile((int)w.tileLocation.X, (int)w.tileLocation.Y); + while (Game1.player.position.X > center.X && xTargetReached == false) + { + if (Utilities.isWithinRange(Game1.player.position.X, center.X, 6) == true) + { + ModCore.CoreMonitor.Log("XXXXXXXtargetReached"); + xTargetReached = true; + InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_A); + //break; + continue; + } + InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_A); + } + while (Game1.player.position.X < center.X && xTargetReached == false) + { + ModCore.CoreMonitor.Log("Player x: " + Game1.player.position.X); + ModCore.CoreMonitor.Log("center x: " + center.X); + if (Utilities.isWithinRange(Game1.player.position.X, center.X, 6) == true) + { + xTargetReached = true; + ModCore.CoreMonitor.Log("XXXXXXXtargetReached"); + InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_D); + continue; + } + InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_D); + } + while (Game1.player.position.Y < center.Y && yTargetReached == false) + { + ModCore.CoreMonitor.Log("banana"); + if (Utilities.isWithinRange(Game1.player.position.Y, center.Y, 6) == true) + { + yTargetReached = true; + ModCore.CoreMonitor.Log("YYYYYYYYYtargetReached"); + InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_S); + continue; + } + InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_S); + } + while (Game1.player.position.Y > center.Y && yTargetReached == false) + { + ModCore.CoreMonitor.Log("potato"); + if (Utilities.isWithinRange(Game1.player.position.Y, center.Y, 6) == true) + { + yTargetReached = true; + ModCore.CoreMonitor.Log("YYYYYYYYYtargetReached"); + InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_W); + continue; + } + InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_W); + } + + + if (xTargetReached == true && yTargetReached == true) + { + path.Remove(w); + + + + xTargetReached = false; + yTargetReached = false; + StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(w); + w.thisLocation.objects.Remove(w.tileLocation); + + InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_A); + InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_D); + InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_W); + InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_S); + //return; + ModCore.CoreMonitor.Log("Reached goal!", LogLevel.Error); + //Game1.player.position = new Vector2(center.X, center.Y); + continue; + } + + + ModCore.CoreMonitor.Log("UNCAUGHT EXCEPTION", LogLevel.Error); + } + } + + } + + } } diff --git a/StarAI/StarAI/StarAI/PathFindingCore/TileNodeObject.cs b/StarAI/StarAI/StarAI/PathFindingCore/TileNodeObject.cs index 4fddcb35..9af89c5a 100644 --- a/StarAI/StarAI/StarAI/PathFindingCore/TileNodeObject.cs +++ b/StarAI/StarAI/StarAI/PathFindingCore/TileNodeObject.cs @@ -129,93 +129,6 @@ namespace StarAI.PathFindingCore } } - public void setAdjacentTiles(bool recursive) - { - Vector2 startPosition = this.tileLocation; - - int xMin=-1; - int yMin=-1; - int xMax=1; - int yMax = 1; - - for (int x = xMin; x <= xMax; x++) - { - for (int y = yMin; y <= yMax; y++) - { - if (x == 0 && y == 0) continue; - int xPos = (int)(this.tileLocation.X + x) * Game1.tileSize; - int yPos = (int)(this.tileLocation.Y + y) * Game1.tileSize; - ModCore.CoreMonitor.Log("ATTEMPTING TO PLACE ITEM AT: " + new Vector2(this.tileLocation.X + x, this.tileLocation.Y + y)); - Rectangle r = new Rectangle(xPos, yPos, Game1.tileSize, Game1.tileSize); - Vector2 pos = new Vector2(r.X, r.Y); - - ModCore.CoreMonitor.Log("THIS IS MY LOCATION!!!: " + this.thisLocation.name); - - bool ok = checkIfICanPlaceHere(this,pos,null,recursive); - if (ok == false) continue; - if (this.thisLocation.isTileLocationOpen(new xTile.Dimensions.Location((int) (pos.X) ,(int) (pos.Y)) )==true && this.thisLocation.isObjectAt((int) (pos.X),(int) (pos.Y))==false) ; //&& Game1.currentLocation.isTileLocationOpen(new xTile.Dimensions.Location((int)startPosition.X+x,(int)startPosition.Y+y))) - { - TileNode child = new TileNode(1,Vector2.Zero,this.texturePath,this.drawColor); - child.placementAction(Game1.currentLocation, xPos, yPos); - this.children.Add(child); - } - } - } - } - - - //Unused???? - public static void setAdjacentTiles(Vector2 position, GameLocation loc) - { - Vector2 startPosition = position; - - int xMin = -1; - int yMin = -1; - int xMax = 1; - int yMax = 1; - for (int x = xMin; x <= xMax; x++) - { - for (int y = yMin; y <= yMax; y++) - { - int xPos = (int)(startPosition.X + x) * Game1.tileSize; - int yPos = (int)(startPosition.Y + y) * Game1.tileSize; - // ModCore.CoreMonitor.Log("ATTEMPTING TO PLACE ITEM AT: " + new Vector2(this.tileLocation.X + x, this.tileLocation.Y + y)); - Rectangle r = new Rectangle(xPos, yPos, Game1.tileSize, Game1.tileSize); - Vector2 pos = new Vector2(r.X, r.Y); - - ModCore.CoreMonitor.Log("THIS IS MY LOCATION!!!: " +loc.name); - if (loc.isObjectAt((int)pos.X, (int)pos.Y)) - { - // ModCore.CoreMonitor.Log("F!: " + loc.name, LogLevel.Warn); - continue; - } - if (loc.isTileOccupied(pos / Game1.tileSize)) - { - //ModCore.CoreMonitor.Log("K!!!!: " + loc.name, LogLevel.Error); - continue; - } - - if (loc.isTilePlaceable(pos) == false) - { - //ModCore.CoreMonitor.Log("C!!!!: " + loc.name, LogLevel.Error); - //continue; - } - if (loc.isTilePlaceable(pos / Game1.tileSize) == false) - { - // ModCore.CoreMonitor.Log("J!!!!: " + loc.name, LogLevel.Error); - // continue; - } - - - if (loc.isTilePassable(new Rectangle(xPos, yPos, Game1.tileSize, Game1.tileSize), Game1.viewport) == true && loc.isTileLocationOpen(new xTile.Dimensions.Location((int)(pos.X), (int)(pos.Y)))) ; //&& Game1.currentLocation.isTileLocationOpen(new xTile.Dimensions.Location((int)startPosition.X+x,(int)startPosition.Y+y))) - { - //TileNode child = new TileNode(1, Vector2.Zero, this.texturePath, this.drawColor); - //child.placementAction(Game1.currentLocation, xPos, yPos); - //this.children.Add(child); - } - } - } - } public override string Name diff --git a/StarAI/StarAI/StarAI/PathFindingCore/Utilities.cs b/StarAI/StarAI/StarAI/PathFindingCore/Utilities.cs index 6e8a7ec0..1614224b 100644 --- a/StarAI/StarAI/StarAI/PathFindingCore/Utilities.cs +++ b/StarAI/StarAI/StarAI/PathFindingCore/Utilities.cs @@ -28,94 +28,6 @@ namespace StarAI.PathFindingCore } - - - public static void calculateMovement() - { - bool xTargetReached = false; - bool yTargetReached = false; - while (ModCore.warpGoals.Count > 0) - { - Warp w = ModCore.warpGoals[0]; - if (Game1.player.getTileX() == w.X && Game1.player.getTileY() == w.Y) - { - ModCore.warpGoals.Remove(w); - ModCore.CoreMonitor.Log("LOOOP", LogLevel.Debug); - // return; - continue; - } - else - { - Vector2 center = parseCenterFromTile(w.X, w.Y); - while (Game1.player.position.X > center.X && xTargetReached == false) - { - if (isWithinRange(Game1.player.position.X, center.X, 12) == true) - { - ModCore.CoreMonitor.Log("XXXXXXXtargetReached"); - xTargetReached = true; - InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_A); - //break; - continue; - } - InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_A); - } - while (Game1.player.position.X < center.X && xTargetReached == false) - { - if (isWithinRange(Game1.player.position.X, center.X, 6) == true) - { - xTargetReached = true; - ModCore.CoreMonitor.Log("XXXXXXXtargetReached"); - InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_D); - continue; - } - InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_D); - } - ModCore.CoreMonitor.Log("Run???"); - while (Game1.player.position.Y < center.Y && yTargetReached == false) - { - ModCore.CoreMonitor.Log("banana"); - if (isWithinRange(Game1.player.position.Y, center.Y, 6) == true) - { - yTargetReached = true; - ModCore.CoreMonitor.Log("YYYYYYYYYtargetReached"); - InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_S); - continue; - } - InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_S); - } - ModCore.CoreMonitor.Log("Or no???"); - while (Game1.player.position.Y > center.Y && yTargetReached == false) - { - ModCore.CoreMonitor.Log("potato"); - if (isWithinRange(Game1.player.position.Y, center.Y, 6) == true) - { - yTargetReached = true; - ModCore.CoreMonitor.Log("YYYYYYYYYtargetReached"); - InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_W); - continue; - } - InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_W); - } - - - if (xTargetReached == true && yTargetReached == true) - { - ModCore.warpGoals.Remove(w); - InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_A); - InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_D); - InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_W); - InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_S); - //return; - ModCore.CoreMonitor.Log("Reached goal!", LogLevel.Error); - continue; - } - - - ModCore.CoreMonitor.Log("UNCAUGHT EXCEPTION", LogLevel.Error); - } - } - } - /// /// Used to calculate center of a tile with varience. ///