CGObjectInstance.cpp 9.4 KB

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