CObjectHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 "CObjectHandler.h"
  12. #include "../NetPacks.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../CHeroHandler.h"
  15. #include "../CSoundBase.h"
  16. #include "../filesystem/ResourceID.h"
  17. #include "../IGameCallback.h"
  18. #include "../CGameState.h"
  19. #include "../StringConstants.h"
  20. #include "../mapping/CMap.h"
  21. #include "../TerrainHandler.h"
  22. #include "CObjectClassesHandler.h"
  23. #include "CGTownInstance.h"
  24. #include "../serializer/JsonSerializeFormat.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. IGameCallback * IObjectInterface::cb = nullptr;
  27. ///helpers
  28. static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1)
  29. {
  30. OpenWindow ow;
  31. ow.window = type;
  32. ow.id1 = id1;
  33. ow.id2 = id2;
  34. IObjectInterface::cb->sendAndApply(&ow);
  35. }
  36. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  37. {
  38. InfoWindow iw;
  39. iw.soundID = soundID;
  40. iw.player = playerID;
  41. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  42. IObjectInterface::cb->sendAndApply(&iw);
  43. }
  44. /*static void showInfoDialog(const ObjectInstanceID heroID, const ui32 txtID, const ui16 soundID)
  45. {
  46. const PlayerColor playerID = IObjectInterface::cb->getOwner(heroID);
  47. showInfoDialog(playerID,txtID,soundID);
  48. }*/
  49. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID = 0)
  50. {
  51. const PlayerColor playerID = h->getOwner();
  52. showInfoDialog(playerID,txtID,soundID);
  53. }
  54. ///IObjectInterface
  55. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  56. {}
  57. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  58. {}
  59. void IObjectInterface::newTurn(CRandomGenerator & rand) const
  60. {}
  61. IObjectInterface::~IObjectInterface()
  62. {}
  63. IObjectInterface::IObjectInterface()
  64. {}
  65. void IObjectInterface::initObj(CRandomGenerator & rand)
  66. {}
  67. void IObjectInterface::setProperty( ui8 what, ui32 val )
  68. {}
  69. bool IObjectInterface::wasVisited (PlayerColor player) const
  70. {
  71. return false;
  72. }
  73. bool IObjectInterface::wasVisited (const CGHeroInstance * h) const
  74. {
  75. return false;
  76. }
  77. void IObjectInterface::postInit()
  78. {}
  79. void IObjectInterface::preInit()
  80. {}
  81. void IObjectInterface::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  82. {}
  83. void IObjectInterface::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  84. {}
  85. void IObjectInterface::garrisonDialogClosed(const CGHeroInstance *hero) const
  86. {}
  87. void IObjectInterface::heroLevelUpDone(const CGHeroInstance *hero) const
  88. {}
  89. CObjectHandler::CObjectHandler()
  90. {
  91. logGlobal->trace("\t\tReading resources prices ");
  92. const JsonNode config2(ResourceID("config/resources.json"));
  93. for(const JsonNode &price : config2["resources_prices"].Vector())
  94. {
  95. resVals.push_back(static_cast<ui32>(price.Float()));
  96. }
  97. logGlobal->trace("\t\tDone loading resource prices!");
  98. }
  99. CGObjectInstance::CGObjectInstance():
  100. pos(-1,-1,-1),
  101. ID(Obj::NO_OBJ),
  102. subID(-1),
  103. tempOwner(PlayerColor::UNFLAGGABLE),
  104. blockVisit(false)
  105. {
  106. }
  107. CGObjectInstance::~CGObjectInstance()
  108. {
  109. }
  110. int32_t CGObjectInstance::getObjGroupIndex() const
  111. {
  112. return ID.num;
  113. }
  114. int32_t CGObjectInstance::getObjTypeIndex() const
  115. {
  116. return subID;
  117. }
  118. int3 CGObjectInstance::getPosition() const
  119. {
  120. return pos;
  121. }
  122. void CGObjectInstance::setOwner(PlayerColor ow)
  123. {
  124. tempOwner = ow;
  125. }
  126. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  127. {
  128. return appearance->getWidth();
  129. }
  130. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  131. {
  132. return appearance->getHeight();
  133. }
  134. 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)
  135. {
  136. return appearance->isVisitableAt(pos.x - x, pos.y - y);
  137. }
  138. bool CGObjectInstance::blockingAt(int x, int y) const
  139. {
  140. return appearance->isBlockedAt(pos.x - x, pos.y - y);
  141. }
  142. bool CGObjectInstance::coveringAt(int x, int y) const
  143. {
  144. return appearance->isVisibleAt(pos.x - x, pos.y - y);
  145. }
  146. std::set<int3> CGObjectInstance::getBlockedPos() const
  147. {
  148. std::set<int3> ret;
  149. for(int w=0; w<getWidth(); ++w)
  150. {
  151. for(int h=0; h<getHeight(); ++h)
  152. {
  153. if(appearance->isBlockedAt(w, h))
  154. ret.insert(int3(pos.x - w, pos.y - h, pos.z));
  155. }
  156. }
  157. return ret;
  158. }
  159. std::set<int3> CGObjectInstance::getBlockedOffsets() const
  160. {
  161. return appearance->getBlockedOffsets();
  162. }
  163. void CGObjectInstance::setType(si32 ID, si32 subID)
  164. {
  165. auto position = visitablePos();
  166. auto oldOffset = getVisitableOffset();
  167. auto &tile = cb->gameState()->map->getTile(position);
  168. //recalculate blockvis tiles - new appearance might have different blockmap than before
  169. cb->gameState()->map->removeBlockVisTiles(this, true);
  170. auto handler = VLC->objtypeh->getHandlerFor(ID, subID);
  171. if(!handler)
  172. {
  173. logGlobal->error("Unknown object type %d:%d at %s", ID, subID, visitablePos().toString());
  174. return;
  175. }
  176. if(!handler->getTemplates(tile.terType->getId()).empty())
  177. {
  178. appearance = handler->getTemplates(tile.terType->getId())[0];
  179. }
  180. else
  181. {
  182. logGlobal->warn("Object %d:%d at %s has no templates suitable for terrain %s", ID, subID, visitablePos().toString(), tile.terType->getNameTranslated());
  183. appearance = handler->getTemplates()[0]; // get at least some appearance since alternative is crash
  184. }
  185. if(this->ID == Obj::PRISON && ID == Obj::HERO)
  186. {
  187. auto newOffset = getVisitableOffset();
  188. // FIXME: potentially unused code - setType is NOT called when releasing hero from prison
  189. // instead, appearance update & pos adjustment occurs in GiveHero::applyGs
  190. // adjust position since hero and prison may have different visitable offset
  191. pos = pos - oldOffset + newOffset;
  192. }
  193. this->ID = Obj(ID);
  194. this->subID = subID;
  195. cb->gameState()->map->addBlockVisTiles(this);
  196. }
  197. void CGObjectInstance::initObj(CRandomGenerator & rand)
  198. {
  199. switch(ID)
  200. {
  201. case Obj::TAVERN:
  202. blockVisit = true;
  203. break;
  204. }
  205. }
  206. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  207. {
  208. setPropertyDer(what, val); // call this before any actual changes (needed at least for dwellings)
  209. switch(what)
  210. {
  211. case ObjProperty::OWNER:
  212. tempOwner = PlayerColor(val);
  213. break;
  214. case ObjProperty::BLOCKVIS:
  215. blockVisit = val;
  216. break;
  217. case ObjProperty::ID:
  218. ID = Obj(val);
  219. break;
  220. case ObjProperty::SUBID:
  221. subID = val;
  222. break;
  223. }
  224. }
  225. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  226. {}
  227. int3 CGObjectInstance::getSightCenter() const
  228. {
  229. return visitablePos();
  230. }
  231. int CGObjectInstance::getSightRadius() const
  232. {
  233. return 3;
  234. }
  235. int3 CGObjectInstance::getVisitableOffset() const
  236. {
  237. return appearance->getVisitableOffset();
  238. }
  239. void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
  240. {
  241. GiveBonus gbonus;
  242. gbonus.bonus.type = Bonus::NONE;
  243. gbonus.id = heroID.getNum();
  244. gbonus.bonus.duration = (Bonus::BonusDuration)duration;
  245. gbonus.bonus.source = Bonus::OBJECT;
  246. gbonus.bonus.sid = ID;
  247. cb->giveHeroBonus(&gbonus);
  248. }
  249. std::string CGObjectInstance::getObjectName() const
  250. {
  251. return VLC->objtypeh->getObjectName(ID, subID);
  252. }
  253. boost::optional<std::string> CGObjectInstance::getAmbientSound() const
  254. {
  255. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).ambient;
  256. if(sounds.size())
  257. return sounds.front(); // TODO: Support randomization of ambient sounds
  258. return boost::none;
  259. }
  260. boost::optional<std::string> CGObjectInstance::getVisitSound() const
  261. {
  262. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).visit;
  263. if(sounds.size())
  264. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  265. return boost::none;
  266. }
  267. boost::optional<std::string> CGObjectInstance::getRemovalSound() const
  268. {
  269. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).removal;
  270. if(sounds.size())
  271. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  272. return boost::none;
  273. }
  274. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  275. {
  276. return getObjectName();
  277. }
  278. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  279. {
  280. return getHoverText(hero->tempOwner);
  281. }
  282. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  283. {
  284. switch(ID)
  285. {
  286. case Obj::HILL_FORT:
  287. {
  288. openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum());
  289. }
  290. break;
  291. case Obj::SANCTUARY:
  292. {
  293. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  294. showInfoDialog(h, 114);
  295. }
  296. break;
  297. case Obj::TAVERN:
  298. {
  299. openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum());
  300. }
  301. break;
  302. }
  303. }
  304. int3 CGObjectInstance::visitablePos() const
  305. {
  306. return pos - getVisitableOffset();
  307. }
  308. bool CGObjectInstance::isVisitable() const
  309. {
  310. return appearance->isVisitable();
  311. }
  312. bool CGObjectInstance::passableFor(PlayerColor color) const
  313. {
  314. return false;
  315. }
  316. void CGObjectInstance::updateFrom(const JsonNode & data)
  317. {
  318. }
  319. void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
  320. {
  321. //only save here, loading is handled by map loader
  322. if(handler.saving)
  323. {
  324. handler.serializeString("type", typeName);
  325. handler.serializeString("subtype", subTypeName);
  326. handler.serializeInt("x", pos.x);
  327. handler.serializeInt("y", pos.y);
  328. handler.serializeInt("l", pos.z);
  329. JsonNode app;
  330. appearance->writeJson(app, false);
  331. handler.serializeRaw("template",app, boost::none);
  332. }
  333. {
  334. auto options = handler.enterStruct("options");
  335. serializeJsonOptions(handler);
  336. }
  337. }
  338. void CGObjectInstance::afterAddToMap(CMap * map)
  339. {
  340. //nothing here
  341. }
  342. void CGObjectInstance::afterRemoveFromMap(CMap * map)
  343. {
  344. //nothing here
  345. }
  346. void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  347. {
  348. //nothing here
  349. }
  350. void CGObjectInstance::serializeJsonOwner(JsonSerializeFormat & handler)
  351. {
  352. ui8 temp = tempOwner.getNum();
  353. handler.serializeEnum("owner", temp, PlayerColor::NEUTRAL.getNum(), GameConstants::PLAYER_COLOR_NAMES);
  354. if(!handler.saving)
  355. tempOwner = PlayerColor(temp);
  356. }
  357. BattleField CGObjectInstance::getBattlefield() const
  358. {
  359. return VLC->objtypeh->getHandlerFor(ID, subID)->getBattlefield();
  360. }
  361. CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
  362. {
  363. }
  364. bool CGObjectInstanceBySubIdFinder::operator()(CGObjectInstance * obj) const
  365. {
  366. return this->obj->subID == obj->subID;
  367. }
  368. int3 IBoatGenerator::bestLocation() const
  369. {
  370. std::vector<int3> offsets;
  371. getOutOffsets(offsets);
  372. for (auto & offset : offsets)
  373. {
  374. if(const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offset, false)) //tile is in the map
  375. {
  376. if(tile->terType->isWater() && (!tile->blocked || tile->blockingObjects.front()->ID == Obj::BOAT)) //and is water and is not blocked or is blocked by boat
  377. return o->pos + offset;
  378. }
  379. }
  380. return int3 (-1,-1,-1);
  381. }
  382. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  383. {
  384. int3 tile = bestLocation();
  385. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  386. if(!t)
  387. return TILE_BLOCKED; //no available water
  388. else if(!t->blockingObjects.size())
  389. return GOOD; //OK
  390. else if(t->blockingObjects.front()->ID == Obj::BOAT)
  391. return BOAT_ALREADY_BUILT; //blocked with boat
  392. else
  393. return TILE_BLOCKED; //blocked
  394. }
  395. int IBoatGenerator::getBoatType() const
  396. {
  397. //We make good ships by default
  398. return 1;
  399. }
  400. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  401. : o(O)
  402. {
  403. }
  404. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  405. {
  406. switch(shipyardStatus())
  407. {
  408. case BOAT_ALREADY_BUILT:
  409. out.addTxt(MetaString::GENERAL_TXT, 51);
  410. break;
  411. case TILE_BLOCKED:
  412. if(visitor)
  413. {
  414. out.addTxt(MetaString::GENERAL_TXT, 134);
  415. out.addReplacement(visitor->name);
  416. }
  417. else
  418. out.addTxt(MetaString::ADVOB_TXT, 189);
  419. break;
  420. case NO_WATER:
  421. logGlobal->error("Shipyard without water! %s \t %d", o->pos.toString(), o->id.getNum());
  422. return;
  423. }
  424. }
  425. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  426. {
  427. cost.resize(GameConstants::RESOURCE_QUANTITY);
  428. cost[Res::WOOD] = 10;
  429. cost[Res::GOLD] = 1000;
  430. }
  431. IShipyard::IShipyard(const CGObjectInstance *O)
  432. : IBoatGenerator(O)
  433. {
  434. }
  435. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  436. {
  437. if(!obj)
  438. return nullptr;
  439. if(obj->ID == Obj::TOWN)
  440. {
  441. return static_cast<CGTownInstance*>(obj);
  442. }
  443. else if(obj->ID == Obj::SHIPYARD)
  444. {
  445. return static_cast<CGShipyard*>(obj);
  446. }
  447. else
  448. {
  449. return nullptr;
  450. }
  451. }
  452. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  453. {
  454. return castFrom(const_cast<CGObjectInstance*>(obj));
  455. }
  456. VCMI_LIB_NAMESPACE_END