1
0
forked from s425077/PotatoPlan
PotatoPlan/Game1/Sources/Crops/Crops.cs

480 lines
16 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2020-05-06 16:22:30 +02:00
using Microsoft.Xna.Framework.Input;
class Crops
{
2020-05-05 16:27:45 +02:00
private int Status;
private int originalStatus;
private int cropType = 0;
2020-05-07 13:53:07 +02:00
private float Timer = 1;
2020-05-07 01:55:52 +02:00
private int UpdateCrop;
2020-05-07 13:53:07 +02:00
private float fullTimer;
2020-05-05 16:27:45 +02:00
private bool housePos = false;
2020-05-06 16:22:30 +02:00
private Vector2 Size;
2020-05-23 20:37:11 +02:00
private Random r = new Random();
2020-05-07 01:55:52 +02:00
private CropTypes DataSet;
2020-05-06 16:22:30 +02:00
SoilProperties soilProperties = new SoilProperties();
private float ProductionRate;
2020-05-23 20:37:11 +02:00
private float tempRain;
2020-05-24 22:21:49 +02:00
private DayNightCycle Time = new DayNightCycle();
2020-05-03 13:05:05 +02:00
2020-05-24 21:22:17 +02:00
public void updateCrop(Vector2 newSize, float Rain, DayNightCycle nTime)
{
2020-05-24 21:22:17 +02:00
Time = nTime;
2020-05-23 20:37:11 +02:00
tempRain = Rain;
2020-05-07 20:07:11 +02:00
getProductionRate(DataSet);
if (UpdateCrop == 60)
{
2020-05-23 20:37:11 +02:00
degradeSoil(Rain);
UpdateCrop = 0;
}
2020-05-07 01:55:52 +02:00
if (Status == 3 && Timer != 1)
2020-05-06 16:22:30 +02:00
{
Timer = Timer - 1f * ProductionRate;
2020-05-06 16:22:30 +02:00
}
Size = newSize;
2020-05-07 01:55:52 +02:00
UpdateCrop++;
2020-05-07 13:53:07 +02:00
if (Timer < 1)
{
Timer = 1;
}
2020-05-07 01:55:52 +02:00
}
2020-05-10 19:21:52 +02:00
public float getSpeedFactor(float tractorSpeed)
{
2020-05-12 01:46:16 +02:00
if (getCostOnMovement() == 1)
return (1f * tractorSpeed);
return (1f * tractorSpeed) / (getCostOnMovement() / 5.0f);
2020-05-10 19:21:52 +02:00
}
2020-05-07 01:55:52 +02:00
public SoilProperties getSoilProperties()
{
return soilProperties;
}
2020-05-10 01:38:08 +02:00
public void Fertilize(Fertilizer fertilizer)
{
soilProperties.Nitrogen += fertilizer.Nitrogen;
soilProperties.Phosphorous += fertilizer.Phosphorus;
soilProperties.Potassium += fertilizer.Potassium;
2020-05-10 12:55:13 +02:00
if (soilProperties.Nitrogen > 42)
soilProperties.Nitrogen = 42;
if (soilProperties.Phosphorous > 42)
soilProperties.Phosphorous = 42;
if (soilProperties.Potassium > 19)
soilProperties.Potassium = 19;
2020-05-10 01:38:08 +02:00
}
public bool isSaturated()
{
2020-05-10 12:55:13 +02:00
if (soilProperties.Nitrogen > 40 && (soilProperties.Phosphorous > 40 || soilProperties.Potassium > 17))
2020-05-10 01:38:08 +02:00
return true;
2020-05-10 12:55:13 +02:00
if (soilProperties.Phosphorous > 40 && soilProperties.Potassium > 17)
return true;
return false;
}
public bool isSaturated(int threshold)
{
if (soilProperties.Nitrogen > 40 - threshold && (soilProperties.Phosphorous > 40 - threshold || soilProperties.Potassium > 17 - (threshold * 17/40)))
return true;
if (soilProperties.Phosphorous > 40 - threshold && soilProperties.Potassium > 17 - (threshold *17/40))
2020-05-10 01:38:08 +02:00
return true;
return false;
}
2020-05-07 13:53:07 +02:00
public float getCropTimer()
{
return Timer;
}
2020-05-05 16:27:45 +02:00
public int getCropTimerBar(int tileSize)
{
2020-05-06 16:22:30 +02:00
int x = (int)((1 - ((float)Timer / fullTimer)) * (tileSize - tileSize / 3));
2020-05-05 16:27:45 +02:00
return x;
}
2020-05-06 16:22:30 +02:00
public void init()
{
soilProperties.setSoilProperties();
}
2020-05-05 16:27:45 +02:00
// Changes the time required for the crops to be harvestable
public void setCropTimer()
{
2020-05-10 19:21:52 +02:00
if (cropType == 1) // Barley
2020-05-05 16:27:45 +02:00
{
Timer = 300;
fullTimer = Timer;
}
else if (cropType == 1) // Wheat
{
Timer = 600;
fullTimer = Timer;
}
else if (cropType == 2) // Berries
{
Timer = 1200;
fullTimer = Timer;
}
else // Fruit Trees
{
Timer = 2400;
fullTimer = Timer;
2020-05-06 16:22:30 +02:00
}
2020-05-05 16:27:45 +02:00
}
2020-05-03 13:05:05 +02:00
public int getCostOnMovement()
{
if (Status == 1) //grass
2020-05-03 13:05:05 +02:00
{
return 1;
}
else if (Status == 2) //dirt
2020-05-03 13:05:05 +02:00
{
2020-05-10 19:21:52 +02:00
return 8;
2020-05-03 13:05:05 +02:00
}
2020-05-07 01:55:52 +02:00
else
2020-05-03 13:05:05 +02:00
{
2020-05-10 21:31:06 +02:00
if (cropType == 0)
{
return 16; //Barley
}
2020-05-10 19:21:52 +02:00
if (cropType == 1)
2020-05-05 16:27:45 +02:00
{
2020-05-10 19:21:52 +02:00
return 16; //Barley
2020-05-05 16:27:45 +02:00
}
2020-05-10 19:21:52 +02:00
else if (cropType == 2)
2020-05-05 16:27:45 +02:00
{
2020-05-10 19:21:52 +02:00
return 16; //Cotton
2020-05-05 16:27:45 +02:00
}
2020-05-10 19:21:52 +02:00
else if (cropType == 3)
{
return 16; //Ground Nuts
}
else if (cropType == 4)
{
return 16; //Maize
}
else if (cropType == 5)
{
return 16; //Millets
}
else if (cropType == 6)
{
return 16; //Oil Seeds
}
else if (cropType == 7)
{
return 16; //Paddy
}
else if (cropType == 8)
{
return 16; //Pulses
}
else if (cropType == 9)
{
return 16; //Sugarcane
}
else if (cropType == 10)
2020-05-05 16:27:45 +02:00
{
2020-05-10 19:21:52 +02:00
return 16; //Wheat
2020-05-05 16:27:45 +02:00
}
else
{
2020-05-10 19:39:38 +02:00
return 16; //Tobacco
2020-05-05 16:27:45 +02:00
}
2020-05-03 13:05:05 +02:00
}
2020-05-07 01:55:52 +02:00
}
2020-05-23 20:37:11 +02:00
public void degradeSoil(float Rain)
2020-05-07 01:55:52 +02:00
{
if (soilProperties.Nitrogen > 4.0f)
2020-05-03 13:05:05 +02:00
{
2020-05-10 18:17:53 +02:00
soilProperties.Nitrogen = soilProperties.Nitrogen - (soilProperties.NitrogenDegradeRate * (float)Math.Pow(ProductionRate, 2));
2020-05-03 13:05:05 +02:00
}
2020-05-07 01:55:52 +02:00
if (soilProperties.Phosphorous > 0.1f)
{
2020-05-10 18:17:53 +02:00
soilProperties.Phosphorous = soilProperties.Phosphorous - (soilProperties.PhosphorousDegradeRate * (float)Math.Pow(ProductionRate, 2));
2020-05-07 01:55:52 +02:00
}
if (soilProperties.Potassium > 0.1f)
{
2020-05-10 18:17:53 +02:00
soilProperties.Potassium = soilProperties.Potassium - (soilProperties.PotassiumDegradeRate * (float)Math.Pow(ProductionRate, 2));
2020-05-07 01:55:52 +02:00
}
2020-05-23 20:37:11 +02:00
2020-05-24 21:00:24 +02:00
if (Rain >= 0.45f)
2020-05-23 20:37:11 +02:00
{
2020-05-24 21:00:24 +02:00
soilProperties.Rainfall = soilProperties.Rainfall + Rain * 600;
2020-05-23 20:37:11 +02:00
}
}
2020-05-24 21:00:24 +02:00
public void setPrevRainfall()
2020-05-23 20:37:11 +02:00
{
2020-05-24 21:00:24 +02:00
soilProperties.prevRainfall = soilProperties.Rainfall;
if (soilProperties.prevRainfall > 3616)
soilProperties.prevRainfall = 3616;
else if (soilProperties.prevRainfall < 236)
soilProperties.prevRainfall = 236;
2020-05-23 20:37:11 +02:00
soilProperties.Rainfall = 0;
2020-05-24 21:00:24 +02:00
}
2020-05-06 16:22:30 +02:00
2020-05-07 01:55:52 +02:00
public void setCropType(int Type, CropTypes nCropType)
{
2020-05-24 21:22:17 +02:00
2020-05-07 01:55:52 +02:00
DataSet = nCropType;
2020-05-05 16:27:45 +02:00
cropType = Type;
2020-05-24 21:22:17 +02:00
if (Status == 3)
{
soilProperties.Area = r.Next(nCropType.AreaMin, nCropType.AreaMax);
}
}
2020-05-03 13:05:05 +02:00
2020-05-05 16:27:45 +02:00
public int getStatus()
{
2020-05-07 01:55:52 +02:00
if (Status != 3)
2020-05-05 16:27:45 +02:00
{
return Status;
}
else
{
return Status; // + cropType; When unique crop textures have been added
}
}
public int getCropType()
{
return cropType;
}
public void setStatus(int newStatus)
{
Status = newStatus;
}
public void setOriginalStatus()
{
originalStatus = Status;
}
public void setHousePos(bool state)
{
housePos = state;
if (state)
{
2020-05-24 17:08:41 +02:00
Timer = 1;
2020-05-05 16:27:45 +02:00
Status = 1;
}
else
{
Status = originalStatus;
}
}
public bool getHousePos()
2020-05-03 13:05:05 +02:00
{
2020-05-05 16:27:45 +02:00
return housePos;
2020-05-03 13:05:05 +02:00
}
2020-05-06 16:22:30 +02:00
2020-05-10 01:38:08 +02:00
public bool belowCapacity()
{
return ((int)(soilProperties.Nitrogen + soilProperties.Potassium + soilProperties.Phosphorous)) < soilProperties.Capacity;
}
2020-05-10 18:17:53 +02:00
public Vector4 getColour()
{
float r, g, b, a;
float productionRate, overhead;
if (ProductionRate > 1.0f)
{
productionRate = 1.0f;
overhead = ProductionRate - 1.0f;
}
else
{
productionRate = ProductionRate;
overhead = 0.0f;
}
2020-05-10 21:16:24 +02:00
r = (1.0f - productionRate) * 4;
2020-05-10 18:17:53 +02:00
g = 0.0f + productionRate;
2020-05-10 23:21:03 +02:00
b = 0.0f + overhead * 3;
2020-05-10 18:17:53 +02:00
a = 255;
return new Vector4(r, g, b, a);
}
public float getProductionRate(CropTypes Sample)
2020-05-07 01:55:52 +02:00
{
2020-05-24 22:21:49 +02:00
float predProd = 1.0f;
if (Status > 1)
{
bool correctSeason = false;
foreach (string i in Sample.Season)
{
if (i == Time.getTimeOfYear() || i == "Whole Year")
{
correctSeason = true;
break;
}
}
if (correctSeason)
{
predProd = Game1.Sources.ML_Joel.Engine.PredictProductionwithRainfall(soilProperties, Time);
predProd = (predProd / soilProperties.Area) / 2;
}
else
predProd = 0.20f;
}
2020-05-07 13:53:07 +02:00
ProductionRate = 1;
2020-05-09 15:54:53 +02:00
float min = 1.0f;
2020-05-07 01:55:52 +02:00
if (DataSet != null)
{
2020-05-10 18:17:53 +02:00
//ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Temperature, Sample.Temparature));
//ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Moisture, Sample.Moisture));
//ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Humidity, Sample.Humidity));
2020-05-11 01:11:00 +02:00
if (DataSet.soilType[0] != null)
{
if (Sample.soilType[0] == soilProperties.soilType)
{
ProductionRate = ProductionRate + 0.1f;
}
if (DataSet.soilType[1] != null)
{
if (Sample.soilType[1] == soilProperties.soilType)
{
ProductionRate = ProductionRate + 0.08f;
}
if (DataSet.soilType[2] != null)
{
if (Sample.soilType[2] == soilProperties.soilType)
{
ProductionRate = ProductionRate + 0.5f;
}
}
}
}
2020-05-09 15:54:53 +02:00
min = Math.Min(compareToDatset(soilProperties.Phosphorous, Sample.Phosphorous), Math.Min(compareToDatset(soilProperties.Potassium, Sample.Potassium), compareToDatset(soilProperties.Nitrogen, Sample.Nitrogen)));
ProductionRate = ProductionRate + (ProductionRate * min);
2020-05-07 01:55:52 +02:00
if (ProductionRate < 0)
{
2020-05-07 13:53:07 +02:00
ProductionRate = 0;
2020-05-07 01:55:52 +02:00
}
2020-05-10 18:17:53 +02:00
ProductionRate = ProductionRate / 1.5f;
2020-05-07 01:55:52 +02:00
}
2020-05-10 16:42:25 +02:00
ProductionRate = (float)Math.Pow(ProductionRate, 2.5f);
2020-05-24 22:21:49 +02:00
return ProductionRate * predProd;
2020-05-07 01:55:52 +02:00
}
2020-05-10 21:16:24 +02:00
public float getProductionRate()
{
return ProductionRate;
}
2020-05-07 01:55:52 +02:00
public float compareToDatset(float i, float j)
{
if (i < j)
2020-05-07 13:53:07 +02:00
{
return (i / j);
2020-05-07 01:55:52 +02:00
}
else
{
return (j / i);
2020-05-07 01:55:52 +02:00
}
2020-05-07 01:55:52 +02:00
}
2020-05-06 16:22:30 +02:00
public void Inspect(int tileSize, int Spacing, SpriteFont Bold, SpriteBatch spriteBatch, string[] cropTypesNames)
{
spriteBatch.Begin();
if (housePos)
{
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, "Tiletype: House", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.Teal);
2020-05-06 16:22:30 +02:00
}
else if (Status == 0)
{
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, "Tiletype: Boulders", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.Teal);
2020-05-06 16:22:30 +02:00
}
else if (Status == 1)
{
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, "Tiletype: Grassfield", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.Teal);
2020-05-06 16:22:30 +02:00
}
else if (Status == 2)
{
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, "Tiletype: Soil", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.Teal);
2020-05-06 16:22:30 +02:00
}
else if (Status == 3)
{
int x = (int)(((float)Timer / fullTimer) * 100);
x = 100 - x;
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, "Tiletype: Crop ", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.Teal);
spriteBatch.DrawString(Bold, "Completion: " + x + "%", new Vector2(240, Size.Y * (tileSize + Spacing) + 82), Color.Teal);
2020-05-06 16:22:30 +02:00
}
2020-05-07 01:55:52 +02:00
if (Status != 3)
2020-05-06 16:22:30 +02:00
{
spriteBatch.DrawString(Bold, "-------------", new Vector2(240, Size.Y * (tileSize + Spacing) + 82), Color.DarkRed);
}
if (Status > 1)
{
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, "Prefered Crop: " + cropTypesNames[cropType], new Vector2(240, Size.Y * (tileSize + Spacing) + 62), Color.Teal);
2020-05-06 16:22:30 +02:00
}
else
{
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, "None", new Vector2(240, Size.Y * (tileSize + Spacing) + 62), Color.Teal);
2020-05-06 16:22:30 +02:00
}
spriteBatch.DrawString(Bold, "Soil Properties:", new Vector2(240, Size.Y * (tileSize + Spacing) + 122), Color.DarkRed);
2020-05-07 01:55:52 +02:00
spriteBatch.DrawString(Bold, "Soil Type: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 142), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, soilProperties.soilType, new Vector2(370, Size.Y * (tileSize + Spacing) + 142), Color.Teal);
2020-05-07 01:55:52 +02:00
spriteBatch.DrawString(Bold, "Temparature: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 162), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, soilProperties.Temperature.ToString(), new Vector2(370, Size.Y * (tileSize + Spacing) + 162), Color.Teal);
2020-05-07 01:55:52 +02:00
spriteBatch.DrawString(Bold, "Moisture: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 182), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, soilProperties.Moisture.ToString(), new Vector2(370, Size.Y * (tileSize + Spacing) + 182), Color.Teal);
2020-05-07 01:55:52 +02:00
spriteBatch.DrawString(Bold, "Humidity: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 202), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, soilProperties.Humidity.ToString(), new Vector2(370, Size.Y * (tileSize + Spacing) + 202), Color.Teal);
2020-05-07 01:55:52 +02:00
spriteBatch.DrawString(Bold, "Phosphorous: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 222), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, Math.Round(soilProperties.Phosphorous,1).ToString(), new Vector2(370, Size.Y * (tileSize + Spacing) + 222), Color.Teal);
2020-05-07 01:55:52 +02:00
spriteBatch.DrawString(Bold, "Potassium: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 242), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, Math.Round(soilProperties.Potassium, 1).ToString(), new Vector2(370, Size.Y * (tileSize + Spacing) + 242), Color.Teal);
2020-05-07 01:55:52 +02:00
spriteBatch.DrawString(Bold, "Nitrogen: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 262), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, Math.Round(soilProperties.Nitrogen, 1).ToString(), new Vector2(370, Size.Y * (tileSize + Spacing) + 262), Color.Teal);
2020-05-07 01:55:52 +02:00
spriteBatch.DrawString(Bold, "Production Rate: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 282), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, Math.Round((ProductionRate * 100), 1).ToString() + "%", new Vector2(370, Size.Y * (tileSize + Spacing) + 282), Color.Teal);
2020-05-23 20:37:11 +02:00
spriteBatch.DrawString(Bold, "Last Years Rainfall: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 302), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, Math.Round((soilProperties.prevRainfall), 1).ToString() + "mm", new Vector2(370, Size.Y * (tileSize + Spacing) + 302), Color.Teal);
2020-05-23 20:37:11 +02:00
spriteBatch.DrawString(Bold, "Rainfall: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 322), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, Math.Round((soilProperties.Rainfall), 1).ToString() + "mm", new Vector2(370, Size.Y * (tileSize + Spacing) + 322), Color.Teal);
2020-05-23 20:37:11 +02:00
spriteBatch.DrawString(Bold, "Rain mm/s: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 342), Color.DarkRed);
2020-05-24 21:00:24 +02:00
spriteBatch.DrawString(Bold, Math.Round((tempRain * 2), 2).ToString() + "mm", new Vector2(370, Size.Y * (tileSize + Spacing) + 342), Color.Teal);
spriteBatch.DrawString(Bold, "Area: ", new Vector2(240, Size.Y * (tileSize + Spacing) + 362), Color.DarkRed);
spriteBatch.DrawString(Bold, soilProperties.Area + "m^2", new Vector2(370, Size.Y * (tileSize + Spacing) + 362), Color.Teal);
2020-05-06 16:22:30 +02:00
spriteBatch.End();
}
}