some fixes, optimized AI

This commit is contained in:
BOTLester 2020-05-10 13:51:19 +02:00
parent 3647b10dc5
commit 2375a52cd4
4 changed files with 27 additions and 26 deletions

View File

@ -133,7 +133,7 @@ class Crops
} }
else if (Status == 2) //dirt else if (Status == 2) //dirt
{ {
return 8; return 16;
} }
else else
{ {

View File

@ -75,9 +75,9 @@ class Astar
if (currDir == newDir) if (currDir == newDir)
return 0; return 0;
else if (Math.Abs(currDir - newDir) == 1 || Math.Abs(currDir - newDir) == 3) else if (Math.Abs(currDir - newDir) == 1 || Math.Abs(currDir - newDir) == 3)
return 20; return 3;
else if (Math.Abs(currDir - newDir) == 0 || Math.Abs(currDir - newDir) == 2) else if (Math.Abs(currDir - newDir) == 0 || Math.Abs(currDir - newDir) == 2)
return 900; return 18;
return 0; return 0;
} }

View File

@ -45,17 +45,19 @@ class AI
inventory.printItems(input, spriteBatch, Bold); inventory.printItems(input, spriteBatch, Bold);
} }
public Path newTarget()
public Vector2 newTarget()
{ {
PriorityQueueC5 queue = new PriorityQueueC5(); PriorityQueueC5 queue = new PriorityQueueC5();
int score = 0; int score = 0;
int count = 0; int count = 0;
int testsize = 2; int testsize = 2;
Vector2 newTarget; Path newTarget;
if (tractorPos != housePos) if (tractorPos != housePos)
if (inventory.getWeight() == inventory.getMaxWeight() || inventory.isMissingFertilizer()) if (inventory.getWeight() == inventory.getMaxWeight() || inventory.isMissingFertilizer())
return housePos; {
astar.update(farm.getCrops(), Size, tractorPos, housePos, housePos, Rotation);
return astar.FindPath(true);
}
while (true) while (true)
{ {
for (int x = 0; x < Size.X; x++) for (int x = 0; x < Size.X; x++)
@ -63,7 +65,7 @@ class AI
{ {
if (farm.getCrop(x, y).getStatus() >= 2 && tractorPos != new Vector2(x, y)) if (farm.getCrop(x, y).getStatus() >= 2 && tractorPos != new Vector2(x, y))
{ {
if (farm.getCrop(x, y).isSaturated(5) || !farm.getCrop(x, y).belowCapacity()) if (farm.getCrop(x, y).getStatus() != 2 && (farm.getCrop(x, y).isSaturated(5) || !farm.getCrop(x, y).belowCapacity()))
{ {
//do nothing //do nothing
} }
@ -100,7 +102,7 @@ class AI
{ {
farm.getCrop(x, y).Fertilize(fertilizer); farm.getCrop(x, y).Fertilize(fertilizer);
fertilizer = fertilizerHolder.GetFertilizer(Engine.PredictFertilizer(farm.getCrop(x, y), farm.getPresetCropTypes(farm.getCrop(x, y).getCropType()))); fertilizer = fertilizerHolder.GetFertilizer(Engine.PredictFertilizer(farm.getCrop(x, y), farm.getPresetCropTypes(farm.getCrop(x, y).getCropType())));
System.Threading.Thread.Sleep(5); //System.Threading.Thread.Sleep(5);
} }
} }
if (farm.getCrop(x, y).getCropTimer() == 1) if (farm.getCrop(x, y).getCropTimer() == 1)
@ -164,12 +166,13 @@ class AI
return ((val - min) / (max - min)); return ((val - min) / (max - min));
} }
public Vector2 GetMinFNode(int testSize, PriorityQueueC5 queue) public Path GetMinFNode(int testSize, PriorityQueueC5 queue)
{ {
int index = 0; int index = 0;
int min = 9999; int min = 9999;
Path path = new Path(); Path path = new Path();
List<PQEntry> entryList = new List<PQEntry>(); List<PQEntry> entryList = new List<PQEntry>();
Path[] pathList = new Path[testSize];
for (int i = 0; i < testSize; i++) for (int i = 0; i < testSize; i++)
{ {
entryList.Add(queue.DeleteMax()); entryList.Add(queue.DeleteMax());
@ -178,7 +181,9 @@ class AI
for (int i = 0; i < testSize; i++) for (int i = 0; i < testSize; i++)
{ {
Nodes temp = new Nodes(entryList[i].Coordinates); Nodes temp = new Nodes(entryList[i].Coordinates);
astar.update(farm.getCrops(), Size, tractorPos, housePos, temp.getCords(), Rotation);
path = astar.FindPath(false); path = astar.FindPath(false);
pathList[i] = path;
Nodes tempF = new Nodes(path.getByIndex(0)); Nodes tempF = new Nodes(path.getByIndex(0));
if (min > tempF.getF()) if (min > tempF.getF())
{ {
@ -186,21 +191,17 @@ class AI
index = i; index = i;
} }
} }
PQEntry minEntry = entryList[index];
entryList.RemoveAt(index);
//add the non-minimum entries back to the queue. return pathList[index].FlipArray();
queue.AddAll(entryList);
return minEntry.Coordinates;
} }
public Vector2 GetMaxFNode(int testSize, PriorityQueueC5 queue) public Path GetMaxFNode(int testSize, PriorityQueueC5 queue)
{ {
int index = 0; int index = 0;
int max = -9999; int max = -9999;
Path path = new Path(); Path path = new Path();
List<PQEntry> entryList = new List<PQEntry>(); List<PQEntry> entryList = new List<PQEntry>();
Path[] pathList = new Path[testSize];
for (int i = 0; i < testSize; i++) for (int i = 0; i < testSize; i++)
{ {
entryList.Add(queue.DeleteMax()); entryList.Add(queue.DeleteMax());
@ -209,7 +210,9 @@ class AI
for (int i = 0; i < testSize; i++) for (int i = 0; i < testSize; i++)
{ {
Nodes temp = new Nodes(entryList[i].Coordinates); Nodes temp = new Nodes(entryList[i].Coordinates);
astar.update(farm.getCrops(), Size, tractorPos, housePos, temp.getCords(), Rotation);
path = astar.FindPath(false); path = astar.FindPath(false);
pathList[i] = path;
Nodes tempF = new Nodes(path.getByIndex(0)); Nodes tempF = new Nodes(path.getByIndex(0));
if (max < tempF.getF()) if (max < tempF.getF())
{ {
@ -217,15 +220,11 @@ class AI
index = i; index = i;
} }
} }
PQEntry minEntry = entryList[index];
entryList.RemoveAt(index);
//add the non-minimum entries back to the queue. return pathList[index].FlipArray();
queue.AddAll(entryList);
return minEntry.Coordinates;
} }
public void reloadCargo() public void reloadCargo()
{ {
inventory.clearInventory(); inventory.clearInventory();

View File

@ -20,12 +20,14 @@ class SmartTractor
farm.UpdatePreferedCrops(Size); farm.UpdatePreferedCrops(Size);
farm = ai.changeCropStatus(); farm = ai.changeCropStatus();
astar.update(farm.getCrops(), Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Rotation); astar.update(farm.getCrops(), Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Rotation);
getTargetPosition(ai.newTarget()); //getTargetPosition(ai.newTarget());
astar.update(farm.getCrops(), Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target / (tileSize + Spacing), Rotation); //astar.update(farm.getCrops(), Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target / (tileSize + Spacing), Rotation);
if (tractorPos == housePos) if (tractorPos == housePos)
ai.reloadCargo(); ai.reloadCargo();
return ai.newTarget();
return astar.FindPath(true);
//return astar.FindPath(true);
} }