RmgObject.cpp 12 KB

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