TerrainPainter.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. VCMI_LIB_NAMESPACE_BEGIN
  23. void TerrainPainter::process()
  24. {
  25. initTerrainType();
  26. auto v = zone.getArea().getTilesVector();
  27. mapProxy->drawTerrain(zone.getRand(), v, zone.getTerrainType());
  28. }
  29. void TerrainPainter::init()
  30. {
  31. DEPENDENCY(TownPlacer);
  32. DEPENDENCY_ALL(WaterAdopter);
  33. POSTFUNCTION_ALL(WaterProxy);
  34. POSTFUNCTION_ALL(ConnectionsPlacer);
  35. POSTFUNCTION(ObjectManager);
  36. }
  37. void TerrainPainter::initTerrainType()
  38. {
  39. if(zone.getType()==ETemplateZoneType::WATER)
  40. {
  41. //collect all water terrain types
  42. std::vector<TerrainId> waterTerrains;
  43. for(const auto & terrain : VLC->terrainTypeHandler->objects)
  44. if(terrain->isWater())
  45. waterTerrains.push_back(terrain->getId());
  46. zone.setTerrainType(*RandomGeneratorUtil::nextItem(waterTerrains, zone.getRand()));
  47. }
  48. else
  49. {
  50. if(zone.isMatchTerrainToTown() && zone.getTownType() != ETownType::NEUTRAL)
  51. {
  52. auto terrainType = (*VLC->townh)[zone.getTownType()]->nativeTerrain;
  53. if (terrainType <= ETerrainId::NONE)
  54. {
  55. logGlobal->warn("Town %s has invalid terrain type: %d", zone.getTownType(), terrainType);
  56. zone.setTerrainType(ETerrainId::DIRT);
  57. }
  58. else
  59. {
  60. zone.setTerrainType(terrainType);
  61. }
  62. }
  63. else
  64. {
  65. auto terrainTypes = zone.getTerrainTypes();
  66. if (terrainTypes.empty())
  67. {
  68. //Fill with all terain types by default
  69. {
  70. for (auto terrain : VLC->terrainTypeHandler->objects)
  71. {
  72. if (terrain->isLand() && terrain->isPassable())
  73. {
  74. if ((terrain->isSurface() && !zone.isUnderground()) ||
  75. (terrain->isUnderground() && zone.isUnderground()))
  76. {
  77. terrainTypes.insert(TerrainId(terrain->getId()));
  78. }
  79. }
  80. }
  81. }
  82. }
  83. zone.setTerrainType(*RandomGeneratorUtil::nextItem(terrainTypes, zone.getRand()));
  84. }
  85. //Now, replace disallowed terrains on surface and in the underground
  86. const auto & terrainType = VLC->terrainTypeHandler->getById(zone.getTerrainType());
  87. if(zone.isUnderground())
  88. {
  89. if(!terrainType->isUnderground())
  90. {
  91. zone.setTerrainType(ETerrainId::SUBTERRANEAN);
  92. }
  93. }
  94. else
  95. {
  96. if (!terrainType->isSurface())
  97. {
  98. zone.setTerrainType(ETerrainId::DIRT);
  99. }
  100. }
  101. }
  102. }
  103. VCMI_LIB_NAMESPACE_END