RmgObject.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. #include <vstd/RNG.h>
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. using namespace rmg;
  25. Object::Instance::Instance(const Object& parent, CGObjectInstance & object): dParent(parent), dObject(object)
  26. {
  27. setPosition(dPosition);
  28. }
  29. Object::Instance::Instance(const Object& parent, 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().valid())
  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 = VLC->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. delete &dObject;
  124. dBlockedAreaCache.clear();
  125. dAccessibleAreaCache.clear();
  126. dParent.clearCachedArea();
  127. }
  128. bool Object::Instance::isVisitableFrom(const int3 & position) const
  129. {
  130. auto relPosition = position - getPosition(true);
  131. return dObject.appearance->isVisitableFrom(relPosition.x, relPosition.y);
  132. }
  133. bool Object::Instance::isBlockedVisitable() const
  134. {
  135. return dObject.isBlockedVisitable();
  136. }
  137. bool Object::Instance::isRemovable() const
  138. {
  139. return dObject.isRemovable();
  140. }
  141. CGObjectInstance & Object::Instance::object()
  142. {
  143. return dObject;
  144. }
  145. const CGObjectInstance & Object::Instance::object() const
  146. {
  147. return dObject;
  148. }
  149. Object::Object(CGObjectInstance & object, const int3 & position):
  150. guarded(false),
  151. value(0)
  152. {
  153. addInstance(object, position);
  154. }
  155. Object::Object(CGObjectInstance & object):
  156. guarded(false),
  157. value(0)
  158. {
  159. addInstance(object);
  160. }
  161. Object::Object(const Object & object):
  162. guarded(false),
  163. value(object.value)
  164. {
  165. for(const auto & i : object.dInstances)
  166. addInstance(const_cast<CGObjectInstance &>(i.object()), i.getPosition());
  167. setPosition(object.getPosition());
  168. }
  169. std::list<Object::Instance*> & Object::instances()
  170. {
  171. if (cachedInstanceList.empty())
  172. {
  173. for(auto & i : dInstances)
  174. cachedInstanceList.push_back(&i);
  175. }
  176. return cachedInstanceList;
  177. }
  178. std::list<const Object::Instance*> & Object::instances() const
  179. {
  180. if (cachedInstanceConstList.empty())
  181. {
  182. for(const auto & i : dInstances)
  183. cachedInstanceConstList.push_back(&i);
  184. }
  185. return cachedInstanceConstList;
  186. }
  187. void Object::addInstance(Instance & object)
  188. {
  189. //assert(object.dParent == *this);
  190. setGuardedIfMonster(object);
  191. dInstances.push_back(object);
  192. cachedInstanceList.push_back(&object);
  193. cachedInstanceConstList.push_back(&object);
  194. clearCachedArea();
  195. visibleTopOffset.reset();
  196. }
  197. Object::Instance & Object::addInstance(CGObjectInstance & object)
  198. {
  199. dInstances.emplace_back(*this, object);
  200. setGuardedIfMonster(dInstances.back());
  201. cachedInstanceList.push_back(&dInstances.back());
  202. cachedInstanceConstList.push_back(&dInstances.back());
  203. clearCachedArea();
  204. visibleTopOffset.reset();
  205. return dInstances.back();
  206. }
  207. Object::Instance & Object::addInstance(CGObjectInstance & object, const int3 & position)
  208. {
  209. dInstances.emplace_back(*this, object, position);
  210. setGuardedIfMonster(dInstances.back());
  211. cachedInstanceList.push_back(&dInstances.back());
  212. cachedInstanceConstList.push_back(&dInstances.back());
  213. clearCachedArea();
  214. visibleTopOffset.reset();
  215. return dInstances.back();
  216. }
  217. bool Object::isVisitable() const
  218. {
  219. return vstd::contains_if(dInstances, [](const Instance & instance)
  220. {
  221. return instance.object().isVisitable();
  222. });
  223. }
  224. const int3 & Object::getPosition() const
  225. {
  226. return dPosition;
  227. }
  228. int3 Object::getVisitablePosition() const
  229. {
  230. assert(!dInstances.empty());
  231. // FIXME: This doesn't take into account Hota monster offset
  232. if (guarded)
  233. return getGuardPos();
  234. for(const auto & instance : dInstances)
  235. if(!getArea().contains(instance.getVisitablePosition()))
  236. return instance.getVisitablePosition();
  237. return dInstances.back().getVisitablePosition(); //fallback - return position of last object
  238. }
  239. const rmg::Area & Object::getAccessibleArea(bool exceptLast) const
  240. {
  241. if(dInstances.empty())
  242. return dAccessibleAreaFullCache;
  243. if(exceptLast && !dAccessibleAreaCache.empty())
  244. return dAccessibleAreaCache;
  245. if(!exceptLast && !dAccessibleAreaFullCache.empty())
  246. return dAccessibleAreaFullCache;
  247. // FIXME: This clears tiles for every consecutive object
  248. for(auto i = dInstances.begin(); i != std::prev(dInstances.end()); ++i)
  249. dAccessibleAreaCache.unite(i->getAccessibleArea());
  250. dAccessibleAreaFullCache = dAccessibleAreaCache;
  251. dAccessibleAreaFullCache.unite(dInstances.back().getAccessibleArea());
  252. dAccessibleAreaCache.subtract(getArea());
  253. dAccessibleAreaFullCache.subtract(getArea());
  254. if(exceptLast)
  255. return dAccessibleAreaCache;
  256. else
  257. return dAccessibleAreaFullCache;
  258. }
  259. const rmg::Area & Object::getBlockVisitableArea() const
  260. {
  261. if(dBlockVisitableCache.empty())
  262. {
  263. for(const auto & i : dInstances)
  264. {
  265. // FIXME: Account for blockvis objects with multiple visitable tiles
  266. if (i.isBlockedVisitable())
  267. dBlockVisitableCache.add(i.getVisitablePosition());
  268. }
  269. }
  270. return dBlockVisitableCache;
  271. }
  272. const rmg::Area & Object::getRemovableArea() const
  273. {
  274. if(dRemovableAreaCache.empty())
  275. {
  276. for(const auto & i : dInstances)
  277. {
  278. if (i.isRemovable())
  279. dRemovableAreaCache.unite(i.getBlockedArea());
  280. }
  281. }
  282. return dRemovableAreaCache;
  283. }
  284. const rmg::Area & Object::getVisitableArea() const
  285. {
  286. if(dVisitableCache.empty())
  287. {
  288. for(const auto & i : dInstances)
  289. {
  290. // FIXME: Account for objects with multiple visitable tiles
  291. dVisitableCache.add(i.getVisitablePosition());
  292. }
  293. }
  294. return dVisitableCache;
  295. }
  296. const rmg::Area Object::getEntrableArea() const
  297. {
  298. // Calculate Area that hero can freely pass
  299. // Do not use blockVisitTiles, unless they belong to removable objects (resources etc.)
  300. // area = accessibleArea - (blockVisitableArea - removableArea)
  301. // FIXME: What does it do? AccessibleArea means area AROUND the object
  302. rmg::Area entrableArea = getVisitableArea();
  303. rmg::Area blockVisitableArea = getBlockVisitableArea();
  304. blockVisitableArea.subtract(getRemovableArea());
  305. entrableArea.subtract(blockVisitableArea);
  306. return entrableArea;
  307. }
  308. void Object::setPosition(const int3 & position)
  309. {
  310. auto shift = position - dPosition;
  311. dAccessibleAreaCache.translate(shift);
  312. dAccessibleAreaFullCache.translate(shift);
  313. dBlockVisitableCache.translate(shift);
  314. dVisitableCache.translate(shift);
  315. dRemovableAreaCache.translate(shift);
  316. dFullAreaCache.translate(shift);
  317. dBorderAboveCache.translate(shift);
  318. dPosition = position;
  319. for(auto& i : dInstances)
  320. i.setPositionRaw(i.getPosition());
  321. }
  322. void Object::setTemplate(const TerrainId & terrain, vstd::RNG & rng)
  323. {
  324. for(auto& i : dInstances)
  325. i.setTemplate(terrain, rng);
  326. visibleTopOffset.reset();
  327. }
  328. const Area & Object::getArea() const
  329. {
  330. if(!dFullAreaCache.empty() || dInstances.empty())
  331. return dFullAreaCache;
  332. for(const auto & instance : dInstances)
  333. {
  334. dFullAreaCache.unite(instance.getBlockedArea());
  335. }
  336. return dFullAreaCache;
  337. }
  338. const Area & Object::getBorderAbove() const
  339. {
  340. if(dBorderAboveCache.empty())
  341. {
  342. for(const auto & instance : dInstances)
  343. {
  344. if (instance.isRemovable() || instance.object().appearance->isVisitableFromTop())
  345. continue;
  346. dBorderAboveCache.unite(instance.getBorderAbove());
  347. }
  348. }
  349. return dBorderAboveCache;
  350. }
  351. const int3 Object::getVisibleTop() const
  352. {
  353. if (visibleTopOffset)
  354. {
  355. return dPosition + visibleTopOffset.value();
  356. }
  357. else
  358. {
  359. int3 topTile(-1, 10000, -1); //Start at the bottom
  360. for (const auto& i : dInstances)
  361. {
  362. if (i.getTopTile().y < topTile.y)
  363. {
  364. topTile = i.getTopTile();
  365. }
  366. }
  367. visibleTopOffset = topTile - dPosition;
  368. return topTile;
  369. }
  370. }
  371. bool rmg::Object::isGuarded() const
  372. {
  373. return guarded;
  374. }
  375. void rmg::Object::setGuardedIfMonster(const Instance& object)
  376. {
  377. if (object.object().ID == Obj::MONSTER)
  378. {
  379. guarded = true;
  380. }
  381. }
  382. int3 rmg::Object::getGuardPos() const
  383. {
  384. if (guarded)
  385. {
  386. for (auto & instance : dInstances)
  387. {
  388. if (instance.object().ID == Obj::MONSTER)
  389. {
  390. return instance.getVisitablePosition();
  391. }
  392. }
  393. }
  394. return int3(-1,-1,-1);
  395. }
  396. void rmg::Object::setValue(uint32_t newValue)
  397. {
  398. value = newValue;
  399. }
  400. uint32_t rmg::Object::getValue() const
  401. {
  402. return value;
  403. }
  404. rmg::Area Object::Instance::getBorderAbove() const
  405. {
  406. if (!object().isVisitable())
  407. {
  408. // TODO: Non-visitable objects don't need this, but theoretically could return valid area
  409. return rmg::Area();
  410. }
  411. int3 visitablePos = getVisitablePosition();
  412. auto areaVisitable = rmg::Area({visitablePos});
  413. auto borderAbove = areaVisitable.getBorderOutside();
  414. vstd::erase_if(borderAbove, [&](const int3 & tile)
  415. {
  416. return tile.y >= visitablePos.y ||
  417. (!object().blockingAt(tile + int3(0, 1, 0)) &&
  418. object().blockingAt(tile));
  419. });
  420. return borderAbove;
  421. }
  422. void Object::Instance::finalize(RmgMap & map, vstd::RNG & rng)
  423. {
  424. if(!map.isOnMap(getPosition(true)))
  425. throw rmgException(boost::str(boost::format("Position of object %d at %s is outside the map") % dObject.id % getPosition(true).toString()));
  426. //If no specific template was defined for this object, select any matching
  427. if (!dObject.appearance)
  428. {
  429. const auto * terrainType = map.getTile(getPosition(true)).getTerrain();
  430. auto templates = dObject.getObjectHandler()->getTemplates(terrainType->getId());
  431. if (templates.empty())
  432. {
  433. 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));
  434. }
  435. else
  436. {
  437. setTemplate(terrainType->getId(), rng);
  438. }
  439. }
  440. if (dObject.isVisitable() && !map.isOnMap(dObject.visitablePos()))
  441. 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()));
  442. for(const auto & tile : dObject.getBlockedPos())
  443. {
  444. if(!map.isOnMap(tile))
  445. throw rmgException(boost::str(boost::format("Tile %s of object %d at %s is outside the map") % tile.toString() % dObject.id % dObject.anchorPos().toString()));
  446. }
  447. for(const auto & tile : getBlockedArea().getTilesVector())
  448. {
  449. map.setOccupied(tile, ETileType::USED);
  450. }
  451. map.getMapProxy()->insertObject(&dObject);
  452. }
  453. void Object::finalize(RmgMap & map, vstd::RNG & rng)
  454. {
  455. if(dInstances.empty())
  456. throw rmgException("Cannot finalize object without instances");
  457. for(auto & dInstance : dInstances)
  458. {
  459. dInstance.finalize(map, rng);
  460. }
  461. }
  462. void Object::clearCachedArea() const
  463. {
  464. dFullAreaCache.clear();
  465. dAccessibleAreaCache.clear();
  466. dAccessibleAreaFullCache.clear();
  467. dBlockVisitableCache.clear();
  468. dVisitableCache.clear();
  469. dRemovableAreaCache.clear();
  470. dBorderAboveCache.clear();
  471. }
  472. void Object::clear()
  473. {
  474. for(auto & instance : dInstances)
  475. instance.clear();
  476. dInstances.clear();
  477. cachedInstanceList.clear();
  478. cachedInstanceConstList.clear();
  479. visibleTopOffset.reset();
  480. clearCachedArea();
  481. }
  482. VCMI_LIB_NAMESPACE_END