RmgObject.cpp 13 KB

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