RmgObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * RmgObject.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 "RmgObject.h"
  12. #include "RmgMap.h"
  13. #include "../mapping/CMapEditManager.h"
  14. #include "../mapping/CMap.h"
  15. #include "../VCMI_Lib.h"
  16. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  17. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  18. #include "../mapObjects/ObjectTemplate.h"
  19. #include "Functions.h"
  20. #include "../TerrainHandler.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. using namespace rmg;
  23. Object::Instance::Instance(const Object& parent, CGObjectInstance & object): dParent(parent), dObject(object)
  24. {
  25. setPosition(dPosition);
  26. }
  27. Object::Instance::Instance(const Object& parent, CGObjectInstance & object, const int3 & position): Instance(parent, object)
  28. {
  29. setPosition(position);
  30. }
  31. const Area & Object::Instance::getBlockedArea() const
  32. {
  33. if(dBlockedAreaCache.empty())
  34. {
  35. std::set<int3> blockedArea = dObject.getBlockedPos();
  36. dBlockedAreaCache.assign(rmg::Tileset(blockedArea.begin(), blockedArea.end()));
  37. if(dObject.isVisitable() || dBlockedAreaCache.empty())
  38. dBlockedAreaCache.add(dObject.visitablePos());
  39. }
  40. return dBlockedAreaCache;
  41. }
  42. int3 Object::Instance::getTopTile() const
  43. {
  44. return object().getTopVisiblePos();
  45. }
  46. int3 Object::Instance::getPosition(bool isAbsolute) const
  47. {
  48. if(isAbsolute)
  49. return dPosition + dParent.getPosition();
  50. else
  51. return dPosition;
  52. }
  53. int3 Object::Instance::getVisitablePosition() const
  54. {
  55. return dObject.visitablePos();
  56. }
  57. const rmg::Area & Object::Instance::getAccessibleArea() const
  58. {
  59. if(dAccessibleAreaCache.empty())
  60. {
  61. auto neighbours = rmg::Area({getVisitablePosition()}).getBorderOutside();
  62. // FIXME: Blocked area of removable object is also accessible area for neighbors
  63. rmg::Area visitable = rmg::Area(neighbours) - getBlockedArea();
  64. // TODO: Add in one operation to avoid multiple invalidation
  65. for(const auto & from : visitable.getTilesVector())
  66. {
  67. if(isVisitableFrom(from))
  68. dAccessibleAreaCache.add(from);
  69. }
  70. }
  71. return dAccessibleAreaCache;
  72. }
  73. void Object::Instance::setPosition(const int3 & position)
  74. {
  75. dPosition = position;
  76. dObject.pos = dPosition + dParent.getPosition();
  77. dBlockedAreaCache.clear();
  78. dAccessibleAreaCache.clear();
  79. dParent.clearCachedArea();
  80. }
  81. void Object::Instance::setPositionRaw(const int3 & position)
  82. {
  83. if(!dObject.pos.valid())
  84. {
  85. dObject.pos = dPosition + dParent.getPosition();
  86. dBlockedAreaCache.clear();
  87. dAccessibleAreaCache.clear();
  88. dParent.clearCachedArea();
  89. }
  90. auto shift = position + dParent.getPosition() - dObject.pos;
  91. dAccessibleAreaCache.translate(shift);
  92. dBlockedAreaCache.translate(shift);
  93. dPosition = position;
  94. dObject.pos = dPosition + dParent.getPosition();
  95. }
  96. void Object::Instance::setAnyTemplate(CRandomGenerator & rng)
  97. {
  98. auto templates = dObject.getObjectHandler()->getTemplates();
  99. if(templates.empty())
  100. throw rmgException(boost::str(boost::format("Did not find any graphics for object (%d,%d)") % dObject.ID % dObject.getObjTypeIndex()));
  101. dObject.appearance = *RandomGeneratorUtil::nextItem(templates, rng);
  102. dAccessibleAreaCache.clear();
  103. setPosition(getPosition(false));
  104. }
  105. void Object::Instance::setTemplate(TerrainId terrain, CRandomGenerator & rng)
  106. {
  107. auto templates = dObject.getObjectHandler()->getMostSpecificTemplates(terrain);
  108. if (templates.empty())
  109. {
  110. auto terrainName = VLC->terrainTypeHandler->getById(terrain)->getNameTranslated();
  111. throw rmgException(boost::str(boost::format("Did not find graphics for object (%d,%d) at %s") % dObject.ID % dObject.getObjTypeIndex() % terrainName));
  112. }
  113. dObject.appearance = *RandomGeneratorUtil::nextItem(templates, rng);
  114. dAccessibleAreaCache.clear();
  115. setPosition(getPosition(false));
  116. }
  117. void Object::Instance::clear()
  118. {
  119. delete &dObject;
  120. dBlockedAreaCache.clear();
  121. dAccessibleAreaCache.clear();
  122. dParent.clearCachedArea();
  123. }
  124. bool Object::Instance::isVisitableFrom(const int3 & position) const
  125. {
  126. auto relPosition = position - getPosition(true);
  127. return dObject.appearance->isVisitableFrom(relPosition.x, relPosition.y);
  128. }
  129. bool Object::Instance::isBlockedVisitable() const
  130. {
  131. return dObject.isBlockedVisitable();
  132. }
  133. bool Object::Instance::isRemovable() const
  134. {
  135. return dObject.isRemovable();
  136. }
  137. CGObjectInstance & Object::Instance::object()
  138. {
  139. return dObject;
  140. }
  141. const CGObjectInstance & Object::Instance::object() const
  142. {
  143. return dObject;
  144. }
  145. Object::Object(CGObjectInstance & object, const int3 & position):
  146. guarded(false)
  147. {
  148. addInstance(object, position);
  149. }
  150. Object::Object(CGObjectInstance & object):
  151. guarded(false)
  152. {
  153. addInstance(object);
  154. }
  155. Object::Object(const Object & object):
  156. guarded(false)
  157. {
  158. for(const auto & i : object.dInstances)
  159. addInstance(const_cast<CGObjectInstance &>(i.object()), i.getPosition());
  160. setPosition(object.getPosition());
  161. }
  162. std::list<Object::Instance*> & Object::instances()
  163. {
  164. if (cachedInstanceList.empty())
  165. {
  166. for(auto & i : dInstances)
  167. cachedInstanceList.push_back(&i);
  168. }
  169. return cachedInstanceList;
  170. }
  171. std::list<const Object::Instance*> & Object::instances() const
  172. {
  173. if (cachedInstanceConstList.empty())
  174. {
  175. for(const auto & i : dInstances)
  176. cachedInstanceConstList.push_back(&i);
  177. }
  178. return cachedInstanceConstList;
  179. }
  180. void Object::addInstance(Instance & object)
  181. {
  182. //assert(object.dParent == *this);
  183. setGuardedIfMonster(object);
  184. dInstances.push_back(object);
  185. cachedInstanceList.push_back(&object);
  186. cachedInstanceConstList.push_back(&object);
  187. clearCachedArea();
  188. visibleTopOffset.reset();
  189. }
  190. Object::Instance & Object::addInstance(CGObjectInstance & object)
  191. {
  192. dInstances.emplace_back(*this, object);
  193. setGuardedIfMonster(dInstances.back());
  194. cachedInstanceList.push_back(&dInstances.back());
  195. cachedInstanceConstList.push_back(&dInstances.back());
  196. clearCachedArea();
  197. visibleTopOffset.reset();
  198. return dInstances.back();
  199. }
  200. Object::Instance & Object::addInstance(CGObjectInstance & object, const int3 & position)
  201. {
  202. dInstances.emplace_back(*this, object, position);
  203. setGuardedIfMonster(dInstances.back());
  204. cachedInstanceList.push_back(&dInstances.back());
  205. cachedInstanceConstList.push_back(&dInstances.back());
  206. clearCachedArea();
  207. visibleTopOffset.reset();
  208. return dInstances.back();
  209. }
  210. const int3 & Object::getPosition() const
  211. {
  212. return dPosition;
  213. }
  214. int3 Object::getVisitablePosition() const
  215. {
  216. assert(!dInstances.empty());
  217. for(const auto & instance : dInstances)
  218. if(!getArea().contains(instance.getVisitablePosition()))
  219. return instance.getVisitablePosition();
  220. return dInstances.back().getVisitablePosition(); //fallback - return position of last object
  221. }
  222. const rmg::Area & Object::getAccessibleArea(bool exceptLast) const
  223. {
  224. if(dInstances.empty())
  225. return dAccessibleAreaFullCache;
  226. if(exceptLast && !dAccessibleAreaCache.empty())
  227. return dAccessibleAreaCache;
  228. if(!exceptLast && !dAccessibleAreaFullCache.empty())
  229. return dAccessibleAreaFullCache;
  230. // FIXME: This clears tiles for every consecutive object
  231. for(auto i = dInstances.begin(); i != std::prev(dInstances.end()); ++i)
  232. dAccessibleAreaCache.unite(i->getAccessibleArea());
  233. dAccessibleAreaFullCache = dAccessibleAreaCache;
  234. dAccessibleAreaFullCache.unite(dInstances.back().getAccessibleArea());
  235. dAccessibleAreaCache.subtract(getArea());
  236. dAccessibleAreaFullCache.subtract(getArea());
  237. if(exceptLast)
  238. return dAccessibleAreaCache;
  239. else
  240. return dAccessibleAreaFullCache;
  241. }
  242. const rmg::Area & Object::getBlockVisitableArea() const
  243. {
  244. if(dBlockVisitableCache.empty())
  245. {
  246. for(const auto & i : dInstances)
  247. {
  248. // FIXME: Account for blockvis objects with multiple visitable tiles
  249. if (i.isBlockedVisitable())
  250. dBlockVisitableCache.add(i.getVisitablePosition());
  251. }
  252. }
  253. return dBlockVisitableCache;
  254. }
  255. const rmg::Area & Object::getRemovableArea() const
  256. {
  257. if(dRemovableAreaCache.empty())
  258. {
  259. for(const auto & i : dInstances)
  260. {
  261. if (i.isRemovable())
  262. dRemovableAreaCache.unite(i.getBlockedArea());
  263. }
  264. }
  265. return dRemovableAreaCache;
  266. }
  267. const rmg::Area Object::getEntrableArea() const
  268. {
  269. // Calculate Area that hero can freely pass
  270. // Do not use blockVisitTiles, unless they belong to removable objects (resources etc.)
  271. // area = accessibleArea - (blockVisitableArea - removableArea)
  272. rmg::Area entrableArea = getAccessibleArea();
  273. rmg::Area blockVisitableArea = getBlockVisitableArea();
  274. blockVisitableArea.subtract(getRemovableArea());
  275. entrableArea.subtract(blockVisitableArea);
  276. return entrableArea;
  277. }
  278. void Object::setPosition(const int3 & position)
  279. {
  280. dAccessibleAreaCache.translate(position - dPosition);
  281. dAccessibleAreaFullCache.translate(position - dPosition);
  282. dBlockVisitableCache.translate(position - dPosition);
  283. dRemovableAreaCache.translate(position - dPosition);
  284. dFullAreaCache.translate(position - dPosition);
  285. dPosition = position;
  286. for(auto& i : dInstances)
  287. i.setPositionRaw(i.getPosition());
  288. }
  289. void Object::setTemplate(const TerrainId & terrain, CRandomGenerator & rng)
  290. {
  291. for(auto& i : dInstances)
  292. i.setTemplate(terrain, rng);
  293. visibleTopOffset.reset();
  294. }
  295. const Area & Object::getArea() const
  296. {
  297. if(!dFullAreaCache.empty() || dInstances.empty())
  298. return dFullAreaCache;
  299. for(const auto & instance : dInstances)
  300. {
  301. dFullAreaCache.unite(instance.getBlockedArea());
  302. }
  303. return dFullAreaCache;
  304. }
  305. const int3 Object::getVisibleTop() const
  306. {
  307. if (visibleTopOffset)
  308. {
  309. return dPosition + visibleTopOffset.value();
  310. }
  311. else
  312. {
  313. int3 topTile(-1, 10000, -1); //Start at the bottom
  314. for (const auto& i : dInstances)
  315. {
  316. if (i.getTopTile().y < topTile.y)
  317. {
  318. topTile = i.getTopTile();
  319. }
  320. }
  321. visibleTopOffset = topTile - dPosition;
  322. return topTile;
  323. }
  324. }
  325. bool rmg::Object::isGuarded() const
  326. {
  327. return guarded;
  328. }
  329. void rmg::Object::setGuardedIfMonster(const Instance& object)
  330. {
  331. if (object.object().ID == Obj::MONSTER)
  332. {
  333. guarded = true;
  334. }
  335. }
  336. void Object::Instance::finalize(RmgMap & map, CRandomGenerator & rng)
  337. {
  338. if(!map.isOnMap(getPosition(true)))
  339. throw rmgException(boost::str(boost::format("Position of object %d at %s is outside the map") % dObject.id % getPosition(true).toString()));
  340. //If no specific template was defined for this object, select any matching
  341. if (!dObject.appearance)
  342. {
  343. const auto * terrainType = map.getTile(getPosition(true)).terType;
  344. auto templates = dObject.getObjectHandler()->getTemplates(terrainType->getId());
  345. if (templates.empty())
  346. {
  347. throw rmgException(boost::str(boost::format("Did not find graphics for object (%d,%d) at %s (terrain %d)") % dObject.ID % dObject.getObjTypeIndex() % getPosition(true).toString() % terrainType));
  348. }
  349. else
  350. {
  351. setTemplate(terrainType->getId(), rng);
  352. }
  353. }
  354. if (dObject.isVisitable() && !map.isOnMap(dObject.visitablePos()))
  355. throw rmgException(boost::str(boost::format("Visitable tile %s of object %d at %s is outside the map") % dObject.visitablePos().toString() % dObject.id % dObject.pos.toString()));
  356. for(const auto & tile : dObject.getBlockedPos())
  357. {
  358. if(!map.isOnMap(tile))
  359. throw rmgException(boost::str(boost::format("Tile %s of object %d at %s is outside the map") % tile.toString() % dObject.id % dObject.pos.toString()));
  360. }
  361. for(const auto & tile : getBlockedArea().getTilesVector())
  362. {
  363. map.setOccupied(tile, ETileType::USED);
  364. }
  365. map.getMapProxy()->insertObject(&dObject);
  366. }
  367. void Object::finalize(RmgMap & map, CRandomGenerator & rng)
  368. {
  369. if(dInstances.empty())
  370. throw rmgException("Cannot finalize object without instances");
  371. for(auto & dInstance : dInstances)
  372. {
  373. dInstance.finalize(map, rng);
  374. }
  375. }
  376. void Object::clearCachedArea() const
  377. {
  378. dFullAreaCache.clear();
  379. dAccessibleAreaCache.clear();
  380. dAccessibleAreaFullCache.clear();
  381. dBlockVisitableCache.clear();
  382. dRemovableAreaCache.clear();
  383. }
  384. void Object::clear()
  385. {
  386. for(auto & instance : dInstances)
  387. instance.clear();
  388. dInstances.clear();
  389. cachedInstanceList.clear();
  390. cachedInstanceConstList.clear();
  391. visibleTopOffset.reset();
  392. clearCachedArea();
  393. }
  394. VCMI_LIB_NAMESPACE_END