2
0

CObjectHandler.cpp 14 KB

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