Skip to content

Commit

Permalink
Merge pull request #440 from Nashet/ver.0.19.0
Browse files Browse the repository at this point in the history
Upload Ver 0 19 0
  • Loading branch information
Nashet authored Mar 12, 2018
2 parents f407019 + e3589b5 commit 9ab83a8
Show file tree
Hide file tree
Showing 32 changed files with 305 additions and 211 deletions.
Binary file modified Assets/EconomicSimulation/ES-base.unity
Binary file not shown.
48 changes: 34 additions & 14 deletions Assets/EconomicSimulation/Scripts/Logic/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract public class Agent : IHasCountry, IStatisticable
{
/// <summary> Used to calculate income tax, now it's only for statistics </summary>
public Money moneyIncomeThisTurn = new Money(0);
private readonly Money moneyIncomeLastTurn = new Money(0);
protected readonly Money moneyIncomeLastTurn = new Money(0);
private readonly Money cash = new Money(0);
public ReadOnlyValue Cash { get { return cash; } }

Expand Down Expand Up @@ -61,10 +61,10 @@ public virtual void SetStatisticToZero()
incomeTaxPayed.SetZero();
}
/// <summary> Returns difference between moneyIncomeLastTurn and value</summary>
protected Value getSpendingLimit(ReadOnlyValue value)
{
return moneyIncomeLastTurn.Copy().Subtract(value, false);
}
//protected Value getSpendingLimit(ReadOnlyValue value)
//{
// return moneyIncomeLastTurn.Copy().Subtract(value, false);
//}



Expand Down Expand Up @@ -125,13 +125,15 @@ public Value getMoneyAvailable()
// return loans.get() * -1f;
//}
//***************

/// <summary>
/// Ignores if need is available on market or not
/// </summary>
internal bool CanAfford(Storage need)
{
Storage realNeed;
if (need.isAbstractProduct())
//realNeed = new Storage(Game.market.getCheapestSubstitute(need).Product, need);
realNeed = Game.market.getCheapestSubstitute(need);
realNeed = Game.market.GetRandomCheapestSubstitute(need);
else
realNeed = need;

Expand Down Expand Up @@ -243,12 +245,7 @@ internal bool CanPayCashOnly(ReadOnlyValue howMuchPay)
public bool PayWithoutRecord(Agent whom, ReadOnlyValue howMuch, bool showMessageAboutNegativeValue = true)
{
if (CanPay(howMuch))// It does has enough cash or deposit
{
//if (!canPayCashOnly(howMuch) && bank != null)// checked for bank invention
//{
// bank.giveLackingMoneyInCredit(this, howMuch);
// bank.giveLackingMoneyInCredit(this, howMuch.Copy().Multiply(5));
//}
{
if (CanPayCashOnly(howMuch))
{
whom.cash.Add(howMuch);
Expand All @@ -264,7 +261,30 @@ public bool PayWithoutRecord(Agent whom, ReadOnlyValue howMuch, bool showMessage
Debug.Log("Not enough money to pay in Agent.payWithoutRecord");
return false;
}

}
/// <summary>
/// Checks inside. Wouldn't pay if can't. Takes back deposits from bank, if needed
/// Doesn't pay tax, doesn't register transaction
/// </summary>
public bool PayWithoutRecord(Money whom, ReadOnlyValue howMuch, bool showMessageAboutNegativeValue = true)
{
if (CanPay(howMuch))// It does has enough cash or deposit
{
if (CanPayCashOnly(howMuch))
{
whom.Add(howMuch);
this.cash.Subtract(howMuch);
}
else
Bank.ReturnDeposit(this, HowMuchLacksMoneyCashOnly(howMuch));
return true;
}
else
{
if (showMessageAboutNegativeValue)
Debug.Log("Not enough money to pay in Agent.payWithoutRecord");
return false;
}
}
/// <summary>
/// Checks inside. Wouldn't pay if can't. Takes back deposit from bank if needed
Expand Down
35 changes: 19 additions & 16 deletions Assets/EconomicSimulation/Scripts/Logic/Country.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public Money RestIncome
public readonly ModifiersList modMyOpinionOfXCountry;
public static readonly DoubleConditionsList canAttack = new DoubleConditionsList(new List<Condition>
{
new DoubleCondition((province, country)=>(province as Province).isNeighborButNotOwn(country as Country), x=>"Is neighbor province", true),
//new ConditionForDoubleObjects((province, province)=>(province as Province).Country.government.getValue, x=>"Is neighbor province", false),
new DoubleCondition((province, country)=>(province as Province).getAllNeighbors().Any(x => x.Country == country)
&& (province as Province) .Country != country, x=>"Is neighbor province", true),
new DoubleCondition((province, country)=>!Government.isDemocracy.checkIfTrue(country)
|| !Government.isDemocracy.checkIfTrue((province as Province).Country), x=>"Democracies can't attack each other", true),
});
Expand Down Expand Up @@ -568,15 +568,22 @@ override internal void sendArmy(Province target, Procent procent)
// //!province.isBelongsTo(this) &&
// return province.isNeighbor(this);
//}

internal List<Province> getNeighborProvinces()
/// <summary>
/// Has duplicates!
/// </summary>
internal IEnumerable<Province> getAllNeighborProvinces()
{
List<Province> result = new List<Province>();
//var res = Enumerable.Empty<Province>();
foreach (var province in ownedProvinces)
result.AddRange(
province.getAllNeigbors().Where(p => p.Country != this && !result.Contains(p))
);
return result;
foreach (var neighbor in province.getAllNeighbors().Where(p => p.Country != this))
yield return neighbor;

//List<Province> result = new List<Province>();
//foreach (var province in ownedProvinces)
// result.AddRange(
// province.getAllNeighbors().Where(p => p.Country != this && !result.Contains(p))
// );
//return result;
}
private bool isOnlyCountry()
{
Expand All @@ -590,10 +597,6 @@ internal Province getRandomOwnedProvince()
{
return ownedProvinces.Random();
}
internal Province getRandomOwnedProvince(Predicate<Province> predicate)
{
return ownedProvinces.Random(predicate);
}

internal void setCapitalTextMesh(Province province)
{
Expand Down Expand Up @@ -877,7 +880,7 @@ public void AIThink()
if (Game.Random.Next(10) == 1)
{
var thisStrength = this.getStrengthExluding(null);
var possibleTarget = getNeighborProvinces().MinBy(x => getRelationTo(x.Country).get());
var possibleTarget = getAllNeighborProvinces().Distinct().MinBy(x => getRelationTo(x.Country).get());
if (possibleTarget != null
&& (getRelationTo(possibleTarget.Country).get() < 1f || Game.Random.Next(200) == 1)
&& thisStrength > 0
Expand Down Expand Up @@ -1012,7 +1015,7 @@ internal void simulate()

private void aiInvent()
{
var invention = GetAllUninvented().ToList().Random(x => this.sciencePoints.isBiggerOrEqual(x.Key.getCost()));
var invention = GetAllUninvented().Where(x => this.sciencePoints.isBiggerOrEqual(x.Key.getCost())).Random();//.ToList()
if (invention.Key != null)
invent(invention.Key);
}
Expand Down Expand Up @@ -1192,7 +1195,7 @@ public override void consumeNeeds()

void buyNeeds(Storage toBuy)
{
Storage realyBougth = Game.market.buy(this, toBuy, null);
Storage realyBougth = Game.market.Sell(this, toBuy, null);
if (realyBougth.isNotZero())
{
countryStorageSet.Add(realyBougth);
Expand Down
9 changes: 3 additions & 6 deletions Assets/EconomicSimulation/Scripts/Logic/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ namespace Nashet.EconomicSimulation
public class Game : ThreadedJob
{
static private readonly bool readMapFormFile = false;
static private MyTexture mapTexture;
//static public GameObject mapObject;
static private MyTexture mapTexture;
static internal GameObject r3dTextPrefab;

static public Country Player;
Expand All @@ -30,14 +29,12 @@ public class Game : ThreadedJob

static public readonly Market market;


static internal bool devMode = false;
static private int mapMode;
static private bool surrended = devMode;
static internal Material defaultCountryBorderMaterial, defaultProvinceBorderMaterial, selectedProvinceBorderMaterial,
impassableBorder;


static private VoxelGrid grid;
private readonly Rect mapBorders;

Expand Down Expand Up @@ -186,7 +183,7 @@ static void generateMapImage()
//width = 170 + Random.Next(65);
//mapSize = 30000;
//width = 180 + Random.Next(65);
mapSize = 35000;
mapSize = 40000;
width = 250 + Random.Next(40);
}
// 140 is sqrt of 20000
Expand Down Expand Up @@ -369,7 +366,7 @@ internal static void simulate()
}
}
}
// big AFTER all circle
// big AFTER all and get money for sold circle
foreach (Country country in World.getAllExistingCountries())
{
country.getMoneyForSoldProduct();
Expand Down
22 changes: 11 additions & 11 deletions Assets/EconomicSimulation/Scripts/Logic/Market.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ internal ReadOnlyValue getCost(Storage need)
/// <summary>
/// Just transfers it to StorageSet.convertToCheapestStorageProduct(Storage)
/// </summary>
internal Storage getCheapestSubstitute(Storage need)
internal Storage GetRandomCheapestSubstitute(Storage need)
{
return marketPrice.convertToCheapestStorageProduct(need);
return marketPrice.ConvertToRandomCheapestStorageProduct(need);
}
//todo change it to 1 run by every products, not run for every product
private Storage recalculateProductForConsumers(Product product, Func<Consumer, StorageSet> selector)
Expand Down Expand Up @@ -299,7 +299,7 @@ internal Storage buy(Consumer buyer, Storage whatWantedToBuy)
Storage buying;
if (whatWantedToBuy.Product.isAbstract())
{
buying = marketPrice.convertToCheapestExistingSubstitute(whatWantedToBuy);
buying = marketPrice.ConvertToRandomCheapestExistingSubstitute(whatWantedToBuy);
if (buying == null)//no substitution available on market
return new Storage(whatWantedToBuy.Product);
}
Expand Down Expand Up @@ -377,7 +377,7 @@ internal Storage buy(Consumer buyer, Storage whatWantedToBuy)
/// <summary>
/// Buys, returns actually bought, subsidizations allowed, uses deposits if available
/// </summary>
public Storage buy(Consumer forWhom, Storage need, Country subsidizer)
public Storage Sell(Consumer forWhom, Storage need, Country subsidizer)
{
if (forWhom.CanAfford(need) || subsidizer == null)
return buy(forWhom, need);
Expand All @@ -391,12 +391,12 @@ public Storage buy(Consumer forWhom, Storage need, Country subsidizer)
/// <summary>
/// Buying PrimitiveStorageSet, subsidizations allowed
/// </summary>
internal void buy(Consumer buyer, StorageSet buying, Country subsidizer)
{
foreach (Storage item in buying)
if (item.isNotZero())
buy(buyer, item, subsidizer);
}
//internal void SellList(Consumer buyer, StorageSet buying, Country subsidizer)
//{
// foreach (Storage item in buying)
// if (item.isNotZero())
// buy(buyer, item, subsidizer);
//}


/// <summary>
Expand All @@ -415,7 +415,7 @@ internal bool buy(Producer buyer, StorageSet stillHaveToBuy, Procent buyInTime,
// check if consumeOnThisIteration is not bigger than stillHaveToBuy
if (!stillHaveToBuy.has(consumeOnThisIteration))
consumeOnThisIteration = stillHaveToBuy.getBiggestStorage(what.Product);
var reallyBought = buy(buyer, consumeOnThisIteration, null);
var reallyBought = Sell(buyer, consumeOnThisIteration, null);

stillHaveToBuy.Subtract(reallyBought);

Expand Down
6 changes: 3 additions & 3 deletions Assets/EconomicSimulation/Scripts/Logic/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static class Options
internal static readonly Procent minFactoryWorkforceFulfillingToInvest = new Procent(0.70f);

internal static readonly int fabricConstructionTimeWithoutCapitalism = 20;
internal static readonly byte FactoryInputReservInDays = 5;
internal static readonly byte FactoryInputReservInDays = 2;
internal static readonly int FactoryMediumTierLevels = 8 + 1;
internal static readonly int FactoryMediumHighLevels = 15 + 1;
internal static readonly Money FactoryMinPossibleSallary = new Money(0.001f);
Expand Down Expand Up @@ -114,7 +114,7 @@ public static class Options

//ARTISANS
internal static readonly float ArtisansProductionModifier = 0.75f;
internal static readonly Procent ArtisansChangeProductionRate = new Procent(0.20f);
internal static readonly Procent ArtisansChangeProductionRate = new Procent(0.01f);
/// <summary> change production with needs fulfilling lower than that /// </summary>
internal static readonly Value ArtisansChangeProductionLevel = new Value(0.3f);

Expand All @@ -123,7 +123,7 @@ public static class Options
internal static readonly float votingPassBillLimit = 0.5f;
internal static readonly float votingForcedReformPenalty = 0.5f;
internal static readonly int familySize = 5;
internal static readonly Procent savePopMoneyReserv = new Procent(0.66666f);
internal static readonly Procent savePopMoneyReserv = new Procent(0.5f);//0.66666f);
internal static readonly float PopMinLandForTribemen = 1f;
internal static readonly float PopMinLandForFarmers = 0.25f;
internal static readonly float PopMinLandForTownspeople = 0.0025f;
Expand Down
46 changes: 24 additions & 22 deletions Assets/EconomicSimulation/Scripts/Logic/Population/Artisans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ override protected void deleteData()
{
base.deleteData();
artisansProduction = null;
}
}
public override bool canThisPromoteInto(PopType targetType)
{
if (targetType == PopType.Capitalists && Country.Invented(Invention.Manufactures))
Expand All @@ -37,14 +37,12 @@ public override void produce()
artisansProduction = null;
else
{
if (Rand.Chance(Options.ArtisansChangeProductionRate))
// && (artisansProduction==null
//|| (artisansProduction !=null && needsFulfilled.isSmallerThan(Options.ArtisansChangeProductionLevel))))
Rand.Call(() => checkProfit(), 10);// changes production type if needed
if (Rand.Chance(Options.ArtisansChangeProductionRate)) // check if it's best production type so far
changeProductionType();

if (artisansProduction != null)
{
if (artisansProduction.isAllInputProductsCollected())
//if (artisansProduction.isAllInputProductsCollected())
{
artisansProduction.produce();
if (Economy.isMarket.checkIfTrue(Country))
Expand Down Expand Up @@ -74,17 +72,17 @@ public override void consumeNeeds()
PayWithoutRecord(artisansProduction, Cash);

// take loan if don't have enough money to buy inputs
if (Country.Invented(Invention.Banking) && !artisansProduction.isAllInputProductsCollected())
if (artisansProduction.Type.getPossibleProfit().isNotZero())
if (Country.Invented(Invention.Banking) && !artisansProduction.isAllInputProductsCollected()
&& artisansProduction.Type.getPossibleProfit().isNotZero())
{
var needs = artisansProduction.getRealAllNeeds();
if (!artisansProduction.CanAfford(needs))
{
var needs = artisansProduction.getRealAllNeeds();
if (!artisansProduction.CanAfford(needs))
{
var loanSize = Game.market.getCost(needs); // takes little more than really need, could be fixed
Bank.GiveCredit(this, loanSize);
PayWithoutRecord(artisansProduction, Cash);
}
var loanSize = Game.market.getCost(needs); // takes little more than really need, could be fixed
Bank.GiveCredit(this, loanSize);
PayWithoutRecord(artisansProduction, Cash);
}
}

artisansProduction.consumeNeeds();
artisansProduction.PayWithoutRecord(this, artisansProduction.Cash);
Expand Down Expand Up @@ -136,12 +134,15 @@ private void changeProductionType()
{
var newProductionType = ProductionType.getAllInventedArtisanships(Country).
Where(x => !x.isResourceGathering() && x.basicProduction.Product != Product.Education).Where(x => x.getPossibleProfit().isNotZero()).MaxBy(x => x.getPossibleProfit().get());

if (newProductionType != null && (artisansProduction == null || artisansProduction != null && newProductionType != artisansProduction.Type))
{
artisansProduction = new ArtisanProduction(newProductionType, Province, this);
base.changeProductionType(artisansProduction.Type.basicProduction.Product);
}

if (newProductionType != null)
if (artisansProduction == null
|| (artisansProduction != null && newProductionType != artisansProduction.Type))

{
artisansProduction = new ArtisanProduction(newProductionType, Province, this);
base.changeProductionType(artisansProduction.Type.basicProduction.Product);
}
}
public StorageSet getInputProducts()
{
Expand Down Expand Up @@ -170,7 +171,8 @@ public ProductionType Type
internal void checkProfit()
{
// todo doesn't include taxes. Should it?
if (artisansProduction == null || moneyIncomeThisTurn.Copy().subtract(artisansProduction.getExpences()).isZero())
if (artisansProduction == null
|| moneyIncomeThisTurn.Copy().subtract(artisansProduction.getExpences()).isZero())
changeProductionType();
}
}
Expand Down
Loading

0 comments on commit 9ab83a8

Please sign in to comment.