CTradeWindow.cpp 20 KB

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