TerrainPainter.cpp 2.8 KB

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