Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.28 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class CityService
  7. {
  8. public CityService()
  9. {
  10.  
  11. }
  12.  
  13. public City GenerateCity(int userId, int cityCount)
  14. {
  15. int offset = 1;
  16. int subGridSize = 3;
  17.  
  18. Dictionary<Coords, int> coords = GetCoords(cityCount + 1, subGridSize);
  19.  
  20. return new City
  21. {
  22. UserId = userId,
  23. PopulationAllocation = ApplicationConstants.CityConstants.DefaultPopulationAllocation,
  24. Resources = ApplicationConstants.CityConstants.DefaultResources,
  25. BuildingLevels = ApplicationConstants.CityConstants.DefaultBuildingLevels,
  26. ResearchLevels = ApplicationConstants.CityConstants.DefaultResearchesLevel,
  27. TroopAmounts = ApplicationConstants.CityConstants.DefaultTroopAmounts,
  28. OccupiedMerchants = ApplicationConstants.CityConstants.DefaultOccupiedMerchants,
  29. XCoordinate = coords[Coords.x] += UnityEngine.Random.Range (-offset, offset),
  30. YCoordinate = coords[Coords.y] += UnityEngine.Random.Range (-offset, offset),
  31. LastUpdate = DateTime.Now
  32. };
  33. }
  34.  
  35. public City UpdateCity(City city)
  36. {
  37. DateTime currDate = DateTime.Now;
  38. float elapsedTime = (float)(currDate - city.LastUpdate).TotalHours;
  39.  
  40. float[] populationResult = UpdatePopulation(city, elapsedTime);
  41. city.Resources[KingResource.Population] = populationResult[0];
  42.  
  43. float food = UpdateResource(city, KingResource.Food, elapsedTime, populationResult[1], false);
  44. bool underPenalty = false;
  45. if(food < 0)
  46. {
  47. underPenalty = true;
  48. food = 0;
  49. }
  50. city.Resources[KingResource.Food] = food;
  51.  
  52. foreach(KingResource kingResource in Enum.GetValues(typeof(KingResource)))
  53. {
  54. if(kingResource == KingResource.Population || kingResource == KingResource.Food)
  55. {
  56. continue;
  57. }
  58.  
  59. city.Resources[kingResource] = UpdateResource(city, kingResource, elapsedTime, populationResult[1], underPenalty);
  60. }
  61.  
  62. city.LastUpdate = currDate;
  63.  
  64. return city;
  65. }
  66.  
  67. private float[] UpdatePopulation(City city, float elapsedTime)
  68. {
  69. float[] result = new float[2];
  70.  
  71. KingResource kingResource = KingResource.Population;
  72. float value = city.Resources[kingResource];
  73.  
  74. KingBuilding productionBuilding = ApplicationConstants.SharedCityConstants.ResourceProduction[kingResource];
  75. int productionBuildingLevel = city.BuildingLevels[productionBuilding];
  76. float production = 0;
  77. if (productionBuildingLevel > 0)
  78. {
  79. production =
  80. ApplicationConstants.SharedCityConstants.HourlyProductionBase[productionBuilding] *
  81. Mathf.Pow(
  82. ApplicationConstants.SharedCityConstants.HourlyProductionMultiplier[productionBuilding],
  83. productionBuildingLevel);
  84. }
  85.  
  86. KingBuilding storageBuilding = ApplicationConstants.SharedCityConstants.ResourceStorage[kingResource];
  87. int storageBuildingLevel = city.BuildingLevels[storageBuilding];
  88. float capacity = 0;
  89. if (storageBuildingLevel > 0)
  90. {
  91. capacity =
  92. ApplicationConstants.SharedCityConstants.CapacityBase[storageBuilding] *
  93. Mathf.Pow(
  94. ApplicationConstants.SharedCityConstants.CapacityMultiplier[storageBuilding],
  95. storageBuildingLevel);
  96. }
  97.  
  98. float oldValue = value;
  99. value += production * elapsedTime;
  100.  
  101. float untilFull = capacity - value;
  102. float timeUntilFull = untilFull / production;
  103. float average = 0;
  104.  
  105. if (timeUntilFull > elapsedTime)
  106. {
  107. average = (timeUntilFull * ((capacity + value) / 2) + (elapsedTime - timeUntilFull) * capacity) / elapsedTime;
  108. value = capacity;
  109. }
  110. else
  111. {
  112. average = (value + oldValue) / 2;
  113. }
  114.  
  115. result[0] = value;
  116. result[1] = average;
  117.  
  118. return result;
  119. }
  120.  
  121. private float UpdateResource(City city, KingResource kingResource, float elapsedTime, float averagePopulation, bool underPenalty)
  122. {
  123. float value = city.Resources[kingResource];
  124.  
  125. KingBuilding productionBuilding = ApplicationConstants.SharedCityConstants.ResourceProduction[kingResource];
  126. int productionBuildingLevel = city.BuildingLevels[productionBuilding];
  127. float production = 0;
  128. if (productionBuildingLevel > 0)
  129. {
  130. production =
  131. ApplicationConstants.SharedCityConstants.HourlyProductionBase[productionBuilding] *
  132. Mathf.Pow(
  133. ApplicationConstants.SharedCityConstants.HourlyProductionMultiplier[productionBuilding],
  134. productionBuildingLevel);
  135.  
  136. float researchBonus = 1;
  137. if(kingResource != KingResource.Science)
  138. {
  139. KingResearch kingResearch = ApplicationConstants.SharedCityConstants.ResourceResearch[kingResource];
  140. int researchLevel = city.ResearchLevels[kingResearch];
  141. float researchBonusPerLevel = ApplicationConstants.SharedCityConstants.ResearchBonusPerLevel[kingResearch];
  142. researchBonus += researchLevel * researchBonusPerLevel;
  143. }
  144.  
  145. if (underPenalty)
  146. {
  147. production *= researchBonus * ApplicationConstants.SharedCityConstants.ProductionUnderPenalty;
  148. }
  149. }
  150.  
  151. KingBuilding storageBuilding = ApplicationConstants.SharedCityConstants.ResourceStorage[kingResource];
  152. int storageBuildingLevel = city.BuildingLevels[storageBuilding];
  153. float capacity = 0;
  154. if(storageBuildingLevel > 0)
  155. {
  156. capacity =
  157. ApplicationConstants.SharedCityConstants.CapacityBase[storageBuilding] *
  158. Mathf.Pow(
  159. ApplicationConstants.SharedCityConstants.CapacityMultiplier[storageBuilding],
  160. storageBuildingLevel);
  161. }
  162.  
  163. value += production * elapsedTime;
  164.  
  165. if(value > capacity)
  166. {
  167. value = capacity;
  168. }
  169.  
  170. return value;
  171. }
  172.  
  173. #region Messages
  174. public void UpdateAll(NetworkContact networkContact, City city)
  175. {
  176. UpdatePopulationAllocation(networkContact, city);
  177. UpdateResources(networkContact, city);
  178. UpdateBuildingLevels(networkContact, city);
  179. UpdateResearchLevels(networkContact, city);
  180. UpdateTroopAmounts(networkContact, city);
  181. UpdateGeneric(networkContact, city);
  182. }
  183.  
  184. public void UpdatePopulationAllocation(NetworkContact networkContact , City city)
  185. {
  186. int[] resourcesPop = new int[city.PopulationAllocation.Count];
  187. foreach(KeyValuePair<KingResourcePopulation, int> entry in city.PopulationAllocation)
  188. {
  189. resourcesPop[(int)entry.Key] = entry.Value;
  190. }
  191.  
  192. UpdateKingResourcesPopMessage updatePopulationAllocation = new UpdateKingResourcesPopMessage
  193. {
  194. ResourcesPop = resourcesPop
  195. };
  196. NetworkController.Send(MessageType.UpdateKingResourcesPop, updatePopulationAllocation, networkContact);
  197. }
  198.  
  199. public void UpdateResources(NetworkContact networkContact, City city)
  200. {
  201. float[] resources = new float[city.Resources.Count];
  202. foreach(KeyValuePair<KingResource, float> entry in city.Resources)
  203. {
  204. resources[(int)entry.Key] = entry.Value;
  205. }
  206.  
  207. UpdateKingResourcesMessage updateKingResourcesMessage = new UpdateKingResourcesMessage
  208. {
  209. Resources = resources
  210. };
  211. NetworkController.Send(MessageType.UpdateKingResources, updateKingResourcesMessage, networkContact);
  212. }
  213.  
  214. public void UpdateBuildingLevels(NetworkContact networkContact, City city)
  215. {
  216. int[] level = new int[city.BuildingLevels.Count];
  217. foreach (KeyValuePair<KingBuilding, int> entry in city.BuildingLevels)
  218. {
  219. level[(int)entry.Key] = entry.Value;
  220. }
  221.  
  222. UpdateKingBuildingsLevelMessage updateKingBuildingsLevelMessage = new UpdateKingBuildingsLevelMessage
  223. {
  224. Levels = level
  225. };
  226. NetworkController.Send(MessageType.UpdateKingBuildingsLevel, updateKingBuildingsLevelMessage, networkContact);
  227. }
  228.  
  229. public void UpdateResearchLevels(NetworkContact networkContact, City city)
  230. {
  231. int[] researchLevels = new int[city.ResearchLevels.Count];
  232. foreach (KeyValuePair<KingResearch, int> entry in city.ResearchLevels)
  233. {
  234. researchLevels[(int)entry.Key] = entry.Value;
  235. }
  236.  
  237. UpdateKingResearchesLevelMessage updateKingResearchesLevelMessage = new UpdateKingResearchesLevelMessage
  238. {
  239. ResearchLevels = researchLevels
  240. };
  241. NetworkController.Send(MessageType.UpdateKingResearchesLevel, updateKingResearchesLevelMessage, networkContact);
  242. }
  243.  
  244. public void UpdateTroopAmounts(NetworkContact networkContact, City city)
  245. {
  246. int[] troops = new int[city.TroopAmounts.Count];
  247. foreach (KeyValuePair<KingTroopType, int> entry in city.TroopAmounts)
  248. {
  249. troops[(int)entry.Key] = entry.Value;
  250. }
  251.  
  252. UpdateKingTroopsMessage updateKingTroopsMessage = new UpdateKingTroopsMessage
  253. {
  254. Troops = troops
  255. };
  256. NetworkController.Send(MessageType.UpdateKingTroops, updateKingTroopsMessage, networkContact);
  257. }
  258.  
  259. public void UpdateGeneric(NetworkContact networkContact, City city)
  260. {
  261. UpdateKingGenericMessage updateKingGenericMessage = new UpdateKingGenericMessage
  262. {
  263. XCoordinate = city.XCoordinate,
  264. YCoordinate = city.YCoordinate,
  265. LastUpdate = city.LastUpdate
  266. };
  267.  
  268. NetworkController.Send(MessageType.UpdateKingGeneric, updateKingGenericMessage, networkContact);
  269. }
  270. #endregion
  271.  
  272. #region AuxFunctions
  273. private Dictionary<Coords, int> GetCoords(int n, int subGridSize)
  274. {
  275. var ret = new Dictionary<Coords, int>();
  276.  
  277. var i = 1;
  278. var j = 1;
  279. var slot = 1;
  280.  
  281. while (slot < n)
  282. {
  283. i += 2;
  284. j++;
  285. slot = (int)Math.Pow(i, 2);
  286. }
  287.  
  288. if (i == 1)
  289. {
  290. ret[Coords.x] = 0;
  291. ret[Coords.y] = 0;
  292. }
  293. else
  294. {
  295. var delta2 = n - (Math.Pow(i - 2, 2) + 1);
  296. var div = (int)Math.Floor(delta2 / (i - 1));
  297. int x;
  298. int y;
  299. int n2 = (int)Math.Pow(i - 2, 2) + 1 + div * (i - 1);
  300. switch (div)
  301. {
  302. case 0:
  303. x = -subGridSize * (j - 1);
  304. y = subGridSize * (j - 1);
  305. while (n2 < n)
  306. {
  307. x += subGridSize;
  308. n2++;
  309. }
  310. break;
  311. case 1:
  312. x = subGridSize * (j - 1);
  313. y = subGridSize * (j - 1);
  314. while (n2 < n)
  315. {
  316. y -= subGridSize;
  317. n2++;
  318. }
  319. break;
  320. case 2:
  321. x = subGridSize * (j - 1);
  322. y = -subGridSize * (j - 1);
  323. while (n2 < n)
  324. {
  325. x -= subGridSize;
  326. n2++;
  327. }
  328. break;
  329. case 3:
  330. x = -subGridSize * (j - 1);
  331. y = -subGridSize * (j - 1);
  332. while (n2 < n)
  333. {
  334. y += subGridSize;
  335. n2++;
  336. }
  337. break;
  338. default:
  339. Debug.LogError("Shouldn't arrive here at City Service Get Coords");
  340. return null;
  341. }
  342.  
  343. ret[Coords.x] = x;
  344. ret[Coords.y] = y;
  345. }
  346.  
  347. return ret;
  348. }
  349. #endregion
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement