things are broken

This commit is contained in:
Simon O'Shea
2023-08-08 16:29:52 -04:00
parent 239f7ed4c5
commit 2dcb6ea96d
7 changed files with 121 additions and 95 deletions
@@ -1,25 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AStar : MonoBehaviour
{
LogicGrid world;
List<int[][]> obstacles;
List<int[][]> samples;
int heuristic; // 0 = h0 |
public AStar(LogicGrid world, int heuristic)
{
this.world = world;
this.obstacles = world.obstacles;
this.samples = world.samples;
this.heuristic = heuristic;
}
public void go()
{
}
}
@@ -30,7 +30,7 @@ public class CellMesh : MonoBehaviour
{
// if(e.x == -1 && e.y == -1), grid is being reset
Debug.Log(e.x + " " + e.y);
//Debug.Log(e.x + " " + e.y);
UpdateCellVisual();
}
@@ -18,12 +18,6 @@ public class LogicGrid
// Debug Grid Array used for updating the text-objects
private TextMesh[,] debugTextArray;
// Coordinates for pathfinding
public int[] agent;
public List<int[][]> obstacles;
public List<int[][]> samples;
public event EventHandler<OnGridValueChangedEventArgs> OnGridValueChanged;
public class OnGridValueChangedEventArgs : EventArgs
{
@@ -103,46 +97,6 @@ public class LogicGrid
x = x,
y = y
});
// If deleting a cell's value, remove coord from set
if (value == 0)
{
// make behavior to check for -1 values in coords
if (agent[0].Equals(x) && agent[1].Equals(y))
{
agent[0] = -1;
agent[1] = -2;
}
}
// Add coordinate to obstacle array
if (value == 1)
{
int[][] coord = new int[2][];
coord[0][0] = x;
coord[1][0] = y;
obstacles.Add(coord);
}
// Add coordinate to sample array
if (value == 2)
{
int[][] coord = new int[2][];
coord[0][0] = x;
coord[1][0] = y;
samples.Add(coord);
}
// Add coordinate to obstacle array
if (value == 3)
{
agent[0] = x;
agent[1] = y;
}
}
}
@@ -183,10 +137,6 @@ public class LogicGrid
{
gridArray = new int[width, height];
agent = new int[2];
obstacles = new List<int[][]>();
samples = new List<int[][]>();
if (OnGridValueChanged != null)
OnGridValueChanged(this, new OnGridValueChangedEventArgs
{
+57 -12
View File
@@ -8,27 +8,29 @@ public class Main : MonoBehaviour
[SerializeField] private CellMesh cellMesh;
private LogicGrid world;
char placementMode;
int placementValue;
bool astarSelected;
// Coordinates for pathfinding
public Vector2 agent = new Vector2();
public List<Vector2> obstacles = new List<Vector2>();
public List<Vector2> samples = new List<Vector2>();
// Start is called before the first frame update
void Start()
{
// Create world
int width = 100;
int height = 100;
float cellSize = 5f;
Vector3 origin = new Vector3(-110, -110);
int width = 5;
int height = 5;
float cellSize = 10f;
Vector3 origin = new Vector3(-10, -10);
world = new LogicGrid(width, height, cellSize, origin);
// Set default placement to Obstacle
placementValue = 1;
placementMode = 'o';
cellMesh.SetGrid(world);
@@ -45,21 +47,18 @@ public class Main : MonoBehaviour
if (Input.GetKeyDown("o"))
{
Debug.Log("Placing Mode: Obstacle");
placementMode = 'o';
placementValue = 1;
}
// Place Sample in cell
if(Input.GetKeyDown("s"))
{
Debug.Log("Placing Mode: Sample");
placementMode = 's';
placementValue = 2;
}
// Place agent in cell
if (Input.GetKeyDown("a"))
{
Debug.Log("Placing Mode: Agent");
placementMode = 'a';
placementValue = 3;
}
// Reset Canvas
@@ -67,6 +66,11 @@ public class Main : MonoBehaviour
{
ResetGrid();
}
if (Input.GetKeyDown("g"))
{
StartAlgorithm();
}
//////////////////////////
@@ -107,9 +111,50 @@ public class Main : MonoBehaviour
public void StartAlgorithm()
{
if(astarSelected)
Vector2 coord = new Vector2();
// Collect information about world
for (int x = 0; x < world.GetWidth(); x++)
for(int y = 0; y < world.GetHeight(); y++)
{
int index = x * world.GetHeight() + y;
int value = world.GetValue(x, y);
Debug.Log("x: " + x + " y: " + y + " | value: " + value);
// Add coordinate to obstacle array
if (value == 1)
{
//coord = new int[2][];
coord.x = x;
coord.y = y;
if (!obstacles.Contains(coord))
obstacles.Add(coord);
}
// Add coordinate to sample array
if (value == 2)
{
//coord = new int[2][];
coord.x = x;
coord.y = y;
samples.Add(coord);
}
// Add coordinate to obstacle array
if (value == 3)
{
agent[0] = x;
agent[1] = y;
}
}
if ( true )
{
AStar astar = new AStar(world, 0);
Pathfinder astar = new Pathfinder(world, agent, obstacles, samples, 0, 0);
astar.go();
}
}
@@ -43,7 +43,7 @@ using UnityEngine;
int[] onSample = new int[2];
// since this state is being currently visited (expanded), put in closed list
// SampleWorld.closed.add(this.getState());
Pathfinder.closed.Add(this.getState());
////////////////////////////////////
// BEGIN CHECKING FOR VALID MOVES //
@@ -115,14 +115,17 @@ using UnityEngine;
public bool isOpen(int[] position)
{
// check that agent is not trying to move into an obstacle
for (int i = 0; i < SampleWorld.obstacles.size(); i++)
for (int i = 0; i < Pathfinder.obstacles.Count(); i++)
{
if (Enumerable.SequenceEqual(SampleWorld.obstacles.get(i), position) == 0)
int[] coord = new int[2];
coord[0] = Pathfinder.obstacles[i][0][0];
coord[1] = Pathfinder.obstacles[i][1][0];
if (Pathfinder.obstacles[i][0][0] == position[0] && Pathfinder.obstacles[i][1][0] == position[1])
return false;
}
// check that agent is not stepping out of the world
if (!((position[row] >= 0) && (position[row] <= SampleWorld.worldRows - 1) && (position[col] >= 0) && (position[col] <= SampleWorld.worldCols - 1)))
if (!((position[row] >= 0) && (position[row] <= Pathfinder.height - 1) && (position[col] >= 0) && (position[col] <= Pathfinder.width - 1)))
return false;
return true;
@@ -216,7 +219,7 @@ using UnityEngine;
}
}
// helper class for Node
class State
public class State
{
int[] agent;
int[][] samples;
@@ -237,9 +240,9 @@ using UnityEngine;
public bool inClosed()
{
for (int i = 0; i < SampleWorld.closed.size(); i++)
for (int i = 0; i < Pathfinder.closed.Count(); i++)
{
if (this.equals(SampleWorld.closed.get(i)))
if (this.equals(Pathfinder.closed[i]))
{
return true;
}
@@ -0,0 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pathfinder : MonoBehaviour
{
// used to store all unique states that agent has visited (holds agent and sample coords)
public static List<State> closed = new List<State>();
LogicGrid world;
public static int[] agent;
public static List<int[][]> obstacles;
public static List<int[][]> samples;
public static int algorithm; // 0 = A* | 1 = dfs | 2 = bfs
public static int heuristic; // 0 = h0 | 1 = h1 | 2 = h2
public static int height;
public static int width;
public Pathfinder(LogicGrid world, int[] a, List<int[][]> o, List<int[][]> s, int algo, int heu)
{
this.world = world;
height = world.GetHeight();
width = world.GetWidth();
agent = a;
obstacles = o;
samples = s;
algorithm = algo;
heuristic = heu;
}
public void go()
{
Debug.Log("Agent: " + agent[0] + "," + agent[1]);
for(int i = 0; i < obstacles.Count; i++)
{
Debug.Log("Obstacle " + i + ": " + obstacles[i][0][0] + ", " + obstacles[i][0][1]);
}
for (int i = 0; i < samples.Count; i++)
{
Debug.Log("Sample " + i + ": " + samples[i][0][0] + ", " + samples[i][0][1]);
}
}
}