CGObjectInstance.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * CObjectHandler.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 "CGObjectInstance.h"
  12. #include "CGHeroInstance.h"
  13. #include "ObjectTemplate.h"
  14. #include "../gameState/CGameState.h"
  15. #include "../texts/CGeneralTextHandler.h"
  16. #include "../IGameCallback.h"
  17. #include "../constants/StringConstants.h"
  18. #include "../TerrainHandler.h"
  19. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  20. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  21. #include "../mapping/CMap.h"
  22. #include "../networkPacks/PacksForClient.h"
  23. #include "../serializer/JsonSerializeFormat.h"
  24. #include <vstd/RNG.h>
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. //TODO: remove constructor
  27. CGObjectInstance::CGObjectInstance(IGameCallback *cb):
  28. IObjectInterface(cb),
  29. pos(-1,-1,-1),
  30. ID(Obj::NO_OBJ),
  31. subID(-1),
  32. tempOwner(PlayerColor::UNFLAGGABLE),
  33. blockVisit(false),
  34. removable(false)
  35. {
  36. }
  37. //must be instantiated in .cpp file for access to complete types of all member fields
  38. CGObjectInstance::~CGObjectInstance() = default;
  39. MapObjectID CGObjectInstance::getObjGroupIndex() const
  40. {
  41. return ID;
  42. }
  43. MapObjectSubID CGObjectInstance::getObjTypeIndex() const
  44. {
  45. return subID;
  46. }
  47. int3 CGObjectInstance::anchorPos() const
  48. {
  49. return pos;
  50. }
  51. int3 CGObjectInstance::getTopVisiblePos() const
  52. {
  53. return anchorPos() - appearance->getTopVisibleOffset();
  54. }
  55. void CGObjectInstance::setOwner(const PlayerColor & ow)
  56. {
  57. tempOwner = ow;
  58. }
  59. void CGObjectInstance::setAnchorPos(int3 newPos)
  60. {
  61. pos = newPos;
  62. }
  63. int CGObjectInstance::getWidth() const
  64. {
  65. return appearance->getWidth();
  66. }
  67. int CGObjectInstance::getHeight() const
  68. {
  69. return appearance->getHeight();
  70. }
  71. bool CGObjectInstance::visitableAt(const int3 & testPos) const
  72. {
  73. return anchorPos().z == testPos.z && appearance->isVisitableAt(anchorPos().x - testPos.x, anchorPos().y - testPos.y);
  74. }
  75. bool CGObjectInstance::blockingAt(const int3 & testPos) const
  76. {
  77. return anchorPos().z == testPos.z && appearance->isBlockedAt(anchorPos().x - testPos.x, anchorPos().y - testPos.y);
  78. }
  79. bool CGObjectInstance::coveringAt(const int3 & testPos) const
  80. {
  81. return anchorPos().z == testPos.z && appearance->isVisibleAt(anchorPos().x - testPos.x, anchorPos().y - testPos.y);
  82. }
  83. std::set<int3> CGObjectInstance::getBlockedPos() const
  84. {
  85. std::set<int3> ret;
  86. for(int w=0; w<getWidth(); ++w)
  87. {
  88. for(int h=0; h<getHeight(); ++h)
  89. {
  90. if(appearance->isBlockedAt(w, h))
  91. ret.insert(int3(anchorPos().x - w, anchorPos().y - h, anchorPos().z));
  92. }
  93. }
  94. return ret;
  95. }
  96. const std::set<int3> & CGObjectInstance::getBlockedOffsets() const
  97. {
  98. return appearance->getBlockedOffsets();
  99. }
  100. void CGObjectInstance::setType(MapObjectID newID, MapObjectSubID newSubID)
  101. {
  102. auto position = visitablePos();
  103. auto oldOffset = getVisitableOffset();
  104. auto &tile = cb->gameState()->getMap().getTile(position);
  105. //recalculate blockvis tiles - new appearance might have different blockmap than before
  106. cb->gameState()->getMap().removeBlockVisTiles(this, true);
  107. auto handler = LIBRARY->objtypeh->getHandlerFor(newID, newSubID);
  108. if(!handler->getTemplates(tile.getTerrainID()).empty())
  109. {
  110. appearance = handler->getTemplates(tile.getTerrainID())[0];
  111. }
  112. else
  113. {
  114. logGlobal->warn("Object %d:%d at %s has no templates suitable for terrain %s", newID, newSubID, visitablePos().toString(), tile.getTerrain()->getNameTranslated());
  115. appearance = handler->getTemplates()[0]; // get at least some appearance since alternative is crash
  116. }
  117. bool needToAdjustOffset = false;
  118. // FIXME: potentially unused code - setType is NOT called when releasing hero from prison
  119. // instead, appearance update & pos adjustment occurs in GiveHero::applyGs
  120. needToAdjustOffset |= this->ID == Obj::PRISON && newID == Obj::HERO;
  121. needToAdjustOffset |= newID == Obj::MONSTER;
  122. if(needToAdjustOffset)
  123. {
  124. // adjust position since object visitable offset might have changed
  125. auto newOffset = getVisitableOffset();
  126. pos = pos - oldOffset + newOffset;
  127. }
  128. this->ID = Obj(newID);
  129. this->subID = newSubID;
  130. cb->gameState()->getMap().addBlockVisTiles(this);
  131. }
  132. void CGObjectInstance::pickRandomObject(vstd::RNG & rand)
  133. {
  134. // no-op
  135. }
  136. void CGObjectInstance::initObj(vstd::RNG & rand)
  137. {
  138. // no-op
  139. }
  140. void CGObjectInstance::setProperty( ObjProperty what, ObjPropertyID identifier )
  141. {
  142. setPropertyDer(what, identifier); // call this before any actual changes (needed at least for dwellings)
  143. switch(what)
  144. {
  145. case ObjProperty::OWNER:
  146. tempOwner = identifier.as<PlayerColor>();
  147. break;
  148. case ObjProperty::BLOCKVIS:
  149. // Never actually used in code, but possible in ERM
  150. blockVisit = identifier.getNum();
  151. break;
  152. case ObjProperty::ID:
  153. ID = identifier.as<MapObjectID>();
  154. break;
  155. }
  156. }
  157. TObjectTypeHandler CGObjectInstance::getObjectHandler() const
  158. {
  159. return LIBRARY->objtypeh->getHandlerFor(ID, subID);
  160. }
  161. std::string CGObjectInstance::getTypeName() const
  162. {
  163. return getObjectHandler()->getTypeName();
  164. }
  165. std::string CGObjectInstance::getSubtypeName() const
  166. {
  167. return getObjectHandler()->getSubTypeName();
  168. }
  169. void CGObjectInstance::setPropertyDer( ObjProperty what, ObjPropertyID identifier )
  170. {}
  171. int3 CGObjectInstance::getSightCenter() const
  172. {
  173. return visitablePos();
  174. }
  175. int CGObjectInstance::getSightRadius() const
  176. {
  177. return 3;
  178. }
  179. int3 CGObjectInstance::getVisitableOffset() const
  180. {
  181. if (!isVisitable())
  182. logGlobal->debug("Attempt to access visitable offset on a non-visitable object!");
  183. return appearance->getVisitableOffset();
  184. }
  185. void CGObjectInstance::giveDummyBonus(const ObjectInstanceID & heroID, BonusDuration::Type duration) const
  186. {
  187. GiveBonus gbonus;
  188. gbonus.bonus.type = BonusType::NONE;
  189. gbonus.id = heroID;
  190. gbonus.bonus.duration = duration;
  191. gbonus.bonus.source = BonusSource::OBJECT_TYPE;
  192. gbonus.bonus.sid = BonusSourceID(ID);
  193. cb->giveHeroBonus(&gbonus);
  194. }
  195. std::string CGObjectInstance::getObjectName() const
  196. {
  197. return LIBRARY->objtypeh->getObjectName(ID, subID);
  198. }
  199. std::optional<AudioPath> CGObjectInstance::getAmbientSound(vstd::RNG & rng) const
  200. {
  201. const auto & sounds = LIBRARY->objtypeh->getObjectSounds(ID, subID).ambient;
  202. if(!sounds.empty())
  203. return sounds.front(); // TODO: Support randomization of ambient sounds
  204. return std::nullopt;
  205. }
  206. std::optional<AudioPath> CGObjectInstance::getVisitSound(vstd::RNG & rng) const
  207. {
  208. const auto & sounds = LIBRARY->objtypeh->getObjectSounds(ID, subID).visit;
  209. if(!sounds.empty())
  210. return *RandomGeneratorUtil::nextItem(sounds, rng);
  211. return std::nullopt;
  212. }
  213. std::optional<AudioPath> CGObjectInstance::getRemovalSound(vstd::RNG & rng) const
  214. {
  215. const auto & sounds = LIBRARY->objtypeh->getObjectSounds(ID, subID).removal;
  216. if(!sounds.empty())
  217. return *RandomGeneratorUtil::nextItem(sounds, rng);
  218. return std::nullopt;
  219. }
  220. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  221. {
  222. auto text = getObjectName();
  223. if (tempOwner.isValidPlayer())
  224. text += "\n" + LIBRARY->generaltexth->arraytxt[23 + tempOwner.getNum()];
  225. return text;
  226. }
  227. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  228. {
  229. return getHoverText(hero->tempOwner);
  230. }
  231. std::string CGObjectInstance::getPopupText(PlayerColor player) const
  232. {
  233. return getHoverText(player);
  234. }
  235. std::string CGObjectInstance::getPopupText(const CGHeroInstance * hero) const
  236. {
  237. return getHoverText(hero);
  238. }
  239. std::vector<Component> CGObjectInstance::getPopupComponents(PlayerColor player) const
  240. {
  241. return {};
  242. }
  243. std::vector<Component> CGObjectInstance::getPopupComponents(const CGHeroInstance * hero) const
  244. {
  245. return getPopupComponents(hero->getOwner());
  246. }
  247. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  248. {
  249. switch(ID.toEnum())
  250. {
  251. case Obj::SANCTUARY:
  252. {
  253. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  254. h->showInfoDialog(114);
  255. }
  256. break;
  257. case Obj::TAVERN:
  258. {
  259. cb->showObjectWindow(this, EOpenWindowMode::TAVERN_WINDOW, h, true);
  260. }
  261. break;
  262. }
  263. }
  264. int3 CGObjectInstance::visitablePos() const
  265. {
  266. if (!isVisitable())
  267. logGlobal->debug("Attempt to access visitable position on a non-visitable object!");
  268. return pos - getVisitableOffset();
  269. }
  270. bool CGObjectInstance::isVisitable() const
  271. {
  272. return appearance->isVisitable();
  273. }
  274. bool CGObjectInstance::isBlockedVisitable() const
  275. {
  276. // TODO: Read from json
  277. return blockVisit;
  278. }
  279. bool CGObjectInstance::isRemovable() const
  280. {
  281. // TODO: Read from json
  282. return removable;
  283. }
  284. bool CGObjectInstance::isCoastVisitable() const
  285. {
  286. return false;
  287. }
  288. bool CGObjectInstance::passableFor(PlayerColor color) const
  289. {
  290. return false;
  291. }
  292. void CGObjectInstance::updateFrom(const JsonNode & data)
  293. {
  294. }
  295. void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
  296. {
  297. //only save here, loading is handled by map loader
  298. if(handler.saving)
  299. {
  300. std::string ourTypeName = getTypeName();
  301. std::string ourSubtypeName = getSubtypeName();
  302. handler.serializeString("type", ourTypeName);
  303. handler.serializeString("subtype", ourSubtypeName);
  304. handler.serializeInt("x", pos.x);
  305. handler.serializeInt("y", pos.y);
  306. handler.serializeInt("l", pos.z);
  307. JsonNode app;
  308. appearance->writeJson(app, false);
  309. handler.serializeRaw("template", app, std::nullopt);
  310. }
  311. {
  312. auto options = handler.enterStruct("options");
  313. serializeJsonOptions(handler);
  314. }
  315. }
  316. void CGObjectInstance::afterAddToMap(CMap * map)
  317. {
  318. //nothing here
  319. }
  320. void CGObjectInstance::afterRemoveFromMap(CMap * map)
  321. {
  322. //nothing here
  323. }
  324. void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  325. {
  326. //nothing here
  327. }
  328. void CGObjectInstance::serializeJsonOwner(JsonSerializeFormat & handler)
  329. {
  330. handler.serializeId("owner", tempOwner, PlayerColor::NEUTRAL);
  331. }
  332. BattleField CGObjectInstance::getBattlefield() const
  333. {
  334. return LIBRARY->objtypeh->getHandlerFor(ID, subID)->getBattlefield();
  335. }
  336. const IOwnableObject * CGObjectInstance::asOwnable() const
  337. {
  338. return nullptr;
  339. }
  340. VCMI_LIB_NAMESPACE_END