2
0

CTradeWindow.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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 "../widgets/Images.h"
  15. #include "../render/Canvas.h"
  16. #include "../gui/TextAlignment.h"
  17. #include "../gui/Shortcut.h"
  18. #include "../gui/WindowHandler.h"
  19. #include "../widgets/Buttons.h"
  20. #include "../widgets/Slider.h"
  21. #include "../widgets/TextControls.h"
  22. #include "../windows/InfoWindows.h"
  23. #include "../CGameInfo.h"
  24. #include "../CPlayerInterface.h"
  25. #include "../../CCallback.h"
  26. #include "../../lib/VCMI_Lib.h"
  27. #include "../../lib/CArtHandler.h"
  28. #include "../../lib/CCreatureHandler.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. #include "../../lib/CHeroHandler.h"
  31. #include "../../lib/mapObjects/CGHeroInstance.h"
  32. #include "../../lib/mapObjects/CGTownInstance.h"
  33. #include "../../lib/mapObjects/CGMarket.h"
  34. CTradeWindow::CTradeableItem::CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial)
  35. : CIntObject(LCLICK | HOVER | SHOW_POPUP, pos),
  36. type(EType(-1)),// set to invalid, will be corrected in setType
  37. id(ID),
  38. serial(Serial),
  39. left(Left)
  40. {
  41. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  42. downSelection = false;
  43. hlp = nullptr;
  44. setType(Type);
  45. }
  46. void CTradeWindow::CTradeableItem::setType(EType newType)
  47. {
  48. if(type != newType)
  49. {
  50. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  51. type = newType;
  52. if(getIndex() < 0)
  53. {
  54. image = std::make_shared<CAnimImage>(getFilename(), 0);
  55. image->disable();
  56. }
  57. else
  58. {
  59. image = std::make_shared<CAnimImage>(getFilename(), getIndex());
  60. }
  61. }
  62. }
  63. void CTradeWindow::CTradeableItem::setID(int newID)
  64. {
  65. if (id != newID)
  66. {
  67. id = newID;
  68. if (image)
  69. {
  70. int index = getIndex();
  71. if (index < 0)
  72. image->disable();
  73. else
  74. {
  75. image->enable();
  76. image->setFrame(index);
  77. }
  78. }
  79. }
  80. }
  81. AnimationPath CTradeWindow::CTradeableItem::getFilename()
  82. {
  83. switch(type)
  84. {
  85. case RESOURCE:
  86. return AnimationPath::builtin("RESOURCE");
  87. case PLAYER:
  88. return AnimationPath::builtin("CREST58");
  89. case ARTIFACT_TYPE:
  90. case ARTIFACT_PLACEHOLDER:
  91. case ARTIFACT_INSTANCE:
  92. return AnimationPath::builtin("artifact");
  93. case CREATURE:
  94. return AnimationPath::builtin("TWCRPORT");
  95. default:
  96. return {};
  97. }
  98. }
  99. int CTradeWindow::CTradeableItem::getIndex()
  100. {
  101. if (id < 0)
  102. return -1;
  103. switch(type)
  104. {
  105. case RESOURCE:
  106. case PLAYER:
  107. return id;
  108. case ARTIFACT_TYPE:
  109. case ARTIFACT_INSTANCE:
  110. case ARTIFACT_PLACEHOLDER:
  111. return CGI->artifacts()->getByIndex(id)->getIconIndex();
  112. case CREATURE:
  113. return CGI->creatures()->getByIndex(id)->getIconIndex();
  114. default:
  115. return -1;
  116. }
  117. }
  118. void CTradeWindow::CTradeableItem::showAll(Canvas & to)
  119. {
  120. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  121. assert(mw);
  122. Point posToBitmap;
  123. Point posToSubCenter;
  124. switch(type)
  125. {
  126. case RESOURCE:
  127. posToBitmap = Point(19,9);
  128. posToSubCenter = Point(36, 59);
  129. break;
  130. case CREATURE_PLACEHOLDER:
  131. case CREATURE:
  132. posToSubCenter = Point(29, 76);
  133. // Positing of unit count is different in Altar of Sacrifice and Freelancer's Guild
  134. if(mw->mode == EMarketMode::CREATURE_EXP && downSelection)
  135. posToSubCenter.y += 5;
  136. break;
  137. case PLAYER:
  138. posToSubCenter = Point(31, 76);
  139. break;
  140. case ARTIFACT_PLACEHOLDER:
  141. case ARTIFACT_INSTANCE:
  142. posToSubCenter = Point(19, 55);
  143. if(downSelection)
  144. posToSubCenter.y += 8;
  145. break;
  146. case ARTIFACT_TYPE:
  147. posToSubCenter = Point(19, 58);
  148. break;
  149. }
  150. if (image)
  151. {
  152. image->moveTo(pos.topLeft() + posToBitmap);
  153. CIntObject::showAll(to);
  154. }
  155. to.drawText(pos.topLeft() + posToSubCenter, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, subtitle);
  156. }
  157. void CTradeWindow::CTradeableItem::clickPressed(const Point & cursorPosition)
  158. {
  159. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  160. assert(mw);
  161. if(type == ARTIFACT_PLACEHOLDER)
  162. {
  163. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  164. const auto pickedArtInst = aw->getPickedArtifact();
  165. if(pickedArtInst)
  166. {
  167. aw->arts->pickedArtMoveToAltar(ArtifactPosition::TRANSITION_POS);
  168. aw->moveArtToAltar(this->shared_from_this(), pickedArtInst);
  169. }
  170. else if(const CArtifactInstance *art = getArtInstance())
  171. {
  172. const auto hero = aw->arts->getHero();
  173. const auto slot = hero->getSlotByInstance(art);
  174. assert(slot != ArtifactPosition::PRE_FIRST);
  175. LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero, slot),
  176. ArtifactLocation(hero, ArtifactPosition::TRANSITION_POS));
  177. aw->arts->pickedArtFromSlot = slot;
  178. aw->arts->artifactsOnAltar.erase(art);
  179. setID(-1);
  180. subtitle.clear();
  181. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  182. }
  183. aw->calcTotalExp();
  184. return;
  185. }
  186. if(left)
  187. {
  188. if(mw->hLeft != this->shared_from_this())
  189. mw->hLeft = this->shared_from_this();
  190. else
  191. return;
  192. }
  193. else
  194. {
  195. if(mw->hRight != this->shared_from_this())
  196. mw->hRight = this->shared_from_this();
  197. else
  198. return;
  199. }
  200. mw->selectionChanged(left);
  201. }
  202. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, Canvas & to)
  203. {
  204. Rect oldPos = pos;
  205. std::string oldSub = subtitle;
  206. downSelection = true;
  207. moveTo(dstPos);
  208. subtitle = customSub;
  209. showAll(to);
  210. downSelection = false;
  211. moveTo(oldPos.topLeft());
  212. subtitle = oldSub;
  213. }
  214. void CTradeWindow::CTradeableItem::hover(bool on)
  215. {
  216. if(!on)
  217. {
  218. GH.statusbar()->clear();
  219. return;
  220. }
  221. switch(type)
  222. {
  223. case CREATURE:
  224. case CREATURE_PLACEHOLDER:
  225. GH.statusbar()->write(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->objects[id]->getNamePluralTranslated()));
  226. break;
  227. case ARTIFACT_PLACEHOLDER:
  228. if(id < 0)
  229. GH.statusbar()->write(CGI->generaltexth->zelp[582].first);
  230. else
  231. GH.statusbar()->write(CGI->artifacts()->getByIndex(id)->getNameTranslated());
  232. break;
  233. }
  234. }
  235. void CTradeWindow::CTradeableItem::showPopupWindow(const Point & cursorPosition)
  236. {
  237. switch(type)
  238. {
  239. case CREATURE:
  240. case CREATURE_PLACEHOLDER:
  241. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->objects[id]->namePl));
  242. break;
  243. case ARTIFACT_TYPE:
  244. case ARTIFACT_PLACEHOLDER:
  245. //TODO: it's would be better for market to contain actual CArtifactInstance and not just ids of certain artifact type so we can use getEffectiveDescription.
  246. if(id >= 0)
  247. CRClickPopup::createAndPush(CGI->artifacts()->getByIndex(id)->getDescriptionTranslated());
  248. break;
  249. }
  250. }
  251. std::string CTradeWindow::CTradeableItem::getName(int number) const
  252. {
  253. switch(type)
  254. {
  255. case PLAYER:
  256. return CGI->generaltexth->capColors[id];
  257. case RESOURCE:
  258. return CGI->generaltexth->restypes[id];
  259. case CREATURE:
  260. if(number == 1)
  261. return CGI->creh->objects[id]->getNameSingularTranslated();
  262. else
  263. return CGI->creh->objects[id]->getNamePluralTranslated();
  264. case ARTIFACT_TYPE:
  265. case ARTIFACT_INSTANCE:
  266. return CGI->artifacts()->getByIndex(id)->getNameTranslated();
  267. }
  268. logGlobal->error("Invalid trade item type: %d", (int)type);
  269. return "";
  270. }
  271. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  272. {
  273. switch(type)
  274. {
  275. case ARTIFACT_PLACEHOLDER:
  276. case ARTIFACT_INSTANCE:
  277. return hlp;
  278. default:
  279. return nullptr;
  280. }
  281. }
  282. void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art)
  283. {
  284. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  285. hlp = art;
  286. if(art)
  287. setID(art->artType->getId());
  288. else
  289. setID(-1);
  290. }
  291. CTradeWindow::CTradeWindow(const ImagePath & bgName, const IMarket *Market, const CGHeroInstance *Hero, const std::function<void()> & onWindowClosed, EMarketMode Mode):
  292. CWindowObject(PLAYER_COLORED, bgName),
  293. market(Market),
  294. onWindowClosed(onWindowClosed),
  295. hero(Hero),
  296. readyToTrade(false)
  297. {
  298. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  299. mode = Mode;
  300. initTypes();
  301. }
  302. void CTradeWindow::initTypes()
  303. {
  304. switch(mode)
  305. {
  306. case EMarketMode::RESOURCE_RESOURCE:
  307. itemsType[1] = RESOURCE;
  308. itemsType[0] = RESOURCE;
  309. break;
  310. case EMarketMode::RESOURCE_PLAYER:
  311. itemsType[1] = RESOURCE;
  312. itemsType[0] = PLAYER;
  313. break;
  314. case EMarketMode::CREATURE_RESOURCE:
  315. itemsType[1] = CREATURE;
  316. itemsType[0] = RESOURCE;
  317. break;
  318. case EMarketMode::RESOURCE_ARTIFACT:
  319. itemsType[1] = RESOURCE;
  320. itemsType[0] = ARTIFACT_TYPE;
  321. break;
  322. case EMarketMode::ARTIFACT_RESOURCE:
  323. itemsType[1] = ARTIFACT_INSTANCE;
  324. itemsType[0] = RESOURCE;
  325. break;
  326. case EMarketMode::CREATURE_EXP:
  327. itemsType[1] = CREATURE;
  328. itemsType[0] = CREATURE_PLACEHOLDER;
  329. break;
  330. case EMarketMode::ARTIFACT_EXP:
  331. itemsType[1] = ARTIFACT_TYPE;
  332. itemsType[0] = ARTIFACT_PLACEHOLDER;
  333. break;
  334. }
  335. }
  336. void CTradeWindow::initItems(bool Left)
  337. {
  338. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  339. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  340. {
  341. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  342. {
  343. auto item = std::make_shared<CTradeableItem>(Point(137, 469), itemsType[Left], -1, 1, 0);
  344. item->recActions &= ~(UPDATE | SHOWALL);
  345. items[Left].push_back(item);
  346. }
  347. }
  348. else
  349. {
  350. std::vector<int> *ids = getItemsIds(Left);
  351. std::vector<Rect> pos;
  352. int amount = -1;
  353. getPositionsFor(pos, Left, itemsType[Left]);
  354. if(Left || !ids)
  355. amount = 7;
  356. else
  357. amount = static_cast<int>(ids->size());
  358. if(ids)
  359. vstd::amin(amount, ids->size());
  360. for(int j=0; j<amount; j++)
  361. {
  362. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  363. if(id < 0 && mode != EMarketMode::ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  364. continue;
  365. auto item = std::make_shared<CTradeableItem>(pos[j].topLeft(), itemsType[Left], id, Left, j);
  366. item->pos = pos[j] + this->pos.topLeft();
  367. items[Left].push_back(item);
  368. }
  369. vstd::clear_pointer(ids);
  370. initSubs(Left);
  371. }
  372. }
  373. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  374. {
  375. std::vector<int> *ids = nullptr;
  376. if(mode == EMarketMode::ARTIFACT_EXP)
  377. return new std::vector<int>(22, -1);
  378. if(Left)
  379. {
  380. switch(itemsType[1])
  381. {
  382. case CREATURE:
  383. ids = new std::vector<int>;
  384. for(int i = 0; i < 7; i++)
  385. {
  386. if(const CCreature *c = hero->getCreature(SlotID(i)))
  387. ids->push_back(c->getId());
  388. else
  389. ids->push_back(-1);
  390. }
  391. break;
  392. }
  393. }
  394. else
  395. {
  396. switch(itemsType[0])
  397. {
  398. case PLAYER:
  399. ids = new std::vector<int>;
  400. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  401. if(PlayerColor(i) != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(PlayerColor(i)) == EPlayerStatus::INGAME)
  402. ids->push_back(i);
  403. break;
  404. case ARTIFACT_TYPE:
  405. ids = new std::vector<int>(market->availableItemsIds(mode));
  406. break;
  407. }
  408. }
  409. return ids;
  410. }
  411. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  412. {
  413. if(mode == EMarketMode::ARTIFACT_EXP && !Left)
  414. {
  415. //22 boxes, 5 in row, last row: two boxes centered
  416. int h, w, x, y, dx, dy;
  417. h = w = 44;
  418. x = 317;
  419. y = 53;
  420. dx = 54;
  421. dy = 70;
  422. for (int i = 0; i < 4 ; i++)
  423. for (int j = 0; j < 5 ; j++)
  424. poss.push_back(Rect(x + dx*j, y + dy*i, w, h));
  425. poss.push_back(Rect((int)(x + dx * 1.5), (y + dy * 4), w, h));
  426. poss.push_back(Rect((int)(x + dx * 2.5), (y + dy * 4), w, h));
  427. }
  428. else
  429. {
  430. //seven boxes:
  431. // X X X
  432. // X X X
  433. // X
  434. int h, w, x, y, dx, dy;
  435. int leftToRightOffset;
  436. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  437. const std::vector<Rect> tmp =
  438. {
  439. Rect(Point(x + 0 * dx, y + 0 * dx), Point(w, h) ),
  440. Rect(Point(x + 1 * dx, y + 0 * dx), Point(w, h) ),
  441. Rect(Point(x + 2 * dx, y + 0 * dx), Point(w, h) ),
  442. Rect(Point(x + 0 * dx, y + 1 * dy), Point(w, h) ),
  443. Rect(Point(x + 1 * dx, y + 1 * dy), Point(w, h) ),
  444. Rect(Point(x + 2 * dx, y + 1 * dy), Point(w, h) ),
  445. Rect(Point(x + 1 * dx, y + 2 * dy), Point(w, h) )
  446. };
  447. vstd::concatenate(poss, tmp);
  448. if(!Left)
  449. {
  450. for(Rect &r : poss)
  451. r.x += leftToRightOffset;
  452. }
  453. }
  454. }
  455. void CTradeWindow::initSubs(bool Left)
  456. {
  457. for(auto item : items[Left])
  458. {
  459. if(Left)
  460. {
  461. switch(itemsType[1])
  462. {
  463. case CREATURE:
  464. item->subtitle = std::to_string(hero->getStackCount(SlotID(item->serial)));
  465. break;
  466. case RESOURCE:
  467. item->subtitle = std::to_string(LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(item->serial)));
  468. break;
  469. }
  470. }
  471. else //right side
  472. {
  473. if(itemsType[0] == PLAYER)
  474. {
  475. item->subtitle = CGI->generaltexth->capColors[item->id];
  476. }
  477. else if(hLeft)//artifact, creature
  478. {
  479. int h1, h2; //hlp variables for getting offer
  480. market->getOffer(hLeft->id, item->id, h1, h2, mode);
  481. if(item->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  482. {
  483. std::ostringstream oss;
  484. oss << h2;
  485. if(h1!=1)
  486. oss << "/" << h1;
  487. item->subtitle = oss.str();
  488. }
  489. else
  490. item->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  491. }
  492. else
  493. item->subtitle = "";
  494. }
  495. }
  496. }
  497. void CTradeWindow::showAll(Canvas & to)
  498. {
  499. CWindowObject::showAll(to);
  500. if(hRight)
  501. to.drawBorder(Rect::createAround(hRight->pos, 1), Colors::BRIGHT_YELLOW, 2);
  502. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  503. to.drawBorder(Rect::createAround(hLeft->pos, 1), Colors::BRIGHT_YELLOW, 2);
  504. if(readyToTrade)
  505. {
  506. if(hLeft)
  507. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  508. if(hRight)
  509. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  510. }
  511. }
  512. void CTradeWindow::close()
  513. {
  514. if (onWindowClosed)
  515. onWindowClosed();
  516. CWindowObject::close();
  517. }
  518. void CTradeWindow::removeItems(const std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  519. {
  520. for(auto item : toRemove)
  521. removeItem(item);
  522. }
  523. void CTradeWindow::removeItem(std::shared_ptr<CTradeableItem> item)
  524. {
  525. items[item->left] -= item;
  526. if(hRight == item)
  527. {
  528. hRight.reset();
  529. selectionChanged(false);
  530. }
  531. }
  532. void CTradeWindow::getEmptySlots(std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  533. {
  534. for(auto item : items[1])
  535. if(!hero->getStackCount(SlotID(item->serial)))
  536. toRemove.insert(item);
  537. }
  538. void CTradeWindow::setMode(EMarketMode Mode)
  539. {
  540. const IMarket *m = market;
  541. const CGHeroInstance *h = hero;
  542. const auto functor = onWindowClosed;
  543. onWindowClosed = nullptr; // don't call on closing of this window - pass it to next window
  544. close();
  545. switch(Mode)
  546. {
  547. case EMarketMode::CREATURE_EXP:
  548. case EMarketMode::ARTIFACT_EXP:
  549. GH.windows().createAndPushWindow<CAltarWindow>(m, h, functor, Mode);
  550. break;
  551. default:
  552. GH.windows().createAndPushWindow<CMarketplaceWindow>(m, h, functor, Mode);
  553. break;
  554. }
  555. }
  556. void CTradeWindow::artifactSelected(CHeroArtPlace *slot)
  557. {
  558. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  559. items[1][0]->setArtInstance(slot->getArt());
  560. if(slot->getArt())
  561. hLeft = items[1][0];
  562. else
  563. hLeft = nullptr;
  564. selectionChanged(true);
  565. }
  566. ImagePath CMarketplaceWindow::getBackgroundForMode(EMarketMode mode)
  567. {
  568. switch(mode)
  569. {
  570. case EMarketMode::RESOURCE_RESOURCE:
  571. return ImagePath::builtin("TPMRKRES.bmp");
  572. case EMarketMode::RESOURCE_PLAYER:
  573. return ImagePath::builtin("TPMRKPTS.bmp");
  574. case EMarketMode::CREATURE_RESOURCE:
  575. return ImagePath::builtin("TPMRKCRS.bmp");
  576. case EMarketMode::RESOURCE_ARTIFACT:
  577. return ImagePath::builtin("TPMRKABS.bmp");
  578. case EMarketMode::ARTIFACT_RESOURCE:
  579. return ImagePath::builtin("TPMRKASS.bmp");
  580. }
  581. assert(0);
  582. return {};
  583. }
  584. CMarketplaceWindow::CMarketplaceWindow(const IMarket * Market, const CGHeroInstance * Hero, const std::function<void()> & onWindowClosed, EMarketMode Mode)
  585. : CTradeWindow(getBackgroundForMode(Mode), Market, Hero, onWindowClosed, Mode)
  586. {
  587. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  588. madeTransaction = false;
  589. bool sliderNeeded = (mode != EMarketMode::RESOURCE_ARTIFACT && mode != EMarketMode::ARTIFACT_RESOURCE);
  590. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  591. std::string title;
  592. if(auto * o = dynamic_cast<const CGTownInstance *>(market))
  593. {
  594. switch (mode)
  595. {
  596. case EMarketMode::CREATURE_RESOURCE:
  597. title = (*CGI->townh)[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->getNameTranslated();
  598. break;
  599. case EMarketMode::RESOURCE_ARTIFACT:
  600. title = (*CGI->townh)[o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated();
  601. break;
  602. case EMarketMode::ARTIFACT_RESOURCE:
  603. title = (*CGI->townh)[o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated();
  604. // create image that copies part of background containing slot MISC_1 into position of slot MISC_5
  605. // this is workaround for bug in H3 files where this slot for ragdoll on this screen is missing
  606. images.push_back(std::make_shared<CPicture>(background->getSurface(), Rect(20, 187, 47, 47), 18, 339 ));
  607. break;
  608. default:
  609. title = CGI->generaltexth->allTexts[158];
  610. break;
  611. }
  612. }
  613. else if(auto * o = dynamic_cast<const CGMarket *>(market))
  614. {
  615. title = o->title;
  616. }
  617. titleLabel = std::make_shared<CLabel>(300, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, title);
  618. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  619. {
  620. arts = std::make_shared<CArtifactsOfHeroMarket>(Point(-361, 46));
  621. arts->selectArtCallback = std::bind(&CTradeWindow::artifactSelected, this, _1);
  622. arts->setHero(hero);
  623. addSetAndCallbacks(arts);
  624. }
  625. initItems(false);
  626. initItems(true);
  627. ok = std::make_shared<CButton>(Point(516, 520), AnimationPath::builtin("IOK6432.DEF"), CGI->generaltexth->zelp[600], [&](){ close(); }, EShortcut::GLOBAL_RETURN);
  628. deal = std::make_shared<CButton>(Point(307, 520), AnimationPath::builtin("TPMRKB.DEF"), CGI->generaltexth->zelp[595], [&](){ makeDeal(); } );
  629. deal->block(true);
  630. if(sliderNeeded)
  631. {
  632. slider = std::make_shared<CSlider>(Point(231, 490), 137, std::bind(&CMarketplaceWindow::sliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL);
  633. max = std::make_shared<CButton>(Point(229, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596], [&](){ setMax(); });
  634. max->block(true);
  635. }
  636. else
  637. {
  638. deal->moveBy(Point(-30, 0));
  639. }
  640. //left side
  641. switch(Mode)
  642. {
  643. case EMarketMode::RESOURCE_RESOURCE:
  644. case EMarketMode::RESOURCE_PLAYER:
  645. case EMarketMode::RESOURCE_ARTIFACT:
  646. labels.push_back(std::make_shared<CLabel>(154, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]));
  647. break;
  648. case EMarketMode::CREATURE_RESOURCE:
  649. //%s's Creatures
  650. 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())));
  651. break;
  652. case EMarketMode::ARTIFACT_RESOURCE:
  653. //%s's Artifacts
  654. 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())));
  655. break;
  656. }
  657. Rect traderTextRect;
  658. //right side
  659. switch(Mode)
  660. {
  661. case EMarketMode::RESOURCE_RESOURCE:
  662. case EMarketMode::CREATURE_RESOURCE:
  663. case EMarketMode::RESOURCE_ARTIFACT:
  664. case EMarketMode::ARTIFACT_RESOURCE:
  665. labels.push_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
  666. traderTextRect = Rect(316, 48, 260, 75);
  667. break;
  668. case EMarketMode::RESOURCE_PLAYER:
  669. labels.push_back(std::make_shared<CLabel>(445, 55, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]));
  670. traderTextRect = Rect(28, 48, 260, 75);
  671. break;
  672. }
  673. traderText = std::make_shared<CTextBox>("", traderTextRect, 0, FONT_SMALL, ETextAlignment::CENTER);
  674. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  675. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  676. buttons.push_back(std::make_shared<CButton>(Point(18, 520),AnimationPath::builtin("TPMRKBU1.DEF"), CGI->generaltexth->zelp[612], [&](){ setMode(EMarketMode::RESOURCE_PLAYER);}));
  677. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  678. buttons.push_back(std::make_shared<CButton>(Point(516, 450 + specialOffset),AnimationPath::builtin("TPMRKBU5.DEF"), CGI->generaltexth->zelp[605], [&](){ setMode(EMarketMode::RESOURCE_RESOURCE);}));
  679. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  680. buttons.push_back(std::make_shared<CButton>(Point(516, 485),AnimationPath::builtin("TPMRKBU4.DEF"), CGI->generaltexth->zelp[599], [&](){ setMode(EMarketMode::CREATURE_RESOURCE);}));
  681. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  682. buttons.push_back(std::make_shared<CButton>(Point(18, 450 + specialOffset),AnimationPath::builtin("TPMRKBU2.DEF"), CGI->generaltexth->zelp[598], [&](){ setMode(EMarketMode::RESOURCE_ARTIFACT);}));
  683. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  684. buttons.push_back(std::make_shared<CButton>(Point(18, 485),AnimationPath::builtin("TPMRKBU3.DEF"), CGI->generaltexth->zelp[613], [&](){ setMode(EMarketMode::ARTIFACT_RESOURCE);}));
  685. updateTraderText();
  686. }
  687. CMarketplaceWindow::~CMarketplaceWindow() = default;
  688. void CMarketplaceWindow::setMax()
  689. {
  690. slider->scrollToMax();
  691. }
  692. void CMarketplaceWindow::makeDeal()
  693. {
  694. int sliderValue = 0;
  695. if(slider)
  696. sliderValue = slider->getValue();
  697. else
  698. sliderValue = !deal->isBlocked(); //should always be 1
  699. if(!sliderValue)
  700. return;
  701. bool allowDeal = true;
  702. int leftIdToSend = hLeft->id;
  703. switch (mode)
  704. {
  705. case EMarketMode::CREATURE_RESOURCE:
  706. leftIdToSend = hLeft->serial;
  707. break;
  708. case EMarketMode::ARTIFACT_RESOURCE:
  709. leftIdToSend = hLeft->getArtInstance()->getId().getNum();
  710. break;
  711. case EMarketMode::RESOURCE_ARTIFACT:
  712. if(!ArtifactID(hRight->id).toArtifact()->canBePutAt(hero))
  713. {
  714. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.326"));
  715. allowDeal = false;
  716. }
  717. break;
  718. default:
  719. break;
  720. }
  721. if(allowDeal)
  722. {
  723. if(slider)
  724. {
  725. LOCPLINT->cb->trade(market, mode, leftIdToSend, hRight->id, slider->getValue() * r1, hero);
  726. slider->scrollTo(0);
  727. }
  728. else
  729. {
  730. LOCPLINT->cb->trade(market, mode, leftIdToSend, hRight->id, r2, hero);
  731. }
  732. }
  733. madeTransaction = true;
  734. hLeft = nullptr;
  735. hRight = nullptr;
  736. selectionChanged(true);
  737. }
  738. void CMarketplaceWindow::sliderMoved( int to )
  739. {
  740. redraw();
  741. }
  742. void CMarketplaceWindow::selectionChanged(bool side)
  743. {
  744. readyToTrade = hLeft && hRight;
  745. if(mode == EMarketMode::RESOURCE_RESOURCE)
  746. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  747. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  748. arts->unmarkSlots();
  749. if(readyToTrade)
  750. {
  751. int soldItemId = hLeft->id;
  752. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  753. if(slider)
  754. {
  755. int newAmount = -1;
  756. if(itemsType[1] == RESOURCE)
  757. newAmount = LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(soldItemId));
  758. else if(itemsType[1] == CREATURE)
  759. newAmount = hero->getStackCount(SlotID(hLeft->serial)) - (hero->stacksCount() == 1 && hero->needsLastStack());
  760. else
  761. assert(0);
  762. slider->setAmount(newAmount / r1);
  763. slider->scrollTo(0);
  764. max->block(false);
  765. deal->block(false);
  766. }
  767. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  768. {
  769. deal->block(LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(soldItemId)) < r1);
  770. }
  771. else
  772. deal->block(false);
  773. }
  774. else
  775. {
  776. if(slider)
  777. {
  778. max->block(true);
  779. slider->setAmount(0);
  780. slider->scrollTo(0);
  781. }
  782. deal->block(true);
  783. }
  784. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  785. initSubs(false);
  786. updateTraderText();
  787. redraw();
  788. }
  789. bool CMarketplaceWindow::printButtonFor(EMarketMode M) const
  790. {
  791. if (!market->allowsTrade(M))
  792. return false;
  793. if (M == mode)
  794. return false;
  795. if ( M == EMarketMode::RESOURCE_RESOURCE || M == EMarketMode::RESOURCE_PLAYER)
  796. {
  797. auto * town = dynamic_cast<const CGTownInstance *>(market);
  798. if (town)
  799. return town->getOwner() == LOCPLINT->playerID;
  800. else
  801. return true;
  802. }
  803. else
  804. {
  805. return hero != nullptr;
  806. }
  807. }
  808. void CMarketplaceWindow::garrisonChanged()
  809. {
  810. if(mode != EMarketMode::CREATURE_RESOURCE)
  811. return;
  812. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  813. getEmptySlots(toRemove);
  814. removeItems(toRemove);
  815. initSubs(true);
  816. }
  817. void CMarketplaceWindow::artifactsChanged(bool Left)
  818. {
  819. assert(!Left);
  820. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  821. return;
  822. std::vector<int> available = market->availableItemsIds(mode);
  823. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  824. for(auto item : items[0])
  825. if(!vstd::contains(available, item->id))
  826. toRemove.insert(item);
  827. removeItems(toRemove);
  828. // clear set to erase final instance of shared_ptr - we want to redraw screen only after it has been deleted
  829. toRemove.clear();
  830. redraw();
  831. }
  832. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  833. {
  834. if(Left)
  835. {
  836. switch(itemsType[1])
  837. {
  838. case RESOURCE:
  839. case CREATURE:
  840. {
  841. int val = slider
  842. ? slider->getValue() * r1
  843. : (((deal->isBlocked())) ? 0 : r1);
  844. return std::to_string(val);
  845. }
  846. case ARTIFACT_INSTANCE:
  847. return ((deal->isBlocked()) ? "0" : "1");
  848. }
  849. }
  850. else
  851. {
  852. switch(itemsType[0])
  853. {
  854. case RESOURCE:
  855. if(slider)
  856. return std::to_string( slider->getValue() * r2 );
  857. else
  858. return std::to_string(r2);
  859. case ARTIFACT_TYPE:
  860. return ((deal->isBlocked()) ? "0" : "1");
  861. case PLAYER:
  862. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  863. }
  864. }
  865. return "???";
  866. }
  867. Point CMarketplaceWindow::selectionOffset(bool Left) const
  868. {
  869. if(Left)
  870. {
  871. switch(itemsType[1])
  872. {
  873. case RESOURCE:
  874. return Point(122, 446);
  875. case CREATURE:
  876. return Point(128, 450);
  877. case ARTIFACT_INSTANCE:
  878. return Point(134, 466);
  879. }
  880. }
  881. else
  882. {
  883. switch(itemsType[0])
  884. {
  885. case RESOURCE:
  886. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  887. return Point(410, 469);
  888. else
  889. return Point(410, 446);
  890. case ARTIFACT_TYPE:
  891. return Point(425, 447);
  892. case PLAYER:
  893. return Point(417, 451);
  894. }
  895. }
  896. assert(0);
  897. return Point(0,0);
  898. }
  899. void CMarketplaceWindow::resourceChanged()
  900. {
  901. initSubs(true);
  902. }
  903. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  904. {
  905. switch(type)
  906. {
  907. case RESOURCE:
  908. dx = 82;
  909. dy = 79;
  910. x = 39;
  911. y = 180;
  912. h = 68;
  913. w = 70;
  914. break;
  915. case PLAYER:
  916. dx = 83;
  917. dy = 118;
  918. h = 64;
  919. w = 58;
  920. x = 44;
  921. y = 83;
  922. assert(Right);
  923. break;
  924. case CREATURE://45,123
  925. x = 45;
  926. y = 123;
  927. w = 58;
  928. h = 64;
  929. dx = 83;
  930. dy = 98;
  931. assert(!Right);
  932. break;
  933. case ARTIFACT_TYPE://45,123
  934. x = 340-289;
  935. y = 180;
  936. w = 44;
  937. h = 44;
  938. dx = 83;
  939. dy = 79;
  940. break;
  941. }
  942. leftToRightOffset = 289;
  943. }
  944. void CMarketplaceWindow::updateTraderText()
  945. {
  946. if(readyToTrade)
  947. {
  948. if(mode == EMarketMode::RESOURCE_PLAYER)
  949. {
  950. //I can give %s to the %s player.
  951. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  952. }
  953. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  954. {
  955. //I can offer you the %s for %d %s of %s.
  956. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  957. }
  958. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  959. {
  960. //I can offer you %d %s of %s for %d %s of %s.
  961. 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()));
  962. }
  963. else if(mode == EMarketMode::CREATURE_RESOURCE)
  964. {
  965. //I can offer you %d %s of %s for %d %s.
  966. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  967. }
  968. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  969. {
  970. //I can offer you %d %s of %s for your %s.
  971. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  972. }
  973. return;
  974. }
  975. int gnrtxtnr = -1;
  976. if(madeTransaction)
  977. {
  978. if(mode == EMarketMode::RESOURCE_PLAYER)
  979. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  980. else
  981. 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?
  982. }
  983. else
  984. {
  985. if(mode == EMarketMode::RESOURCE_PLAYER)
  986. 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.
  987. else
  988. 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.
  989. }
  990. traderText->setText(CGI->generaltexth->allTexts[gnrtxtnr]);
  991. }
  992. CAltarWindow::CAltarWindow(const IMarket * Market, const CGHeroInstance * Hero, const std::function<void()> & onWindowClosed, EMarketMode Mode)
  993. : CTradeWindow(ImagePath::builtin(Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"), Market, Hero, onWindowClosed, Mode)
  994. {
  995. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  996. if(Mode == EMarketMode::CREATURE_EXP)
  997. {
  998. //%s's Creatures
  999. labels.push_back(std::make_shared<CLabel>(155, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW,
  1000. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  1001. //Altar of Sacrifice
  1002. labels.push_back(std::make_shared<CLabel>(450, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]));
  1003. //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  1004. new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW);
  1005. slider = std::make_shared<CSlider>(Point(231, 481), 137, std::bind(&CAltarWindow::sliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL);
  1006. max = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[578], std::bind(&CSlider::scrollToMax, slider));
  1007. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  1008. sacrificeAll = std::make_shared<CButton>(Point(393, 520), AnimationPath::builtin("ALTARMY.DEF"), CGI->generaltexth->zelp[579], std::bind(&CAltarWindow::SacrificeAll,this));
  1009. initItems(true);
  1010. mimicCres();
  1011. }
  1012. else
  1013. {
  1014. //Sacrifice artifacts for experience
  1015. labels.push_back(std::make_shared<CLabel>(450, 34, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]));
  1016. //%s's Creatures
  1017. labels.push_back(std::make_shared<CLabel>(302, 423, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]));
  1018. sacrificeAll = std::make_shared<CButton>(Point(393, 520), AnimationPath::builtin("ALTFILL.DEF"), CGI->generaltexth->zelp[571], std::bind(&CAltarWindow::SacrificeAll,this));
  1019. sacrificeAll->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  1020. sacrificeBackpack = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("ALTEMBK.DEF"), CGI->generaltexth->zelp[570], std::bind(&CAltarWindow::SacrificeBackpack,this));
  1021. sacrificeBackpack->block(hero->artifactsInBackpack.empty());
  1022. arts = std::make_shared<CArtifactsOfHeroAltar>(Point(-365, -12));
  1023. arts->setHero(hero);
  1024. addSetAndCallbacks(arts);
  1025. initItems(true);
  1026. initItems(false);
  1027. artIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("ARTIFACT"), 0, 0, 281, 442);
  1028. artIcon->disable();
  1029. }
  1030. //Experience needed to reach next level
  1031. texts.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  1032. //Total experience on the Altar
  1033. texts.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  1034. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1035. ok = std::make_shared<CButton>(Point(516, 520), AnimationPath::builtin("IOK6432.DEF"), CGI->generaltexth->zelp[568], [&](){ close();}, EShortcut::GLOBAL_RETURN);
  1036. deal = std::make_shared<CButton>(Point(269, 520), AnimationPath::builtin("ALTSACR.DEF"), CGI->generaltexth->zelp[585], std::bind(&CAltarWindow::makeDeal,this));
  1037. if(Mode == EMarketMode::CREATURE_EXP)
  1038. {
  1039. auto changeMode = std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTART.DEF"), CGI->generaltexth->zelp[580], std::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP));
  1040. if(Hero->getAlignment() == ::EAlignment::EVIL)
  1041. changeMode->block(true);
  1042. buttons.push_back(changeMode);
  1043. }
  1044. else if(Mode == EMarketMode::ARTIFACT_EXP)
  1045. {
  1046. auto changeMode = std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTSACC.DEF"), CGI->generaltexth->zelp[572], std::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP));
  1047. if(Hero->getAlignment() == ::EAlignment::GOOD)
  1048. changeMode->block(true);
  1049. buttons.push_back(changeMode);
  1050. }
  1051. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  1052. getExpValues();
  1053. expToLevel = std::make_shared<CLabel>(73, 475, FONT_SMALL, ETextAlignment::CENTER);
  1054. expOnAltar = std::make_shared<CLabel>(73, 543, FONT_SMALL, ETextAlignment::CENTER);
  1055. setExpToLevel();
  1056. calcTotalExp();
  1057. blockTrade();
  1058. }
  1059. CAltarWindow::~CAltarWindow() = default;
  1060. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  1061. {
  1062. leftToRightOffset = 289;
  1063. x = 45;
  1064. y = 110;
  1065. w = 58;
  1066. h = 64;
  1067. dx = 83;
  1068. dy = 98;
  1069. }
  1070. void CAltarWindow::sliderMoved(int to)
  1071. {
  1072. if(hLeft)
  1073. sacrificedUnits[hLeft->serial] = to;
  1074. if(hRight)
  1075. updateRight(hRight);
  1076. deal->block(!to);
  1077. calcTotalExp();
  1078. redraw();
  1079. }
  1080. void CAltarWindow::makeDeal()
  1081. {
  1082. if(mode == EMarketMode::CREATURE_EXP)
  1083. {
  1084. blockTrade();
  1085. slider->scrollTo(0);
  1086. std::vector<ui32> ids;
  1087. std::vector<ui32> toSacrifice;
  1088. for(int i = 0; i < sacrificedUnits.size(); i++)
  1089. {
  1090. if(sacrificedUnits[i])
  1091. {
  1092. ids.push_back(i);
  1093. toSacrifice.push_back(sacrificedUnits[i]);
  1094. }
  1095. }
  1096. LOCPLINT->cb->trade(market, mode, ids, {}, toSacrifice, hero);
  1097. for(int& val : sacrificedUnits)
  1098. val = 0;
  1099. for(auto item : items[0])
  1100. {
  1101. item->setType(CREATURE_PLACEHOLDER);
  1102. item->subtitle = "";
  1103. }
  1104. }
  1105. else
  1106. {
  1107. std::vector<ui32> positions;
  1108. for(const CArtifactInstance * art : arts->artifactsOnAltar)
  1109. {
  1110. positions.push_back(hero->getSlotByInstance(art));
  1111. }
  1112. std::sort(positions.begin(), positions.end(), std::greater<>());
  1113. LOCPLINT->cb->trade(market, mode, positions, {}, {}, hero);
  1114. arts->artifactsOnAltar.clear();
  1115. for(auto item : items[0])
  1116. {
  1117. item->setID(-1);
  1118. item->subtitle = "";
  1119. }
  1120. //arts->scrollBackpack(0);
  1121. deal->block(true);
  1122. }
  1123. calcTotalExp();
  1124. }
  1125. void CAltarWindow::SacrificeAll()
  1126. {
  1127. if(mode == EMarketMode::CREATURE_EXP)
  1128. {
  1129. bool movedAnything = false;
  1130. for(auto item : items[1])
  1131. sacrificedUnits[item->serial] = hero->getStackCount(SlotID(item->serial));
  1132. sacrificedUnits[items[1].front()->serial]--;
  1133. for(auto item : items[0])
  1134. {
  1135. updateRight(item);
  1136. if(item->type == CREATURE)
  1137. movedAnything = true;
  1138. }
  1139. deal->block(!movedAnything);
  1140. calcTotalExp();
  1141. }
  1142. else
  1143. {
  1144. std::vector<ConstTransitivePtr<CArtifactInstance>> artsForMove;
  1145. for(const auto& slotInfo : arts->visibleArtSet.artifactsWorn)
  1146. {
  1147. if(!slotInfo.second.locked && slotInfo.second.artifact->artType->isTradable())
  1148. artsForMove.push_back(slotInfo.second.artifact);
  1149. }
  1150. for(auto artInst : artsForMove)
  1151. moveArtToAltar(nullptr, artInst);
  1152. arts->updateWornSlots();
  1153. SacrificeBackpack();
  1154. }
  1155. redraw();
  1156. }
  1157. void CAltarWindow::selectionChanged(bool side)
  1158. {
  1159. if(mode != EMarketMode::CREATURE_EXP)
  1160. return;
  1161. int stackCount = 0;
  1162. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  1163. if(hero->getStackCount(SlotID(i)) > sacrificedUnits[i])
  1164. stackCount++;
  1165. slider->setAmount(hero->getStackCount(SlotID(hLeft->serial)) - (stackCount == 1));
  1166. slider->block(!slider->getAmount());
  1167. slider->scrollTo(sacrificedUnits[hLeft->serial]);
  1168. max->block(!slider->getAmount());
  1169. selectOppositeItem(side);
  1170. readyToTrade = true;
  1171. redraw();
  1172. }
  1173. void CAltarWindow::selectOppositeItem(bool side)
  1174. {
  1175. bool oppositeSide = !side;
  1176. int pos = vstd::find_pos(items[side], side ? hLeft : hRight);
  1177. int oppositePos = vstd::find_pos(items[oppositeSide], oppositeSide ? hLeft : hRight);
  1178. if(pos >= 0 && pos != oppositePos)
  1179. {
  1180. if(oppositeSide)
  1181. hLeft = items[oppositeSide][pos];
  1182. else
  1183. hRight = items[oppositeSide][pos];
  1184. selectionChanged(oppositeSide);
  1185. }
  1186. }
  1187. void CAltarWindow::mimicCres()
  1188. {
  1189. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  1190. std::vector<Rect> positions;
  1191. getPositionsFor(positions, false, CREATURE);
  1192. for(auto item : items[1])
  1193. {
  1194. auto hlp = std::make_shared<CTradeableItem>(positions[item->serial].topLeft(), CREATURE_PLACEHOLDER, item->id, false, item->serial);
  1195. hlp->pos = positions[item->serial] + this->pos.topLeft();
  1196. items[0].push_back(hlp);
  1197. }
  1198. }
  1199. Point CAltarWindow::selectionOffset(bool Left) const
  1200. {
  1201. if(Left)
  1202. return Point(150, 421);
  1203. else
  1204. return Point(396, 421);
  1205. }
  1206. std::string CAltarWindow::selectionSubtitle(bool Left) const
  1207. {
  1208. if(Left && slider && hLeft)
  1209. return std::to_string(slider->getValue());
  1210. else if(!Left && hRight)
  1211. return hRight->subtitle;
  1212. else
  1213. return "";
  1214. }
  1215. void CAltarWindow::artifactsChanged(bool left)
  1216. {
  1217. }
  1218. void CAltarWindow::garrisonChanged()
  1219. {
  1220. if(mode != EMarketMode::CREATURE_EXP)
  1221. return;
  1222. std::set<std::shared_ptr<CTradeableItem>> empty;
  1223. getEmptySlots(empty);
  1224. removeItems(empty);
  1225. initSubs(true);
  1226. getExpValues();
  1227. }
  1228. void CAltarWindow::getExpValues()
  1229. {
  1230. int dump;
  1231. for(auto item : items[1])
  1232. {
  1233. if(item->id >= 0)
  1234. market->getOffer(item->id, 0, dump, expPerUnit[item->serial], EMarketMode::CREATURE_EXP);
  1235. }
  1236. }
  1237. void CAltarWindow::calcTotalExp()
  1238. {
  1239. int val = 0;
  1240. if(mode == EMarketMode::CREATURE_EXP)
  1241. {
  1242. for (int i = 0; i < sacrificedUnits.size(); i++)
  1243. {
  1244. val += expPerUnit[i] * sacrificedUnits[i];
  1245. }
  1246. }
  1247. else
  1248. {
  1249. auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
  1250. for(const CArtifactInstance * art : artifactsOfHero->artifactsOnAltar)
  1251. {
  1252. int dmp, valOfArt;
  1253. market->getOffer(art->artType->getId(), 0, dmp, valOfArt, mode);
  1254. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  1255. }
  1256. }
  1257. val = static_cast<int>(hero->calculateXp(val));
  1258. expOnAltar->setText(std::to_string(val));
  1259. }
  1260. void CAltarWindow::setExpToLevel()
  1261. {
  1262. expToLevel->setText(std::to_string(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  1263. }
  1264. void CAltarWindow::blockTrade()
  1265. {
  1266. hLeft = hRight = nullptr;
  1267. readyToTrade = false;
  1268. if(slider)
  1269. {
  1270. slider->block(true);
  1271. max->block(true);
  1272. }
  1273. deal->block(true);
  1274. }
  1275. void CAltarWindow::updateRight(std::shared_ptr<CTradeableItem> toUpdate)
  1276. {
  1277. int val = sacrificedUnits[toUpdate->serial];
  1278. toUpdate->setType(val ? CREATURE : CREATURE_PLACEHOLDER);
  1279. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % std::to_string(hero->calculateXp(val * expPerUnit[toUpdate->serial]))) : ""; //%s exp
  1280. }
  1281. int CAltarWindow::firstFreeSlot()
  1282. {
  1283. int ret = -1;
  1284. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  1285. return items[0][ret]->id == -1 ? ret : -1;
  1286. }
  1287. void CAltarWindow::SacrificeBackpack()
  1288. {
  1289. while(!arts->visibleArtSet.artifactsInBackpack.empty())
  1290. {
  1291. if(!putOnAltar(nullptr, arts->visibleArtSet.artifactsInBackpack[0].artifact))
  1292. break;
  1293. };
  1294. calcTotalExp();
  1295. }
  1296. void CAltarWindow::artifactPicked()
  1297. {
  1298. redraw();
  1299. }
  1300. void CAltarWindow::showAll(Canvas & to)
  1301. {
  1302. CTradeWindow::showAll(to);
  1303. if(mode == EMarketMode::ARTIFACT_EXP && arts)
  1304. {
  1305. if(auto pickedArt = arts->getPickedArtifact())
  1306. {
  1307. artIcon->setFrame(pickedArt->artType->getIconIndex());
  1308. artIcon->showAll(to);
  1309. int dmp, val;
  1310. market->getOffer(pickedArt->getTypeId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1311. val = static_cast<int>(hero->calculateXp(val));
  1312. to.drawText(Point(304, 498), FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, std::to_string(val));
  1313. }
  1314. }
  1315. }
  1316. bool CAltarWindow::putOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
  1317. {
  1318. if(!art->artType->isTradable()) //special art
  1319. {
  1320. logGlobal->warn("Cannot put special artifact on altar!");
  1321. return false;
  1322. }
  1323. if(!altarSlot || altarSlot->id != -1)
  1324. {
  1325. int slotIndex = firstFreeSlot();
  1326. if(slotIndex < 0)
  1327. {
  1328. logGlobal->warn("No free slots on altar!");
  1329. return false;
  1330. }
  1331. altarSlot = items[0][slotIndex];
  1332. }
  1333. int dmp, val;
  1334. market->getOffer(art->artType->getId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1335. val = static_cast<int>(hero->calculateXp(val));
  1336. arts->artifactsOnAltar.insert(art);
  1337. arts->deleteFromVisible(art);
  1338. altarSlot->setArtInstance(art);
  1339. altarSlot->subtitle = std::to_string(val);
  1340. deal->block(false);
  1341. return true;
  1342. }
  1343. void CAltarWindow::moveArtToAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
  1344. {
  1345. if(putOnAltar(altarSlot, art))
  1346. {
  1347. CCS->curh->dragAndDropCursor(nullptr);
  1348. arts->unmarkSlots();
  1349. }
  1350. }