TerrainPainter.cpp 2.7 KB

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