public void TestBuying() { City city = GenerateCity(); while (!city.buildings.Exists(b => b is FoodMarket)) { city = GenerateCity(); } int money = 200; HumanPlayer player = new HumanPlayer(PlayerType.localHuman, "Test", null, new Player[] { }, null, money); player.troop = new Troop("Test", null, Resources.playerTroop, 0, null, player); int playerItems = player.troop.Items.Count; Assert.IsTrue(player.Money.Value == money); FoodMarket market = city.buildings.Find(b => b is FoodMarket) as FoodMarket; Assert.IsNotNull(market); market.Items[0] = new InventoryItem(new Food("Apple", 1, 2, 1), 20, 2); Assert.IsTrue(market.Items[0].Cost == 2); for (int i = 0; i < 10; i++) { Store.BuyItem(market.Items[0], market.Items, player, market); money -= 2; Assert.IsTrue(player.troop.Items.Count == playerItems + 1); Assert.IsTrue(player.Money.Value == money); Assert.IsTrue(player.Money.ToString() == $"Money: {money} Coins"); } }
void BuyItems() { if (items != null) { return; } items = new List <InventoryItem>(); int moneyPerItem = money / itemTypes.Count; FoodMarket foodMarket = start.GetBuilding <FoodMarket>(); List <InventoryItem> localItems = foodMarket.Items; foreach (string item in itemTypes) { int currentItemMoney = moneyPerItem; int amount = 0; int healAmount = 0; int sellablePrice = end.DetermineLocalFoodPrice(item); List <InventoryItem> enumerable = new List <InventoryItem>(localItems.Where(i => i.item.name == item)); foreach (var i in enumerable) { if (i.Cost < sellablePrice) { int addedAmount = Math.Min(i.Amount, moneyPerItem / i.Cost); if (addedAmount == 0) { continue; } amount += addedAmount; money -= i.Cost * addedAmount; currentItemMoney -= i.Cost * addedAmount; healAmount = (i.item as Food).healAmount; if (i.Amount == addedAmount) { foodMarket.Items.Remove(i); } else { i.Amount -= addedAmount; } foodMarket.money += i.Cost * addedAmount; if (currentItemMoney <= 0) { break; } } } items.Add(new InventoryItem(new Food(item, 1, sellablePrice, healAmount), amount, sellablePrice)); } }
public void TestGenerateGoods() { for (int i = 0; i < 100; i++) { City city = GenerateCity(); for (int j = 0; j < 100; j++) { int value1 = World.random.Next(city.value); System.Collections.Generic.List <InventoryItem> food = FoodMarket.GenerateGoods(value1, city); InventoryItem tooCostly = food.Find(g => g.Cost > 40 || g.Cost <= 0); if (tooCostly != null) { Trace.TraceInformation(tooCostly.ToString()); } Assert.IsNull(tooCostly); } } }
public override void WorldAction() { if (dead) { return; } day++; description = $"The agricultural value of the town is {city.agriculturalProduction} and the mineral value is {city.mineralProduction}. The city requires {FoodRequirement()} food per week and is getting food from {farms.Count}. It has {money} coins."; if (day % 7 == 0) { //Update this in case farms get added or removed Island island = World.Instance.worldMap.Get(city.position).island; farms = World.Instance.nation.farms.Where(f => World.Instance.worldMap.Get(f.position).island == island).OrderBy(f => AIUtility.Distance(city.position, f.position)).ToList(); int foodRequirement = FoodRequirement(); int initialFoodRequirement = foodRequirement; FoodMarket market = city.buildings.Find(f => f is FoodMarket) as FoodMarket; while (foodRequirement != 0) { if (market.Items.Count == 0 || !market.Items.Exists(i => market.GetBuyPrice(i.item) <= money)) { //People are going hungry Trace.TraceInformation($"In {city.name} {foodRequirement} died from hunger - Wealth {money} - Market Items: {market.Items.Sum(i => i.Amount)}" + $" Market Money: {market.money}"); checked { city.Population -= foodRequirement; } break; } List <InventoryItem> foodList = market.Items.OrderBy(r => r.Cost).ToList(); foreach (var food in foodList) { int amount = Math.Min(food.Amount, foodRequirement); int individualPrice = market.GetBuyPrice(food.item); amount = Math.Min(money / individualPrice, amount); int price = individualPrice * amount; if (money >= price) { foodRequirement -= amount; money -= price; market.money += price; food.Amount -= amount; if (food.Amount == 0) { market.Items.Remove(food); } } if (foodRequirement == 0) { break; } } } } if (day % 365 == 0) { money += city.Population * 2; Trace.TraceInformation($"City: {city.name} Wealth: {money}"); if (city.Population < city.lastPopulation || (city.buildings.Find(f => f is FoodMarket) as FoodMarket).Items.Count == 0) { //Find close tile and build new farm WorldTile tile = null; double maxAgri = 0; for (int x = 0; x < 11; x++) { for (int y = 0; y < 11; y++) { WorldTile worldTile = World.Instance.worldMap[(city.position.X - 5 + x).Cut(0, World.WORLD_SIZE - 1), (city.position.Y - 5 + y).Cut(0, World.WORLD_SIZE - 1)]; double agriculture = World.Instance.agriculturalMap[(city.position.X - 5 + x).Cut(0, World.WORLD_SIZE - 1), (city.position.Y - 5 + y).Cut(0, World.WORLD_SIZE - 1)]; if (maxAgri < agriculture) { tile = worldTile; maxAgri = agriculture; } } } if (tile != null && maxAgri > 0) { Trace.TraceInformation($"{city.name} created a new farm at {tile.position}"); World.Instance.nation.toChange.Add(new Farm(tile.position, (int)(maxAgri * 100))); } } } if (city.Population < 100) { Trace.TraceInformation($"The city {city.name} is dead!"); dead = true; //The city is dead World.Instance.nation.toChange.Add(city); } //Trace.TraceInformation($"{city.name} on {World.Instance.worldMap.Get(city.position).island.Name}: {initialFoodRequirement} - Hunger deaths: {foodRequirement}"); }