2
0

CGObjectInstance.cpp 9.4 KB

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