CObjectHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. //if (state)
  114. // delete state;
  115. //state=nullptr;
  116. }
  117. const std::string & CGObjectInstance::getHoverText() const
  118. {
  119. return hoverName;
  120. }
  121. void CGObjectInstance::setOwner(PlayerColor ow)
  122. {
  123. //if (state)
  124. // state->owner = ow;
  125. //else
  126. tempOwner = ow;
  127. }
  128. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  129. {
  130. return appearance.getWidth();
  131. }
  132. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  133. {
  134. return appearance.getHeight();
  135. }
  136. 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)
  137. {
  138. return appearance.isVisitableAt(pos.x - x, pos.y - y);
  139. }
  140. bool CGObjectInstance::blockingAt(int x, int y) const
  141. {
  142. return appearance.isBlockedAt(pos.x - x, pos.y - y);
  143. }
  144. bool CGObjectInstance::coveringAt(int x, int y) const
  145. {
  146. return appearance.isVisibleAt(pos.x - x, pos.y - y);
  147. }
  148. std::set<int3> CGObjectInstance::getBlockedPos() const
  149. {
  150. std::set<int3> ret;
  151. for(int w=0; w<getWidth(); ++w)
  152. {
  153. for(int h=0; h<getHeight(); ++h)
  154. {
  155. if (appearance.isBlockedAt(w, h))
  156. ret.insert(int3(pos.x - w, pos.y - h, pos.z));
  157. }
  158. }
  159. return ret;
  160. }
  161. std::set<int3> CGObjectInstance::getBlockedOffsets() const
  162. {
  163. std::set<int3> ret;
  164. for(int w=0; w<getWidth(); ++w)
  165. {
  166. for(int h=0; h<getHeight(); ++h)
  167. {
  168. if (appearance.isBlockedAt(w, h))
  169. ret.insert(int3(-w, -h, 0));
  170. }
  171. }
  172. return ret;
  173. }
  174. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  175. {
  176. if (appearance.printPriority != cmp.appearance.printPriority)
  177. return appearance.printPriority > cmp.appearance.printPriority;
  178. if(pos.y != cmp.pos.y)
  179. return pos.y < cmp.pos.y;
  180. if(cmp.ID==Obj::HERO && ID!=Obj::HERO)
  181. return true;
  182. if(cmp.ID!=Obj::HERO && ID==Obj::HERO)
  183. return false;
  184. if(!isVisitable() && cmp.isVisitable())
  185. return true;
  186. if(!cmp.isVisitable() && isVisitable())
  187. return false;
  188. if(this->pos.x<cmp.pos.x)
  189. return true;
  190. return false;
  191. }
  192. void CGObjectInstance::setType(si32 ID, si32 subID)
  193. {
  194. const TerrainTile &tile = cb->gameState()->map->getTile(visitablePos());
  195. this->ID = Obj(ID);
  196. this->subID = subID;
  197. //recalculate blockvis tiles - new appearance might have different blockmap than before
  198. cb->gameState()->map->removeBlockVisTiles(this, true);
  199. auto handler = VLC->objtypeh->getHandlerFor(ID, subID);
  200. appearance = handler->getTemplates(tile.terType).at(0);
  201. cb->gameState()->map->addBlockVisTiles(this);
  202. }
  203. void CGObjectInstance::initObj()
  204. {
  205. switch(ID)
  206. {
  207. case Obj::TAVERN:
  208. blockVisit = true;
  209. break;
  210. }
  211. }
  212. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  213. {
  214. switch(what)
  215. {
  216. case ObjProperty::OWNER:
  217. tempOwner = PlayerColor(val);
  218. break;
  219. case ObjProperty::BLOCKVIS:
  220. blockVisit = val;
  221. break;
  222. case ObjProperty::ID:
  223. ID = Obj(val);
  224. break;
  225. case ObjProperty::SUBID:
  226. subID = val;
  227. break;
  228. }
  229. setPropertyDer(what, val);
  230. }
  231. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  232. {}
  233. int3 CGObjectInstance::getSightCenter() const
  234. {
  235. //return vistiable tile if possible
  236. for(int i=0; i < 8; i++)
  237. for(int j=0; j < 6; j++)
  238. if(visitableAt(i,j))
  239. return(pos + int3(i-7, j-5, 0));
  240. return pos;
  241. }
  242. int CGObjectInstance::getSightRadious() const
  243. {
  244. return 3;
  245. }
  246. void CGObjectInstance::getSightTiles(std::unordered_set<int3, ShashInt3> &tiles) const //returns reference to the set
  247. {
  248. cb->getTilesInRange(tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  249. }
  250. void CGObjectInstance::hideTiles(PlayerColor ourplayer, int radius) const
  251. {
  252. for (auto i = cb->gameState()->teams.begin(); i != cb->gameState()->teams.end(); i++)
  253. {
  254. if ( !vstd::contains(i->second.players, ourplayer ))//another team
  255. {
  256. for (auto & elem : i->second.players)
  257. if ( cb->getPlayer(elem)->status == EPlayerStatus::INGAME )//seek for living player (if any)
  258. {
  259. FoWChange fw;
  260. fw.mode = 0;
  261. fw.player = elem;
  262. cb->getTilesInRange (fw.tiles, pos, radius, (elem), -1);
  263. cb->sendAndApply (&fw);
  264. break;
  265. }
  266. }
  267. }
  268. }
  269. int3 CGObjectInstance::getVisitableOffset() const
  270. {
  271. for(int y = 0; y < appearance.getHeight(); y++)
  272. for (int x = 0; x < appearance.getWidth(); x++)
  273. if (appearance.isVisitableAt(x, y))
  274. return int3(x,y,0);
  275. logGlobal->warnStream() << "Warning: getVisitableOffset called on non-visitable obj!";
  276. return int3(0,0,0);
  277. }
  278. void CGObjectInstance::getNameVis( std::string &hname ) const
  279. {
  280. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  281. hname = VLC->objtypeh->getObjectName(ID);
  282. if(h)
  283. {
  284. const bool visited = h->hasBonusFrom(Bonus::OBJECT,ID);
  285. hname + " " + visitedTxt(visited);
  286. }
  287. }
  288. void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
  289. {
  290. GiveBonus gbonus;
  291. gbonus.bonus.type = Bonus::NONE;
  292. gbonus.id = heroID.getNum();
  293. gbonus.bonus.duration = duration;
  294. gbonus.bonus.source = Bonus::OBJECT;
  295. gbonus.bonus.sid = ID;
  296. cb->giveHeroBonus(&gbonus);
  297. }
  298. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  299. {
  300. switch(ID)
  301. {
  302. case Obj::HILL_FORT:
  303. {
  304. openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum());
  305. }
  306. break;
  307. case Obj::SANCTUARY:
  308. {
  309. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  310. showInfoDialog(h,114,soundBase::GETPROTECTION);
  311. }
  312. break;
  313. case Obj::TAVERN:
  314. {
  315. openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum());
  316. }
  317. break;
  318. }
  319. }
  320. ui8 CGObjectInstance::getPassableness() const
  321. {
  322. return 0;
  323. }
  324. int3 CGObjectInstance::visitablePos() const
  325. {
  326. return pos - getVisitableOffset();
  327. }
  328. bool CGObjectInstance::isVisitable() const
  329. {
  330. return appearance.isVisitable();
  331. }
  332. bool CGObjectInstance::passableFor(PlayerColor color) const
  333. {
  334. return getPassableness() & 1<<color.getNum();
  335. }
  336. CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
  337. {
  338. }
  339. bool CGObjectInstanceBySubIdFinder::operator()(CGObjectInstance * obj) const
  340. {
  341. return this->obj->subID == obj->subID;
  342. }
  343. int3 IBoatGenerator::bestLocation() const
  344. {
  345. std::vector<int3> offsets;
  346. getOutOffsets(offsets);
  347. for (auto & offset : offsets)
  348. {
  349. if (const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offset, false)) //tile is in the map
  350. {
  351. if (tile->terType == ETerrainType::WATER && (!tile->blocked || tile->blockingObjects.front()->ID == 8)) //and is water and is not blocked or is blocked by boat
  352. return o->pos + offset;
  353. }
  354. }
  355. return int3 (-1,-1,-1);
  356. }
  357. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  358. {
  359. int3 tile = bestLocation();
  360. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  361. if(!t)
  362. return TILE_BLOCKED; //no available water
  363. else if(!t->blockingObjects.size())
  364. return GOOD; //OK
  365. else if(t->blockingObjects.front()->ID == Obj::BOAT)
  366. return BOAT_ALREADY_BUILT; //blocked with boat
  367. else
  368. return TILE_BLOCKED; //blocked
  369. }
  370. int IBoatGenerator::getBoatType() const
  371. {
  372. //We make good ships by default
  373. return 1;
  374. }
  375. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  376. : o(O)
  377. {
  378. }
  379. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  380. {
  381. switch(shipyardStatus())
  382. {
  383. case BOAT_ALREADY_BUILT:
  384. out.addTxt(MetaString::GENERAL_TXT, 51);
  385. break;
  386. case TILE_BLOCKED:
  387. if(visitor)
  388. {
  389. out.addTxt(MetaString::GENERAL_TXT, 134);
  390. out.addReplacement(visitor->name);
  391. }
  392. else
  393. out.addTxt(MetaString::ADVOB_TXT, 189);
  394. break;
  395. case NO_WATER:
  396. logGlobal->errorStream() << "Shipyard without water!!! " << o->pos << "\t" << o->id;
  397. return;
  398. }
  399. }
  400. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  401. {
  402. cost.resize(GameConstants::RESOURCE_QUANTITY);
  403. cost[Res::WOOD] = 10;
  404. cost[Res::GOLD] = 1000;
  405. }
  406. IShipyard::IShipyard(const CGObjectInstance *O)
  407. : IBoatGenerator(O)
  408. {
  409. }
  410. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  411. {
  412. if(!obj)
  413. return nullptr;
  414. if(obj->ID == Obj::TOWN)
  415. {
  416. return static_cast<CGTownInstance*>(obj);
  417. }
  418. else if(obj->ID == Obj::SHIPYARD)
  419. {
  420. return static_cast<CGShipyard*>(obj);
  421. }
  422. else
  423. {
  424. return nullptr;
  425. }
  426. }
  427. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  428. {
  429. return castFrom(const_cast<CGObjectInstance*>(obj));
  430. }