CTradeWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * CTradeWindow.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 "CTradeWindow.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/CursorHandler.h"
  14. #include "../render/Canvas.h"
  15. #include "../gui/Shortcut.h"
  16. #include "../gui/WindowHandler.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/Slider.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../CGameInfo.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../../CCallback.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. #include "../../lib/CHeroHandler.h"
  25. #include "../../lib/mapObjects/CGHeroInstance.h"
  26. #include "../../lib/mapObjects/CGTownInstance.h"
  27. #include "../../lib/mapObjects/CGMarket.h"
  28. CTradeWindow::CTradeWindow(const ImagePath & bgName, const IMarket *Market, const CGHeroInstance *Hero, const std::function<void()> & onWindowClosed, EMarketMode Mode):
  29. CTradeBase(Market, Hero),
  30. CWindowObject(PLAYER_COLORED, bgName),
  31. onWindowClosed(onWindowClosed),
  32. readyToTrade(false)
  33. {
  34. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  35. mode = Mode;
  36. initTypes();
  37. }
  38. void CTradeWindow::initTypes()
  39. {
  40. switch(mode)
  41. {
  42. case EMarketMode::RESOURCE_RESOURCE:
  43. itemsType[1] = RESOURCE;
  44. itemsType[0] = RESOURCE;
  45. break;
  46. case EMarketMode::RESOURCE_PLAYER:
  47. itemsType[1] = RESOURCE;
  48. itemsType[0] = PLAYER;
  49. break;
  50. case EMarketMode::CREATURE_RESOURCE:
  51. itemsType[1] = CREATURE;
  52. itemsType[0] = RESOURCE;
  53. break;
  54. case EMarketMode::RESOURCE_ARTIFACT:
  55. itemsType[1] = RESOURCE;
  56. itemsType[0] = ARTIFACT_TYPE;
  57. break;
  58. case EMarketMode::ARTIFACT_RESOURCE:
  59. itemsType[1] = ARTIFACT_INSTANCE;
  60. itemsType[0] = RESOURCE;
  61. break;
  62. }
  63. }
  64. void CTradeWindow::initItems(bool Left)
  65. {
  66. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  67. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  68. {
  69. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  70. {
  71. auto item = std::make_shared<CTradeableItem>(Point(137, 469), itemsType[Left], -1, 1, 0);
  72. item->recActions &= ~(UPDATE | SHOWALL);
  73. items[Left].push_back(item);
  74. }
  75. }
  76. else
  77. {
  78. std::vector<int> *ids = getItemsIds(Left);
  79. std::vector<Rect> pos;
  80. int amount = -1;
  81. getPositionsFor(pos, Left, itemsType[Left]);
  82. if(Left || !ids)
  83. amount = 7;
  84. else
  85. amount = static_cast<int>(ids->size());
  86. if(ids)
  87. vstd::amin(amount, ids->size());
  88. for(int j=0; j<amount; j++)
  89. {
  90. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  91. if(id < 0 && mode != EMarketMode::ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  92. continue;
  93. auto item = std::make_shared<CTradeableItem>(pos[j].topLeft(), itemsType[Left], id, Left, j);
  94. item->pos = pos[j] + this->pos.topLeft();
  95. if(mode != EMarketMode::ARTIFACT_EXP)
  96. item->clickPressedCallback = [this](std::shared_ptr<CTradeableItem> altarSlot) -> void
  97. {
  98. if(altarSlot->left)
  99. {
  100. if(hLeft != altarSlot)
  101. hLeft = altarSlot;
  102. else
  103. return;
  104. }
  105. else
  106. {
  107. if(hRight != altarSlot)
  108. hRight = altarSlot;
  109. else
  110. return;
  111. }
  112. selectionChanged(altarSlot->left);
  113. };
  114. items[Left].push_back(item);
  115. }
  116. vstd::clear_pointer(ids);
  117. initSubs(Left);
  118. }
  119. }
  120. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  121. {
  122. std::vector<int> *ids = nullptr;
  123. if(Left)
  124. {
  125. switch(itemsType[1])
  126. {
  127. case CREATURE:
  128. ids = new std::vector<int>;
  129. for(int i = 0; i < 7; i++)
  130. {
  131. if(const CCreature *c = hero->getCreature(SlotID(i)))
  132. ids->push_back(c->getId());
  133. else
  134. ids->push_back(-1);
  135. }
  136. break;
  137. }
  138. }
  139. else
  140. {
  141. switch(itemsType[0])
  142. {
  143. case PLAYER:
  144. ids = new std::vector<int>;
  145. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  146. if(PlayerColor(i) != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(PlayerColor(i)) == EPlayerStatus::INGAME)
  147. ids->push_back(i);
  148. break;
  149. case ARTIFACT_TYPE:
  150. ids = new std::vector<int>;
  151. for (auto const & item : market->availableItemsIds(mode))
  152. ids->push_back(item.getNum());
  153. break;
  154. }
  155. }
  156. return ids;
  157. }
  158. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  159. {
  160. //seven boxes:
  161. // X X X
  162. // X X X
  163. // X
  164. int h = 0, w = 0, x = 0, y = 0, dx = 0, dy = 0;
  165. switch(type)
  166. {
  167. case RESOURCE:
  168. dx = 82;
  169. dy = 79;
  170. x = 39;
  171. y = 180;
  172. h = 68;
  173. w = 70;
  174. break;
  175. case PLAYER:
  176. dx = 83;
  177. dy = 118;
  178. h = 64;
  179. w = 58;
  180. x = 44;
  181. y = 83;
  182. assert(!Left);
  183. break;
  184. case CREATURE://45,123
  185. x = 45;
  186. y = 123;
  187. w = 58;
  188. h = 64;
  189. dx = 83;
  190. dy = 98;
  191. assert(Left);
  192. break;
  193. case ARTIFACT_TYPE://45,123
  194. x = 340 - 289;
  195. y = 180;
  196. w = 44;
  197. h = 44;
  198. dx = 83;
  199. dy = 79;
  200. break;
  201. }
  202. int leftToRightOffset = 289;
  203. const std::vector<Rect> tmp =
  204. {
  205. Rect(Point(x + 0 * dx, y + 0 * dx), Point(w, h) ),
  206. Rect(Point(x + 1 * dx, y + 0 * dx), Point(w, h) ),
  207. Rect(Point(x + 2 * dx, y + 0 * dx), Point(w, h) ),
  208. Rect(Point(x + 0 * dx, y + 1 * dy), Point(w, h) ),
  209. Rect(Point(x + 1 * dx, y + 1 * dy), Point(w, h) ),
  210. Rect(Point(x + 2 * dx, y + 1 * dy), Point(w, h) ),
  211. Rect(Point(x + 1 * dx, y + 2 * dy), Point(w, h) )
  212. };
  213. vstd::concatenate(poss, tmp);
  214. if(!Left)
  215. {
  216. for(Rect &r : poss)
  217. r.x += leftToRightOffset;
  218. }
  219. }
  220. void CTradeWindow::initSubs(bool Left)
  221. {
  222. for(auto item : items[Left])
  223. {
  224. if(Left)
  225. {
  226. switch(itemsType[1])
  227. {
  228. case CREATURE:
  229. item->subtitle = std::to_string(hero->getStackCount(SlotID(item->serial)));
  230. break;
  231. case RESOURCE:
  232. item->subtitle = std::to_string(LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(item->serial)));
  233. break;
  234. }
  235. }
  236. else //right side
  237. {
  238. if(itemsType[0] == PLAYER)
  239. {
  240. item->subtitle = CGI->generaltexth->capColors[item->id];
  241. }
  242. else if(hLeft)//artifact, creature
  243. {
  244. int h1, h2; //hlp variables for getting offer
  245. market->getOffer(hLeft->id, item->id, h1, h2, mode);
  246. if(item->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  247. {
  248. std::ostringstream oss;
  249. oss << h2;
  250. if(h1!=1)
  251. oss << "/" << h1;
  252. item->subtitle = oss.str();
  253. }
  254. else
  255. item->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  256. }
  257. else
  258. item->subtitle = "";
  259. }
  260. }
  261. }
  262. void CTradeWindow::showAll(Canvas & to)
  263. {
  264. CWindowObject::showAll(to);
  265. if(hRight)
  266. to.drawBorder(Rect::createAround(hRight->pos, 1), Colors::BRIGHT_YELLOW, 2);
  267. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  268. to.drawBorder(Rect::createAround(hLeft->pos, 1), Colors::BRIGHT_YELLOW, 2);
  269. if(readyToTrade)
  270. {
  271. if(hLeft)
  272. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), updateSlotSubtitle(true), to);
  273. if(hRight)
  274. hRight->showAllAt(pos.topLeft() + selectionOffset(false), updateSlotSubtitle(false), to);
  275. }
  276. }
  277. void CTradeWindow::close()
  278. {
  279. if (onWindowClosed)
  280. onWindowClosed();
  281. CWindowObject::close();
  282. }
  283. void CTradeWindow::setMode(EMarketMode Mode)
  284. {
  285. const IMarket *m = market;
  286. const CGHeroInstance *h = hero;
  287. const auto functor = onWindowClosed;
  288. onWindowClosed = nullptr; // don't call on closing of this window - pass it to next window
  289. close();
  290. switch(Mode)
  291. {
  292. case EMarketMode::CREATURE_EXP:
  293. case EMarketMode::ARTIFACT_EXP:
  294. break;
  295. default:
  296. GH.windows().createAndPushWindow<CMarketplaceWindow>(m, h, functor, Mode);
  297. break;
  298. }
  299. }
  300. void CTradeWindow::artifactSelected(CArtPlace * slot)
  301. {
  302. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  303. items[1][0]->setArtInstance(slot->getArt());
  304. if(slot->getArt())
  305. hLeft = items[1][0];
  306. else
  307. hLeft = nullptr;
  308. selectionChanged(true);
  309. }
  310. ImagePath CMarketplaceWindow::getBackgroundForMode(EMarketMode mode)
  311. {
  312. switch(mode)
  313. {
  314. case EMarketMode::RESOURCE_RESOURCE:
  315. return ImagePath::builtin("TPMRKRES.bmp");
  316. case EMarketMode::RESOURCE_PLAYER:
  317. return ImagePath::builtin("TPMRKPTS.bmp");
  318. case EMarketMode::CREATURE_RESOURCE:
  319. return ImagePath::builtin("TPMRKCRS.bmp");
  320. case EMarketMode::RESOURCE_ARTIFACT:
  321. return ImagePath::builtin("TPMRKABS.bmp");
  322. case EMarketMode::ARTIFACT_RESOURCE:
  323. return ImagePath::builtin("TPMRKASS.bmp");
  324. }
  325. assert(0);
  326. return {};
  327. }
  328. CMarketplaceWindow::CMarketplaceWindow(const IMarket * Market, const CGHeroInstance * Hero, const std::function<void()> & onWindowClosed, EMarketMode Mode)
  329. : CTradeWindow(getBackgroundForMode(Mode), Market, Hero, onWindowClosed, Mode)
  330. {
  331. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  332. madeTransaction = false;
  333. bool sliderNeeded = (mode != EMarketMode::RESOURCE_ARTIFACT && mode != EMarketMode::ARTIFACT_RESOURCE);
  334. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  335. std::string title;
  336. if(auto * o = dynamic_cast<const CGTownInstance *>(market))
  337. {
  338. switch (mode)
  339. {
  340. case EMarketMode::CREATURE_RESOURCE:
  341. title = (*CGI->townh)[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->getNameTranslated();
  342. break;
  343. case EMarketMode::RESOURCE_ARTIFACT:
  344. title = (*CGI->townh)[o->getFaction()]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated();
  345. break;
  346. case EMarketMode::ARTIFACT_RESOURCE:
  347. title = (*CGI->townh)[o->getFaction()]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated();
  348. // create image that copies part of background containing slot MISC_1 into position of slot MISC_5
  349. // this is workaround for bug in H3 files where this slot for ragdoll on this screen is missing
  350. images.push_back(std::make_shared<CPicture>(background->getSurface(), Rect(20, 187, 47, 47), 18, 339 ));
  351. break;
  352. default:
  353. title = CGI->generaltexth->allTexts[158];
  354. break;
  355. }
  356. }
  357. else if(auto * o = dynamic_cast<const CGMarket *>(market))
  358. {
  359. title = o->title;
  360. }
  361. titleLabel = std::make_shared<CLabel>(300, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, title);
  362. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  363. {
  364. arts = std::make_shared<CArtifactsOfHeroMarket>(Point(-361, 46));
  365. arts->selectArtCallback = std::bind(&CTradeWindow::artifactSelected, this, _1);
  366. arts->setHero(hero);
  367. addSetAndCallbacks(arts);
  368. }
  369. initItems(false);
  370. initItems(true);
  371. ok = std::make_shared<CButton>(Point(516, 520), AnimationPath::builtin("IOK6432.DEF"), CGI->generaltexth->zelp[600], [&](){ close(); }, EShortcut::GLOBAL_RETURN);
  372. deal = std::make_shared<CButton>(Point(307, 520), AnimationPath::builtin("TPMRKB.DEF"), CGI->generaltexth->zelp[595], [&](){ makeDeal(); } );
  373. deal->block(true);
  374. if(sliderNeeded)
  375. {
  376. slider = std::make_shared<CSlider>(Point(231, 490), 137, std::bind(&CMarketplaceWindow::sliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL);
  377. max = std::make_shared<CButton>(Point(229, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596], [&](){ setMax(); });
  378. max->block(true);
  379. }
  380. else
  381. {
  382. deal->moveBy(Point(-30, 0));
  383. }
  384. //left side
  385. switch(Mode)
  386. {
  387. case EMarketMode::RESOURCE_RESOURCE:
  388. case EMarketMode::RESOURCE_PLAYER:
  389. case EMarketMode::RESOURCE_ARTIFACT:
  390. labels.push_back(std::make_shared<CLabel>(154, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]));
  391. break;
  392. case EMarketMode::CREATURE_RESOURCE:
  393. //%s's Creatures
  394. labels.push_back(std::make_shared<CLabel>(152, 102, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  395. break;
  396. case EMarketMode::ARTIFACT_RESOURCE:
  397. //%s's Artifacts
  398. labels.push_back(std::make_shared<CLabel>(152, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->allTexts[271]) % hero->getNameTranslated())));
  399. break;
  400. }
  401. Rect traderTextRect;
  402. //right side
  403. switch(Mode)
  404. {
  405. case EMarketMode::RESOURCE_RESOURCE:
  406. case EMarketMode::CREATURE_RESOURCE:
  407. case EMarketMode::RESOURCE_ARTIFACT:
  408. case EMarketMode::ARTIFACT_RESOURCE:
  409. labels.push_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
  410. traderTextRect = Rect(316, 48, 260, 75);
  411. break;
  412. case EMarketMode::RESOURCE_PLAYER:
  413. labels.push_back(std::make_shared<CLabel>(445, 55, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]));
  414. traderTextRect = Rect(28, 48, 260, 75);
  415. break;
  416. }
  417. traderText = std::make_shared<CTextBox>("", traderTextRect, 0, FONT_SMALL, ETextAlignment::CENTER);
  418. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  419. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  420. buttons.push_back(std::make_shared<CButton>(Point(18, 520),AnimationPath::builtin("TPMRKBU1.DEF"), CGI->generaltexth->zelp[612], [&](){ setMode(EMarketMode::RESOURCE_PLAYER);}));
  421. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  422. buttons.push_back(std::make_shared<CButton>(Point(516, 450 + specialOffset),AnimationPath::builtin("TPMRKBU5.DEF"), CGI->generaltexth->zelp[605], [&](){ setMode(EMarketMode::RESOURCE_RESOURCE);}));
  423. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  424. buttons.push_back(std::make_shared<CButton>(Point(516, 485),AnimationPath::builtin("TPMRKBU4.DEF"), CGI->generaltexth->zelp[599], [&](){ setMode(EMarketMode::CREATURE_RESOURCE);}));
  425. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  426. buttons.push_back(std::make_shared<CButton>(Point(18, 450 + specialOffset),AnimationPath::builtin("TPMRKBU2.DEF"), CGI->generaltexth->zelp[598], [&](){ setMode(EMarketMode::RESOURCE_ARTIFACT);}));
  427. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  428. buttons.push_back(std::make_shared<CButton>(Point(18, 485),AnimationPath::builtin("TPMRKBU3.DEF"), CGI->generaltexth->zelp[613], [&](){ setMode(EMarketMode::ARTIFACT_RESOURCE);}));
  429. updateTraderText();
  430. }
  431. CMarketplaceWindow::~CMarketplaceWindow() = default;
  432. void CMarketplaceWindow::setMax()
  433. {
  434. slider->scrollToMax();
  435. }
  436. void CMarketplaceWindow::makeDeal()
  437. {
  438. int sliderValue = 0;
  439. if(slider)
  440. sliderValue = slider->getValue();
  441. else
  442. sliderValue = !deal->isBlocked(); //should always be 1
  443. if(!sliderValue)
  444. return;
  445. bool allowDeal = true;
  446. int leftIdToSend = hLeft->id;
  447. switch (mode)
  448. {
  449. case EMarketMode::CREATURE_RESOURCE:
  450. leftIdToSend = hLeft->serial;
  451. break;
  452. case EMarketMode::ARTIFACT_RESOURCE:
  453. leftIdToSend = hLeft->getArtInstance()->getId().getNum();
  454. break;
  455. case EMarketMode::RESOURCE_ARTIFACT:
  456. if(!ArtifactID(hRight->id).toArtifact()->canBePutAt(hero))
  457. {
  458. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.326"));
  459. allowDeal = false;
  460. }
  461. break;
  462. default:
  463. break;
  464. }
  465. if(allowDeal)
  466. {
  467. switch(mode)
  468. {
  469. case EMarketMode::RESOURCE_RESOURCE:
  470. LOCPLINT->cb->trade(market, mode, GameResID(leftIdToSend), GameResID(hRight->id), slider->getValue() * r1, hero);
  471. slider->scrollTo(0);
  472. break;
  473. case EMarketMode::CREATURE_RESOURCE:
  474. LOCPLINT->cb->trade(market, mode, SlotID(leftIdToSend), GameResID(hRight->id), slider->getValue() * r1, hero);
  475. slider->scrollTo(0);
  476. break;
  477. case EMarketMode::RESOURCE_PLAYER:
  478. LOCPLINT->cb->trade(market, mode, GameResID(leftIdToSend), PlayerColor(hRight->id), slider->getValue() * r1, hero);
  479. slider->scrollTo(0);
  480. break;
  481. case EMarketMode::RESOURCE_ARTIFACT:
  482. LOCPLINT->cb->trade(market, mode, GameResID(leftIdToSend), ArtifactID(hRight->id), r2, hero);
  483. break;
  484. case EMarketMode::ARTIFACT_RESOURCE:
  485. LOCPLINT->cb->trade(market, mode, ArtifactInstanceID(leftIdToSend), GameResID(hRight->id), r2, hero);
  486. break;
  487. }
  488. }
  489. madeTransaction = true;
  490. hLeft = nullptr;
  491. hRight = nullptr;
  492. selectionChanged(true);
  493. }
  494. void CMarketplaceWindow::sliderMoved( int to )
  495. {
  496. redraw();
  497. }
  498. void CMarketplaceWindow::selectionChanged(bool side)
  499. {
  500. readyToTrade = hLeft && hRight;
  501. if(mode == EMarketMode::RESOURCE_RESOURCE)
  502. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  503. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  504. arts->unmarkSlots();
  505. if(readyToTrade)
  506. {
  507. int soldItemId = hLeft->id;
  508. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  509. if(slider)
  510. {
  511. int newAmount = -1;
  512. if(itemsType[1] == RESOURCE)
  513. newAmount = LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(soldItemId));
  514. else if(itemsType[1] == CREATURE)
  515. newAmount = hero->getStackCount(SlotID(hLeft->serial)) - (hero->stacksCount() == 1 && hero->needsLastStack());
  516. else
  517. assert(0);
  518. slider->setAmount(newAmount / r1);
  519. slider->scrollTo(0);
  520. max->block(false);
  521. deal->block(false);
  522. }
  523. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  524. {
  525. deal->block(LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(soldItemId)) < r1);
  526. }
  527. else
  528. deal->block(false);
  529. }
  530. else
  531. {
  532. if(slider)
  533. {
  534. max->block(true);
  535. slider->setAmount(0);
  536. slider->scrollTo(0);
  537. }
  538. deal->block(true);
  539. }
  540. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  541. initSubs(false);
  542. updateTraderText();
  543. redraw();
  544. }
  545. bool CMarketplaceWindow::printButtonFor(EMarketMode M) const
  546. {
  547. if (!market->allowsTrade(M))
  548. return false;
  549. if (M == mode)
  550. return false;
  551. if ( M == EMarketMode::RESOURCE_RESOURCE || M == EMarketMode::RESOURCE_PLAYER)
  552. {
  553. auto * town = dynamic_cast<const CGTownInstance *>(market);
  554. if (town)
  555. return town->getOwner() == LOCPLINT->playerID;
  556. else
  557. return true;
  558. }
  559. else
  560. {
  561. return hero != nullptr;
  562. }
  563. }
  564. void CMarketplaceWindow::updateGarrison()
  565. {
  566. if(mode != EMarketMode::CREATURE_RESOURCE)
  567. return;
  568. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  569. getEmptySlots(toRemove);
  570. removeItems(toRemove);
  571. initSubs(true);
  572. }
  573. void CMarketplaceWindow::artifactsChanged(bool Left)
  574. {
  575. assert(!Left);
  576. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  577. return;
  578. std::vector<TradeItemBuy> available = market->availableItemsIds(mode);
  579. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  580. for(auto item : items[0])
  581. if(!vstd::contains(available, ArtifactID(item->id)))
  582. toRemove.insert(item);
  583. removeItems(toRemove);
  584. // clear set to erase final instance of shared_ptr - we want to redraw screen only after it has been deleted
  585. toRemove.clear();
  586. redraw();
  587. }
  588. std::string CMarketplaceWindow::updateSlotSubtitle(bool Left) const
  589. {
  590. if(Left)
  591. {
  592. switch(itemsType[1])
  593. {
  594. case RESOURCE:
  595. case CREATURE:
  596. {
  597. int val = slider
  598. ? slider->getValue() * r1
  599. : (((deal->isBlocked())) ? 0 : r1);
  600. return std::to_string(val);
  601. }
  602. case ARTIFACT_INSTANCE:
  603. return ((deal->isBlocked()) ? "0" : "1");
  604. }
  605. }
  606. else
  607. {
  608. switch(itemsType[0])
  609. {
  610. case RESOURCE:
  611. if(slider)
  612. return std::to_string( slider->getValue() * r2 );
  613. else
  614. return std::to_string(r2);
  615. case ARTIFACT_TYPE:
  616. return ((deal->isBlocked()) ? "0" : "1");
  617. case PLAYER:
  618. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  619. }
  620. }
  621. return "???";
  622. }
  623. Point CMarketplaceWindow::selectionOffset(bool Left) const
  624. {
  625. if(Left)
  626. {
  627. switch(itemsType[1])
  628. {
  629. case RESOURCE:
  630. return Point(122, 446);
  631. case CREATURE:
  632. return Point(128, 450);
  633. case ARTIFACT_INSTANCE:
  634. return Point(134, 466);
  635. }
  636. }
  637. else
  638. {
  639. switch(itemsType[0])
  640. {
  641. case RESOURCE:
  642. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  643. return Point(410, 469);
  644. else
  645. return Point(410, 446);
  646. case ARTIFACT_TYPE:
  647. return Point(425, 447);
  648. case PLAYER:
  649. return Point(417, 451);
  650. }
  651. }
  652. assert(0);
  653. return Point(0,0);
  654. }
  655. void CMarketplaceWindow::resourceChanged()
  656. {
  657. initSubs(true);
  658. }
  659. void CMarketplaceWindow::updateTraderText()
  660. {
  661. if(readyToTrade)
  662. {
  663. if(mode == EMarketMode::RESOURCE_PLAYER)
  664. {
  665. //I can give %s to the %s player.
  666. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  667. }
  668. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  669. {
  670. //I can offer you the %s for %d %s of %s.
  671. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  672. }
  673. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  674. {
  675. //I can offer you %d %s of %s for %d %s of %s.
  676. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[157]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  677. }
  678. else if(mode == EMarketMode::CREATURE_RESOURCE)
  679. {
  680. //I can offer you %d %s of %s for %d %s.
  681. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  682. }
  683. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  684. {
  685. //I can offer you %d %s of %s for your %s.
  686. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  687. }
  688. return;
  689. }
  690. int gnrtxtnr = -1;
  691. if(madeTransaction)
  692. {
  693. if(mode == EMarketMode::RESOURCE_PLAYER)
  694. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  695. else
  696. gnrtxtnr = 162; //You have received quite a bargain. I expect to make no profit on the deal. Can I interest you in any of my other wares?
  697. }
  698. else
  699. {
  700. if(mode == EMarketMode::RESOURCE_PLAYER)
  701. gnrtxtnr = 167; //If you'd like to give any of your resources to another player, click on the item you wish to give and to whom.
  702. else
  703. gnrtxtnr = 163; //Please inspect our fine wares. If you feel like offering a trade, click on the items you wish to trade with and for.
  704. }
  705. traderText->setText(CGI->generaltexth->allTexts[gnrtxtnr]);
  706. }