CGObjectInstance.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. const 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( ObjProperty what, ObjPropertyID identifier )
  156. {
  157. setPropertyDer(what, identifier); // call this before any actual changes (needed at least for dwellings)
  158. switch(what)
  159. {
  160. case ObjProperty::OWNER:
  161. tempOwner = identifier.as<PlayerColor>();
  162. break;
  163. case ObjProperty::BLOCKVIS:
  164. blockVisit = identifier.getNum();
  165. break;
  166. case ObjProperty::ID:
  167. ID = identifier.as<MapObjectID>();
  168. break;
  169. }
  170. }
  171. TObjectTypeHandler CGObjectInstance::getObjectHandler() const
  172. {
  173. return VLC->objtypeh->getHandlerFor(ID, subID);
  174. }
  175. void CGObjectInstance::setPropertyDer( ObjProperty what, ObjPropertyID identifier )
  176. {}
  177. int3 CGObjectInstance::getSightCenter() const
  178. {
  179. return visitablePos();
  180. }
  181. int CGObjectInstance::getSightRadius() const
  182. {
  183. return 3;
  184. }
  185. int3 CGObjectInstance::getVisitableOffset() const
  186. {
  187. return appearance->getVisitableOffset();
  188. }
  189. void CGObjectInstance::giveDummyBonus(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. cb->giveHeroBonus(&gbonus);
  198. }
  199. std::string CGObjectInstance::getObjectName() const
  200. {
  201. return VLC->objtypeh->getObjectName(ID, subID);
  202. }
  203. std::optional<AudioPath> CGObjectInstance::getAmbientSound() const
  204. {
  205. const auto & sounds = VLC->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() const
  211. {
  212. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).visit;
  213. if(!sounds.empty())
  214. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  215. return std::nullopt;
  216. }
  217. std::optional<AudioPath> CGObjectInstance::getRemovalSound() const
  218. {
  219. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).removal;
  220. if(!sounds.empty())
  221. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  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" + VLC->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( 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(114);
  259. }
  260. break;
  261. case Obj::TAVERN:
  262. {
  263. cb->showObjectWindow(this, EOpenWindowMode::TAVERN_WINDOW, h, true);
  264. }
  265. break;
  266. }
  267. }
  268. int3 CGObjectInstance::visitablePos() const
  269. {
  270. return pos - getVisitableOffset();
  271. }
  272. bool CGObjectInstance::isVisitable() const
  273. {
  274. return appearance->isVisitable();
  275. }
  276. bool CGObjectInstance::isBlockedVisitable() const
  277. {
  278. return blockVisit;
  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. VCMI_LIB_NAMESPACE_END