CGObjectInstance.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "../CGameState.h"
  15. #include "../CGeneralTextHandler.h"
  16. #include "../IGameCallback.h"
  17. #include "../NetPacks.h"
  18. #include "../StringConstants.h"
  19. #include "../TerrainHandler.h"
  20. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  21. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  22. #include "../mapping/CMap.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. int32_t CGObjectInstance::getObjGroupIndex() const
  37. {
  38. return ID.num;
  39. }
  40. int32_t 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//returns width of object graphic in tiles
  57. {
  58. return appearance->getWidth();
  59. }
  60. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  61. {
  62. return appearance->getHeight();
  63. }
  64. bool CGObjectInstance::visitableAt(int x, int y) const //returns true if object is visitable at location (x, y) form left top tile of image (x, y in tiles)
  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. std::set<int3> CGObjectInstance::getBlockedPos() const
  77. {
  78. std::set<int3> ret;
  79. for(int w=0; w<getWidth(); ++w)
  80. {
  81. for(int h=0; h<getHeight(); ++h)
  82. {
  83. if(appearance->isBlockedAt(w, h))
  84. ret.insert(int3(pos.x - w, pos.y - h, pos.z));
  85. }
  86. }
  87. return ret;
  88. }
  89. std::set<int3> CGObjectInstance::getBlockedOffsets() const
  90. {
  91. return appearance->getBlockedOffsets();
  92. }
  93. void CGObjectInstance::setType(si32 newID, si32 newSubID)
  94. {
  95. auto position = visitablePos();
  96. auto oldOffset = getVisitableOffset();
  97. auto &tile = cb->gameState()->map->getTile(position);
  98. //recalculate blockvis tiles - new appearance might have different blockmap than before
  99. cb->gameState()->map->removeBlockVisTiles(this, true);
  100. auto handler = VLC->objtypeh->getHandlerFor(newID, newSubID);
  101. if(!handler)
  102. {
  103. logGlobal->error("Unknown object type %d:%d at %s", newID, newSubID, visitablePos().toString());
  104. return;
  105. }
  106. if(!handler->getTemplates(tile.terType->getId()).empty())
  107. {
  108. appearance = handler->getTemplates(tile.terType->getId())[0];
  109. }
  110. else
  111. {
  112. logGlobal->warn("Object %d:%d at %s has no templates suitable for terrain %s", newID, newSubID, visitablePos().toString(), tile.terType->getNameTranslated());
  113. appearance = handler->getTemplates()[0]; // get at least some appearance since alternative is crash
  114. }
  115. bool needToAdjustOffset = false;
  116. // FIXME: potentially unused code - setType is NOT called when releasing hero from prison
  117. // instead, appearance update & pos adjustment occurs in GiveHero::applyGs
  118. needToAdjustOffset |= this->ID == Obj::PRISON && newID == Obj::HERO;
  119. needToAdjustOffset |= newID == Obj::MONSTER;
  120. if(needToAdjustOffset)
  121. {
  122. // adjust position since object visitable offset might have changed
  123. auto newOffset = getVisitableOffset();
  124. pos = pos - oldOffset + newOffset;
  125. }
  126. this->ID = Obj(newID);
  127. this->subID = newSubID;
  128. cb->gameState()->map->addBlockVisTiles(this);
  129. }
  130. void CGObjectInstance::initObj(CRandomGenerator & rand)
  131. {
  132. switch(ID)
  133. {
  134. case Obj::TAVERN:
  135. blockVisit = true;
  136. break;
  137. }
  138. }
  139. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  140. {
  141. setPropertyDer(what, val); // call this before any actual changes (needed at least for dwellings)
  142. switch(what)
  143. {
  144. case ObjProperty::OWNER:
  145. tempOwner = PlayerColor(val);
  146. break;
  147. case ObjProperty::BLOCKVIS:
  148. blockVisit = val;
  149. break;
  150. case ObjProperty::ID:
  151. ID = Obj(val);
  152. break;
  153. case ObjProperty::SUBID:
  154. subID = val;
  155. break;
  156. }
  157. }
  158. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  159. {}
  160. int3 CGObjectInstance::getSightCenter() const
  161. {
  162. return visitablePos();
  163. }
  164. int CGObjectInstance::getSightRadius() const
  165. {
  166. return 3;
  167. }
  168. int3 CGObjectInstance::getVisitableOffset() const
  169. {
  170. return appearance->getVisitableOffset();
  171. }
  172. void CGObjectInstance::giveDummyBonus(const ObjectInstanceID & heroID, BonusDuration::Type duration) const
  173. {
  174. GiveBonus gbonus;
  175. gbonus.bonus.type = BonusType::NONE;
  176. gbonus.id = heroID.getNum();
  177. gbonus.bonus.duration = duration;
  178. gbonus.bonus.source = BonusSource::OBJECT;
  179. gbonus.bonus.sid = ID;
  180. cb->giveHeroBonus(&gbonus);
  181. }
  182. std::string CGObjectInstance::getObjectName() const
  183. {
  184. return VLC->objtypeh->getObjectName(ID, subID);
  185. }
  186. std::optional<std::string> CGObjectInstance::getAmbientSound() const
  187. {
  188. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).ambient;
  189. if(!sounds.empty())
  190. return sounds.front(); // TODO: Support randomization of ambient sounds
  191. return std::nullopt;
  192. }
  193. std::optional<std::string> CGObjectInstance::getVisitSound() const
  194. {
  195. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).visit;
  196. if(!sounds.empty())
  197. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  198. return std::nullopt;
  199. }
  200. std::optional<std::string> CGObjectInstance::getRemovalSound() const
  201. {
  202. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).removal;
  203. if(!sounds.empty())
  204. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  205. return std::nullopt;
  206. }
  207. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  208. {
  209. auto text = getObjectName();
  210. if (tempOwner.isValidPlayer())
  211. text += "\n" + VLC->generaltexth->arraytxt[23 + tempOwner.getNum()];
  212. return text;
  213. }
  214. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  215. {
  216. return getHoverText(hero->tempOwner);
  217. }
  218. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  219. {
  220. switch(ID)
  221. {
  222. case Obj::SANCTUARY:
  223. {
  224. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  225. h->showInfoDialog(114);
  226. }
  227. break;
  228. case Obj::TAVERN:
  229. {
  230. openWindow(EOpenWindowMode::TAVERN_WINDOW,h->id.getNum(),id.getNum());
  231. }
  232. break;
  233. }
  234. }
  235. int3 CGObjectInstance::visitablePos() const
  236. {
  237. return pos - getVisitableOffset();
  238. }
  239. bool CGObjectInstance::isVisitable() const
  240. {
  241. return appearance->isVisitable();
  242. }
  243. bool CGObjectInstance::isBlockedVisitable() const
  244. {
  245. return blockVisit;
  246. }
  247. bool CGObjectInstance::isCoastVisitable() const
  248. {
  249. return false;
  250. }
  251. bool CGObjectInstance::passableFor(PlayerColor color) const
  252. {
  253. return false;
  254. }
  255. void CGObjectInstance::updateFrom(const JsonNode & data)
  256. {
  257. }
  258. void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
  259. {
  260. //only save here, loading is handled by map loader
  261. if(handler.saving)
  262. {
  263. handler.serializeString("type", typeName);
  264. handler.serializeString("subtype", subTypeName);
  265. handler.serializeInt("x", pos.x);
  266. handler.serializeInt("y", pos.y);
  267. handler.serializeInt("l", pos.z);
  268. JsonNode app;
  269. appearance->writeJson(app, false);
  270. handler.serializeRaw("template", app, std::nullopt);
  271. }
  272. {
  273. auto options = handler.enterStruct("options");
  274. serializeJsonOptions(handler);
  275. }
  276. }
  277. void CGObjectInstance::afterAddToMap(CMap * map)
  278. {
  279. //nothing here
  280. }
  281. void CGObjectInstance::afterRemoveFromMap(CMap * map)
  282. {
  283. //nothing here
  284. }
  285. void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  286. {
  287. //nothing here
  288. }
  289. void CGObjectInstance::serializeJsonOwner(JsonSerializeFormat & handler)
  290. {
  291. ui8 temp = tempOwner.getNum();
  292. handler.serializeEnum("owner", temp, PlayerColor::NEUTRAL.getNum(), GameConstants::PLAYER_COLOR_NAMES);
  293. if(!handler.saving)
  294. tempOwner = PlayerColor(temp);
  295. }
  296. BattleField CGObjectInstance::getBattlefield() const
  297. {
  298. return VLC->objtypeh->getHandlerFor(ID, subID)->getBattlefield();
  299. }
  300. VCMI_LIB_NAMESPACE_END