/// <summary> /// Add all resources together. /// </summary> /// <param name="planet"> /// The planet. /// </param> /// <param name="value"> /// The value. /// </param> public static void Add(this PlanetValue planet, PlanetValue value) { planet.Add(value as NetValue); planet.CashFactoryCash += value.CashFactoryCash; planet.PopulationCash += value.PopulationCash; planet.TaxOfficeCash += value.TaxOfficeCash; }
/// <summary> /// The main update that happens each tick in the game /// 1. Explore /// 2. Update fleet position/run battle /// 3. Update planets /// </summary> public void Update() { var planetsToUpdate = this.planetRepository.All.Where(p => p.Owner != null).GroupBy(p => p.Owner.ID); planetsToUpdate .AsParallel() .ForAll(planetSet => { var user = this.playerRepository.Get(planetSet.Key); if (user == null) { foreach (var planet in planetSet) { planet.Owner = null; } return; } var netTotalValue = new PlanetValue(); var galaxySettings = user.Galaxy.GalaxySettings; foreach (var planet in planetSet) { var netValue = planet.Update(galaxySettings, user.Bonuses); netTotalValue.Add(netValue); this.planetRepository.Update(planet); } user.Update(netTotalValue, galaxySettings); }); this.playerRepository.SaveChanges(); }
/// <summary> /// Updates the player's resources each tick - This uses the calculations generated from all planets. /// </summary> /// <param name="player">The player to update.</param> /// <param name="totalPlanetValue">The calculated values from all planets.</param> /// <param name="settings">Settings used for this galaxy.</param> public static void Update(this Player player, PlanetValue totalPlanetValue, GalaxySettings settings) { var tickValue = player.TickValue; // Decay var decay = settings.Decay; tickValue.DecayedCash = player.TotalNetValue.Cash * decay; tickValue.DecayedEnergy = player.TotalNetValue.Energy * decay; tickValue.DecayedFood = player.TotalNetValue.Food * decay; tickValue.DecayedIron = player.TotalNetValue.Iron * decay; tickValue.DecayedMana = player.TotalNetValue.Mana * decay; // Produced tickValue.Buildings = totalPlanetValue.EntityCount; tickValue.Units = player.UnitCount; tickValue.ProducedCashFactoryCash = totalPlanetValue.CashFactoryCash; tickValue.ProducedPopulationCash = totalPlanetValue.PopulationCash; tickValue.ProducedTaxOfficeCash = totalPlanetValue.TaxOfficeCash; tickValue.ProducedCash = totalPlanetValue.Cash; tickValue.ProducedEnergy = totalPlanetValue.Energy; tickValue.ProducedFood = totalPlanetValue.Food; tickValue.ProducedIron = totalPlanetValue.Iron; tickValue.ProducedPopulation = totalPlanetValue.Population; tickValue.ProducedResearch = totalPlanetValue.Research; // Decay existing values from last tick player.TotalNetValue.Cash -= tickValue.DecayedCash; player.TotalNetValue.Energy -= tickValue.DecayedEnergy; player.TotalNetValue.Food -= tickValue.DecayedFood; player.TotalNetValue.Iron -= tickValue.DecayedIron; player.TotalNetValue.Mana -= tickValue.DecayedMana; // Add new values player.TotalNetValue.Add(totalPlanetValue); player.TotalNetValue.Population = totalPlanetValue.Population; // let them eat cake! -- but not so much cake that it goes below zero... player.TotalNetValue.Food -= (player.TotalNetValue.Population / 10.0) + player.UnitCount; if (player.TotalNetValue.Food < 0) { player.TotalNetValue.Food = 0; player.TotalNetValue.Cash -= totalPlanetValue.Cash / 2; tickValue.IsPopulationStarving = true; } // building maintainance & unit upkeep player.TotalNetValue.Cash = Math.Max(0, player.TotalNetValue.Cash - (player.TotalNetValue.EntityCount + player.UnitCount)); // TODO - allocate the research points... }
public void WillUpdateThePlayerTickUnits() { var player = new Player { UnitCount = 1, TickValue = new TickValue(), TotalNetValue = new NetValue() }; var totalPlanetValue = new PlanetValue(); player.Update(totalPlanetValue, new GalaxySettings()); Assert.AreEqual(player.TickValue.Units, 1); }
public void WillUpdateThePlayerTickResearch() { var player = new Player { TickValue = new TickValue(), TotalNetValue = new NetValue() }; var totalPlanetValue = new PlanetValue { Research = 1 }; player.Update(totalPlanetValue, new GalaxySettings()); Assert.AreEqual(player.TickValue.ProducedResearch, 1); }
public void WillUpdateThePlayerTickPopulation() { var player = new Player { TickValue = new TickValue(), TotalNetValue = new NetValue() }; var totalPlanetValue = new PlanetValue { Population = 1 }; player.Update(totalPlanetValue, new GalaxySettings()); Assert.AreEqual(player.TickValue.ProducedPopulation, 1); }
public void WillUpdateThePlayerTickBuildings() { var player = new Player { TickValue = new TickValue(), TotalNetValue = new NetValue() }; var totalPlanetValue = new PlanetValue { EntityCount = 1 }; player.Update(totalPlanetValue, new GalaxySettings()); Assert.AreEqual(player.TickValue.Buildings, 1); }
/// <summary> /// Update a planet and determine population for tick. /// </summary> /// <param name="planet">The planet to update.</param> /// <param name="settings">Settings for the current Galaxy.</param> /// <param name="bonuses">Bonuses for the player based on their race.</param> /// <returns>The calculated values from the planet.</returns> public static PlanetValue Update(this Planet planet, GalaxySettings settings, PlayerBonuses bonuses) { var output = new PlanetValue(); output.EntityCount = planet.TotalBuildings; // Set the base population if (Math.Abs(planet.Population - 0) < settings.BasePopulation) { planet.Population = settings.BasePopulation; } // multiply by 0.01 to convert to percentage var min1 = planet.Population * (1 + (settings.PopulationGrowth * bonuses.WelfareBonus * 0.01)); var min2 = settings.BasePopulation + (settings.MaxPopulationPerBuildings * planet.BuildingCapacity) + (planet.LivingQuartersCount * settings.PeoplePerLivingQuarter * bonuses.WelfareBonus); // cap the population output.Population = Math.Min(min1, min2); planet.PopulationGrowth = output.Population - planet.Population; planet.Population = output.Population; // cash calculations var cashBonus = bonuses.EconomyBonus * planet.CashBonus; // so the user always makes cash var positiveIncome = settings.PositiveIncomeCash * cashBonus; var buildingCount = Math.Max(output.EntityCount, 1); output.CashFactoryCash = planet.CashFactoryCount * settings.CashOutput * cashBonus; output.PopulationCash = planet.Population / settings.PopulationCashDivider * cashBonus; output.TaxOfficeCash = (double)planet.TaxOfficeCount / buildingCount * (positiveIncome + output.CashFactoryCash + output.PopulationCash); output.Cash = positiveIncome + output.CashFactoryCash + output.PopulationCash + output.TaxOfficeCash; output.Energy = planet.EnergyLabCount * planet.EnergyBonus; output.Food = settings.FoodOutput * planet.FarmCount * planet.FoodBonus; output.Iron = planet.MineCount * planet.IronBonus; output.Mana = planet.ManaCount * planet.ManaBonus; output.Research = settings.ResearchOutput * planet.ResearchLabCount * planet.ResearchBonus * bonuses.ResearchBonus; return(output); }
public void WillAddPlanetValue() { var nv1 = new PlanetValue { Cash = 1, Energy = 1, Food = 1, Iron = 1, Mana = 1, Population = 1, Research = 1, EntityCount = 1, CashFactoryCash = 1, PopulationCash = 1, TaxOfficeCash = 1, }; var nv2 = new PlanetValue { Cash = 1, Energy = 1, Food = 1, Iron = 1, Mana = 1, Population = 1, Research = 1, EntityCount = 1, CashFactoryCash = 1, PopulationCash = 1, TaxOfficeCash = 1, }; nv1.Add(nv2); Assert.AreEqual(nv1.Cash, 2, "Cash"); Assert.AreEqual(nv1.Energy, 2, "Energy"); Assert.AreEqual(nv1.Food, 2, "Food"); Assert.AreEqual(nv1.Iron, 2, "Iron"); Assert.AreEqual(nv1.Mana, 2, "Mana"); Assert.AreEqual(nv1.Population, 2, "Population"); Assert.AreEqual(nv1.Research, 2, "Research"); Assert.AreEqual(nv1.EntityCount, 2, "EntityCount"); Assert.AreEqual(nv1.CashFactoryCash, 2, "CashFactoryCash"); Assert.AreEqual(nv1.PopulationCash, 2, "PopulationCash"); Assert.AreEqual(nv1.TaxOfficeCash, 2, "TaxOfficeCash"); }