2
0

CGObjectInstance.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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->getTemplates(tile.terType->getId()).empty())
  116. {
  117. appearance = handler->getTemplates(tile.terType->getId())[0];
  118. }
  119. else
  120. {
  121. logGlobal->warn("Object %d:%d at %s has no templates suitable for terrain %s", newID, newSubID, visitablePos().toString(), tile.terType->getNameTranslated());
  122. appearance = handler->getTemplates()[0]; // get at least some appearance since alternative is crash
  123. }
  124. bool needToAdjustOffset = false;
  125. // FIXME: potentially unused code - setType is NOT called when releasing hero from prison
  126. // instead, appearance update & pos adjustment occurs in GiveHero::applyGs
  127. needToAdjustOffset |= this->ID == Obj::PRISON && newID == Obj::HERO;
  128. needToAdjustOffset |= newID == Obj::MONSTER;
  129. if(needToAdjustOffset)
  130. {
  131. // adjust position since object visitable offset might have changed
  132. auto newOffset = getVisitableOffset();
  133. pos = pos - oldOffset + newOffset;
  134. }
  135. this->ID = Obj(newID);
  136. this->subID = newSubID;
  137. cb->gameState()->map->addBlockVisTiles(this);
  138. }
  139. void CGObjectInstance::pickRandomObject(CRandomGenerator & rand)
  140. {
  141. // no-op
  142. }
  143. void CGObjectInstance::initObj(CRandomGenerator & rand)
  144. {
  145. // no-op
  146. }
  147. void CGObjectInstance::setProperty( ObjProperty what, ObjPropertyID identifier )
  148. {
  149. setPropertyDer(what, identifier); // call this before any actual changes (needed at least for dwellings)
  150. switch(what)
  151. {
  152. case ObjProperty::OWNER:
  153. tempOwner = identifier.as<PlayerColor>();
  154. break;
  155. case ObjProperty::BLOCKVIS:
  156. // Never actually used in code, but possible in ERM
  157. blockVisit = identifier.getNum();
  158. break;
  159. case ObjProperty::ID:
  160. ID = identifier.as<MapObjectID>();
  161. break;
  162. }
  163. }
  164. TObjectTypeHandler CGObjectInstance::getObjectHandler() const
  165. {
  166. return VLC->objtypeh->getHandlerFor(ID, subID);
  167. }
  168. void CGObjectInstance::setPropertyDer( ObjProperty what, ObjPropertyID identifier )
  169. {}
  170. int3 CGObjectInstance::getSightCenter() const
  171. {
  172. return visitablePos();
  173. }
  174. int CGObjectInstance::getSightRadius() const
  175. {
  176. return 3;
  177. }
  178. int3 CGObjectInstance::getVisitableOffset() const
  179. {
  180. return appearance->getVisitableOffset();
  181. }
  182. void CGObjectInstance::giveDummyBonus(const ObjectInstanceID & heroID, BonusDuration::Type duration) const
  183. {
  184. GiveBonus gbonus;
  185. gbonus.bonus.type = BonusType::NONE;
  186. gbonus.id = heroID;
  187. gbonus.bonus.duration = duration;
  188. gbonus.bonus.source = BonusSource::OBJECT_TYPE;
  189. gbonus.bonus.sid = BonusSourceID(ID);
  190. cb->giveHeroBonus(&gbonus);
  191. }
  192. std::string CGObjectInstance::getObjectName() const
  193. {
  194. return VLC->objtypeh->getObjectName(ID, subID);
  195. }
  196. std::optional<AudioPath> CGObjectInstance::getAmbientSound() const
  197. {
  198. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).ambient;
  199. if(!sounds.empty())
  200. return sounds.front(); // TODO: Support randomization of ambient sounds
  201. return std::nullopt;
  202. }
  203. std::optional<AudioPath> CGObjectInstance::getVisitSound() const
  204. {
  205. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).visit;
  206. if(!sounds.empty())
  207. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  208. return std::nullopt;
  209. }
  210. std::optional<AudioPath> CGObjectInstance::getRemovalSound() const
  211. {
  212. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).removal;
  213. if(!sounds.empty())
  214. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  215. return std::nullopt;
  216. }
  217. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  218. {
  219. auto text = getObjectName();
  220. if (tempOwner.isValidPlayer())
  221. text += "\n" + VLC->generaltexth->arraytxt[23 + tempOwner.getNum()];
  222. return text;
  223. }
  224. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  225. {
  226. return getHoverText(hero->tempOwner);
  227. }
  228. std::string CGObjectInstance::getPopupText(PlayerColor player) const
  229. {
  230. return getHoverText(player);
  231. }
  232. std::string CGObjectInstance::getPopupText(const CGHeroInstance * hero) const
  233. {
  234. return getHoverText(hero);
  235. }
  236. std::vector<Component> CGObjectInstance::getPopupComponents(PlayerColor player) const
  237. {
  238. return {};
  239. }
  240. std::vector<Component> CGObjectInstance::getPopupComponents(const CGHeroInstance * hero) const
  241. {
  242. return getPopupComponents(hero->getOwner());
  243. }
  244. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  245. {
  246. switch(ID.toEnum())
  247. {
  248. case Obj::SANCTUARY:
  249. {
  250. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  251. h->showInfoDialog(114);
  252. }
  253. break;
  254. case Obj::TAVERN:
  255. {
  256. cb->showObjectWindow(this, EOpenWindowMode::TAVERN_WINDOW, h, true);
  257. }
  258. break;
  259. }
  260. }
  261. int3 CGObjectInstance::visitablePos() const
  262. {
  263. return pos - getVisitableOffset();
  264. }
  265. bool CGObjectInstance::isVisitable() const
  266. {
  267. return appearance->isVisitable();
  268. }
  269. bool CGObjectInstance::isBlockedVisitable() const
  270. {
  271. // TODO: Read from json
  272. return blockVisit;
  273. }
  274. bool CGObjectInstance::isRemovable() const
  275. {
  276. // TODO: Read from json
  277. return removable;
  278. }
  279. bool CGObjectInstance::isCoastVisitable() const
  280. {
  281. return false;
  282. }
  283. bool CGObjectInstance::passableFor(PlayerColor color) const
  284. {
  285. return false;
  286. }
  287. void CGObjectInstance::updateFrom(const JsonNode & data)
  288. {
  289. }
  290. void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
  291. {
  292. //only save here, loading is handled by map loader
  293. if(handler.saving)
  294. {
  295. handler.serializeString("type", typeName);
  296. handler.serializeString("subtype", subTypeName);
  297. handler.serializeInt("x", pos.x);
  298. handler.serializeInt("y", pos.y);
  299. handler.serializeInt("l", pos.z);
  300. JsonNode app;
  301. appearance->writeJson(app, false);
  302. handler.serializeRaw("template", app, std::nullopt);
  303. }
  304. {
  305. auto options = handler.enterStruct("options");
  306. serializeJsonOptions(handler);
  307. }
  308. }
  309. void CGObjectInstance::afterAddToMap(CMap * map)
  310. {
  311. //nothing here
  312. }
  313. void CGObjectInstance::afterRemoveFromMap(CMap * map)
  314. {
  315. //nothing here
  316. }
  317. void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  318. {
  319. //nothing here
  320. }
  321. void CGObjectInstance::serializeJsonOwner(JsonSerializeFormat & handler)
  322. {
  323. handler.serializeId("owner", tempOwner, PlayerColor::NEUTRAL);
  324. }
  325. BattleField CGObjectInstance::getBattlefield() const
  326. {
  327. return VLC->objtypeh->getHandlerFor(ID, subID)->getBattlefield();
  328. }
  329. VCMI_LIB_NAMESPACE_END