ObstacleProxy.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * ObstacleProxy.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 "ObstacleProxy.h"
  11. #include "../mapObjects/CObjectClassesHandler.h"
  12. #include "../mapObjects/CObjectHandler.h"
  13. #include "../mapping/CMap.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. void ObstacleProxy::collectPossibleObstacles(TerrainId terrain)
  16. {
  17. //get all possible obstacles for this terrain
  18. for(auto primaryID : VLC->objtypeh->knownObjects())
  19. {
  20. for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
  21. {
  22. auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
  23. if(handler->isStaticObject())
  24. {
  25. for(const auto & temp : handler->getTemplates())
  26. {
  27. if(temp->canBePlacedAt(terrain) && temp->getBlockMapOffset().valid())
  28. obstaclesBySize[temp->getBlockedOffsets().size()].push_back(temp);
  29. }
  30. }
  31. }
  32. }
  33. for(const auto & o : obstaclesBySize)
  34. {
  35. possibleObstacles.emplace_back(o);
  36. }
  37. boost::sort(possibleObstacles, [](const ObstaclePair &p1, const ObstaclePair &p2) -> bool
  38. {
  39. return p1.first > p2.first; //bigger obstacles first
  40. });
  41. }
  42. void ObstacleProxy::addBlockedTile(const int3& tile)
  43. {
  44. blockedArea.add(tile);
  45. }
  46. void ObstacleProxy::setBlockedArea(const rmg::Area& area)
  47. {
  48. blockedArea = area;
  49. }
  50. void ObstacleProxy::clearBlockedArea()
  51. {
  52. blockedArea.clear();
  53. }
  54. bool ObstacleProxy::isProhibited(const rmg::Area& objArea) const
  55. {
  56. return false;
  57. };
  58. int ObstacleProxy::getWeightedObjects(const int3 & tile, CRandomGenerator & rand, std::list<rmg::Object> & allObjects, std::vector<std::pair<rmg::Object*, int3>> & weightedObjects)
  59. {
  60. int maxWeight = std::numeric_limits<int>::min();
  61. for(auto & possibleObstacle : possibleObstacles)
  62. {
  63. if(!possibleObstacle.first)
  64. continue;
  65. auto shuffledObstacles = possibleObstacle.second;
  66. RandomGeneratorUtil::randomShuffle(shuffledObstacles, rand);
  67. for(const auto & temp : shuffledObstacles)
  68. {
  69. auto handler = VLC->objtypeh->getHandlerFor(temp->id, temp->subid);
  70. auto * obj = handler->create(temp);
  71. allObjects.emplace_back(*obj);
  72. rmg::Object * rmgObject = &allObjects.back();
  73. for(const auto & offset : obj->getBlockedOffsets())
  74. {
  75. rmgObject->setPosition(tile - offset);
  76. if(!isInTheMap(rmgObject->getPosition()))
  77. continue;
  78. if(!rmgObject->getArea().getSubarea([this](const int3 & t)
  79. {
  80. return !isInTheMap(t);
  81. }).empty())
  82. continue;
  83. if(isProhibited(rmgObject->getArea()))
  84. continue;
  85. int coverageBlocked = 0;
  86. int coveragePossible = 0;
  87. //do not use area intersection in optimization purposes
  88. for(const auto & t : rmgObject->getArea().getTilesVector())
  89. {
  90. auto coverage = verifyCoverage(t);
  91. if(coverage.first)
  92. ++coverageBlocked;
  93. if(coverage.second)
  94. ++coveragePossible;
  95. }
  96. int coverageOverlap = possibleObstacle.first - coverageBlocked - coveragePossible;
  97. int weight = possibleObstacle.first + coverageBlocked - coverageOverlap * possibleObstacle.first;
  98. assert(coverageOverlap >= 0);
  99. if(weight > maxWeight)
  100. {
  101. weightedObjects.clear();
  102. maxWeight = weight;
  103. weightedObjects.emplace_back(rmgObject, rmgObject->getPosition());
  104. if(weight > 0)
  105. break;
  106. }
  107. else if(weight == maxWeight)
  108. weightedObjects.emplace_back(rmgObject, rmgObject->getPosition());
  109. }
  110. }
  111. if(maxWeight > 0)
  112. break;
  113. }
  114. return maxWeight;
  115. }
  116. std::set<CGObjectInstance*> ObstacleProxy::createObstacles(CRandomGenerator & rand)
  117. {
  118. //reverse order, since obstacles begin in bottom-right corner, while the map coordinates begin in top-left
  119. auto blockedTiles = blockedArea.getTilesVector();
  120. int tilePos = 0;
  121. std::set<CGObjectInstance*> objs;
  122. while(!blockedArea.empty() && tilePos < blockedArea.getTilesVector().size())
  123. {
  124. auto tile = blockedArea.getTilesVector()[tilePos];
  125. std::list<rmg::Object> allObjects;
  126. std::vector<std::pair<rmg::Object*, int3>> weightedObjects;
  127. int maxWeight = getWeightedObjects(tile, rand, allObjects, weightedObjects);
  128. if(weightedObjects.empty())
  129. {
  130. tilePos += 1;
  131. continue;
  132. }
  133. auto objIter = RandomGeneratorUtil::nextItem(weightedObjects, rand);
  134. objIter->first->setPosition(objIter->second);
  135. placeObject(*objIter->first, objs);
  136. blockedArea.subtract(objIter->first->getArea());
  137. tilePos = 0;
  138. postProcess(*objIter->first);
  139. if(maxWeight < 0)
  140. logGlobal->warn("Placed obstacle with negative weight at %s", objIter->second.toString());
  141. for(auto & o : allObjects)
  142. {
  143. if(&o != objIter->first)
  144. o.clear();
  145. }
  146. }
  147. return objs;
  148. }
  149. //FIXME: Only editor placer obstacles directly
  150. void ObstacleProxy::finalInsertion(CMapEditManager * manager, std::set<CGObjectInstance*> & instances)
  151. {
  152. manager->insertObjects(instances); //insert as one operation - for undo purposes
  153. }
  154. std::pair<bool, bool> ObstacleProxy::verifyCoverage(const int3 & t) const
  155. {
  156. return {blockedArea.contains(t), false};
  157. }
  158. void ObstacleProxy::placeObject(rmg::Object & object, std::set<CGObjectInstance*> & instances)
  159. {
  160. for (auto * instance : object.instances())
  161. {
  162. instances.insert(&instance->object());
  163. }
  164. }
  165. EditorObstaclePlacer::EditorObstaclePlacer(CMap* map):
  166. map(map)
  167. {
  168. }
  169. bool EditorObstaclePlacer::isInTheMap(const int3& tile)
  170. {
  171. return map->isInTheMap(tile);
  172. }
  173. void EditorObstaclePlacer::placeObstacles(CRandomGenerator & rand)
  174. {
  175. auto obstacles = createObstacles(rand);
  176. finalInsertion(map->getEditManager(), obstacles);
  177. }
  178. VCMI_LIB_NAMESPACE_END