CGObjectInstance.cpp 8.9 KB

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