2020-05-05 16:27:45 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Xna.Framework;
|
2020-05-06 16:22:30 +02:00
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2020-05-05 16:27:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AI
|
|
|
|
|
{
|
|
|
|
|
private Vector2 tractorPos;
|
|
|
|
|
private Vector2 housePos;
|
2020-05-06 16:22:30 +02:00
|
|
|
|
private Farm farm;
|
2020-05-05 16:27:45 +02:00
|
|
|
|
private Vector2 Size;
|
|
|
|
|
private Vector2 targetPos;
|
2020-05-06 16:22:30 +02:00
|
|
|
|
private Inventory inventory = new Inventory();
|
2020-05-05 16:27:45 +02:00
|
|
|
|
private int Rotation;
|
2020-05-07 22:09:45 +02:00
|
|
|
|
private PriorityQueueC5 PriorityQueueC5;
|
|
|
|
|
private Astar astar = new Astar();
|
2020-05-05 16:27:45 +02:00
|
|
|
|
|
|
|
|
|
private Random r = new Random();
|
|
|
|
|
|
|
|
|
|
|
2020-05-06 16:22:30 +02:00
|
|
|
|
public void init()
|
|
|
|
|
{
|
|
|
|
|
inventory.initInventorySystem();
|
|
|
|
|
inventory.addItem(1, 1);
|
|
|
|
|
inventory.addItem(1, 1);
|
|
|
|
|
inventory.addItem(3, 1);
|
|
|
|
|
inventory.addItem(5, 1);
|
|
|
|
|
inventory.addItem(7, 1);
|
|
|
|
|
inventory.addItem(7, 1);
|
|
|
|
|
inventory.addItem(10, 1);
|
2020-05-07 21:46:33 +02:00
|
|
|
|
inventory.addItem(10, 1);
|
2020-05-06 16:22:30 +02:00
|
|
|
|
inventory.useItem(10, 1);
|
|
|
|
|
}
|
2020-05-05 16:27:45 +02:00
|
|
|
|
|
|
|
|
|
|
2020-05-06 16:22:30 +02:00
|
|
|
|
public void update(Farm newFarm, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newtargetPos, int rotation)
|
2020-05-05 16:27:45 +02:00
|
|
|
|
{
|
|
|
|
|
tractorPos = new Vector2((int)newTractorPos.X, (int)newTractorPos.Y);
|
|
|
|
|
housePos = new Vector2((int)newHousePos.X, (int)newHousePos.Y);
|
|
|
|
|
targetPos = newtargetPos;
|
2020-05-06 16:22:30 +02:00
|
|
|
|
farm = newFarm;
|
2020-05-05 16:27:45 +02:00
|
|
|
|
Size = newSize;
|
|
|
|
|
Rotation = rotation;
|
2020-05-07 22:09:45 +02:00
|
|
|
|
astar.update(farm.getCrops(), Size, tractorPos, housePos, rotation);
|
2020-05-05 16:27:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-05-06 16:22:30 +02:00
|
|
|
|
public void drawInventory(Input input, SpriteBatch spriteBatch, SpriteFont Bold, Cargo itemStorageDefined)
|
|
|
|
|
{
|
|
|
|
|
inventory.printItems(input, spriteBatch, Bold);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-05-05 16:27:45 +02:00
|
|
|
|
public Vector2 newTarget()
|
|
|
|
|
{
|
2020-05-07 22:09:45 +02:00
|
|
|
|
PriorityQueueC5 queue = new PriorityQueueC5();
|
|
|
|
|
int score = 0;
|
2020-05-08 00:35:53 +02:00
|
|
|
|
int count = 0;
|
|
|
|
|
int testsize = 2;
|
2020-05-07 22:09:45 +02:00
|
|
|
|
Vector2 newTarget;
|
2020-05-08 00:35:53 +02:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < Size.X; x++)
|
|
|
|
|
for (int y = 0; y < Size.Y; y++)
|
2020-05-07 22:09:45 +02:00
|
|
|
|
{
|
2020-05-08 00:35:53 +02:00
|
|
|
|
if (farm.getCrop(x, y).getStatus() >= 2 && tractorPos != new Vector2(x, y))
|
2020-05-07 22:09:45 +02:00
|
|
|
|
{
|
2020-05-08 00:35:53 +02:00
|
|
|
|
if (farm.getCrop(x, y).getStatus() == 3 && farm.getCrop(x, y).getCropTimer() != 1)
|
|
|
|
|
{
|
|
|
|
|
if (housePos == tractorPos)
|
|
|
|
|
{
|
|
|
|
|
score = calculateSoilScore(x, y);
|
|
|
|
|
queue.AddToQueue(x, y, score);
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
//do nothing
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
score = calculateSoilScore(x, y);
|
|
|
|
|
queue.AddToQueue(x, y, score);
|
|
|
|
|
count++;
|
|
|
|
|
}
|
2020-05-07 22:09:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-08 00:35:53 +02:00
|
|
|
|
if (count > 0)
|
|
|
|
|
break;
|
|
|
|
|
else if (tractorPos != housePos)
|
|
|
|
|
return housePos;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
else if (tractorPos == housePos)
|
|
|
|
|
{
|
|
|
|
|
List<Nodes> temp = astar.GetAdjacentNodes(tractorPos);
|
|
|
|
|
return temp[0].getCords();
|
2020-05-07 22:09:45 +02:00
|
|
|
|
}
|
2020-05-08 00:35:53 +02:00
|
|
|
|
*/
|
|
|
|
|
}
|
2020-05-07 22:09:45 +02:00
|
|
|
|
|
2020-05-08 00:35:53 +02:00
|
|
|
|
newTarget = GetMinFNode(Math.Min(testsize, count), queue);
|
2020-05-07 22:09:45 +02:00
|
|
|
|
return newTarget;
|
2020-05-05 16:27:45 +02:00
|
|
|
|
}
|
2020-05-06 16:22:30 +02:00
|
|
|
|
|
|
|
|
|
public Farm changeCropStatus()
|
|
|
|
|
{
|
|
|
|
|
if (farm.getCrop((int)tractorPos.X, (int)tractorPos.Y).getCropTimer() == 1)
|
|
|
|
|
{
|
|
|
|
|
farm.setCropStatus(tractorPos.X, tractorPos.Y);
|
|
|
|
|
}
|
|
|
|
|
return farm;
|
|
|
|
|
}
|
2020-05-06 20:48:20 +02:00
|
|
|
|
|
|
|
|
|
public Inventory getInventory()
|
|
|
|
|
{
|
|
|
|
|
return inventory;
|
|
|
|
|
}
|
2020-05-07 22:09:45 +02:00
|
|
|
|
|
|
|
|
|
private int calculateSoilScore(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
int score = 0;
|
|
|
|
|
int statusScore = 0;
|
|
|
|
|
int aproxDistance = (int)(Math.Abs(x - tractorPos.X) + Math.Abs(y - tractorPos.Y));
|
|
|
|
|
CropTypesHolder holder = new CropTypesHolder();
|
|
|
|
|
holder.init();
|
|
|
|
|
|
|
|
|
|
Crops crop = farm.getCrop(x, y);
|
|
|
|
|
SoilProperties soilP = crop.getSoilProperties();
|
|
|
|
|
int cropType = crop.getCropType();
|
|
|
|
|
CropTypes avgHold = holder.getPresetCropTypes(cropType);
|
|
|
|
|
|
|
|
|
|
if (crop.getStatus() == 2)
|
|
|
|
|
statusScore = 25;
|
|
|
|
|
else if (crop.getStatus() == 3)
|
|
|
|
|
statusScore = 5;
|
|
|
|
|
else if (crop.getStatus() == 4)
|
|
|
|
|
statusScore = -10;
|
|
|
|
|
else
|
|
|
|
|
statusScore = 1;
|
|
|
|
|
|
|
|
|
|
float[] currentValue = { soilP.Temperature, soilP.Humidity, soilP.Moisture, soilP.Nitrogen, soilP.Potassium, soilP.Phosphorous };
|
|
|
|
|
float[] targetAvg = { avgHold.Temparature, avgHold.Humidity, avgHold.Moisture, avgHold.Nitrogen, avgHold.Potassium, avgHold.Phosphorous };
|
|
|
|
|
float[,] minMax = { {22,30}, {22*1.9f, 30*2.1f}, {20, 70}, {4, 55}, {0, 28}, {0, 60} }; //probably should make it dynamic
|
|
|
|
|
float[] normVal = normArray(minMax, currentValue);
|
|
|
|
|
float[] normAvg = normArray(minMax, targetAvg);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < normVal.Count(); i++)
|
|
|
|
|
{
|
|
|
|
|
score = score + (int)Math.Round(System.Convert.ToDouble(10*(1 - Math.Abs(normAvg[i] - normVal[i]))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return score + (-aproxDistance * 3) + statusScore;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float[] normArray(float[,] minMax, float[] values)
|
|
|
|
|
{
|
|
|
|
|
float[] temp = new float[values.Count()];
|
|
|
|
|
if (minMax.GetLength(0) != values.Count())
|
|
|
|
|
throw new InvalidOperationException("Values and their MinMax arrays are not of the same length!");
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < values.Count(); i++)
|
|
|
|
|
temp[i] = norm(minMax[i, 0], minMax[i, 1], values[i]);
|
|
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float norm(float min, float max, float val)
|
|
|
|
|
{
|
|
|
|
|
return ((val - min) / (max - min));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 GetMinFNode(int testSize, PriorityQueueC5 queue)
|
|
|
|
|
{
|
|
|
|
|
int index = 0;
|
|
|
|
|
int min = 999;
|
|
|
|
|
Path path = new Path();
|
|
|
|
|
List<PQEntry> entryList = new List<PQEntry>();
|
|
|
|
|
for (int i = 0; i < testSize; i++)
|
|
|
|
|
{
|
|
|
|
|
entryList.Add(queue.DeleteMax());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < testSize; i++)
|
|
|
|
|
{
|
|
|
|
|
Nodes temp = new Nodes(entryList[i].Coordinates);
|
|
|
|
|
path = astar.FindPath(false);
|
|
|
|
|
Nodes tempF = new Nodes(path.getByIndex(0));
|
|
|
|
|
if (min > tempF.getF())
|
|
|
|
|
{
|
|
|
|
|
min = tempF.getF();
|
|
|
|
|
index = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PQEntry minEntry = entryList[index];
|
|
|
|
|
entryList.RemoveAt(index);
|
|
|
|
|
|
|
|
|
|
//add the non-minimum entries back to the queue.
|
|
|
|
|
queue.AddAll(entryList);
|
|
|
|
|
|
|
|
|
|
return minEntry.Coordinates;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 16:27:45 +02:00
|
|
|
|
}
|