Add evaluation of migration and agriculture

This commit is contained in:
Andrew Cooper
2023-01-19 07:36:00 +11:00
parent 9d8a4fbc93
commit 051f3eb5d5
11 changed files with 95 additions and 24 deletions

View File

@@ -6,6 +6,7 @@ internal class Year
{
private readonly Country _country;
private readonly IRandom _random;
private readonly IReadWrite _io;
private readonly int _plantingCost;
private readonly int _landValue;
@@ -14,7 +15,12 @@ internal class Year
private float _landPlanted;
private float _pollutionControlCost;
public Year(Country country, IRandom random)
private float _citizenSupport;
private int _deaths;
private int _pollutionDeaths;
public Year(Country country, IRandom random, IReadWrite io)
{
_country = country;
_random = random;
@@ -35,54 +41,81 @@ internal class Year
return playerSoldLand || playerDistributedRallods || playerPlantedLand || playerControlledPollution;
}
public Result EvaluateResults(IReadWrite io)
public Result EvaluateResults()
{
var unspentRallods = _country.Rallods;
var statusUpdate = new StringBuilder();
var result = EvaluateDeaths(statusUpdate, out var deaths);
io.Write(statusUpdate);
var result = EvaluateDeaths();
return Result.Continue;
}
public Result? EvaluateDeaths(StringBuilder statusUpdate, out int deaths)
public Result? EvaluateDeaths()
{
deaths = default;
var supportedCountrymen = _rallodsDistributed / 100;
var starvationDeaths = _country.Countrymen - supportedCountrymen;
_citizenSupport = supportedCountrymen - _country.Countrymen;
var starvationDeaths = -_citizenSupport;
if (starvationDeaths > 0)
{
if (supportedCountrymen < 50) { return Result.GameOver(EndOneThirdDead(_random)); }
statusUpdate.AppendLine(DeathsStarvation(starvationDeaths));
_io.WriteLine(DeathsStarvation(starvationDeaths));
}
var pollutionControl = _pollutionControlCost >= 25 ? _pollutionControlCost / 25 : 1;
var pollutionDeaths = (int)(_random.Next((int)_country.IndustryLand) / pollutionControl);
if (pollutionDeaths > 0)
_pollutionDeaths = (int)(_random.Next((int)_country.IndustryLand) / pollutionControl);
if (_pollutionDeaths > 0)
{
statusUpdate.AppendLine(DeathsPollution(pollutionDeaths));
_io.WriteLine(DeathsPollution(_pollutionDeaths));
}
deaths = (int)(starvationDeaths + pollutionDeaths);
if (deaths > 0)
_deaths = (int)(starvationDeaths + _pollutionDeaths);
if (_deaths > 0)
{
var funeralCosts = deaths * 9;
statusUpdate.AppendLine(FuneralExpenses(funeralCosts));
var funeralCosts = _deaths * 9;
_io.WriteLine(FuneralExpenses(funeralCosts));
if (!_country.TrySpend(funeralCosts, _landValue))
{
statusUpdate.AppendLine(InsufficientReserves);
_io.WriteLine(InsufficientReserves);
}
_country.RemoveTheDead(deaths);
_country.RemoveTheDead(_deaths);
}
return null;
}
private Result? EvaluateMigration()
{
if (_landSold > 0)
{
var newWorkers = (int)(_landSold + _random.NextFloat(10) - _random.NextFloat(20));
if (!_country.HasWorkers) { newWorkers += 20; }
_io.Write(WorkerMigration(newWorkers));
_country.AddWorkers(newWorkers);
}
var migration =
(int)(_citizenSupport / 10 + _pollutionControlCost / 25 - _country.IndustryLand / 50 - _pollutionDeaths / 2);
_io.WriteLine(Migration(migration));
_country.Migration(migration);
return null;
}
private Result? EvaluateAgriculture()
{
var ruinedCrops = (int)Math.Min(_country.IndustryLand * (_random.NextFloat() + 1.5f) / 2, _landPlanted);
var yield = (int)(_landPlanted - ruinedCrops);
var income = (int)(yield * _landValue / 2f);
_io.Write(LandPlanted(_landPlanted));
_io.Write(Harvest(yield, income, _country.IndustryLand > 0));
_country.SellCrops(income);
return null;
}
internal record struct Result (bool IsGameOver, string Message)
{