From de15a965f54d3ab4e68dcf50f381c086f9cc7f47 Mon Sep 17 00:00:00 2001 From: Date: Fri, 24 Nov 2017 19:56:10 -0800 Subject: [PATCH] Added functionality to path to chest and pull seasonal seeds from it. --- StarAI/StarAI/StarAI/Commands.cs | 7 +- StarAI/StarAI/StarAI/ModCore.cs | 2 +- .../StarAI/PathFindingCore/ChestLogic.cs | 271 ++++++++++++++++++ .../PathFindingCore/PathFindingLogic.cs | 4 + StarAI/StarAI/StarAI/StarAI.csproj | 1 + 5 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 StarAI/StarAI/StarAI/PathFindingCore/ChestLogic.cs diff --git a/StarAI/StarAI/StarAI/Commands.cs b/StarAI/StarAI/StarAI/Commands.cs index 2aa180ee..8ea39393 100644 --- a/StarAI/StarAI/StarAI/Commands.cs +++ b/StarAI/StarAI/StarAI/Commands.cs @@ -26,13 +26,18 @@ namespace StarAI ModCore.CoreHelper.ConsoleCommands.Add("runTasks", "Run tasks", new Action(Commands.runTasks)); ModCore.CoreHelper.ConsoleCommands.Add("Water", "Water the crops", new Action(Commands.waterCrops)); ModCore.CoreHelper.ConsoleCommands.Add("Harvest", "Harvest the crops", new Action(Commands.harvestCrops)); - + ModCore.CoreHelper.ConsoleCommands.Add("getseeds", "Get Seeds From chests.", new Action(Commands.getSeedsFromChests)); pathfind("Initialize Delay 0", new string[] { "setDelay", "0" }); } + public static void getSeedsFromChests(string s, string[] args) + { + ChestLogic.getAllChestsFromLocation(Game1.player.currentLocation); + } + public static void runTasks(string s, string[] args) { ExecutionCore.TaskList.runTaskList(); diff --git a/StarAI/StarAI/StarAI/ModCore.cs b/StarAI/StarAI/StarAI/ModCore.cs index 68c2f1e6..84acf848 100644 --- a/StarAI/StarAI/StarAI/ModCore.cs +++ b/StarAI/StarAI/StarAI/ModCore.cs @@ -142,7 +142,7 @@ namespace StarAI if (e.KeyPressed == Microsoft.Xna.Framework.Input.Keys.U) { - ModCore.CoreMonitor.Log(Game1.player.facingDirection.ToString()); + } if (e.KeyPressed == Microsoft.Xna.Framework.Input.Keys.O) diff --git a/StarAI/StarAI/StarAI/PathFindingCore/ChestLogic.cs b/StarAI/StarAI/StarAI/PathFindingCore/ChestLogic.cs new file mode 100644 index 00000000..80189a18 --- /dev/null +++ b/StarAI/StarAI/StarAI/PathFindingCore/ChestLogic.cs @@ -0,0 +1,271 @@ +using Microsoft.Xna.Framework; +using StardewModdingAPI; +using StardewValley; +using StardewValley.Objects; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StarAI.PathFindingCore +{ + class ChestLogic + { + public static List chestsAtThisLocation = new List(); + + public static void getAllChestsFromLocation(GameLocation location) + { + object[] arr = new object[1]; + arr[0] = location; + getAllChestsFromLocation(arr); + } + + public static void getAllChestsFromLocation(object obj) + { + object[] objArr = (object[])obj; + GameLocation location = (GameLocation)objArr[0]; + foreach (var v in location.objects) + { + ModCore.CoreMonitor.Log(v.Value.name); + if (v.Value is StardewValley.Objects.Chest) + { + //if contains seeds that can be planted this season. + foreach(var item in (v.Value as StardewValley.Objects.Chest).items) + { + if (item.getCategoryName() == "Seed") + { + + StardewValley.Crop c = new Crop(item.parentSheetIndex, 0, 0); + if (c.seasonsToGrowIn.Contains(Game1.currentSeason)) + { + TileNode t = new TileNode(1, Vector2.Zero, Path.Combine("Tiles", "GenericUncoloredTile.xnb"), Path.Combine("Tiles", "TileData.xnb"), StardustCore.IlluminateFramework.Colors.invertColor(StardustCore.IlluminateFramework.ColorsList.Brown)); + //t.placementAction(Game1.currentLocation, (int)v.Key.X * Game1.tileSize, (int)v.Key.Y * Game1.tileSize); + t.tileLocation=new Vector2((int)v.Key.X, (int)v.Key.Y); + t.position = new Vector2(v.Key.X*Game1.tileSize, v.Key.Y*Game1.tileSize); + t.thisLocation = Game1.currentLocation; + //StardustCore.Utilities.masterAdditionList.Add(new StardustCore.DataNodes.PlacementNode(t, Game1.currentLocation, (int)v.Key.X * Game1.tileSize, (int)v.Key.Y * Game1.tileSize)); + Utilities.tileExceptionList.Add(new TileExceptionMetaData(t, "Chest")); + chestsAtThisLocation.Add(t); + } + } + } + + //TileNode t = new TileNode(1, Vector2.Zero, Path.Combine("Tiles", "GenericUncoloredTile.xnb"), Path.Combine("Tiles", "TileData.xnb"), StardustCore.IlluminateFramework.Colors.invertColor(StardustCore.IlluminateFramework.ColorsList.Brown)); + // t.placementAction(Game1.currentLocation, (int)v.Key.X * Game1.tileSize, (int)v.Key.Y * Game1.tileSize); + //StardustCore.Utilities.masterAdditionList.Add(new StardustCore.DataNodes.PlacementNode(t, Game1.currentLocation, (int)v.Key.X * Game1.tileSize, (int)v.Key.Y * Game1.tileSize)); + //Utilities.tileExceptionList.Add(new TileExceptionMetaData(t, "Chest")); + //chestsAtThisLocation.Add(t); + + } + + } + + foreach (var v in chestsAtThisLocation) + { + object[] objList = new object[1]; + objList[0] = v; + // ExecutionCore.TaskList.taskList.Add(new Task(new Action(waterSingleCrop), obj)); + ExecutionCore.TaskList.taskList.Add(new ExecutionCore.CustomTask(pathToSingleChest, objList)); + // waterSingleCrop(v); + } + } + + public static void pathToSingleChest(object obj) + { + object[] objArr = (object[])obj; + TileNode v = (TileNode)objArr[0]; + bool moveOn = false; + foreach (var q in Utilities.tileExceptionList) + { + if (q.tile == v && q.actionType == "Chest") + { + moveOn = true; + } + } + if (moveOn == false) return; + + WindowsInput.InputSimulator.SimulateKeyUp(WindowsInput.VirtualKeyCode.VK_C); + int xMin = -1; + int yMin = -1; + int xMax = 1; + int yMax = 1; + List miniGoals = new List(); + List> paths = new List>(); + //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); + // 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)); + t.placementAction(Game1.currentLocation, (int)pos.X * Game1.tileSize, (int)pos.Y * Game1.tileSize); + //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, "CropNavigation")); + } + } + } + List removalList = new List(); + 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)); + tempSource.placementAction(Game1.player.currentLocation, Game1.player.getTileX() * Game1.tileSize, Game1.player.getTileY() * Game1.tileSize); + //StaardustCore.Utilities.masterAdditionList.Add(new StardustCore.DataNodes.PlacementNode(tempSource, Game1.currentLocation, Game1.player.getTileX() * Game1.tileSize, Game1.player.getTileY() * Game1.tileSize)); + List path = PathFindingCore.PathFindingLogic.pathFindToSingleGoalReturnPath(tempSource, nav, new List()); + + 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); + 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); + q.thisLocation.objects.Remove(q.tileLocation); + } + removalList.Clear(); + int pathCost = 999999999; + List correctPath = new List(); + 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); + //StardustCore.Utilities.masterAdditionList.Add(new StardustCore.DataNodes.PlacementNode(goodTile, Game1.currentLocation, (int)goodTile.tileLocation.X * Game1.tileSize, (int)goodTile.tileLocation.Y * Game1.tileSize)); + goodTile.placementAction(goodTile.thisLocation, (int)goodTile.tileLocation.X * Game1.tileSize, (int)goodTile.tileLocation.Y * Game1.tileSize); + + } + PathFindingLogic.calculateMovement(correctPath); + if (v.tileLocation.X < Game1.player.getTileX()) + { + Game1.player.faceDirection(3); + } + else if (v.tileLocation.X > Game1.player.getTileX()) + { + Game1.player.faceDirection(1); + } + else if (v.tileLocation.Y < Game1.player.getTileY()) + { + Game1.player.faceDirection(0); + } + else if (v.tileLocation.Y > Game1.player.getTileY()) + { + Game1.player.faceDirection(2); + } + + bool move = false; + Chest chest =(Chest) v.thisLocation.objects[v.tileLocation]; + List removalListSeeds = new List(); + //Try to grab all the seeds I can from the chest. + while (Game1.player.isInventoryFull()==false&&chest.items.Count>0) + { + + if (chest.giftbox) + { + ModCore.CoreMonitor.Log("GIFT BOX", LogLevel.Warn); + v.thisLocation.objects.Remove(v.tileLocation); + } + foreach (var item in chest.items) + { + if (item.getCategoryName() == "Seed") + { + + StardewValley.Crop c = new Crop(item.parentSheetIndex, 0, 0); + if (c.seasonsToGrowIn.Contains(Game1.currentSeason)) + { + Game1.player.addItemToInventory(item); + removalListSeeds.Add(item); + break; + } + } + } + + + foreach(var remove in removalListSeeds) + { + chest.items.Remove(remove); + } + // if (WindowsInput.InputSimulator.IsKeyDown(WindowsInput.VirtualKeyCode.VK_C) == false) WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.VK_C); + + + + Vector2 center = new Vector2(); + if (Game1.player.facingDirection == 2) + { + center = Utilities.parseCenterFromTile((int)v.tileLocation.X + 1, (int)v.tileLocation.Y); + continue; + } + if (Game1.player.facingDirection == 1) + { + center = Utilities.parseCenterFromTile((int)v.tileLocation.X - 1, (int)v.tileLocation.Y); + continue; + } + if (Game1.player.facingDirection == 0) + { + center = Utilities.parseCenterFromTile((int)v.tileLocation.X, (int)v.tileLocation.Y + 1); + continue; + + } + if (Game1.player.facingDirection == 3) + { + center = Utilities.parseCenterFromTile((int)v.tileLocation.X, (int)v.tileLocation.Y - 1); + continue; + } + Game1.player.position = center; + + } + Utilities.cleanExceptionList(v); + StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(v); + // StardustCore.Utilities.masterRemovalList.Add(v); + //v.performRemoveAction(v.tileLocation, v.thisLocation); + // v.thisLocation.objects.Remove(v.tileLocation); + foreach (var goodTile in correctPath) + { + StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(goodTile); + //StardustCore.Utilities.masterRemovalList.Add(v); + goodTile.performRemoveAction(goodTile.tileLocation, goodTile.thisLocation); + //goodTile.placementAction(goodTile.thisLocation, (int)goodTile.tileLocation.X * Game1.tileSize, (int)goodTile.tileLocation.Y * Game1.tileSize); + } + //WindowsInput.InputSimulator.SimulateKeyUp(WindowsInput.VirtualKeyCode.VK_C); + } + + + } +} diff --git a/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs b/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs index 248329fd..b716b267 100644 --- a/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs +++ b/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs @@ -435,6 +435,10 @@ namespace StarAI.PathFindingCore { currentNode.parent.animationManager.setAnimation("Up", 0); } + if (currentNode.parent.tileLocation == currentNode.tileLocation) + { + currentNode.drawColor = StardustCore.IlluminateFramework.Colors.invertColor(StardustCore.IlluminateFramework.ColorsList.Black); //Working + } currentNode.parent.animationManager.enableAnimation(); currentNode = currentNode.parent; System.Threading.Thread.Sleep(delay); diff --git a/StarAI/StarAI/StarAI/StarAI.csproj b/StarAI/StarAI/StarAI/StarAI.csproj index ba330d0e..a2908035 100644 --- a/StarAI/StarAI/StarAI/StarAI.csproj +++ b/StarAI/StarAI/StarAI/StarAI.csproj @@ -61,6 +61,7 @@ +