RmgObject.cpp 12 KB

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