RmgObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. if (onCleared)
  120. onCleared(&dObject);
  121. delete &dObject;
  122. dBlockedAreaCache.clear();
  123. dAccessibleAreaCache.clear();
  124. dParent.clearCachedArea();
  125. }
  126. bool Object::Instance::isVisitableFrom(const int3 & position) const
  127. {
  128. auto relPosition = position - getPosition(true);
  129. return dObject.appearance->isVisitableFrom(relPosition.x, relPosition.y);
  130. }
  131. bool Object::Instance::isBlockedVisitable() const
  132. {
  133. return dObject.isBlockedVisitable();
  134. }
  135. bool Object::Instance::isRemovable() const
  136. {
  137. return dObject.isRemovable();
  138. }
  139. CGObjectInstance & Object::Instance::object()
  140. {
  141. return dObject;
  142. }
  143. const CGObjectInstance & Object::Instance::object() const
  144. {
  145. return dObject;
  146. }
  147. Object::Object(CGObjectInstance & object, const int3 & position):
  148. guarded(false)
  149. {
  150. addInstance(object, position);
  151. }
  152. Object::Object(CGObjectInstance & object):
  153. guarded(false)
  154. {
  155. addInstance(object);
  156. }
  157. Object::Object(const Object & object):
  158. guarded(false)
  159. {
  160. for(const auto & i : object.dInstances)
  161. addInstance(const_cast<CGObjectInstance &>(i.object()), i.getPosition());
  162. setPosition(object.getPosition());
  163. }
  164. std::list<Object::Instance*> & Object::instances()
  165. {
  166. if (cachedInstanceList.empty())
  167. {
  168. for(auto & i : dInstances)
  169. cachedInstanceList.push_back(&i);
  170. }
  171. return cachedInstanceList;
  172. }
  173. std::list<const Object::Instance*> & Object::instances() const
  174. {
  175. if (cachedInstanceConstList.empty())
  176. {
  177. for(const auto & i : dInstances)
  178. cachedInstanceConstList.push_back(&i);
  179. }
  180. return cachedInstanceConstList;
  181. }
  182. void Object::addInstance(Instance & object)
  183. {
  184. //assert(object.dParent == *this);
  185. setGuardedIfMonster(object);
  186. dInstances.push_back(object);
  187. cachedInstanceList.push_back(&object);
  188. cachedInstanceConstList.push_back(&object);
  189. clearCachedArea();
  190. visibleTopOffset.reset();
  191. }
  192. Object::Instance & Object::addInstance(CGObjectInstance & object)
  193. {
  194. dInstances.emplace_back(*this, object);
  195. setGuardedIfMonster(dInstances.back());
  196. cachedInstanceList.push_back(&dInstances.back());
  197. cachedInstanceConstList.push_back(&dInstances.back());
  198. clearCachedArea();
  199. visibleTopOffset.reset();
  200. return dInstances.back();
  201. }
  202. Object::Instance & Object::addInstance(CGObjectInstance & object, const int3 & position)
  203. {
  204. dInstances.emplace_back(*this, object, position);
  205. setGuardedIfMonster(dInstances.back());
  206. cachedInstanceList.push_back(&dInstances.back());
  207. cachedInstanceConstList.push_back(&dInstances.back());
  208. clearCachedArea();
  209. visibleTopOffset.reset();
  210. return dInstances.back();
  211. }
  212. const int3 & Object::getPosition() const
  213. {
  214. return dPosition;
  215. }
  216. int3 Object::getVisitablePosition() const
  217. {
  218. assert(!dInstances.empty());
  219. for(const auto & instance : dInstances)
  220. if(!getArea().contains(instance.getVisitablePosition()))
  221. return instance.getVisitablePosition();
  222. return dInstances.back().getVisitablePosition(); //fallback - return position of last object
  223. }
  224. const rmg::Area & Object::getAccessibleArea(bool exceptLast) const
  225. {
  226. if(dInstances.empty())
  227. return dAccessibleAreaFullCache;
  228. if(exceptLast && !dAccessibleAreaCache.empty())
  229. return dAccessibleAreaCache;
  230. if(!exceptLast && !dAccessibleAreaFullCache.empty())
  231. return dAccessibleAreaFullCache;
  232. // FIXME: This clears tiles for every consecutive object
  233. for(auto i = dInstances.begin(); i != std::prev(dInstances.end()); ++i)
  234. dAccessibleAreaCache.unite(i->getAccessibleArea());
  235. dAccessibleAreaFullCache = dAccessibleAreaCache;
  236. dAccessibleAreaFullCache.unite(dInstances.back().getAccessibleArea());
  237. dAccessibleAreaCache.subtract(getArea());
  238. dAccessibleAreaFullCache.subtract(getArea());
  239. if(exceptLast)
  240. return dAccessibleAreaCache;
  241. else
  242. return dAccessibleAreaFullCache;
  243. }
  244. const rmg::Area & Object::getBlockVisitableArea() const
  245. {
  246. if(dBlockVisitableCache.empty())
  247. {
  248. for(const auto & i : dInstances)
  249. {
  250. // FIXME: Account for blockvis objects with multiple visitable tiles
  251. if (i.isBlockedVisitable())
  252. dBlockVisitableCache.add(i.getVisitablePosition());
  253. }
  254. }
  255. return dBlockVisitableCache;
  256. }
  257. const rmg::Area & Object::getRemovableArea() const
  258. {
  259. if(dRemovableAreaCache.empty())
  260. {
  261. for(const auto & i : dInstances)
  262. {
  263. if (i.isRemovable())
  264. dRemovableAreaCache.unite(i.getBlockedArea());
  265. }
  266. }
  267. return dRemovableAreaCache;
  268. }
  269. const rmg::Area & Object::getVisitableArea() const
  270. {
  271. if(dVisitableCache.empty())
  272. {
  273. for(const auto & i : dInstances)
  274. {
  275. // FIXME: Account for bjects with multiple visitable tiles
  276. dVisitableCache.add(i.getVisitablePosition());
  277. }
  278. }
  279. return dVisitableCache;
  280. }
  281. const rmg::Area Object::getEntrableArea() const
  282. {
  283. // Calculate Area that hero can freely pass
  284. // Do not use blockVisitTiles, unless they belong to removable objects (resources etc.)
  285. // area = accessibleArea - (blockVisitableArea - removableArea)
  286. // FIXME: What does it do? AccessibleArea means area AROUND the object
  287. rmg::Area entrableArea = getVisitableArea();
  288. rmg::Area blockVisitableArea = getBlockVisitableArea();
  289. blockVisitableArea.subtract(getRemovableArea());
  290. entrableArea.subtract(blockVisitableArea);
  291. return entrableArea;
  292. }
  293. void Object::setPosition(const int3 & position)
  294. {
  295. auto shift = position - dPosition;
  296. dAccessibleAreaCache.translate(shift);
  297. dAccessibleAreaFullCache.translate(shift);
  298. dBlockVisitableCache.translate(shift);
  299. dVisitableCache.translate(shift);
  300. dRemovableAreaCache.translate(shift);
  301. dFullAreaCache.translate(shift);
  302. dPosition = position;
  303. for(auto& i : dInstances)
  304. i.setPositionRaw(i.getPosition());
  305. }
  306. void Object::setTemplate(const TerrainId & terrain, CRandomGenerator & rng)
  307. {
  308. for(auto& i : dInstances)
  309. i.setTemplate(terrain, rng);
  310. visibleTopOffset.reset();
  311. }
  312. const Area & Object::getArea() const
  313. {
  314. if(!dFullAreaCache.empty() || dInstances.empty())
  315. return dFullAreaCache;
  316. for(const auto & instance : dInstances)
  317. {
  318. dFullAreaCache.unite(instance.getBlockedArea());
  319. }
  320. return dFullAreaCache;
  321. }
  322. const int3 Object::getVisibleTop() const
  323. {
  324. if (visibleTopOffset)
  325. {
  326. return dPosition + visibleTopOffset.value();
  327. }
  328. else
  329. {
  330. int3 topTile(-1, 10000, -1); //Start at the bottom
  331. for (const auto& i : dInstances)
  332. {
  333. if (i.getTopTile().y < topTile.y)
  334. {
  335. topTile = i.getTopTile();
  336. }
  337. }
  338. visibleTopOffset = topTile - dPosition;
  339. return topTile;
  340. }
  341. }
  342. bool rmg::Object::isGuarded() const
  343. {
  344. return guarded;
  345. }
  346. void rmg::Object::setGuardedIfMonster(const Instance& object)
  347. {
  348. if (object.object().ID == Obj::MONSTER)
  349. {
  350. guarded = true;
  351. }
  352. }
  353. void Object::Instance::finalize(RmgMap & map, CRandomGenerator & rng)
  354. {
  355. if(!map.isOnMap(getPosition(true)))
  356. throw rmgException(boost::str(boost::format("Position of object %d at %s is outside the map") % dObject.id % getPosition(true).toString()));
  357. //If no specific template was defined for this object, select any matching
  358. if (!dObject.appearance)
  359. {
  360. const auto * terrainType = map.getTile(getPosition(true)).terType;
  361. auto templates = dObject.getObjectHandler()->getTemplates(terrainType->getId());
  362. if (templates.empty())
  363. {
  364. 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));
  365. }
  366. else
  367. {
  368. setTemplate(terrainType->getId(), rng);
  369. }
  370. }
  371. if (dObject.isVisitable() && !map.isOnMap(dObject.visitablePos()))
  372. 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()));
  373. for(const auto & tile : dObject.getBlockedPos())
  374. {
  375. if(!map.isOnMap(tile))
  376. throw rmgException(boost::str(boost::format("Tile %s of object %d at %s is outside the map") % tile.toString() % dObject.id % dObject.pos.toString()));
  377. }
  378. for(const auto & tile : getBlockedArea().getTilesVector())
  379. {
  380. map.setOccupied(tile, ETileType::USED);
  381. }
  382. map.getMapProxy()->insertObject(&dObject);
  383. }
  384. void Object::finalize(RmgMap & map, CRandomGenerator & rng)
  385. {
  386. if(dInstances.empty())
  387. throw rmgException("Cannot finalize object without instances");
  388. for(auto & dInstance : dInstances)
  389. {
  390. dInstance.finalize(map, rng);
  391. }
  392. }
  393. void Object::clearCachedArea() const
  394. {
  395. dFullAreaCache.clear();
  396. dAccessibleAreaCache.clear();
  397. dAccessibleAreaFullCache.clear();
  398. dBlockVisitableCache.clear();
  399. dVisitableCache.clear();
  400. dRemovableAreaCache.clear();
  401. }
  402. void Object::clear()
  403. {
  404. for(auto & instance : dInstances)
  405. instance.clear();
  406. dInstances.clear();
  407. cachedInstanceList.clear();
  408. cachedInstanceConstList.clear();
  409. visibleTopOffset.reset();
  410. clearCachedArea();
  411. }
  412. VCMI_LIB_NAMESPACE_END