diff --git a/StarAI/StarAI/StarAI/Commands.cs b/StarAI/StarAI/StarAI/Commands.cs index a32ee8ca..b79e3b45 100644 --- a/StarAI/StarAI/StarAI/Commands.cs +++ b/StarAI/StarAI/StarAI/Commands.cs @@ -33,7 +33,47 @@ namespace StarAI public static void runTasks(string s, string[] args) { - ExecutionCore.TaskList.runTaskList(); + ModCore.CoreMonitor.Log("EXECUTE TASKS"); + if (ExecutionCore.TaskList.executioner.Status == TaskStatus.Running) + { + ModCore.CoreMonitor.Log("Tasklist is already executing. Just going to return."); + return; + } + if (ExecutionCore.TaskList.executioner.Status == TaskStatus.RanToCompletion) + { + ModCore.CoreMonitor.Log("Tasklist is finished. Going to restart"); + + List removalList = new List(); + foreach(var v in ExecutionCore.TaskList.taskList) + { + if (v.IsCompleted) removalList.Add(v); + } + foreach(var v in removalList) + { + ExecutionCore.TaskList.taskList.Remove(v); + } + + ExecutionCore.TaskList.executioner = new Task(new Action(ExecutionCore.TaskList.runTaskList)); + ExecutionCore.TaskList.executioner.Start(); + // ExecutionCore.TaskList.taskList.Clear(); + return; + //ExecutionCore.TaskList.runTaskList(); + + } + + if (ExecutionCore.TaskList.executioner.Status == TaskStatus.Faulted) + { + ModCore.CoreMonitor.Log(ExecutionCore.TaskList.executioner.Exception.ToString()); + } + + if (ExecutionCore.TaskList.executioner.Status == TaskStatus.Created) + { + //ExecutionCore.TaskList.runTaskList(); + List removalList = new List(); + ExecutionCore.TaskList.executioner.Start(); + + return; + } } /// diff --git a/StarAI/StarAI/StarAI/ExecutionCore/TaskList.cs b/StarAI/StarAI/StarAI/ExecutionCore/TaskList.cs index a8fbb83d..fab73f59 100644 --- a/StarAI/StarAI/StarAI/ExecutionCore/TaskList.cs +++ b/StarAI/StarAI/StarAI/ExecutionCore/TaskList.cs @@ -10,30 +10,34 @@ namespace StarAI.ExecutionCore class TaskList { public static List taskList = new List(); - + public static Task executioner = new Task(new Action(runTaskList)); public static void runTaskList() { List removalList = new List(); bool assignNewTask = true; - for (int i= 0; i <= taskList.Count;i++) + for (int i= 0; i < taskList.Count;i++) { + ModCore.CoreMonitor.Log("I value: " + i); + ModCore.CoreMonitor.Log("Count: " + taskList.Count); Task v = taskList[i]; // v.Start(); if (assignNewTask) { - ModCore.CoreMonitor.Log("CREATE AND RUN A TASK!!! PATHFINDING!"); + //ModCore.CoreMonitor.Log("CREATE AND RUN A TASK!!! PATHFINDING!"); ModCore.fun = v; ModCore.fun.Start(); assignNewTask = false; + i--; + continue; } if (ModCore.fun.Status == TaskStatus.Running) { assignNewTask = false; - ModCore.CoreMonitor.Log("TASK IS RUNNING CAN'T PATHFIND AT THE MOMENT", LogLevel.Alert); + //ModCore.CoreMonitor.Log("TASK IS RUNNING CAN'T PATHFIND AT THE MOMENT", LogLevel.Alert); i--; continue; //return; @@ -42,6 +46,7 @@ namespace StarAI.ExecutionCore { assignNewTask = true; + removalList.Add(v); continue; // return; } @@ -49,6 +54,7 @@ namespace StarAI.ExecutionCore if (ModCore.fun.Status == TaskStatus.Faulted) { assignNewTask = true; + removalList.Add(v); continue; //ModCore.CoreMonitor.Log(ModCore.fun.Exception.ToString()); //ModCore.CoreMonitor.Log("CREATE AND RUN A TASK!!! PATHFINDING!"); @@ -56,14 +62,10 @@ namespace StarAI.ExecutionCore //ModCore.fun.Start(); } - removalList.Add(v); + } - foreach(var v in removalList) - { - taskList.Remove(v); - } - removalList.Clear(); + } } } diff --git a/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs b/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs index 7adcee05..56ad6bca 100644 --- a/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs +++ b/StarAI/StarAI/StarAI/PathFindingCore/PathFindingLogic.cs @@ -44,7 +44,7 @@ namespace StarAI.PathFindingCore foreach (var tile in removalList) { StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(tile); - (tile as TileNode).thisLocation.objects.Remove(tile.tileLocation); + v.thisLocation.removeObject(v.tileLocation, false); } cleanseGoals.Add(v); } @@ -71,10 +71,6 @@ namespace StarAI.PathFindingCore ModCore.CoreMonitor.Log("LET'S GO!!!!", LogLevel.Error); object[] obj = (object[])data; - foreach(var v in obj) - { - ModCore.CoreMonitor.Log(v.ToString(), LogLevel.Warn); - } TileNode Source =(TileNode) obj[0]; TileNode Goal = (TileNode)obj[1]; @@ -240,7 +236,7 @@ namespace StarAI.PathFindingCore foreach(var v in removalList) { StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(v); - (v as TileNode).thisLocation.objects.Remove(v.tileLocation); + v.thisLocation.removeObject(v.tileLocation, false); } calculateMovement(path); @@ -253,94 +249,104 @@ namespace StarAI.PathFindingCore path.Reverse(); bool xTargetReached = false; bool yTargetReached = false; + List removalList = new List(); 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("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); + removalList.Add(w); xTargetReached = false; yTargetReached = false; - StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(w); - ModCore.CoreMonitor.Log("LOOOP", LogLevel.Debug); + + //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"); + //ModCore.CoreMonitor.Log("XXXXXXXtargetReached"); xTargetReached = true; InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_A); //break; continue; } - InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_A); + if (InputSimulator.IsKeyDown(VirtualKeyCode.VK_A) == false) 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); + //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"); + //ModCore.CoreMonitor.Log("XXXXXXXtargetReached"); InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_D); continue; - } - InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_D); + } + if (InputSimulator.IsKeyDown(VirtualKeyCode.VK_D) == false) InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_D); } + + + + while (Game1.player.position.Y < center.Y && yTargetReached == false) { - ModCore.CoreMonitor.Log("banana"); + //ModCore.CoreMonitor.Log("banana"); if (Utilities.isWithinRange(Game1.player.position.Y, center.Y, 6) == true) { yTargetReached = true; - ModCore.CoreMonitor.Log("YYYYYYYYYtargetReached"); + //ModCore.CoreMonitor.Log("YYYYYYYYYtargetReached"); + InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_S); continue; } - InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_S); + if(InputSimulator.IsKeyDown(VirtualKeyCode.VK_S)==false) InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_S); + } + + + while (Game1.player.position.Y > center.Y && yTargetReached == false) { - ModCore.CoreMonitor.Log("potato"); + //ModCore.CoreMonitor.Log("potato"); if (Utilities.isWithinRange(Game1.player.position.Y, center.Y, 6) == true) { yTargetReached = true; - ModCore.CoreMonitor.Log("YYYYYYYYYtargetReached"); + // ModCore.CoreMonitor.Log("YYYYYYYYYtargetReached"); InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_W); continue; } - InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_W); + if (InputSimulator.IsKeyDown(VirtualKeyCode.VK_W) == false) InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_W); } if (xTargetReached == true && yTargetReached == true) { path.Remove(w); - - - + removalList.Add(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); + //ModCore.CoreMonitor.Log("Reached goal!", LogLevel.Error); //Game1.player.position = new Vector2(center.X, center.Y); continue; } @@ -349,6 +355,12 @@ namespace StarAI.PathFindingCore ModCore.CoreMonitor.Log("UNCAUGHT EXCEPTION", LogLevel.Error); } } + foreach(var v in removalList) + { + //v.thisLocation.objects.Remove(v.tileLocation); + v.thisLocation.removeObject(v.tileLocation, false); + StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(v); + } } diff --git a/StarAI/StarAI/StarAI/PathFindingCore/TileNodeObject.cs b/StarAI/StarAI/StarAI/PathFindingCore/TileNodeObject.cs index 9af89c5a..cc109b7b 100644 --- a/StarAI/StarAI/StarAI/PathFindingCore/TileNodeObject.cs +++ b/StarAI/StarAI/StarAI/PathFindingCore/TileNodeObject.cs @@ -79,20 +79,20 @@ namespace StarAI.PathFindingCore if (t.thisLocation.isObjectAt((int)pos.X, (int)pos.Y)) { - ModCore.CoreMonitor.Log("Object at this tile position!: " + t.thisLocation.name, LogLevel.Warn); + //ModCore.CoreMonitor.Log("Object at this tile position!: " + t.thisLocation.name, LogLevel.Warn); if (cry == true) this.thisLocation = null; return false; } 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 (cry == true) this.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); + //ModCore.CoreMonitor.Log("Tile Not placeable at location. " + t.thisLocation.name, LogLevel.Error); if (cry == true) this.thisLocation = null; return false; } @@ -101,7 +101,7 @@ namespace StarAI.PathFindingCore 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 (cry == true) this.thisLocation = null; return false; }