CGObjectInstance.cpp 9.9 KB

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