RmgObject.cpp 13 KB

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