RmgObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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::getVisitableArea() const
  268. {
  269. if(dVisitableCache.empty())
  270. {
  271. for(const auto & i : dInstances)
  272. {
  273. // FIXME: Account for bjects with multiple visitable tiles
  274. dVisitableCache.add(i.getVisitablePosition());
  275. }
  276. }
  277. return dVisitableCache;
  278. }
  279. const rmg::Area Object::getEntrableArea() const
  280. {
  281. // Calculate Area that hero can freely pass
  282. // Do not use blockVisitTiles, unless they belong to removable objects (resources etc.)
  283. // area = accessibleArea - (blockVisitableArea - removableArea)
  284. // FIXME: What does it do? AccessibleArea means area AROUND the object
  285. rmg::Area entrableArea = getVisitableArea();
  286. rmg::Area blockVisitableArea = getBlockVisitableArea();
  287. blockVisitableArea.subtract(getRemovableArea());
  288. entrableArea.subtract(blockVisitableArea);
  289. return entrableArea;
  290. }
  291. void Object::setPosition(const int3 & position)
  292. {
  293. auto shift = position - dPosition;
  294. dAccessibleAreaCache.translate(shift);
  295. dAccessibleAreaFullCache.translate(shift);
  296. dBlockVisitableCache.translate(shift);
  297. dVisitableCache.translate(shift);
  298. dRemovableAreaCache.translate(shift);
  299. dFullAreaCache.translate(shift);
  300. dPosition = position;
  301. for(auto& i : dInstances)
  302. i.setPositionRaw(i.getPosition());
  303. }
  304. void Object::setTemplate(const TerrainId & terrain, CRandomGenerator & rng)
  305. {
  306. for(auto& i : dInstances)
  307. i.setTemplate(terrain, rng);
  308. visibleTopOffset.reset();
  309. }
  310. const Area & Object::getArea() const
  311. {
  312. if(!dFullAreaCache.empty() || dInstances.empty())
  313. return dFullAreaCache;
  314. for(const auto & instance : dInstances)
  315. {
  316. dFullAreaCache.unite(instance.getBlockedArea());
  317. }
  318. return dFullAreaCache;
  319. }
  320. const int3 Object::getVisibleTop() const
  321. {
  322. if (visibleTopOffset)
  323. {
  324. return dPosition + visibleTopOffset.value();
  325. }
  326. else
  327. {
  328. int3 topTile(-1, 10000, -1); //Start at the bottom
  329. for (const auto& i : dInstances)
  330. {
  331. if (i.getTopTile().y < topTile.y)
  332. {
  333. topTile = i.getTopTile();
  334. }
  335. }
  336. visibleTopOffset = topTile - dPosition;
  337. return topTile;
  338. }
  339. }
  340. bool rmg::Object::isGuarded() const
  341. {
  342. return guarded;
  343. }
  344. void rmg::Object::setGuardedIfMonster(const Instance& object)
  345. {
  346. if (object.object().ID == Obj::MONSTER)
  347. {
  348. guarded = true;
  349. }
  350. }
  351. void Object::Instance::finalize(RmgMap & map, CRandomGenerator & rng)
  352. {
  353. if(!map.isOnMap(getPosition(true)))
  354. throw rmgException(boost::str(boost::format("Position of object %d at %s is outside the map") % dObject.id % getPosition(true).toString()));
  355. //If no specific template was defined for this object, select any matching
  356. if (!dObject.appearance)
  357. {
  358. const auto * terrainType = map.getTile(getPosition(true)).terType;
  359. auto templates = dObject.getObjectHandler()->getTemplates(terrainType->getId());
  360. if (templates.empty())
  361. {
  362. 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));
  363. }
  364. else
  365. {
  366. setTemplate(terrainType->getId(), rng);
  367. }
  368. }
  369. if (dObject.isVisitable() && !map.isOnMap(dObject.visitablePos()))
  370. 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()));
  371. for(const auto & tile : dObject.getBlockedPos())
  372. {
  373. if(!map.isOnMap(tile))
  374. throw rmgException(boost::str(boost::format("Tile %s of object %d at %s is outside the map") % tile.toString() % dObject.id % dObject.pos.toString()));
  375. }
  376. for(const auto & tile : getBlockedArea().getTilesVector())
  377. {
  378. map.setOccupied(tile, ETileType::USED);
  379. }
  380. map.getMapProxy()->insertObject(&dObject);
  381. }
  382. void Object::finalize(RmgMap & map, CRandomGenerator & rng)
  383. {
  384. if(dInstances.empty())
  385. throw rmgException("Cannot finalize object without instances");
  386. for(auto & dInstance : dInstances)
  387. {
  388. dInstance.finalize(map, rng);
  389. }
  390. }
  391. void Object::clearCachedArea() const
  392. {
  393. dFullAreaCache.clear();
  394. dAccessibleAreaCache.clear();
  395. dAccessibleAreaFullCache.clear();
  396. dBlockVisitableCache.clear();
  397. dVisitableCache.clear();
  398. dRemovableAreaCache.clear();
  399. }
  400. void Object::clear()
  401. {
  402. for(auto & instance : dInstances)
  403. instance.clear();
  404. dInstances.clear();
  405. cachedInstanceList.clear();
  406. cachedInstanceConstList.clear();
  407. visibleTopOffset.reset();
  408. clearCachedArea();
  409. }
  410. VCMI_LIB_NAMESPACE_END