2
0

ObstacleProxy.cpp 5.7 KB

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