ObstacleProxy.cpp 5.5 KB

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