RmgObject.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. for(const auto & instance : dInstances)
  232. if(!getArea().contains(instance.getVisitablePosition()))
  233. return instance.getVisitablePosition();
  234. return dInstances.back().getVisitablePosition(); //fallback - return position of last object
  235. }
  236. const rmg::Area & Object::getAccessibleArea(bool exceptLast) const
  237. {
  238. if(dInstances.empty())
  239. return dAccessibleAreaFullCache;
  240. if(exceptLast && !dAccessibleAreaCache.empty())
  241. return dAccessibleAreaCache;
  242. if(!exceptLast && !dAccessibleAreaFullCache.empty())
  243. return dAccessibleAreaFullCache;
  244. // FIXME: This clears tiles for every consecutive object
  245. for(auto i = dInstances.begin(); i != std::prev(dInstances.end()); ++i)
  246. dAccessibleAreaCache.unite(i->getAccessibleArea());
  247. dAccessibleAreaFullCache = dAccessibleAreaCache;
  248. dAccessibleAreaFullCache.unite(dInstances.back().getAccessibleArea());
  249. dAccessibleAreaCache.subtract(getArea());
  250. dAccessibleAreaFullCache.subtract(getArea());
  251. if(exceptLast)
  252. return dAccessibleAreaCache;
  253. else
  254. return dAccessibleAreaFullCache;
  255. }
  256. const rmg::Area & Object::getBlockVisitableArea() const
  257. {
  258. if(dBlockVisitableCache.empty())
  259. {
  260. for(const auto & i : dInstances)
  261. {
  262. // FIXME: Account for blockvis objects with multiple visitable tiles
  263. if (i.isBlockedVisitable())
  264. dBlockVisitableCache.add(i.getVisitablePosition());
  265. }
  266. }
  267. return dBlockVisitableCache;
  268. }
  269. const rmg::Area & Object::getRemovableArea() const
  270. {
  271. if(dRemovableAreaCache.empty())
  272. {
  273. for(const auto & i : dInstances)
  274. {
  275. if (i.isRemovable())
  276. dRemovableAreaCache.unite(i.getBlockedArea());
  277. }
  278. }
  279. return dRemovableAreaCache;
  280. }
  281. const rmg::Area & Object::getVisitableArea() const
  282. {
  283. if(dVisitableCache.empty())
  284. {
  285. for(const auto & i : dInstances)
  286. {
  287. // FIXME: Account for objects with multiple visitable tiles
  288. dVisitableCache.add(i.getVisitablePosition());
  289. }
  290. }
  291. return dVisitableCache;
  292. }
  293. const rmg::Area Object::getEntrableArea() const
  294. {
  295. // Calculate Area that hero can freely pass
  296. // Do not use blockVisitTiles, unless they belong to removable objects (resources etc.)
  297. // area = accessibleArea - (blockVisitableArea - removableArea)
  298. // FIXME: What does it do? AccessibleArea means area AROUND the object
  299. rmg::Area entrableArea = getVisitableArea();
  300. rmg::Area blockVisitableArea = getBlockVisitableArea();
  301. blockVisitableArea.subtract(getRemovableArea());
  302. entrableArea.subtract(blockVisitableArea);
  303. return entrableArea;
  304. }
  305. void Object::setPosition(const int3 & position)
  306. {
  307. auto shift = position - dPosition;
  308. dAccessibleAreaCache.translate(shift);
  309. dAccessibleAreaFullCache.translate(shift);
  310. dBlockVisitableCache.translate(shift);
  311. dVisitableCache.translate(shift);
  312. dRemovableAreaCache.translate(shift);
  313. dFullAreaCache.translate(shift);
  314. dBorderAboveCache.translate(shift);
  315. dPosition = position;
  316. for(auto& i : dInstances)
  317. i.setPositionRaw(i.getPosition());
  318. }
  319. void Object::setTemplate(const TerrainId & terrain, vstd::RNG & rng)
  320. {
  321. for(auto& i : dInstances)
  322. i.setTemplate(terrain, rng);
  323. visibleTopOffset.reset();
  324. }
  325. const Area & Object::getArea() const
  326. {
  327. if(!dFullAreaCache.empty() || dInstances.empty())
  328. return dFullAreaCache;
  329. for(const auto & instance : dInstances)
  330. {
  331. dFullAreaCache.unite(instance.getBlockedArea());
  332. }
  333. return dFullAreaCache;
  334. }
  335. const Area & Object::getBorderAbove() const
  336. {
  337. if(dBorderAboveCache.empty())
  338. {
  339. for(const auto & instance : dInstances)
  340. {
  341. if (instance.isRemovable() || instance.object().appearance->isVisitableFromTop())
  342. continue;
  343. dBorderAboveCache.unite(instance.getBorderAbove());
  344. }
  345. }
  346. return dBorderAboveCache;
  347. }
  348. const int3 Object::getVisibleTop() const
  349. {
  350. if (visibleTopOffset)
  351. {
  352. return dPosition + visibleTopOffset.value();
  353. }
  354. else
  355. {
  356. int3 topTile(-1, 10000, -1); //Start at the bottom
  357. for (const auto& i : dInstances)
  358. {
  359. if (i.getTopTile().y < topTile.y)
  360. {
  361. topTile = i.getTopTile();
  362. }
  363. }
  364. visibleTopOffset = topTile - dPosition;
  365. return topTile;
  366. }
  367. }
  368. bool rmg::Object::isGuarded() const
  369. {
  370. return guarded;
  371. }
  372. void rmg::Object::setGuardedIfMonster(const Instance& object)
  373. {
  374. if (object.object().ID == Obj::MONSTER)
  375. {
  376. guarded = true;
  377. }
  378. }
  379. int3 rmg::Object::getGuardPos() const
  380. {
  381. if (guarded)
  382. {
  383. for (auto & instance : dInstances)
  384. {
  385. if (instance.object().ID == Obj::MONSTER)
  386. {
  387. return instance.getVisitablePosition();
  388. }
  389. }
  390. }
  391. return int3(-1,-1,-1);
  392. }
  393. void rmg::Object::setValue(uint32_t newValue)
  394. {
  395. value = newValue;
  396. }
  397. uint32_t rmg::Object::getValue() const
  398. {
  399. return value;
  400. }
  401. rmg::Area Object::Instance::getBorderAbove() const
  402. {
  403. if (!object().isVisitable())
  404. {
  405. // TODO: Non-visitable objects don't need this, but theoretically could return valid area
  406. return rmg::Area();
  407. }
  408. int3 visitablePos = getVisitablePosition();
  409. auto areaVisitable = rmg::Area({visitablePos});
  410. auto borderAbove = areaVisitable.getBorderOutside();
  411. vstd::erase_if(borderAbove, [&](const int3 & tile)
  412. {
  413. return tile.y >= visitablePos.y ||
  414. (!object().blockingAt(tile + int3(0, 1, 0)) &&
  415. object().blockingAt(tile));
  416. });
  417. return borderAbove;
  418. }
  419. void Object::Instance::finalize(RmgMap & map, vstd::RNG & rng)
  420. {
  421. if(!map.isOnMap(getPosition(true)))
  422. throw rmgException(boost::str(boost::format("Position of object %d at %s is outside the map") % dObject.id % getPosition(true).toString()));
  423. //If no specific template was defined for this object, select any matching
  424. if (!dObject.appearance)
  425. {
  426. const auto * terrainType = map.getTile(getPosition(true)).getTerrain();
  427. auto templates = dObject.getObjectHandler()->getTemplates(terrainType->getId());
  428. if (templates.empty())
  429. {
  430. 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));
  431. }
  432. else
  433. {
  434. setTemplate(terrainType->getId(), rng);
  435. }
  436. }
  437. if (dObject.isVisitable() && !map.isOnMap(dObject.visitablePos()))
  438. 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()));
  439. for(const auto & tile : dObject.getBlockedPos())
  440. {
  441. if(!map.isOnMap(tile))
  442. throw rmgException(boost::str(boost::format("Tile %s of object %d at %s is outside the map") % tile.toString() % dObject.id % dObject.anchorPos().toString()));
  443. }
  444. for(const auto & tile : getBlockedArea().getTilesVector())
  445. {
  446. map.setOccupied(tile, ETileType::USED);
  447. }
  448. map.getMapProxy()->insertObject(&dObject);
  449. }
  450. void Object::finalize(RmgMap & map, vstd::RNG & rng)
  451. {
  452. if(dInstances.empty())
  453. throw rmgException("Cannot finalize object without instances");
  454. for(auto & dInstance : dInstances)
  455. {
  456. dInstance.finalize(map, rng);
  457. }
  458. }
  459. void Object::clearCachedArea() const
  460. {
  461. dFullAreaCache.clear();
  462. dAccessibleAreaCache.clear();
  463. dAccessibleAreaFullCache.clear();
  464. dBlockVisitableCache.clear();
  465. dVisitableCache.clear();
  466. dRemovableAreaCache.clear();
  467. dBorderAboveCache.clear();
  468. }
  469. void Object::clear()
  470. {
  471. for(auto & instance : dInstances)
  472. instance.clear();
  473. dInstances.clear();
  474. cachedInstanceList.clear();
  475. cachedInstanceConstList.clear();
  476. visibleTopOffset.reset();
  477. clearCachedArea();
  478. }
  479. VCMI_LIB_NAMESPACE_END