From ea2e7c134f2514c8dae5470c382dc30b5a99b077 Mon Sep 17 00:00:00 2001 From: Date: Sat, 2 Dec 2017 21:53:30 -0800 Subject: [PATCH] Fixed a really dumb stack overflow error on pathfinding across the world. THANK YOU. --- StarAI/StarAI/CheatCore/DoorsToWarps.cs | 36 ++++ StarAI/StarAI/ModCore.cs | 25 ++- .../MapTransitionLogic/WarpGoal.cs | 171 ++++++++++++++---- .../{ => TileNodes}/PlacementNode.cs | 0 .../{ => TileNodes}/TileExceptionMetaData.cs | 0 .../{ => TileNodes}/TileExceptionNode.cs | 0 .../{ => TileNodes}/TileNodeObject.cs | 70 ++++++- StarAI/StarAI/PathFindingCore/Utilities.cs | 20 +- StarAI/StarAI/StarAI.csproj | 9 +- 9 files changed, 273 insertions(+), 58 deletions(-) create mode 100644 StarAI/StarAI/CheatCore/DoorsToWarps.cs rename StarAI/StarAI/PathFindingCore/{ => TileNodes}/PlacementNode.cs (100%) rename StarAI/StarAI/PathFindingCore/{ => TileNodes}/TileExceptionMetaData.cs (100%) rename StarAI/StarAI/PathFindingCore/{ => TileNodes}/TileExceptionNode.cs (100%) rename StarAI/StarAI/PathFindingCore/{ => TileNodes}/TileNodeObject.cs (93%) diff --git a/StarAI/StarAI/CheatCore/DoorsToWarps.cs b/StarAI/StarAI/CheatCore/DoorsToWarps.cs new file mode 100644 index 00000000..d0470452 --- /dev/null +++ b/StarAI/StarAI/CheatCore/DoorsToWarps.cs @@ -0,0 +1,36 @@ +using StardewValley; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StarAI.CheatCore +{ + class DoorsToWarps + { + + public static void makeAllDoorsWarps() + { + foreach(var v in Game1.locations) + { + foreach(var door in v.doors) + { + ModCore.CoreMonitor.Log(v.name.ToString()); + ModCore.CoreMonitor.Log(door.Key.ToString()); + ModCore.CoreMonitor.Log(door.Value); + + foreach(var warp in Game1.getLocationFromName(door.Value).warps) + { + if (warp.TargetName == v.name && warp.TargetX==door.Key.X&& warp.TargetY==door.Key.Y+1) + { + Warp w = new Warp(door.Key.X, door.Key.Y, door.Value, warp.X, warp.Y - 1,false); + v.warps.Add(w); + } + } + + } + } + } + } +} diff --git a/StarAI/StarAI/ModCore.cs b/StarAI/StarAI/ModCore.cs index da77b3f4..057f5843 100644 --- a/StarAI/StarAI/ModCore.cs +++ b/StarAI/StarAI/ModCore.cs @@ -42,7 +42,7 @@ namespace StarAI PathFindingCore.Utilities.initializeTileExceptionList(); ExecutionCore.TaskMetaDataHeuristics.initializeToolCostDictionary(); //throw new NotImplementedException(); - StardewModdingAPI.Events.LocationEvents.CurrentLocationChanged += LocationEvents_CurrentLocationChanged; + //StardewModdingAPI.Events.LocationEvents.CurrentLocationChanged += LocationEvents_CurrentLocationChanged; StardewModdingAPI.Events.ControlEvents.KeyPressed += ControlEvents_KeyPressed; StardewModdingAPI.Events.SaveEvents.AfterLoad += SaveEvents_AfterLoad; @@ -50,6 +50,7 @@ namespace StarAI StardewModdingAPI.Events.SaveEvents.BeforeSave += SaveEvents_BeforeSave; StardustCore.ModCore.SerializationManager.acceptedTypes.Add("StarAI.PathFindingCore.TileNode", new StardustCore.Serialization.SerializerDataNode(new StardustCore.Serialization.SerializerDataNode.SerializingFunction(StarAI.PathFindingCore.TileNode.Serialize), new StardustCore.Serialization.SerializerDataNode.ParsingFunction(StarAI.PathFindingCore.TileNode.ParseIntoInventory), new StardustCore.Serialization.SerializerDataNode.WorldParsingFunction(StarAI.PathFindingCore.TileNode.SerializeFromWorld), new StardustCore.Serialization.SerializerDataNode.SerializingToContainerFunction(StarAI.PathFindingCore.TileNode.Serialize))); + } private void SaveEvents_BeforeSave(object sender, EventArgs e) @@ -81,6 +82,7 @@ namespace StarAI private void SaveEvents_AfterLoad(object sender, EventArgs e) { loadExceptionTiles(); + CheatCore.DoorsToWarps.makeAllDoorsWarps(); } public void loadExceptionTiles() @@ -138,11 +140,8 @@ namespace StarAI //K key for placing a tile. #region - if (e.KeyPressed == Microsoft.Xna.Framework.Input.Keys.H) - { - CoreMonitor.Log(Game1.player.position.ToString()); - } - if (e.KeyPressed == Microsoft.Xna.Framework.Input.Keys.K) + + if (e.KeyPressed == Microsoft.Xna.Framework.Input.Keys.K) { CoreMonitor.Log("OK THE K KEY WAS PRESSED!"); @@ -198,12 +197,12 @@ namespace StarAI ModCore.CoreMonitor.Log("SHITTTTTT: " + layer, LogLevel.Error); } - int tileIndex = Game1.player.currentLocation.getTileIndexAt((int)Game1.player.getTileX() / Game1.tileSize, (int)Game1.player.getTileY() / Game1.tileSize, layer); + int tileIndex = Game1.player.currentLocation.getTileIndexAt((int)Game1.player.getTileX(), (int)Game1.player.getTileY(), layer); if (tileIndex == -1) continue; - //ModCore.CoreMonitor.Log("Position: " + (Game1.player.getTileLocation() / Game1.tileSize).ToString(), LogLevel.Warn); - //ModCore.CoreMonitor.Log("Layer: " + layer, LogLevel.Warn); - //ModCore.CoreMonitor.Log("Index: " + tileIndex.ToString(), LogLevel.Warn); - //ModCore.CoreMonitor.Log("Image Source: " + v.ImageSource, LogLevel.Warn); + ModCore.CoreMonitor.Log("Position: " + (Game1.player.getTileLocation()).ToString(), LogLevel.Warn); + ModCore.CoreMonitor.Log("Layer: " + layer, LogLevel.Warn); + ModCore.CoreMonitor.Log("Index: " + tileIndex.ToString(), LogLevel.Warn); + ModCore.CoreMonitor.Log("Image Source: " + v.ImageSource, LogLevel.Warn); if (layer == "Buildings") { @@ -216,8 +215,8 @@ namespace StarAI return; //tile is already initialized. } } - PathFindingCore.Utilities.ignoreCheckTiles.Add(tileException); - tileException.serializeJson(Path.Combine(ModCore.CoreHelper.DirectoryPath, PathFindingCore.Utilities.folderForExceptionTiles)); + //PathFindingCore.Utilities.ignoreCheckTiles.Add(tileException); + // tileException.serializeJson(Path.Combine(ModCore.CoreHelper.DirectoryPath, PathFindingCore.Utilities.folderForExceptionTiles)); //StardustCore.ModCore.SerializationManager. } } diff --git a/StarAI/StarAI/PathFindingCore/MapTransitionLogic/WarpGoal.cs b/StarAI/StarAI/PathFindingCore/MapTransitionLogic/WarpGoal.cs index 8ecec252..4f5de3c5 100644 --- a/StarAI/StarAI/PathFindingCore/MapTransitionLogic/WarpGoal.cs +++ b/StarAI/StarAI/PathFindingCore/MapTransitionLogic/WarpGoal.cs @@ -16,6 +16,10 @@ namespace StarAI.PathFindingCore.MapTransitionLogic public Warp warp; public List childrenWarps; + + public static List checkedLocations = new List(); + public static List exploredLocations = new List(); + public WarpGoal(WarpGoal Parent, Warp CurrentWarp) { this.parentWarpGoal = Parent; @@ -26,6 +30,7 @@ namespace StarAI.PathFindingCore.MapTransitionLogic public static void getWarpChain(GameLocation location, string mapName) { + List blerp = new List(); GameLocation check = Game1.getLocationFromName(mapName); if (check == null) { @@ -43,11 +48,16 @@ namespace StarAI.PathFindingCore.MapTransitionLogic TransitionLogic.transitionToAdjacentMap(location, mapName); return; } + exploredLocations.Add(Warp); } //keep chaining children + //exploredLocations.Add(location); + checkedLocations.Add(location); + List warpChain = okBye(startinggoals, mapName, location,checkedLocations); - List warpChain = okBye(startinggoals, mapName); + checkedLocations.Clear(); + exploredLocations.Clear(); if (warpChain == null) { ModCore.CoreMonitor.Log("NULL WARP CHAIN"); @@ -177,6 +187,9 @@ namespace StarAI.PathFindingCore.MapTransitionLogic public static List> getWarpChainReturn(GameLocation location,string mapName) { GameLocation check = Game1.getLocationFromName(mapName); + + List blerp = new List(); + if (check.isStructure) mapName = check.uniqueName; if (check == null) { ModCore.CoreMonitor.Log("INVALID LOCATION"); @@ -188,6 +201,7 @@ namespace StarAI.PathFindingCore.MapTransitionLogic { WarpGoal child = new WarpGoal(null, Warp); startinggoals.Add(child); + exploredLocations.Add(Warp); /* if (Warp.TargetName == mapName) { @@ -200,8 +214,11 @@ namespace StarAI.PathFindingCore.MapTransitionLogic } //keep chaining children - - List warpChain= okBye(startinggoals, mapName); + // exploredLocations.Add(location); + checkedLocations.Add(location); + List warpChain= okBye(startinggoals, mapName,location,checkedLocations); + checkedLocations.Clear(); + exploredLocations.Clear(); if (warpChain == null) { ModCore.CoreMonitor.Log("NULL WARP CHAIN"); @@ -396,43 +413,135 @@ namespace StarAI.PathFindingCore.MapTransitionLogic //Do final location walk to stuff here. } - public static List okBye(List param,string targetMapName) +/* + public static List okByeGOODLOCATION(List param, string targetMapName, GameLocation lastCheckedLocation) { - bool found = false; - WarpGoal theOne= new WarpGoal(null,null); - List warpChain = new List(); - foreach (WarpGoal w in param) + + List placesToExplore = new List(); + List placesIHaveBeen = new List(); + + foreach(var warpGoal in param) { - GameLocation loc = Game1.getLocationFromName(w.warp.TargetName); - foreach (var v in loc.warps) + ModCore.CoreMonitor.Log(warpGoal.warp.TargetName); + placesToExplore.Add(Game1.getLocationFromName(warpGoal.warp.TargetName)); + } + placesIHaveBeen.Add(lastCheckedLocation); + + + while (placesToExplore.Count != 0) + { + GameLocation currentCheckingLocation = placesToExplore.ElementAt(0); + while(checkedLocations.Contains(currentCheckingLocation)) { - WarpGoal ok = new WarpGoal(w, v); - w.childrenWarps.Add(ok); - if (v.TargetName == targetMapName) + placesToExplore.Remove(currentCheckingLocation); + currentCheckingLocation = placesToExplore.ElementAt(0); + ModCore.CoreMonitor.Log("REMOVING " + currentCheckingLocation.name, StardewModdingAPI.LogLevel.Warn); + + } + foreach(var warp in currentCheckingLocation.warps) + { + bool addNewLocation = true; + foreach (var checkedPlace in placesIHaveBeen) { - found = true; - theOne = ok; - break; + + if (checkedPlace.name == warp.TargetName) + { + addNewLocation = false; + continue; + } + } + foreach(var location in placesToExplore) + { + if(location.name== warp.TargetName) + { + addNewLocation = false; + continue; + } + } + + if (addNewLocation == true) + { + placesToExplore.Add(Game1.getLocationFromName(warp.TargetName)); + ModCore.CoreMonitor.Log("ADDING NEW LOCATION" + warp.TargetName, StardewModdingAPI.LogLevel.Error); + } + else + { + ModCore.CoreMonitor.Log("ALREADY BEEN AT THIS LOCATION: " + currentCheckingLocation.name, StardewModdingAPI.LogLevel.Warn); } } - if (found == false) - { - return okBye(w.childrenWarps,targetMapName); - } - if (found == true) - { - while (theOne.parentWarpGoal != null) - { - warpChain.Add(theOne); - theOne = theOne.parentWarpGoal; - } - warpChain.Add(theOne); - } - return warpChain; - //recursively call this logic??? + placesIHaveBeen.Add(currentCheckingLocation); + placesToExplore.Remove(currentCheckingLocation); + ModCore.CoreMonitor.Log("CHECKING LOCATION: " + currentCheckingLocation.name,StardewModdingAPI.LogLevel.Alert); } return new List(); } +*/ + public static List okBye(List param, string targetMapName, GameLocation lastCheckedLocation,List place) + { + // List placesToExplore = new List(); + List placesIHaveBeen = place; + + List initialLocations = new List(); + + + + placesIHaveBeen.Add(lastCheckedLocation); + bool found = false; + if (param.Count == 0) + { + return new List(); + } + + foreach(var warpGoal in param) + { + + WarpGoal lastWarp = warpGoal; + + GameLocation targetLocation = Game1.getLocationFromName(warpGoal.warp.TargetName); + + if (targetLocation.name == targetMapName) + { + List hate = new List(); + while (lastWarp.parentWarpGoal!=null) + { + hate.Add(lastWarp); + lastWarp = lastWarp.parentWarpGoal; + } + hate.Add(lastWarp); + return hate; + } + + bool ignore = false; + + foreach (var v in placesIHaveBeen) + { + if (v.name == targetLocation.name) + { + ModCore.CoreMonitor.Log("I guve ps"+v.name); + ignore = true; + break; + } + } + if (ignore == true) continue; + ModCore.CoreMonitor.Log("I AM HERE:"+targetLocation.name); + foreach (Warp warp in targetLocation.warps) + { + WarpGoal fun = new WarpGoal(warpGoal, warp); + warpGoal.childrenWarps.Add(fun); + } + placesIHaveBeen.Add(targetLocation); + List idk = okBye(lastWarp.childrenWarps, targetMapName, targetLocation,placesIHaveBeen); + if (idk.Count == 0) continue; + if (idk.ElementAt(0).warp.TargetName == targetMapName) return idk; + // placesIHaveBeen.Clear(); + + } + + return new List(); + + } + + } } diff --git a/StarAI/StarAI/PathFindingCore/PlacementNode.cs b/StarAI/StarAI/PathFindingCore/TileNodes/PlacementNode.cs similarity index 100% rename from StarAI/StarAI/PathFindingCore/PlacementNode.cs rename to StarAI/StarAI/PathFindingCore/TileNodes/PlacementNode.cs diff --git a/StarAI/StarAI/PathFindingCore/TileExceptionMetaData.cs b/StarAI/StarAI/PathFindingCore/TileNodes/TileExceptionMetaData.cs similarity index 100% rename from StarAI/StarAI/PathFindingCore/TileExceptionMetaData.cs rename to StarAI/StarAI/PathFindingCore/TileNodes/TileExceptionMetaData.cs diff --git a/StarAI/StarAI/PathFindingCore/TileExceptionNode.cs b/StarAI/StarAI/PathFindingCore/TileNodes/TileExceptionNode.cs similarity index 100% rename from StarAI/StarAI/PathFindingCore/TileExceptionNode.cs rename to StarAI/StarAI/PathFindingCore/TileNodes/TileExceptionNode.cs diff --git a/StarAI/StarAI/PathFindingCore/TileNodeObject.cs b/StarAI/StarAI/PathFindingCore/TileNodes/TileNodeObject.cs similarity index 93% rename from StarAI/StarAI/PathFindingCore/TileNodeObject.cs rename to StarAI/StarAI/PathFindingCore/TileNodes/TileNodeObject.cs index 35bbc7aa..99ea8498 100644 --- a/StarAI/StarAI/PathFindingCore/TileNodeObject.cs +++ b/StarAI/StarAI/PathFindingCore/TileNodes/TileNodeObject.cs @@ -87,6 +87,7 @@ namespace StarAI.PathFindingCore if (t.thisLocation.isObjectAt((int)pos.X, (int)pos.Y)) { + // ModCore.CoreMonitor.Log("OBJECt??? " + t.thisLocation.name, LogLevel.Error); //ModCore.CoreMonitor.Log("Object at this tile position!: " + t.thisLocation.objects[new Vector2(pos.X/Game1.tileSize,pos.Y/Game1.tileSize)].name, LogLevel.Warn); if (cry == true) t.thisLocation = null; return false; @@ -95,9 +96,18 @@ namespace StarAI.PathFindingCore if (utilityCheck) { foreach (var v in Utilities.tileExceptionList) { - // ModCore.CoreMonitor.Log(v.actionType); - if (v.tile.position == pos) return false; - if (v.tile.tileLocation == pos / Game1.tileSize) return false; + // ModCore.CoreMonitor.Log(v.actionType); + // ModCore.CoreMonitor.Log("UTILITY????: " + t.thisLocation.name, LogLevel.Error); + if (v.tile.position == pos) + { + // ModCore.CoreMonitor.Log("DERP????: " + t.thisLocation.name, LogLevel.Error); + return false; + } + if (v.tile.tileLocation == pos / Game1.tileSize) + { + // ModCore.CoreMonitor.Log("BLURP " + t.thisLocation.name, LogLevel.Error); + return false; + } } } @@ -105,30 +115,34 @@ namespace StarAI.PathFindingCore if (terrainFeature) { TerrainFeature terrain = t.thisLocation.terrainFeatures[pos / Game1.tileSize]; - if (terrain.isPassable()) return true; + // ModCore.CoreMonitor.Log("TERRAIN NOPE!: " + t.thisLocation.name, LogLevel.Error); + if (terrain.isPassable()) return true; } if (t.thisLocation.isTileOccupied(pos / Game1.tileSize)) { - // ModCore.CoreMonitor.Log("Tile occupied!: " + t.thisLocation.name, LogLevel.Error); + // ModCore.CoreMonitor.Log("Tile occupied!: " + t.thisLocation.name, LogLevel.Error); + if (isTileExempt(t,pos)) return true; if (cry == true) t.thisLocation = null; return false; } + /* if (t.thisLocation.isTilePlaceable(pos / Game1.tileSize) == false) { // ModCore.CoreMonitor.Log("Tile Not placeable at location. " + t.thisLocation.name, LogLevel.Error); if (cry == true) t.thisLocation = null; return false; } + */ - - + if (t.thisLocation.isTilePassable(new xTile.Dimensions.Location((int)(pos.X/Game1.tileSize), (int)(pos.Y/Game1.tileSize)), Game1.viewport)==false) { - // ModCore.CoreMonitor.Log("Tile not passable check 2?????!!!!: " + t.thisLocation.name, LogLevel.Error); + //ModCore.CoreMonitor.Log("Tile not passable check 2?????!!!!: " + t.thisLocation.name, LogLevel.Error); + if (isTileExempt(t,pos)) return true; if (cry == true) t.thisLocation = null; - return false; + else return false; } @@ -136,6 +150,44 @@ namespace StarAI.PathFindingCore if (cry == true) t.thisLocation = null; return true; } + + + public static bool isTileExempt(TileNode t,Vector2 pos) + { + foreach (var v in Game1.player.currentLocation.map.TileSheets) + { + foreach (var q in Game1.player.currentLocation.map.Layers) + { + string[] s = q.ToString().Split(':'); + string layer = s[1].Trim(); + int tileIndex = t.thisLocation.getTileIndexAt((int)pos.X/Game1.tileSize, (int)pos.Y / Game1.tileSize, layer); + if (tileIndex == -1) continue; + //ModCore.CoreMonitor.Log("Position: " + (Game1.player.getTileLocation() / Game1.tileSize).ToString(), LogLevel.Warn); + //ModCore.CoreMonitor.Log("Layer: " + layer, LogLevel.Warn); + //ModCore.CoreMonitor.Log("Index: " + tileIndex.ToString(), LogLevel.Warn); + //ModCore.CoreMonitor.Log("Image Source: " + v.ImageSource, LogLevel.Warn); + + if (layer == "Buildings") + { + TileExceptionNode tileException = new TileExceptionNode(v.ImageSource, tileIndex); + foreach (var tile in PathFindingCore.Utilities.ignoreCheckTiles) + { + if (tile.imageSource == tileException.imageSource && tile.index == tileException.index) + { + ModCore.CoreMonitor.Log("Tile exception already initialized!"); + return true; //tile is already initialized. + } + } + // PathFindingCore.Utilities.ignoreCheckTiles.Add(tileException); + //tileException.serializeJson(Path.Combine(ModCore.CoreHelper.DirectoryPath, PathFindingCore.Utilities.folderForExceptionTiles)); + //StardustCore.ModCore.SerializationManager. + } + } + } + return false; + } + + public static void setSingleTileAsChild(TileNode t,int x, int y,bool checkForUtility,bool placementAction=true) { diff --git a/StarAI/StarAI/PathFindingCore/Utilities.cs b/StarAI/StarAI/PathFindingCore/Utilities.cs index db5ac98a..69fb3c2a 100644 --- a/StarAI/StarAI/PathFindingCore/Utilities.cs +++ b/StarAI/StarAI/PathFindingCore/Utilities.cs @@ -34,7 +34,25 @@ namespace StarAI.PathFindingCore public static void initializeTileExceptionList() { - + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\spring_outdoorsTileSheet", 779)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\spring_outdoorsTileSheet", 780)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\spring_outdoorsTileSheet", 781)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\spring_outdoorsTileSheet", 782)); + + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\summer_outdoorsTileSheet", 779)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\summer_outdoorsTileSheet", 780)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\summer_outdoorsTileSheet", 781)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\summer_outdoorsTileSheet", 782)); + + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\fall_outdoorsTileSheet", 779)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\fall_outdoorsTileSheet", 780)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\fall_outdoorsTileSheet", 781)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\fall_outdoorsTileSheet", 782)); + + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\winter_outdoorsTileSheet", 779)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\winter_outdoorsTileSheet", 780)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\winter_outdoorsTileSheet", 781)); + ignoreCheckTiles.Add(new TileExceptionNode("Maps\\winter_outdoorsTileSheet", 782)); } diff --git a/StarAI/StarAI/StarAI.csproj b/StarAI/StarAI/StarAI.csproj index 60ef272d..f34debde 100644 --- a/StarAI/StarAI/StarAI.csproj +++ b/StarAI/StarAI/StarAI.csproj @@ -56,6 +56,7 @@ + @@ -76,10 +77,10 @@ - - - - + + + +