RmgObject.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 "../GameLibrary.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. #include <vstd/RNG.h>
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. using namespace rmg;
  25. Object::Instance::Instance(const Object& parent, std::shared_ptr<CGObjectInstance> object): dParent(parent), dObject(object)
  26. {
  27. setPosition(dPosition);
  28. }
  29. Object::Instance::Instance(const Object& parent, std::shared_ptr<CGObjectInstance> object, const int3 & position): Instance(parent, object)
  30. {
  31. setPosition(position);
  32. }
  33. const Area & Object::Instance::getBlockedArea() const
  34. {
  35. if(dBlockedAreaCache.empty())
  36. {
  37. std::set<int3> blockedArea = dObject->getBlockedPos();
  38. dBlockedAreaCache.assign(rmg::Tileset(blockedArea.begin(), blockedArea.end()));
  39. if(dObject->isVisitable() || dBlockedAreaCache.empty())
  40. dBlockedAreaCache.add(dObject->visitablePos());
  41. }
  42. return dBlockedAreaCache;
  43. }
  44. int3 Object::Instance::getTopTile() const
  45. {
  46. return object().getTopVisiblePos();
  47. }
  48. int3 Object::Instance::getPosition(bool isAbsolute) const
  49. {
  50. if(isAbsolute)
  51. return dPosition + dParent.getPosition();
  52. else
  53. return dPosition;
  54. }
  55. int3 Object::Instance::getVisitablePosition() const
  56. {
  57. return dObject->visitablePos();
  58. }
  59. const rmg::Area & Object::Instance::getAccessibleArea() const
  60. {
  61. if(dAccessibleAreaCache.empty())
  62. {
  63. auto neighbours = rmg::Area({getVisitablePosition()}).getBorderOutside();
  64. // FIXME: Blocked area of removable object is also accessible area for neighbors
  65. rmg::Area visitable = rmg::Area(neighbours) - getBlockedArea();
  66. // TODO: Add in one operation to avoid multiple invalidation
  67. for(const auto & from : visitable.getTilesVector())
  68. {
  69. if(isVisitableFrom(from))
  70. dAccessibleAreaCache.add(from);
  71. }
  72. }
  73. return dAccessibleAreaCache;
  74. }
  75. void Object::Instance::setPosition(const int3 & position)
  76. {
  77. dPosition = position;
  78. dObject->setAnchorPos(dPosition + dParent.getPosition());
  79. dBlockedAreaCache.clear();
  80. dAccessibleAreaCache.clear();
  81. dParent.clearCachedArea();
  82. }
  83. void Object::Instance::setPositionRaw(const int3 & position)
  84. {
  85. if(!dObject->anchorPos().isValid())
  86. {
  87. dObject->setAnchorPos(dPosition + dParent.getPosition());
  88. dBlockedAreaCache.clear();
  89. dAccessibleAreaCache.clear();
  90. dParent.clearCachedArea();
  91. }
  92. auto shift = position + dParent.getPosition() - dObject->anchorPos();
  93. dAccessibleAreaCache.translate(shift);
  94. dBlockedAreaCache.translate(shift);
  95. dPosition = position;
  96. dObject->setAnchorPos(dPosition + dParent.getPosition());
  97. }
  98. void Object::Instance::setAnyTemplate(vstd::RNG & rng)
  99. {
  100. auto templates = dObject->getObjectHandler()->getTemplates();
  101. if(templates.empty())
  102. throw rmgException(boost::str(boost::format("Did not find any graphics for object (%d,%d)") % dObject->ID % dObject->getObjTypeIndex()));
  103. dObject->appearance = *RandomGeneratorUtil::nextItem(templates, rng);
  104. dAccessibleAreaCache.clear();
  105. setPosition(getPosition(false));
  106. }
  107. void Object::Instance::setTemplate(TerrainId terrain, vstd::RNG & rng)
  108. {
  109. auto templates = dObject->getObjectHandler()->getMostSpecificTemplates(terrain);
  110. if (templates.empty())
  111. {
  112. auto terrainName = LIBRARY->terrainTypeHandler->getById(terrain)->getNameTranslated();
  113. throw rmgException(boost::str(boost::format("Did not find graphics for object (%d,%d) at %s") % dObject->ID % dObject->getObjTypeIndex() % terrainName));
  114. }
  115. dObject->appearance = *RandomGeneratorUtil::nextItem(templates, rng);
  116. dAccessibleAreaCache.clear();
  117. setPosition(getPosition(false));
  118. }
  119. void Object::Instance::clear()
  120. {
  121. if (onCleared)
  122. onCleared(*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. std::shared_ptr<CGObjectInstance> Object::Instance::pointer() const
  145. {
  146. return dObject;
  147. }
  148. const CGObjectInstance & Object::Instance::object() const
  149. {
  150. return *dObject;
  151. }
  152. Object::Object(std::shared_ptr<CGObjectInstance> object, const int3 & position):
  153. guarded(false),
  154. value(0)
  155. {
  156. addInstance(object, position);
  157. }
  158. Object::Object(std::shared_ptr<CGObjectInstance> object):
  159. guarded(false),
  160. value(0)
  161. {
  162. addInstance(object);
  163. }
  164. Object::Object(const Object & object):
  165. guarded(false),
  166. value(object.value)
  167. {
  168. for(const auto & i : object.dInstances)
  169. addInstance(i.pointer(), i.getPosition());
  170. setPosition(object.getPosition());
  171. }
  172. std::list<Object::Instance*> & Object::instances()
  173. {
  174. if (cachedInstanceList.empty())
  175. {
  176. for(auto & i : dInstances)
  177. cachedInstanceList.push_back(&i);
  178. }
  179. return cachedInstanceList;
  180. }
  181. std::list<const Object::Instance*> & Object::instances() const
  182. {
  183. if (cachedInstanceConstList.empty())
  184. {
  185. for(const auto & i : dInstances)
  186. cachedInstanceConstList.push_back(&i);
  187. }
  188. return cachedInstanceConstList;
  189. }
  190. void Object::addInstance(Instance & object)
  191. {
  192. //assert(object.dParent == *this);
  193. setGuardedIfMonster(object);
  194. dInstances.push_back(object);
  195. cachedInstanceList.push_back(&object);
  196. cachedInstanceConstList.push_back(&object);
  197. clearCachedArea();
  198. visibleTopOffset.reset();
  199. }
  200. Object::Instance & Object::addInstance(std::shared_ptr<CGObjectInstance> object)
  201. {
  202. dInstances.emplace_back(*this, object);
  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. Object::Instance & Object::addInstance(std::shared_ptr<CGObjectInstance> object, const int3 & position)
  211. {
  212. dInstances.emplace_back(*this, object, position);
  213. setGuardedIfMonster(dInstances.back());
  214. cachedInstanceList.push_back(&dInstances.back());
  215. cachedInstanceConstList.push_back(&dInstances.back());
  216. clearCachedArea();
  217. visibleTopOffset.reset();
  218. return dInstances.back();
  219. }
  220. bool Object::isVisitable() const
  221. {
  222. return vstd::contains_if(dInstances, [](const Instance & instance)
  223. {
  224. return instance.object().isVisitable();
  225. });
  226. }
  227. const int3 & Object::getPosition() const
  228. {
  229. return dPosition;
  230. }
  231. int3 Object::getVisitablePosition() const
  232. {
  233. assert(!dInstances.empty());
  234. // FIXME: This doesn't take into account Hota monster offset
  235. if (guarded)
  236. return getGuardPos();
  237. for(const auto & instance : dInstances)
  238. if(!getArea().contains(instance.getVisitablePosition()))
  239. return instance.getVisitablePosition();
  240. return dInstances.back().getVisitablePosition(); //fallback - return position of last object
  241. }
  242. const rmg::Area & Object::getAccessibleArea(bool exceptLast) const
  243. {
  244. if(dInstances.empty())
  245. return dAccessibleAreaFullCache;
  246. if(exceptLast && !dAccessibleAreaCache.empty())
  247. return dAccessibleAreaCache;
  248. if(!exceptLast && !dAccessibleAreaFullCache.empty())
  249. return dAccessibleAreaFullCache;
  250. // FIXME: This clears tiles for every consecutive object
  251. for(auto i = dInstances.begin(); i != std::prev(dInstances.end()); ++i)
  252. dAccessibleAreaCache.unite(i->getAccessibleArea());
  253. dAccessibleAreaFullCache = dAccessibleAreaCache;
  254. dAccessibleAreaFullCache.unite(dInstances.back().getAccessibleArea());
  255. dAccessibleAreaCache.subtract(getArea());
  256. dAccessibleAreaFullCache.subtract(getArea());
  257. if(exceptLast)
  258. return dAccessibleAreaCache;
  259. else
  260. return dAccessibleAreaFullCache;
  261. }
  262. const rmg::Area & Object::getBlockVisitableArea() const
  263. {
  264. if(dBlockVisitableCache.empty())
  265. {
  266. for(const auto & i : dInstances)
  267. {
  268. // FIXME: Account for blockvis objects with multiple visitable tiles
  269. if (i.isBlockedVisitable())
  270. dBlockVisitableCache.add(i.getVisitablePosition());
  271. }
  272. }
  273. return dBlockVisitableCache;
  274. }
  275. const rmg::Area & Object::getRemovableArea() const
  276. {
  277. if(dRemovableAreaCache.empty())
  278. {
  279. for(const auto & i : dInstances)
  280. {
  281. if (i.isRemovable())
  282. dRemovableAreaCache.unite(i.getBlockedArea());
  283. }
  284. }
  285. return dRemovableAreaCache;
  286. }
  287. const rmg::Area & Object::getVisitableArea() const
  288. {
  289. if(dVisitableCache.empty())
  290. {
  291. for(const auto & i : dInstances)
  292. {
  293. // FIXME: Account for objects with multiple visitable tiles
  294. dVisitableCache.add(i.getVisitablePosition());
  295. }
  296. }
  297. return dVisitableCache;
  298. }
  299. const rmg::Area Object::getEntrableArea() const
  300. {
  301. // Calculate Area that hero can freely pass
  302. // Do not use blockVisitTiles, unless they belong to removable objects (resources etc.)
  303. // area = accessibleArea - (blockVisitableArea - removableArea)
  304. // FIXME: What does it do? AccessibleArea means area AROUND the object
  305. rmg::Area entrableArea = getVisitableArea();
  306. rmg::Area blockVisitableArea = getBlockVisitableArea();
  307. blockVisitableArea.subtract(getRemovableArea());
  308. entrableArea.subtract(blockVisitableArea);
  309. return entrableArea;
  310. }
  311. void Object::setPosition(const int3 & position)
  312. {
  313. auto shift = position - dPosition;
  314. dAccessibleAreaCache.translate(shift);
  315. dAccessibleAreaFullCache.translate(shift);
  316. dBlockVisitableCache.translate(shift);
  317. dVisitableCache.translate(shift);
  318. dRemovableAreaCache.translate(shift);
  319. dFullAreaCache.translate(shift);
  320. dBorderAboveCache.translate(shift);
  321. dPosition = position;
  322. for(auto& i : dInstances)
  323. i.setPositionRaw(i.getPosition());
  324. }
  325. void Object::setTemplate(const TerrainId & terrain, vstd::RNG & rng)
  326. {
  327. for(auto& i : dInstances)
  328. i.setTemplate(terrain, rng);
  329. visibleTopOffset.reset();
  330. }
  331. const Area & Object::getArea() const
  332. {
  333. if(!dFullAreaCache.empty() || dInstances.empty())
  334. return dFullAreaCache;
  335. for(const auto & instance : dInstances)
  336. {
  337. dFullAreaCache.unite(instance.getBlockedArea());
  338. }
  339. return dFullAreaCache;
  340. }
  341. const Area & Object::getBorderAbove() const
  342. {
  343. if(dBorderAboveCache.empty())
  344. {
  345. for(const auto & instance : dInstances)
  346. {
  347. if (instance.isRemovable() || instance.object().appearance->isVisitableFromTop())
  348. continue;
  349. dBorderAboveCache.unite(instance.getBorderAbove());
  350. }
  351. }
  352. return dBorderAboveCache;
  353. }
  354. const int3 Object::getVisibleTop() const
  355. {
  356. if (visibleTopOffset)
  357. {
  358. return dPosition + visibleTopOffset.value();
  359. }
  360. else
  361. {
  362. int3 topTile(-1, 10000, -1); //Start at the bottom
  363. for (const auto& i : dInstances)
  364. {
  365. if (i.getTopTile().y < topTile.y)
  366. {
  367. topTile = i.getTopTile();
  368. }
  369. }
  370. visibleTopOffset = topTile - dPosition;
  371. return topTile;
  372. }
  373. }
  374. bool rmg::Object::isGuarded() const
  375. {
  376. return guarded;
  377. }
  378. void rmg::Object::setGuardedIfMonster(const Instance& object)
  379. {
  380. if (object.object().ID == Obj::MONSTER)
  381. {
  382. guarded = true;
  383. }
  384. }
  385. int3 rmg::Object::getGuardPos() const
  386. {
  387. if (guarded)
  388. {
  389. for (auto & instance : dInstances)
  390. {
  391. if (instance.object().ID == Obj::MONSTER)
  392. {
  393. return instance.getVisitablePosition();
  394. }
  395. }
  396. }
  397. return int3(-1,-1,-1);
  398. }
  399. void rmg::Object::setValue(uint32_t newValue)
  400. {
  401. value = newValue;
  402. }
  403. uint32_t rmg::Object::getValue() const
  404. {
  405. return value;
  406. }
  407. rmg::Area Object::Instance::getBorderAbove() const
  408. {
  409. if (!object().isVisitable())
  410. {
  411. // TODO: Non-visitable objects don't need this, but theoretically could return valid area
  412. return rmg::Area();
  413. }
  414. int3 visitablePos = getVisitablePosition();
  415. auto areaVisitable = rmg::Area({visitablePos});
  416. auto borderAbove = areaVisitable.getBorderOutside();
  417. vstd::erase_if(borderAbove, [&](const int3 & tile)
  418. {
  419. return tile.y >= visitablePos.y ||
  420. (!object().blockingAt(tile + int3(0, 1, 0)) &&
  421. object().blockingAt(tile));
  422. });
  423. return borderAbove;
  424. }
  425. void Object::Instance::finalize(RmgMap & map, vstd::RNG & rng)
  426. {
  427. if(!map.isOnMap(getPosition(true)))
  428. throw rmgException(boost::str(boost::format("Position of object %d at %s is outside the map") % dObject->id % getPosition(true).toString()));
  429. //If no specific template was defined for this object, select any matching
  430. if (!dObject->appearance)
  431. {
  432. const auto * terrainType = map.getTile(getPosition(true)).getTerrain();
  433. auto templates = dObject->getObjectHandler()->getTemplates(terrainType->getId());
  434. if (templates.empty())
  435. {
  436. 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));
  437. }
  438. else
  439. {
  440. setTemplate(terrainType->getId(), rng);
  441. }
  442. }
  443. if (dObject->isVisitable() && !map.isOnMap(dObject->visitablePos()))
  444. throw rmgException(boost::str(boost::format("Visitable tile %s of object %d at %s is outside the map") % dObject->visitablePos().toString() % dObject->id % dObject->anchorPos().toString()));
  445. for(const auto & tile : dObject->getBlockedPos())
  446. {
  447. if(!map.isOnMap(tile))
  448. throw rmgException(boost::str(boost::format("Tile %s of object %d at %s is outside the map") % tile.toString() % dObject->id % dObject->anchorPos().toString()));
  449. }
  450. for(const auto & tile : getBlockedArea().getTilesVector())
  451. {
  452. map.setOccupied(tile, ETileType::USED);
  453. }
  454. map.getMapProxy()->insertObject(dObject);
  455. }
  456. void Object::finalize(RmgMap & map, vstd::RNG & rng)
  457. {
  458. if(dInstances.empty())
  459. throw rmgException("Cannot finalize object without instances");
  460. for(auto & dInstance : dInstances)
  461. {
  462. dInstance.finalize(map, rng);
  463. }
  464. }
  465. void Object::clearCachedArea() const
  466. {
  467. dFullAreaCache.clear();
  468. dAccessibleAreaCache.clear();
  469. dAccessibleAreaFullCache.clear();
  470. dBlockVisitableCache.clear();
  471. dVisitableCache.clear();
  472. dRemovableAreaCache.clear();
  473. dBorderAboveCache.clear();
  474. }
  475. void Object::clear()
  476. {
  477. for(auto & instance : dInstances)
  478. instance.clear();
  479. dInstances.clear();
  480. cachedInstanceList.clear();
  481. cachedInstanceConstList.clear();
  482. visibleTopOffset.reset();
  483. clearCachedArea();
  484. }
  485. VCMI_LIB_NAMESPACE_END