forked from s425077/PotatoPlan
MinHeap added, A* working
This commit is contained in:
parent
54205f0d7f
commit
1015886a71
@ -34,7 +34,7 @@ namespace Game1
|
||||
|
||||
|
||||
//Generates the map with some random values
|
||||
input.init(graphics, new Vector2(8,8), 56, 1); //Generates the starting size
|
||||
input.init(graphics, new Vector2(16,16), 56, 1); //Generates the starting size
|
||||
houseUnit.init(input.getTileSize(), input.getSpacing()); //Generates the house position
|
||||
tractorUnit.init(houseUnit.GetRectangle(), input); //Generates the Tractor
|
||||
tractorUnit.updateSizing(input, 0, houseUnit.getVector()); //Updates the first Size of the Tractor
|
||||
|
@ -50,6 +50,7 @@
|
||||
<Compile Include="Sources\Controlls\Input.cs" />
|
||||
<Compile Include="Sources\Objects\tractorPositionCorrector.cs" />
|
||||
<Compile Include="Sources\Pathing\A-Star\Astar.cs" />
|
||||
<Compile Include="Sources\Pathing\A-Star\PathSaver\MinHeap.cs" />
|
||||
<Compile Include="Sources\Pathing\A-Star\PathSaver\Nodes.cs" />
|
||||
<Compile Include="Sources\Pathing\A-Star\PathSaver\Path.cs" />
|
||||
<Compile Include="Sources\Pathing\A-Star\PathSaver\PriorityQueue.cs" />
|
||||
|
@ -37,15 +37,15 @@ class Crops
|
||||
}
|
||||
else if (Status == 2)
|
||||
{
|
||||
return 20;
|
||||
return 5;
|
||||
}
|
||||
else if (Status == 3)
|
||||
{
|
||||
return 30;
|
||||
return 20;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 40;
|
||||
return 30;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,11 +104,10 @@ class Tractor
|
||||
else
|
||||
{
|
||||
//Sets a random Target
|
||||
path = smartTractor.returnChoice(1);
|
||||
int xTarget = (int)TargetPosition.X / (sizeTile + Spacing);
|
||||
int yTarget = (int)TargetPosition.Y / (sizeTile + Spacing);
|
||||
currentTask = scoreSystem.MessageAndScore(farm.getCrop(xTarget, yTarget).Status, 1);
|
||||
|
||||
path = smartTractor.returnChoice(1);
|
||||
}
|
||||
|
||||
TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
|
||||
|
@ -65,31 +65,41 @@ class Astar
|
||||
public Path FindPath()
|
||||
{
|
||||
Path path = new Path();
|
||||
PriorityQueue openList = new PriorityQueue();
|
||||
PriorityQueue closedList = new PriorityQueue();
|
||||
//PriorityQueue openList = new PriorityQueue();
|
||||
MinHeap openList = new MinHeap();
|
||||
//PriorityQueue closedList = new PriorityQueue();
|
||||
MinHeap closedList = new MinHeap();
|
||||
Nodes target = new Nodes(targetPos);
|
||||
Nodes startPos = new Nodes (tractorPos);
|
||||
Nodes current = null;
|
||||
int g = 0;
|
||||
|
||||
openList.Enqueue(startPos);
|
||||
//openList.Enqueue(startPos);
|
||||
openList.Insert(startPos);
|
||||
|
||||
while (openList.Count > 0)
|
||||
//while (openList.Count > 0)
|
||||
while (openList.GetSize() > 0)
|
||||
{
|
||||
current = openList.Peek();
|
||||
current = openList.getMin();
|
||||
//current = openList.Peek();
|
||||
//g = current.getG();
|
||||
closedList.Enqueue(current);
|
||||
openList.Dequeue();
|
||||
closedList.Insert(current);
|
||||
//closedList.Enqueue(current);
|
||||
openList.removeMin();
|
||||
//openList.Dequeue();
|
||||
|
||||
if (closedList.Exists(target.getCords()))
|
||||
if (current.getCords() == target.getCords())
|
||||
break;
|
||||
|
||||
|
||||
//if (closedList.Exists(target.getCords()))
|
||||
// break;
|
||||
|
||||
var adjacentNodes = GetAdjacentNodes(current.getCords());
|
||||
//g++;
|
||||
|
||||
foreach(var adjacentNode in adjacentNodes)
|
||||
{
|
||||
g = g + crops[(int)adjacentNode.getCords().X, (int)adjacentNode.getCords().Y].getCostOnMovement();
|
||||
g = current.getG() + crops[(int)adjacentNode.getCords().X, (int)adjacentNode.getCords().Y].getCostOnMovement();
|
||||
if (closedList.Exists(adjacentNode.getCords()))
|
||||
continue;
|
||||
if (!(openList.Exists(adjacentNode.getCords())))
|
||||
@ -99,7 +109,8 @@ class Astar
|
||||
adjacentNode.setH(ComputeHScore(adjacentNode.getCords(), target.getCords()));
|
||||
adjacentNode.calculateF();
|
||||
adjacentNode.setParent(current);
|
||||
openList.Enqueue(adjacentNode);
|
||||
openList.Insert(adjacentNode);
|
||||
//openList.Enqueue(adjacentNode);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -111,6 +122,7 @@ class Astar
|
||||
}
|
||||
}
|
||||
}
|
||||
//openList.BuildMinHeap(openList.GetList());
|
||||
}
|
||||
|
||||
while (current != null)
|
||||
@ -119,8 +131,10 @@ class Astar
|
||||
current = current.getParent();
|
||||
}
|
||||
path = path.FlipArray();
|
||||
openList.Clear();
|
||||
closedList.Clear();
|
||||
openList.deleteHeap();
|
||||
closedList.deleteHeap();
|
||||
//openList.Clear();
|
||||
//closedList.Clear();
|
||||
return path;
|
||||
}
|
||||
|
||||
|
195
Game1/Sources/Pathing/A-Star/PathSaver/MinHeap.cs
Normal file
195
Game1/Sources/Pathing/A-Star/PathSaver/MinHeap.cs
Normal file
@ -0,0 +1,195 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class MinHeap
|
||||
{
|
||||
List<Nodes> arr = new List<Nodes>();
|
||||
public MinHeap()
|
||||
{
|
||||
arr = new List<Nodes>();
|
||||
}
|
||||
|
||||
public void Insert(Nodes value)
|
||||
{
|
||||
arr.Add(value);
|
||||
siftUp(arr.Count - 1);
|
||||
}
|
||||
|
||||
public void removeMin()
|
||||
{
|
||||
if (arr.Count == 0)
|
||||
{
|
||||
throw new Exception("Heap is empty!");
|
||||
}
|
||||
else
|
||||
{
|
||||
arr[0] = arr[arr.Count - 1];
|
||||
arr.RemoveAt(arr.Count - 1);
|
||||
if (arr.Count > 0)
|
||||
{
|
||||
siftDown(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void siftUp(int index)
|
||||
{
|
||||
int parentIndex;
|
||||
Nodes temp;
|
||||
if (index != 0)
|
||||
{
|
||||
parentIndex = getParentIndex(index);
|
||||
if (arr[parentIndex].getF() > arr[index].getF())
|
||||
{
|
||||
temp = arr[parentIndex];
|
||||
arr[parentIndex] = arr[index];
|
||||
arr[index] = temp;
|
||||
siftUp(parentIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int getParentIndex(int index)
|
||||
{
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
|
||||
private void siftDown(int nodeIndex)
|
||||
{
|
||||
int leftChildIndex, rightChildIndex, minIndex;
|
||||
Nodes tmp;
|
||||
|
||||
leftChildIndex = getLeftChildIndex(nodeIndex);
|
||||
|
||||
rightChildIndex = getRightChildIndex(nodeIndex);
|
||||
|
||||
if (rightChildIndex >= arr.Count)
|
||||
{
|
||||
if (leftChildIndex >= arr.Count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
minIndex = leftChildIndex;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (arr[leftChildIndex].getF() <= arr[rightChildIndex].getF())
|
||||
{
|
||||
minIndex = leftChildIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
minIndex = rightChildIndex;
|
||||
}
|
||||
}
|
||||
if (arr[nodeIndex].getF() > arr[minIndex].getF())
|
||||
{
|
||||
tmp = arr[minIndex];
|
||||
|
||||
arr[minIndex] = arr[nodeIndex];
|
||||
|
||||
arr[nodeIndex] = tmp;
|
||||
|
||||
siftDown(minIndex);
|
||||
}
|
||||
}
|
||||
|
||||
private int getRightChildIndex(int nodeIndex)
|
||||
{
|
||||
return (2 * nodeIndex) + 2;
|
||||
}
|
||||
|
||||
private int getLeftChildIndex(int nodeIndex)
|
||||
{
|
||||
return (2 * nodeIndex) + 1;
|
||||
}
|
||||
|
||||
public Nodes getMin()
|
||||
{
|
||||
return arr[0];
|
||||
}
|
||||
|
||||
public void BuildMinHeap(List<Nodes> input)
|
||||
{
|
||||
if (arr.Count > 0)
|
||||
{
|
||||
//clear the current heap
|
||||
//Array.Resize(ref arr, input.Length);
|
||||
//heapSize = 0;
|
||||
for (int i = 0; i < arr.Count; i++)
|
||||
{
|
||||
arr[i] = input[i];
|
||||
}
|
||||
}
|
||||
for (int i = arr.Count - 1 / 2; i >= 0; i--)
|
||||
{
|
||||
MinHeapify(i);
|
||||
}
|
||||
}
|
||||
|
||||
private void MinHeapify(int index)
|
||||
{
|
||||
int left = 2 * index;
|
||||
int right = (2 * index) + 1;
|
||||
int smallest = index;
|
||||
if (left < arr.Count && arr[left].getF() < arr[index].getF())
|
||||
{
|
||||
smallest = left;
|
||||
}
|
||||
else
|
||||
{
|
||||
smallest = index;
|
||||
}
|
||||
if (right < arr.Count && arr[right].getF() < arr[smallest].getF())
|
||||
{
|
||||
smallest = right;
|
||||
}
|
||||
if (smallest != index)
|
||||
{
|
||||
swap(ref arr, index, smallest);
|
||||
MinHeapify(smallest);
|
||||
}
|
||||
}
|
||||
|
||||
private void swap(ref List<Nodes> input, int a, int b)
|
||||
{
|
||||
Nodes temp = input[a];
|
||||
input[a] = input[b];
|
||||
input[b] = temp;
|
||||
}
|
||||
|
||||
public int GetSize()
|
||||
{
|
||||
return arr.Count;
|
||||
}
|
||||
|
||||
public Boolean Exists(Vector2 coordinates)
|
||||
{
|
||||
if (arr.Count == 0)
|
||||
return false;
|
||||
foreach (Nodes node in arr)
|
||||
{
|
||||
if (node.getCords() == coordinates)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<Nodes> GetList()
|
||||
{
|
||||
return arr;
|
||||
}
|
||||
|
||||
public void deleteHeap()
|
||||
{
|
||||
arr.Clear();
|
||||
}
|
||||
|
||||
}
|
@ -27,7 +27,7 @@ class Path
|
||||
{
|
||||
nodes[i] = nodes[i + 1];
|
||||
}
|
||||
|
||||
nodes[Count + 1] = null;
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@ class PriorityQueue
|
||||
list.Add(x);
|
||||
int i = Count - 1;
|
||||
|
||||
|
||||
while (i > 0)
|
||||
{
|
||||
int p = (i - 1) / 2;
|
||||
@ -35,6 +36,7 @@ class PriorityQueue
|
||||
}
|
||||
|
||||
if (Count > 0) list[i] = x;
|
||||
|
||||
}
|
||||
|
||||
public void Dequeue()
|
||||
|
Loading…
Reference in New Issue
Block a user