TerrainPainter.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * TerrainPainter.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "TerrainPainter.h"
  12. #include "TownPlacer.h"
  13. #include "WaterAdopter.h"
  14. #include "WaterProxy.h"
  15. #include "ConnectionsPlacer.h"
  16. #include "ObjectManager.h"
  17. #include "../Functions.h"
  18. #include "../CMapGenerator.h"
  19. #include "../RmgMap.h"
  20. #include "../../VCMI_Lib.h"
  21. #include "../../TerrainHandler.h"
  22. #include "../../CTownHandler.h"
  23. #include <vstd/RNG.h>
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. void TerrainPainter::process()
  26. {
  27. initTerrainType();
  28. auto v = zone.area()->getTilesVector();
  29. mapProxy->drawTerrain(zone.getRand(), v, zone.getTerrainType());
  30. }
  31. void TerrainPainter::init()
  32. {
  33. DEPENDENCY(TownPlacer);
  34. DEPENDENCY_ALL(WaterAdopter);
  35. POSTFUNCTION_ALL(WaterProxy);
  36. POSTFUNCTION_ALL(ConnectionsPlacer);
  37. POSTFUNCTION(ObjectManager);
  38. }
  39. void TerrainPainter::initTerrainType()
  40. {
  41. if(zone.getType()==ETemplateZoneType::WATER)
  42. {
  43. //collect all water terrain types
  44. std::vector<TerrainId> waterTerrains;
  45. for(const auto & terrain : VLC->terrainTypeHandler->objects)
  46. if(terrain->isWater())
  47. waterTerrains.push_back(terrain->getId());
  48. zone.setTerrainType(*RandomGeneratorUtil::nextItem(waterTerrains, zone.getRand()));
  49. }
  50. else
  51. {
  52. if(zone.isMatchTerrainToTown() && zone.getTownType() != ETownType::NEUTRAL)
  53. {
  54. auto terrainType = (*VLC->townh)[zone.getTownType()]->nativeTerrain;
  55. if (terrainType <= ETerrainId::NONE)
  56. {
  57. logGlobal->warn("Town %s has invalid terrain type: %d", zone.getTownType(), terrainType);
  58. zone.setTerrainType(ETerrainId::DIRT);
  59. }
  60. else
  61. {
  62. zone.setTerrainType(terrainType);
  63. }
  64. }
  65. else
  66. {
  67. auto terrainTypes = zone.getTerrainTypes();
  68. if (terrainTypes.empty())
  69. {
  70. //Fill with all terain types by default
  71. {
  72. for (const auto & terrain : VLC->terrainTypeHandler->objects)
  73. {
  74. if (terrain->isLand() && terrain->isPassable())
  75. {
  76. if ((terrain->isSurface() && !zone.isUnderground()) ||
  77. (terrain->isUnderground() && zone.isUnderground()))
  78. {
  79. terrainTypes.insert(TerrainId(terrain->getId()));
  80. }
  81. }
  82. }
  83. }
  84. }
  85. zone.setTerrainType(*RandomGeneratorUtil::nextItem(terrainTypes, zone.getRand()));
  86. }
  87. //Now, replace disallowed terrains on surface and in the underground
  88. const auto & terrainType = VLC->terrainTypeHandler->getById(zone.getTerrainType());
  89. if(zone.isUnderground())
  90. {
  91. if(!terrainType->isUnderground())
  92. {
  93. zone.setTerrainType(ETerrainId::SUBTERRANEAN);
  94. }
  95. }
  96. else
  97. {
  98. if (!terrainType->isSurface())
  99. {
  100. zone.setTerrainType(ETerrainId::DIRT);
  101. }
  102. }
  103. }
  104. }
  105. VCMI_LIB_NAMESPACE_END