CObjectHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 "CObjectClassesHandler.h"
  17. using namespace boost::assign;
  18. IGameCallback * IObjectInterface::cb = nullptr;
  19. ///helpers
  20. static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1)
  21. {
  22. OpenWindow ow;
  23. ow.window = type;
  24. ow.id1 = id1;
  25. ow.id2 = id2;
  26. IObjectInterface::cb->sendAndApply(&ow);
  27. }
  28. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  29. {
  30. InfoWindow iw;
  31. iw.soundID = soundID;
  32. iw.player = playerID;
  33. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  34. IObjectInterface::cb->sendAndApply(&iw);
  35. }
  36. /*static void showInfoDialog(const ObjectInstanceID heroID, const ui32 txtID, const ui16 soundID)
  37. {
  38. const PlayerColor playerID = IObjectInterface::cb->getOwner(heroID);
  39. showInfoDialog(playerID,txtID,soundID);
  40. }*/
  41. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
  42. {
  43. const PlayerColor playerID = h->getOwner();
  44. showInfoDialog(playerID,txtID,soundID);
  45. }
  46. static std::string & visitedTxt(const bool visited)
  47. {
  48. int id = visited ? 352 : 353;
  49. return VLC->generaltexth->allTexts[id];
  50. }
  51. ///IObjectInterface
  52. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  53. {}
  54. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  55. {}
  56. void IObjectInterface::newTurn () const
  57. {}
  58. IObjectInterface::~IObjectInterface()
  59. {}
  60. IObjectInterface::IObjectInterface()
  61. {}
  62. void IObjectInterface::initObj()
  63. {}
  64. void IObjectInterface::setProperty( ui8 what, ui32 val )
  65. {}
  66. bool IObjectInterface::wasVisited (PlayerColor player) const
  67. {
  68. return false;
  69. }
  70. bool IObjectInterface::wasVisited (const CGHeroInstance * h) const
  71. {
  72. return false;
  73. }
  74. void IObjectInterface::postInit()
  75. {}
  76. void IObjectInterface::preInit()
  77. {}
  78. void IObjectInterface::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  79. {}
  80. void IObjectInterface::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  81. {}
  82. void IObjectInterface::garrisonDialogClosed(const CGHeroInstance *hero) const
  83. {}
  84. void IObjectInterface::heroLevelUpDone(const CGHeroInstance *hero) const
  85. {}
  86. CObjectHandler::CObjectHandler()
  87. {
  88. logGlobal->traceStream() << "\t\tReading resources prices ";
  89. const JsonNode config2(ResourceID("config/resources.json"));
  90. for(const JsonNode &price : config2["resources_prices"].Vector())
  91. {
  92. resVals.push_back(price.Float());
  93. }
  94. logGlobal->traceStream() << "\t\tDone loading resource prices!";
  95. }
  96. PlayerColor CGObjectInstance::getOwner() const
  97. {
  98. //if (state)
  99. // return state->owner;
  100. //else
  101. return tempOwner; //won't have owner
  102. }
  103. CGObjectInstance::CGObjectInstance():
  104. pos(-1,-1,-1),
  105. ID(Obj::NO_OBJ),
  106. subID(-1),
  107. tempOwner(PlayerColor::UNFLAGGABLE),
  108. blockVisit(false)
  109. {
  110. }
  111. CGObjectInstance::~CGObjectInstance()
  112. {
  113. }
  114. const std::string & CGObjectInstance::getHoverText() const
  115. {
  116. return hoverName;
  117. }
  118. void CGObjectInstance::setOwner(PlayerColor ow)
  119. {
  120. tempOwner = ow;
  121. }
  122. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  123. {
  124. return appearance.getWidth();
  125. }
  126. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  127. {
  128. return appearance.getHeight();
  129. }
  130. 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)
  131. {
  132. return appearance.isVisitableAt(pos.x - x, pos.y - y);
  133. }
  134. bool CGObjectInstance::blockingAt(int x, int y) const
  135. {
  136. return appearance.isBlockedAt(pos.x - x, pos.y - y);
  137. }
  138. bool CGObjectInstance::coveringAt(int x, int y) const
  139. {
  140. return appearance.isVisibleAt(pos.x - x, pos.y - y);
  141. }
  142. std::set<int3> CGObjectInstance::getBlockedPos() const
  143. {
  144. std::set<int3> ret;
  145. for(int w=0; w<getWidth(); ++w)
  146. {
  147. for(int h=0; h<getHeight(); ++h)
  148. {
  149. if (appearance.isBlockedAt(w, h))
  150. ret.insert(int3(pos.x - w, pos.y - h, pos.z));
  151. }
  152. }
  153. return ret;
  154. }
  155. std::set<int3> CGObjectInstance::getBlockedOffsets() const
  156. {
  157. std::set<int3> ret;
  158. for(int w=0; w<getWidth(); ++w)
  159. {
  160. for(int h=0; h<getHeight(); ++h)
  161. {
  162. if (appearance.isBlockedAt(w, h))
  163. ret.insert(int3(-w, -h, 0));
  164. }
  165. }
  166. return ret;
  167. }
  168. void CGObjectInstance::setType(si32 ID, si32 subID)
  169. {
  170. const TerrainTile &tile = cb->gameState()->map->getTile(visitablePos());
  171. this->ID = Obj(ID);
  172. this->subID = subID;
  173. //recalculate blockvis tiles - new appearance might have different blockmap than before
  174. cb->gameState()->map->removeBlockVisTiles(this, true);
  175. auto handler = VLC->objtypeh->getHandlerFor(ID, subID);
  176. appearance = handler->getTemplates(tile.terType).at(0);
  177. cb->gameState()->map->addBlockVisTiles(this);
  178. }
  179. void CGObjectInstance::initObj()
  180. {
  181. switch(ID)
  182. {
  183. case Obj::TAVERN:
  184. blockVisit = true;
  185. break;
  186. }
  187. }
  188. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  189. {
  190. switch(what)
  191. {
  192. case ObjProperty::OWNER:
  193. tempOwner = PlayerColor(val);
  194. break;
  195. case ObjProperty::BLOCKVIS:
  196. blockVisit = val;
  197. break;
  198. case ObjProperty::ID:
  199. ID = Obj(val);
  200. break;
  201. case ObjProperty::SUBID:
  202. subID = val;
  203. break;
  204. }
  205. setPropertyDer(what, val);
  206. }
  207. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  208. {}
  209. int3 CGObjectInstance::getSightCenter() const
  210. {
  211. //return vistiable tile if possible
  212. for(int i=0; i < 8; i++)
  213. for(int j=0; j < 6; j++)
  214. if(visitableAt(i,j))
  215. return(pos + int3(i-7, j-5, 0));
  216. return pos;
  217. }
  218. int CGObjectInstance::getSightRadious() const
  219. {
  220. return 3;
  221. }
  222. void CGObjectInstance::getSightTiles(std::unordered_set<int3, ShashInt3> &tiles) const //returns reference to the set
  223. {
  224. cb->getTilesInRange(tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  225. }
  226. void CGObjectInstance::hideTiles(PlayerColor ourplayer, int radius) const
  227. {
  228. for (auto i = cb->gameState()->teams.begin(); i != cb->gameState()->teams.end(); i++)
  229. {
  230. if ( !vstd::contains(i->second.players, ourplayer ))//another team
  231. {
  232. for (auto & elem : i->second.players)
  233. if ( cb->getPlayer(elem)->status == EPlayerStatus::INGAME )//seek for living player (if any)
  234. {
  235. FoWChange fw;
  236. fw.mode = 0;
  237. fw.player = elem;
  238. cb->getTilesInRange (fw.tiles, pos, radius, (elem), -1);
  239. cb->sendAndApply (&fw);
  240. break;
  241. }
  242. }
  243. }
  244. }
  245. int3 CGObjectInstance::getVisitableOffset() const
  246. {
  247. for(int y = 0; y < appearance.getHeight(); y++)
  248. for (int x = 0; x < appearance.getWidth(); x++)
  249. if (appearance.isVisitableAt(x, y))
  250. return int3(x,y,0);
  251. logGlobal->warnStream() << "Warning: getVisitableOffset called on non-visitable obj!";
  252. return int3(0,0,0);
  253. }
  254. void CGObjectInstance::getNameVis( std::string &hname ) const
  255. {
  256. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  257. hname = VLC->objtypeh->getObjectName(ID);
  258. if(h)
  259. {
  260. const bool visited = h->hasBonusFrom(Bonus::OBJECT,ID);
  261. hname + " " + visitedTxt(visited);
  262. }
  263. }
  264. void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
  265. {
  266. GiveBonus gbonus;
  267. gbonus.bonus.type = Bonus::NONE;
  268. gbonus.id = heroID.getNum();
  269. gbonus.bonus.duration = duration;
  270. gbonus.bonus.source = Bonus::OBJECT;
  271. gbonus.bonus.sid = ID;
  272. cb->giveHeroBonus(&gbonus);
  273. }
  274. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  275. {
  276. switch(ID)
  277. {
  278. case Obj::HILL_FORT:
  279. {
  280. openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum());
  281. }
  282. break;
  283. case Obj::SANCTUARY:
  284. {
  285. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  286. showInfoDialog(h,114,soundBase::GETPROTECTION);
  287. }
  288. break;
  289. case Obj::TAVERN:
  290. {
  291. openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum());
  292. }
  293. break;
  294. }
  295. }
  296. int3 CGObjectInstance::visitablePos() const
  297. {
  298. return pos - getVisitableOffset();
  299. }
  300. bool CGObjectInstance::isVisitable() const
  301. {
  302. return appearance.isVisitable();
  303. }
  304. bool CGObjectInstance::passableFor(PlayerColor color) const
  305. {
  306. return false;
  307. }
  308. CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
  309. {
  310. }
  311. bool CGObjectInstanceBySubIdFinder::operator()(CGObjectInstance * obj) const
  312. {
  313. return this->obj->subID == obj->subID;
  314. }
  315. int3 IBoatGenerator::bestLocation() const
  316. {
  317. std::vector<int3> offsets;
  318. getOutOffsets(offsets);
  319. for (auto & offset : offsets)
  320. {
  321. if (const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offset, false)) //tile is in the map
  322. {
  323. if (tile->terType == ETerrainType::WATER && (!tile->blocked || tile->blockingObjects.front()->ID == 8)) //and is water and is not blocked or is blocked by boat
  324. return o->pos + offset;
  325. }
  326. }
  327. return int3 (-1,-1,-1);
  328. }
  329. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  330. {
  331. int3 tile = bestLocation();
  332. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  333. if(!t)
  334. return TILE_BLOCKED; //no available water
  335. else if(!t->blockingObjects.size())
  336. return GOOD; //OK
  337. else if(t->blockingObjects.front()->ID == Obj::BOAT)
  338. return BOAT_ALREADY_BUILT; //blocked with boat
  339. else
  340. return TILE_BLOCKED; //blocked
  341. }
  342. int IBoatGenerator::getBoatType() const
  343. {
  344. //We make good ships by default
  345. return 1;
  346. }
  347. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  348. : o(O)
  349. {
  350. }
  351. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  352. {
  353. switch(shipyardStatus())
  354. {
  355. case BOAT_ALREADY_BUILT:
  356. out.addTxt(MetaString::GENERAL_TXT, 51);
  357. break;
  358. case TILE_BLOCKED:
  359. if(visitor)
  360. {
  361. out.addTxt(MetaString::GENERAL_TXT, 134);
  362. out.addReplacement(visitor->name);
  363. }
  364. else
  365. out.addTxt(MetaString::ADVOB_TXT, 189);
  366. break;
  367. case NO_WATER:
  368. logGlobal->errorStream() << "Shipyard without water!!! " << o->pos << "\t" << o->id;
  369. return;
  370. }
  371. }
  372. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  373. {
  374. cost.resize(GameConstants::RESOURCE_QUANTITY);
  375. cost[Res::WOOD] = 10;
  376. cost[Res::GOLD] = 1000;
  377. }
  378. IShipyard::IShipyard(const CGObjectInstance *O)
  379. : IBoatGenerator(O)
  380. {
  381. }
  382. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  383. {
  384. if(!obj)
  385. return nullptr;
  386. if(obj->ID == Obj::TOWN)
  387. {
  388. return static_cast<CGTownInstance*>(obj);
  389. }
  390. else if(obj->ID == Obj::SHIPYARD)
  391. {
  392. return static_cast<CGShipyard*>(obj);
  393. }
  394. else
  395. {
  396. return nullptr;
  397. }
  398. }
  399. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  400. {
  401. return castFrom(const_cast<CGObjectInstance*>(obj));
  402. }