CGObjectInstance.cpp 10 KB

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