Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CityService
- {
- public CityService()
- {
- }
- public City GenerateCity(int userId, int cityCount)
- {
- int offset = 1;
- int subGridSize = 3;
- Dictionary<Coords, int> coords = GetCoords(cityCount + 1, subGridSize);
- return new City
- {
- UserId = userId,
- PopulationAllocation = ApplicationConstants.CityConstants.DefaultPopulationAllocation,
- Resources = ApplicationConstants.CityConstants.DefaultResources,
- BuildingLevels = ApplicationConstants.CityConstants.DefaultBuildingLevels,
- ResearchLevels = ApplicationConstants.CityConstants.DefaultResearchesLevel,
- TroopAmounts = ApplicationConstants.CityConstants.DefaultTroopAmounts,
- OccupiedMerchants = ApplicationConstants.CityConstants.DefaultOccupiedMerchants,
- XCoordinate = coords[Coords.x] += UnityEngine.Random.Range (-offset, offset),
- YCoordinate = coords[Coords.y] += UnityEngine.Random.Range (-offset, offset),
- LastUpdate = DateTime.Now
- };
- }
- public City UpdateCity(City city)
- {
- DateTime currDate = DateTime.Now;
- float elapsedTime = (float)(currDate - city.LastUpdate).TotalHours;
- float[] populationResult = UpdatePopulation(city, elapsedTime);
- city.Resources[KingResource.Population] = populationResult[0];
- float food = UpdateResource(city, KingResource.Food, elapsedTime, populationResult[1], false);
- bool underPenalty = false;
- if(food < 0)
- {
- underPenalty = true;
- food = 0;
- }
- city.Resources[KingResource.Food] = food;
- foreach(KingResource kingResource in Enum.GetValues(typeof(KingResource)))
- {
- if(kingResource == KingResource.Population || kingResource == KingResource.Food)
- {
- continue;
- }
- city.Resources[kingResource] = UpdateResource(city, kingResource, elapsedTime, populationResult[1], underPenalty);
- }
- city.LastUpdate = currDate;
- return city;
- }
- private float[] UpdatePopulation(City city, float elapsedTime)
- {
- float[] result = new float[2];
- KingResource kingResource = KingResource.Population;
- float value = city.Resources[kingResource];
- KingBuilding productionBuilding = ApplicationConstants.SharedCityConstants.ResourceProduction[kingResource];
- int productionBuildingLevel = city.BuildingLevels[productionBuilding];
- float production = 0;
- if (productionBuildingLevel > 0)
- {
- production =
- ApplicationConstants.SharedCityConstants.HourlyProductionBase[productionBuilding] *
- Mathf.Pow(
- ApplicationConstants.SharedCityConstants.HourlyProductionMultiplier[productionBuilding],
- productionBuildingLevel);
- }
- KingBuilding storageBuilding = ApplicationConstants.SharedCityConstants.ResourceStorage[kingResource];
- int storageBuildingLevel = city.BuildingLevels[storageBuilding];
- float capacity = 0;
- if (storageBuildingLevel > 0)
- {
- capacity =
- ApplicationConstants.SharedCityConstants.CapacityBase[storageBuilding] *
- Mathf.Pow(
- ApplicationConstants.SharedCityConstants.CapacityMultiplier[storageBuilding],
- storageBuildingLevel);
- }
- float oldValue = value;
- value += production * elapsedTime;
- float untilFull = capacity - value;
- float timeUntilFull = untilFull / production;
- float average = 0;
- if (timeUntilFull > elapsedTime)
- {
- average = (timeUntilFull * ((capacity + value) / 2) + (elapsedTime - timeUntilFull) * capacity) / elapsedTime;
- value = capacity;
- }
- else
- {
- average = (value + oldValue) / 2;
- }
- result[0] = value;
- result[1] = average;
- return result;
- }
- private float UpdateResource(City city, KingResource kingResource, float elapsedTime, float averagePopulation, bool underPenalty)
- {
- float value = city.Resources[kingResource];
- KingBuilding productionBuilding = ApplicationConstants.SharedCityConstants.ResourceProduction[kingResource];
- int productionBuildingLevel = city.BuildingLevels[productionBuilding];
- float production = 0;
- if (productionBuildingLevel > 0)
- {
- production =
- ApplicationConstants.SharedCityConstants.HourlyProductionBase[productionBuilding] *
- Mathf.Pow(
- ApplicationConstants.SharedCityConstants.HourlyProductionMultiplier[productionBuilding],
- productionBuildingLevel);
- float researchBonus = 1;
- if(kingResource != KingResource.Science)
- {
- KingResearch kingResearch = ApplicationConstants.SharedCityConstants.ResourceResearch[kingResource];
- int researchLevel = city.ResearchLevels[kingResearch];
- float researchBonusPerLevel = ApplicationConstants.SharedCityConstants.ResearchBonusPerLevel[kingResearch];
- researchBonus += researchLevel * researchBonusPerLevel;
- }
- if (underPenalty)
- {
- production *= researchBonus * ApplicationConstants.SharedCityConstants.ProductionUnderPenalty;
- }
- }
- KingBuilding storageBuilding = ApplicationConstants.SharedCityConstants.ResourceStorage[kingResource];
- int storageBuildingLevel = city.BuildingLevels[storageBuilding];
- float capacity = 0;
- if(storageBuildingLevel > 0)
- {
- capacity =
- ApplicationConstants.SharedCityConstants.CapacityBase[storageBuilding] *
- Mathf.Pow(
- ApplicationConstants.SharedCityConstants.CapacityMultiplier[storageBuilding],
- storageBuildingLevel);
- }
- value += production * elapsedTime;
- if(value > capacity)
- {
- value = capacity;
- }
- return value;
- }
- #region Messages
- public void UpdateAll(NetworkContact networkContact, City city)
- {
- UpdatePopulationAllocation(networkContact, city);
- UpdateResources(networkContact, city);
- UpdateBuildingLevels(networkContact, city);
- UpdateResearchLevels(networkContact, city);
- UpdateTroopAmounts(networkContact, city);
- UpdateGeneric(networkContact, city);
- }
- public void UpdatePopulationAllocation(NetworkContact networkContact , City city)
- {
- int[] resourcesPop = new int[city.PopulationAllocation.Count];
- foreach(KeyValuePair<KingResourcePopulation, int> entry in city.PopulationAllocation)
- {
- resourcesPop[(int)entry.Key] = entry.Value;
- }
- UpdateKingResourcesPopMessage updatePopulationAllocation = new UpdateKingResourcesPopMessage
- {
- ResourcesPop = resourcesPop
- };
- NetworkController.Send(MessageType.UpdateKingResourcesPop, updatePopulationAllocation, networkContact);
- }
- public void UpdateResources(NetworkContact networkContact, City city)
- {
- float[] resources = new float[city.Resources.Count];
- foreach(KeyValuePair<KingResource, float> entry in city.Resources)
- {
- resources[(int)entry.Key] = entry.Value;
- }
- UpdateKingResourcesMessage updateKingResourcesMessage = new UpdateKingResourcesMessage
- {
- Resources = resources
- };
- NetworkController.Send(MessageType.UpdateKingResources, updateKingResourcesMessage, networkContact);
- }
- public void UpdateBuildingLevels(NetworkContact networkContact, City city)
- {
- int[] level = new int[city.BuildingLevels.Count];
- foreach (KeyValuePair<KingBuilding, int> entry in city.BuildingLevels)
- {
- level[(int)entry.Key] = entry.Value;
- }
- UpdateKingBuildingsLevelMessage updateKingBuildingsLevelMessage = new UpdateKingBuildingsLevelMessage
- {
- Levels = level
- };
- NetworkController.Send(MessageType.UpdateKingBuildingsLevel, updateKingBuildingsLevelMessage, networkContact);
- }
- public void UpdateResearchLevels(NetworkContact networkContact, City city)
- {
- int[] researchLevels = new int[city.ResearchLevels.Count];
- foreach (KeyValuePair<KingResearch, int> entry in city.ResearchLevels)
- {
- researchLevels[(int)entry.Key] = entry.Value;
- }
- UpdateKingResearchesLevelMessage updateKingResearchesLevelMessage = new UpdateKingResearchesLevelMessage
- {
- ResearchLevels = researchLevels
- };
- NetworkController.Send(MessageType.UpdateKingResearchesLevel, updateKingResearchesLevelMessage, networkContact);
- }
- public void UpdateTroopAmounts(NetworkContact networkContact, City city)
- {
- int[] troops = new int[city.TroopAmounts.Count];
- foreach (KeyValuePair<KingTroopType, int> entry in city.TroopAmounts)
- {
- troops[(int)entry.Key] = entry.Value;
- }
- UpdateKingTroopsMessage updateKingTroopsMessage = new UpdateKingTroopsMessage
- {
- Troops = troops
- };
- NetworkController.Send(MessageType.UpdateKingTroops, updateKingTroopsMessage, networkContact);
- }
- public void UpdateGeneric(NetworkContact networkContact, City city)
- {
- UpdateKingGenericMessage updateKingGenericMessage = new UpdateKingGenericMessage
- {
- XCoordinate = city.XCoordinate,
- YCoordinate = city.YCoordinate,
- LastUpdate = city.LastUpdate
- };
- NetworkController.Send(MessageType.UpdateKingGeneric, updateKingGenericMessage, networkContact);
- }
- #endregion
- #region AuxFunctions
- private Dictionary<Coords, int> GetCoords(int n, int subGridSize)
- {
- var ret = new Dictionary<Coords, int>();
- var i = 1;
- var j = 1;
- var slot = 1;
- while (slot < n)
- {
- i += 2;
- j++;
- slot = (int)Math.Pow(i, 2);
- }
- if (i == 1)
- {
- ret[Coords.x] = 0;
- ret[Coords.y] = 0;
- }
- else
- {
- var delta2 = n - (Math.Pow(i - 2, 2) + 1);
- var div = (int)Math.Floor(delta2 / (i - 1));
- int x;
- int y;
- int n2 = (int)Math.Pow(i - 2, 2) + 1 + div * (i - 1);
- switch (div)
- {
- case 0:
- x = -subGridSize * (j - 1);
- y = subGridSize * (j - 1);
- while (n2 < n)
- {
- x += subGridSize;
- n2++;
- }
- break;
- case 1:
- x = subGridSize * (j - 1);
- y = subGridSize * (j - 1);
- while (n2 < n)
- {
- y -= subGridSize;
- n2++;
- }
- break;
- case 2:
- x = subGridSize * (j - 1);
- y = -subGridSize * (j - 1);
- while (n2 < n)
- {
- x -= subGridSize;
- n2++;
- }
- break;
- case 3:
- x = -subGridSize * (j - 1);
- y = -subGridSize * (j - 1);
- while (n2 < n)
- {
- y += subGridSize;
- n2++;
- }
- break;
- default:
- Debug.LogError("Shouldn't arrive here at City Service Get Coords");
- return null;
- }
- ret[Coords.x] = x;
- ret[Coords.y] = y;
- }
- return ret;
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement