CGObjectInstance.cpp 9.4 KB

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