CGObjectInstance.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 "../callback/IGameCallback.h"
  15. #include "../gameState/CGameState.h"
  16. #include "../texts/CGeneralTextHandler.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 = appearance->getCornerOffset();
  104. auto &tile = cb->gameState().getMap().getTile(position);
  105. //recalculate blockvis tiles - new appearance might have different blockmap than before
  106. cb->gameState().getMap().hideObject(this);
  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. needToAdjustOffset |= newID == Obj::CREATURE_GENERATOR1 || newID == Obj::CREATURE_GENERATOR2 || newID == Obj::CREATURE_GENERATOR3 || newID == Obj::CREATURE_GENERATOR4;
  123. if(needToAdjustOffset)
  124. {
  125. // adjust position since object visitable offset might have changed
  126. auto newOffset = appearance->getCornerOffset();
  127. pos = pos - oldOffset + newOffset;
  128. }
  129. this->ID = Obj(newID);
  130. this->subID = newSubID;
  131. cb->gameState().getMap().showObject(this);
  132. }
  133. void CGObjectInstance::pickRandomObject(vstd::RNG & rand)
  134. {
  135. // no-op
  136. }
  137. void CGObjectInstance::initObj(vstd::RNG & rand)
  138. {
  139. // no-op
  140. }
  141. void CGObjectInstance::setProperty( ObjProperty what, ObjPropertyID identifier )
  142. {
  143. setPropertyDer(what, identifier); // call this before any actual changes (needed at least for dwellings)
  144. switch(what)
  145. {
  146. case ObjProperty::OWNER:
  147. tempOwner = identifier.as<PlayerColor>();
  148. break;
  149. case ObjProperty::BLOCKVIS:
  150. // Never actually used in code, but possible in ERM
  151. blockVisit = identifier.getNum();
  152. break;
  153. case ObjProperty::ID:
  154. ID = identifier.as<MapObjectID>();
  155. break;
  156. }
  157. }
  158. TObjectTypeHandler CGObjectInstance::getObjectHandler() const
  159. {
  160. return LIBRARY->objtypeh->getHandlerFor(ID, subID);
  161. }
  162. std::string CGObjectInstance::getTypeName() const
  163. {
  164. return getObjectHandler()->getTypeName();
  165. }
  166. std::string CGObjectInstance::getSubtypeName() const
  167. {
  168. return getObjectHandler()->getSubTypeName();
  169. }
  170. void CGObjectInstance::setPropertyDer( ObjProperty what, ObjPropertyID identifier )
  171. {}
  172. int3 CGObjectInstance::getSightCenter() const
  173. {
  174. return visitablePos();
  175. }
  176. int CGObjectInstance::getSightRadius() const
  177. {
  178. return 3;
  179. }
  180. int3 CGObjectInstance::getVisitableOffset() const
  181. {
  182. if (!isVisitable())
  183. logGlobal->debug("Attempt to access visitable offset on a non-visitable object!");
  184. return appearance->getVisitableOffset();
  185. }
  186. void CGObjectInstance::giveDummyBonus(const ObjectInstanceID & heroID, BonusDuration::Type duration) const
  187. {
  188. GiveBonus gbonus;
  189. gbonus.bonus.type = BonusType::NONE;
  190. gbonus.id = heroID;
  191. gbonus.bonus.duration = duration;
  192. gbonus.bonus.source = BonusSource::OBJECT_TYPE;
  193. gbonus.bonus.sid = BonusSourceID(ID);
  194. cb->giveHeroBonus(&gbonus);
  195. }
  196. std::string CGObjectInstance::getObjectName() const
  197. {
  198. return LIBRARY->objtypeh->getObjectName(ID, subID);
  199. }
  200. std::optional<AudioPath> CGObjectInstance::getAmbientSound(vstd::RNG & rng) const
  201. {
  202. const auto & sounds = LIBRARY->objtypeh->getObjectSounds(ID, subID).ambient;
  203. if(!sounds.empty())
  204. return sounds.front(); // TODO: Support randomization of ambient sounds
  205. return std::nullopt;
  206. }
  207. std::optional<AudioPath> CGObjectInstance::getVisitSound(vstd::RNG & rng) const
  208. {
  209. const auto & sounds = LIBRARY->objtypeh->getObjectSounds(ID, subID).visit;
  210. if(!sounds.empty())
  211. return *RandomGeneratorUtil::nextItem(sounds, rng);
  212. return std::nullopt;
  213. }
  214. std::optional<AudioPath> CGObjectInstance::getRemovalSound(vstd::RNG & rng) const
  215. {
  216. const auto & sounds = LIBRARY->objtypeh->getObjectSounds(ID, subID).removal;
  217. if(!sounds.empty())
  218. return *RandomGeneratorUtil::nextItem(sounds, rng);
  219. return std::nullopt;
  220. }
  221. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  222. {
  223. auto text = getObjectName();
  224. if (tempOwner.isValidPlayer())
  225. text += "\n" + LIBRARY->generaltexth->arraytxt[23 + tempOwner.getNum()];
  226. return text;
  227. }
  228. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  229. {
  230. return getHoverText(hero->tempOwner);
  231. }
  232. std::string CGObjectInstance::getPopupText(PlayerColor player) const
  233. {
  234. return getHoverText(player);
  235. }
  236. std::string CGObjectInstance::getPopupText(const CGHeroInstance * hero) const
  237. {
  238. return getHoverText(hero);
  239. }
  240. std::vector<Component> CGObjectInstance::getPopupComponents(PlayerColor player) const
  241. {
  242. return {};
  243. }
  244. std::vector<Component> CGObjectInstance::getPopupComponents(const CGHeroInstance * hero) const
  245. {
  246. return getPopupComponents(hero->getOwner());
  247. }
  248. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  249. {
  250. switch(ID.toEnum())
  251. {
  252. case Obj::SANCTUARY:
  253. {
  254. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  255. h->showInfoDialog(114);
  256. }
  257. break;
  258. case Obj::TAVERN:
  259. {
  260. cb->showObjectWindow(this, EOpenWindowMode::TAVERN_WINDOW, h, true);
  261. }
  262. break;
  263. }
  264. }
  265. int3 CGObjectInstance::visitablePos() const
  266. {
  267. if (!isVisitable())
  268. logGlobal->debug("Attempt to access visitable position on a non-visitable object!");
  269. return pos - getVisitableOffset();
  270. }
  271. bool CGObjectInstance::isVisitable() const
  272. {
  273. return appearance->isVisitable();
  274. }
  275. bool CGObjectInstance::isBlockedVisitable() const
  276. {
  277. // TODO: Read from json
  278. return blockVisit;
  279. }
  280. bool CGObjectInstance::isRemovable() const
  281. {
  282. // TODO: Read from json
  283. return removable;
  284. }
  285. bool CGObjectInstance::isCoastVisitable() const
  286. {
  287. return false;
  288. }
  289. bool CGObjectInstance::passableFor(PlayerColor color) const
  290. {
  291. return false;
  292. }
  293. void CGObjectInstance::updateFrom(const JsonNode & data)
  294. {
  295. }
  296. void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
  297. {
  298. //only save here, loading is handled by map loader
  299. if(handler.saving)
  300. {
  301. std::string ourTypeName = getTypeName();
  302. std::string ourSubtypeName = getSubtypeName();
  303. handler.serializeString("type", ourTypeName);
  304. handler.serializeString("subtype", ourSubtypeName);
  305. handler.serializeInt("x", pos.x);
  306. handler.serializeInt("y", pos.y);
  307. handler.serializeInt("l", pos.z);
  308. JsonNode app;
  309. appearance->writeJson(app, false);
  310. handler.serializeRaw("template", app, std::nullopt);
  311. }
  312. {
  313. auto options = handler.enterStruct("options");
  314. serializeJsonOptions(handler);
  315. }
  316. }
  317. void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  318. {
  319. //nothing here
  320. }
  321. void CGObjectInstance::serializeJsonOwner(JsonSerializeFormat & handler)
  322. {
  323. handler.serializeId("owner", tempOwner, PlayerColor::NEUTRAL);
  324. }
  325. BattleField CGObjectInstance::getBattlefield() const
  326. {
  327. return LIBRARY->objtypeh->getHandlerFor(ID, subID)->getBattlefield();
  328. }
  329. const IOwnableObject * CGObjectInstance::asOwnable() const
  330. {
  331. return nullptr;
  332. }
  333. VCMI_LIB_NAMESPACE_END