RmgObject.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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(0)
  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. dPosition = position;
  307. for(auto& i : dInstances)
  308. i.setPositionRaw(i.getPosition());
  309. }
  310. void Object::setTemplate(const TerrainId & terrain, CRandomGenerator & rng)
  311. {
  312. for(auto& i : dInstances)
  313. i.setTemplate(terrain, rng);
  314. visibleTopOffset.reset();
  315. }
  316. const Area & Object::getArea() const
  317. {
  318. if(!dFullAreaCache.empty() || dInstances.empty())
  319. return dFullAreaCache;
  320. for(const auto & instance : dInstances)
  321. {
  322. dFullAreaCache.unite(instance.getBlockedArea());
  323. }
  324. return dFullAreaCache;
  325. }
  326. const int3 Object::getVisibleTop() const
  327. {
  328. if (visibleTopOffset)
  329. {
  330. return dPosition + visibleTopOffset.value();
  331. }
  332. else
  333. {
  334. int3 topTile(-1, 10000, -1); //Start at the bottom
  335. for (const auto& i : dInstances)
  336. {
  337. if (i.getTopTile().y < topTile.y)
  338. {
  339. topTile = i.getTopTile();
  340. }
  341. }
  342. visibleTopOffset = topTile - dPosition;
  343. return topTile;
  344. }
  345. }
  346. bool rmg::Object::isGuarded() const
  347. {
  348. return guarded;
  349. }
  350. void rmg::Object::setGuardedIfMonster(const Instance& object)
  351. {
  352. if (object.object().ID == Obj::MONSTER)
  353. {
  354. guarded = true;
  355. }
  356. }
  357. int3 rmg::Object::getGuardPos() const
  358. {
  359. if (guarded)
  360. {
  361. for (auto & instance : dInstances)
  362. {
  363. if (instance.object().ID == Obj::MONSTER)
  364. {
  365. return instance.getPosition(true);
  366. }
  367. }
  368. }
  369. return int3(-1,-1,-1);
  370. }
  371. void rmg::Object::setValue(size_t newValue)
  372. {
  373. value = newValue;
  374. }
  375. size_t rmg::Object::getValue() const
  376. {
  377. return value;
  378. }
  379. void Object::Instance::finalize(RmgMap & map, CRandomGenerator & rng)
  380. {
  381. if(!map.isOnMap(getPosition(true)))
  382. throw rmgException(boost::str(boost::format("Position of object %d at %s is outside the map") % dObject.id % getPosition(true).toString()));
  383. //If no specific template was defined for this object, select any matching
  384. if (!dObject.appearance)
  385. {
  386. const auto * terrainType = map.getTile(getPosition(true)).terType;
  387. auto templates = dObject.getObjectHandler()->getTemplates(terrainType->getId());
  388. if (templates.empty())
  389. {
  390. 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));
  391. }
  392. else
  393. {
  394. setTemplate(terrainType->getId(), rng);
  395. }
  396. }
  397. if (dObject.isVisitable() && !map.isOnMap(dObject.visitablePos()))
  398. 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()));
  399. for(const auto & tile : dObject.getBlockedPos())
  400. {
  401. if(!map.isOnMap(tile))
  402. throw rmgException(boost::str(boost::format("Tile %s of object %d at %s is outside the map") % tile.toString() % dObject.id % dObject.pos.toString()));
  403. }
  404. for(const auto & tile : getBlockedArea().getTilesVector())
  405. {
  406. map.setOccupied(tile, ETileType::USED);
  407. }
  408. map.getMapProxy()->insertObject(&dObject);
  409. }
  410. void Object::finalize(RmgMap & map, CRandomGenerator & rng)
  411. {
  412. if(dInstances.empty())
  413. throw rmgException("Cannot finalize object without instances");
  414. for(auto & dInstance : dInstances)
  415. {
  416. dInstance.finalize(map, rng);
  417. }
  418. }
  419. void Object::clearCachedArea() const
  420. {
  421. dFullAreaCache.clear();
  422. dAccessibleAreaCache.clear();
  423. dAccessibleAreaFullCache.clear();
  424. dBlockVisitableCache.clear();
  425. dVisitableCache.clear();
  426. dRemovableAreaCache.clear();
  427. }
  428. void Object::clear()
  429. {
  430. for(auto & instance : dInstances)
  431. instance.clear();
  432. dInstances.clear();
  433. cachedInstanceList.clear();
  434. cachedInstanceConstList.clear();
  435. visibleTopOffset.reset();
  436. clearCachedArea();
  437. }
  438. VCMI_LIB_NAMESPACE_END