CObjectHandler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. // Bank helper. Find the creature ID and their number, and store the
  87. // result in storage (either guards or reward creatures).
  88. static void readCreatures(const JsonNode &creature, std::vector< std::pair <CreatureID, ui32> > &storage)
  89. {
  90. std::pair<CreatureID, si32> creInfo = std::make_pair(CreatureID::NONE, 0);
  91. //TODO: replace numeric id's with mod-friendly string id's
  92. creInfo.second = creature["number"].Float();
  93. creInfo.first = CreatureID((si32)creature["id"].Float());
  94. storage.push_back(creInfo);
  95. }
  96. // Bank helper. Process a bank level.
  97. static void readBankLevel(const JsonNode &level, BankConfig &bc)
  98. {
  99. int idx;
  100. bc.chance = level["chance"].Float();
  101. for(const JsonNode &creature : level["guards"].Vector())
  102. {
  103. readCreatures(creature, bc.guards);
  104. }
  105. bc.upgradeChance = level["upgrade_chance"].Float();
  106. bc.combatValue = level["combat_value"].Float();
  107. bc.resources = Res::ResourceSet(level["reward_resources"]);
  108. for(const JsonNode &creature : level["reward_creatures"].Vector())
  109. {
  110. readCreatures(creature, bc.creatures);
  111. }
  112. bc.artifacts.resize(4);
  113. idx = 0;
  114. for(const JsonNode &artifact : level["reward_artifacts"].Vector())
  115. {
  116. bc.artifacts[idx] = artifact.Float();
  117. idx ++;
  118. }
  119. bc.value = level["value"].Float();
  120. bc.rewardDifficulty = level["profitability"].Float();
  121. bc.easiest = level["easiest"].Float();
  122. }
  123. CObjectHandler::CObjectHandler()
  124. {
  125. logGlobal->traceStream() << "\t\tReading resources prices ";
  126. const JsonNode config2(ResourceID("config/resources.json"));
  127. for(const JsonNode &price : config2["resources_prices"].Vector())
  128. {
  129. resVals.push_back(price.Float());
  130. }
  131. logGlobal->traceStream() << "\t\tDone loading resource prices!";
  132. logGlobal->traceStream() << "\t\tReading banks configs";
  133. const JsonNode config3(ResourceID("config/bankconfig.json"));
  134. int bank_num = 0;
  135. for(const JsonNode &bank : config3["banks"].Vector())
  136. {
  137. creBanksNames[bank_num] = bank["name"].String();
  138. int level_num = 0;
  139. for(const JsonNode &level : bank["levels"].Vector())
  140. {
  141. banksInfo[bank_num].push_back(new BankConfig);
  142. BankConfig &bc = *banksInfo[bank_num].back();
  143. bc.level = level_num;
  144. readBankLevel(level, bc);
  145. level_num ++;
  146. }
  147. bank_num ++;
  148. }
  149. logGlobal->traceStream() << "\t\tDone loading banks configs";
  150. }
  151. CObjectHandler::~CObjectHandler()
  152. {
  153. for(auto & mapEntry : banksInfo)
  154. {
  155. for(auto & vecEntry : mapEntry.second)
  156. {
  157. vecEntry.dellNull();
  158. }
  159. }
  160. }
  161. int CObjectHandler::bankObjToIndex (const CGObjectInstance * obj)
  162. {
  163. switch (obj->ID) //find appriopriate key
  164. {
  165. case Obj::CREATURE_BANK:
  166. return obj->subID;
  167. case Obj::DERELICT_SHIP:
  168. return 8;
  169. case Obj::DRAGON_UTOPIA:
  170. return 10;
  171. case Obj::CRYPT:
  172. return 9;
  173. case Obj::SHIPWRECK:
  174. return 7;
  175. case Obj::PYRAMID:
  176. return 21;
  177. default:
  178. logGlobal->warnStream() << "Unrecognized Bank indetifier!";
  179. return 0;
  180. }
  181. }
  182. PlayerColor CGObjectInstance::getOwner() const
  183. {
  184. //if (state)
  185. // return state->owner;
  186. //else
  187. return tempOwner; //won't have owner
  188. }
  189. CGObjectInstance::CGObjectInstance():
  190. pos(-1,-1,-1),
  191. ID(Obj::NO_OBJ),
  192. subID(-1),
  193. tempOwner(PlayerColor::UNFLAGGABLE),
  194. blockVisit(false)
  195. {
  196. }
  197. CGObjectInstance::~CGObjectInstance()
  198. {
  199. //if (state)
  200. // delete state;
  201. //state=nullptr;
  202. }
  203. const std::string & CGObjectInstance::getHoverText() const
  204. {
  205. return hoverName;
  206. }
  207. void CGObjectInstance::setOwner(PlayerColor ow)
  208. {
  209. //if (state)
  210. // state->owner = ow;
  211. //else
  212. tempOwner = ow;
  213. }
  214. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  215. {
  216. return appearance.getWidth();
  217. }
  218. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  219. {
  220. return appearance.getHeight();
  221. }
  222. 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)
  223. {
  224. return appearance.isVisitableAt(pos.x - x, pos.y - y);
  225. }
  226. bool CGObjectInstance::blockingAt(int x, int y) const
  227. {
  228. return appearance.isBlockedAt(pos.x - x, pos.y - y);
  229. }
  230. bool CGObjectInstance::coveringAt(int x, int y) const
  231. {
  232. return appearance.isVisibleAt(pos.x - x, pos.y - y);
  233. }
  234. std::set<int3> CGObjectInstance::getBlockedPos() const
  235. {
  236. std::set<int3> ret;
  237. for(int w=0; w<getWidth(); ++w)
  238. {
  239. for(int h=0; h<getHeight(); ++h)
  240. {
  241. if (appearance.isBlockedAt(w, h))
  242. ret.insert(int3(pos.x - w, pos.y - h, pos.z));
  243. }
  244. }
  245. return ret;
  246. }
  247. std::set<int3> CGObjectInstance::getBlockedOffsets() const
  248. {
  249. std::set<int3> ret;
  250. for(int w=0; w<getWidth(); ++w)
  251. {
  252. for(int h=0; h<getHeight(); ++h)
  253. {
  254. if (appearance.isBlockedAt(w, h))
  255. ret.insert(int3(-w, -h, 0));
  256. }
  257. }
  258. return ret;
  259. }
  260. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  261. {
  262. if (appearance.printPriority != cmp.appearance.printPriority)
  263. return appearance.printPriority > cmp.appearance.printPriority;
  264. if(pos.y != cmp.pos.y)
  265. return pos.y < cmp.pos.y;
  266. if(cmp.ID==Obj::HERO && ID!=Obj::HERO)
  267. return true;
  268. if(cmp.ID!=Obj::HERO && ID==Obj::HERO)
  269. return false;
  270. if(!isVisitable() && cmp.isVisitable())
  271. return true;
  272. if(!cmp.isVisitable() && isVisitable())
  273. return false;
  274. if(this->pos.x<cmp.pos.x)
  275. return true;
  276. return false;
  277. }
  278. void CGObjectInstance::setType(si32 ID, si32 subID)
  279. {
  280. const TerrainTile &tile = cb->gameState()->map->getTile(visitablePos());
  281. this->ID = Obj(ID);
  282. this->subID = subID;
  283. //recalculate blockvis tiles - new appearance might have different blockmap than before
  284. cb->gameState()->map->removeBlockVisTiles(this, true);
  285. auto handler = VLC->objtypeh->getHandlerFor(ID, subID);
  286. appearance = handler->getTemplates(tile.terType).at(0);
  287. cb->gameState()->map->addBlockVisTiles(this);
  288. }
  289. void CGObjectInstance::initObj()
  290. {
  291. switch(ID)
  292. {
  293. case Obj::TAVERN:
  294. blockVisit = true;
  295. break;
  296. }
  297. }
  298. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  299. {
  300. switch(what)
  301. {
  302. case ObjProperty::OWNER:
  303. tempOwner = PlayerColor(val);
  304. break;
  305. case ObjProperty::BLOCKVIS:
  306. blockVisit = val;
  307. break;
  308. case ObjProperty::ID:
  309. ID = Obj(val);
  310. break;
  311. case ObjProperty::SUBID:
  312. subID = val;
  313. break;
  314. }
  315. setPropertyDer(what, val);
  316. }
  317. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  318. {}
  319. int3 CGObjectInstance::getSightCenter() const
  320. {
  321. //return vistiable tile if possible
  322. for(int i=0; i < 8; i++)
  323. for(int j=0; j < 6; j++)
  324. if(visitableAt(i,j))
  325. return(pos + int3(i-7, j-5, 0));
  326. return pos;
  327. }
  328. int CGObjectInstance::getSightRadious() const
  329. {
  330. return 3;
  331. }
  332. void CGObjectInstance::getSightTiles(std::unordered_set<int3, ShashInt3> &tiles) const //returns reference to the set
  333. {
  334. cb->getTilesInRange(tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  335. }
  336. void CGObjectInstance::hideTiles(PlayerColor ourplayer, int radius) const
  337. {
  338. for (auto i = cb->gameState()->teams.begin(); i != cb->gameState()->teams.end(); i++)
  339. {
  340. if ( !vstd::contains(i->second.players, ourplayer ))//another team
  341. {
  342. for (auto & elem : i->second.players)
  343. if ( cb->getPlayer(elem)->status == EPlayerStatus::INGAME )//seek for living player (if any)
  344. {
  345. FoWChange fw;
  346. fw.mode = 0;
  347. fw.player = elem;
  348. cb->getTilesInRange (fw.tiles, pos, radius, (elem), -1);
  349. cb->sendAndApply (&fw);
  350. break;
  351. }
  352. }
  353. }
  354. }
  355. int3 CGObjectInstance::getVisitableOffset() const
  356. {
  357. for(int y = 0; y < appearance.getHeight(); y++)
  358. for (int x = 0; x < appearance.getWidth(); x++)
  359. if (appearance.isVisitableAt(x, y))
  360. return int3(x,y,0);
  361. logGlobal->warnStream() << "Warning: getVisitableOffset called on non-visitable obj!";
  362. return int3(0,0,0);
  363. }
  364. void CGObjectInstance::getNameVis( std::string &hname ) const
  365. {
  366. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  367. hname = VLC->objtypeh->getObjectName(ID);
  368. if(h)
  369. {
  370. const bool visited = h->hasBonusFrom(Bonus::OBJECT,ID);
  371. hname + " " + visitedTxt(visited);
  372. }
  373. }
  374. void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
  375. {
  376. GiveBonus gbonus;
  377. gbonus.bonus.type = Bonus::NONE;
  378. gbonus.id = heroID.getNum();
  379. gbonus.bonus.duration = duration;
  380. gbonus.bonus.source = Bonus::OBJECT;
  381. gbonus.bonus.sid = ID;
  382. cb->giveHeroBonus(&gbonus);
  383. }
  384. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  385. {
  386. switch(ID)
  387. {
  388. case Obj::HILL_FORT:
  389. {
  390. openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum());
  391. }
  392. break;
  393. case Obj::SANCTUARY:
  394. {
  395. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  396. showInfoDialog(h,114,soundBase::GETPROTECTION);
  397. }
  398. break;
  399. case Obj::TAVERN:
  400. {
  401. openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum());
  402. }
  403. break;
  404. }
  405. }
  406. ui8 CGObjectInstance::getPassableness() const
  407. {
  408. return 0;
  409. }
  410. int3 CGObjectInstance::visitablePos() const
  411. {
  412. return pos - getVisitableOffset();
  413. }
  414. bool CGObjectInstance::isVisitable() const
  415. {
  416. return appearance.isVisitable();
  417. }
  418. bool CGObjectInstance::passableFor(PlayerColor color) const
  419. {
  420. return getPassableness() & 1<<color.getNum();
  421. }
  422. CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
  423. {
  424. }
  425. bool CGObjectInstanceBySubIdFinder::operator()(CGObjectInstance * obj) const
  426. {
  427. return this->obj->subID == obj->subID;
  428. }
  429. int3 IBoatGenerator::bestLocation() const
  430. {
  431. std::vector<int3> offsets;
  432. getOutOffsets(offsets);
  433. for (auto & offset : offsets)
  434. {
  435. if (const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offset, false)) //tile is in the map
  436. {
  437. if (tile->terType == ETerrainType::WATER && (!tile->blocked || tile->blockingObjects.front()->ID == 8)) //and is water and is not blocked or is blocked by boat
  438. return o->pos + offset;
  439. }
  440. }
  441. return int3 (-1,-1,-1);
  442. }
  443. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  444. {
  445. int3 tile = bestLocation();
  446. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  447. if(!t)
  448. return TILE_BLOCKED; //no available water
  449. else if(!t->blockingObjects.size())
  450. return GOOD; //OK
  451. else if(t->blockingObjects.front()->ID == Obj::BOAT)
  452. return BOAT_ALREADY_BUILT; //blocked with boat
  453. else
  454. return TILE_BLOCKED; //blocked
  455. }
  456. int IBoatGenerator::getBoatType() const
  457. {
  458. //We make good ships by default
  459. return 1;
  460. }
  461. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  462. : o(O)
  463. {
  464. }
  465. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  466. {
  467. switch(shipyardStatus())
  468. {
  469. case BOAT_ALREADY_BUILT:
  470. out.addTxt(MetaString::GENERAL_TXT, 51);
  471. break;
  472. case TILE_BLOCKED:
  473. if(visitor)
  474. {
  475. out.addTxt(MetaString::GENERAL_TXT, 134);
  476. out.addReplacement(visitor->name);
  477. }
  478. else
  479. out.addTxt(MetaString::ADVOB_TXT, 189);
  480. break;
  481. case NO_WATER:
  482. logGlobal->errorStream() << "Shipyard without water!!! " << o->pos << "\t" << o->id;
  483. return;
  484. }
  485. }
  486. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  487. {
  488. cost.resize(GameConstants::RESOURCE_QUANTITY);
  489. cost[Res::WOOD] = 10;
  490. cost[Res::GOLD] = 1000;
  491. }
  492. IShipyard::IShipyard(const CGObjectInstance *O)
  493. : IBoatGenerator(O)
  494. {
  495. }
  496. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  497. {
  498. if(!obj)
  499. return nullptr;
  500. if(obj->ID == Obj::TOWN)
  501. {
  502. return static_cast<CGTownInstance*>(obj);
  503. }
  504. else if(obj->ID == Obj::SHIPYARD)
  505. {
  506. return static_cast<CGShipyard*>(obj);
  507. }
  508. else
  509. {
  510. return nullptr;
  511. }
  512. }
  513. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  514. {
  515. return castFrom(const_cast<CGObjectInstance*>(obj));
  516. }