CGObjectInstance.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 "../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. 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. MapObjectID CGObjectInstance::getObjGroupIndex() const
  37. {
  38. return ID;
  39. }
  40. MapObjectSubID 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(MapObjectID newID, MapObjectSubID 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::pickRandomObject(CRandomGenerator & rand)
  143. {
  144. // no-op
  145. }
  146. void CGObjectInstance::initObj(CRandomGenerator & rand)
  147. {
  148. switch(ID.toEnum())
  149. {
  150. case Obj::TAVERN:
  151. blockVisit = true;
  152. break;
  153. }
  154. }
  155. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  156. {
  157. setPropertyDer(what, val); // call this before any actual changes (needed at least for dwellings)
  158. switch(what)
  159. {
  160. case ObjProperty::OWNER:
  161. tempOwner = PlayerColor(val);
  162. break;
  163. case ObjProperty::BLOCKVIS:
  164. blockVisit = val;
  165. break;
  166. case ObjProperty::ID:
  167. ID = Obj(val);
  168. break;
  169. case ObjProperty::SUBID:
  170. subID = val;
  171. break;
  172. }
  173. }
  174. TObjectTypeHandler CGObjectInstance::getObjectHandler() const
  175. {
  176. return VLC->objtypeh->getHandlerFor(ID, subID);
  177. }
  178. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  179. {}
  180. int3 CGObjectInstance::getSightCenter() const
  181. {
  182. return visitablePos();
  183. }
  184. int CGObjectInstance::getSightRadius() const
  185. {
  186. return 3;
  187. }
  188. int3 CGObjectInstance::getVisitableOffset() const
  189. {
  190. return appearance->getVisitableOffset();
  191. }
  192. void CGObjectInstance::giveDummyBonus(const ObjectInstanceID & heroID, BonusDuration::Type duration) const
  193. {
  194. GiveBonus gbonus;
  195. gbonus.bonus.type = BonusType::NONE;
  196. gbonus.id = heroID.getNum();
  197. gbonus.bonus.duration = duration;
  198. gbonus.bonus.source = BonusSource::OBJECT_TYPE;
  199. gbonus.bonus.sid = BonusSourceID(ID);
  200. cb->giveHeroBonus(&gbonus);
  201. }
  202. std::string CGObjectInstance::getObjectName() const
  203. {
  204. return VLC->objtypeh->getObjectName(ID, subID);
  205. }
  206. std::optional<AudioPath> CGObjectInstance::getAmbientSound() const
  207. {
  208. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).ambient;
  209. if(!sounds.empty())
  210. return sounds.front(); // TODO: Support randomization of ambient sounds
  211. return std::nullopt;
  212. }
  213. std::optional<AudioPath> CGObjectInstance::getVisitSound() const
  214. {
  215. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).visit;
  216. if(!sounds.empty())
  217. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  218. return std::nullopt;
  219. }
  220. std::optional<AudioPath> CGObjectInstance::getRemovalSound() const
  221. {
  222. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).removal;
  223. if(!sounds.empty())
  224. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  225. return std::nullopt;
  226. }
  227. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  228. {
  229. auto text = getObjectName();
  230. if (tempOwner.isValidPlayer())
  231. text += "\n" + VLC->generaltexth->arraytxt[23 + tempOwner.getNum()];
  232. return text;
  233. }
  234. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  235. {
  236. return getHoverText(hero->tempOwner);
  237. }
  238. std::string CGObjectInstance::getPopupText(PlayerColor player) const
  239. {
  240. return getHoverText(player);
  241. }
  242. std::string CGObjectInstance::getPopupText(const CGHeroInstance * hero) const
  243. {
  244. return getHoverText(hero);
  245. }
  246. std::vector<Component> CGObjectInstance::getPopupComponents(PlayerColor player) const
  247. {
  248. return {};
  249. }
  250. std::vector<Component> CGObjectInstance::getPopupComponents(const CGHeroInstance * hero) const
  251. {
  252. return getPopupComponents(hero->getOwner());
  253. }
  254. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  255. {
  256. switch(ID.toEnum())
  257. {
  258. case Obj::SANCTUARY:
  259. {
  260. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  261. h->showInfoDialog(114);
  262. }
  263. break;
  264. case Obj::TAVERN:
  265. {
  266. cb->showObjectWindow(this, EOpenWindowMode::TAVERN_WINDOW, h, true);
  267. }
  268. break;
  269. }
  270. }
  271. int3 CGObjectInstance::visitablePos() const
  272. {
  273. return pos - getVisitableOffset();
  274. }
  275. bool CGObjectInstance::isVisitable() const
  276. {
  277. return appearance->isVisitable();
  278. }
  279. bool CGObjectInstance::isBlockedVisitable() const
  280. {
  281. return blockVisit;
  282. }
  283. bool CGObjectInstance::isCoastVisitable() const
  284. {
  285. return false;
  286. }
  287. bool CGObjectInstance::passableFor(PlayerColor color) const
  288. {
  289. return false;
  290. }
  291. void CGObjectInstance::updateFrom(const JsonNode & data)
  292. {
  293. }
  294. void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
  295. {
  296. //only save here, loading is handled by map loader
  297. if(handler.saving)
  298. {
  299. handler.serializeString("type", typeName);
  300. handler.serializeString("subtype", subTypeName);
  301. handler.serializeInt("x", pos.x);
  302. handler.serializeInt("y", pos.y);
  303. handler.serializeInt("l", pos.z);
  304. JsonNode app;
  305. appearance->writeJson(app, false);
  306. handler.serializeRaw("template", app, std::nullopt);
  307. }
  308. {
  309. auto options = handler.enterStruct("options");
  310. serializeJsonOptions(handler);
  311. }
  312. }
  313. void CGObjectInstance::afterAddToMap(CMap * map)
  314. {
  315. //nothing here
  316. }
  317. void CGObjectInstance::afterRemoveFromMap(CMap * map)
  318. {
  319. //nothing here
  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. return VLC->objtypeh->getHandlerFor(ID, subID)->getBattlefield();
  332. }
  333. VCMI_LIB_NAMESPACE_END